Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ typedef struct ROCPROFILER_SDK_EXPERIMENTAL rocprofiler_counter_info_v1_t
const char* expression; ///< Counter expression (derived counters only)
uint8_t is_constant : 1; ///< If this counter is HW constant
uint8_t is_derived : 1; ///< If this counter is a derived counter
uint8_t spm_support : 1; ///< If this counter supports SPM

uint64_t dimensions_count;
const rocprofiler_counter_record_dimension_info_t** dimensions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ add_subdirectory(aql)
add_subdirectory(pc_sampling)
add_subdirectory(marker)
add_subdirectory(thread_trace)
add_subdirectory(spm)
add_subdirectory(tracing)
add_subdirectory(kernel_dispatch)
add_subdirectory(kfd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ rocprofiler_query_counter_info(rocprofiler_counter_id_t counter_id,
return true;
};

auto spm_info = [&](auto& out_struct) {
if(const auto* metric_ptr = common::get_val(id_map, static_cast<uint64_t>(base_metric_id)))
out_struct.spm_support = isSupportSpm(*metric_ptr);
return false;
};
switch(version)
{
case ROCPROFILER_COUNTER_INFO_VERSION_0:
Expand All @@ -313,6 +318,7 @@ rocprofiler_query_counter_info(rocprofiler_counter_id_t counter_id,

if(!dim_info(_out_struct, agent_id)) return ROCPROFILER_STATUS_ERROR_DIM_NOT_FOUND;
if(!dim_permutations(_out_struct)) return ROCPROFILER_STATUS_ERROR_DIM_NOT_FOUND;
spm_info(_out_struct);

return ROCPROFILER_STATUS_SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "lib/common/synchronized.hpp"
#include "lib/common/utility.hpp"
#include "lib/rocprofiler-sdk/agent.hpp"
#include "lib/rocprofiler-sdk/aql/helpers.hpp"
#include "lib/rocprofiler-sdk/spm/dlsym.hpp"

#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/cxx/details/tokenize.hpp>
Expand Down Expand Up @@ -423,15 +425,16 @@ operator==(Metric const& lhs, Metric const& rhs)
};
return get_tie(lhs) == get_tie(rhs);
}
Metric::Metric(const std::string&, // Get rid of this...
Metric::Metric(const std::string& arch,
std::string name,
std::string block,
std::string event,
std::string dsc,
std::string expr,
std::string constant,
uint64_t id)
: name_(std::move(name))
: arch_(std::move(arch))
, name_(std::move(name))
, block_(std::move(block))
, event_(std::move(event))
, description_(std::move(dsc))
Expand All @@ -453,5 +456,26 @@ Metric::Metric(const std::string&, // Get rid of this...
}
}
}

