Files
rocm-systems/source/lib/rocprofiler-sdk-tool/config.cpp
T
Jonathan R. Madsen 5eb8c2658c rocprofv3: refactor and reorganize rocprofiler-sdk-tool library (#1138)
* Add rocprofv3-multi-node.md to source/lib/rocprofiler-sdk-tool

* Initial source re-organization

- create "output" static library

* Update include/rocprofiler-sdk/cxx/serialization.hpp

- add GPR count fields to kernel symbol serialization

* Add source/scripts/generate-rocpd.py

- reads one or more JSON output files from rocprofv3 and writes rocpd SQLite3 database
- Note: preliminary implementation

* More reorganization b/t lib/rocprofiler-sdk-tool and lib/output

* Updates to generate-rocpd.py

- add SQL views
- option: --absolute-timestamps -> --normalize-timestamps
- option: --generic-markers
- misc fixes with regards to getting the views working
- support marker names

* Update generate-rocpd.py

- Add --marker-mode option

* Update generate-rocpd.py

- Improve debugging of bad bulk SQLite statements

* Update rocprofv3-multi-node.md

- cleanup of proposed SQL schema

* lib/output/format_path.{hpp,cpp}

- rename format to format_path (in config.hpp and config.cpp)
- move format_path functionality to format_path.{hpp,cpp}

* Rework lib/output/tmp_file_buffer.{hpp,cpp}

* Update output_key.cpp

- support %cwd%, %launch_date%

* Rework lib/output/buffered_output.hpp

* Support csv_output_file constructed via domain_type

* Update lib/output/domain_type.{hpp,cpp}

- get_domain_trace_file_name
- get_domain_stats_file_name

* Update lib/rocprofiler-sdk-tool/tool.cpp

- tweak headers

* Update lib/output/generate*.cpp

- remove include of helpers.hpp
- CSV uses domain_type for filenames

* Update samples/counter_collection/per_dev_serialization.cpp

- make wait_on volatile

* Remove tool_table from lib/output and lib/rocprofiler-sdk-tool

- Also split various structs into their own files
  - lib/output/agent_info
  - lib/output/metadata
  - lib/output/kernel_symbol_info
  - lib/output/counter_info
- Implemented rocprofiler::tool::metadata

* Optimize rocprofiler_tool_counter_collection_record_t

- reduce the size of the struct from 24784 bytes to 8376 bytes

* Introduced output_config

- split subset of config (from tools library) into output_config to be able to configure the output generating functions separately from the tool library
- this is a significant step towards the output generating functions not relying on static global memory

* Stream chunks of data into output instead of loading all info memory

* Remove duplicate group_segment_size in rocprofiler_kernel_dispatch_info_t serialization

* Adding Q&A to rocprofv3-multi-node.md

* Remove all remaining include lib/rocprofiler-sdk-tool from lib/output

- migrated a fair amount of code from lib/rocprofiler-sdk-tool/helper.hpp to lib/output

* Update Q&A of rocprofv3-multi-node.md

* Fix minor compilation errors + minor cleanup

* Update hsa/async_copy.cpp

- when ROCPROFILER_CI_STRICT_TIMESTAMPS > 0, reduce the active_signal sync wait time

* Update profiling_time.hpp

- fix log messages for when start/end time is less/greater than enqueue/current CPU time

* Fix generate_stats for tool_counter_record_t

* Dictionary optimization for generate-rocpd.py

---------

Co-authored-by: SrirakshaNag <104580803+SrirakshaNag@users.noreply.github.com>
2024-11-07 01:15:19 -06:00

221 строка
6.4 KiB
C++

// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "config.hpp"
#include "lib/common/defines.hpp"
#include "lib/common/demangle.hpp"
#include "lib/common/environment.hpp"
#include "lib/common/filesystem.hpp"
#include "lib/common/logging.hpp"
#include "lib/common/units.hpp"
#include "lib/common/utility.hpp"
#include "lib/output/output_key.hpp"
#include <rocprofiler-sdk/cxx/details/tokenize.hpp>
#include <fmt/core.h>
#include <linux/limits.h>
#include <unistd.h>
#include <algorithm>
#include <chrono>
#include <cstring>
#include <ctime>
#include <fstream>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
namespace rocprofiler
{
namespace tool
{
namespace
{
const auto env_regexes =
new std::array<std::regex, 3>{std::regex{"(.*)%(env|ENV)\\{([A-Z0-9_]+)\\}%(.*)"},
std::regex{"(.*)\\$(env|ENV)\\{([A-Z0-9_]+)\\}(.*)"},
std::regex{"(.*)%q\\{([A-Z0-9_]+)\\}(.*)"}};
// env regex examples:
// - %env{USER}% Consistent with other output key formats (start+end with %)
// - $ENV{USER} Similar to CMake
// - %q{USER} Compatibility with NVIDIA
//
inline bool
not_is_space(int ch)
{
return std::isspace(ch) == 0;
}
inline std::string
ltrim(std::string s, bool (*f)(int) = not_is_space)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), f));
return s;
}
inline std::string
rtrim(std::string s, bool (*f)(int) = not_is_space)
{
s.erase(std::find_if(s.rbegin(), s.rend(), f).base(), s.end());
return s;
}
inline std::string
trim(std::string s, bool (*f)(int) = not_is_space)
{
ltrim(s, f);
rtrim(s, f);
return s;
}
// replace unsuported specail chars with space
void
handle_special_chars(std::string& str)
{
// Iterate over the string and replace any special characters with a space.
auto pos = std::string::npos;
while((pos = str.find_first_of("!@#$%&(),*+-./;<>?@{}^`~|")) != std::string::npos)
str.at(pos) = ' ';
}
bool
has_counter_format(std::string const& str)
{
return std::find_if(str.begin(), str.end(), [](unsigned char ch) {
return (isalnum(ch) != 0 || ch == '_');
}) != str.end();
}
// validate kernel names
std::unordered_set<uint32_t>
get_kernel_filter_range(const std::string& kernel_filter)
{
if(kernel_filter.empty()) return {};
auto delim = rocprofiler::sdk::parse::tokenize(kernel_filter, "[], ");
auto range_set = std::unordered_set<uint32_t>{};
for(const auto& itr : delim)
{
if(itr.find('-') != std::string::npos)
{
auto drange = rocprofiler::sdk::parse::tokenize(itr, "- ");
ROCP_FATAL_IF(drange.size() != 2)
<< "bad range format for '" << itr << "'. Expected [A-B] where A and B are numbers";
uint32_t start_range = std::stoul(drange.front());
uint32_t end_range = std::stoul(drange.back());
for(auto i = start_range; i <= end_range; i++)
range_set.emplace(i);
}
else
{
ROCP_FATAL_IF(itr.find_first_not_of("0123456789") != std::string::npos)
<< "expected integer for " << itr << ". Non-integer value detected";
range_set.emplace(std::stoul(itr));
}
}
return range_set;
}
std::set<std::string>
parse_counters(std::string line)
{
auto counters = std::set<std::string>{};
if(line.empty()) return counters;
// strip the comment
if(auto pos = std::string::npos; (pos = line.find('#')) != std::string::npos)
line = line.substr(0, pos);
// trim line for any white spaces after comment strip
trim(line);
// check to see if comment stripping + trim resulted in empty line
if(line.empty()) return counters;
constexpr auto pmc_qualifier = std::string_view{"pmc:"};
auto pos = std::string::npos;
// should we handle an "pmc:" not being present? Seems like it should be a fatal error
if((pos = line.find(pmc_qualifier)) != std::string::npos)
{
// strip out pmc qualifier
line = line.substr(pos + pmc_qualifier.length());
handle_special_chars(line);
auto input_ss = std::stringstream{line};
while(true)
{
auto counter = std::string{};
input_ss >> counter;
if(counter.empty())
break;
else if(counter != pmc_qualifier && has_counter_format(counter))
counters.emplace(counter);
}
}
return counters;
}
} // namespace
config::config()
: base_type{base_type::load_from_env()}
, kernel_filter_range{get_kernel_filter_range(
get_env("ROCPROF_KERNEL_FILTER_RANGE", std::string{}))}
, counters{parse_counters(get_env("ROCPROF_COUNTERS", std::string{}))}
{
if(kernel_filter_include.empty()) kernel_filter_include = std::string{".*"};
}
std::string
format_name(std::string_view _name, const config& _cfg)
{
if(!_cfg.demangle && !_cfg.truncate) return std::string{_name};
// truncating requires demangling first so always demangle
auto _demangled_name =
common::cxx_demangle(std::regex_replace(_name.data(), std::regex{"(\\.kd)$"}, ""));
if(_cfg.truncate) return common::truncate_name(_demangled_name);
return _demangled_name;
}
void
initialize()
{
(void) get_config<config_context::global>();
}
} // namespace tool
} // namespace rocprofiler