Support Tool Intercept API Tables (#165)
* Update include/rocprofiler - intercept_table.h header - generic rocprofiler_runtime_library_t - rocprofiler_internal_thread_library_t is not typedef for rocprofiler_runtime_library_t - rocprofiler_at_runtime_api_registration * Update lib/rocprofiler - minor tweaks to context.cpp - check if none context early - disallow stop_context when finalizing - add intercept_table.hpp and intercept_table.cpp - implement rocprofiler_at_runtime_api_registration - implement notify_runtime_api_registration - update registration.cpp - invoke notify_runtime_api_registration - tweak to fini status when invoking client finalizer * Update lib/rocprofiler/tests - add tests for intercept table * Add samples/intercept_table - demonstrate how to install custom API function wrappers instead of relying on HSA callback tracing * Update lib/rocprofiler/tests/intercept_table.cpp - remove _SERVICE from ROCPROFILER_SERVICE_ * Update include/rocprofiler/intercept_table.h - Update doxygen comments * Update lib/rocprofiler/intercept_table.cpp - return error config locked if already initialized * Update lib/rocprofiler/intercept_table.cpp - remove unnecessary alias * Apply suggestions from code review Co-authored-by: Tony Tye <Tony.Tye@amd.com> * Update doxygen comments - clarify when rocprofiler_at_runtime_api_registration can be invoked * Use rocprofiler_runtime_library_t for intercept table and internal threading - remove rocprofiler_intercept_library_t alias to rocprofiler_runtime_library_t - remove rocprofiler_internal_thread_library_t alias to rocprofiler_runtime_library_t - move around documentation with regard to rocprofiler_runtime_library_t enumeration - added some extra doxygen documentation to internal threading functions --------- Co-authored-by: Tony Tye <Tony.Tye@amd.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
14373c57be
Коммит
4f2dc896d3
@@ -4,7 +4,7 @@
|
||||
rocprofiler_activate_clang_tidy()
|
||||
|
||||
set(ROCPROFILER_LIB_HEADERS agent.hpp buffer.hpp external_correlation.hpp
|
||||
internal_threading.hpp registration.hpp)
|
||||
intercept_table.hpp internal_threading.hpp registration.hpp)
|
||||
set(ROCPROFILER_LIB_SOURCES
|
||||
agent.cpp
|
||||
buffer.cpp
|
||||
@@ -14,6 +14,7 @@ set(ROCPROFILER_LIB_SOURCES
|
||||
counters.cpp
|
||||
dispatch_profile.cpp
|
||||
external_correlation.cpp
|
||||
intercept_table.cpp
|
||||
internal_threading.cpp
|
||||
pc_sampling.cpp
|
||||
profile_config.cpp
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include <rocprofiler/context.h>
|
||||
#include <rocprofiler/fwd.h>
|
||||
#include <rocprofiler/rocprofiler.h>
|
||||
|
||||
#include "lib/rocprofiler/context/context.hpp"
|
||||
@@ -31,12 +32,17 @@
|
||||
#include <atomic>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr auto rocprofiler_context_none = ROCPROFILER_CONTEXT_NONE;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
rocprofiler_status_t
|
||||
rocprofiler_create_context(rocprofiler_context_id_t* context_id)
|
||||
{
|
||||
// always set to none first
|
||||
*context_id = ROCPROFILER_CONTEXT_NONE;
|
||||
*context_id = rocprofiler_context_none;
|
||||
|
||||
if(rocprofiler::registration::get_init_status() > -1)
|
||||
return ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED;
|
||||
@@ -50,12 +56,22 @@ rocprofiler_create_context(rocprofiler_context_id_t* context_id)
|
||||
rocprofiler_status_t
|
||||
rocprofiler_start_context(rocprofiler_context_id_t context_id)
|
||||
{
|
||||
if(context_id.handle == rocprofiler_context_none.handle)
|
||||
return ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND;
|
||||
|
||||
// if currently finalizing or finalized, don't allow starting a context
|
||||
if(rocprofiler::registration::get_fini_status() != 0)
|
||||
return ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED;
|
||||
|
||||
return rocprofiler::context::start_context(context_id);
|
||||
}
|
||||
|
||||
rocprofiler_status_t
|
||||
rocprofiler_stop_context(rocprofiler_context_id_t context_id)
|
||||
{
|
||||
if(context_id.handle == rocprofiler_context_none.handle)
|
||||
return ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND;
|
||||
|
||||
return rocprofiler::context::stop_context(context_id);
|
||||
}
|
||||
|
||||
@@ -63,6 +79,10 @@ rocprofiler_status_t
|
||||
rocprofiler_context_is_active(rocprofiler_context_id_t context_id, int* status)
|
||||
{
|
||||
*status = 0;
|
||||
|
||||
if(context_id.handle == rocprofiler_context_none.handle)
|
||||
return ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND;
|
||||
|
||||
for(const auto& itr : rocprofiler::context::get_active_contexts())
|
||||
{
|
||||
const auto* cfg = itr.load(std::memory_order_relaxed);
|
||||
@@ -79,6 +99,10 @@ rocprofiler_status_t
|
||||
rocprofiler_context_is_valid(rocprofiler_context_id_t context_id, int* status)
|
||||
{
|
||||
*status = 0;
|
||||
|
||||
if(context_id.handle == rocprofiler_context_none.handle)
|
||||
return ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND;
|
||||
|
||||
for(const auto& itr : rocprofiler::context::get_registered_contexts())
|
||||
{
|
||||
if(itr && itr->context_idx == context_id.handle)
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2023 ROCm Developer Tools
|
||||
//
|
||||
// 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 <rocprofiler/fwd.h>
|
||||
#include <rocprofiler/intercept_table.h>
|
||||
#include <rocprofiler/rocprofiler.h>
|
||||
|
||||
#include "lib/common/container/stable_vector.hpp"
|
||||
#include "lib/common/utility.hpp"
|
||||
#include "lib/rocprofiler/buffer.hpp"
|
||||
#include "lib/rocprofiler/context/context.hpp"
|
||||
#include "lib/rocprofiler/internal_threading.hpp"
|
||||
#include "lib/rocprofiler/registration.hpp"
|
||||
|
||||
#include <hsa/hsa_api_trace.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace rocprofiler
|
||||
{
|
||||
namespace intercept_table
|
||||
{
|
||||
namespace
|
||||
{
|
||||
template <rocprofiler_runtime_library_t... Idx>
|
||||
using library_sequence_t = std::integer_sequence<rocprofiler_runtime_library_t, Idx...>;
|
||||
|
||||
// this is used to loop over the different libraries
|
||||
constexpr auto intercept_library_seq = library_sequence_t<ROCPROFILER_HSA_LIBRARY,
|
||||
ROCPROFILER_HIP_LIBRARY,
|
||||
ROCPROFILER_MARKER_LIBRARY>{};
|
||||
|
||||
// check that intercept_library_seq is up to date
|
||||
static_assert((1 << (intercept_library_seq.size())) == ROCPROFILER_LIBRARY_LAST,
|
||||
"Update intercept_library_seq to include new libraries");
|
||||
|
||||
// data structure holding list of callbacks
|
||||
template <rocprofiler_runtime_library_t LibT>
|
||||
struct intercept
|
||||
{
|
||||
static constexpr auto value = LibT;
|
||||
|
||||
std::vector<rocprofiler_intercept_library_cb_t> callbacks = {};
|
||||
std::vector<void*> user_data = {};
|
||||
std::mutex mutex = {};
|
||||
};
|
||||
|
||||
// static accessor for intercept instance
|
||||
template <rocprofiler_runtime_library_t LibT>
|
||||
auto&
|
||||
get_intercept()
|
||||
{
|
||||
static auto _v = intercept<LibT>{};
|
||||
return _v;
|
||||
}
|
||||
|
||||
// adds callbacks to intercept instance(s)
|
||||
template <rocprofiler_runtime_library_t... Idx>
|
||||
void
|
||||
update_intercepts(rocprofiler_intercept_library_cb_t cb,
|
||||
int libs,
|
||||
void* data,
|
||||
library_sequence_t<Idx...>)
|
||||
{
|
||||
auto update = [cb, libs, data](auto& notifier) {
|
||||
if(libs == 0 || ((libs & notifier.value) == notifier.value))
|
||||
{
|
||||
notifier.mutex.lock();
|
||||
notifier.callbacks.emplace_back(cb);
|
||||
notifier.user_data.emplace_back(data);
|
||||
notifier.mutex.unlock();
|
||||
}
|
||||
};
|
||||
|
||||
(update(get_intercept<Idx>()), ...);
|
||||
}
|
||||
|
||||
template <typename... Tp, size_t... Idx>
|
||||
auto
|
||||
get_void_array(std::tuple<Tp*...> data, std::index_sequence<Idx...>)
|
||||
{
|
||||
constexpr auto size = sizeof...(Idx);
|
||||
return std::array<void*, size>{static_cast<void*>(std::get<Idx>(data))...};
|
||||
};
|
||||
|
||||
// invokes creation notifiers
|
||||
template <typename... ApiTableT, rocprofiler_runtime_library_t... Idx>
|
||||
void
|
||||
execute_intercepts(rocprofiler_runtime_library_t lib,
|
||||
uint64_t lib_version,
|
||||
uint64_t lib_instance,
|
||||
std::tuple<ApiTableT*...> tables,
|
||||
std::integer_sequence<rocprofiler_runtime_library_t, Idx...>)
|
||||
{
|
||||
auto execute = [lib, lib_version, lib_instance, tables](auto& notifier) {
|
||||
if(((lib & notifier.value) == notifier.value))
|
||||
{
|
||||
constexpr uint64_t num_tables = sizeof...(ApiTableT);
|
||||
auto tables_v = get_void_array(tables, std::make_index_sequence<num_tables>{});
|
||||
|
||||
notifier.mutex.lock();
|
||||
for(size_t i = 0; i < notifier.callbacks.size(); ++i)
|
||||
{
|
||||
auto itr = notifier.callbacks.at(i);
|
||||
if(itr)
|
||||
itr(notifier.value,
|
||||
lib_version,
|
||||
lib_instance,
|
||||
tables_v.data(),
|
||||
num_tables,
|
||||
notifier.user_data.at(i));
|
||||
}
|
||||
notifier.mutex.unlock();
|
||||
}
|
||||
};
|
||||
|
||||
(execute(get_intercept<Idx>()), ...);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
template <typename... ApiTableT>
|
||||
void
|
||||
notify_runtime_api_registration(rocprofiler_runtime_library_t lib,
|
||||
uint64_t lib_version,
|
||||
uint64_t lib_instance,
|
||||
std::tuple<ApiTableT*...> tables)
|
||||
{
|
||||
execute_intercepts(lib, lib_version, lib_instance, tables, intercept_library_seq);
|
||||
}
|
||||
|
||||
// template instantiation for HsaApiTable
|
||||
template void notify_runtime_api_registration(rocprofiler_runtime_library_t,
|
||||
uint64_t,
|
||||
uint64_t,
|
||||
std::tuple<HsaApiTable*>);
|
||||
} // namespace intercept_table
|
||||
} // namespace rocprofiler
|
||||
|
||||
extern "C" {
|
||||
rocprofiler_status_t
|
||||
rocprofiler_at_runtime_api_registration(rocprofiler_intercept_library_cb_t callback,
|
||||
int libs,
|
||||
void* data)
|
||||
{
|
||||
// if this function is invoked after initialization, we cannot guarantee that the runtime
|
||||
// intercept API has not already be registered and returned to the runtime.
|
||||
if(rocprofiler::registration::get_init_status() > 0)
|
||||
return ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED;
|
||||
|
||||
if((libs & ROCPROFILER_LIBRARY) == ROCPROFILER_LIBRARY)
|
||||
return ROCPROFILER_STATUS_ERROR_INVALID_ARGUMENT;
|
||||
else if((libs & ROCPROFILER_HIP_LIBRARY) == ROCPROFILER_HIP_LIBRARY)
|
||||
return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED;
|
||||
else if((libs & ROCPROFILER_MARKER_LIBRARY) == ROCPROFILER_MARKER_LIBRARY)
|
||||
return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED;
|
||||
|
||||
rocprofiler::intercept_table::update_intercepts(
|
||||
callback, libs, data, rocprofiler::intercept_table::intercept_library_seq);
|
||||
return ROCPROFILER_STATUS_SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2023 ROCm Developer Tools
|
||||
//
|
||||
// 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/intercept_table.h>
|
||||
|
||||
#include <tuple>
|
||||
|
||||
namespace rocprofiler
|
||||
{
|
||||
namespace intercept_table
|
||||
{
|
||||
template <typename... ApiTableT>
|
||||
void
|
||||
notify_runtime_api_registration(rocprofiler_runtime_library_t lib,
|
||||
uint64_t lib_version,
|
||||
uint64_t lib_instance,
|
||||
std::tuple<ApiTableT*...> tables);
|
||||
}
|
||||
} // namespace rocprofiler
|
||||
@@ -43,9 +43,9 @@ namespace internal_threading
|
||||
{
|
||||
namespace
|
||||
{
|
||||
template <rocprofiler_internal_thread_library_t... Idx>
|
||||
using library_sequence_t = std::integer_sequence<rocprofiler_internal_thread_library_t, Idx...>;
|
||||
using creation_notifier_cb_t = void (*)(rocprofiler_internal_thread_library_t, void*);
|
||||
template <rocprofiler_runtime_library_t... Idx>
|
||||
using library_sequence_t = std::integer_sequence<rocprofiler_runtime_library_t, Idx...>;
|
||||
using creation_notifier_cb_t = void (*)(rocprofiler_runtime_library_t, void*);
|
||||
using thread_pool_config_t = PTL::ThreadPool::Config;
|
||||
|
||||
// this is used to loop over the different libraries
|
||||
@@ -66,7 +66,7 @@ enum class notifier_stage
|
||||
};
|
||||
|
||||
// data structure holding list of callbacks
|
||||
template <rocprofiler_internal_thread_library_t LibT>
|
||||
template <rocprofiler_runtime_library_t LibT>
|
||||
struct creation_notifier
|
||||
{
|
||||
static constexpr auto value = LibT;
|
||||
@@ -78,7 +78,7 @@ struct creation_notifier
|
||||
};
|
||||
|
||||
// static accessor for creation_notifier instance
|
||||
template <rocprofiler_internal_thread_library_t LibT>
|
||||
template <rocprofiler_runtime_library_t LibT>
|
||||
auto&
|
||||
get_creation_notifier()
|
||||
{
|
||||
@@ -87,7 +87,7 @@ get_creation_notifier()
|
||||
}
|
||||
|
||||
// adds callbacks to creation_notifier instance(s)
|
||||
template <rocprofiler_internal_thread_library_t... Idx>
|
||||
template <rocprofiler_runtime_library_t... Idx>
|
||||
void
|
||||
update_creation_notifiers(creation_notifier_cb_t pre,
|
||||
creation_notifier_cb_t post,
|
||||
@@ -110,10 +110,10 @@ update_creation_notifiers(creation_notifier_cb_t pre,
|
||||
}
|
||||
|
||||
// invokes creation notifiers
|
||||
template <notifier_stage StageT, rocprofiler_internal_thread_library_t... Idx>
|
||||
template <notifier_stage StageT, rocprofiler_runtime_library_t... Idx>
|
||||
void
|
||||
execute_creation_notifiers(rocprofiler_internal_thread_library_t libs,
|
||||
std::integer_sequence<rocprofiler_internal_thread_library_t, Idx...>)
|
||||
execute_creation_notifiers(rocprofiler_runtime_library_t libs,
|
||||
std::integer_sequence<rocprofiler_runtime_library_t, Idx...>)
|
||||
{
|
||||
auto execute = [libs](auto& notifier) {
|
||||
if(((libs & notifier.value) == notifier.value))
|
||||
@@ -189,13 +189,13 @@ finalize()
|
||||
}
|
||||
|
||||
void
|
||||
notify_pre_internal_thread_create(rocprofiler_internal_thread_library_t libs)
|
||||
notify_pre_internal_thread_create(rocprofiler_runtime_library_t libs)
|
||||
{
|
||||
execute_creation_notifiers<notifier_stage::precreation>(libs, creation_notifier_library_seq);
|
||||
}
|
||||
|
||||
void
|
||||
notify_post_internal_thread_create(rocprofiler_internal_thread_library_t libs)
|
||||
notify_post_internal_thread_create(rocprofiler_runtime_library_t libs)
|
||||
{
|
||||
execute_creation_notifiers<notifier_stage::postcreation>(libs, creation_notifier_library_seq);
|
||||
}
|
||||
|
||||
@@ -54,8 +54,8 @@ using task_group_vec_t = rocprofiler::common::static_cleanup_wrapper<
|
||||
std::vector<task_group_cleanup_t>,
|
||||
std::function<void(std::vector<task_group_cleanup_t>&)>>;
|
||||
|
||||
void notify_pre_internal_thread_create(rocprofiler_internal_thread_library_t);
|
||||
void notify_post_internal_thread_create(rocprofiler_internal_thread_library_t);
|
||||
void notify_pre_internal_thread_create(rocprofiler_runtime_library_t);
|
||||
void notify_post_internal_thread_create(rocprofiler_runtime_library_t);
|
||||
|
||||
// initialize the default thread pool
|
||||
void
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "lib/rocprofiler/hsa/hsa.hpp"
|
||||
#include "lib/rocprofiler/hsa/queue.hpp"
|
||||
#include "lib/rocprofiler/hsa/queue_controller.hpp"
|
||||
#include "lib/rocprofiler/intercept_table.hpp"
|
||||
#include "lib/rocprofiler/internal_threading.hpp"
|
||||
|
||||
#include <rocprofiler/context.h>
|
||||
@@ -369,7 +370,10 @@ invoke_client_finalizer(rocprofiler_client_id_t client_id)
|
||||
{
|
||||
if(itr.configure_result && itr.configure_result->finalize)
|
||||
{
|
||||
auto _fini_status = get_fini_status();
|
||||
if(_fini_status == 0) set_fini_status(-1);
|
||||
itr.configure_result->finalize(itr.configure_result->tool_data);
|
||||
if(_fini_status == 0) set_fini_status(_fini_status);
|
||||
// set to nullptr so finalize only gets called once
|
||||
itr.configure_result->finalize = nullptr;
|
||||
}
|
||||
@@ -553,7 +557,12 @@ rocprofiler_set_api_table(const char* name,
|
||||
auto& saved_hsa_api_table = rocprofiler::hsa::get_table();
|
||||
::copyTables(hsa_api_table, &saved_hsa_api_table);
|
||||
|
||||
// install rocprofiler API wrappers
|
||||
rocprofiler::hsa::update_table(hsa_api_table);
|
||||
|
||||
// allow tools to install API wrappers
|
||||
rocprofiler::intercept_table::notify_runtime_api_registration(
|
||||
ROCPROFILER_HSA_LIBRARY, lib_version, lib_instance, std::make_tuple(hsa_api_table));
|
||||
}
|
||||
else if(std::string_view{name} == "roctx")
|
||||
{
|
||||
|
||||
@@ -34,7 +34,8 @@ set_tests_properties(${lib_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests")
|
||||
#
|
||||
# -------------------------------------------------------------------------------------- #
|
||||
|
||||
set(rocprofiler_shared_lib_sources external_correlation.cpp registration.cpp status.cpp)
|
||||
set(rocprofiler_shared_lib_sources external_correlation.cpp intercept_table.cpp
|
||||
registration.cpp status.cpp)
|
||||
|
||||
add_executable(rocprofiler-lib-tests-shared)
|
||||
target_sources(rocprofiler-lib-tests-shared PRIVATE ${rocprofiler_shared_lib_sources})
|
||||
|
||||
@@ -208,14 +208,14 @@ tool_tracing_buffered(rocprofiler_context_id_t context,
|
||||
}
|
||||
|
||||
void
|
||||
thread_precreate(rocprofiler_internal_thread_library_t /*lib*/, void* tool_data)
|
||||
thread_precreate(rocprofiler_runtime_library_t /*lib*/, void* tool_data)
|
||||
{
|
||||
auto* cb_data = static_cast<callback_data*>(tool_data);
|
||||
cb_data->client_workflow_count++;
|
||||
}
|
||||
|
||||
void
|
||||
thread_postcreate(rocprofiler_internal_thread_library_t /*lib*/, void* tool_data)
|
||||
thread_postcreate(rocprofiler_runtime_library_t /*lib*/, void* tool_data)
|
||||
{
|
||||
auto* cb_data = static_cast<callback_data*>(tool_data);
|
||||
cb_data->client_workflow_count++;
|
||||
|
||||
@@ -0,0 +1,631 @@
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2023 ROCm Developer Tools
|
||||
//
|
||||
// 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 <rocprofiler/context.h>
|
||||
#include <rocprofiler/fwd.h>
|
||||
#include <rocprofiler/registration.h>
|
||||
#include <rocprofiler/rocprofiler.h>
|
||||
|
||||
#include "lib/common/environment.hpp"
|
||||
#include "lib/common/units.hpp"
|
||||
#include "lib/common/utility.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <hsa/hsa.h>
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <pthread.h>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <string_view>
|
||||
#include <typeinfo>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#define ROCPROFILER_CALL(ARG, MSG) \
|
||||
{ \
|
||||
auto _status = (ARG); \
|
||||
EXPECT_EQ(_status, ROCPROFILER_STATUS_SUCCESS) \
|
||||
<< MSG << " (" << _status << "=" << rocprofiler_get_status_string(_status) \
|
||||
<< ") :: " << #ARG; \
|
||||
}
|
||||
|
||||
#define ROCPROFILER_CALL_EXPECT(ARG, MSG, STATUS) \
|
||||
{ \
|
||||
auto _status = (ARG); \
|
||||
EXPECT_EQ(_status, STATUS) << MSG << " (" << _status << "=" \
|
||||
<< rocprofiler_get_status_string(_status) << ") :: " << #ARG; \
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
struct callback_data
|
||||
{
|
||||
using callback_count_data_t = std::pair<uint64_t, uint64_t>;
|
||||
using callback_count_map_t = std::map<std::string_view, callback_count_data_t>;
|
||||
|
||||
rocprofiler_client_id_t* client_id = nullptr;
|
||||
rocprofiler_client_finalize_t client_fini_func = nullptr;
|
||||
rocprofiler_context_id_t client_ctx = {};
|
||||
rocprofiler_buffer_id_t client_buffer = {};
|
||||
rocprofiler_callback_thread_t client_thread = {};
|
||||
uint64_t client_workflow_count = {};
|
||||
uint64_t client_elapsed = {};
|
||||
int64_t current_depth = 0;
|
||||
int64_t max_depth = 0;
|
||||
callback_count_map_t client_callback_count = {};
|
||||
};
|
||||
|
||||
struct agent_data
|
||||
{
|
||||
uint64_t agent_count = 0;
|
||||
std::vector<hsa_device_type_t> agents = {};
|
||||
};
|
||||
|
||||
using callback_kind_names_t = std::map<rocprofiler_service_callback_tracing_kind_t, const char*>;
|
||||
using callback_kind_operation_names_t =
|
||||
std::map<rocprofiler_service_callback_tracing_kind_t, std::map<uint32_t, const char*>>;
|
||||
using wrap_count_t = std::pair<std::string_view, size_t>;
|
||||
|
||||
struct callback_name_info
|
||||
{
|
||||
callback_kind_names_t kind_names = {};
|
||||
callback_kind_operation_names_t operation_names = {};
|
||||
};
|
||||
|
||||
auto&
|
||||
get_preincrement()
|
||||
{
|
||||
static size_t _v = 0;
|
||||
return _v;
|
||||
}
|
||||
|
||||
auto&
|
||||
get_postincrement()
|
||||
{
|
||||
static size_t _v = 0;
|
||||
return _v;
|
||||
}
|
||||
|
||||
auto&
|
||||
get_client_callback_data()
|
||||
{
|
||||
static auto _v = callback_data{};
|
||||
return _v;
|
||||
}
|
||||
|
||||
auto
|
||||
get_callback_tracing_names()
|
||||
{
|
||||
auto cb_name_info = callback_name_info{};
|
||||
//
|
||||
// callback for each kind operation
|
||||
//
|
||||
static auto tracing_kind_operation_cb =
|
||||
[](rocprofiler_service_callback_tracing_kind_t kindv, uint32_t operation, void* data_v) {
|
||||
auto* name_info_v = static_cast<callback_name_info*>(data_v);
|
||||
|
||||
if(kindv == ROCPROFILER_CALLBACK_TRACING_HSA_API)
|
||||
{
|
||||
const char* name = nullptr;
|
||||
ROCPROFILER_CALL(rocprofiler_query_callback_tracing_kind_operation_name(
|
||||
kindv, operation, &name, nullptr),
|
||||
"query callback tracing kind operation name");
|
||||
if(name) name_info_v->operation_names[kindv][operation] = name;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
//
|
||||
// callback for each callback kind (i.e. domain)
|
||||
//
|
||||
static auto tracing_kind_cb = [](rocprofiler_service_callback_tracing_kind_t kind, void* data) {
|
||||
// store the callback kind name
|
||||
auto* name_info_v = static_cast<callback_name_info*>(data);
|
||||
const char* name = nullptr;
|
||||
ROCPROFILER_CALL(rocprofiler_query_callback_tracing_kind_name(kind, &name, nullptr),
|
||||
"query callback tracing kind operation name");
|
||||
if(name) name_info_v->kind_names[kind] = name;
|
||||
|
||||
if(kind == ROCPROFILER_CALLBACK_TRACING_HSA_API)
|
||||
{
|
||||
ROCPROFILER_CALL(rocprofiler_iterate_callback_tracing_kind_operations(
|
||||
kind, tracing_kind_operation_cb, static_cast<void*>(data)),
|
||||
"iterating callback tracing kind operations");
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
ROCPROFILER_CALL(rocprofiler_iterate_callback_tracing_kinds(tracing_kind_cb,
|
||||
static_cast<void*>(&cb_name_info)),
|
||||
"iterating callback tracing kinds");
|
||||
|
||||
return cb_name_info;
|
||||
}
|
||||
|
||||
void
|
||||
tool_tracing_callback(rocprofiler_callback_tracing_record_t record,
|
||||
rocprofiler_user_data_t* /*user_data*/,
|
||||
void* client_data)
|
||||
{
|
||||
auto* cb_data = static_cast<callback_data*>(client_data);
|
||||
|
||||
static auto name_map = get_callback_tracing_names();
|
||||
if(record.phase == ROCPROFILER_CALLBACK_PHASE_ENTER)
|
||||
{
|
||||
cb_data->client_callback_count[name_map.operation_names[record.kind][record.operation]]
|
||||
.second += get_preincrement();
|
||||
}
|
||||
else if(record.phase == ROCPROFILER_CALLBACK_PHASE_EXIT)
|
||||
{
|
||||
cb_data->client_callback_count[name_map.operation_names[record.kind][record.operation]]
|
||||
.second += get_postincrement();
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t Idx, typename RetT, typename... Args>
|
||||
auto
|
||||
generate_wrapper(const char* name, RetT (*func)(Args...))
|
||||
{
|
||||
using functor_type = RetT (*)(Args...);
|
||||
|
||||
static functor_type underlying_func = func;
|
||||
static auto func_name = std::string_view{name};
|
||||
|
||||
get_client_callback_data().client_callback_count.emplace(
|
||||
std::string_view{name}, callback_data::callback_count_data_t{0, 0});
|
||||
|
||||
static functor_type wrapped_func = [](Args... args) -> RetT {
|
||||
get_client_callback_data().client_callback_count.at(func_name).first +=
|
||||
get_preincrement() + get_postincrement();
|
||||
if(underlying_func) return underlying_func(args...);
|
||||
if constexpr(!std::is_void<RetT>::value) return RetT{};
|
||||
};
|
||||
|
||||
return wrapped_func;
|
||||
}
|
||||
|
||||
#define GENERATE_WRAPPER(TABLE, FUNC) \
|
||||
TABLE->FUNC##_fn = generate_wrapper<__COUNTER__>(#FUNC, TABLE->FUNC##_fn)
|
||||
|
||||
void
|
||||
api_registration_callback(rocprofiler_runtime_library_t type,
|
||||
uint64_t lib_version,
|
||||
uint64_t lib_instance,
|
||||
void** tables,
|
||||
uint64_t num_tables,
|
||||
void* client_data)
|
||||
{
|
||||
auto* cb_data = static_cast<callback_data*>(client_data);
|
||||
|
||||
cb_data->client_workflow_count++;
|
||||
|
||||
EXPECT_EQ(type, ROCPROFILER_HSA_LIBRARY) << "unexpected library type: " << type;
|
||||
EXPECT_EQ(lib_instance, 0) << "multiple instances of HSA runtime library";
|
||||
EXPECT_EQ(num_tables, 1) << "expected only one table of type HsaApiTable";
|
||||
EXPECT_GT(lib_version, 0) << "expected library version > 0";
|
||||
|
||||
auto* hsa_api_table = static_cast<HsaApiTable*>(tables[0]);
|
||||
GENERATE_WRAPPER(hsa_api_table->core_, hsa_init);
|
||||
GENERATE_WRAPPER(hsa_api_table->core_, hsa_agent_get_info);
|
||||
GENERATE_WRAPPER(hsa_api_table->core_, hsa_iterate_agents);
|
||||
GENERATE_WRAPPER(hsa_api_table->core_, hsa_shut_down);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(rocprofiler_lib, intercept_table_and_callback_tracing)
|
||||
{
|
||||
// test layering of tool interception of API table on top of rocprofiler API tracing.
|
||||
// This test enables both rocprofiler API tracing and a tool installing it's own
|
||||
// wrappers via the HsaApiTable. With both active, one should see the same
|
||||
// number of calls to "hsa_init", "hsa_iterate_agents", "hsa_agent_get_info", and
|
||||
// "hsa_shut_down"
|
||||
|
||||
using init_func_t = int (*)(rocprofiler_client_finalize_t, void*);
|
||||
using fini_func_t = void (*)(void*);
|
||||
using hsa_iterate_agents_cb_t = hsa_status_t (*)(hsa_agent_t, void*);
|
||||
|
||||
get_preincrement() = 1;
|
||||
get_postincrement() = 0;
|
||||
|
||||
static init_func_t tool_init = [](rocprofiler_client_finalize_t fini_func,
|
||||
void* client_data) -> int {
|
||||
auto* cb_data = static_cast<callback_data*>(client_data);
|
||||
|
||||
cb_data->client_workflow_count++;
|
||||
cb_data->client_fini_func = fini_func;
|
||||
|
||||
ROCPROFILER_CALL(rocprofiler_create_context(&cb_data->client_ctx),
|
||||
"failed to create context");
|
||||
|
||||
auto operations = std::vector<uint32_t>{ROCPROFILER_HSA_API_ID_hsa_init,
|
||||
ROCPROFILER_HSA_API_ID_hsa_iterate_agents,
|
||||
ROCPROFILER_HSA_API_ID_hsa_agent_get_info,
|
||||
ROCPROFILER_HSA_API_ID_hsa_shut_down};
|
||||
|
||||
ROCPROFILER_CALL(
|
||||
rocprofiler_configure_callback_tracing_service(cb_data->client_ctx,
|
||||
ROCPROFILER_CALLBACK_TRACING_HSA_API,
|
||||
operations.data(),
|
||||
operations.size(),
|
||||
tool_tracing_callback,
|
||||
client_data),
|
||||
"callback tracing service failed to configure");
|
||||
|
||||
int valid_ctx = 0;
|
||||
ROCPROFILER_CALL(rocprofiler_context_is_valid(cb_data->client_ctx, &valid_ctx),
|
||||
"failure checking context validity");
|
||||
|
||||
EXPECT_EQ(valid_ctx, 1);
|
||||
|
||||
ROCPROFILER_CALL(rocprofiler_start_context(cb_data->client_ctx),
|
||||
"rocprofiler context start failed");
|
||||
|
||||
// no errors
|
||||
return 0;
|
||||
};
|
||||
|
||||
static fini_func_t tool_fini = [](void* client_data) -> void {
|
||||
auto* cb_data = static_cast<callback_data*>(client_data);
|
||||
ROCPROFILER_CALL(rocprofiler_stop_context(cb_data->client_ctx),
|
||||
"rocprofiler context stop failed");
|
||||
|
||||
static_cast<callback_data*>(client_data)->client_workflow_count++;
|
||||
};
|
||||
|
||||
static auto& cb_data = get_client_callback_data();
|
||||
|
||||
static auto cfg_result =
|
||||
rocprofiler_tool_configure_result_t{sizeof(rocprofiler_tool_configure_result_t),
|
||||
tool_init,
|
||||
tool_fini,
|
||||
static_cast<void*>(&cb_data)};
|
||||
|
||||
static rocprofiler_configure_func_t rocp_init =
|
||||
[](uint32_t version,
|
||||
const char* runtime_version,
|
||||
uint32_t prio,
|
||||
rocprofiler_client_id_t* client_id) -> rocprofiler_tool_configure_result_t* {
|
||||
auto expected_version = ROCPROFILER_VERSION;
|
||||
EXPECT_EQ(expected_version, version);
|
||||
EXPECT_EQ(std::string_view{runtime_version}, std::string_view{ROCPROFILER_VERSION_STRING});
|
||||
EXPECT_EQ(prio, 0);
|
||||
EXPECT_EQ(client_id->name, nullptr);
|
||||
cb_data.client_id = client_id;
|
||||
cb_data.client_id->name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
|
||||
|
||||
ROCPROFILER_CALL_EXPECT(
|
||||
rocprofiler_at_runtime_api_registration(api_registration_callback,
|
||||
ROCPROFILER_LIBRARY | ROCPROFILER_HSA_LIBRARY |
|
||||
ROCPROFILER_HIP_LIBRARY |
|
||||
ROCPROFILER_MARKER_LIBRARY,
|
||||
static_cast<void*>(&cb_data)),
|
||||
"function should return invalid argument if ROCPROFILER_LIBRARY included",
|
||||
ROCPROFILER_STATUS_ERROR_INVALID_ARGUMENT);
|
||||
|
||||
using init_list_t = std::initializer_list<int>;
|
||||
for(auto itr : init_list_t{
|
||||
(ROCPROFILER_HSA_LIBRARY | ROCPROFILER_HIP_LIBRARY | ROCPROFILER_MARKER_LIBRARY),
|
||||
(ROCPROFILER_HSA_LIBRARY | ROCPROFILER_HIP_LIBRARY),
|
||||
(ROCPROFILER_HIP_LIBRARY),
|
||||
(ROCPROFILER_HSA_LIBRARY | ROCPROFILER_MARKER_LIBRARY),
|
||||
(ROCPROFILER_MARKER_LIBRARY)})
|
||||
{
|
||||
ROCPROFILER_CALL_EXPECT(
|
||||
rocprofiler_at_runtime_api_registration(
|
||||
api_registration_callback, itr, static_cast<void*>(&cb_data)),
|
||||
"test should be updated if new (non-HSA) intercept table is supported",
|
||||
ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED);
|
||||
}
|
||||
|
||||
ROCPROFILER_CALL_EXPECT(rocprofiler_at_runtime_api_registration(
|
||||
api_registration_callback,
|
||||
ROCPROFILER_HSA_LIBRARY | ROCPROFILER_HIP_LIBRARY,
|
||||
static_cast<void*>(&cb_data)),
|
||||
"test should be updated if HIP intercept table is supported",
|
||||
ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED);
|
||||
|
||||
ROCPROFILER_CALL_EXPECT(
|
||||
rocprofiler_at_runtime_api_registration(
|
||||
api_registration_callback, ROCPROFILER_HIP_LIBRARY, static_cast<void*>(&cb_data)),
|
||||
"test should be updated if HIP intercept table is supported",
|
||||
ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED);
|
||||
|
||||
ROCPROFILER_CALL_EXPECT(rocprofiler_at_runtime_api_registration(
|
||||
api_registration_callback,
|
||||
ROCPROFILER_HSA_LIBRARY | ROCPROFILER_MARKER_LIBRARY,
|
||||
static_cast<void*>(&cb_data)),
|
||||
"test should be updated if ROCTx intercept table is supported",
|
||||
ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED);
|
||||
|
||||
ROCPROFILER_CALL(
|
||||
rocprofiler_at_runtime_api_registration(
|
||||
api_registration_callback, ROCPROFILER_HSA_LIBRARY, static_cast<void*>(&cb_data)),
|
||||
"HSA API intercept table registration failed");
|
||||
|
||||
return &cfg_result;
|
||||
};
|
||||
|
||||
EXPECT_EQ(rocprofiler_force_configure(rocp_init), ROCPROFILER_STATUS_SUCCESS);
|
||||
|
||||
hsa_iterate_agents_cb_t agent_cb = [](hsa_agent_t agent, void* data) {
|
||||
static_cast<agent_data*>(data)->agent_count++;
|
||||
|
||||
auto status = HSA_STATUS_SUCCESS;
|
||||
auto agent_type = hsa_device_type_t{};
|
||||
if((status = hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &agent_type)) ==
|
||||
HSA_STATUS_SUCCESS)
|
||||
static_cast<agent_data*>(data)->agents.emplace_back(agent_type);
|
||||
|
||||
return status;
|
||||
};
|
||||
|
||||
hsa_init();
|
||||
auto _agent_data = agent_data{};
|
||||
hsa_status_t itr_status = hsa_iterate_agents(agent_cb, static_cast<void*>(&_agent_data));
|
||||
hsa_shut_down();
|
||||
|
||||
EXPECT_EQ(itr_status, HSA_STATUS_SUCCESS);
|
||||
EXPECT_GT(_agent_data.agent_count, 0);
|
||||
EXPECT_EQ(_agent_data.agent_count, _agent_data.agents.size());
|
||||
|
||||
ASSERT_NE(cb_data.client_id, nullptr);
|
||||
ASSERT_NE(cb_data.client_fini_func, nullptr);
|
||||
|
||||
cb_data.client_fini_func(*cb_data.client_id);
|
||||
|
||||
EXPECT_EQ(cb_data.client_workflow_count, 3);
|
||||
|
||||
for(auto itr : cb_data.client_callback_count)
|
||||
{
|
||||
EXPECT_EQ(itr.second.first, itr.second.second)
|
||||
<< "mismatched wrap counts for " << itr.first
|
||||
<< " (lhs=tool_wrapper, rhs=rocprofiler_wrapper)";
|
||||
if(itr.first != "hsa_init")
|
||||
{
|
||||
EXPECT_GT(itr.second.first, 0) << itr.first << " not wrapped";
|
||||
}
|
||||
else
|
||||
{
|
||||
EXPECT_EQ(itr.second.first, 0) << itr.first
|
||||
<< " was wrapped. If hsa runtime has been updated to "
|
||||
"include first call to hsa_init, update this test";
|
||||
}
|
||||
}
|
||||
|
||||
auto get_count = [](std::string_view func_name) {
|
||||
// we already checked that first == second so we can just check first here
|
||||
return cb_data.client_callback_count.at(func_name).first;
|
||||
};
|
||||
|
||||
EXPECT_EQ(get_count("hsa_init"), 0);
|
||||
EXPECT_EQ(get_count("hsa_iterate_agents"), 1);
|
||||
EXPECT_EQ(get_count("hsa_agent_get_info"), _agent_data.agent_count);
|
||||
EXPECT_EQ(get_count("hsa_shut_down"), 1);
|
||||
}
|
||||
|
||||
TEST(rocprofiler_lib, intercept_table_and_callback_tracing_disable_context)
|
||||
{
|
||||
// test layering of tool interception of API table on top of rocprofiler API tracing.
|
||||
// Similar to intercept_table_and_callback_tracing test except on the
|
||||
// first call to hsa_iterate_agents the context is disabled. As a result,
|
||||
// one should only see the rocprofiler API tracing for hsa_iterate_agents
|
||||
// and not for hsa_agent_get_info or hsa_shut_down.
|
||||
|
||||
using init_func_t = int (*)(rocprofiler_client_finalize_t, void*);
|
||||
using fini_func_t = void (*)(void*);
|
||||
using hsa_iterate_agents_cb_t = hsa_status_t (*)(hsa_agent_t, void*);
|
||||
|
||||
get_preincrement() = 1;
|
||||
get_postincrement() = 1;
|
||||
|
||||
static init_func_t tool_init = [](rocprofiler_client_finalize_t fini_func,
|
||||
void* client_data) -> int {
|
||||
auto* cb_data = static_cast<callback_data*>(client_data);
|
||||
|
||||
cb_data->client_workflow_count++;
|
||||
cb_data->client_fini_func = fini_func;
|
||||
|
||||
ROCPROFILER_CALL(rocprofiler_create_context(&cb_data->client_ctx),
|
||||
"failed to create context");
|
||||
|
||||
auto operations = std::vector<uint32_t>{ROCPROFILER_HSA_API_ID_hsa_init,
|
||||
ROCPROFILER_HSA_API_ID_hsa_iterate_agents,
|
||||
ROCPROFILER_HSA_API_ID_hsa_agent_get_info,
|
||||
ROCPROFILER_HSA_API_ID_hsa_shut_down};
|
||||
|
||||
ROCPROFILER_CALL(
|
||||
rocprofiler_configure_callback_tracing_service(cb_data->client_ctx,
|
||||
ROCPROFILER_CALLBACK_TRACING_HSA_API,
|
||||
operations.data(),
|
||||
operations.size(),
|
||||
tool_tracing_callback,
|
||||
client_data),
|
||||
"callback tracing service failed to configure");
|
||||
|
||||
int valid_ctx = 0;
|
||||
ROCPROFILER_CALL(rocprofiler_context_is_valid(cb_data->client_ctx, &valid_ctx),
|
||||
"failure checking context validity");
|
||||
|
||||
EXPECT_EQ(valid_ctx, 1);
|
||||
|
||||
ROCPROFILER_CALL(rocprofiler_start_context(cb_data->client_ctx),
|
||||
"rocprofiler context start failed");
|
||||
|
||||
// no errors
|
||||
return 0;
|
||||
};
|
||||
|
||||
static fini_func_t tool_fini = [](void* client_data) -> void {
|
||||
auto* cb_data = static_cast<callback_data*>(client_data);
|
||||
ROCPROFILER_CALL_EXPECT(rocprofiler_stop_context(cb_data->client_ctx),
|
||||
"rocprofiler context stop",
|
||||
ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND);
|
||||
|
||||
ROCPROFILER_CALL_EXPECT(rocprofiler_start_context(cb_data->client_ctx),
|
||||
"rocprofiler context start",
|
||||
ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED);
|
||||
|
||||
static_cast<callback_data*>(client_data)->client_workflow_count++;
|
||||
};
|
||||
|
||||
static auto& cb_data = get_client_callback_data();
|
||||
cb_data = callback_data{};
|
||||
|
||||
static auto cfg_result =
|
||||
rocprofiler_tool_configure_result_t{sizeof(rocprofiler_tool_configure_result_t),
|
||||
tool_init,
|
||||
tool_fini,
|
||||
static_cast<void*>(&cb_data)};
|
||||
|
||||
static rocprofiler_configure_func_t rocp_init =
|
||||
[](uint32_t version,
|
||||
const char* runtime_version,
|
||||
uint32_t prio,
|
||||
rocprofiler_client_id_t* client_id) -> rocprofiler_tool_configure_result_t* {
|
||||
auto expected_version = ROCPROFILER_VERSION;
|
||||
EXPECT_EQ(expected_version, version);
|
||||
EXPECT_EQ(std::string_view{runtime_version}, std::string_view{ROCPROFILER_VERSION_STRING});
|
||||
EXPECT_EQ(prio, 0);
|
||||
EXPECT_EQ(client_id->name, nullptr);
|
||||
cb_data.client_id = client_id;
|
||||
cb_data.client_id->name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
|
||||
|
||||
ROCPROFILER_CALL_EXPECT(
|
||||
rocprofiler_at_runtime_api_registration(api_registration_callback,
|
||||
ROCPROFILER_LIBRARY | ROCPROFILER_HSA_LIBRARY |
|
||||
ROCPROFILER_HIP_LIBRARY |
|
||||
ROCPROFILER_MARKER_LIBRARY,
|
||||
static_cast<void*>(&cb_data)),
|
||||
"function should return invalid argument if ROCPROFILER_LIBRARY included",
|
||||
ROCPROFILER_STATUS_ERROR_INVALID_ARGUMENT);
|
||||
|
||||
using init_list_t = std::initializer_list<int>;
|
||||
for(auto itr : init_list_t{
|
||||
(ROCPROFILER_HSA_LIBRARY | ROCPROFILER_HIP_LIBRARY | ROCPROFILER_MARKER_LIBRARY),
|
||||
(ROCPROFILER_HSA_LIBRARY | ROCPROFILER_HIP_LIBRARY),
|
||||
(ROCPROFILER_HIP_LIBRARY),
|
||||
(ROCPROFILER_HSA_LIBRARY | ROCPROFILER_MARKER_LIBRARY),
|
||||
(ROCPROFILER_MARKER_LIBRARY)})
|
||||
{
|
||||
ROCPROFILER_CALL_EXPECT(
|
||||
rocprofiler_at_runtime_api_registration(
|
||||
api_registration_callback, itr, static_cast<void*>(&cb_data)),
|
||||
"test should be updated if new (non-HSA) intercept table is supported",
|
||||
ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED);
|
||||
}
|
||||
|
||||
ROCPROFILER_CALL_EXPECT(rocprofiler_at_runtime_api_registration(
|
||||
api_registration_callback,
|
||||
ROCPROFILER_HSA_LIBRARY | ROCPROFILER_HIP_LIBRARY,
|
||||
static_cast<void*>(&cb_data)),
|
||||
"test should be updated if HIP intercept table is supported",
|
||||
ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED);
|
||||
|
||||
ROCPROFILER_CALL_EXPECT(
|
||||
rocprofiler_at_runtime_api_registration(
|
||||
api_registration_callback, ROCPROFILER_HIP_LIBRARY, static_cast<void*>(&cb_data)),
|
||||
"test should be updated if HIP intercept table is supported",
|
||||
ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED);
|
||||
|
||||
ROCPROFILER_CALL_EXPECT(rocprofiler_at_runtime_api_registration(
|
||||
api_registration_callback,
|
||||
ROCPROFILER_HSA_LIBRARY | ROCPROFILER_MARKER_LIBRARY,
|
||||
static_cast<void*>(&cb_data)),
|
||||
"test should be updated if ROCTx intercept table is supported",
|
||||
ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED);
|
||||
|
||||
ROCPROFILER_CALL(
|
||||
rocprofiler_at_runtime_api_registration(
|
||||
api_registration_callback, ROCPROFILER_HSA_LIBRARY, static_cast<void*>(&cb_data)),
|
||||
"HSA API intercept table registration failed");
|
||||
|
||||
return &cfg_result;
|
||||
};
|
||||
|
||||
EXPECT_EQ(rocprofiler_force_configure(rocp_init), ROCPROFILER_STATUS_SUCCESS);
|
||||
|
||||
hsa_iterate_agents_cb_t agent_cb = [](hsa_agent_t agent, void* data) {
|
||||
auto* _data = static_cast<std::pair<agent_data*, callback_data*>*>(data);
|
||||
_data->first->agent_count++;
|
||||
|
||||
if(int _is_active = 0;
|
||||
rocprofiler_context_is_active(_data->second->client_ctx, &_is_active) ==
|
||||
ROCPROFILER_STATUS_SUCCESS &&
|
||||
_is_active != 0)
|
||||
{
|
||||
ROCPROFILER_CALL(rocprofiler_stop_context(_data->second->client_ctx),
|
||||
"rocprofiler context stop failed");
|
||||
}
|
||||
|
||||
auto status = HSA_STATUS_SUCCESS;
|
||||
auto agent_type = hsa_device_type_t{};
|
||||
if((status = hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &agent_type)) ==
|
||||
HSA_STATUS_SUCCESS)
|
||||
_data->first->agents.emplace_back(agent_type);
|
||||
|
||||
return status;
|
||||
};
|
||||
|
||||
hsa_init();
|
||||
auto _agent_data = agent_data{};
|
||||
auto _pair_data = std::make_pair(&_agent_data, &cb_data);
|
||||
hsa_status_t itr_status = hsa_iterate_agents(agent_cb, static_cast<void*>(&_pair_data));
|
||||
hsa_shut_down();
|
||||
|
||||
EXPECT_EQ(itr_status, HSA_STATUS_SUCCESS);
|
||||
EXPECT_GT(_agent_data.agent_count, 0);
|
||||
EXPECT_EQ(_agent_data.agent_count, _agent_data.agents.size());
|
||||
|
||||
ASSERT_NE(cb_data.client_id, nullptr);
|
||||
ASSERT_NE(cb_data.client_fini_func, nullptr);
|
||||
|
||||
cb_data.client_fini_func(*cb_data.client_id);
|
||||
|
||||
EXPECT_EQ(cb_data.client_workflow_count, 3);
|
||||
|
||||
auto get_tool_count = [](std::string_view func_name) {
|
||||
// we already checked that first == second so we can just check first here
|
||||
return cb_data.client_callback_count.at(func_name).first;
|
||||
};
|
||||
|
||||
auto get_rocp_count = [](std::string_view func_name) {
|
||||
// we already checked that first == second so we can just check first here
|
||||
return cb_data.client_callback_count.at(func_name).second;
|
||||
};
|
||||
|
||||
EXPECT_EQ(get_tool_count("hsa_init"), 0);
|
||||
EXPECT_EQ(get_tool_count("hsa_iterate_agents"), 2);
|
||||
EXPECT_EQ(get_tool_count("hsa_agent_get_info"), 2 * _agent_data.agent_count);
|
||||
EXPECT_EQ(get_tool_count("hsa_shut_down"), 2);
|
||||
|
||||
EXPECT_EQ(get_rocp_count("hsa_init"), 0);
|
||||
EXPECT_EQ(get_rocp_count("hsa_iterate_agents"), 2)
|
||||
<< "if equal to 1, then ENTER phase was invoked but EXIT phase was not (incorrect)";
|
||||
EXPECT_EQ(get_rocp_count("hsa_agent_get_info"), 0);
|
||||
EXPECT_EQ(get_rocp_count("hsa_shut_down"), 0);
|
||||
}
|
||||
@@ -337,14 +337,14 @@ tool_tracing_buffered(rocprofiler_context_id_t context,
|
||||
}
|
||||
|
||||
void
|
||||
thread_precreate(rocprofiler_internal_thread_library_t /*lib*/, void* tool_data)
|
||||
thread_precreate(rocprofiler_runtime_library_t /*lib*/, void* tool_data)
|
||||
{
|
||||
auto* cb_data = static_cast<callback_data*>(tool_data);
|
||||
cb_data->client_workflow_count++;
|
||||
}
|
||||
|
||||
void
|
||||
thread_postcreate(rocprofiler_internal_thread_library_t /*lib*/, void* tool_data)
|
||||
thread_postcreate(rocprofiler_runtime_library_t /*lib*/, void* tool_data)
|
||||
{
|
||||
auto* cb_data = static_cast<callback_data*>(tool_data);
|
||||
cb_data->client_workflow_count++;
|
||||
|
||||
Ссылка в новой задаче
Block a user