From 0adba5ccb79110a410686ce3af3bbe14e0eb5d83 Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Tue, 21 Sep 2021 07:07:52 -0400 Subject: [PATCH 01/15] Fix fine-grained control Replaced all C++ standard vectors with C arrays in functions used to enable fine-grained control. These vectors appeared to be empty at the moment when roctracer_enable_op_callback is called in OnLoad function. This may be due to the fact that this function is called by another .so or DLL library and exchanging STL objects (like C++ vectors) through these kinds of libraries can lead to unexpected behavior when the libraries were not compiled with the same compiler and compiler version. The use of C native objects can solve these issues. --- test/tool/tracer_tool.cpp | 79 ++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 26 deletions(-) diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 0b1721d5..4cd498c8 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -109,10 +109,13 @@ bool trace_hip_api = false; bool trace_hip_activity = false; bool trace_kfd = false; bool trace_pcs = false; -// API trace vector -std::vector hsa_api_vec; -std::vector kfd_api_vec; -std::vector hip_api_vec; +// API trace arrays and sizes +uint32_t hsa_api_array_size=0; +char** hsa_api_array; +uint32_t kfd_api_array_size=0; +char** kfd_api_array; +uint32_t hip_api_array_size=0; +char** hip_api_array; LOADER_INSTANTIATE(); TRACE_BUFFER_INSTANTIATE(); @@ -511,9 +514,9 @@ void hip_api_flush_cb(hip_api_trace_entry_t* entry) { if (hip_api_stats != NULL) { hip_api_stats->add_event(cid, end_timestamp - begin_timestamp); if (is_hip_kernel_launch_api(cid)) { - hip_kernel_mutex.lock(); + hip_kernel_mutex.lock(); (*hip_kernel_map)[correlation_id] = entry->name; - hip_kernel_mutex.unlock(); + hip_kernel_mutex.unlock(); } } else { const char* str = hipApiString((hip_api_id_t)cid, data); @@ -913,7 +916,13 @@ void tool_load() { if (name == "HSA") { found = true; trace_hsa_api = true; - hsa_api_vec = api_vec; + hsa_api_array_size = api_vec.size(); + if(hsa_api_array_size > 0){ + hsa_api_array = (char**)malloc(api_vec.size() * sizeof(char*)); + for(uint64_t i = 0 ; i < api_vec.size(); i++){ + hsa_api_array[i] = strdup(api_vec[i].c_str()); + } + } } if (name == "GPU") { found = true; @@ -923,12 +932,24 @@ void tool_load() { found = true; trace_hip_api = true; trace_hip_activity = true; - hip_api_vec = api_vec; + hip_api_array_size = api_vec.size(); + if(hip_api_array_size > 0){ + hip_api_array = (char**)malloc(api_vec.size() * sizeof(char*)); + for(uint64_t i = 0 ; i < api_vec.size(); i++){ + hip_api_array[i] = strdup(api_vec[i].c_str()); + } + } } if (name == "KFD") { found = true; trace_kfd = true; - kfd_api_vec = api_vec; + kfd_api_array_size = api_vec.size(); + if(kfd_api_array_size > 0){ + kfd_api_array = (char**)malloc(api_vec.size() * sizeof(char*)); + for(uint64_t i = 0 ; i < api_vec.size(); i++){ + kfd_api_array[i] = strdup(api_vec[i].c_str()); + } + } } } @@ -1008,14 +1029,16 @@ void tool_load() { roctracer_set_properties(ACTIVITY_DOMAIN_KFD_API, NULL); printf(" KFD-trace("); - if (kfd_api_vec.size() != 0) { - for (unsigned i = 0; i < kfd_api_vec.size(); ++i) { + if (kfd_api_array_size != 0) { + for (unsigned i = 0; i < kfd_api_array_size; ++i) { uint32_t cid = KFD_API_ID_NUMBER; - const char* api = kfd_api_vec[i].c_str(); + const char* api = kfd_api_array[i]; ROCTRACER_CALL(roctracer_op_code(ACTIVITY_DOMAIN_KFD_API, api, &cid, NULL)); ROCTRACER_CALL(roctracer_enable_op_callback(ACTIVITY_DOMAIN_KFD_API, cid, kfd_api_callback, NULL)); printf(" %s", api); + free((char*)api); } + free(kfd_api_array); } else { ROCTRACER_CALL(roctracer_enable_domain_callback(ACTIVITY_DOMAIN_KFD_API, kfd_api_callback, NULL)); } @@ -1052,14 +1075,16 @@ extern "C" PUBLIC_API bool OnLoad(HsaApiTable* table, uint64_t runtime_version, roctracer_set_properties(ACTIVITY_DOMAIN_HSA_API, (void*)table); fprintf(stdout, " HSA-trace("); fflush(stdout); - if (hsa_api_vec.size() != 0) { - for (unsigned i = 0; i < hsa_api_vec.size(); ++i) { + if (hsa_api_array_size != 0) { + for (unsigned i = 0; i < hsa_api_array_size; ++i) { uint32_t cid = HSA_API_ID_NUMBER; - const char* api = hsa_api_vec[i].c_str(); + const char* api = hsa_api_array[i]; ROCTRACER_CALL(roctracer_op_code(ACTIVITY_DOMAIN_HSA_API, api, &cid, NULL)); ROCTRACER_CALL(roctracer_enable_op_callback(ACTIVITY_DOMAIN_HSA_API, cid, hsa_api_callback, NULL)); printf(" %s", api); + free((char*)api); } + free(hsa_api_array); } else { ROCTRACER_CALL(roctracer_enable_domain_callback(ACTIVITY_DOMAIN_HSA_API, hsa_api_callback, NULL)); } @@ -1100,26 +1125,28 @@ extern "C" PUBLIC_API bool OnLoad(HsaApiTable* table, uint64_t runtime_version, // Enable tracing if (trace_hip_api) { hip_api_file_handle = open_output_file(output_prefix, "hip_api_trace.txt"); - if (hip_api_vec.size() != 0) { - for (unsigned i = 0; i < hip_api_vec.size(); ++i) { + if (hip_api_array_size != 0) { + for (unsigned i = 0; i < hip_api_array_size; ++i) { uint32_t cid = HIP_API_ID_NUMBER; - const char* api = hip_api_vec[i].c_str(); + const char* api = hip_api_array[i]; ROCTRACER_CALL(roctracer_op_code(ACTIVITY_DOMAIN_HIP_API, api, &cid, NULL)); ROCTRACER_CALL(roctracer_enable_op_callback(ACTIVITY_DOMAIN_HIP_API, cid, hip_api_callback, NULL)); printf(" %s", api); + free((char*)api); } + free(hip_api_array); } else { ROCTRACER_CALL(roctracer_enable_domain_callback(ACTIVITY_DOMAIN_HIP_API, hip_api_callback, NULL)); } if (is_stats_opt) { - const char* path = NULL; - FILE* f = open_output_file(output_prefix, "hip_api_stats.csv", &path); + const char* path = NULL; + FILE* f = open_output_file(output_prefix, "hip_api_stats.csv", &path); hip_api_stats = new EvtStats(f, path); - for (uint32_t id = 0; id < HIP_API_ID_NUMBER; id += 1) { + for (uint32_t id = 0; id < HIP_API_ID_NUMBER; id += 1) { const char* label = roctracer_op_string(ACTIVITY_DOMAIN_HIP_API, id, 0); hip_api_stats->set_label(id, label); - } + } } } @@ -1128,11 +1155,11 @@ extern "C" PUBLIC_API bool OnLoad(HsaApiTable* table, uint64_t runtime_version, ROCTRACER_CALL(roctracer_enable_domain_activity(ACTIVITY_DOMAIN_HCC_OPS)); if (is_stats_opt) { - FILE* f = NULL; - const char* path = NULL; - f = open_output_file(output_prefix, "hip_kernel_stats.csv", &path); + FILE* f = NULL; + const char* path = NULL; + f = open_output_file(output_prefix, "hip_kernel_stats.csv", &path); hip_kernel_stats = new EvtStatsA(f, path); - f = open_output_file(output_prefix, "hip_memcpy_stats.csv", &path); + f = open_output_file(output_prefix, "hip_memcpy_stats.csv", &path); hip_memcpy_stats = new EvtStatsA(f, path); } } From b4e487e81332f1758f8c8ddf8ddd7468c5922d30 Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Tue, 21 Sep 2021 09:30:59 -0400 Subject: [PATCH 02/15] Add buffer for tracing KFD API Added a ring buffer to trace KFD API and separated lines of code dedicated to flushing into text from lines of code dedicated to collecting events data for KFD API. Added a new structure to store KFD events data into the newly added buffer. This commit aims for rocprof to have the same behavior when tracing KFD API as when tracing HSA or HIP APIs. --- test/tool/tracer_tool.cpp | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 4cd498c8..c122fedd 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -671,7 +671,24 @@ void pool_activity_callback(const char* begin, const char* end, void* arg) { /////////////////////////////////////////////////////////////////////////////////////////////////////// // KFD API tracing +struct kfd_api_trace_entry_t { + std::atomic valid; + roctracer::entry_type_t type; + uint32_t domain; + uint32_t cid; + timestamp_t begin; + timestamp_t end; + uint32_t pid; + uint32_t tid; + kfd_api_data_t data; +}; + +void kfd_api_flush_cb(kfd_api_trace_entry_t* entry); +constexpr roctracer::TraceBuffer::flush_prm_t kfd_api_flush_prm = {roctracer::DFLT_ENTRY_TYPE, kfd_api_flush_cb}; +roctracer::TraceBuffer* kfd_api_trace_buffer = NULL; + // KFD API callback function + static thread_local bool in_kfd_api_callback = false; void kfd_api_callback( uint32_t domain, @@ -687,13 +704,23 @@ void kfd_api_callback( kfd_begin_timestamp = timer->timestamp_fn_ns(); } else { const timestamp_t end_timestamp = timer->timestamp_fn_ns(); - std::ostringstream os; - os << kfd_begin_timestamp << ":" << end_timestamp << " " << GetPid() << ":" << GetTid() << " " << kfd_api_data_pair_t(cid, *data); - fprintf(kfd_api_file_handle, "%s\n", os.str().c_str()); + kfd_api_trace_entry_t* entry = kfd_api_trace_buffer->GetEntry(); + entry->cid = cid; + entry->begin = kfd_begin_timestamp; + entry->end = end_timestamp; + entry->pid = GetPid(); + entry->tid = GetTid(); + entry->data = *data; + entry->valid.store(roctracer::TRACE_ENTRY_COMPL, std::memory_order_release); } in_kfd_api_callback = false; } +void kfd_api_flush_cb(kfd_api_trace_entry_t* entry) { + std::ostringstream os; + os << entry->begin << ":" << entry->end << " " << entry->pid << ":" << entry->tid << " " << kfd_api_data_pair_t(entry->cid, entry->data); + fprintf(kfd_api_file_handle, "%s\n", os.str().c_str()); +} /////////////////////////////////////////////////////////////////////////////////////////////////////// // Input parser @@ -1194,6 +1221,7 @@ extern "C" CONSTRUCTOR_API void constructor() { hip_api_trace_buffer = new roctracer::TraceBuffer("HIP API", 0x200000, &hip_api_flush_prm, 1); hip_act_trace_buffer = new roctracer::TraceBuffer("HIP ACT", 0x200000, &hip_act_flush_prm, 1, 1); hsa_api_trace_buffer = new roctracer::TraceBuffer("HSA API", 0x200000, &hsa_flush_prm, 1); + kfd_api_trace_buffer = new roctracer::TraceBuffer("KFD API", 0x200000, &kfd_api_flush_prm, 1); roctracer_load(); tool_load(); ONLOAD_TRACE_END(); From a669e4ad0b332d428c3d301d7a6697d2004f6cee Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Tue, 21 Sep 2021 10:56:47 -0400 Subject: [PATCH 03/15] Isolate flushing function for roctx In some cases (if ROCTX_CLOCK_TIME is defined), the timestamp of the rocTX event must be processed before flushing the data. This commit aims to separate the computation of the timestamp from the flushing part : a wrapping function is defined to do it and the roctx_flush_cb function only contains operation on C++ string streams and flushing instructions --- test/tool/tracer_tool.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index c122fedd..8230f2c5 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -236,8 +236,8 @@ struct roctx_trace_entry_t { const char* message; }; -void roctx_flush_cb(roctx_trace_entry_t* entry); -constexpr roctracer::TraceBuffer::flush_prm_t roctx_flush_prm = {roctracer::DFLT_ENTRY_TYPE, roctx_flush_cb}; +void roctx_flush_cb_wrapper(roctx_trace_entry_t* entry); +constexpr roctracer::TraceBuffer::flush_prm_t roctx_flush_prm = {roctracer::DFLT_ENTRY_TYPE, roctx_flush_cb_wrapper}; roctracer::TraceBuffer* roctx_trace_buffer = NULL; // rocTX callback function @@ -286,19 +286,22 @@ void stop_callback() { roctracer::RocTxLoader::Instance().RangeStackIterate(roct // rocTX buffer flush function void roctx_flush_cb(roctx_trace_entry_t* entry) { -#if ROCTX_CLOCK_TIME - timestamp_t timestamp = 0; - HsaRsrcFactory::Instance().GetTimestamp(HsaTimer::TIME_ID_CLOCK_MONOTONIC, entry->time, ×tamp); -#else - const timestamp_t timestamp = entry->time; -#endif std::ostringstream os; - os << timestamp << " " << entry->pid << ":" << entry->tid << " " << entry->cid << ":" << entry->rid; + os << entry->time << " " << entry->pid << ":" << entry->tid << " " << entry->cid << ":" << entry->rid; if (entry->message != NULL) os << ":\"" << entry->message << "\""; else os << ":\"\""; fprintf(roctx_file_handle, "%s\n", os.str().c_str()); fflush(roctx_file_handle); } +void roctx_flush_cb_wrapper(roctx_trace_entry_t* entry){ +#if ROCTX_CLOCK_TIME + timestamp_t timestamp = 0; + HsaRsrcFactory::Instance().GetTimestamp(HsaTimer::TIME_ID_CLOCK_MONOTONIC, entry->time, ×tamp); + entry->time = timestamp +#endif + roctx_flush_cb(entry); +}; + /////////////////////////////////////////////////////////////////////////////////////////////////////// // HSA API tracing From bb24c8286ed1c92e47530176589df9df8e0ca9e2 Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Tue, 21 Sep 2021 11:18:05 -0400 Subject: [PATCH 04/15] Isolate flushing instructions for HSA activity Index incrementation was initially done inside HSA activity flushing function. Separated the flushing instructions from the index incrementation with a wrapping function. The pid is also given as a parameter to the flushing function. --- test/tool/tracer_tool.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 8230f2c5..c2de29f8 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -353,14 +353,22 @@ void hsa_api_flush_cb(hsa_api_trace_entry_t* entry) { } void hsa_activity_callback( + uint64_t index, uint32_t op, + uint32_t pid, activity_record_t* record, void* arg) { + fprintf(hsa_async_copy_file_handle, "%lu:%lu async-copy:%lu:%u\n", record->begin_ns, record->end_ns, index, pid); fflush(hsa_async_copy_file_handle); +} + +void hsa_activity_callback_wrapper( uint32_t op, + activity_record_t* record, + void* arg){ static uint64_t index = 0; - fprintf(hsa_async_copy_file_handle, "%lu:%lu async-copy:%lu:%u\n", record->begin_ns, record->end_ns, index, my_pid); fflush(hsa_async_copy_file_handle); + hsa_activity_callback(index, op, my_pid, record, arg); index++; -} + } /////////////////////////////////////////////////////////////////////////////////////////////////////// // HIP API tracing @@ -1128,7 +1136,7 @@ extern "C" PUBLIC_API bool OnLoad(HsaApiTable* table, uint64_t runtime_version, // initialize HSA tracing roctracer::hsa_ops_properties_t ops_properties { table, - reinterpret_cast(hsa_activity_callback), + reinterpret_cast(hsa_activity_callback_wrapper), NULL, output_prefix }; From f06ef8d6f6f38a89b007aee9e6a42e2a8749ef32 Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Wed, 22 Sep 2021 06:03:32 -0400 Subject: [PATCH 05/15] Isolate flushing instructions for HIP API When collecting statistics about HIP API, the initial flushing function also fills an EvtStats structure. With this commit, flushing instruction are wrapped into another function that is called after this structure is filled if needed. The flushing function only contains flushing instructions in this commit. --- test/tool/tracer_tool.cpp | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index c2de29f8..94e9bfdf 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -502,7 +502,7 @@ typedef std::map hip_kernel_map_t; hip_kernel_map_t* hip_kernel_map = NULL; std::mutex hip_kernel_mutex; -void hip_api_flush_cb(hip_api_trace_entry_t* entry) { +void hip_api_flush_cb(hip_api_trace_entry_t *entry){ const uint32_t domain = entry->domain; const uint32_t cid = entry->cid; const hip_api_data_t* data = &(entry->data); @@ -522,14 +522,7 @@ void hip_api_flush_cb(hip_api_trace_entry_t* entry) { if (domain == ACTIVITY_DOMAIN_HIP_API) { #if HIP_PROF_HIP_API_STRING - if (hip_api_stats != NULL) { - hip_api_stats->add_event(cid, end_timestamp - begin_timestamp); - if (is_hip_kernel_launch_api(cid)) { - hip_kernel_mutex.lock(); - (*hip_kernel_map)[correlation_id] = entry->name; - hip_kernel_mutex.unlock(); - } - } else { + if (hip_api_stats == NULL) { const char* str = hipApiString((hip_api_id_t)cid, data); rec_ss << " " << str; if (is_hip_kernel_launch_api(cid) && entry->name) { @@ -600,6 +593,29 @@ void hip_api_flush_cb(hip_api_trace_entry_t* entry) { fflush(hip_api_file_handle); } +void hip_api_flush_cb_wrapper(hip_api_trace_entry_t *entry){ + const uint32_t domain = entry->domain; + const uint32_t cid = entry->cid; + const hip_api_data_t* data = &(entry->data); + const uint64_t correlation_id = data->correlation_id; + const timestamp_t begin_timestamp = entry->begin; + const timestamp_t end_timestamp = entry->end; + +#if HIP_PROF_HIP_API_STRING + if (domain == ACTIVITY_DOMAIN_HIP_API && hip_api_stats != NULL) { + hip_api_stats->add_event(cid, end_timestamp - begin_timestamp); + if (is_hip_kernel_launch_api(cid)) { + hip_kernel_mutex.lock(); + (*hip_kernel_map)[correlation_id] = entry->name; + hip_kernel_mutex.unlock(); + } + } +#endif + hip_api_flush_cb(entry); +} + + + /////////////////////////////////////////////////////////////////////////////////////////////////////// // HSA API tracing From 035a498cdbcec7bee59a861e2e355e95d839b32e Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Wed, 22 Sep 2021 06:38:49 -0400 Subject: [PATCH 06/15] Isolate flushing instructions for HIP activity Instructions for flushing HIP activities informations into hcc_ops_trace.txt have been put into a wrapping function --- test/tool/tracer_tool.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 94e9bfdf..2c553d13 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -658,6 +658,15 @@ void hip_act_flush_cb(hip_act_trace_entry_t* entry) { // Activity tracing callback // hipMalloc id(3) correlation_id(1): begin_ns(1525888652762640464) end_ns(1525888652762877067) + +void hip_activity_flush_cb(const roctracer_record_t *record, const char *name, uint32_t pid){ + fprintf(hcc_activity_file_handle, "%lu:%lu %d:%lu %s:%lu:%u\n", + record->begin_ns, record->end_ns, + record->device_id, record->queue_id, + name, record->correlation_id, pid); + fflush(hcc_activity_file_handle); +} + void pool_activity_callback(const char* begin, const char* end, void* arg) { const roctracer_record_t* record = reinterpret_cast(begin); const roctracer_record_t* end_record = reinterpret_cast(end); @@ -676,11 +685,7 @@ void pool_activity_callback(const char* begin, const char* end, void* arg) { entry->correlation_id = record->correlation_id; entry->valid.store(roctracer::TRACE_ENTRY_COMPL, std::memory_order_release); } else { - fprintf(hcc_activity_file_handle, "%lu:%lu %d:%lu %s:%lu:%u\n", - record->begin_ns, record->end_ns, - record->device_id, record->queue_id, - name, record->correlation_id, my_pid); - fflush(hcc_activity_file_handle); + hip_activity_flush_cb(record, name, my_pid); } break; case ACTIVITY_DOMAIN_HSA_OPS: From 77d23b689e73e74595276ae1c195c61201b08e30 Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Thu, 23 Sep 2021 05:48:01 -0400 Subject: [PATCH 07/15] Update HSA activity flushing function signature Updated HSA activity flushing function signature in order to have a standard signature for all flushing functions. The function name has been changed and a wrapping data structure has been defined and is now the only one parameter for the new function --- test/tool/tracer_tool.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 2c553d13..1518e86c 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -352,21 +352,26 @@ void hsa_api_flush_cb(hsa_api_trace_entry_t* entry) { fprintf(hsa_api_file_handle, "%s\n", os.str().c_str()); fflush(hsa_api_file_handle); } -void hsa_activity_callback( - uint64_t index, - uint32_t op, - uint32_t pid, - activity_record_t* record, - void* arg) +struct hsa_activity_trace_entry_t { + uint64_t index; + uint32_t op; + uint32_t pid; + activity_record_t *record; + void *arg; +}; + +void hsa_activity_flush_cb( + hsa_activity_trace_entry_t *entry) { - fprintf(hsa_async_copy_file_handle, "%lu:%lu async-copy:%lu:%u\n", record->begin_ns, record->end_ns, index, pid); fflush(hsa_async_copy_file_handle); + fprintf(hsa_async_copy_file_handle, "%lu:%lu async-copy:%lu:%u\n", entry->record->begin_ns, entry->record->end_ns, entry->index, entry->pid); fflush(hsa_async_copy_file_handle); } void hsa_activity_callback_wrapper( uint32_t op, activity_record_t* record, void* arg){ static uint64_t index = 0; - hsa_activity_callback(index, op, my_pid, record, arg); + hsa_activity_trace_entry_t hsa_activity_trace_entry = {index, op, my_pid, record, arg}; + hsa_activity_flush_cb(&hsa_activity_trace_entry); index++; } From 9b390bfd34db541266e0545d77ced994fb6b6130 Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Thu, 23 Sep 2021 06:08:43 -0400 Subject: [PATCH 08/15] Update HIP activity (HCC ops) flushing function signature As for HSA activity, a new data structure has been defined and the signature updated to conform to other flushing functions signatures. The new datatype is named hip_activity_trace_entry_t. Another similar type (hip_act_trace_entry_t) is already defined to collect HIP activity statistics into a csv file. To avoid confusions, these two types could be merged or the name of one of them could be changed in a future patch. --- test/tool/tracer_tool.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 1518e86c..5347cd3f 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -663,12 +663,17 @@ void hip_act_flush_cb(hip_act_trace_entry_t* entry) { // Activity tracing callback // hipMalloc id(3) correlation_id(1): begin_ns(1525888652762640464) end_ns(1525888652762877067) +struct hip_activity_trace_entry_t { + const roctracer_record_t *record; + const char *name; + uint32_t pid; +}; -void hip_activity_flush_cb(const roctracer_record_t *record, const char *name, uint32_t pid){ +void hip_activity_flush_cb(hip_activity_trace_entry_t *entry){ fprintf(hcc_activity_file_handle, "%lu:%lu %d:%lu %s:%lu:%u\n", - record->begin_ns, record->end_ns, - record->device_id, record->queue_id, - name, record->correlation_id, pid); + entry->record->begin_ns, entry->record->end_ns, + entry->record->device_id, entry->record->queue_id, + entry->name, entry->record->correlation_id, entry->pid); fflush(hcc_activity_file_handle); } @@ -690,7 +695,8 @@ void pool_activity_callback(const char* begin, const char* end, void* arg) { entry->correlation_id = record->correlation_id; entry->valid.store(roctracer::TRACE_ENTRY_COMPL, std::memory_order_release); } else { - hip_activity_flush_cb(record, name, my_pid); + hip_activity_trace_entry_t hip_activity_trace_entry = {record, name, my_pid}; + hip_activity_flush_cb(&hip_activity_trace_entry); } break; case ACTIVITY_DOMAIN_HSA_OPS: From 3787d72466cd29d6c83236a1d4a61f75dbe76b3b Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Thu, 23 Sep 2021 10:06:29 -0400 Subject: [PATCH 09/15] Move roctx_trace_entry_t to a new header Definition of roctx_trace_entry_t has been moved to a new header. This new header will contain the definitions of all the _trace_entry_t types used to flush the payloads of the events. The purpose of this modification is to have only one header that needs to be included by developers that want to implement new plugins with the interface proposal. Some fields types of the _trace_entry_t structures are defined either in tool.cpp or in headers lying in /src/core (/src/core/trace_buffer.h). Since trace_buffer.h is removed once ROCTrace has been installed, the definition of these types has been moved to the new header and deleted from the ancient header to avoid redefinition issues. --- inc/roctracer_trace_entries.h | 33 +++++++++++++++++++++++++++++++++ src/core/trace_buffer.h | 10 ++-------- test/tool/tracer_tool.cpp | 13 +------------ 3 files changed, 36 insertions(+), 20 deletions(-) create mode 100644 inc/roctracer_trace_entries.h diff --git a/inc/roctracer_trace_entries.h b/inc/roctracer_trace_entries.h new file mode 100644 index 00000000..f0176879 --- /dev/null +++ b/inc/roctracer_trace_entries.h @@ -0,0 +1,33 @@ +#ifndef INC_ROCTRACER_TRACE_ENTRIES_H_ +#define INC_ROCTRACER_TRACE_ENTRIES_H_ + +#include +#include + +#include +#include + +typedef hsa_rt_utils::Timer::timestamp_t timestamp_t; + +namespace roctracer { +enum entry_type_t { + DFLT_ENTRY_TYPE = 0, + API_ENTRY_TYPE = 1, + COPY_ENTRY_TYPE = 2, + KERNEL_ENTRY_TYPE = 3, + NUM_ENTRY_TYPE = 4 +}; +} + +struct roctx_trace_entry_t { + std::atomic valid; + roctracer::entry_type_t type; + uint32_t cid; + timestamp_t time; + uint32_t pid; + uint32_t tid; + roctx_range_id_t rid; + const char* message; +}; + +#endif \ No newline at end of file diff --git a/src/core/trace_buffer.h b/src/core/trace_buffer.h index cb6767f2..0d71300d 100644 --- a/src/core/trace_buffer.h +++ b/src/core/trace_buffer.h @@ -11,6 +11,8 @@ #include #include +#include + #define FATAL(stream) \ do { \ std::ostringstream oss; \ @@ -36,14 +38,6 @@ enum { TRACE_ENTRY_COMPL = 2 }; -enum entry_type_t { - DFLT_ENTRY_TYPE = 0, - API_ENTRY_TYPE = 1, - COPY_ENTRY_TYPE = 2, - KERNEL_ENTRY_TYPE = 3, - NUM_ENTRY_TYPE = 4 -}; - struct trace_entry_t { std::atomic valid; entry_type_t type; diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 5347cd3f..28caec35 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -40,6 +40,7 @@ THE SOFTWARE. #include #include #include +#include #include "src/core/loader.h" #include "src/core/trace_buffer.h" @@ -97,7 +98,6 @@ inline static void DEBUG_TRACE(const char* fmt, ...) { inline static void DEBUG_TRACE(const char* fmt, ...) {} #endif -typedef hsa_rt_utils::Timer::timestamp_t timestamp_t; hsa_rt_utils::Timer* timer = NULL; thread_local timestamp_t hsa_begin_timestamp = 0; thread_local timestamp_t hip_begin_timestamp = 0; @@ -225,17 +225,6 @@ void* flush_thr_fun(void*) { /////////////////////////////////////////////////////////////////////////////////////////////////////// // rocTX annotation tracing -struct roctx_trace_entry_t { - std::atomic valid; - roctracer::entry_type_t type; - uint32_t cid; - timestamp_t time; - uint32_t pid; - uint32_t tid; - roctx_range_id_t rid; - const char* message; -}; - void roctx_flush_cb_wrapper(roctx_trace_entry_t* entry); constexpr roctracer::TraceBuffer::flush_prm_t roctx_flush_prm = {roctracer::DFLT_ENTRY_TYPE, roctx_flush_cb_wrapper}; roctracer::TraceBuffer* roctx_trace_buffer = NULL; From 9d0dd89f1278706e3eaddb9ecc3aed736d5ef7c3 Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Thu, 23 Sep 2021 10:12:02 -0400 Subject: [PATCH 10/15] Update CMakeLists to take into account the new header This modification allows to copy the new header to /opt/rocm/roctracer/include and /opt/rocm/include/roctracer when installing ROCTracer --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index f8bff20c..5335c226 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,6 +101,7 @@ set ( PUBLIC_HEADERS roctracer_kfd.h roctracer_roctx.h roctracer_cb_table.h + roctracer_trace_entries.h ext/prof_protocol.h ext/hsa_rt_utils.hpp ) From dccd171fab8ca6e225ec0aef3e84d52c5744b148 Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Thu, 23 Sep 2021 10:16:52 -0400 Subject: [PATCH 11/15] Move hsa_api_trace_entry_t to roctracer_trace_entries.h --- inc/roctracer_trace_entries.h | 12 ++++++++++++ test/tool/tracer_tool.cpp | 11 ----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/inc/roctracer_trace_entries.h b/inc/roctracer_trace_entries.h index f0176879..d7bfeca7 100644 --- a/inc/roctracer_trace_entries.h +++ b/inc/roctracer_trace_entries.h @@ -5,6 +5,7 @@ #include #include +#include #include typedef hsa_rt_utils::Timer::timestamp_t timestamp_t; @@ -30,4 +31,15 @@ struct roctx_trace_entry_t { const char* message; }; +struct hsa_api_trace_entry_t { + std::atomic valid; + roctracer::entry_type_t type; + uint32_t cid; + timestamp_t begin; + timestamp_t end; + uint32_t pid; + uint32_t tid; + hsa_api_data_t data; +}; + #endif \ No newline at end of file diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 28caec35..7e180f34 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -294,17 +294,6 @@ void roctx_flush_cb_wrapper(roctx_trace_entry_t* entry){ /////////////////////////////////////////////////////////////////////////////////////////////////////// // HSA API tracing -struct hsa_api_trace_entry_t { - std::atomic valid; - roctracer::entry_type_t type; - uint32_t cid; - timestamp_t begin; - timestamp_t end; - uint32_t pid; - uint32_t tid; - hsa_api_data_t data; -}; - void hsa_api_flush_cb(hsa_api_trace_entry_t* entry); constexpr roctracer::TraceBuffer::flush_prm_t hsa_flush_prm = {roctracer::DFLT_ENTRY_TYPE, hsa_api_flush_cb}; roctracer::TraceBuffer* hsa_api_trace_buffer = NULL; From 5b06b8002a08e20936c2977c14a7a06ea7a8f638 Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Thu, 23 Sep 2021 10:22:34 -0400 Subject: [PATCH 12/15] Move hsa_activity_trace_entry_t to roctracer_trace_entries.h --- inc/roctracer_trace_entries.h | 9 +++++++++ test/tool/tracer_tool.cpp | 8 -------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/inc/roctracer_trace_entries.h b/inc/roctracer_trace_entries.h index d7bfeca7..197a06f9 100644 --- a/inc/roctracer_trace_entries.h +++ b/inc/roctracer_trace_entries.h @@ -6,6 +6,7 @@ #include #include +#include #include typedef hsa_rt_utils::Timer::timestamp_t timestamp_t; @@ -42,4 +43,12 @@ struct hsa_api_trace_entry_t { hsa_api_data_t data; }; +struct hsa_activity_trace_entry_t { + uint64_t index; + uint32_t op; + uint32_t pid; + activity_record_t *record; + void *arg; +}; + #endif \ No newline at end of file diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 7e180f34..61fa710a 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -330,14 +330,6 @@ void hsa_api_flush_cb(hsa_api_trace_entry_t* entry) { fprintf(hsa_api_file_handle, "%s\n", os.str().c_str()); fflush(hsa_api_file_handle); } -struct hsa_activity_trace_entry_t { - uint64_t index; - uint32_t op; - uint32_t pid; - activity_record_t *record; - void *arg; -}; - void hsa_activity_flush_cb( hsa_activity_trace_entry_t *entry) { From a73995f9a43c8f7ea0925795d3d4c051f2d035eb Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Thu, 23 Sep 2021 10:25:08 -0400 Subject: [PATCH 13/15] Move hip_api_trace_entry_t to roctracer_trace_entries.h --- inc/roctracer_trace_entries.h | 15 +++++++++++++++ test/tool/tracer_tool.cpp | 14 -------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/inc/roctracer_trace_entries.h b/inc/roctracer_trace_entries.h index 197a06f9..4601cb84 100644 --- a/inc/roctracer_trace_entries.h +++ b/inc/roctracer_trace_entries.h @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -51,4 +52,18 @@ struct hsa_activity_trace_entry_t { void *arg; }; +struct hip_api_trace_entry_t { + std::atomic valid; + roctracer::entry_type_t type; + uint32_t domain; + uint32_t cid; + timestamp_t begin; + timestamp_t end; + uint32_t pid; + uint32_t tid; + hip_api_data_t data; + const char* name; + void* ptr; +}; + #endif \ No newline at end of file diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 61fa710a..f4e6acea 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -348,20 +348,6 @@ void hsa_activity_callback_wrapper( uint32_t op, /////////////////////////////////////////////////////////////////////////////////////////////////////// // HIP API tracing -struct hip_api_trace_entry_t { - std::atomic valid; - roctracer::entry_type_t type; - uint32_t domain; - uint32_t cid; - timestamp_t begin; - timestamp_t end; - uint32_t pid; - uint32_t tid; - hip_api_data_t data; - const char* name; - void* ptr; -}; - void hip_api_flush_cb(hip_api_trace_entry_t* entry); constexpr roctracer::TraceBuffer::flush_prm_t hip_api_flush_prm = {roctracer::DFLT_ENTRY_TYPE, hip_api_flush_cb}; roctracer::TraceBuffer* hip_api_trace_buffer = NULL; From abbab33daba62130b6672bca39d354cdd0c99109 Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Thu, 23 Sep 2021 10:28:20 -0400 Subject: [PATCH 14/15] Move hip_activity_trace_entry_t to roctracer_trace_entries.h --- inc/roctracer_trace_entries.h | 7 +++++++ test/tool/tracer_tool.cpp | 5 ----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/inc/roctracer_trace_entries.h b/inc/roctracer_trace_entries.h index 4601cb84..f5a1350a 100644 --- a/inc/roctracer_trace_entries.h +++ b/inc/roctracer_trace_entries.h @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -66,4 +67,10 @@ struct hip_api_trace_entry_t { void* ptr; }; +struct hip_activity_trace_entry_t { + const roctracer_record_t *record; + const char *name; + uint32_t pid; +}; + #endif \ No newline at end of file diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index f4e6acea..2fbac7cd 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -619,11 +619,6 @@ void hip_act_flush_cb(hip_act_trace_entry_t* entry) { // Activity tracing callback // hipMalloc id(3) correlation_id(1): begin_ns(1525888652762640464) end_ns(1525888652762877067) -struct hip_activity_trace_entry_t { - const roctracer_record_t *record; - const char *name; - uint32_t pid; -}; void hip_activity_flush_cb(hip_activity_trace_entry_t *entry){ fprintf(hcc_activity_file_handle, "%lu:%lu %d:%lu %s:%lu:%u\n", From b3a07812bdc3186526193ad7cb64750bd47a0c98 Mon Sep 17 00:00:00 2001 From: yoann-heitz Date: Thu, 23 Sep 2021 10:35:40 -0400 Subject: [PATCH 15/15] Move kfd_api_trace_entry_t to roctracer_trace_entries.h --- inc/roctracer_trace_entries.h | 13 +++++++++++++ test/tool/tracer_tool.cpp | 12 ------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/inc/roctracer_trace_entries.h b/inc/roctracer_trace_entries.h index f5a1350a..ab54593c 100644 --- a/inc/roctracer_trace_entries.h +++ b/inc/roctracer_trace_entries.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -73,4 +74,16 @@ struct hip_activity_trace_entry_t { uint32_t pid; }; +struct kfd_api_trace_entry_t { + std::atomic valid; + roctracer::entry_type_t type; + uint32_t domain; + uint32_t cid; + timestamp_t begin; + timestamp_t end; + uint32_t pid; + uint32_t tid; + kfd_api_data_t data; +}; + #endif \ No newline at end of file diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 2fbac7cd..b616ee47 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -665,18 +665,6 @@ void pool_activity_callback(const char* begin, const char* end, void* arg) { /////////////////////////////////////////////////////////////////////////////////////////////////////// // KFD API tracing -struct kfd_api_trace_entry_t { - std::atomic valid; - roctracer::entry_type_t type; - uint32_t domain; - uint32_t cid; - timestamp_t begin; - timestamp_t end; - uint32_t pid; - uint32_t tid; - kfd_api_data_t data; -}; - void kfd_api_flush_cb(kfd_api_trace_entry_t* entry); constexpr roctracer::TraceBuffer::flush_prm_t kfd_api_flush_prm = {roctracer::DFLT_ENTRY_TYPE, kfd_api_flush_cb}; roctracer::TraceBuffer* kfd_api_trace_buffer = NULL;