From c45fbe91ff12ce8cf6fa7e65812365da67aaee5a Mon Sep 17 00:00:00 2001 From: Benjamin Welton Date: Thu, 8 Aug 2024 01:14:38 +0000 Subject: [PATCH] Add support for kernel name filtering to rocprofv2 rocprofv2 does not support the "kernel:" attribute in input files. This commit adds support for this attribute for cli/csv output. This is requested by Alibaba/Omniperf to have compatible execution with both v1/v2 (JSON and CTF are not supported here and not needed). Filtering can also be enabled by the flag ROCPROFILER_KERNEL_FILTER. Change-Id: I376382d9e5b10a8356df8d175b1a56396b5b51d3 [ROCm/rocprofiler commit: b32eb2bda92262c188a76c4c574ec8e06efdd7c0] --- projects/rocprofiler/bin/rocprofv2 | 8 +++++++ projects/rocprofiler/plugin/cli/cli.cpp | 28 ++++++++++++++++++++++- projects/rocprofiler/plugin/file/file.cpp | 25 ++++++++++++++++++++ projects/rocprofiler/plugin/utils.h | 18 +++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/projects/rocprofiler/bin/rocprofv2 b/projects/rocprofiler/bin/rocprofv2 index adcec35064..a768476cfb 100755 --- a/projects/rocprofiler/bin/rocprofv2 +++ b/projects/rocprofiler/bin/rocprofv2 @@ -295,6 +295,14 @@ export LD_PRELOAD=$LD_PRELOAD:$ROCM_DIR/lib/rocprofiler/librocprofiler_tool.so if [ -n "$PMC_LINES" ] && [ ! -n "$ATT_ARGV" ]; then COUNTER=1 + + for i in ${!PMC_LINES[@]}; do + if [[ ${PMC_LINES[$i]} =~ "kernel:" ]]; then + echo ${PMC_LINES[$i]} + export ROCPROFILER_KERNEL_FILTER="${PMC_LINES[$i]}" + fi + done + for i in ${!PMC_LINES[@]}; do export ROCPROFILER_COUNTERS="${PMC_LINES[$i]}" if [[ ! ${PMC_LINES[$i]} =~ "pmc" ]]; then diff --git a/projects/rocprofiler/plugin/cli/cli.cpp b/projects/rocprofiler/plugin/cli/cli.cpp index 996af70008..fe056b87f3 100644 --- a/projects/rocprofiler/plugin/cli/cli.cpp +++ b/projects/rocprofiler/plugin/cli/cli.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -136,7 +137,10 @@ class file_plugin_t { } public: - file_plugin_t() { valid_ = true; } + file_plugin_t() { + valid_ = true; + kernel_filters_ = GetKernelFilters(); + } std::mutex writing_lock; @@ -208,6 +212,22 @@ class file_plugin_t { void FlushProfilerRecord(const rocprofiler_record_profiler_t* profiler_record, rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) { + auto check_filter_cache = [&](const char * k_name) { + auto str = std::string{k_name}; + auto f = kernel_filter_cache_.find(k_name); + if (f == kernel_filter_cache_.end()) { + bool found_match = false; + for (const auto& filter : kernel_filters_) { + if (str.find(filter) != std::string::npos) { + found_match = true; + break; + } + } + f = kernel_filter_cache_.emplace(str, found_match).first; + } + return f->second; + }; + std::lock_guard lock(writing_lock); size_t name_length = 0; output_file_t* output_file{nullptr}; @@ -220,6 +240,10 @@ class file_plugin_t { if (name_length > 1) { CHECK_ROCPROFILER(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME, profiler_record->kernel_id, &kernel_name_c)); + if (kernel_name_c != nullptr && !kernel_filters_.empty() && !check_filter_cache(kernel_name_c)) { + free(const_cast(kernel_name_c)); + return; + } } *output_file << "Dispatch_ID(" << std::to_string(profiler_record->header.id.handle) << "), " << "GPU_ID(" << std::to_string(profiler_record->gpu_id.handle) << "), " @@ -340,6 +364,8 @@ class file_plugin_t { private: bool valid_{false}; + std::set kernel_filters_; + std::map kernel_filter_cache_; std::atomic tracer_header_written_{false}; std::atomic profiler_header_written_{false}; diff --git a/projects/rocprofiler/plugin/file/file.cpp b/projects/rocprofiler/plugin/file/file.cpp index 8d463daa2d..e9f74e0cd9 100644 --- a/projects/rocprofiler/plugin/file/file.cpp +++ b/projects/rocprofiler/plugin/file/file.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -176,6 +177,7 @@ class file_plugin_t { file_plugin_t(void* data) { if (data) counter_names_ = GetCounterNames(); + kernel_filters_ = GetKernelFilters(); const char* str = getenv("ROCPROFILER_INDIVIDUAL_XCC_MODE"); if (str != NULL) is_individual_xcc_mode = (atol(str) > 0); @@ -328,6 +330,23 @@ class file_plugin_t { void FlushProfilerRecord(const rocprofiler_record_profiler_t* profiler_record, rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) { + + auto check_filter_cache = [&](const char * k_name) { + auto str = std::string{k_name}; + auto f = kernel_filter_cache_.find(k_name); + if (f == kernel_filter_cache_.end()) { + bool found_match = false; + for (const auto& filter : kernel_filters_) { + if (str.find(filter) != std::string::npos) { + found_match = true; + break; + } + } + f = kernel_filter_cache_.emplace(str, found_match).first; + } + return f->second; + }; + std::lock_guard lock(writing_lock); std::stringstream ss; WriteHeader(output_type_t::COUNTER, ACTIVITY_DOMAIN_NUMBER); @@ -342,6 +361,10 @@ class file_plugin_t { if (name_length > 1) { CHECK_ROCPROFILER(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME, profiler_record->kernel_id, &kernel_name_c)); + if (kernel_name_c != nullptr && !kernel_filters_.empty() && !check_filter_cache(kernel_name_c)) { + free(const_cast(kernel_name_c)); + return; + } } if (!counter_header_written_) { @@ -465,6 +488,8 @@ class file_plugin_t { bool counter_header_written_ = false; bool is_individual_xcc_mode=false; std::vector counter_names_; + std::set kernel_filters_; + std::map kernel_filter_cache_; std::atomic roctx_header_written_{false}, hsa_api_header_written_{false}, hip_api_header_written_{false}, hip_activity_header_written_{false}, diff --git a/projects/rocprofiler/plugin/utils.h b/projects/rocprofiler/plugin/utils.h index f1c8ebe160..b5fbdf8c36 100644 --- a/projects/rocprofiler/plugin/utils.h +++ b/projects/rocprofiler/plugin/utils.h @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include "src/utils/helper.h" @@ -51,4 +53,20 @@ namespace { [[maybe_unused]] uint64_t GetMachineID() { return gethostid(); } +[[maybe_unused]] std::set GetKernelFilters() { + std::set ret; + if (const char* line_c_str = getenv("ROCPROFILER_KERNEL_FILTER")) { + std::stringstream ss(std::string{line_c_str}); + std::string filter_name; + while(std::getline(ss, filter_name, ' ')) + { + if (filter_name.find("kernel:") != std::string::npos) { + continue; + } + ret.insert(filter_name); + } + } + return ret; +} + } // namespace