refactor: duplicated path helpers into common/path.hpp (#1249)

* refactor: duplicated path helpers into common/path.hpp

* update rocprof-sys-instrument to use shared path utility

* Add path::realpath(std::string[, std::string*]) helper function in common/path.hpp for binaries

* common: centralize remove_env implementation in environment.hpp

* remove unused includes from rocprof-sys binaries and argparse

* changing set to unordered_set wherever sorting is not required and additional cleanup

* review comment incorporated

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* copilot review for remove_env incorporated

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Этот коммит содержится в:
habajpai-amd
2025-11-20 09:55:00 +05:30
коммит произвёл GitHub
родитель 2e0e613397
Коммит b09834e784
15 изменённых файлов: 165 добавлений и 357 удалений
+13 -70
Просмотреть файл
@@ -23,10 +23,9 @@
#include "rocprof-sys-causal.hpp"
#include "common/defines.h"
#include "common/delimit.hpp"
#include "common/environment.hpp"
#include "common/join.hpp"
#include "common/setup.hpp"
#include "common/path.hpp"
#include "core/mproc.hpp"
#include "core/utility.hpp"
@@ -38,8 +37,6 @@
#include <timemory/utility/filepath.hpp>
#include <timemory/utility/join.hpp>
#include <array>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdio>
@@ -52,7 +49,6 @@
#include <string>
#include <string_view>
#include <sys/wait.h>
#include <thread>
#include <unistd.h>
#include <vector>
@@ -60,6 +56,7 @@ namespace color = ::tim::log::color;
namespace filepath = ::tim::filepath;
namespace console = ::tim::utility::console;
namespace argparse = ::tim::argparse;
namespace path = rocprofsys::common::path;
using namespace ::timemory::join;
using ::rocprofsys::utility::parse_numeric_range;
using ::tim::get_env;
@@ -78,9 +75,9 @@ to_string(bool _v)
namespace
{
int verbose = 0;
auto updated_envs = std::set<std::string_view>{};
auto original_envs = std::set<std::string>{};
auto child_pids = std::set<pid_t>{};
auto updated_envs = std::unordered_set<std::string_view>{};
auto original_envs = std::unordered_set<std::string>{};
auto child_pids = std::unordered_set<pid_t>{};
auto launcher = std::string{};
inline signal_handler&
@@ -160,15 +157,6 @@ diagnose_status(pid_t _pid, int _status)
return ::rocprofsys::mproc::diagnose_status(_pid, _status, get_verbose());
}
std::string
get_realpath(const std::string& _v)
{
auto* _tmp = realpath(_v.c_str(), nullptr);
auto _ret = std::string{ _tmp };
free(_tmp);
return _ret;
}
void
print_command(const std::vector<char*>& _argv, std::string_view _prefix)
{
@@ -250,45 +238,16 @@ prepare_environment_for_run(std::vector<char*>& _env)
{
if(launcher.empty())
{
update_env(_env, "LD_PRELOAD",
join(":", LIBPTHREAD_SO,
get_realpath(get_internal_libpath("librocprof-sys-dl.so"))),
true);
update_env(_env, "ROCPROFSYS_SCRIPT_DIR", get_internal_script_path());
update_env(_env, "ROCPROFSYS_ROOT", get_rocprofsys_root());
update_env(
_env, "LD_PRELOAD",
join(":", LIBPTHREAD_SO,
path::realpath(path::get_internal_libpath("librocprof-sys-dl.so"))),
true);
update_env(_env, "ROCPROFSYS_SCRIPT_DIR", path::get_internal_script_path());
update_env(_env, "ROCPROFSYS_ROOT", path::get_rocprofsys_root());
}
}
std::string
get_rocprofsys_root(void)
{
char* _tmp = realpath("/proc/self/exe", nullptr);
std::string _exe = (_tmp) ? std::string{ _tmp } : std::string{};
if(_tmp) free(_tmp);
auto _pos = _exe.find_last_of('/');
auto _dir = std::string{ "./" };
if(_pos != std::string::npos) _dir = _exe.substr(0, _pos);
return rocprofsys::common::join("/", _dir, "..");
}
std::string
get_internal_libpath(const std::string& _lib)
{
auto _root = get_rocprofsys_root();
return rocprofsys::common::join("/", _root, "lib", _lib);
}
std::string
get_internal_script_path(void)
{
auto _root = get_rocprofsys_root();
return rocprofsys::common::join("/", _root, "libexec", "rocprofiler-systems");
}
void
print_updated_environment(std::vector<char*> _env, std::string_view _prefix)
{
@@ -398,22 +357,6 @@ add_default_env(std::vector<char*>& _environ, std::string_view _env_var, Tp&& _e
strdup(rocprofsys::common::join('=', _env_var, _env_val).c_str()));
}
void
remove_env(std::vector<char*>& _environ, std::string_view _env_var)
{
auto _key = join("", _env_var, "=");
auto _match = [&_key](auto itr) { return std::string_view{ itr }.find(_key) == 0; };
_environ.erase(std::remove_if(_environ.begin(), _environ.end(), _match),
_environ.end());
for(const auto& itr : original_envs)
{
if(std::string_view{ itr }.find(_key) == 0)
_environ.emplace_back(strdup(itr.c_str()));
}
}
std::vector<char*>
parse_args(int argc, char** argv, std::vector<char*>& _env,
std::vector<std::map<std::string_view, std::string>>& _causal_envs)
@@ -422,7 +365,7 @@ parse_args(int argc, char** argv, std::vector<char*>& _env,
using parser_err_t = typename parser_t::result_type;
auto help_check = [](parser_t& p, int _argc, char** _argv) {
std::set<std::string> help_args = { "-h", "--help", "-?" };
std::unordered_set<std::string> help_args = { "-h", "--help", "-?" };
return (p.exists("help") || _argc == 1 ||
(_argc > 1 && help_args.find(_argv[1]) != help_args.end()));
};
-1
Просмотреть файл
@@ -24,7 +24,6 @@
#include <timemory/log/macros.hpp>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
-15
Просмотреть файл
@@ -35,9 +35,6 @@
int
get_verbose();
std::string
get_realpath(const std::string&);
void
print_command(const std::vector<char*>& _argv, std::string_view);
@@ -52,15 +49,6 @@ prepare_command_for_run(char*, std::vector<char*>&);
void
prepare_environment_for_run(std::vector<char*>&);
std::string
get_rocprofsys_root(void);
std::string
get_internal_libpath(const std::string& _lib);
std::string
get_internal_script_path(void);
template <typename Tp>
void
update_env(std::vector<char*>&, std::string_view, Tp&&, bool _append = false,
@@ -70,9 +58,6 @@ template <typename Tp>
void
add_default_env(std::vector<char*>&, std::string_view, Tp&&);
void
remove_env(std::vector<char*>&, std::string_view);
std::vector<char*>
parse_args(int argc, char** argv, std::vector<char*>&,
std::vector<std::map<std::string_view, std::string>>&);
+21 -37
Просмотреть файл
@@ -23,6 +23,7 @@
#include "rocprof-sys-instrument.hpp"
#include "common/defines.h"
#include "common/join.hpp"
#include "common/path.hpp"
#include "dl/dl.hpp"
#include "fwd.hpp"
#include "internal_libs.hpp"
@@ -43,7 +44,6 @@
#include <timemory/utility/signals.hpp>
#include <algorithm>
#include <chrono>
#include <csignal>
#include <cstddef>
#include <cstdint>
@@ -159,6 +159,7 @@ namespace
namespace process = tim::process; // NOLINT
namespace signals = tim::signals;
namespace filepath = tim::filepath;
namespace path = rocprofsys::common::path;
using signal_settings = tim::signals::signal_settings;
using sys_signal = tim::signals::sys_signal;
@@ -192,25 +193,17 @@ strset_t print_formats = { "txt", "json
std::string modfunc_dump_dir = {};
auto regex_opts = std::regex_constants::egrep | std::regex_constants::optimize;
std::string
get_internal_libpath()
{
auto _exe = std::string_view{ ::realpath("/proc/self/exe", nullptr) };
auto _pos = _exe.find_last_of('/');
auto _dir = std::string{ "./" };
if(_pos != std::string_view::npos) _dir = _exe.substr(0, _pos);
return rocprofsys::common::join("/", _dir, "..", "lib");
}
strvec_t lib_search_paths = tim::delimit(
JOIN(':', get_internal_libpath(), tim::get_env<std::string>("DYNINSTAPI_RT_LIB"),
tim::get_env<std::string>("DYNINST_REWRITER_PATHS"),
tim::get_env<std::string>("LD_LIBRARY_PATH")),
":");
strvec_t lib_search_paths =
tim::delimit(rocprofsys::join(':', path::get_internal_libdir(),
tim::get_env<std::string>("DYNINSTAPI_RT_LIB"),
tim::get_env<std::string>("DYNINST_REWRITER_PATHS"),
tim::get_env<std::string>("LD_LIBRARY_PATH")),
":");
strvec_t bin_search_paths = tim::delimit(tim::get_env<std::string>("PATH"), ":");
auto _dyn_api_rt_paths = tim::delimit(
JOIN(":", get_internal_libpath(), JOIN("/", get_internal_libpath(), "rocprofsys")),
rocprofsys::join(":", path::get_internal_libdir(),
rocprofsys::join("/", path::get_internal_libdir(), "rocprofsys")),
":");
std::string
@@ -234,9 +227,6 @@ is_file(std::string _name);
bool
is_directory(std::string _name);
std::string
get_realpath(const std::string&);
std::string
get_cwd();
@@ -334,10 +324,10 @@ main(int argc, char** argv)
"::", "rocprofiler-systems root path: ", _omni_root);
}
auto _omni_exe_path = get_realpath(get_absolute_exe_filepath(argv[0]));
auto _omni_exe_path = path::realpath(get_absolute_exe_filepath(argv[0]));
if(!exists(_omni_exe_path))
_omni_exe_path =
get_realpath(get_absolute_exe_filepath(rocprofsys_get_exe_realpath()));
path::realpath(get_absolute_exe_filepath(rocprofsys_get_exe_realpath()));
bin_search_paths.emplace_back(filepath::dirname(_omni_exe_path));
auto _omni_lib_path =
@@ -464,7 +454,7 @@ main(int argc, char** argv)
if(_cmdc > 0 && !mutname.empty())
{
auto resolved_mutname = get_realpath(get_absolute_filepath(mutname));
auto resolved_mutname = path::realpath(get_absolute_filepath(mutname));
if(resolved_mutname != mutname)
{
mutname = resolved_mutname;
@@ -670,7 +660,7 @@ main(int argc, char** argv)
p.print_help(extra_help);
std::exit(EXIT_FAILURE);
}
keys.at(0) = get_realpath(get_absolute_filepath(keys.at(0)));
keys.at(0) = path::realpath(get_absolute_filepath(keys.at(0)));
mutname = keys.at(0);
_cmdc = keys.size();
_cmdv = new char*[_cmdc];
@@ -1184,7 +1174,7 @@ main(int argc, char** argv)
!parser.exists("min-instructions") &&
!parser.exists("min-address-range-loop"));
auto _rocprofsys_exe_path = tim::dirname(::get_realpath("/proc/self/exe"));
auto _rocprofsys_exe_path = tim::dirname(path::realpath("/proc/self/exe"));
verbprintf(4, "rocprof-sys exe path: %s\n", _rocprofsys_exe_path.c_str());
if(_cmdv && _cmdv[0] && strlen(_cmdv[0]) > 0)
@@ -1232,7 +1222,7 @@ main(int argc, char** argv)
if(binary_rewrite && outfile.empty())
{
auto _is_local = (get_realpath(cmdv0) ==
auto _is_local = (path::realpath(cmdv0) ==
TIMEMORY_JOIN('/', get_cwd(), ::basename(cmdv0.c_str())));
auto _cmd = std::string{ ::basename(cmdv0.c_str()) };
if(_cmd.find('.') == std::string::npos)
@@ -1481,8 +1471,8 @@ main(int argc, char** argv)
image_t* app_image = addr_space->getImage();
std::vector<module_t*>* app_modules = app_image->getModules();
std::vector<procedure_t*>* app_functions = app_image->getProcedures(include_uninstr);
std::set<module_t*> modules = {};
std::set<procedure_t*> functions = {};
std::unordered_set<module_t*> modules = {};
std::unordered_set<procedure_t*> functions = {};
if(app_modules) process_modules(*app_modules);
@@ -1655,7 +1645,7 @@ main(int argc, char** argv)
for(auto _libname : _libnames)
{
ROCPROFSYS_ADD_LOG_ENTRY("Getting the absolute lib filepath to", _libname);
_libname = get_realpath(get_absolute_lib_filepath(_libname));
_libname = path::realpath(get_absolute_lib_filepath(_libname));
_tried_libs += string_t("|") + _libname;
verbprintf(1, "loading library: '%s'...\n", _libname.c_str());
result = (addr_space->loadLibrary(_libname.c_str()) != nullptr);
@@ -2843,7 +2833,7 @@ exists(const std::string& name)
bool
is_file(std::string _name)
{
_name = get_realpath(_name);
_name = path::realpath(_name);
struct stat buffer;
return (stat(_name.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode) != 0);
}
@@ -2851,17 +2841,11 @@ is_file(std::string _name)
bool
is_directory(std::string _name)
{
_name = get_realpath(_name);
_name = path::realpath(_name);
struct stat buffer;
return (stat(_name.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode) != 0);
}
std::string
get_realpath(const std::string& _f)
{
return filepath::realpath(_f, nullptr, false);
}
std::string
get_cwd()
{
-1
Просмотреть файл
@@ -32,7 +32,6 @@
#include <timemory/utility/join.hpp>
#include <dlfcn.h>
#include <ios>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
+5 -53
Просмотреть файл
@@ -23,13 +23,10 @@
#include "rocprof-sys-run.hpp"
#include "common/defines.h"
#include "common/delimit.hpp"
#include "common/environment.hpp"
#include "common/join.hpp"
#include "common/setup.hpp"
#include "common/path.hpp"
#include "core/argparse.hpp"
#include "core/config.hpp"
#include "core/state.hpp"
#include "core/timemory.hpp"
#include <timemory/environment.hpp>
@@ -43,11 +40,8 @@
#include <timemory/utility/filepath.hpp>
#include <timemory/utility/join.hpp>
#include <array>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@@ -57,7 +51,6 @@
#include <string>
#include <string_view>
#include <sys/wait.h>
#include <thread>
#include <unistd.h>
#include <vector>
@@ -66,6 +59,7 @@ namespace filepath = ::tim::filepath; // NOLINT
namespace console = ::tim::utility::console;
namespace argparse = ::tim::argparse;
namespace signals = ::tim::signals;
namespace path = rocprofsys::common::path;
using settings = ::rocprofsys::settings;
using namespace ::timemory::join;
using ::tim::get_env;
@@ -82,7 +76,7 @@ to_string(bool _v)
namespace
{
auto original_envs = std::set<std::string>{};
auto original_envs = std::unordered_set<std::string>{};
enum update_mode : int
{
UPD_REPLACE = 0, // no PREPEND/APPEND bits set
@@ -91,48 +85,6 @@ enum update_mode : int
UPD_WEAK = 1 << 2, // 0x04
};
std::string
get_rocprofsys_root(void)
{
char* _tmp = realpath("/proc/self/exe", nullptr);
std::string _exe = (_tmp) ? std::string{ _tmp } : std::string{};
if(_tmp) free(_tmp);
auto _pos = _exe.find_last_of('/');
auto _dir = std::string{ "./" };
if(_pos != std::string::npos) _dir = _exe.substr(0, _pos);
return rocprofsys::common::join("/", _dir, "..");
}
std::string
get_internal_libpath(const std::string& _lib)
{
auto _root = get_rocprofsys_root();
return rocprofsys::common::join("/", _root, "lib", _lib);
}
std::string
get_internal_script_path(void)
{
auto _root = get_rocprofsys_root();
return rocprofsys::common::join("/", _root, "libexec", "rocprofiler-systems");
}
std::string
get_realpath(const std::string& _v)
{
if(auto* _tmp = realpath(_v.c_str(), nullptr))
{
std::string _ret{ _tmp };
free(_tmp);
return _ret;
}
return {};
}
template <typename Tp>
void
update_env(std::vector<char*>& _environ, std::string_view _env_var, Tp&& _env_val,
@@ -217,7 +169,7 @@ get_initial_environment(parser_data_t& _data)
}
}
auto _libexecpath = get_realpath(get_internal_script_path());
auto _libexecpath = path::realpath(path::get_internal_script_path());
if(!_libexecpath.empty())
{
update_env(_data.current, "ROCPROFSYS_SCRIPT_PATH", _libexecpath, UPD_REPLACE);
@@ -362,7 +314,7 @@ parse_args(int argc, char** argv, parser_data_t& _parser_data, bool& _fork_exec)
using parser_err_t = typename parser_t::result_type;
auto help_check = [](parser_t& p, int _argc, char** _argv) {
std::set<std::string> help_args = { "-h", "--help", "-?" };
std::unordered_set<std::string> help_args = { "-h", "--help", "-?" };
return (p.exists("help") || _argc == 1 ||
(_argc > 1 && help_args.find(_argv[1]) != help_args.end()));
};
-4
Просмотреть файл
@@ -26,13 +26,9 @@
#include <timemory/log/color.hpp>
#include <timemory/log/macros.hpp>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string_view>
#include <unistd.h>
-4
Просмотреть файл
@@ -25,12 +25,8 @@
#include "core/argparse.hpp"
#include <csignal>
#include <map>
#include <sched.h>
#include <set>
#include <string>
#include <string_view>
#include <vector>
using parser_data_t = rocprofsys::argparse::parser_data;
+13 -73
Просмотреть файл
@@ -22,10 +22,9 @@
#include "rocprof-sys-sample.hpp"
#include "common/delimit.hpp"
#include "common/environment.hpp"
#include "common/join.hpp"
#include "common/setup.hpp"
#include "common/path.hpp"
#include <timemory/environment.hpp>
#include <timemory/log/color.hpp>
@@ -34,7 +33,6 @@
#include <timemory/utility/filepath.hpp>
#include <timemory/utility/join.hpp>
#include <array>
#include <cstdint>
#include <cstdio>
#include <cstring>
@@ -45,7 +43,9 @@
#include <vector>
namespace color = tim::log::color;
namespace path = rocprofsys::common::path;
using namespace timemory::join;
using rocprofsys::common::remove_env;
using tim::get_env;
using tim::log::monochrome;
using tim::log::stream;
@@ -53,8 +53,8 @@ using tim::log::stream;
namespace
{
int verbose = 0;
auto updated_envs = std::set<std::string_view>{};
auto original_envs = std::set<std::string>{};
auto updated_envs = std::unordered_set<std::string_view>{};
auto original_envs = std::unordered_set<std::string>{};
auto clock_id_choices = []() {
auto clock_name = [](std::string _v) {
constexpr auto _clock_prefix = std::string_view{ "clock_" };
@@ -90,15 +90,6 @@ auto clock_id_choices = []() {
}();
} // namespace
std::string
get_realpath(const std::string& _v)
{
auto* _tmp = realpath(_v.c_str(), nullptr);
auto _ret = std::string{ _tmp };
free(_tmp);
return _ret;
}
void
print_command(const std::vector<char*>& _argv)
{
@@ -122,10 +113,10 @@ get_initial_environment()
}
}
auto _dl_libpath = get_realpath(get_internal_libpath("librocprof-sys-dl.so"));
auto _omni_libpath = get_realpath(get_internal_libpath("librocprof-sys.so"));
auto _libexecpath = get_realpath(get_internal_script_path());
auto _rootpath = get_realpath(get_rocprofsys_root());
auto _dl_libpath = path::realpath(path::get_internal_libpath("librocprof-sys-dl.so"));
auto _omni_libpath = path::realpath(path::get_internal_libpath("librocprof-sys.so"));
auto _libexecpath = path::realpath(path::get_internal_script_path());
auto _rootpath = path::realpath(path::get_rocprofsys_root());
update_env(_env, "ROCPROFSYS_ROOT", _rootpath, UPD_REPLACE);
update_env(_env, "LD_PRELOAD", _dl_libpath, UPD_APPEND);
@@ -146,36 +137,6 @@ get_initial_environment()
return _env;
}
std::string
get_rocprofsys_root(void)
{
char* _tmp = realpath("/proc/self/exe", nullptr);
std::string _exe = (_tmp) ? std::string{ _tmp } : std::string{};
if(_tmp) free(_tmp);
auto _pos = _exe.find_last_of('/');
auto _dir = std::string{ "./" };
if(_pos != std::string::npos) _dir = _exe.substr(0, _pos);
return rocprofsys::common::join("/", _dir, "..");
}
std::string
get_internal_libpath(const std::string& _lib)
{
auto _root = get_rocprofsys_root();
return rocprofsys::common::join("/", _root, "lib", _lib);
}
std::string
get_internal_script_path(void)
{
auto _root = get_rocprofsys_root();
return rocprofsys::common::join("/", _root, "libexec", "rocprofiler-systems");
}
void
print_updated_environment(std::vector<char*> _env)
{
@@ -282,22 +243,6 @@ update_env(std::vector<char*>& _environ, std::string_view _env_var, Tp&& _env_va
strdup(rocprofsys::common::join('=', _env_var, _env_val).c_str()));
}
void
remove_env(std::vector<char*>& _environ, std::string_view _env_var)
{
auto _key = join("", _env_var, "=");
auto _match = [&_key](auto itr) { return std::string_view{ itr }.find(_key) == 0; };
_environ.erase(std::remove_if(_environ.begin(), _environ.end(), _match),
_environ.end());
for(const auto& itr : original_envs)
{
if(std::string_view{ itr }.find(_key) == 0)
_environ.emplace_back(strdup(itr.c_str()));
}
}
std::vector<char*>
parse_args(int argc, char** argv, std::vector<char*>& _env)
{
@@ -305,7 +250,7 @@ parse_args(int argc, char** argv, std::vector<char*>& _env)
using parser_err_t = typename parser_t::result_type;
auto help_check = [](parser_t& p, int _argc, char** _argv) {
std::set<std::string> help_args = { "-h", "--help", "-?" };
std::unordered_set<std::string> help_args = { "-h", "--help", "-?" };
return (p.exists("help") || _argc == 1 ||
(_argc > 1 && help_args.find(_argv[1]) != help_args.end()));
};
@@ -327,10 +272,8 @@ parse_args(int argc, char** argv, std::vector<char*>& _env)
exit(_pec);
};
auto* _dl_libpath =
realpath(get_internal_libpath("librocprof-sys-dl.so").c_str(), nullptr);
auto* _omni_libpath =
realpath(get_internal_libpath("librocprof-sys.so").c_str(), nullptr);
auto _dl_libpath = path::realpath(path::get_internal_libpath("librocprof-sys-dl.so"));
auto _omni_libpath = path::realpath(path::get_internal_libpath("librocprof-sys.so"));
auto parser = parser_t(argv[0]);
@@ -810,7 +753,7 @@ parse_args(int argc, char** argv, std::vector<char*>& _env)
_update("ROCPROFSYS_TRACE_THREAD_SPIN_LOCKS", _v.count("spin-locks") > 0);
if(_v.count("all") > 0 || _v.count("kokkosp") > 0)
remove_env(_env, "KOKKOS_TOOLS_LIBS");
remove_env(_env, "KOKKOS_TOOLS_LIBS", original_envs);
});
parser.start_group("HARDWARE COUNTER OPTIONS", "See also: rocprof-sys-avail -H");
@@ -875,8 +818,5 @@ parse_args(int argc, char** argv, std::vector<char*>& _env)
throw std::runtime_error(
"Error! '--profile' argument conflicts with '--flat-profile' argument");
free(_dl_libpath);
free(_omni_libpath);
return _outv;
}
-2
Просмотреть файл
@@ -22,8 +22,6 @@
#include "rocprof-sys-sample.hpp"
#include <algorithm>
#include <iostream>
#include <string_view>
#include <unistd.h>
-16
Просмотреть файл
@@ -22,7 +22,6 @@
#pragma once
#include <string>
#include <string_view>
#include <vector>
@@ -34,9 +33,6 @@ enum update_mode : int
UPD_WEAK = 1 << 2, // 0x04
};
std::string
get_realpath(const std::string& _fpath);
void
print_command(const std::vector<char*>& _argv);
@@ -46,22 +42,10 @@ print_updated_environment(std::vector<char*> _env);
std::vector<char*>
get_initial_environment();
std::string
get_rocprofsys_root(void);
std::string
get_internal_libpath(const std::string& _lib);
std::string
get_internal_script_path(void);
template <typename Tp>
void
update_env(std::vector<char*>& _environ, std::string_view _env_var, Tp&& _env_val,
update_mode&& _mode = UPD_REPLACE, std::string_view _join_delim = ":");
void
remove_env(std::vector<char*>& _environ, std::string_view _env_var);
std::vector<char*>
parse_args(int argc, char** argv, std::vector<char*>& envp);
+37 -1
Просмотреть файл
@@ -24,17 +24,18 @@
#include "common/defines.h"
#include "common/join.hpp"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <timemory/utility/filepath.hpp>
#include <type_traits>
#include <unistd.h>
#include <unordered_set>
#if !defined(ROCPROFSYS_ENVIRON_LOG_NAME)
# if defined(ROCPROFSYS_COMMON_LIBRARY_NAME)
@@ -83,6 +84,7 @@ inline namespace common
{
namespace
{
inline std::string
get_env_impl(std::string_view env_id, std::string_view _default)
{
@@ -180,6 +182,40 @@ struct ROCPROFSYS_INTERNAL_API env_config
}
};
inline void
remove_env(std::vector<char*>& _environ, std::string_view _env_var,
const std::unordered_set<std::string>& _original_envs)
{
auto key = join("", _env_var, "=");
auto match = [&key](auto itr) -> bool {
return itr && std::string_view{ itr }.find(key) == 0;
};
// Free memory for matching entries
for(auto& itr : _environ)
{
if(match(itr))
{
free(itr);
itr = nullptr;
}
}
// Remove null entries
_environ.erase(std::remove_if(_environ.begin(), _environ.end(), match),
_environ.end());
// Restore from original_envs if previously existed
for(const auto& orig : _original_envs)
{
if(std::string_view{ orig.data(), orig.size() }.find(key) == 0)
{
_environ.emplace_back(strdup(orig.c_str()));
}
}
}
inline std::string
discover_llvm_libdir_for_ompt(bool verbose = false)
{
+47
Просмотреть файл
@@ -124,6 +124,18 @@ is_link(const std::string& _path) ROCPROFSYS_INTERNAL_API;
inline std::string
readlink(const std::string& _path) ROCPROFSYS_INTERNAL_API;
inline std::string
get_rocprofsys_root() ROCPROFSYS_INTERNAL_API;
inline std::string
get_internal_libpath(const std::string& _lib) ROCPROFSYS_INTERNAL_API;
inline std::string
get_internal_script_path() ROCPROFSYS_INTERNAL_API;
inline std::string
get_internal_libdir() ROCPROFSYS_INTERNAL_API;
struct ROCPROFSYS_INTERNAL_API path_type
{
enum path_type_e
@@ -411,6 +423,41 @@ get_origin(const std::string& _filename, std::vector<int>&& _open_modes)
return std::string{};
}
std::string
get_rocprofsys_root()
{
auto _exe_rp = realpath("/proc/self/exe");
auto _exe_dir = dirname(_exe_rp);
if(_exe_dir.empty()) _exe_dir = "./";
return rocprofsys::common::join('/', _exe_dir, "..");
}
std::string
get_internal_libpath(const std::string& _lib)
{
auto _root = get_rocprofsys_root();
for(const auto* libdir : { "lib", "lib64" })
{
auto _candidate = rocprofsys::common::join('/', _root, libdir, _lib);
if(exists(_candidate)) return _candidate;
}
return rocprofsys::common::join('/', _root, "lib", _lib);
}
std::string
get_internal_script_path()
{
auto _root = get_rocprofsys_root();
return rocprofsys::common::join('/', _root, "libexec", "rocprofiler-systems");
}
std::string
get_internal_libdir()
{
return rocprofsys::common::join('/', get_rocprofsys_root(), "lib");
}
} // namespace path
} // namespace common
} // namespace rocprofsys
+11 -61
Просмотреть файл
@@ -21,9 +21,10 @@
// SOFTWARE.
#include "argparse.hpp"
#include "common/environment.hpp"
#include "common/join.hpp"
#include "common/path.hpp"
#include "config.hpp"
#include "defines.hpp"
#include "exception.hpp"
#include "gpu.hpp"
#include "state.hpp"
@@ -39,8 +40,9 @@ namespace argparse
namespace
{
namespace filepath = ::tim::filepath;
namespace path = rocprofsys::common::path;
using array_config_t = ::timemory::join::array_config;
using ::tim::get_env;
using rocprofsys::common::remove_env;
using ::timemory::join::join;
auto
@@ -79,12 +81,6 @@ get_clock_id_choices()
return std::make_pair(_choices, _aliases);
}
auto
get_realpath(const std::string& _path)
{
return filepath::realpath(_path, nullptr, false);
}
enum update_mode : int
{
UPD_REPLACE = 0, // no PREPEND/APPEND bits set
@@ -152,54 +148,6 @@ update_env(parser_data& _data, std::string_view _env_var, Tp&& _env_val,
strdup(rocprofsys::common::join('=', _env_var, _env_val).c_str()));
}
void
remove_env(parser_data& _data, std::string_view _env_var)
{
auto _key = join("", _env_var, "=");
auto _match = [&_key](auto itr) { return std::string_view{ itr }.find(_key) == 0; };
auto& _environ = _data.current;
_environ.erase(std::remove_if(_environ.begin(), _environ.end(), _match),
_environ.end());
auto& _initial = _data.initial;
for(const auto& itr : _initial)
{
if(std::string_view{ itr }.find(_key) == 0)
_environ.emplace_back(strdup(itr.c_str()));
}
}
std::string
get_rocprofsys_root(void)
{
char* _tmp = realpath("/proc/self/exe", nullptr);
std::string _exe = (_tmp) ? std::string{ _tmp } : std::string{};
if(_tmp) free(_tmp);
auto _pos = _exe.find_last_of('/');
auto _dir = std::string{ "./" };
if(_pos != std::string::npos) _dir = _exe.substr(0, _pos);
return rocprofsys::common::join("/", _dir, "..");
}
std::string
get_internal_libpath(const std::string& _lib)
{
auto _root = get_rocprofsys_root();
return rocprofsys::common::join("/", _root, "lib", _lib);
}
std::string
get_internal_script_path(void)
{
auto _root = get_rocprofsys_root();
return rocprofsys::common::join("/", _root, "libexec", "rocprofiler-systems");
}
} // namespace
bool
@@ -245,13 +193,15 @@ init_parser(parser_data& _data)
}
}
_data.dl_libpath = get_realpath(get_internal_libpath("librocprof-sys-dl.so").c_str());
_data.omni_libpath = get_realpath(get_internal_libpath("librocprof-sys.so").c_str());
_data.dl_libpath =
path::realpath(path::get_internal_libpath("librocprof-sys-dl.so").c_str());
_data.omni_libpath =
path::realpath(path::get_internal_libpath("librocprof-sys.so").c_str());
auto _libexecpath = get_realpath(get_internal_script_path());
auto _libexecpath = path::realpath(path::get_internal_script_path());
update_env(_data, "ROCPROFSYS_SCRIPT_PATH", _libexecpath, UPD_REPLACE);
auto _rootpath = get_realpath(get_rocprofsys_root());
auto _rootpath = path::realpath(path::get_rocprofsys_root());
update_env(_data, "ROCPROFSYS_ROOT", _rootpath, UPD_REPLACE);
return _data;
@@ -663,7 +613,7 @@ add_core_arguments(parser_t& _parser, parser_data& _data)
_update("ROCPROFSYS_TRACE_THREAD_SPIN_LOCKS", _v.count("spin-locks") > 0);
if(_v.count("all") > 0 || _v.count("kokkosp") > 0)
remove_env(_data, "KOKKOS_TOOLS_LIBS");
remove_env(_data.current, "KOKKOS_TOOLS_LIBS", _data.initial);
});
_data.processed_environs.emplace("exclude");
+18 -19
Просмотреть файл
@@ -22,13 +22,12 @@
#pragma once
#include "defines.hpp"
#include <timemory/settings/vsettings.hpp>
#include <timemory/utility/argparse.hpp>
#include <functional>
#include <set>
#include <unordered_set>
#include <vector>
namespace rocprofsys
@@ -39,7 +38,7 @@ struct parser_data;
using parser_t = ::tim::argparse::argument_parser;
using vsetting_t = ::tim::vsettings;
using vsettings_set_t = std::set<vsetting_t*>;
using vsettings_set_t = std::unordered_set<vsetting_t*>;
using strset_t = std::set<std::string>;
using strvec_t = std::vector<std::string>;
using setting_filter_t = std::function<bool(vsetting_t*, const parser_data&)>;
@@ -57,22 +56,22 @@ default_grouping_filter(std::string_view, const parser_data&);
struct parser_data
{
bool monochrome = false;
bool debug = false;
int verbose = 0;
std::string dl_libpath = {};
std::string omni_libpath = {};
std::string launcher = {};
vsettings_set_t processed_settings = {};
std::set<std::string> processed_environs = {};
std::set<std::string> processed_groups = {};
std::vector<char*> current = {};
std::vector<char*> command = {};
std::set<std::string_view> updated = {};
std::set<std::string> initial = {};
grouping_filter_t grouping_filter = default_grouping_filter;
setting_filter_t setting_filter = default_setting_filter;
environ_filter_t environ_filter = default_environ_filter;
bool monochrome = false;
bool debug = false;
int verbose = 0;
std::string dl_libpath = {};
std::string omni_libpath = {};
std::string launcher = {};
vsettings_set_t processed_settings = {};
std::unordered_set<std::string> processed_environs = {};
std::unordered_set<std::string> processed_groups = {};
std::vector<char*> current = {};
std::vector<char*> command = {};
std::unordered_set<std::string_view> updated = {};
std::unordered_set<std::string> initial = {};
grouping_filter_t grouping_filter = default_grouping_filter;
setting_filter_t setting_filter = default_setting_filter;
environ_filter_t environ_filter = default_environ_filter;
};
parser_data&