Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align Roctracer to TSC Clock #991

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions libkineto/src/CuptiActivityProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ std::function<time_t(approx_time_t)>& get_time_converter() {
};
return _time_converter;
}
#ifdef HAS_ROCTRACER
timestamp_t getTimeOffset() {
int64_t t0, t00;
timespec t1;
t0 = libkineto::getApproximateTime();
clock_gettime(CLOCK_MONOTONIC, &t1);
t00 = libkineto::getApproximateTime();

// Confvert to ns (if necessary)
t0 = libkineto::get_time_converter()(t0);
t00 = libkineto::get_time_converter()(t00);

// Our stored timestamps (from roctracer and generated) are in CLOCK_MONOTONIC domain (in ns).
return (t0 >> 1) + (t00 >> 1) - timespec_to_ns(t1);
}
#endif

#ifdef HAS_CUPTI
bool& use_cupti_tsc() {
Expand Down Expand Up @@ -340,6 +356,8 @@ void CuptiActivityProfiler::processTraceInternal(ActivityLogger& logger) {
#ifdef HAS_ROCTRACER
if (!cpuOnly_) {
VLOG(0) << "Retrieving GPU activity buffers";
timestamp_t offset = getTimeOffset();
cupti_.setTimeOffset(offset);
const int count = cupti_.processActivities(
std::bind(&CuptiActivityProfiler::handleRoctracerActivity, this, std::placeholders::_1, &logger),
std::bind(&CuptiActivityProfiler::handleCorrelationActivity, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
Expand Down
19 changes: 8 additions & 11 deletions libkineto/src/RoctracerActivityApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

#include "RoctracerActivityApi.h"

#include "ApproximateClock.h"
#include <cstring>
#include <chrono>
#include <functional>
#include <time.h>

#include "Logger.h"
#include "Demangle.h"
#include "output_base.h"
#include "ThreadUtil.h"
Expand Down Expand Up @@ -65,20 +66,16 @@ inline bool RoctracerActivityApi::isLogged(libkineto::ActivityType atype) {
return activityMaskSnapshot_ & (1 << static_cast<uint32_t>(atype));
}

void RoctracerActivityApi::setTimeOffset(timestamp_t toffset) {
toffset_ = toffset;
}

int RoctracerActivityApi::processActivities(
std::function<void(const roctracerBase*)> handler,
std::function<void(uint64_t, uint64_t, RoctracerLogger::CorrelationDomain)> correlationHandler) {
// Find offset to map from monotonic clock to system clock.
// This will break time-ordering of events but is status quo.

timespec t0, t1, t00;
clock_gettime(CLOCK_REALTIME, &t0);
clock_gettime(CLOCK_MONOTONIC, &t1);
clock_gettime(CLOCK_REALTIME, &t00);

const timestamp_t toffset = (timespec_to_ns(t0) >> 1) + (timespec_to_ns(t00) >> 1) - timespec_to_ns(t1);
// Our stored timestamps (from roctracer and generated) are in CLOCK_MONOTONIC domain (in ns).

int count = 0;

// Process all external correlations pairs
Expand Down Expand Up @@ -125,8 +122,8 @@ int RoctracerActivityApi::processActivities(
}
if (!filtered) {
// Convert the begin and end timestamps from monotonic clock to system clock.
item->begin = item->begin + toffset;
item->end = item->end + toffset;
item->begin = item->begin + toffset_;
item->end = item->end + toffset_;
handler(item);
++count;
}
Expand Down
2 changes: 2 additions & 0 deletions libkineto/src/RoctracerActivityApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class RoctracerActivityApi {
const std::set<ActivityType>& selected_activities);
void clearActivities();
void teardownContext() {}
void setTimeOffset(timestamp_t toffset);

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

private:
bool registered_{false};
timestamp_t toffset_{0};

// Enabled Activity Filters
uint32_t activityMask_{0};
Expand Down
Loading