Record loaded runtimes (#6)

* Provide rocprofiler_register_iterate_registration_info function

- stores the runtime arguments for later reference

* Fix compilation error

* Removed unused variable

* Store api tables in vector

* Update license

* Replace global_mutex usage with scoped_count

- in rocp_invoke_registrations

* Formatting

---------

Co-authored-by: Jonathan R. Madsen <jonathanrmadsen@gmail.com>

[ROCm/rocprofiler-register commit: fc712350ef]
This commit is contained in:
Madsen, Jonathan
2025-03-21 04:36:58 -05:00
committed by GitHub
parent e820b8862a
commit 8f1cb6417a
5 changed files with 386 additions and 28 deletions
@@ -1,4 +1,5 @@
#include <rocprofiler-register/rocprofiler-register.h>
#include <amdhip/amdhip.hpp>
#include <hsa-runtime/hsa-runtime.hpp>
#include <rccl/rccl.hpp>
@@ -8,8 +9,11 @@
#include <dlfcn.h>
#include <pthread.h>
#include <sstream>
#include <stdexcept>
#include <string_view>
#include <tuple>
#include <vector>
#ifndef ROCP_REG_FILE_NAME
# define ROCP_REG_FILE_NAME \
@@ -64,6 +68,26 @@ roctx_range_pop(const char* name)
{
printf("[%s][pop] %s\n", ROCP_REG_FILE_NAME, name);
}
using reginfo_vec_t = std::vector<rocprofiler_register_registration_info_t>;
bool
check_registration_info(const char* name,
uint64_t lib_version,
uint64_t num_tables,
const reginfo_vec_t& infovec)
{
for(const auto& itr : infovec)
{
if(std::string_view{ name } == std::string_view{ itr.common_name })
{
return std::tie(lib_version, num_tables) ==
std::tie(itr.lib_version, itr.api_table_length);
}
}
return false;
}
} // namespace rocprofiler
extern "C" {
@@ -99,6 +123,34 @@ rocprofiler_set_api_table(const char* name,
" did not contain rocprofiler_configure symbol" };
}
auto registration_info = ::rocprofiler::reginfo_vec_t{};
{
auto* _handle =
dlopen("librocprofiler-register.so", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
if(!_handle)
throw std::runtime_error{
"error opening librocprofiler-register.so library "
};
auto* _sym = dlsym(_handle, "rocprofiler_register_iterate_registration_info");
if(!_sym)
throw std::runtime_error{
"librocprofiler-register.so did not contain "
"rocprofiler_register_iterate_registration_info symbol"
};
auto _func = [](rocprofiler_register_registration_info_t* _info,
void* _vdata) -> int {
auto* _vec = static_cast<::rocprofiler::reginfo_vec_t*>(_vdata);
_vec->emplace_back(*_info);
return 0;
};
auto iterate_registration_info =
reinterpret_cast<decltype(&rocprofiler_register_iterate_registration_info)>(
_sym);
iterate_registration_info(_func, &registration_info);
}
using hip_table_t = hip::HipApiTable;
using hsa_table_t = hsa::HsaApiTable;
using roctx_table_t = roctx::ROCTxApiTable;
@@ -121,23 +173,23 @@ rocprofiler_set_api_table(const char* name,
if(std::string_view{ name } == "hip")
{
hip_table_t* _table = static_cast<hip_table_t*>(tables[0]);
_table->hip_init_fn = &rocprofiler::hip_init;
_table->hip_init_fn = &::rocprofiler::hip_init;
}
else if(std::string_view{ name } == "hsa")
{
hsa_table_t* _table = static_cast<hsa_table_t*>(tables[0]);
_table->hsa_init_fn = &rocprofiler::hsa_init;
_table->hsa_init_fn = &::rocprofiler::hsa_init;
}
else if(std::string_view{ name } == "roctx")
{
roctx_table_t* _table = static_cast<roctx_table_t*>(tables[0]);
_table->roctxRangePush_fn = &rocprofiler::roctx_range_push;
_table->roctxRangePop_fn = &rocprofiler::roctx_range_pop;
_table->roctxRangePush_fn = &::rocprofiler::roctx_range_push;
_table->roctxRangePop_fn = &::rocprofiler::roctx_range_pop;
}
else if(std::string_view{ name } == "rccl")
{
rccl_table_t* _table = static_cast<rccl_table_t*>(tables[0]);
_table->ncclGetVersion_fn = &rocprofiler::ncclGetVersion;
_table->ncclGetVersion_fn = &::rocprofiler::ncclGetVersion;
}
else if(std::string_view{ name } == "rocdecode")
{
@@ -151,6 +203,15 @@ rocprofiler_set_api_table(const char* name,
}
}
if(!::rocprofiler::check_registration_info(
name, lib_version, num_tables, registration_info))
{
auto ss = std::stringstream{};
ss << "no matching registration info for " << name << " "
<< " version " << lib_version << " (# tables = " << num_tables << ")";
throw std::runtime_error{ ss.str() };
}
return 0;
}
}