Runtime Initialization Tracing (#1105)

* Runtime initialization tracing

- calbacks and buffer entries notifying when a runtime has been initialized

* Minor cleanup to registration.cpp

* JSON tool implementation

* Increase perfetto_reader timeout

* Handle perfetto_reader timeout when attr doesn't exist

* clang-tidy fixes to memory_allocation.cpp
This commit is contained in:
Jonathan R. Madsen
2024-11-18 20:50:29 -06:00
committed by GitHub
parent 3bd7773cf7
commit 249c50fc40
14 changed files with 496 additions and 49 deletions
+5 -3
View File
@@ -3,8 +3,9 @@
#
rocprofiler_activate_clang_tidy()
set(ROCPROFILER_LIB_HEADERS agent.hpp buffer.hpp external_correlation.hpp
intercept_table.hpp internal_threading.hpp registration.hpp)
set(ROCPROFILER_LIB_HEADERS
agent.hpp buffer.hpp external_correlation.hpp intercept_table.hpp
internal_threading.hpp registration.hpp runtime_initialization.hpp)
set(ROCPROFILER_LIB_SOURCES
agent.cpp
buffer.cpp
@@ -19,8 +20,9 @@ set(ROCPROFILER_LIB_SOURCES
internal_threading.cpp
pc_sampling.cpp
profile_config.cpp
registration.cpp
rocprofiler.cpp
registration.cpp)
runtime_initialization.cpp)
# ----------------------------------------------------------------------------------------#
#
+19 -7
View File
@@ -20,13 +20,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/hip/table_id.h>
#include <rocprofiler-sdk/hsa/table_id.h>
#include <rocprofiler-sdk/marker/table_id.h>
#include <rocprofiler-sdk/rccl/table_id.h>
#include <rocprofiler-sdk/rocprofiler.h>
#include "lib/common/logging.hpp"
#include "lib/rocprofiler-sdk/context/context.hpp"
#include "lib/rocprofiler-sdk/context/domain.hpp"
@@ -40,6 +33,14 @@
#include "lib/rocprofiler-sdk/page_migration/page_migration.hpp"
#include "lib/rocprofiler-sdk/rccl/rccl.hpp"
#include "lib/rocprofiler-sdk/registration.hpp"
#include "lib/rocprofiler-sdk/runtime_initialization.hpp"
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/hip/table_id.h>
#include <rocprofiler-sdk/hsa/table_id.h>
#include <rocprofiler-sdk/marker/table_id.h>
#include <rocprofiler-sdk/rccl/table_id.h>
#include <rocprofiler-sdk/rocprofiler.h>
#include <atomic>
#include <limits>
@@ -88,6 +89,7 @@ ROCPROFILER_BUFFER_TRACING_KIND_STRING(SCRATCH_MEMORY)
ROCPROFILER_BUFFER_TRACING_KIND_STRING(CORRELATION_ID_RETIREMENT)
ROCPROFILER_BUFFER_TRACING_KIND_STRING(RCCL_API)
ROCPROFILER_BUFFER_TRACING_KIND_STRING(OPENMP)
ROCPROFILER_BUFFER_TRACING_KIND_STRING(RUNTIME_INITIALIZATION)
template <size_t Idx, size_t... Tail>
std::pair<const char*, size_t>
@@ -271,6 +273,11 @@ rocprofiler_query_buffer_tracing_kind_operation_name(rocprofiler_buffer_tracing_
val = rocprofiler::page_migration::name_by_id(operation);
break;
}
case ROCPROFILER_BUFFER_TRACING_RUNTIME_INITIALIZATION:
{
val = rocprofiler::runtime_init::name_by_id(operation);
break;
}
case ROCPROFILER_BUFFER_TRACING_CORRELATION_ID_RETIREMENT:
{
return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED;
@@ -396,6 +403,11 @@ rocprofiler_iterate_buffer_tracing_kind_operations(
ops = rocprofiler::page_migration::get_ids();
break;
}
case ROCPROFILER_BUFFER_TRACING_RUNTIME_INITIALIZATION:
{
ops = rocprofiler::runtime_init::get_ids();
break;
}
case ROCPROFILER_BUFFER_TRACING_CORRELATION_ID_RETIREMENT:
{
return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED;
@@ -20,14 +20,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <rocprofiler-sdk/callback_tracing.h>
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/hip/table_id.h>
#include <rocprofiler-sdk/hsa/table_id.h>
#include <rocprofiler-sdk/marker/table_id.h>
#include <rocprofiler-sdk/rccl/table_id.h>
#include <rocprofiler-sdk/rocprofiler.h>
#include "lib/rocprofiler-sdk/code_object/code_object.hpp"
#include "lib/rocprofiler-sdk/context/context.hpp"
#include "lib/rocprofiler-sdk/context/domain.hpp"
@@ -40,6 +32,15 @@
#include "lib/rocprofiler-sdk/marker/marker.hpp"
#include "lib/rocprofiler-sdk/rccl/rccl.hpp"
#include "lib/rocprofiler-sdk/registration.hpp"
#include "lib/rocprofiler-sdk/runtime_initialization.hpp"
#include <rocprofiler-sdk/callback_tracing.h>
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/hip/table_id.h>
#include <rocprofiler-sdk/hsa/table_id.h>
#include <rocprofiler-sdk/marker/table_id.h>
#include <rocprofiler-sdk/rccl/table_id.h>
#include <rocprofiler-sdk/rocprofiler.h>
#include <atomic>
#include <cstdint>
@@ -85,6 +86,7 @@ ROCPROFILER_CALLBACK_TRACING_KIND_STRING(MEMORY_COPY)
ROCPROFILER_CALLBACK_TRACING_KIND_STRING(MEMORY_ALLOCATION)
ROCPROFILER_CALLBACK_TRACING_KIND_STRING(RCCL_API)
ROCPROFILER_CALLBACK_TRACING_KIND_STRING(OPENMP)
ROCPROFILER_CALLBACK_TRACING_KIND_STRING(RUNTIME_INITIALIZATION)
template <size_t Idx, size_t... Tail>
std::pair<const char*, size_t>
@@ -263,6 +265,10 @@ rocprofiler_query_callback_tracing_kind_operation_name(rocprofiler_callback_trac
val = rocprofiler::hsa::memory_allocation::name_by_id(operation);
break;
}
case ROCPROFILER_CALLBACK_TRACING_RUNTIME_INITIALIZATION:
{
val = rocprofiler::runtime_init::name_by_id(operation);
}
};
if(!val)
@@ -387,6 +393,10 @@ rocprofiler_iterate_callback_tracing_kind_operations(
ops = rocprofiler::hsa::memory_allocation::get_ids();
break;
}
case ROCPROFILER_CALLBACK_TRACING_RUNTIME_INITIALIZATION:
{
ops = rocprofiler::runtime_init::get_ids();
}
};
for(const auto& itr : ops)
@@ -519,6 +529,7 @@ rocprofiler_iterate_callback_tracing_kind_operation_args(
case ROCPROFILER_CALLBACK_TRACING_MEMORY_ALLOCATION:
case ROCPROFILER_CALLBACK_TRACING_RCCL_API:
case ROCPROFILER_CALLBACK_TRACING_OPENMP:
case ROCPROFILER_CALLBACK_TRACING_RUNTIME_INITIALIZATION:
{
return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED;
}
@@ -105,7 +105,7 @@ id_by_name(const char* name, std::index_sequence<Idx, IdxTail...>)
if constexpr(sizeof...(IdxTail) > 0)
return id_by_name(name, std::index_sequence<IdxTail...>{});
else
return ROCPROFILER_HSA_AMD_EXT_API_ID_NONE;
return ROCPROFILER_MEMORY_COPY_LAST;
}
template <size_t... Idx>
@@ -113,7 +113,7 @@ void
get_ids(std::vector<uint32_t>& _id_list, std::index_sequence<Idx...>)
{
auto _emplace = [](auto& _vec, uint32_t _v) {
if(_v < static_cast<uint32_t>(ROCPROFILER_HSA_AMD_EXT_API_ID_LAST)) _vec.emplace_back(_v);
if(_v < static_cast<uint32_t>(ROCPROFILER_MEMORY_COPY_LAST)) _vec.emplace_back(_v);
};
(_emplace(_id_list, async_copy_info<Idx>::operation_idx), ...);
@@ -318,12 +318,12 @@ get_agent(T val, IterateFunc iterate_func, CallbackFunc callback)
if(existing.count(val) == 0)
{
auto agents = rocprofiler::agent::get_agents();
for(auto itr : agents)
for(const auto* itr : agents)
{
auto hsa_agent = rocprofiler::agent::get_hsa_agent(itr);
if(hsa_agent)
{
auto rocprof_agent = rocprofiler::agent::get_rocprofiler_agent(*hsa_agent);
const auto* rocprof_agent = rocprofiler::agent::get_rocprofiler_agent(*hsa_agent);
if(rocprof_agent)
{
auto data = typename memory_allocation_info<OpIdx>::pairtype{&existing,
+37 -4
View File
@@ -45,6 +45,7 @@
#include "lib/rocprofiler-sdk/pc_sampling/code_object.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/service.hpp"
#include "lib/rocprofiler-sdk/rccl/rccl.hpp"
#include "lib/rocprofiler-sdk/runtime_initialization.hpp"
#include <rocprofiler-sdk/context.h>
#include <rocprofiler-sdk/fwd.h>
@@ -52,6 +53,9 @@
#include <rocprofiler-sdk/marker.h>
#include <rocprofiler-sdk/version.h>
#include <hsa/hsa_api_trace.h>
#include <hip/amd_detail/hip_api_trace.hpp>
#include <fmt/format.h>
#include <dlfcn.h>
@@ -59,9 +63,9 @@
#include <unistd.h>
#include <atomic>
#include <cctype>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <hip/amd_detail/hip_api_trace.hpp>
#include <iostream>
#include <limits>
#include <memory>
@@ -732,6 +736,11 @@ rocprofiler_set_api_table(const char* name,
// install rocprofiler API wrappers
rocprofiler::hip::update_table(hip_runtime_api_table);
// Tracing notifications the runtime has initialized
rocprofiler::runtime_init::initialize(
ROCPROFILER_RUNTIME_INITIALIZATION_HIP, lib_version, lib_instance);
// allow tools to install API wrappers
rocprofiler::intercept_table::notify_intercept_table_registration(
ROCPROFILER_HIP_RUNTIME_TABLE,
lib_version,
@@ -754,6 +763,7 @@ rocprofiler_set_api_table(const char* name,
// install rocprofiler API wrappers
rocprofiler::hip::update_table(hip_compiler_api_table);
// allow tools to install API wrappers
rocprofiler::intercept_table::notify_intercept_table_registration(
ROCPROFILER_HIP_COMPILER_TABLE,
lib_version,
@@ -773,6 +783,12 @@ rocprofiler_set_api_table(const char* name,
auto* hsa_api_table = static_cast<HsaApiTable*>(*tables);
#if ROCPROFILER_SDK_HSA_PC_SAMPLING > 0
auto hsa_api_table_size = hsa_api_table->version.minor_id;
auto runtime_pc_sampling_table =
(offsetof(::HsaApiTable, pc_sampling_ext_) < hsa_api_table_size);
#endif
// store a reference of the HsaApiTable implementations for invoking these functions
// without going through tracing wrappers
rocprofiler::hsa::copy_table(hsa_api_table->core_, lib_instance);
@@ -781,7 +797,8 @@ rocprofiler_set_api_table(const char* name,
rocprofiler::hsa::copy_table(hsa_api_table->finalizer_ext_, lib_instance);
rocprofiler::hsa::copy_table(hsa_api_table->tools_, lib_instance);
#if ROCPROFILER_SDK_HSA_PC_SAMPLING > 0
rocprofiler::hsa::copy_table(hsa_api_table->pc_sampling_ext_, lib_instance);
if(runtime_pc_sampling_table)
rocprofiler::hsa::copy_table(hsa_api_table->pc_sampling_ext_, lib_instance);
#endif
// need to construct agent mappings before initializing the queue controller
@@ -796,7 +813,8 @@ rocprofiler_set_api_table(const char* name,
rocprofiler::code_object::initialize(hsa_api_table);
rocprofiler::thread_trace::initialize(hsa_api_table);
#if ROCPROFILER_SDK_HSA_PC_SAMPLING > 0
rocprofiler::pc_sampling::code_object::initialize(hsa_api_table);
if(runtime_pc_sampling_table)
rocprofiler::pc_sampling::code_object::initialize(hsa_api_table);
#endif
// install rocprofiler API wrappers
@@ -808,9 +826,14 @@ rocprofiler_set_api_table(const char* name,
#if ROCPROFILER_SDK_HSA_PC_SAMPLING > 0
// Initialize PC sampling service if configured
rocprofiler::pc_sampling::post_hsa_init_start_active_service();
if(runtime_pc_sampling_table)
rocprofiler::pc_sampling::post_hsa_init_start_active_service();
#endif
// Tracing notifications the runtime has initialized
rocprofiler::runtime_init::initialize(
ROCPROFILER_RUNTIME_INITIALIZATION_HSA, lib_version, lib_instance);
// allow tools to install API wrappers
rocprofiler::intercept_table::notify_intercept_table_registration(
ROCPROFILER_HSA_TABLE, lib_version, lib_instance, std::make_tuple(hsa_api_table));
@@ -839,6 +862,11 @@ rocprofiler_set_api_table(const char* name,
rocprofiler::marker::update_table(roctx_ctrl);
rocprofiler::marker::update_table(roctx_name);
// Tracing notifications the runtime has initialized
rocprofiler::runtime_init::initialize(
ROCPROFILER_RUNTIME_INITIALIZATION_MARKER, lib_version, lib_instance);
// allow tools to install API wrappers
rocprofiler::intercept_table::notify_intercept_table_registration(
ROCPROFILER_MARKER_CORE_TABLE, lib_version, lib_instance, std::make_tuple(roctx_core));
@@ -867,6 +895,11 @@ rocprofiler_set_api_table(const char* name,
// install rocprofiler API wrappers
rocprofiler::rccl::update_table(rccl_api);
// Tracing notifications the runtime has initialized
rocprofiler::runtime_init::initialize(
ROCPROFILER_RUNTIME_INITIALIZATION_RCCL, lib_version, lib_instance);
// allow tools to install API wrappers
rocprofiler::intercept_table::notify_intercept_table_registration(
ROCPROFILER_RCCL_TABLE, lib_version, lib_instance, std::make_tuple(rccl_api));
}
@@ -0,0 +1,162 @@
// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "lib/rocprofiler-sdk/runtime_initialization.hpp"
#include "lib/common/utility.hpp"
#include "lib/rocprofiler-sdk/context/context.hpp"
#include "lib/rocprofiler-sdk/tracing/fwd.hpp"
#include "lib/rocprofiler-sdk/tracing/tracing.hpp"
#include <rocprofiler-sdk/fwd.h>
#include <cstdint>
#include <mutex>
#include <stdexcept>
#include <string>
#include <vector>
namespace rocprofiler
{
namespace runtime_init
{
namespace
{
template <size_t OpIdx>
struct runtime_init_info;
#define SPECIALIZE_RUNTIME_INIT_INFO(NAME, PRETTY_NAME) \
template <> \
struct runtime_init_info<ROCPROFILER_RUNTIME_INITIALIZATION_##NAME> \
{ \
static constexpr auto operation_idx = ROCPROFILER_RUNTIME_INITIALIZATION_##NAME; \
static constexpr auto name = "RUNTIME_INITIALIZATION_" #NAME; \
static constexpr auto pretty_name = PRETTY_NAME; \
};
SPECIALIZE_RUNTIME_INIT_INFO(NONE, "<unknown-runtime>")
SPECIALIZE_RUNTIME_INIT_INFO(HSA, "HSA runtime")
SPECIALIZE_RUNTIME_INIT_INFO(HIP, "HIP runtime")
SPECIALIZE_RUNTIME_INIT_INFO(MARKER, "Marker (ROCTx) runtime")
SPECIALIZE_RUNTIME_INIT_INFO(RCCL, "RCCL runtime")
#undef SPECIALIZE_RUNTIME_INIT_INFO
template <size_t Idx, size_t... IdxTail>
std::pair<const char*, const char*>
name_by_id(const uint32_t id, std::index_sequence<Idx, IdxTail...>)
{
if(Idx == id) return {runtime_init_info<Idx>::name, runtime_init_info<Idx>::pretty_name};
if constexpr(sizeof...(IdxTail) > 0)
return name_by_id(id, std::index_sequence<IdxTail...>{});
else
return {nullptr, nullptr};
}
template <size_t... Idx>
void
get_ids(std::vector<uint32_t>& _id_list, std::index_sequence<Idx...>)
{
auto _emplace = [](auto& _vec, uint32_t _v) {
if(_v < static_cast<uint32_t>(ROCPROFILER_RUNTIME_INITIALIZATION_LAST))
_vec.emplace_back(_v);
};
(_emplace(_id_list, runtime_init_info<Idx>::operation_idx), ...);
}
} // namespace
const char*
name_by_id(uint32_t id)
{
return name_by_id(id, std::make_index_sequence<ROCPROFILER_RUNTIME_INITIALIZATION_LAST>{})
.first;
}
const char*
pretty_name_by_id(uint32_t id)
{
return name_by_id(id, std::make_index_sequence<ROCPROFILER_RUNTIME_INITIALIZATION_LAST>{})
.second;
}
std::vector<uint32_t>
get_ids()
{
auto _data = std::vector<uint32_t>{};
_data.reserve(ROCPROFILER_RUNTIME_INITIALIZATION_LAST);
get_ids(_data, std::make_index_sequence<ROCPROFILER_RUNTIME_INITIALIZATION_LAST>{});
return _data;
}
void
initialize(rocprofiler_runtime_initialization_operation_t operation_idx,
uint64_t lib_version,
uint64_t lib_instance)
{
using runtime_init_record_t = rocprofiler_buffer_tracing_runtime_initialization_record_t;
using runtime_init_data_t = rocprofiler_callback_tracing_runtime_initialization_data_t;
constexpr auto callback_domain_idx = ROCPROFILER_CALLBACK_TRACING_RUNTIME_INITIALIZATION;
constexpr auto buffered_domain_idx = ROCPROFILER_BUFFER_TRACING_RUNTIME_INITIALIZATION;
constexpr auto corr_id = rocprofiler_correlation_id_t{0, rocprofiler_user_data_t{.value = 0}};
ROCP_INFO << pretty_name_by_id(operation_idx) << " has been initialized";
auto thr_id = common::get_tid();
auto data = tracing::tracing_data{};
tracing::populate_contexts(callback_domain_idx, buffered_domain_idx, operation_idx, data);
auto tracer_data = common::init_public_api_struct(runtime_init_data_t{});
auto buffer_record = common::init_public_api_struct(runtime_init_record_t{});
{
tracer_data.version = lib_version;
tracer_data.instance = lib_instance;
tracing::execute_phase_none_callbacks(data.callback_contexts,
thr_id,
corr_id.internal,
data.external_correlation_ids,
callback_domain_idx,
operation_idx,
tracer_data);
}
{
buffer_record.version = lib_version;
buffer_record.instance = lib_instance;
buffer_record.timestamp = common::timestamp_ns();
tracing::execute_buffer_record_emplace(data.buffered_contexts,
thr_id,
corr_id.internal,
data.external_correlation_ids,
buffered_domain_idx,
operation_idx,
buffer_record);
}
}
void
finalize()
{}
} // namespace runtime_init
} // namespace rocprofiler
@@ -0,0 +1,51 @@
// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <rocprofiler-sdk/fwd.h>
#include <cstdint>
#include <vector>
namespace rocprofiler
{
namespace runtime_init
{
const char*
name_by_id(uint32_t id);
const char*
pretty_name_by_id(uint32_t id);
std::vector<uint32_t>
get_ids();
void
initialize(rocprofiler_runtime_initialization_operation_t _runtime,
uint64_t lib_version,
uint64_t lib_instance);
void
finalize();
} // namespace runtime_init
} // namespace rocprofiler