diff --git a/source/lib/rocprofiler-sdk-tool/config.cpp b/source/lib/rocprofiler-sdk-tool/config.cpp index 4a61211049..e6463cc78a 100644 --- a/source/lib/rocprofiler-sdk-tool/config.cpp +++ b/source/lib/rocprofiler-sdk-tool/config.cpp @@ -48,7 +48,10 @@ namespace tool { namespace { -auto launch_time = new std::time_t{std::time(nullptr)}; +const auto launch_time = new std::time_t{std::time(nullptr)}; +const auto env_regexes = + new std::array{std::regex{"(.*)%(env|ENV)\\{([A-Z0-9_]+)\\}%(.*)"}, + std::regex{"(.*)\\$(env|ENV)\\{([A-Z0-9_]+)\\}(.*)"}}; std::string get_local_datetime(const char* dt_format, std::time_t* dt_curr) @@ -362,25 +365,15 @@ format(std::string _fpath, const std::string& _tag) // environment and configuration variables try { - for(const auto& _expr : {std::string{"(.*)%(env|ENV)\\{([A-Z0-9_]+)\\}%(.*)"}, - std::string{"(.*)\\$(env|ENV)\\{([A-Z0-9_]+)\\}(.*)"}}) + for(const auto& _re : *env_regexes) { - 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(_var, ""); - } - auto _beg = std::regex_replace(_fpath, _re, "$1"); - auto _end = std::regex_replace(_fpath, _re, "$4"); - _fpath = fmt::format("{}{}{}", _beg, _val, _end); + std::string _val = get_env(_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) diff --git a/source/lib/rocprofiler-sdk-tool/config.hpp b/source/lib/rocprofiler-sdk-tool/config.hpp index da799d1f22..95678ca730 100644 --- a/source/lib/rocprofiler-sdk-tool/config.hpp +++ b/source/lib/rocprofiler-sdk-tool/config.hpp @@ -78,7 +78,7 @@ struct config std::string output_path = get_env("ROCPROF_OUTPUT_PATH", fs::current_path().string()); std::string output_file = get_env("ROCPROF_OUTPUT_FILE_NAME", std::to_string(getpid())); std::string output_format = get_env("ROCPROF_OUTPUT_FORMAT", "CSV"); - std::string tmp_directory = get_env("ROCPROF_TMPDIR", fs::current_path().string()); + std::string tmp_directory = get_env("ROCPROF_TMPDIR", output_path); std::vector kernel_names = {}; std::set counters = {}; }; diff --git a/source/lib/rocprofiler-sdk-tool/tmp_file.cpp b/source/lib/rocprofiler-sdk-tool/tmp_file.cpp index 095da445c6..fffd32ba77 100644 --- a/source/lib/rocprofiler-sdk-tool/tmp_file.cpp +++ b/source/lib/rocprofiler-sdk-tool/tmp_file.cpp @@ -30,6 +30,9 @@ namespace fs = ::rocprofiler::common::filesystem; bool tmp_file::fopen(const char* _mode) { + auto fpath = fs::path{filename}.parent_path(); + if(!fs::exists(fpath)) fs::create_directories(fpath); + if(!fs::exists(filename)) { // if the filepath does not exist, open in out mode to create it @@ -101,6 +104,9 @@ tmp_file::close() bool tmp_file::open(std::ios::openmode _mode) { + auto fpath = fs::path{filename}.parent_path(); + if(!fs::exists(fpath)) fs::create_directories(fpath); + if(!fs::exists(filename)) { // if the filepath does not exist, open in out mode to create it diff --git a/source/lib/rocprofiler-sdk-tool/tool.cpp b/source/lib/rocprofiler-sdk-tool/tool.cpp index 1f3db3a69f..0c4d2eb900 100644 --- a/source/lib/rocprofiler-sdk-tool/tool.cpp +++ b/source/lib/rocprofiler-sdk-tool/tool.cpp @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -629,38 +630,33 @@ flush() std::string get_file_name(buffer_type_t buffer_type) { - std::string filename; switch(buffer_type) { - case buffer_type_t::ROCPROFILER_TOOL_BUFFER_HSA: filename = "hsa_trace"; break; - case buffer_type_t::ROCPROFILER_TOOL_BUFFER_HIP: filename = "hip_trace"; break; - case buffer_type_t::ROCPROFILER_TOOL_BUFFER_MARKER_API: filename = "marker_trace"; break; - case buffer_type_t::ROCPROFILER_TOOL_BUFFER_MEMORY_COPY: filename = "memory_copy"; break; + case buffer_type_t::ROCPROFILER_TOOL_BUFFER_HSA: return "hsa_trace"; break; + case buffer_type_t::ROCPROFILER_TOOL_BUFFER_HIP: return "hip_trace"; break; + case buffer_type_t::ROCPROFILER_TOOL_BUFFER_MARKER_API: return "marker_trace"; break; + case buffer_type_t::ROCPROFILER_TOOL_BUFFER_MEMORY_COPY: return "memory_copy"; break; case buffer_type_t::ROCPROFILER_TOOL_BUFFER_COUNTER_COLLECTION: - filename = "counter_collection"; + return "counter_collection"; break; case buffer_type_t::ROCPROFILER_TOOL_BUFFER_KERNEL_DISPATCH: - filename = "kernel_dispatch"; + return "kernel_dispatch"; break; - case buffer_type_t::ROCPROFILER_TOOL_BUFFER_SCRATCH_MEMORY: - filename = "scratch_memory"; - break; - default: LOG_IF(FATAL, "buffer tpe invalid"); + case buffer_type_t::ROCPROFILER_TOOL_BUFFER_SCRATCH_MEMORY: return "scratch_memory"; break; } - return filename; + + LOG(FATAL) << "buffer type " << static_cast>(buffer_type) + << " not supported"; + return std::string{}; } std::string compose_tmp_file_name(buffer_type_t buffer_type) { - static std::mutex _mutex{}; - std::unique_lock _lk{_mutex}; - std::string ext = ".dat"; - std::string explicit_path = rocprofiler::tool::get_config().tmp_directory; - std::string subdirectory = rocprofiler::tool::format(explicit_path + '/' + "%ppid%_"); - std::string fname = get_file_name(buffer_type); - std::string filename = subdirectory + fname + ext; - return filename; + return rocprofiler::tool::format(fmt::format("{}/.rocprofv3/{}-{}.dat", + rocprofiler::tool::get_config().tmp_directory, + "%ppid%-%pid%", + get_file_name(buffer_type))); } template @@ -680,12 +676,12 @@ offload_buffer(buffer_type_t type) tmp_file* _tmp_file = nullptr; std::tie(_tmp_buf, _tmp_file) = get_tmp_file_buffer(type); auto _lk = std::lock_guard(_tmp_file->file_mutex); - auto& _fs = _tmp_file->stream; [[maybe_unused]] static auto _success = _tmp_file->open(); + auto& _fs = _tmp_file->stream; _tmp_file->file_pos.emplace(_fs.tellg()); _tmp_buf->save(_fs); _tmp_buf->clear(); - assert(_tmp_buf->is_empty() == true); + CHECK(_tmp_buf->is_empty() == true); } template @@ -700,7 +696,7 @@ write_ring_buffer(Tb* _v, buffer_type_t type) { offload_buffer(type); ptr = _tmp_buf->request(false); - assert(ptr != nullptr); + CHECK(ptr != nullptr); } *ptr = std::move(*reinterpret_cast(_v)); } diff --git a/tests/rocprofv3/counter-collection/input2/validate.py b/tests/rocprofv3/counter-collection/input2/validate.py index 5d7390b313..4619fbc998 100644 --- a/tests/rocprofv3/counter-collection/input2/validate.py +++ b/tests/rocprofv3/counter-collection/input2/validate.py @@ -29,6 +29,9 @@ def test_validate_counter_collection_pmc2(input_dir: pd.DataFrame): # Check if each file in the subdirectory has some data for file_name in os.listdir(subdirectory_path): file_path = os.path.join(subdirectory_path, file_name) + # ignore hidden folders + if os.path.isdir(file_path) and os.path.basename(file_path).startswith("."): + continue assert os.path.isfile(file_path), f"{file_path} is not a file." if "agent_info.csv" not in file_path: diff --git a/tests/rocprofv3/tracing-plus-cc/validate.py b/tests/rocprofv3/tracing-plus-cc/validate.py index 6ed9a73a8d..04edcbeaec 100644 --- a/tests/rocprofv3/tracing-plus-cc/validate.py +++ b/tests/rocprofv3/tracing-plus-cc/validate.py @@ -29,6 +29,9 @@ def test_validate_counter_collection_plus_tracing(input_dir: pd.DataFrame): # Check if each file in the subdirectory has some data for file_name in os.listdir(subdirectory_path): file_path = os.path.join(subdirectory_path, file_name) + # ignore hidden folders + if os.path.isdir(file_path) and os.path.basename(file_path).startswith("."): + continue assert os.path.isfile(file_path), f"{file_path} is not a file." if "agent_info.csv" not in file_path: