[1/N] Agent Counter Collection Implementation (#832)

Added public API call to setup agent counter collection on a context.

Refactored the return types internally for dispatch counter collection
to use rocprofiler_status_t (allow for more verbose failures to be
surfaced via the API)

Subsequent commits will fill out the sampling functionality for agent
counter collection.

Co-authored-by: Benjamin Welton <ben@amd.com>
This commit is contained in:
Benjamin Welton
2024-05-01 13:34:54 -07:00
committato da GitHub
parent e21609c80e
commit cb3fc070c7
14 ha cambiato i file con 192 aggiunte e 53 eliminazioni
@@ -27,6 +27,7 @@
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/rocprofiler.h>
#include "lib/rocprofiler-sdk/buffer.hpp"
#include "lib/rocprofiler-sdk/context/context.hpp"
namespace rocprofiler
@@ -62,11 +63,42 @@ CounterController::destroy_profile(uint64_t id)
_configs.wlock([&](auto& data) { data.erase(id); });
}
rocprofiler_status_t
CounterController::configure_agent_collection(rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer,
rocprofiler_profile_config_id_t config_id)
{
auto* ctx_p = rocprofiler::context::get_mutable_registered_context(context_id);
if(!ctx_p) return ROCPROFILER_STATUS_ERROR_CONTEXT_INVALID;
auto& ctx = *ctx_p;
if(ctx.counter_collection) return ROCPROFILER_STATUS_ERROR_AGENT_DISPATCH_CONFLICT;
if(!rocprofiler::buffer::get_buffer(buffer.handle))
{
return ROCPROFILER_STATUS_ERROR_BUFFER_NOT_FOUND;
}
auto cfg = get_profile_cfg(config_id);
if(!cfg) return ROCPROFILER_STATUS_ERROR_PROFILE_NOT_FOUND;
if(!ctx.agent_counter_collection)
{
ctx.agent_counter_collection =
std::make_unique<rocprofiler::context::agent_counter_collection_service>();
}
ctx.agent_counter_collection->profile = cfg;
ctx.agent_counter_collection->buffer = buffer;
return ROCPROFILER_STATUS_SUCCESS;
}
// Setup the counter collection service. counter_callback_info is created here
// to contain the counters that need to be collected (specified in profile_id) and
// the AQL packet generator for injecting packets. Note: the service is created
// in the stop state.
bool
rocprofiler_status_t
CounterController::configure_dispatch(
rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer,
@@ -76,10 +108,12 @@ CounterController::configure_dispatch(
void* record_callback_args)
{
auto* ctx_p = rocprofiler::context::get_mutable_registered_context(context_id);
if(!ctx_p) return false;
if(!ctx_p) return ROCPROFILER_STATUS_ERROR_CONTEXT_INVALID;
auto& ctx = *ctx_p;
if(ctx.agent_counter_collection) return ROCPROFILER_STATUS_ERROR_AGENT_DISPATCH_CONFLICT;
if(!ctx.counter_collection)
{
ctx.counter_collection =
@@ -100,7 +134,7 @@ CounterController::configure_dispatch(
cb.record_callback = record_callback;
cb.record_callback_args = record_callback_args;
return true;
return ROCPROFILER_STATUS_SUCCESS;
}
std::shared_ptr<profile_config>
@@ -78,14 +78,19 @@ public:
// to contain the counters that need to be collected (specified in profile_id) and
// the AQL packet generator for injecting packets. Note: the service is created
// in the stop state.
static bool configure_dispatch(rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer,
rocprofiler_profile_counting_dispatch_callback_t callback,
void* callback_args,
rocprofiler_profile_counting_record_callback_t record_callback,
void* record_callback_args);
static rocprofiler_status_t configure_dispatch(
rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer,
rocprofiler_profile_counting_dispatch_callback_t callback,
void* callback_args,
rocprofiler_profile_counting_record_callback_t record_callback,
void* record_callback_args);
std::shared_ptr<profile_config> get_profile_cfg(rocprofiler_profile_config_id_t id);
rocprofiler_status_t configure_agent_collection(rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer,
rocprofiler_profile_config_id_t config_id);
private:
rocprofiler::common::Synchronized<std::unordered_map<uint64_t, std::shared_ptr<profile_config>>>
_configs;
@@ -206,7 +206,15 @@ stop_context(const context::context* ctx)
if(controller) controller->disable_serialization();
}
bool
rocprofiler_status_t
configure_agent_collection(rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer_id,
rocprofiler_profile_config_id_t config_id)
{
return get_controller().configure_agent_collection(context_id, buffer_id, config_id);
}
rocprofiler_status_t
configure_buffered_dispatch(rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer,
rocprofiler_profile_counting_dispatch_callback_t callback,
@@ -217,7 +225,7 @@ configure_buffered_dispatch(rocprofiler_context_id_t con
context_id, buffer, callback, callback_args, nullptr, nullptr);
}
bool
rocprofiler_status_t
configure_callback_dispatch(rocprofiler_context_id_t context_id,
rocprofiler_profile_counting_dispatch_callback_t callback,
void* callback_data_args,
@@ -80,19 +80,24 @@ create_counter_profile(std::shared_ptr<rocprofiler::counters::profile_config>&&
void
destroy_counter_profile(uint64_t id);
bool
rocprofiler_status_t
configure_buffered_dispatch(rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer,
rocprofiler_profile_counting_dispatch_callback_t callback,
void* callback_args);
bool
rocprofiler_status_t
configure_callback_dispatch(rocprofiler_context_id_t context_id,
rocprofiler_profile_counting_dispatch_callback_t callback,
void* callback_data_args,
rocprofiler_profile_counting_record_callback_t record_callback,
void* record_callback_args);
rocprofiler_status_t
configure_agent_collection(rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer_id,
rocprofiler_profile_config_id_t config_id);
void
start_context(const context::context*);
@@ -758,3 +758,54 @@ TEST(core, public_api_iterate_agents)
EXPECT_TRUE(from_api.empty());
}
}
TEST(core, init_agent_collection)
{
ASSERT_EQ(hsa_init(), HSA_STATUS_SUCCESS);
registration::init_logging();
registration::set_init_status(-1);
context::push_client(1);
ROCPROFILER_CALL(rocprofiler_create_context(&get_client_ctx()), "context creation failed");
auto agents = hsa::get_queue_controller()->get_supported_agents();
rocprofiler_buffer_id_t opt_buff_id = {.handle = 0};
ROCPROFILER_CALL(rocprofiler_create_buffer(get_client_ctx(),
500 * sizeof(size_t),
500 * sizeof(size_t),
ROCPROFILER_BUFFER_POLICY_LOSSLESS,
null_buffered_callback,
nullptr,
&opt_buff_id),
"Could not create buffer");
for(const auto& [_, agent] : agents)
{
auto metrics = findDeviceMetrics(agent, {});
ASSERT_FALSE(metrics.empty());
ASSERT_TRUE(agent.get_rocp_agent());
for(auto& metric : metrics)
{
expected_dispatch expected = {};
rocprofiler_counter_id_t id = {.handle = metric.id()};
ROCPROFILER_CALL(
rocprofiler_create_profile_config(agent.get_rocp_agent()->id, &id, 1, &expected.id),
"Unable to create profile");
ROCPROFILER_CALL(rocprofiler_configure_agent_profile_counting_service(
get_client_ctx(), opt_buff_id, expected.id),
"Could not create agent collection");
{
auto cfg = counters::get_profile_config(expected.id);
auto* ctx = rocprofiler::context::get_mutable_registered_context(get_client_ctx());
ASSERT_TRUE(ctx);
ASSERT_TRUE(ctx->agent_counter_collection);
EXPECT_EQ(ctx->agent_counter_collection->profile, cfg);
EXPECT_EQ(ctx->agent_counter_collection->buffer.handle, opt_buff_id.handle);
}
ROCPROFILER_CALL(rocprofiler_destroy_profile_config(expected.id),
"Could not delete profile id");
}
}
rocprofiler_destroy_buffer(opt_buff_id);
registration::set_init_status(1);
context::pop_client(1);
}