From 6fe19b681a798ab4fba72725b6dc8ec208c4761c Mon Sep 17 00:00:00 2001 From: David Galiffi Date: Fri, 2 May 2025 16:52:54 -0400 Subject: [PATCH] Fix path to post-processing merge script (#187) - Path to merge script not found unless user explicitly sources "share/rocprofiler-systems/setup-env.sh" to setup PATHs. - Instead, let's derive the path when the application loads and use it when executing the helper script - Rename script to rocprof-sys-merge-output.sh. - Change install folder to /libexec/rocprofiler-systems based on dev-ops feedback. - Updated PATH variable in the modulefile and source scrtipt. - For SWDEV-528101 [ROCm/rocprofiler-systems commit: adc66956b05b0d043616f5ac62d7840191ca1368] --- projects/rocprofiler-systems/CMakeLists.txt | 6 +-- .../cmake/Templates/modulefile.in | 2 +- .../cmake/Templates/setup-env.sh.in | 2 +- .../source/bin/rocprof-sys-causal/impl.cpp | 15 ++++++ .../rocprof-sys-causal/rocprof-sys-causal.hpp | 3 ++ .../rocprof-sys-instrument.cpp | 10 +++- .../source/bin/rocprof-sys-sample/impl.cpp | 17 +++++- .../rocprof-sys-sample/rocprof-sys-sample.hpp | 3 ++ .../source/lib/core/argparse.cpp | 17 ++++++ .../source/lib/core/perfetto.cpp | 54 ++++++++++++------- 10 files changed, 103 insertions(+), 26 deletions(-) diff --git a/projects/rocprofiler-systems/CMakeLists.txt b/projects/rocprofiler-systems/CMakeLists.txt index 06b19046d9..c8976c3d8f 100644 --- a/projects/rocprofiler-systems/CMakeLists.txt +++ b/projects/rocprofiler-systems/CMakeLists.txt @@ -371,7 +371,7 @@ configure_file( configure_file( ${PROJECT_SOURCE_DIR}/scripts/merge-multiprocess-output.sh - ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/bin/merge-multiprocess-output.sh + ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBEXECDIR}/${PROJECT_NAME}/rocprof-sys-merge-output.sh COPYONLY) install( @@ -393,8 +393,8 @@ install( install( PROGRAMS - ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/bin/merge-multiprocess-output.sh - DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/bin/ + ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBEXECDIR}/${PROJECT_NAME}/rocprof-sys-merge-output.sh + DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/${PROJECT_NAME} COMPONENT setup) # ------------------------------------------------------------------------------# diff --git a/projects/rocprofiler-systems/cmake/Templates/modulefile.in b/projects/rocprofiler-systems/cmake/Templates/modulefile.in index f1f09e40b9..c485f5973e 100644 --- a/projects/rocprofiler-systems/cmake/Templates/modulefile.in +++ b/projects/rocprofiler-systems/cmake/Templates/modulefile.in @@ -11,7 +11,7 @@ set ROOT [file normalize [file dirname [file normalize ${ModulesCurrentModulefil setenv @PROJECT_NAME_UNDERSCORED@_ROOT "${ROOT}" prepend-path CMAKE_PREFIX_PATH "${ROOT}" prepend-path PATH "${ROOT}/bin" -prepend-path PATH "${ROOT}/@CMAKE_INSTALL_DATAROOTDIR@/@PROJECT_NAME@/bin" +prepend-path PATH "${ROOT}/@CMAKE_INSTALL_LIBEXECDIR@/@PROJECT_NAME@" prepend-path LD_LIBRARY_PATH "${ROOT}/@CMAKE_INSTALL_LIBDIR@" prepend-path PYTHONPATH "${ROOT}/@CMAKE_INSTALL_PYTHONDIR@" setenv @PROJECT_NAME_UNDERSCORED@_DIR "${ROOT}/@CMAKE_INSTALL_DATAROOTDIR@/cmake/@PROJECT_NAME@" diff --git a/projects/rocprofiler-systems/cmake/Templates/setup-env.sh.in b/projects/rocprofiler-systems/cmake/Templates/setup-env.sh.in index 4e086316c7..423203e741 100644 --- a/projects/rocprofiler-systems/cmake/Templates/setup-env.sh.in +++ b/projects/rocprofiler-systems/cmake/Templates/setup-env.sh.in @@ -15,7 +15,7 @@ fi @PROJECT_NAME_UNDERSCORED@_ROOT=${BASEDIR} PATH=${BASEDIR}/bin:${PATH} -PATH=${BASEDIR}/@CMAKE_INSTALL_DATAROOTDIR@/@PROJECT_NAME@/bin:${PATH} +PATH=${BASEDIR}/@CMAKE_INSTALL_LIBEXECDIR@/@PROJECT_NAME@:${PATH} LD_LIBRARY_PATH=${BASEDIR}/@CMAKE_INSTALL_LIBDIR@:${LD_LIBRARY_PATH} PYTHONPATH=${BASEDIR}/@CMAKE_INSTALL_PYTHONDIR@:${PYTHONPATH} CMAKE_PREFIX_PATH=${BASEDIR}:${CMAKE_PREFIX_PATH} 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 88c000101f..39237443cb 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/impl.cpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/impl.cpp @@ -246,6 +246,7 @@ prepare_environment_for_run(std::vector& _env) join(":", LIBPTHREAD_SO, get_realpath(get_internal_libpath("librocprof-sys-dl.so"))), true); + update_env(_env, "ROCPROFSYS_SCRIPT_DIR", get_internal_script_path()); } } @@ -259,6 +260,20 @@ get_internal_libpath(const std::string& _lib) return rocprofsys::common::join("/", _dir, "..", "lib", _lib); } +std::string +get_internal_script_path(void) +{ + 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); + + auto _script_dir = get_realpath( + rocprofsys::common::join("/", _dir, "..", "libexec", "rocprofiler-systems")); + + return _script_dir; +} + void print_updated_environment(std::vector _env, std::string_view _prefix) { 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 7580b29f44..726011bcdd 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 @@ -55,6 +55,9 @@ prepare_environment_for_run(std::vector&); 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, 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 f7b99e278f..365fdc7b5b 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 @@ -352,8 +352,14 @@ main(int argc, char** argv) lib_search_paths.emplace_back( JOIN('/', _omni_lib_path, "rocprofiler-systems", "lib64")); + auto _omni_internal_libexec_path = + JOIN('/', filepath::dirname(filepath::dirname(_omni_exe_path)), "libexec", + "rocprofiler-systems"); + ROCPROFSYS_ADD_LOG_ENTRY(argv[0], "::", "rocprofsys bin path: ", _omni_exe_path); ROCPROFSYS_ADD_LOG_ENTRY(argv[0], "::", "rocprofsys lib path: ", _omni_lib_path); + ROCPROFSYS_ADD_LOG_ENTRY( + argv[0], "::", "rocprofsys libexec path: ", _omni_internal_libexec_path); for(const auto& itr : rocprofsys_get_link_map(nullptr)) { @@ -1442,7 +1448,6 @@ main(int argc, char** argv) env_vars.emplace_back(TIMEMORY_JOIN('=', "ROCPROFSYS_MPI_FINALIZE", "OFF")); env_vars.emplace_back(TIMEMORY_JOIN('=', "ROCPROFSYS_USE_CODE_COVERAGE", (coverage_mode != CODECOV_NONE) ? "ON" : "OFF")); - addr_space = rocprofsys_get_address_space(bpatch, _cmdc, _cmdv, env_vars, binary_rewrite, _pid, mutname); @@ -1970,6 +1975,9 @@ main(int argc, char** argv) (binary_rewrite && use_mpi) ? "ON" : "OFF")); if(use_mpi) env_vars.emplace_back(TIMEMORY_JOIN('=', "ROCPROFSYS_USE_PID", "ON")); + env_vars.emplace_back( + TIMEMORY_JOIN('=', "ROCPROFSYS_SCRIPT_PATH", _omni_internal_libexec_path)); + for(auto& itr : env_vars) { auto _pos = itr.find('='); 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 e80b0f0f27..37d177b5df 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/impl.cpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/impl.cpp @@ -124,9 +124,11 @@ 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()); update_env(_env, "LD_PRELOAD", _dl_libpath, UPD_APPEND); update_env(_env, "LD_LIBRARY_PATH", tim::filepath::dirname(_dl_libpath), UPD_APPEND); + update_env(_env, "ROCPROFSYS_SCRIPT_PATH", _libexecpath, UPD_REPLACE); auto _mode = get_env("ROCPROFSYS_MODE", "sampling", false); @@ -136,7 +138,6 @@ get_initial_environment() if(!getenv("OMP_TOOL_LIBRARIES")) update_env(_env, "OMP_TOOL_LIBRARIES", _dl_libpath, UPD_APPEND); #endif - return _env; } @@ -150,6 +151,20 @@ get_internal_libpath(const std::string& _lib) return rocprofsys::common::join("/", _dir, "..", "lib", _lib); } +std::string +get_internal_script_path(void) +{ + 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); + + auto _script_dir = + rocprofsys::common::join("/", _dir, "..", "libexec", "rocprofiler-systems"); + + return _script_dir; +} + void print_updated_environment(std::vector _env) { 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 5c446a40a4..8dfad88a3e 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 @@ -49,6 +49,9 @@ get_initial_environment(); 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, diff --git a/projects/rocprofiler-systems/source/lib/core/argparse.cpp b/projects/rocprofiler-systems/source/lib/core/argparse.cpp index 409fe9b1a3..30a41cc479 100644 --- a/projects/rocprofiler-systems/source/lib/core/argparse.cpp +++ b/projects/rocprofiler-systems/source/lib/core/argparse.cpp @@ -174,6 +174,20 @@ get_internal_libpath(const std::string& _lib) return filepath::realpath(rocprofsys::common::join("/", _dir, "..", "lib", _lib), nullptr, false); } +std::string +get_internal_script_path(void) +{ + 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); + + auto _script_dir = + rocprofsys::common::join("/", _dir, "..", "libexec", "rocprofiler-systems"); + + return _script_dir; +} + } // namespace bool @@ -222,6 +236,9 @@ 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()); + auto _libexecpath = get_realpath(get_internal_script_path()); + update_env(_data, "ROCPROFSYS_SCRIPT_PATH", _libexecpath, UPD_REPLACE); + #if defined(ROCPROFSYS_USE_OMPT) if(!getenv("OMP_TOOL_LIBRARIES")) update_env(_data, "OMP_TOOL_LIBRARIES", _data.dl_libpath, UPD_PREPEND); diff --git a/projects/rocprofiler-systems/source/lib/core/perfetto.cpp b/projects/rocprofiler-systems/source/lib/core/perfetto.cpp index 774b51fde1..41b0c71a45 100644 --- a/projects/rocprofiler-systems/source/lib/core/perfetto.cpp +++ b/projects/rocprofiler-systems/source/lib/core/perfetto.cpp @@ -263,25 +263,6 @@ post_process(tim::manager* _timemory_manager, bool& _perfetto_output_error) _timemory_manager->add_file_output("protobuf", "perfetto", _filename); } ofs.close(); - - if(dmp::rank() == 0) - { - const char* file_path = _filename.c_str(); - auto folder_path = [](std::string_view _v) { - return tim::filepath::dirname(std::string(_v)); - }; - // Execute the merge script - std::string command = - "merge-multiprocess-output.sh '" + folder_path(file_path) + "'"; - int result = system(command.c_str()); - if(result != 0) - { - ROCPROFSYS_VERBOSE(0, - "Failed to execute merge-multiprocess-output.sh with " - "folder path: %s\n", - folder_path(file_path).c_str()); - } - } } else if(dmp::rank() == 0) { @@ -290,6 +271,41 @@ post_process(tim::manager* _timemory_manager, bool& _perfetto_output_error) _filename.c_str()); } + // Merge the output files, if rank 0 + if(dmp::rank() == 0) + { + auto _output_folder = filepath::dirname(_filename); + auto _script_path = std::string{ "rocprof-sys-merge-output.sh" }; + auto _script_dir = get_env("ROCPROFSYS_SCRIPT_PATH", std::string{}, false); + + if(!_script_dir.empty()) + { + _script_path = rocprofsys::common::join("/", _script_dir, _script_path); + } + + // Test that the script exists + if(!filepath::exists(_script_path)) + { + ROCPROFSYS_VERBOSE(0, "Script not found: %s\n", _script_path.c_str()); + } + else + { + auto _command = _script_path + " '" + _output_folder + "'"; + + // Execute the merge script + int result = system(_command.c_str()); + + if(result != 0) + { + ROCPROFSYS_VERBOSE(0, "Failed to execute: %s\n", _command.c_str()); + } + else + { + ROCPROFSYS_VERBOSE(0, "Successfully executed: %s\n", _command.c_str()); + } + } + } + auto& _tmp_file = get_perfetto_tmp_file(); if(_tmp_file) {