Skip to content

Commit 0a8763e

Browse files
sraikund16facebook-github-bot
authored andcommitted
Align Roctracer to TSC Clock (#991)
Summary: Pull Request resolved: #991 Right now we align Roctracer events to system clock blindly regardless of what we are using in torch.profiler. We should use a clock based on what is defined instead. This wont fix overlapping kernel events since we do a static offset when aligning but it will help make sure that kernel events always happen after kernel launches Reviewed By: aaronenyeshi, briancoutinho Differential Revision: D62984793 fbshipit-source-id: 4495a83de98dc3fb752754898588b93f4850e7a4
1 parent 45cc65a commit 0a8763e

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

libkineto/src/CuptiActivityProfiler.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ std::function<time_t(approx_time_t)>& get_time_converter() {
103103
};
104104
return _time_converter;
105105
}
106+
#ifdef HAS_ROCTRACER
107+
timestamp_t getTimeOffset() {
108+
int64_t t0, t00;
109+
timespec t1;
110+
t0 = libkineto::getApproximateTime();
111+
clock_gettime(CLOCK_MONOTONIC, &t1);
112+
t00 = libkineto::getApproximateTime();
113+
114+
// Confvert to ns (if necessary)
115+
t0 = libkineto::get_time_converter()(t0);
116+
t00 = libkineto::get_time_converter()(t00);
117+
118+
// Our stored timestamps (from roctracer and generated) are in CLOCK_MONOTONIC domain (in ns).
119+
return (t0 >> 1) + (t00 >> 1) - timespec_to_ns(t1);
120+
}
121+
#endif
106122

107123
#ifdef HAS_CUPTI
108124
bool& use_cupti_tsc() {
@@ -340,6 +356,8 @@ void CuptiActivityProfiler::processTraceInternal(ActivityLogger& logger) {
340356
#ifdef HAS_ROCTRACER
341357
if (!cpuOnly_) {
342358
VLOG(0) << "Retrieving GPU activity buffers";
359+
timestamp_t offset = getTimeOffset();
360+
cupti_.setTimeOffset(offset);
343361
const int count = cupti_.processActivities(
344362
std::bind(&CuptiActivityProfiler::handleRoctracerActivity, this, std::placeholders::_1, &logger),
345363
std::bind(&CuptiActivityProfiler::handleCorrelationActivity, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

libkineto/src/RoctracerActivityApi.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
#include "RoctracerActivityApi.h"
1010

11+
#include "ApproximateClock.h"
1112
#include <cstring>
1213
#include <chrono>
1314
#include <functional>
1415
#include <time.h>
15-
16+
#include "Logger.h"
1617
#include "Demangle.h"
1718
#include "output_base.h"
1819
#include "ThreadUtil.h"
@@ -65,20 +66,16 @@ inline bool RoctracerActivityApi::isLogged(libkineto::ActivityType atype) {
6566
return activityMaskSnapshot_ & (1 << static_cast<uint32_t>(atype));
6667
}
6768

69+
void RoctracerActivityApi::setTimeOffset(timestamp_t toffset) {
70+
toffset_ = toffset;
71+
}
72+
6873
int RoctracerActivityApi::processActivities(
6974
std::function<void(const roctracerBase*)> handler,
7075
std::function<void(uint64_t, uint64_t, RoctracerLogger::CorrelationDomain)> correlationHandler) {
7176
// Find offset to map from monotonic clock to system clock.
7277
// This will break time-ordering of events but is status quo.
7378

74-
timespec t0, t1, t00;
75-
clock_gettime(CLOCK_REALTIME, &t0);
76-
clock_gettime(CLOCK_MONOTONIC, &t1);
77-
clock_gettime(CLOCK_REALTIME, &t00);
78-
79-
const timestamp_t toffset = (timespec_to_ns(t0) >> 1) + (timespec_to_ns(t00) >> 1) - timespec_to_ns(t1);
80-
// Our stored timestamps (from roctracer and generated) are in CLOCK_MONOTONIC domain (in ns).
81-
8279
int count = 0;
8380

8481
// Process all external correlations pairs
@@ -125,8 +122,8 @@ int RoctracerActivityApi::processActivities(
125122
}
126123
if (!filtered) {
127124
// Convert the begin and end timestamps from monotonic clock to system clock.
128-
item->begin = item->begin + toffset;
129-
item->end = item->end + toffset;
125+
item->begin = item->begin + toffset_;
126+
item->end = item->end + toffset_;
130127
handler(item);
131128
++count;
132129
}

libkineto/src/RoctracerActivityApi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class RoctracerActivityApi {
5252
const std::set<ActivityType>& selected_activities);
5353
void clearActivities();
5454
void teardownContext() {}
55+
void setTimeOffset(timestamp_t toffset);
5556

5657
virtual int processActivities(
5758
std::function<void(const roctracerBase*)> handler,
@@ -63,6 +64,7 @@ class RoctracerActivityApi {
6364

6465
private:
6566
bool registered_{false};
67+
timestamp_t toffset_{0};
6668

6769
// Enabled Activity Filters
6870
uint32_t activityMask_{0};

0 commit comments

Comments
 (0)