diff --git a/projects/rocprofiler/src/core/core_timer.h b/projects/rocprofiler/src/core/core_timer.h new file mode 100644 index 0000000000..dd0bf22eb2 --- /dev/null +++ b/projects/rocprofiler/src/core/core_timer.h @@ -0,0 +1,64 @@ +#ifndef _CORE_TIMER_H_ +#define _CORE_TIMER_H_ + +template +class CoreTimer { + CoreTimer() { + index_ = 0; + freq_in_100mhz_ = MeasureTSCFreqHz(); + } + ~CoreTimer() { + if (index_ >= Size) { + printf("ERROR: memory corruption: out of timer data"); + abort(); + } + } + + // retrieve time + double Get() { + double n = 0; + // AMD Linux timing + unsigned int unused; + n = __rdtscp(&unused); + data_[index_] = 10 * n / freq_in_100mhz_; // unit is ns + index_ += 1; + } + + double Print() + + private: + // timer data + double data_[Size]; + // data index + uint32_t index_; + // frequency + double freq_in_100mhz_; + + // timing methods + uint64_t CoreTimer::CoarseTimestampUs() { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC_RAW, &ts); + return uint64_t(ts.tv_sec) * 1000000 + ts.tv_nsec / 1000; + } + + uint64_t CoreTimer::MeasureTSCFreqHz() { + // Make a coarse interval measurement of TSC ticks for 1 gigacycles. + unsigned int unused; + uint64_t tscTicksEnd; + + uint64_t coarseBeginUs = CoarseTimestampUs(); + uint64_t tscTicksBegin = __rdtscp(&unused); + do { + tscTicksEnd = __rdtscp(&unused); + } while (tscTicksEnd - tscTicksBegin < 1000000000); + + uint64_t coarseEndUs = CoarseTimestampUs(); + + // Compute the TSC frequency and round to nearest 100MHz. + uint64_t coarseIntervalNs = (coarseEndUs - coarseBeginUs) * 1000; + uint64_t tscIntervalTicks = tscTicksEnd - tscTicksBegin; + return (tscIntervalTicks * 10 + (coarseIntervalNs / 2)) / coarseIntervalNs; + } +}; + +#endif // _CORE_TIMER_H_ diff --git a/projects/rocprofiler/src/core/rocprofiler.cpp b/projects/rocprofiler/src/core/rocprofiler.cpp index 1840bbdc1b..5299e9ba33 100644 --- a/projects/rocprofiler/src/core/rocprofiler.cpp +++ b/projects/rocprofiler/src/core/rocprofiler.cpp @@ -434,6 +434,7 @@ extern "C" { // HSA-runtime tool on-load method PUBLIC_API bool OnLoad(HsaApiTable* table, uint64_t runtime_version, uint64_t failed_tool_count, const char* const* failed_tool_names) { + ONLOAD_TRACE_BEG(); rocprofiler::SaveHsaApi(table); rocprofiler::ProxyQueue::InitFactory(); bool intercept_mode = false; @@ -496,14 +497,16 @@ PUBLIC_API bool OnLoad(HsaApiTable* table, uint64_t runtime_version, uint64_t fa rocprofiler::StandaloneIntercept(); } + ONLOAD_TRACE_END(); return true; } // HSA-runtime tool on-unload method PUBLIC_API void OnUnload() { - rocprofiler::Tracker::Destroy(); + ONLOAD_TRACE_BEG(); rocprofiler::UnloadTool(); rocprofiler::RestoreHsaApi(); + ONLOAD_TRACE_END(); } // Returns library vesrion diff --git a/projects/rocprofiler/src/core/tracker.h b/projects/rocprofiler/src/core/tracker.h index 67efac6c92..823dc17d97 100644 --- a/projects/rocprofiler/src/core/tracker.h +++ b/projects/rocprofiler/src/core/tracker.h @@ -138,7 +138,7 @@ class Tracker { // Debug trace if (trace_on_) { auto outstanding = outstanding_.fetch_add(1); - fprintf(stdout, "Tracker::Add: entry %p, record %p, outst %lu\n", entry, entry->record, outstanding); + fprintf(stdout, "Tracker::Enable: entry %p, record %p, outst %lu\n", entry, entry->record, outstanding); fflush(stdout); } } @@ -163,6 +163,11 @@ class Tracker { {} ~Tracker() { + if (trace_on_) { + fprintf(stdout, "Tracker::DESTR: sig list %d, outst %lu\n", (int)(sig_list_.size()), outstanding_.load()); + fflush(stdout); + } + auto it = sig_list_.begin(); auto end = sig_list_.end(); while (it != end) { @@ -187,7 +192,7 @@ class Tracker { // Debug trace if (trace_on_) { auto outstanding = outstanding_.fetch_sub(1); - fprintf(stdout, "Tracker::Handler: entry %p, record %p, outst %lu\n", entry, entry->record, outstanding); + fprintf(stdout, "Tracker::Complete: entry %p, record %p, outst %lu\n", entry, entry->record, outstanding); fflush(stdout); } diff --git a/projects/rocprofiler/test/tool/tool.cpp b/projects/rocprofiler/test/tool/tool.cpp index 77c7a97a2a..1c526fbc0b 100644 --- a/projects/rocprofiler/test/tool/tool.cpp +++ b/projects/rocprofiler/test/tool/tool.cpp @@ -1241,6 +1241,7 @@ void rocprofiler_unload(bool is_destr) { } fflush(stdout); +#if 0 // Cleanup if (callbacks_data != NULL) { delete[] callbacks_data->features; @@ -1255,8 +1256,9 @@ void rocprofiler_unload(bool is_destr) { kernel_string_vec = NULL; delete range_vec; range_vec = NULL; - if (!is_destr) delete context_array; + delete context_array; context_array = NULL; +#endif ONLOAD_TRACE_END(); }