Update lib/rocprofiler-sdk-tool (#755)

- questionable data race within std::regex in CI
- simplify rocprofiler::tool::format
- set config::tmp_directory to default to output_path
- fs::create_directories for tmp_file
- rework get_file_name(...) and compose_tmp_file_name(...) in tool.cpp
This commit is contained in:
Jonathan R. Madsen
2024-04-12 03:48:02 -05:00
committed by GitHub
parent 7ef9e1ea8c
commit 87490d0018
6 changed files with 41 additions and 40 deletions
+9 -16
View File
@@ -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, 2>{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<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);
std::string _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)
+1 -1
View File
@@ -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<std::string> kernel_names = {};
std::set<std::string> counters = {};
};
@@ -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
+19 -23
View File
@@ -52,6 +52,7 @@
#include <mutex>
#include <optional>
#include <shared_mutex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
@@ -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<std::underlying_type_t<buffer_type_t>>(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<std::mutex> _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 <typename Tp>
@@ -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<Tp>(type);
auto _lk = std::lock_guard<std::mutex>(_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 <typename Tp, typename Tb>
@@ -700,7 +696,7 @@ write_ring_buffer(Tb* _v, buffer_type_t type)
{
offload_buffer<Tp>(type);
ptr = _tmp_buf->request(false);
assert(ptr != nullptr);
CHECK(ptr != nullptr);
}
*ptr = std::move(*reinterpret_cast<Tb*>(_v));
}
@@ -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:
@@ -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: