2023-09-22 13:51:21 -05:00
|
|
|
// MIT License
|
|
|
|
|
//
|
2025-01-23 06:41:20 +05:30
|
|
|
// Copyright (c) 2023-2025 Advanced Micro Devices, Inc. All rights reserved.
|
2023-09-22 13:51:21 -05:00
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
|
2023-11-29 20:43:18 -06:00
|
|
|
#include <rocprofiler-sdk/agent.h>
|
|
|
|
|
#include <rocprofiler-sdk/fwd.h>
|
|
|
|
|
#include <rocprofiler-sdk/rocprofiler.h>
|
2023-09-22 13:51:21 -05:00
|
|
|
|
2023-11-28 10:04:37 -06:00
|
|
|
#include "lib/common/filesystem.hpp"
|
2024-05-20 15:38:18 -05:00
|
|
|
#include "lib/common/logging.hpp"
|
2023-12-19 13:47:21 -06:00
|
|
|
#include "lib/common/scope_destructor.hpp"
|
|
|
|
|
#include "lib/common/static_object.hpp"
|
2024-07-17 01:07:49 -05:00
|
|
|
#include "lib/common/string_entry.hpp"
|
2024-03-21 20:04:21 -05:00
|
|
|
#include "lib/common/utility.hpp"
|
2023-11-29 20:43:18 -06:00
|
|
|
#include "lib/rocprofiler-sdk/agent.hpp"
|
|
|
|
|
#include "lib/rocprofiler-sdk/hsa/agent_cache.hpp"
|
2023-10-19 19:04:02 -05:00
|
|
|
|
2024-03-21 20:04:21 -05:00
|
|
|
#include <fmt/core.h>
|
2024-01-18 09:48:06 -06:00
|
|
|
#include <fmt/format.h>
|
2024-03-21 20:04:21 -05:00
|
|
|
#include <fmt/ranges.h>
|
|
|
|
|
#include <hsa/hsa.h>
|
2023-10-19 19:04:02 -05:00
|
|
|
#include <hsa/hsa_api_trace.h>
|
2023-10-10 18:10:23 -05:00
|
|
|
#include <libdrm/amdgpu.h>
|
|
|
|
|
#include <xf86drm.h>
|
2023-09-22 13:51:21 -05:00
|
|
|
|
2023-10-10 18:10:23 -05:00
|
|
|
#include <fstream>
|
|
|
|
|
#include <limits>
|
2024-03-21 20:04:21 -05:00
|
|
|
#include <random>
|
2023-10-10 18:10:23 -05:00
|
|
|
#include <regex>
|
2024-03-21 20:04:21 -05:00
|
|
|
#include <set>
|
2023-12-19 13:47:21 -06:00
|
|
|
#include <shared_mutex>
|
2023-10-10 18:10:23 -05:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <unordered_map>
|
2023-09-22 13:51:21 -05:00
|
|
|
#include <vector>
|
|
|
|
|
|
2023-10-10 18:10:23 -05:00
|
|
|
namespace rocprofiler
|
|
|
|
|
{
|
|
|
|
|
namespace agent
|
|
|
|
|
{
|
|
|
|
|
namespace
|
|
|
|
|
{
|
2024-04-11 19:49:49 -05:00
|
|
|
namespace fs = ::rocprofiler::common::filesystem;
|
|
|
|
|
|
2024-03-21 20:04:21 -05:00
|
|
|
uint64_t
|
|
|
|
|
get_agent_offset()
|
|
|
|
|
{
|
|
|
|
|
static uint64_t _v = []() {
|
|
|
|
|
auto gen = std::mt19937{std::random_device{}()};
|
|
|
|
|
auto rng = std::uniform_int_distribution<uint64_t>{std::numeric_limits<uint8_t>::max(),
|
|
|
|
|
std::numeric_limits<uint16_t>::max()};
|
|
|
|
|
return rng(gen);
|
|
|
|
|
}();
|
|
|
|
|
return _v;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-10 18:10:23 -05:00
|
|
|
struct cpu_info
|
|
|
|
|
{
|
|
|
|
|
long processor = -1;
|
|
|
|
|
long family = -1;
|
|
|
|
|
long model = -1;
|
|
|
|
|
long physical_id = -1;
|
|
|
|
|
long core_id = -1;
|
|
|
|
|
long apicid = -1;
|
|
|
|
|
std::string vendor_id = {};
|
|
|
|
|
std::string model_name = {};
|
|
|
|
|
|
|
|
|
|
bool is_valid() const
|
|
|
|
|
{
|
|
|
|
|
return !(processor < 0 || family < 0 || model < 0 || physical_id < 0 || core_id < 0 ||
|
|
|
|
|
apicid < 0 || vendor_id.empty() || model_name.empty());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto
|
|
|
|
|
parse_cpu_info()
|
|
|
|
|
{
|
|
|
|
|
auto ifs = std::ifstream{"/proc/cpuinfo"};
|
|
|
|
|
auto data = std::vector<cpu_info>{};
|
|
|
|
|
if(!ifs) return data;
|
|
|
|
|
|
|
|
|
|
auto read_blocks = [&ifs]() {
|
|
|
|
|
auto blocks = std::vector<std::vector<std::string>>{};
|
|
|
|
|
auto current_block = std::vector<std::string>{};
|
|
|
|
|
auto line = std::string{};
|
|
|
|
|
while(std::getline(ifs, line))
|
|
|
|
|
{
|
|
|
|
|
if(ifs.eof())
|
|
|
|
|
{
|
|
|
|
|
if(!current_block.empty()) blocks.emplace_back(std::move(current_block));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(line.empty())
|
|
|
|
|
{
|
|
|
|
|
if(!current_block.empty()) blocks.emplace_back(std::move(current_block));
|
|
|
|
|
current_block.clear();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
current_block.emplace_back(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return blocks;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto processor_blocks = read_blocks();
|
|
|
|
|
auto processor_info = std::vector<cpu_info>{};
|
|
|
|
|
processor_info.reserve(processor_blocks.size());
|
|
|
|
|
|
|
|
|
|
for(const auto& bitr : processor_blocks)
|
|
|
|
|
{
|
|
|
|
|
auto info_v = cpu_info{};
|
|
|
|
|
for(const auto& itr : bitr)
|
|
|
|
|
{
|
|
|
|
|
auto match = std::smatch{};
|
|
|
|
|
const std::regex re{".*: (.*)$"};
|
|
|
|
|
if(std::regex_match(itr, match, re))
|
|
|
|
|
{
|
|
|
|
|
if(match.size() == 2)
|
|
|
|
|
{
|
|
|
|
|
std::ssub_match value = match[1];
|
|
|
|
|
|
|
|
|
|
if(itr.find("vendor_id") == 0)
|
|
|
|
|
info_v.vendor_id = value.str();
|
|
|
|
|
else if(itr.find("model name") == 0)
|
|
|
|
|
info_v.model_name = value.str();
|
|
|
|
|
else if(itr.find("processor") == 0)
|
|
|
|
|
info_v.processor = std::stol(value.str());
|
|
|
|
|
else if(itr.find("cpu family") == 0)
|
|
|
|
|
info_v.family = std::stol(value.str());
|
|
|
|
|
else if(itr.find("model") == 0 && itr.find("model name") != 0)
|
|
|
|
|
info_v.model = std::stol(value.str());
|
|
|
|
|
else if(itr.find("physical id") == 0)
|
|
|
|
|
info_v.physical_id = std::stol(value.str());
|
|
|
|
|
else if(itr.find("core id") == 0)
|
|
|
|
|
info_v.core_id = std::stol(value.str());
|
|
|
|
|
else if(itr.find("apicid") == 0)
|
|
|
|
|
info_v.apicid = std::stol(value.str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(info_v.is_valid())
|
|
|
|
|
processor_info.emplace_back(info_v);
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-04-02 17:15:30 -07:00
|
|
|
ROCP_ERROR << "Invalid processor info: "
|
2023-10-10 18:10:23 -05:00
|
|
|
<< fmt::format("processor={}, vendor={}, family={}, model={}, name={}, "
|
|
|
|
|
"physical id={}, core id={}, apicid={}",
|
|
|
|
|
info_v.processor,
|
|
|
|
|
info_v.vendor_id,
|
|
|
|
|
info_v.family,
|
|
|
|
|
info_v.model,
|
|
|
|
|
info_v.model_name,
|
|
|
|
|
info_v.physical_id,
|
|
|
|
|
info_v.core_id,
|
|
|
|
|
info_v.apicid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return processor_info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto&
|
|
|
|
|
get_cpu_info()
|
|
|
|
|
{
|
|
|
|
|
static auto _v = parse_cpu_info();
|
|
|
|
|
return _v;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 19:04:02 -05:00
|
|
|
// check to see if the file is readable
|
|
|
|
|
bool
|
|
|
|
|
is_readable(const fs::path& fpath)
|
|
|
|
|
{
|
|
|
|
|
auto ec = std::error_code{};
|
|
|
|
|
auto perms = fs::status(fpath, ec).permissions();
|
2024-05-20 15:38:18 -05:00
|
|
|
ROCP_ERROR_IF(ec) << fmt::format(
|
2023-10-19 19:04:02 -05:00
|
|
|
"Error getting status for file '{}': {}", fpath.string(), ec.message());
|
|
|
|
|
return (!ec && (perms & fs::perms::owner_read) != fs::perms::none);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-10 18:10:23 -05:00
|
|
|
auto
|
|
|
|
|
read_file(const std::string& fname)
|
|
|
|
|
{
|
|
|
|
|
auto data = std::vector<std::string>{};
|
2023-10-19 19:04:02 -05:00
|
|
|
|
|
|
|
|
if(!is_readable(fs::path{fname}))
|
|
|
|
|
throw std::runtime_error{fmt::format("file '{}' cannot be read", fname)};
|
|
|
|
|
|
|
|
|
|
auto ifs = std::ifstream{fname};
|
|
|
|
|
if(!ifs || !ifs.good())
|
|
|
|
|
throw std::runtime_error{fmt::format("file '{}' cannot be read", fname)};
|
2023-10-10 18:10:23 -05:00
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
|
{
|
|
|
|
|
auto value = std::string{};
|
|
|
|
|
ifs >> value;
|
2023-10-19 19:04:02 -05:00
|
|
|
if(ifs.eof() || value.empty()) break;
|
2023-10-10 18:10:23 -05:00
|
|
|
|
2023-10-19 19:04:02 -05:00
|
|
|
data.emplace_back(value);
|
2023-10-10 18:10:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto
|
|
|
|
|
read_map(const std::string& fname)
|
|
|
|
|
{
|
|
|
|
|
auto data = std::unordered_map<std::string, std::string>{};
|
|
|
|
|
|
2023-10-19 19:04:02 -05:00
|
|
|
if(!is_readable(fs::path{fname}))
|
|
|
|
|
throw std::runtime_error{fmt::format("file '{}' cannot be read", fname)};
|
|
|
|
|
|
|
|
|
|
auto ifs = std::ifstream{fname};
|
|
|
|
|
if(!ifs || !ifs.good())
|
|
|
|
|
throw std::runtime_error{fmt::format("file '{}' cannot be read", fname)};
|
|
|
|
|
|
|
|
|
|
auto last_label = std::string{};
|
2023-10-10 18:10:23 -05:00
|
|
|
while(true)
|
|
|
|
|
{
|
|
|
|
|
auto label = std::string{};
|
|
|
|
|
ifs >> label;
|
2023-10-19 19:04:02 -05:00
|
|
|
if(ifs.eof() || label.empty()) break;
|
2023-10-10 18:10:23 -05:00
|
|
|
|
|
|
|
|
auto entry = std::string{};
|
|
|
|
|
ifs >> entry;
|
|
|
|
|
if(ifs.eof())
|
|
|
|
|
throw std::runtime_error{
|
|
|
|
|
fmt::format("unexpected file format in '{}' at {}", fname, label)};
|
|
|
|
|
|
|
|
|
|
auto ret = data.emplace(label, entry);
|
|
|
|
|
if(!ret.second)
|
2023-10-19 19:04:02 -05:00
|
|
|
throw std::runtime_error{
|
|
|
|
|
fmt::format("duplicate entry in '{}': '{}' (='{}'). last label was '{}'",
|
|
|
|
|
fname,
|
|
|
|
|
label,
|
|
|
|
|
entry,
|
|
|
|
|
last_label)};
|
|
|
|
|
|
|
|
|
|
if(!label.empty()) last_label = std::move(label);
|
2023-10-10 18:10:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename MapT, typename Tp>
|
|
|
|
|
void
|
|
|
|
|
read_property(const MapT& data, const std::string& label, Tp& value)
|
|
|
|
|
{
|
2023-10-17 00:39:41 -05:00
|
|
|
using mutable_type = std::remove_const_t<Tp>;
|
|
|
|
|
|
2023-11-17 01:49:51 -08:00
|
|
|
get_agent_available_properties().insert(label);
|
2023-10-10 18:10:23 -05:00
|
|
|
if constexpr(std::is_enum<Tp>::value)
|
|
|
|
|
{
|
2023-10-17 00:39:41 -05:00
|
|
|
using value_type = std::underlying_type_t<mutable_type>;
|
2023-10-10 18:10:23 -05:00
|
|
|
// never expect this to be true but it does guard against infinite recursion
|
|
|
|
|
static_assert(!std::is_enum<value_type>::value, "Expected non-enum type");
|
|
|
|
|
|
|
|
|
|
auto value_v = static_cast<value_type>(value);
|
|
|
|
|
read_property(data, label, value_v);
|
2023-10-17 00:39:41 -05:00
|
|
|
if constexpr(std::is_const<Tp>::value)
|
|
|
|
|
const_cast<mutable_type&>(value) = static_cast<mutable_type>(value_v);
|
|
|
|
|
else
|
|
|
|
|
value = static_cast<Tp>(value_v);
|
2023-10-10 18:10:23 -05:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
static_assert(std::is_integral<Tp>::value, "Expected integral type");
|
|
|
|
|
using value_type = std::conditional_t<std::is_signed<Tp>::value, intmax_t, uintmax_t>;
|
|
|
|
|
|
|
|
|
|
if(data.find(label) == data.end())
|
|
|
|
|
{
|
2024-04-02 17:15:30 -07:00
|
|
|
ROCP_ERROR << "agent properties map missing " << label << " entry";
|
2023-10-10 18:10:23 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto iss = std::istringstream{data.at(label)};
|
|
|
|
|
value_type local_value;
|
|
|
|
|
iss >> local_value;
|
|
|
|
|
|
|
|
|
|
// verify that we have used the correct data sizes
|
|
|
|
|
constexpr auto min_value = std::numeric_limits<Tp>::min();
|
|
|
|
|
constexpr auto max_value = std::numeric_limits<Tp>::max();
|
|
|
|
|
if(local_value < min_value)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error{
|
|
|
|
|
fmt::format("data with label {} has a value (={}) which is less "
|
|
|
|
|
"than the min value for the type (={})",
|
|
|
|
|
label,
|
|
|
|
|
local_value,
|
|
|
|
|
min_value)};
|
|
|
|
|
}
|
|
|
|
|
else if(local_value > max_value)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error{fmt::format("data with label {} has a value (={}) which is "
|
|
|
|
|
"greater "
|
|
|
|
|
"than the max value for the type (={})",
|
|
|
|
|
label,
|
|
|
|
|
local_value,
|
|
|
|
|
max_value)};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 00:39:41 -05:00
|
|
|
if constexpr(std::is_const<Tp>::value)
|
|
|
|
|
const_cast<mutable_type&>(value) = static_cast<mutable_type>(local_value);
|
|
|
|
|
else
|
|
|
|
|
value = static_cast<Tp>(local_value);
|
2023-10-10 18:10:23 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 15:06:54 -07:00
|
|
|
using unique_agent_t = std::unique_ptr<rocprofiler_agent_t, void (*)(rocprofiler_agent_t*)>;
|
|
|
|
|
|
2023-10-10 18:10:23 -05:00
|
|
|
auto
|
|
|
|
|
read_topology()
|
|
|
|
|
{
|
|
|
|
|
auto sysfs_nodes_path = fs::path{"/sys/class/kfd/kfd/topology/nodes/"};
|
|
|
|
|
if(!fs::exists(sysfs_nodes_path))
|
|
|
|
|
throw std::runtime_error{
|
|
|
|
|
fmt::format("sysfs nodes path '{}' does not exist", sysfs_nodes_path.string())};
|
|
|
|
|
|
|
|
|
|
const auto& cpu_info_v = get_cpu_info();
|
|
|
|
|
auto data = std::vector<unique_agent_t>{};
|
2023-10-19 19:04:02 -05:00
|
|
|
uint64_t idcount = 0;
|
|
|
|
|
uint64_t nodecount = 0;
|
2024-06-24 23:18:58 -05:00
|
|
|
uint64_t cpucount = 0;
|
|
|
|
|
uint64_t gpucount = 0;
|
|
|
|
|
uint64_t unkcount = 0;
|
2023-10-10 18:10:23 -05:00
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
|
{
|
2024-03-21 20:04:21 -05:00
|
|
|
auto node_id = nodecount++;
|
|
|
|
|
auto node_path = sysfs_nodes_path / std::to_string(node_id);
|
2023-10-19 19:04:02 -05:00
|
|
|
// assumes that nodes are monotonically increasing and thus once we are missing a node
|
|
|
|
|
// folder for a number, there are no more nodes
|
2023-10-10 18:10:23 -05:00
|
|
|
if(!fs::exists(node_path)) break;
|
2023-10-19 19:04:02 -05:00
|
|
|
// skip if we don't have permission to read the file
|
|
|
|
|
if(!is_readable(node_path)) continue;
|
2023-10-10 18:10:23 -05:00
|
|
|
|
|
|
|
|
auto properties = std::unordered_map<std::string, std::string>{};
|
|
|
|
|
auto name_prop = std::vector<std::string>{};
|
|
|
|
|
auto gpu_id_prop = std::vector<std::string>{};
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
properties = read_map(node_path / "properties");
|
|
|
|
|
name_prop = read_file(node_path / "name");
|
|
|
|
|
gpu_id_prop = read_file(node_path / "gpu_id");
|
|
|
|
|
} catch(std::runtime_error& e)
|
|
|
|
|
{
|
2024-04-02 17:15:30 -07:00
|
|
|
ROCP_ERROR << "Error reading '" << (node_path / "properties").string()
|
2023-10-10 18:10:23 -05:00
|
|
|
<< "' :: " << e.what();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 19:04:02 -05:00
|
|
|
// we may have been able to open the properties file but if it was empty, we ignore it
|
|
|
|
|
if(properties.empty()) continue;
|
|
|
|
|
|
2024-06-24 23:18:58 -05:00
|
|
|
auto agent_info = common::init_public_api_struct(rocprofiler_agent_t{});
|
|
|
|
|
agent_info.type = ROCPROFILER_AGENT_TYPE_NONE;
|
|
|
|
|
agent_info.logical_node_id = idcount++;
|
|
|
|
|
agent_info.node_id = node_id;
|
|
|
|
|
agent_info.id.handle = (agent_info.logical_node_id) + get_agent_offset();
|
|
|
|
|
agent_info.logical_node_type_id = -1;
|
2023-10-10 18:10:23 -05:00
|
|
|
|
|
|
|
|
if(!name_prop.empty())
|
2024-01-18 09:48:06 -06:00
|
|
|
agent_info.model_name =
|
2024-07-17 01:07:49 -05:00
|
|
|
common::get_string_entry(fmt::format("{}", fmt::join(name_prop, " ")))->c_str();
|
2023-10-10 18:10:23 -05:00
|
|
|
else
|
|
|
|
|
agent_info.model_name = "";
|
|
|
|
|
|
|
|
|
|
if(!gpu_id_prop.empty()) agent_info.gpu_id = std::stoull(gpu_id_prop.front());
|
|
|
|
|
|
|
|
|
|
read_property(properties, "cpu_cores_count", agent_info.cpu_cores_count);
|
|
|
|
|
read_property(properties, "simd_count", agent_info.simd_count);
|
|
|
|
|
|
|
|
|
|
if(agent_info.cpu_cores_count > 0)
|
|
|
|
|
agent_info.type = ROCPROFILER_AGENT_TYPE_CPU;
|
|
|
|
|
else if(agent_info.simd_count > 0)
|
|
|
|
|
agent_info.type = ROCPROFILER_AGENT_TYPE_GPU;
|
2024-06-24 23:18:58 -05:00
|
|
|
else
|
|
|
|
|
ROCP_WARNING << "agent " << agent_info.node_id << " is neither a CPU nor a GPU";
|
|
|
|
|
|
|
|
|
|
if(agent_info.type == ROCPROFILER_AGENT_TYPE_CPU)
|
|
|
|
|
agent_info.logical_node_type_id = cpucount++;
|
|
|
|
|
else if(agent_info.type == ROCPROFILER_AGENT_TYPE_GPU)
|
|
|
|
|
agent_info.logical_node_type_id = gpucount++;
|
|
|
|
|
else
|
|
|
|
|
agent_info.logical_node_type_id = unkcount++;
|
2023-10-10 18:10:23 -05:00
|
|
|
|
|
|
|
|
read_property(properties, "mem_banks_count", agent_info.mem_banks_count);
|
|
|
|
|
read_property(properties, "caches_count", agent_info.caches_count);
|
|
|
|
|
read_property(properties, "io_links_count", agent_info.io_links_count);
|
|
|
|
|
read_property(properties, "cpu_core_id_base", agent_info.cpu_core_id_base);
|
|
|
|
|
read_property(properties, "simd_id_base", agent_info.simd_id_base);
|
|
|
|
|
read_property(properties, "max_waves_per_simd", agent_info.max_waves_per_simd);
|
|
|
|
|
read_property(properties, "lds_size_in_kb", agent_info.lds_size_in_kb);
|
|
|
|
|
read_property(properties, "gds_size_in_kb", agent_info.gds_size_in_kb);
|
|
|
|
|
read_property(properties, "num_gws", agent_info.num_gws);
|
|
|
|
|
read_property(properties, "wave_front_size", agent_info.wave_front_size);
|
|
|
|
|
read_property(properties, "array_count", agent_info.array_count);
|
|
|
|
|
read_property(properties, "simd_arrays_per_engine", agent_info.simd_arrays_per_engine);
|
|
|
|
|
read_property(properties, "cu_per_simd_array", agent_info.cu_per_simd_array);
|
|
|
|
|
read_property(properties, "simd_per_cu", agent_info.simd_per_cu);
|
|
|
|
|
read_property(properties, "max_slots_scratch_cu", agent_info.max_slots_scratch_cu);
|
|
|
|
|
read_property(properties, "gfx_target_version", agent_info.gfx_target_version);
|
|
|
|
|
read_property(properties, "vendor_id", agent_info.vendor_id);
|
|
|
|
|
read_property(properties, "device_id", agent_info.device_id);
|
|
|
|
|
read_property(properties, "location_id", agent_info.location_id);
|
|
|
|
|
read_property(properties, "domain", agent_info.domain);
|
|
|
|
|
read_property(properties, "drm_render_minor", agent_info.drm_render_minor);
|
|
|
|
|
read_property(properties, "hive_id", agent_info.hive_id);
|
|
|
|
|
read_property(properties, "num_sdma_engines", agent_info.num_sdma_engines);
|
|
|
|
|
read_property(properties, "num_sdma_xgmi_engines", agent_info.num_sdma_xgmi_engines);
|
|
|
|
|
read_property(
|
|
|
|
|
properties, "num_sdma_queues_per_engine", agent_info.num_sdma_queues_per_engine);
|
|
|
|
|
read_property(properties, "num_cp_queues", agent_info.num_cp_queues);
|
|
|
|
|
read_property(properties, "max_engine_clk_ccompute", agent_info.max_engine_clk_ccompute);
|
|
|
|
|
|
|
|
|
|
agent_info.name = "";
|
|
|
|
|
agent_info.product_name = "";
|
|
|
|
|
agent_info.vendor_name = "";
|
|
|
|
|
if(agent_info.type == ROCPROFILER_AGENT_TYPE_GPU)
|
|
|
|
|
{
|
|
|
|
|
constexpr auto workgrp_max = 1024;
|
|
|
|
|
constexpr auto grid_max = std::numeric_limits<uint32_t>::max();
|
|
|
|
|
|
|
|
|
|
read_property(
|
|
|
|
|
properties, "max_engine_clk_fcompute", agent_info.max_engine_clk_fcompute);
|
|
|
|
|
read_property(properties, "local_mem_size", agent_info.local_mem_size);
|
|
|
|
|
read_property(properties, "fw_version", agent_info.fw_version.Value);
|
|
|
|
|
read_property(properties, "capability", agent_info.capability.Value);
|
|
|
|
|
read_property(properties, "sdma_fw_version", agent_info.sdma_fw_version.Value);
|
|
|
|
|
agent_info.fw_version.Value &= 0x3ff;
|
|
|
|
|
agent_info.sdma_fw_version.Value &= 0x3ff;
|
|
|
|
|
agent_info.workgroup_max_size = workgrp_max; // hardcoded in hsa-runtime
|
|
|
|
|
agent_info.workgroup_max_dim = {workgrp_max, workgrp_max, workgrp_max};
|
|
|
|
|
agent_info.grid_max_size = grid_max; // hardcoded in hsa-runtime
|
|
|
|
|
agent_info.grid_max_dim = {grid_max, grid_max, grid_max};
|
|
|
|
|
agent_info.cu_count = agent_info.simd_count / agent_info.simd_per_cu;
|
|
|
|
|
|
|
|
|
|
if(int drm_fd = 0; (drm_fd = drmOpenRender(agent_info.drm_render_minor)) >= 0)
|
|
|
|
|
{
|
|
|
|
|
uint32_t major_version = 0;
|
|
|
|
|
uint32_t minor_version = 0;
|
|
|
|
|
auto* device_handle = amdgpu_device_handle{};
|
|
|
|
|
if(amdgpu_device_initialize(
|
|
|
|
|
drm_fd, &major_version, &minor_version, &device_handle) == 0)
|
|
|
|
|
{
|
|
|
|
|
auto major = (agent_info.gfx_target_version / 10000) % 100;
|
|
|
|
|
auto minor = (agent_info.gfx_target_version / 100) % 100;
|
|
|
|
|
auto step = (agent_info.gfx_target_version % 100);
|
|
|
|
|
|
|
|
|
|
agent_info.name =
|
2024-07-17 01:07:49 -05:00
|
|
|
common::get_string_entry(fmt::format("gfx{}{}{:x}", major, minor, step))
|
|
|
|
|
->c_str();
|
2023-12-19 13:47:21 -06:00
|
|
|
agent_info.product_name =
|
2024-07-17 01:07:49 -05:00
|
|
|
common::get_string_entry(amdgpu_get_marketing_name(device_handle))->c_str();
|
|
|
|
|
agent_info.vendor_name = common::get_string_entry("AMD")->c_str();
|
2023-10-10 18:10:23 -05:00
|
|
|
|
|
|
|
|
amdgpu_gpu_info gpu_info = {};
|
|
|
|
|
if(amdgpu_query_gpu_info(device_handle, &gpu_info) == 0)
|
|
|
|
|
{
|
|
|
|
|
agent_info.family_id = gpu_info.family_id;
|
|
|
|
|
}
|
|
|
|
|
amdgpu_device_deinitialize(device_handle);
|
|
|
|
|
}
|
|
|
|
|
drmClose(drm_fd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(agent_info.type == ROCPROFILER_AGENT_TYPE_CPU)
|
|
|
|
|
{
|
|
|
|
|
agent_info.cu_count = agent_info.cpu_cores_count;
|
2024-07-17 01:07:49 -05:00
|
|
|
agent_info.vendor_name = common::get_string_entry("CPU")->c_str();
|
2023-10-10 18:10:23 -05:00
|
|
|
for(const auto& itr : cpu_info_v)
|
|
|
|
|
{
|
|
|
|
|
if(agent_info.cpu_core_id_base == itr.apicid)
|
|
|
|
|
{
|
2024-07-17 01:07:49 -05:00
|
|
|
agent_info.name = common::get_string_entry(itr.model_name)->c_str();
|
|
|
|
|
agent_info.product_name = common::get_string_entry(agent_info.name)->c_str();
|
2023-10-10 18:10:23 -05:00
|
|
|
agent_info.family_id = itr.family;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(properties.count("num_xcc") > 0)
|
|
|
|
|
read_property(properties, "num_xcc", agent_info.num_xcc);
|
|
|
|
|
else
|
|
|
|
|
agent_info.num_xcc = 1;
|
|
|
|
|
|
|
|
|
|
agent_info.max_waves_per_cu = agent_info.simd_per_cu * agent_info.max_waves_per_simd;
|
|
|
|
|
|
|
|
|
|
if(agent_info.simd_arrays_per_engine > 0)
|
|
|
|
|
{
|
|
|
|
|
agent_info.num_shader_banks =
|
|
|
|
|
agent_info.array_count / agent_info.simd_arrays_per_engine;
|
|
|
|
|
|
|
|
|
|
// depends on above
|
2023-12-06 15:39:43 -08:00
|
|
|
if(agent_info.num_shader_banks > 0)
|
2023-10-10 18:10:23 -05:00
|
|
|
{
|
2023-12-06 15:39:43 -08:00
|
|
|
agent_info.cu_per_engine = (agent_info.simd_count / agent_info.simd_per_cu) /
|
|
|
|
|
(agent_info.num_shader_banks);
|
2023-10-10 18:10:23 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
agent_info.mem_banks = nullptr;
|
|
|
|
|
agent_info.caches = nullptr;
|
|
|
|
|
agent_info.io_links = nullptr;
|
|
|
|
|
|
|
|
|
|
if(agent_info.mem_banks_count > 0)
|
|
|
|
|
{
|
|
|
|
|
agent_info.mem_banks = new rocprofiler_agent_mem_bank_t[agent_info.mem_banks_count];
|
|
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < agent_info.mem_banks_count; ++i)
|
|
|
|
|
{
|
|
|
|
|
auto subproperties =
|
|
|
|
|
read_map(node_path / "mem_banks" / std::to_string(i) / "properties");
|
|
|
|
|
|
2023-10-17 00:39:41 -05:00
|
|
|
read_property(subproperties, "heap_type", agent_info.mem_banks[i].heap_type);
|
2023-10-10 18:10:23 -05:00
|
|
|
read_property(
|
|
|
|
|
subproperties, "size_in_bytes", agent_info.mem_banks[i].size_in_bytes);
|
|
|
|
|
read_property(subproperties, "flags", agent_info.mem_banks[i].flags.MemoryProperty);
|
|
|
|
|
read_property(subproperties, "width", agent_info.mem_banks[i].width);
|
|
|
|
|
read_property(subproperties, "mem_clk_max", agent_info.mem_banks[i].mem_clk_max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(agent_info.caches_count > 0)
|
|
|
|
|
{
|
|
|
|
|
agent_info.caches = new rocprofiler_agent_cache_t[agent_info.caches_count];
|
|
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < agent_info.caches_count; ++i)
|
|
|
|
|
{
|
|
|
|
|
auto subproperties =
|
|
|
|
|
read_map(node_path / "caches" / std::to_string(i) / "properties");
|
|
|
|
|
|
|
|
|
|
read_property(
|
|
|
|
|
subproperties, "processor_id_low", agent_info.caches[i].processor_id_low);
|
|
|
|
|
read_property(subproperties, "level", agent_info.caches[i].level);
|
|
|
|
|
read_property(subproperties, "size", agent_info.caches[i].size);
|
|
|
|
|
read_property(
|
|
|
|
|
subproperties, "cache_line_size", agent_info.caches[i].cache_line_size);
|
|
|
|
|
read_property(
|
|
|
|
|
subproperties, "cache_lines_per_tag", agent_info.caches[i].cache_lines_per_tag);
|
|
|
|
|
read_property(subproperties, "association", agent_info.caches[i].association);
|
|
|
|
|
read_property(subproperties, "latency", agent_info.caches[i].latency);
|
|
|
|
|
read_property(subproperties, "type", agent_info.caches[i].type.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(agent_info.io_links_count > 0)
|
|
|
|
|
{
|
|
|
|
|
agent_info.io_links = new rocprofiler_agent_io_link_t[agent_info.io_links_count];
|
|
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < agent_info.io_links_count; ++i)
|
|
|
|
|
{
|
|
|
|
|
auto subproperties =
|
|
|
|
|
read_map(node_path / "io_links" / std::to_string(i) / "properties");
|
|
|
|
|
|
|
|
|
|
read_property(subproperties, "type", agent_info.io_links[i].type);
|
|
|
|
|
read_property(subproperties, "version_major", agent_info.io_links[i].version_major);
|
|
|
|
|
read_property(subproperties, "version_minor", agent_info.io_links[i].version_minor);
|
|
|
|
|
read_property(subproperties, "node_from", agent_info.io_links[i].node_from);
|
|
|
|
|
read_property(subproperties, "node_to", agent_info.io_links[i].node_to);
|
|
|
|
|
read_property(subproperties, "weight", agent_info.io_links[i].weight);
|
|
|
|
|
read_property(subproperties, "min_latency", agent_info.io_links[i].min_latency);
|
|
|
|
|
read_property(subproperties, "max_latency", agent_info.io_links[i].max_latency);
|
|
|
|
|
read_property(subproperties, "min_bandwidth", agent_info.io_links[i].min_bandwidth);
|
|
|
|
|
read_property(subproperties, "max_bandwidth", agent_info.io_links[i].max_bandwidth);
|
|
|
|
|
read_property(subproperties,
|
|
|
|
|
"recommended_transfer_size",
|
|
|
|
|
agent_info.io_links[i].recommended_transfer_size);
|
|
|
|
|
read_property(subproperties, "flags", agent_info.io_links[i].flags.LinkProperty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.emplace_back(new rocprofiler_agent_t{agent_info}, [](rocprofiler_agent_t* ptr) {
|
|
|
|
|
if(ptr)
|
|
|
|
|
{
|
|
|
|
|
delete[] ptr->mem_banks;
|
|
|
|
|
delete[] ptr->caches;
|
|
|
|
|
delete[] ptr->io_links;
|
|
|
|
|
}
|
|
|
|
|
delete ptr;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto&
|
|
|
|
|
get_agent_topology()
|
|
|
|
|
{
|
2024-05-28 15:06:54 -07:00
|
|
|
static auto*& _v =
|
|
|
|
|
common::static_object<std::vector<unique_agent_t>>::construct(read_topology());
|
|
|
|
|
return *CHECK_NOTNULL(_v);
|
2023-10-10 18:10:23 -05:00
|
|
|
}
|
2023-10-19 19:04:02 -05:00
|
|
|
|
|
|
|
|
auto&
|
|
|
|
|
get_agent_caches()
|
|
|
|
|
{
|
2024-05-21 13:34:54 -07:00
|
|
|
static auto*& _v = common::static_object<std::vector<hsa::AgentCache>>::construct();
|
2024-07-08 17:53:02 -05:00
|
|
|
return *CHECK_NOTNULL(_v);
|
2023-10-19 19:04:02 -05:00
|
|
|
}
|
2024-01-03 04:26:46 -06:00
|
|
|
|
|
|
|
|
struct agent_pair
|
|
|
|
|
{
|
|
|
|
|
const rocprofiler_agent_t* rocp_agent = nullptr;
|
|
|
|
|
hsa_agent_t hsa_agent = {};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto&
|
|
|
|
|
get_agent_mapping()
|
|
|
|
|
{
|
2024-07-08 17:53:02 -05:00
|
|
|
static auto*& _v = common::static_object<std::vector<agent_pair>>::construct();
|
|
|
|
|
return *CHECK_NOTNULL(_v);
|
2024-01-03 04:26:46 -06:00
|
|
|
}
|
2024-01-25 23:47:40 -06:00
|
|
|
|
2024-04-12 18:46:10 -07:00
|
|
|
const std::vector<aqlprofile_agent_handle_t>&
|
|
|
|
|
get_aql_handles()
|
|
|
|
|
{
|
2024-07-08 17:53:02 -05:00
|
|
|
static auto*& _v =
|
|
|
|
|
common::static_object<std::vector<aqlprofile_agent_handle_t>>::construct([]() {
|
|
|
|
|
std::vector<aqlprofile_agent_handle_t> agent_handles;
|
|
|
|
|
for(auto& agent : get_agents())
|
2024-04-12 18:46:10 -07:00
|
|
|
{
|
2024-07-08 17:53:02 -05:00
|
|
|
aqlprofile_agent_info_t agent_info = {
|
|
|
|
|
.agent_gfxip = agent->name,
|
|
|
|
|
.xcc_num = agent->num_xcc,
|
|
|
|
|
.se_num = agent->num_shader_banks,
|
|
|
|
|
.cu_num = agent->cu_count,
|
|
|
|
|
.shader_arrays_per_se = agent->simd_arrays_per_engine};
|
|
|
|
|
aqlprofile_agent_handle_t handle = {.handle = 0};
|
|
|
|
|
if(aqlprofile_register_agent(&handle, &agent_info) != HSA_STATUS_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
ROCP_WARNING << "Failed to register agent " << agent->name;
|
|
|
|
|
}
|
|
|
|
|
agent_handles.push_back(handle);
|
2024-04-12 18:46:10 -07:00
|
|
|
}
|
2024-07-08 17:53:02 -05:00
|
|
|
return agent_handles;
|
|
|
|
|
}());
|
2024-04-12 18:46:10 -07:00
|
|
|
|
2024-07-08 17:53:02 -05:00
|
|
|
return *CHECK_NOTNULL(_v);
|
2024-04-12 18:46:10 -07:00
|
|
|
}
|
2024-12-06 12:35:29 -06:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
std::vector<const rocprofiler_agent_t*>
|
|
|
|
|
get_agents()
|
|
|
|
|
{
|
|
|
|
|
auto& agents = rocprofiler::agent::get_agent_topology();
|
|
|
|
|
auto pointers = std::vector<const rocprofiler_agent_t*>{};
|
|
|
|
|
pointers.reserve(agents.size());
|
|
|
|
|
for(auto& agent : agents)
|
|
|
|
|
{
|
|
|
|
|
pointers.emplace_back(agent.get());
|
|
|
|
|
}
|
|
|
|
|
return pointers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rocprofiler_agent_t*
|
|
|
|
|
get_agent(rocprofiler_agent_id_t id)
|
|
|
|
|
{
|
|
|
|
|
for(const auto& itr : get_agents())
|
|
|
|
|
{
|
|
|
|
|
if(itr && itr->id.handle == id.handle) return itr;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2024-04-12 18:46:10 -07:00
|
|
|
|
|
|
|
|
const aqlprofile_agent_handle_t*
|
|
|
|
|
get_aql_agent(rocprofiler_agent_id_t id)
|
|
|
|
|
{
|
|
|
|
|
size_t pos = 0;
|
|
|
|
|
for(const auto& itr : get_agents())
|
|
|
|
|
{
|
|
|
|
|
if(itr && itr->id.handle == id.handle)
|
|
|
|
|
{
|
|
|
|
|
return &get_aql_handles().at(pos);
|
|
|
|
|
}
|
|
|
|
|
pos++;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 19:04:02 -05:00
|
|
|
void
|
|
|
|
|
construct_agent_cache(::HsaApiTable* table)
|
|
|
|
|
{
|
|
|
|
|
if(!table) return;
|
|
|
|
|
|
|
|
|
|
auto rocp_agents = agent::get_agents();
|
|
|
|
|
auto hsa_agents = std::vector<hsa_agent_t>{};
|
|
|
|
|
|
|
|
|
|
// Get HSA Agents
|
|
|
|
|
table->core_->hsa_iterate_agents_fn(
|
|
|
|
|
[](hsa_agent_t agent, void* data) {
|
|
|
|
|
CHECK_NOTNULL(static_cast<std::vector<hsa_agent_t>*>(data))->emplace_back(agent);
|
|
|
|
|
return HSA_STATUS_SUCCESS;
|
|
|
|
|
},
|
|
|
|
|
&hsa_agents);
|
|
|
|
|
|
2024-03-21 20:04:21 -05:00
|
|
|
auto get_hsa_status_string = [table](hsa_status_t _status) -> std::string_view {
|
|
|
|
|
const char* _status_msg = nullptr;
|
|
|
|
|
return (table->core_->hsa_status_string_fn(_status, &_status_msg) == HSA_STATUS_SUCCESS &&
|
|
|
|
|
_status_msg)
|
|
|
|
|
? std::string_view{_status_msg}
|
|
|
|
|
: std::string_view{"(unknown HSA error)"};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto rocp_hsa_agent_node_ids = std::set<uint32_t>{};
|
|
|
|
|
if(rocp_agents.size() != hsa_agents.size())
|
|
|
|
|
{
|
|
|
|
|
for(auto hitr : hsa_agents)
|
|
|
|
|
{
|
|
|
|
|
auto internal_node_id = std::numeric_limits<uint32_t>::max();
|
|
|
|
|
auto ret = table->core_->hsa_agent_get_info_fn(
|
|
|
|
|
hitr,
|
|
|
|
|
static_cast<hsa_agent_info_t>(HSA_AMD_AGENT_INFO_DRIVER_NODE_ID),
|
|
|
|
|
&internal_node_id);
|
|
|
|
|
|
2024-05-20 15:38:18 -05:00
|
|
|
ROCP_ERROR_IF(ret != HSA_STATUS_SUCCESS)
|
2024-03-21 20:04:21 -05:00
|
|
|
<< "hsa_agent_get_info(hsa_agent_t=" << hitr.handle
|
|
|
|
|
<< ", HSA_AMD_AGENT_INFO_DRIVER_NODE_ID, ...) returned " << ret
|
|
|
|
|
<< " :: " << get_hsa_status_string(ret);
|
|
|
|
|
|
|
|
|
|
if(ret == HSA_STATUS_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
auto ret_emplace = rocp_hsa_agent_node_ids.emplace(internal_node_id).second;
|
2024-05-20 15:38:18 -05:00
|
|
|
ROCP_WARNING_IF(!ret_emplace)
|
2024-03-21 20:04:21 -05:00
|
|
|
<< "duplicate internal node id " << internal_node_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(const auto* ritr : rocp_agents)
|
|
|
|
|
{
|
2024-04-02 01:38:18 -05:00
|
|
|
// TODO(aelwazir): To be changed back to use node id once ROCR fixes
|
|
|
|
|
// the hsa_agents to use the real node id
|
|
|
|
|
if(ritr->logical_node_id == static_cast<int64_t>(internal_node_id))
|
2024-03-21 20:04:21 -05:00
|
|
|
{
|
|
|
|
|
rocp_hsa_agent_node_ids.erase(internal_node_id);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 15:38:18 -05:00
|
|
|
ROCP_FATAL_IF(!rocp_hsa_agent_node_ids.empty())
|
2023-10-19 19:04:02 -05:00
|
|
|
<< "Found " << rocp_agents.size() << " rocprofiler agents and " << hsa_agents.size()
|
2024-03-21 20:04:21 -05:00
|
|
|
<< " HSA agents. HSA agents contained " << rocp_hsa_agent_node_ids.size()
|
|
|
|
|
<< " internal node ids not found by rocprofiler: "
|
|
|
|
|
<< fmt::format(
|
|
|
|
|
"{}",
|
|
|
|
|
fmt::join(rocp_hsa_agent_node_ids.begin(), rocp_hsa_agent_node_ids.end(), ", "));
|
2023-10-19 19:04:02 -05:00
|
|
|
|
2024-07-01 21:56:41 -03:00
|
|
|
get_agent_caches().clear();
|
|
|
|
|
get_agent_mapping().clear();
|
2024-01-03 04:26:46 -06:00
|
|
|
get_agent_mapping().reserve(get_agent_mapping().size() + rocp_agents.size());
|
|
|
|
|
|
2023-10-19 19:04:02 -05:00
|
|
|
auto hsa_agent_node_map = std::unordered_map<uint32_t, hsa_agent_t>{};
|
|
|
|
|
for(const auto& itr : hsa_agents)
|
|
|
|
|
{
|
|
|
|
|
if(uint32_t node_id = 0;
|
|
|
|
|
table->core_->hsa_agent_get_info_fn(
|
|
|
|
|
itr, static_cast<hsa_agent_info_t>(HSA_AMD_AGENT_INFO_DRIVER_NODE_ID), &node_id) ==
|
|
|
|
|
HSA_STATUS_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
hsa_agent_node_map[node_id] = itr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto agent_map =
|
|
|
|
|
std::unordered_map<uint32_t, std::tuple<const rocprofiler_agent_t*, hsa_agent_t>>{};
|
|
|
|
|
for(const auto* ritr : rocp_agents)
|
|
|
|
|
{
|
|
|
|
|
for(auto hitr : hsa_agents)
|
|
|
|
|
{
|
|
|
|
|
if(uint32_t node_id = 0;
|
|
|
|
|
table->core_->hsa_agent_get_info_fn(
|
|
|
|
|
hitr,
|
|
|
|
|
static_cast<hsa_agent_info_t>(HSA_AMD_AGENT_INFO_DRIVER_NODE_ID),
|
|
|
|
|
&node_id) == HSA_STATUS_SUCCESS)
|
|
|
|
|
{
|
2024-04-02 01:38:18 -05:00
|
|
|
// TODO(aelwazir): To be changed back to use node id once ROCR fixes
|
|
|
|
|
// the hsa_agents to use the real node id
|
|
|
|
|
if(ritr->logical_node_id == static_cast<int64_t>(node_id))
|
2023-10-19 19:04:02 -05:00
|
|
|
{
|
2024-04-02 01:38:18 -05:00
|
|
|
agent_map.emplace(ritr->logical_node_id, std::make_tuple(ritr, hitr));
|
2024-01-03 04:26:46 -06:00
|
|
|
get_agent_mapping().emplace_back(agent_pair{ritr, hitr});
|
2023-10-19 19:04:02 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 19:49:49 -05:00
|
|
|
ROCP_INFO << "# agent node maps: " << hsa_agent_node_map.size();
|
2024-03-21 20:04:21 -05:00
|
|
|
|
2024-05-20 15:38:18 -05:00
|
|
|
ROCP_FATAL_IF(agent_map.size() != hsa_agents.size())
|
2023-10-19 19:04:02 -05:00
|
|
|
<< "rocprofiler was only able to map " << agent_map.size()
|
|
|
|
|
<< " rocprofiler agents to HSA agents, expected " << hsa_agents.size();
|
|
|
|
|
|
|
|
|
|
// For Pre-ROCm 6.0 releases
|
|
|
|
|
#if ROCPROFILER_HSA_RUNTIME_VERSION <= 100900
|
|
|
|
|
# define HSA_AMD_AGENT_INFO_NEAREST_CPU 0xA113
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
auto find_nearest_hsa_cpu_agent = [&table, &agent_map](uint32_t node_id) {
|
|
|
|
|
auto _nearest_cpu = hsa_agent_t{.handle = 0};
|
|
|
|
|
auto _hsa_agent = std::get<1>(agent_map.at(node_id));
|
|
|
|
|
if(table->core_->hsa_agent_get_info_fn(
|
|
|
|
|
_hsa_agent,
|
|
|
|
|
static_cast<hsa_agent_info_t>(HSA_AMD_AGENT_INFO_NEAREST_CPU),
|
|
|
|
|
&_nearest_cpu) != HSA_STATUS_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
const auto* _rocp_agent = std::get<0>(agent_map.at(node_id));
|
|
|
|
|
auto distance_min = std::numeric_limits<int32_t>::max();
|
|
|
|
|
for(uint32_t i = 0; i < _rocp_agent->io_links_count; ++i)
|
|
|
|
|
{
|
|
|
|
|
const auto& io_link = _rocp_agent->io_links[i];
|
|
|
|
|
auto _from = io_link.node_from;
|
|
|
|
|
auto _to = io_link.node_to;
|
|
|
|
|
|
2024-05-20 15:38:18 -05:00
|
|
|
ROCP_FATAL_IF(_from != node_id)
|
2023-10-19 19:04:02 -05:00
|
|
|
<< "unexpected condition for node_id=" << node_id << ". io_link[" << i
|
|
|
|
|
<< "].node_from=" << _from
|
|
|
|
|
<< ". Expected this to match the node_id (node_to=" << _to << ")";
|
|
|
|
|
|
|
|
|
|
if(agent_map.find(_to) == agent_map.end())
|
|
|
|
|
{
|
2024-05-20 15:38:18 -05:00
|
|
|
ROCP_WARNING << "no agent mapping for io_link[" << i << "].node_to=" << _to
|
2023-10-19 19:04:02 -05:00
|
|
|
<< " in rocprofiler agent " << node_id;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto [_to_rocp_agent, _to_hsa_agent] = agent_map.at(_to);
|
|
|
|
|
auto _distance = std::abs(static_cast<int32_t>(_from - _to));
|
|
|
|
|
if(_distance > 0 && _distance < distance_min &&
|
|
|
|
|
_to_rocp_agent->type == ROCPROFILER_AGENT_TYPE_CPU)
|
|
|
|
|
{
|
|
|
|
|
distance_min = _distance;
|
|
|
|
|
_nearest_cpu = _to_hsa_agent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return _nearest_cpu;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto is_duplicate = [](const auto* agent_v) {
|
2023-11-28 10:04:37 -06:00
|
|
|
for(const auto& aitr : get_agent_caches())
|
2023-10-19 19:04:02 -05:00
|
|
|
{
|
2023-11-28 10:04:37 -06:00
|
|
|
if(aitr == agent_v) return true;
|
2023-10-19 19:04:02 -05:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Generate supported agents
|
|
|
|
|
for(const auto& itr : agent_map)
|
|
|
|
|
{
|
|
|
|
|
const auto* rocp_agent = std::get<0>(itr.second);
|
|
|
|
|
auto hsa_agent = std::get<1>(itr.second);
|
|
|
|
|
if(is_duplicate(rocp_agent)) continue;
|
|
|
|
|
|
|
|
|
|
// AgentCache is only for GPU agents
|
|
|
|
|
if(rocp_agent->type != ROCPROFILER_AGENT_TYPE_GPU) continue;
|
|
|
|
|
|
|
|
|
|
auto _nearest_cpu = find_nearest_hsa_cpu_agent(itr.first);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
get_agent_caches().emplace_back(
|
2024-05-21 13:34:54 -07:00
|
|
|
rocp_agent, hsa_agent, itr.first, _nearest_cpu, *table->amd_ext_, *table->core_);
|
2023-10-19 19:04:02 -05:00
|
|
|
} catch(std::runtime_error& err)
|
|
|
|
|
{
|
|
|
|
|
if(rocp_agent->type == ROCPROFILER_AGENT_TYPE_GPU)
|
|
|
|
|
{
|
2024-04-02 01:38:18 -05:00
|
|
|
// TODO(aelwazir): To be changed back to use node id once ROCR fixes
|
|
|
|
|
// the hsa_agents to use the real node id
|
2024-04-02 17:15:30 -07:00
|
|
|
ROCP_ERROR << fmt::format("rocprofiler agent <-> HSA agent mapping failed: {} ({})",
|
2024-04-02 01:38:18 -05:00
|
|
|
rocp_agent->logical_node_id,
|
2023-10-19 19:04:02 -05:00
|
|
|
err.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<hsa_agent_t>
|
|
|
|
|
get_hsa_agent(const rocprofiler_agent_t* agent)
|
|
|
|
|
{
|
2024-01-03 04:26:46 -06:00
|
|
|
for(const auto& itr : get_agent_mapping())
|
2023-10-19 19:04:02 -05:00
|
|
|
{
|
2024-01-03 04:26:46 -06:00
|
|
|
if(itr.rocp_agent->id.handle == agent->id.handle) return itr.hsa_agent;
|
2023-10-19 19:04:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rocprofiler_agent_t*
|
|
|
|
|
get_rocprofiler_agent(hsa_agent_t agent)
|
|
|
|
|
{
|
2024-01-03 04:26:46 -06:00
|
|
|
for(const auto& itr : get_agent_mapping())
|
2023-10-19 19:04:02 -05:00
|
|
|
{
|
2024-01-03 04:26:46 -06:00
|
|
|
if(itr.hsa_agent.handle == agent.handle) return itr.rocp_agent;
|
2023-10-19 19:04:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-12 18:46:10 -07:00
|
|
|
const hsa::AgentCache*
|
2023-10-19 19:04:02 -05:00
|
|
|
get_agent_cache(const rocprofiler_agent_t* agent)
|
|
|
|
|
{
|
|
|
|
|
for(const auto& itr : get_agent_caches())
|
|
|
|
|
{
|
2024-04-12 18:46:10 -07:00
|
|
|
if(itr == agent) return &itr;
|
2023-10-19 19:04:02 -05:00
|
|
|
}
|
|
|
|
|
|
2024-04-12 18:46:10 -07:00
|
|
|
return nullptr;
|
2023-10-19 19:04:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<hsa::AgentCache>
|
|
|
|
|
get_agent_cache(hsa_agent_t agent)
|
|
|
|
|
{
|
|
|
|
|
for(const auto& itr : get_agent_caches())
|
|
|
|
|
{
|
|
|
|
|
if(itr == agent) return itr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
2023-11-17 01:49:51 -08:00
|
|
|
|
|
|
|
|
std::unordered_set<std::string>&
|
|
|
|
|
get_agent_available_properties()
|
|
|
|
|
{
|
|
|
|
|
static std::unordered_set<std::string> _prop;
|
|
|
|
|
return _prop;
|
|
|
|
|
}
|
2023-10-10 18:10:23 -05:00
|
|
|
} // namespace agent
|
|
|
|
|
} // namespace rocprofiler
|
|
|
|
|
|
2023-09-22 13:51:21 -05:00
|
|
|
extern "C" {
|
|
|
|
|
rocprofiler_status_t
|
2024-03-06 02:17:40 -06:00
|
|
|
rocprofiler_query_available_agents(rocprofiler_agent_version_t version,
|
|
|
|
|
rocprofiler_query_available_agents_cb_t callback,
|
|
|
|
|
size_t agent_size,
|
|
|
|
|
void* user_data)
|
2023-09-22 13:51:21 -05:00
|
|
|
{
|
2024-03-06 02:17:40 -06:00
|
|
|
// only support version 0 for now
|
|
|
|
|
if(version != ROCPROFILER_AGENT_INFO_VERSION_0)
|
|
|
|
|
return ROCPROFILER_STATUS_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
|
|
// this will need to be updated for new versions
|
|
|
|
|
if(version == ROCPROFILER_AGENT_INFO_VERSION_0)
|
|
|
|
|
{
|
|
|
|
|
if(agent_size > sizeof(rocprofiler_agent_v0_t))
|
|
|
|
|
{
|
2024-04-02 17:15:30 -07:00
|
|
|
ROCP_ERROR << "size of rocprofiler agent struct used by caller is ABI-incompatible "
|
2024-03-06 02:17:40 -06:00
|
|
|
"with rocprofiler_agent_v0_t in rocprofiler";
|
|
|
|
|
return ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_ABI;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-10-10 18:10:23 -05:00
|
|
|
{
|
2024-04-02 17:15:30 -07:00
|
|
|
ROCP_FATAL << "rocprofiler-sdk does not support given agent info version";
|
2023-10-10 18:10:23 -05:00
|
|
|
}
|
2023-09-22 13:51:21 -05:00
|
|
|
|
2024-03-06 02:17:40 -06:00
|
|
|
auto&& pointers = rocprofiler::agent::get_agents();
|
|
|
|
|
auto v_pointers = std::vector<const void*>{};
|
|
|
|
|
v_pointers.reserve(pointers.size());
|
|
|
|
|
for(const auto& itr : pointers)
|
|
|
|
|
v_pointers.emplace_back(itr);
|
|
|
|
|
return callback(version, v_pointers.data(), pointers.size(), user_data);
|
2023-09-22 13:51:21 -05:00
|
|
|
}
|
|
|
|
|
}
|