From 2d52ff602239c99b5a581c1cdb785929167b79ce Mon Sep 17 00:00:00 2001 From: gobhardw Date: Wed, 14 Jun 2023 16:56:27 +0530 Subject: [PATCH] Handle user PMC input Change-Id: I9146453ca0390aa0df22ee4ed9ca3fb4c7b06dbf [ROCm/rocprofiler commit: c630d83f224cc6693b020c6782e278e702536360] --- projects/rocprofiler/src/tools/tool.cpp | 36 ++++------------- projects/rocprofiler/src/utils/helper.cpp | 49 +++++++++++++++++++++-- projects/rocprofiler/src/utils/helper.h | 4 ++ 3 files changed, 57 insertions(+), 32 deletions(-) diff --git a/projects/rocprofiler/src/tools/tool.cpp b/projects/rocprofiler/src/tools/tool.cpp index fe26abf034..391a6ff76e 100644 --- a/projects/rocprofiler/src/tools/tool.cpp +++ b/projects/rocprofiler/src/tools/tool.cpp @@ -65,7 +65,7 @@ namespace fs = std::experimental::filesystem; #define CHECK_ROCPROFILER(call) \ do { \ if ((call) != ROCPROFILER_STATUS_SUCCESS) \ - rocprofiler::fatal("Error: ROCProfiler API Call Error!"); \ + rocprofiler::fatal("Error: ROCProfiler API Call Error!"); \ } while (false) TRACE_BUFFER_INSTANTIATE(); namespace { @@ -221,27 +221,7 @@ std::vector GetCounterNames() { const char* line_c_str = getenv("ROCPROFILER_COUNTERS"); if (line_c_str) { std::string line = line_c_str; - // skip commented lines - auto found = line.find_first_not_of(" \t"); - if (found != std::string::npos) { - if (line[found] == '#') return {}; - } - if (line.find("pmc") == std::string::npos) return counters; - char seperator = ' '; - std::string::size_type prev_pos = 0, pos = line.find(seperator, prev_pos); - prev_pos = ++pos; - if (pos != std::string::npos) { - while ((pos = line.find(seperator, pos)) != std::string::npos) { - std::string substring(line.substr(prev_pos, pos - prev_pos)); - if (substring.length() > 0 && substring != ":") { - counters.push_back(substring); - } - prev_pos = ++pos; - } - if (!line.substr(prev_pos, pos - prev_pos).empty()) { - counters.push_back(line.substr(prev_pos, pos - prev_pos)); - } - } + rocprofiler::validate_counters_format(counters, line); } return counters; } @@ -302,13 +282,13 @@ att_parsed_input_t GetATTParams() { } else if (param_name == "PERFCOUNTER") { counters_names.push_back(line.substr(pos + 1)); continue; - } else { // param_value is a number + } else { // param_value is a number try { - auto hexa_pos = line.find("0x", pos); // Is it hex? + auto hexa_pos = line.find("0x", pos); // Is it hex? if (hexa_pos != std::string::npos) param_value = stoi(line.substr(hexa_pos + 2), 0, 16); // hexadecimal else - param_value = stoi(line.substr(pos + 1), 0, 10); // decimal + param_value = stoi(line.substr(pos + 1), 0, 10); // decimal } catch (...) { printf("Error: Invalid parameter value %s - (%s)\n", line.substr(pos + 1, line.size()).c_str(), line.c_str()); @@ -434,8 +414,7 @@ void sync_api_trace_callback(rocprofiler_record_tracer_t tracer_record, rocprofiler_timestamp_t timestamp; CHECK_ROCPROFILER(rocprofiler_get_timestamp(×tamp)); tracer_record.timestamps = rocprofiler_record_header_timestamp_t{ - .begin = rocprofiler_timestamp_t{*hip_api_data->phase_data}, - .end = timestamp}; + .begin = rocprofiler_timestamp_t{*hip_api_data->phase_data}, .end = timestamp}; } hip_api_trace_entry_t& entry = hip_api_buffer.Emplace( tracer_record, (const char*)kernel_name_c ? strdup(kernel_name_c) : nullptr, hip_api_data); @@ -461,8 +440,7 @@ void sync_api_trace_callback(rocprofiler_record_tracer_t tracer_record, rocprofiler_timestamp_t timestamp; CHECK_ROCPROFILER(rocprofiler_get_timestamp(×tamp)); tracer_record.timestamps = rocprofiler_record_header_timestamp_t{ - .begin = rocprofiler_timestamp_t{*hsa_api_data->phase_data}, - .end = timestamp}; + .begin = rocprofiler_timestamp_t{*hsa_api_data->phase_data}, .end = timestamp}; } hsa_api_trace_entry_t& entry = hsa_api_buffer.Emplace(tracer_record, hsa_api_data); entry.valid.store(rocprofiler::TRACE_ENTRY_COMPLETE, std::memory_order_release); diff --git a/projects/rocprofiler/src/utils/helper.cpp b/projects/rocprofiler/src/utils/helper.cpp index abd9b00a98..3faa4175fd 100644 --- a/projects/rocprofiler/src/utils/helper.cpp +++ b/projects/rocprofiler/src/utils/helper.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #define amd_comgr_(call) \ @@ -223,9 +224,8 @@ bool has_special_char(std::string const& str) { // check if string has correct counter format bool has_counter_format(std::string const& str) { - return std::find_if(str.begin(), str.end(), [](unsigned char ch) { - return (isalnum(ch) || ch == '_' || ch != ':'); - }) != str.end(); + return std::find_if(str.begin(), str.end(), + [](unsigned char ch) { return (isalnum(ch) || ch == '_'); }) != str.end(); } // trims the begining of the line for spaces @@ -235,5 +235,48 @@ std::string left_trim(const std::string& s) { return (start == std::string::npos) ? "" : s.substr(start); } +// trims begining and end of input line in place +void trim(std::string& str) { + // Remove leading spaces. + str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](unsigned char ch) { + return !std::isspace(ch); + })); + // Remove trailing spaces. + str.erase( + std::find_if(str.rbegin(), str.rend(), [](unsigned char ch) { return !std::isspace(ch); }) + .base(), + str.end()); +} + +// replace unsuported specail chars with space +static void handle_special_chars(std::string& str) { + std::set specialChars = {'!', '@', '#', '$', '%', '&', '(', ')', ',', '*', '+', '-', '.', + '/', ';', '<', '=', '>', '?', '@', '{', '}', '^', '`', '~', '|'}; + + // Iterate over the string and replace any special characters with a space. + for (unsigned int i = 0; i < str.length(); i++) { + if (specialChars.find(str[i]) != specialChars.end()) { + str[i] = ' '; + } + } +} + +// validate input coutners and correct format if needed +void validate_counters_format(std::vector& counters, std::string line) { + // trim line for any white spaces + trim(line); + + if (!(line[0] == '#' || line.find("pmc") == std::string::npos)) { + handle_special_chars(line); + + std::stringstream input_line(line); + std::string counter; + while (getline(input_line, counter, ' ')) { + if (counter.substr(0, 3) != "pmc" && has_counter_format(counter)) { + counters.push_back(counter); + } + } + } +} } // namespace rocprofiler diff --git a/projects/rocprofiler/src/utils/helper.h b/projects/rocprofiler/src/utils/helper.h index 416ede50bd..5dc4d943db 100644 --- a/projects/rocprofiler/src/utils/helper.h +++ b/projects/rocprofiler/src/utils/helper.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include "exception.h" @@ -69,4 +70,7 @@ bool has_counter_format(std::string const& str); // trims the begining of the line for spaces std::string left_trim(const std::string& s); +// validates pmc user input format +void validate_counters_format(std::vector &counters, std::string line); + } // namespace rocprofiler