diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/impl.cpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/impl.cpp index bad3848df8e..45f95b36832 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/impl.cpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/impl.cpp @@ -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 #include -#include -#include #include #include #include @@ -52,7 +49,6 @@ #include #include #include -#include #include #include @@ -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{}; -auto original_envs = std::set{}; -auto child_pids = std::set{}; +auto updated_envs = std::unordered_set{}; +auto original_envs = std::unordered_set{}; +auto child_pids = std::unordered_set{}; 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& _argv, std::string_view _prefix) { @@ -250,45 +238,16 @@ prepare_environment_for_run(std::vector& _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 _env, std::string_view _prefix) { @@ -398,22 +357,6 @@ add_default_env(std::vector& _environ, std::string_view _env_var, Tp&& _e strdup(rocprofsys::common::join('=', _env_var, _env_val).c_str())); } -void -remove_env(std::vector& _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 parse_args(int argc, char** argv, std::vector& _env, std::vector>& _causal_envs) @@ -422,7 +365,7 @@ parse_args(int argc, char** argv, std::vector& _env, using parser_err_t = typename parser_t::result_type; auto help_check = [](parser_t& p, int _argc, char** _argv) { - std::set help_args = { "-h", "--help", "-?" }; + std::unordered_set help_args = { "-h", "--help", "-?" }; return (p.exists("help") || _argc == 1 || (_argc > 1 && help_args.find(_argv[1]) != help_args.end())); }; diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.cpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.cpp index 40d5698d0e1..bab1252f5b4 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.cpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.cpp @@ -24,7 +24,6 @@ #include -#include #include #include #include diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.hpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.hpp index d8535532d35..ef0de26a3e8 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.hpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.hpp @@ -35,9 +35,6 @@ int get_verbose(); -std::string -get_realpath(const std::string&); - void print_command(const std::vector& _argv, std::string_view); @@ -52,15 +49,6 @@ prepare_command_for_run(char*, std::vector&); void prepare_environment_for_run(std::vector&); -std::string -get_rocprofsys_root(void); - -std::string -get_internal_libpath(const std::string& _lib); - -std::string -get_internal_script_path(void); - template void update_env(std::vector&, std::string_view, Tp&&, bool _append = false, @@ -70,9 +58,6 @@ template void add_default_env(std::vector&, std::string_view, Tp&&); -void -remove_env(std::vector&, std::string_view); - std::vector parse_args(int argc, char** argv, std::vector&, std::vector>&); diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.cpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.cpp index cf406c66c76..717d6573247 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.cpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.cpp @@ -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 #include -#include #include #include #include @@ -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("DYNINSTAPI_RT_LIB"), - tim::get_env("DYNINST_REWRITER_PATHS"), - tim::get_env("LD_LIBRARY_PATH")), - ":"); +strvec_t lib_search_paths = + tim::delimit(rocprofsys::join(':', path::get_internal_libdir(), + tim::get_env("DYNINSTAPI_RT_LIB"), + tim::get_env("DYNINST_REWRITER_PATHS"), + tim::get_env("LD_LIBRARY_PATH")), + ":"); strvec_t bin_search_paths = tim::delimit(tim::get_env("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* app_modules = app_image->getModules(); std::vector* app_functions = app_image->getProcedures(include_uninstr); - std::set modules = {}; - std::set functions = {}; + std::unordered_set modules = {}; + std::unordered_set 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() { diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.hpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.hpp index 181aba339e0..5110c341e8f 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.hpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.hpp @@ -32,7 +32,6 @@ #include #include -#include #include #include #include diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-run/impl.cpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-run/impl.cpp index fafa6387175..b9510236b18 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-run/impl.cpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-run/impl.cpp @@ -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 @@ -43,11 +40,8 @@ #include #include -#include #include -#include #include -#include #include #include #include @@ -57,7 +51,6 @@ #include #include #include -#include #include #include @@ -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{}; +auto original_envs = std::unordered_set{}; 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 void update_env(std::vector& _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 help_args = { "-h", "--help", "-?" }; + std::unordered_set help_args = { "-h", "--help", "-?" }; return (p.exists("help") || _argc == 1 || (_argc > 1 && help_args.find(_argv[1]) != help_args.end())); }; diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-run/rocprof-sys-run.cpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-run/rocprof-sys-run.cpp index 1ccfc49a4f7..92cbe0b4633 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-run/rocprof-sys-run.cpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-run/rocprof-sys-run.cpp @@ -26,13 +26,9 @@ #include #include -#include #include #include -#include #include -#include -#include #include #include diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-run/rocprof-sys-run.hpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-run/rocprof-sys-run.hpp index 978deb58bf3..d4dfb047f29 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-run/rocprof-sys-run.hpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-run/rocprof-sys-run.hpp @@ -25,12 +25,8 @@ #include "core/argparse.hpp" #include -#include #include -#include -#include #include -#include using parser_data_t = rocprofsys::argparse::parser_data; diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/impl.cpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/impl.cpp index fa6697612dc..088b92150fc 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/impl.cpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/impl.cpp @@ -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 #include @@ -34,7 +33,6 @@ #include #include -#include #include #include #include @@ -45,7 +43,9 @@ #include 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{}; -auto original_envs = std::set{}; +auto updated_envs = std::unordered_set{}; +auto original_envs = std::unordered_set{}; 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& _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 _env) { @@ -282,22 +243,6 @@ update_env(std::vector& _environ, std::string_view _env_var, Tp&& _env_va strdup(rocprofsys::common::join('=', _env_var, _env_val).c_str())); } -void -remove_env(std::vector& _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 parse_args(int argc, char** argv, std::vector& _env) { @@ -305,7 +250,7 @@ parse_args(int argc, char** argv, std::vector& _env) using parser_err_t = typename parser_t::result_type; auto help_check = [](parser_t& p, int _argc, char** _argv) { - std::set help_args = { "-h", "--help", "-?" }; + std::unordered_set 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& _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& _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& _env) throw std::runtime_error( "Error! '--profile' argument conflicts with '--flat-profile' argument"); - free(_dl_libpath); - free(_omni_libpath); - return _outv; } diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.cpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.cpp index 548cb4811dd..122d02e59d1 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.cpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.cpp @@ -22,8 +22,6 @@ #include "rocprof-sys-sample.hpp" -#include -#include #include #include diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.hpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.hpp index 8ca99967e30..9edf1afa0d0 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.hpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.hpp @@ -22,7 +22,6 @@ #pragma once -#include #include #include @@ -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& _argv); @@ -46,22 +42,10 @@ print_updated_environment(std::vector _env); std::vector 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 void update_env(std::vector& _environ, std::string_view _env_var, Tp&& _env_val, update_mode&& _mode = UPD_REPLACE, std::string_view _join_delim = ":"); -void -remove_env(std::vector& _environ, std::string_view _env_var); - std::vector parse_args(int argc, char** argv, std::vector& envp); diff --git a/projects/rocprofiler-systems/source/lib/common/environment.hpp b/projects/rocprofiler-systems/source/lib/common/environment.hpp index 8c3d42728c7..f253dc2f017 100644 --- a/projects/rocprofiler-systems/source/lib/common/environment.hpp +++ b/projects/rocprofiler-systems/source/lib/common/environment.hpp @@ -24,17 +24,18 @@ #include "common/defines.h" +#include "common/join.hpp" #include #include #include #include -#include #include #include #include #include #include #include +#include #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& _environ, std::string_view _env_var, + const std::unordered_set& _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) { diff --git a/projects/rocprofiler-systems/source/lib/common/path.hpp b/projects/rocprofiler-systems/source/lib/common/path.hpp index f9034c7e7bf..47db9d9b9f7 100644 --- a/projects/rocprofiler-systems/source/lib/common/path.hpp +++ b/projects/rocprofiler-systems/source/lib/common/path.hpp @@ -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&& _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 diff --git a/projects/rocprofiler-systems/source/lib/core/argparse.cpp b/projects/rocprofiler-systems/source/lib/core/argparse.cpp index 0b563bc0873..5cd996f311e 100644 --- a/projects/rocprofiler-systems/source/lib/core/argparse.cpp +++ b/projects/rocprofiler-systems/source/lib/core/argparse.cpp @@ -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"); diff --git a/projects/rocprofiler-systems/source/lib/core/argparse.hpp b/projects/rocprofiler-systems/source/lib/core/argparse.hpp index 5e250711c6d..9280ddd4f93 100644 --- a/projects/rocprofiler-systems/source/lib/core/argparse.hpp +++ b/projects/rocprofiler-systems/source/lib/core/argparse.hpp @@ -22,13 +22,12 @@ #pragma once -#include "defines.hpp" - #include #include #include #include +#include #include 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; +using vsettings_set_t = std::unordered_set; using strset_t = std::set; using strvec_t = std::vector; using setting_filter_t = std::function; @@ -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 processed_environs = {}; - std::set processed_groups = {}; - std::vector current = {}; - std::vector command = {}; - std::set updated = {}; - std::set 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 processed_environs = {}; + std::unordered_set processed_groups = {}; + std::vector current = {}; + std::vector command = {}; + std::unordered_set updated = {}; + std::unordered_set 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&