bool
isSupportSpm(const Metric& metric)
{
auto agents = rocprofiler::agent::get_agents();

const auto it = std::find_if(agents.begin(), agents.end(), [&](const auto* agent) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const auto it = std::find_if(agents.begin(), agents.end(), [&](const auto* agent) {
const auto itr = std::find_if(agents.begin(), agents.end(), [&](const auto* agent) {

Use itr for consistency with other code.

return std::string_view(agent->name) == std::string_view(metric.arch());
});
if(it == agents.end()) return false;
if(metric.event().empty()) return false;
auto sym = rocprofiler::spm::Dlsym{};
if(!sym.valid()) return false;
auto aql_agent = *CHECK_NOTNULL(rocprofiler::agent::get_aql_agent((*it)->id));
auto query_info = rocprofiler::aql::get_query_info((*it)->id, metric);
auto pmc_event = aqlprofile_pmc_event_t{};
pmc_event.block_name = static_cast<hsa_ven_amd_aqlprofile_block_name_t>(query_info.id);
pmc_event.event_id = static_cast<uint32_t>(std::stoul(metric.event().c_str(), nullptr));
return sym.is_supported_fn(aql_agent, pmc_event);
}

} // namespace counters
} // namespace rocprofiler
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ class Metric
{
public:
Metric() = default;
Metric(const std::string&, // Get rid of this...
Metric(const std::string& arch,
std::string name,
std::string block,
std::string event,
std::string dsc,
std::string expr,
std::string constant,
uint64_t id);


const std::string& arch() const { return arch_; }
const std::string& name() const { return name_; }
const std::string& block() const { return block_; }
const std::string& event() const { return event_; }
Expand All @@ -71,7 +72,8 @@ class Metric
friend bool operator<(Metric const& lhs, Metric const& rhs);
friend bool operator==(Metric const& lhs, Metric const& rhs);

private:
private:
std::string arch_ = {};
std::string name_ = {};
std::string block_ = {};
std::string event_ = {};
Expand Down Expand Up @@ -129,6 +131,9 @@ checkValidMetric(const std::string& agent, const Metric& metric);
*/
rocprofiler_status_t
setCustomCounterDefinition(const CustomCounterDefinition& def);

bool
isSupportSpm(const Metric& metric);
} // namespace counters
} // namespace rocprofiler

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ TEST(metrics, check_public_api_query)
EXPECT_EQ(std::string(info.description ? info.description : ""), metric.description());

// Dimensions are now verified through the API call above
EXPECT_EQ(info.spm_support, isSupportSpm(metric));

for(size_t i = 0; i < info.dimensions_count; i++)
{
EXPECT_GT(info.dimensions[i]->instance_size, 0u);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(ROCPROFILER_LIB_SPM_SOURCES dlsym.cpp)
set(ROCPROFILER_LIB_SPM_HEADERS dlsym.hpp)
target_sources(rocprofiler-sdk-object-library PRIVATE ${ROCPROFILER_LIB_SPM_SOURCES}
${ROCPROFILER_LIB_SPM_HEADERS})

64 changes: 64 additions & 0 deletions projects/rocprofiler-sdk/source/lib/rocprofiler-sdk/spm/dlsym.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// MIT License
//
// Copyright (c) 2023-2025 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/spm/dlsym.hpp"
#include "lib/common/logging.hpp"

#include <fmt/format.h>

#include <dlfcn.h>
#include <atomic>
#include <cstdint>
#include <memory>
#include <mutex>
#include <vector>

namespace rocprofiler
{
namespace spm
{
Dlsym::Dlsym()
{
handle = dlopen("libhsa-amd-aqlprofile64.so", RTLD_NOLOAD | RTLD_LAZY);
if(!handle) handle = dlopen("libhsa-amd-aqlprofile64.so.1", RTLD_NOLOAD | RTLD_LAZY);

if(!handle)
{
ROCP_CI_LOG(WARNING) << fmt::format("aqlprofile cannot be opened");
return;
}

create_packets_fn = (CreateFn*) dlsym(handle, "aqlprofile_spm_create_packets");
delete_packets_fn = (DeleteFn*) dlsym(handle, "aqlprofile_spm_delete_packets");
spm_start_fn = (StartFn*) dlsym(handle, "aqlprofile_spm_start");
spm_stop_fn = (StopFn*) dlsym(handle, "aqlprofile_spm_stop");
spm_decode_fn = (DecodeFn*) dlsym(handle, "aqlprofile_spm_decode_stream_v1");
spm_query_fn = (QueryFn*) dlsym(handle, "aqlprofile_spm_decode_query");
is_supported_fn = (SupportFn*) dlsym(handle, "aqlprofile_spm_is_event_supported");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the library is expected to be loaded, you can just dlsym(RTLD_DEFAULT, ...)


Dlsym::~Dlsym()
{
if(handle) dlclose(handle);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are using RTLD_NOLOAD, you should not dlclose

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are using RTLD_NOLOAD, you should not dlclose

}
} // namespace spm
} // namespace rocprofiler
65 changes: 65 additions & 0 deletions projects/rocprofiler-sdk/source/lib/rocprofiler-sdk/spm/dlsym.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// MIT License
//
// Copyright (c) 2023-2025 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 "lib/rocprofiler-sdk/aql/aql_profile_v2.h"

namespace rocprofiler
{
namespace spm
{
/** @brief Wrapper to aqlprofile functions for SPM
*/
class Dlsym
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename this class to interface or something. In #2706, we will probably just have this point to internal functions (no dlsym).

{
public:
using CreateFn = decltype(aqlprofile_spm_create_packets);
using DeleteFn = decltype(aqlprofile_spm_delete_packets);
using StartFn = decltype(aqlprofile_spm_start);
using StopFn = decltype(aqlprofile_spm_stop);
using DecodeFn = decltype(aqlprofile_spm_decode_stream_v1);
using QueryFn = decltype(aqlprofile_spm_decode_query);
using SupportFn = decltype(aqlprofile_spm_is_event_supported);

Dlsym();
~Dlsym();

bool valid() const
{
return create_packets_fn != nullptr && delete_packets_fn != nullptr &&
spm_start_fn != nullptr && spm_stop_fn != nullptr && spm_decode_fn != nullptr &&
spm_query_fn != nullptr && is_supported_fn != nullptr && handle != nullptr;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move implementation to .cpp file


CreateFn* create_packets_fn = nullptr;
DeleteFn* delete_packets_fn = nullptr;
StartFn* spm_start_fn = nullptr;
StopFn* spm_stop_fn = nullptr;
DecodeFn* spm_decode_fn = nullptr;
QueryFn* spm_query_fn = nullptr;
SupportFn* is_supported_fn = nullptr;
void* handle = nullptr;
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class Dlsym
{
public:
using CreateFn = decltype(aqlprofile_spm_create_packets);
using DeleteFn = decltype(aqlprofile_spm_delete_packets);
using StartFn = decltype(aqlprofile_spm_start);
using StopFn = decltype(aqlprofile_spm_stop);
using DecodeFn = decltype(aqlprofile_spm_decode_stream_v1);
using QueryFn = decltype(aqlprofile_spm_decode_query);
using SupportFn = decltype(aqlprofile_spm_is_event_supported);
Dlsym();
~Dlsym();
bool valid() const
{
return create_packets_fn != nullptr && delete_packets_fn != nullptr &&
spm_start_fn != nullptr && spm_stop_fn != nullptr && spm_decode_fn != nullptr &&
spm_query_fn != nullptr && is_supported_fn != nullptr && handle != nullptr;
}
CreateFn* create_packets_fn = nullptr;
DeleteFn* delete_packets_fn = nullptr;
StartFn* spm_start_fn = nullptr;
StopFn* spm_stop_fn = nullptr;
DecodeFn* spm_decode_fn = nullptr;
QueryFn* spm_query_fn = nullptr;
SupportFn* is_supported_fn = nullptr;
void* handle = nullptr;
};
struct spm_interface
{
using spm_create_packets_fn_t = decltype(aqlprofile_spm_create_packets);
// change names below similar to above
using DeleteFn = decltype(aqlprofile_spm_delete_packets);
using StartFn = decltype(aqlprofile_spm_start);
using StopFn = decltype(aqlprofile_spm_stop);
using DecodeFn = decltype(aqlprofile_spm_decode_stream_v1);
using QueryFn = decltype(aqlprofile_spm_decode_query);
using SupportFn = decltype(aqlprofile_spm_is_event_supported);
spm_create_packets_fn_t* spm_create_packets = nullptr;
DeleteFn* spm_delete_packets = nullptr;
StartFn* spm_start = nullptr;
StopFn* spm_stop = nullptr;
DecodeFn* spm_decode_stream_v1 = nullptr;
QueryFn* spm_decode_query = nullptr;
SupportFn* spm_is_event_supported = nullptr;
};
std::optional<spm_interface>
construct_spm_interface(void* handle = nullptr);

} // namespace spm
} // namespace rocprofiler
Loading