fe5d074375
* Adding tools support * cmake formatting (cmake-format) (#227) Co-authored-by: SrirakshaNag <SrirakshaNag@users.noreply.github.com> * Checking to do rebase * Adding rocprofv2 script * cmake formatting (cmake-format) (#229) Co-authored-by: bgopesh <bgopesh@users.noreply.github.com> * Fixing build for the tool * Removing the requirement for rocm_version * Update rocprofiler_utilities.cmake * C++ filesystem fixes - added source/lib/common/filesystem.hpp - support older compilers which have <experimental/filesystem> and do not have <filesystem> - added samples/common/filesystem.hpp - samples now depend on "common" library which provides the correct filesystem header - renamed rocprofiler-stdcxxfs interface target to rocprofiler-cxx-filesystem - support old LLVM in addition to GNU - fix bin/rocprof/rocprof.cpp - was using VLA * Fix rocprofiler-drm include directories - OpenSUSE only has include/libdrm/drm.h (no include/drm/drm.h) * Tools fixes * Fix for the tools * Fix rocprofv2 script * Fixing Filesystem Issues * source formatting (clang-format v11) (#234) Co-authored-by: ammarwa <ammarwa@users.noreply.github.com> * Vlaindic/pc sampling api update (#235) * pcs: updating PC sampling API * source formatting (clang-format v11) (#232) Co-authored-by: vlaindic <vlaindic@users.noreply.github.com> --------- Co-authored-by: vlaindic <vladimir.indic@amd.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: vlaindic <vlaindic@users.noreply.github.com> * Vlaindic/pc sampling api update for ammar branch (#244) *Updating the documentation inside pc_sampling.h --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: vlaindic <vlaindic@users.noreply.github.com> * pcs: use @p in front of params * pcs: documenting struct fields updated * Fixing PC Sampling Documentation issues * Fixing PC Sampling Documentation * Relocated tools directory to source/lib/rocprofiler-tool * Fixes/updates to rocprofiler-tool - updated CMake - Fixed miscellaneous issues in the code (VLAs, etc.) - Updated rocprofv2 to reflect some minor env variables changes in rocprofiler-tool - Fixed clang-tidy warnings * Update lib/rocprofiler-tool/CMakeLists.txt - link to atomic library * Add $ORIGIN/.. RUNPATH to rocprofiler-tool * Adding readme file for tools * Renaming the tools readme file * Update ReadMe.md * Update ReadMe.md * Documentation updates - overview and explanation of design and concepts * Fix lib/rocprofiler-tool/README.md - delete ReadMe.md * Hacks for build * Update Filesystem * cmake formatting (cmake-format) (#248) Co-authored-by: ammarwa <ammarwa@users.noreply.github.com> * source formatting (clang-format v11) (#249) Co-authored-by: ammarwa <ammarwa@users.noreply.github.com> * source formatting (clang-format v11) (#250) Co-authored-by: ammarwa <ammarwa@users.noreply.github.com> * Addressing review comments on the tool readme file * Revert "Hacks for build" This reverts commit d6688cb3d1226c46fc97e37ced889a5b0d180940. * Fixes for GCC 7.5 compiler in OpenSUSE 15.4 * Update lib/rocprofiler-tool/CMakeLists.txt - link to AQL profile library * Fix lib/rocprofiler-tool/README.md - fix markdown * Fix lib/rocprofiler-tool - fix usage of hsa_ven_amd_loader_query_host_address * Fix unused variable warnings - byproduct of variables only used in assert statements * Update docs - update about.md - more "Important Changes" section here - update tool_library_overview.md - extend "Tool Library Design" section - write "Tool Initialization" section - write "Tool Finalization" section * Add ghc::filesystem submodule * Implement usage of ghc::filesystem * Add ROCPROFILER_BUILD_GHC_FS option - option to use external/filesystem (ghc) * Update samples/counter-collection - compile flags - common library - fixes for warnings * Update tests/kernel-tracing/CMakeLists.txt - change install location of kernel-tracing-test-tool and install rpath * Update samples/common/CMakeLists.txt - compile features requiring C++17 * Update lib/rocprofiler-tool/tool.cpp - remove include <filesystem> - comment out unused variable - remove unused functions - move some functions into anonymous namespace --------- Co-authored-by: Sriraksha Nagaraj <Sriraksha.Nagaraj@amd.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: SrirakshaNag <SrirakshaNag@users.noreply.github.com> Co-authored-by: gobhardw <gopesh.bhardwaj@amd.com> Co-authored-by: bgopesh <bgopesh@users.noreply.github.com> Co-authored-by: Jonathan R. Madsen <jonathanrmadsen@gmail.com> Co-authored-by: ammarwa <ammarwa@users.noreply.github.com> Co-authored-by: vlaindic <vladimir.indic@amd.com> Co-authored-by: vlaindic <vlaindic@users.noreply.github.com> Co-authored-by: Vladimir Indic <139573562+vlaindic@users.noreply.github.com> Co-authored-by: Benjamin Welton <bewelton@amd.com> Co-authored-by: Jonathan R. Madsen <jrmadsen@users.noreply.github.com>
393 righe
12 KiB
C++
393 righe
12 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 "lib/common/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/utility.hpp"
|
|
|
|
#include <fmt/core.h>
|
|
|
|
#include <unistd.h>
|
|
#include <algorithm>
|
|
#include <cstring>
|
|
#include <ctime>
|
|
#include <fstream>
|
|
#include <regex>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace rocprofiler
|
|
{
|
|
namespace common
|
|
{
|
|
namespace
|
|
{
|
|
std::time_t* launch_time = new std::time_t{std::time(nullptr)};
|
|
|
|
std::string
|
|
get_local_datetime(const char* dt_format, std::time_t* dt_curr)
|
|
{
|
|
char mbstr[512];
|
|
if(!dt_curr) dt_curr = launch_time;
|
|
|
|
if(std::strftime(mbstr, sizeof(mbstr), dt_format, std::localtime(dt_curr)) != 0)
|
|
return std::string{mbstr};
|
|
return std::string{};
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
inline std::vector<pid_t>
|
|
get_siblings(pid_t _id = getppid())
|
|
{
|
|
auto _data = std::vector<pid_t>{};
|
|
|
|
std::ifstream _ifs{"/proc/" + std::to_string(_id) + "/task/" + std::to_string(_id) +
|
|
"/children"};
|
|
while(_ifs)
|
|
{
|
|
pid_t _n = 0;
|
|
_ifs >> _n;
|
|
if(!_ifs || _n <= 0) break;
|
|
_data.emplace_back(_n);
|
|
}
|
|
return _data;
|
|
}
|
|
|
|
inline auto
|
|
get_num_siblings(pid_t _id = getppid())
|
|
{
|
|
return get_siblings(_id).size();
|
|
}
|
|
} // namespace
|
|
|
|
int
|
|
get_mpi_size()
|
|
{
|
|
static int _v = get_env<int>("OMPI_COMM_WORLD_SIZE",
|
|
get_env<int>("MV2_COMM_WORLD_SIZE", get_env<int>("MPI_SIZE", 0)));
|
|
return _v;
|
|
}
|
|
|
|
int
|
|
get_mpi_rank()
|
|
{
|
|
static int _v = get_env<int>("OMPI_COMM_WORLD_RANK",
|
|
get_env<int>("MV2_COMM_WORLD_RANK", get_env<int>("MPI_RANK", -1)));
|
|
return _v;
|
|
}
|
|
|
|
std::vector<output_key>
|
|
output_keys(std::string _tag)
|
|
{
|
|
using strpair_t = std::pair<std::string, std::string>;
|
|
|
|
auto _cmdline = read_command_line(getpid());
|
|
|
|
if(_tag.empty() && !_cmdline.empty()) _tag = ::basename(_cmdline.front().c_str());
|
|
|
|
std::string _argv_string = {}; // entire argv cmd
|
|
std::string _args_string = {}; // cmdline args
|
|
std::string _argt_string = _tag; // prefix + cmdline args
|
|
const std::string& _tag0_string = _tag; // only the basic prefix
|
|
auto _options = std::vector<output_key>{};
|
|
|
|
auto _replace = [](auto& _v, const strpair_t& pitr) {
|
|
auto pos = std::string::npos;
|
|
while((pos = _v.find(pitr.first)) != std::string::npos)
|
|
_v.replace(pos, pitr.first.length(), pitr.second);
|
|
};
|
|
|
|
if(_cmdline.size() > 1 && _cmdline.at(1) == "--") _cmdline.erase(_cmdline.begin() + 1);
|
|
|
|
for(auto& itr : _cmdline)
|
|
{
|
|
itr = trim(itr);
|
|
_replace(itr, {"/", "_"});
|
|
while(!itr.empty() && itr.at(0) == '.')
|
|
itr = itr.substr(1);
|
|
while(!itr.empty() && itr.at(0) == '_')
|
|
itr = itr.substr(1);
|
|
}
|
|
|
|
if(!_cmdline.empty())
|
|
{
|
|
for(size_t i = 0; i < _cmdline.size(); ++i)
|
|
{
|
|
const auto _l = std::string{(i == 0) ? "" : "_"};
|
|
auto _v = _cmdline.at(i);
|
|
_argv_string += _l + _v;
|
|
if(i > 0)
|
|
{
|
|
_argt_string += (i > 1) ? (_l + _v) : _v;
|
|
_args_string += (i > 1) ? (_l + _v) : _v;
|
|
}
|
|
}
|
|
}
|
|
|
|
auto* _launch_time = launch_time;
|
|
auto _time_format = get_env<std::string>("ROCP_TIME_FORMAT", "%F_%H.%M");
|
|
|
|
auto _mpi_size = get_env<int>("OMPI_COMM_WORLD_SIZE", get_env<int>("MV2_COMM_WORLD_SIZE", 0));
|
|
auto _mpi_rank = get_env<int>("OMPI_COMM_WORLD_RANK", get_env<int>("MV2_COMM_WORLD_RANK", -1));
|
|
|
|
auto _dmp_size = fmt::format("{}", (_mpi_size) > 0 ? _mpi_size : 1);
|
|
auto _dmp_rank = fmt::format("{}", (_mpi_rank) > 0 ? _mpi_rank : 0);
|
|
auto _proc_id = fmt::format("{}", getpid());
|
|
auto _parent_id = fmt::format("{}", getppid());
|
|
auto _pgroup_id = fmt::format("{}", getpgid(getpid()));
|
|
auto _session_id = fmt::format("{}", getsid(getpid()));
|
|
auto _proc_size = fmt::format("{}", get_num_siblings());
|
|
auto _pwd_string = get_env<std::string>("PWD", ".");
|
|
auto _slurm_job_id = get_env<std::string>("SLURM_JOB_ID", "0");
|
|
auto _slurm_proc_id = get_env("SLURM_PROCID", _dmp_rank);
|
|
auto _launch_string = get_local_datetime(_time_format.c_str(), _launch_time);
|
|
|
|
auto _uniq_id = _proc_id;
|
|
if(get_env<int32_t>("SLURM_PROCID", -1) >= 0)
|
|
{
|
|
_uniq_id = _slurm_proc_id;
|
|
}
|
|
else if(_mpi_size > 0 || _mpi_rank >= 0)
|
|
{
|
|
_uniq_id = _dmp_rank;
|
|
}
|
|
|
|
for(auto&& itr : std::initializer_list<output_key>{
|
|
{"%argv%", _argv_string, "Entire command-line condensed into a single string"},
|
|
{"%argt%",
|
|
_argt_string,
|
|
"Similar to `%argv%` except basename of first command line argument"},
|
|
{"%args%", _args_string, "All command line arguments condensed into a single string"},
|
|
{"%tag%", _tag0_string, "Basename of first command line argument"}})
|
|
{
|
|
_options.emplace_back(itr);
|
|
}
|
|
|
|
if(!_cmdline.empty())
|
|
{
|
|
for(size_t i = 0; i < _cmdline.size(); ++i)
|
|
{
|
|
auto _v = _cmdline.at(i);
|
|
_options.emplace_back(fmt::format("%arg{}%", i), _v, fmt::format("Argument #{}", i));
|
|
}
|
|
}
|
|
|
|
for(auto&& itr : std::initializer_list<output_key>{
|
|
{"%pid%", _proc_id, "Process identifier"},
|
|
{"%ppid%", _parent_id, "Parent process identifier"},
|
|
{"%pgid%", _pgroup_id, "Process group identifier"},
|
|
{"%psid%", _session_id, "Process session identifier"},
|
|
{"%psize%", _proc_size, "Number of sibling process"},
|
|
{"%job%", _slurm_job_id, "SLURM_JOB_ID env variable"},
|
|
{"%rank%", _slurm_proc_id, "MPI/UPC++ rank"},
|
|
{"%size%", _dmp_size, "MPI/UPC++ size"},
|
|
{"%nid%", _uniq_id, "%rank% if possible, otherwise %pid%"},
|
|
{"%launch_time%", _launch_string, "Data and/or time of run according to time format"},
|
|
})
|
|
{
|
|
_options.emplace_back(itr);
|
|
}
|
|
|
|
for(auto&& itr : std::initializer_list<output_key>{
|
|
{"%p", _proc_id, "Shorthand for %pid%"},
|
|
{"%j", _slurm_job_id, "Shorthand for %job%"},
|
|
{"%r", _slurm_proc_id, "Shorthand for %rank%"},
|
|
{"%s", _dmp_size, "Shorthand for %size"},
|
|
})
|
|
{
|
|
_options.emplace_back(itr);
|
|
}
|
|
|
|
return _options;
|
|
}
|
|
|
|
std::string
|
|
format(std::string _fpath, const std::string& _tag)
|
|
{
|
|
if(_fpath.find('%') == std::string::npos && _fpath.find('$') == std::string::npos)
|
|
return _fpath;
|
|
|
|
auto _replace = [](auto& _v, const output_key& pitr) {
|
|
auto pos = std::string::npos;
|
|
while((pos = _v.find(pitr.key)) != std::string::npos)
|
|
_v.replace(pos, pitr.key.length(), pitr.value);
|
|
};
|
|
|
|
for(auto&& itr : output_keys(_tag))
|
|
_replace(_fpath, itr);
|
|
|
|
// environment and configuration variables
|
|
try
|
|
{
|
|
for(const auto& _expr : {std::string{"(.*)%(env|ENV)\\{([A-Z0-9_]+)\\}%(.*)"},
|
|
std::string{"(.*)\\$(env|ENV)\\{([A-Z0-9_]+)\\}(.*)"}})
|
|
{
|
|
std::regex _re{_expr};
|
|
std::string _cbeg = (_expr.find("(.*)%") == 0) ? "%" : "$";
|
|
std::string _cend = (_expr.find("(.*)%") == 0) ? "}%" : "}";
|
|
bool _is_env = (_expr.find("(env|ENV)") != std::string::npos);
|
|
_cbeg += (_is_env) ? "env{" : "cfg{";
|
|
while(std::regex_search(_fpath, _re))
|
|
{
|
|
auto _var = std::regex_replace(_fpath, _re, "$3");
|
|
std::string _val = {};
|
|
if(_is_env)
|
|
{
|
|
_val = get_env<std::string>(_var, "");
|
|
}
|
|
auto _beg = std::regex_replace(_fpath, _re, "$1");
|
|
auto _end = std::regex_replace(_fpath, _re, "$4");
|
|
_fpath = fmt::format("{}{}{}", _beg, _val, _end);
|
|
}
|
|
}
|
|
} catch(std::exception& _e)
|
|
{
|
|
LOG(WARNING) << "[rocprofiler] " << __FUNCTION__ << " threw an exception :: " << _e.what()
|
|
<< "\n";
|
|
}
|
|
|
|
// remove %arg<N>% where N >= argc
|
|
try
|
|
{
|
|
std::regex _re{"(.*)%(arg[0-9]+)%([-/_]*)(.*)"};
|
|
while(std::regex_search(_fpath, _re))
|
|
_fpath = std::regex_replace(_fpath, _re, "$1$4");
|
|
} catch(std::exception& _e)
|
|
{
|
|
LOG(WARNING) << "[rocprofiler] " << __FUNCTION__ << " threw an exception :: " << _e.what()
|
|
<< "\n";
|
|
}
|
|
|
|
return _fpath;
|
|
}
|
|
|
|
std::string
|
|
compose_filename(const config& _cfg)
|
|
{
|
|
auto _output_path = _cfg.output_path;
|
|
auto _output_file = _cfg.output_file;
|
|
auto _output_ext = _cfg.output_ext;
|
|
|
|
if(_output_path.empty()) _output_path = ".";
|
|
if(_cfg.mpi_size > 0)
|
|
{
|
|
if(_cfg.mpi_rank >= 0)
|
|
{
|
|
_output_file = fmt::format("{}.{}", _output_file, _cfg.mpi_rank);
|
|
}
|
|
else
|
|
{
|
|
_output_file = fmt::format("{}.{}", _output_file, getpid());
|
|
}
|
|
}
|
|
if(!_output_ext.empty())
|
|
{
|
|
if(_output_ext.find('.') == std::string::npos) _output_ext.insert(0, ".");
|
|
if(_output_file.length() < _output_ext.length() ||
|
|
_output_file.find(_output_ext) != _output_file.length() - _output_ext.length())
|
|
_output_file += _output_ext;
|
|
}
|
|
|
|
// join <OUTPUT_PATH>/<OUTPUT_FILE> and replace any keys with values
|
|
auto _prefix = format(common::filesystem::path{_output_path} / _output_file);
|
|
|
|
// return on empty
|
|
if(_prefix.empty()) return std::string{};
|
|
|
|
// get the absolute path
|
|
auto _fname = common::filesystem::absolute(common::filesystem::path{_prefix});
|
|
|
|
// create the directory if necessary
|
|
auto _fname_path = _fname.parent_path();
|
|
if(!common::filesystem::exists(_fname_path))
|
|
common::filesystem::create_directories(_fname.parent_path());
|
|
|
|
return _fname.string();
|
|
}
|
|
|
|
std::string
|
|
format_name(std::string_view _name, const config& _cfg)
|
|
{
|
|
if(_cfg.demangle && _cfg.truncate)
|
|
{
|
|
return truncate_name(cxx_demangle(_name));
|
|
}
|
|
|
|
if(_cfg.demangle)
|
|
{
|
|
return cxx_demangle(_name);
|
|
}
|
|
|
|
if(_cfg.truncate)
|
|
{
|
|
return truncate_name(_name);
|
|
}
|
|
|
|
return std::string{_name};
|
|
}
|
|
|
|
void
|
|
initialize()
|
|
{
|
|
(void) get_config<config_context::global>();
|
|
}
|
|
|
|
output_key::output_key(std::string _key, std::string _val, std::string _desc)
|
|
: key{std::move(_key)}
|
|
, value{std::move(_val)}
|
|
, description{std::move(_desc)}
|
|
{}
|
|
} // namespace common
|
|
} // namespace rocprofiler
|