2023-11-14 10:58:33 -06:00
|
|
|
// MIT License
|
|
|
|
|
//
|
2025-01-23 06:41:20 +05:30
|
|
|
// Copyright (c) 2023-2025 Advanced Micro Devices, Inc. All rights reserved.
|
2023-11-14 10:58:33 -06: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-09-26 11:50:57 -07:00
|
|
|
|
|
|
|
|
#include "metrics.hpp"
|
|
|
|
|
|
2023-11-29 20:43:18 -06:00
|
|
|
#include <rocprofiler-sdk/rocprofiler.h>
|
2023-10-16 13:41:40 -07:00
|
|
|
|
2023-11-28 10:04:37 -06:00
|
|
|
#include "lib/common/filesystem.hpp"
|
2025-03-14 01:07:16 -07:00
|
|
|
#include "lib/common/logging.hpp"
|
2024-01-11 23:35:12 -08:00
|
|
|
#include "lib/common/static_object.hpp"
|
2024-11-22 01:55:47 -08:00
|
|
|
#include "lib/common/synchronized.hpp"
|
2023-10-16 13:41:40 -07:00
|
|
|
#include "lib/common/utility.hpp"
|
2023-11-29 20:43:18 -06:00
|
|
|
#include "lib/rocprofiler-sdk/agent.hpp"
|
2023-10-16 13:41:40 -07:00
|
|
|
|
|
|
|
|
#include "glog/logging.h"
|
|
|
|
|
|
2024-11-22 01:55:47 -08:00
|
|
|
#include "rocprofiler-sdk/fwd.h"
|
2024-07-12 16:20:33 -07:00
|
|
|
#include "yaml-cpp/exceptions.h"
|
|
|
|
|
#include "yaml-cpp/node/convert.h"
|
|
|
|
|
#include "yaml-cpp/node/detail/impl.h"
|
|
|
|
|
#include "yaml-cpp/node/impl.h"
|
|
|
|
|
#include "yaml-cpp/node/iterator.h"
|
|
|
|
|
#include "yaml-cpp/node/node.h"
|
|
|
|
|
#include "yaml-cpp/node/parse.h"
|
|
|
|
|
#include "yaml-cpp/parser.h"
|
|
|
|
|
|
2023-09-26 11:50:57 -07:00
|
|
|
#include <dlfcn.h> // for dladdr
|
2023-11-03 23:10:40 -05:00
|
|
|
#include <cstdint>
|
2023-09-26 11:50:57 -07:00
|
|
|
#include <cstdlib>
|
2025-03-14 01:07:16 -07:00
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
2023-09-26 11:50:57 -07:00
|
|
|
|
2023-10-16 13:41:40 -07:00
|
|
|
namespace rocprofiler
|
|
|
|
|
{
|
2023-09-26 11:50:57 -07:00
|
|
|
namespace counters
|
|
|
|
|
{
|
|
|
|
|
namespace
|
|
|
|
|
{
|
2024-11-22 01:55:47 -08:00
|
|
|
common::Synchronized<CustomCounterDefinition>&
|
|
|
|
|
getCustomCounterDefinition()
|
|
|
|
|
{
|
|
|
|
|
static common::Synchronized<CustomCounterDefinition> def = {};
|
|
|
|
|
return def;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 01:49:51 -08:00
|
|
|
/**
|
|
|
|
|
* Constant/speical metrics are treated as psudo-metrics in that they
|
|
|
|
|
* are given their own metric id. MAX_WAVE_SIZE for example is not collected
|
|
|
|
|
* by AQL Profiler but is a constant from the topology. It will still have
|
|
|
|
|
* a counter associated with it. Nearly all metrics contained in
|
|
|
|
|
* rocprofiler_agent_t will have a counter id associated with it and can be
|
|
|
|
|
* used in derived counters (exact support properties that can be used can
|
|
|
|
|
* be viewed in evaluate_ast.cpp:get_agent_property()).
|
|
|
|
|
*/
|
2025-03-14 01:07:16 -07:00
|
|
|
std::vector<Metric>
|
|
|
|
|
get_constants(uint64_t starting_id)
|
2023-11-17 01:49:51 -08:00
|
|
|
{
|
2025-03-14 01:07:16 -07:00
|
|
|
std::vector<Metric> constants;
|
2023-11-17 01:49:51 -08:00
|
|
|
// Ensure topology is read
|
|
|
|
|
rocprofiler::agent::get_agents();
|
|
|
|
|
for(const auto& prop : rocprofiler::agent::get_agent_available_properties())
|
|
|
|
|
{
|
|
|
|
|
constants.emplace_back("constant",
|
|
|
|
|
prop,
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
fmt::format("Constant value {} from agent properties", prop),
|
|
|
|
|
"",
|
|
|
|
|
"yes",
|
2025-03-14 01:07:16 -07:00
|
|
|
starting_id);
|
|
|
|
|
starting_id++;
|
2023-11-17 01:49:51 -08:00
|
|
|
}
|
|
|
|
|
return constants;
|
|
|
|
|
}
|
2024-07-12 16:20:33 -07:00
|
|
|
/**
|
|
|
|
|
* Expected YAML Format:
|
|
|
|
|
* COUNTER_NAME:
|
|
|
|
|
* architectures:
|
|
|
|
|
* gfxXX: // Can be more than one, / deliminated if they share idential data
|
|
|
|
|
* block: <Optional>
|
|
|
|
|
* event: <Optional>
|
|
|
|
|
* expression: <optional>
|
|
|
|
|
* description: <Optional>
|
|
|
|
|
* gfxYY:
|
|
|
|
|
* ...
|
|
|
|
|
* description: General counter desctiption
|
|
|
|
|
*/
|
2025-03-14 01:07:16 -07:00
|
|
|
counter_metrics_t
|
|
|
|
|
loadYAML(const std::string& filename, std::optional<ArchMetric> add_metric)
|
2023-09-26 11:50:57 -07:00
|
|
|
{
|
2025-03-14 01:07:16 -07:00
|
|
|
// Stores metrics that are added via the API
|
|
|
|
|
static MetricMap added_metrics;
|
|
|
|
|
|
2023-11-17 01:49:51 -08:00
|
|
|
MetricMap ret;
|
2024-11-22 01:55:47 -08:00
|
|
|
auto override = getCustomCounterDefinition().wlock([&](auto& data) {
|
|
|
|
|
data.loaded = true;
|
|
|
|
|
return data;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
std::stringstream counter_data;
|
|
|
|
|
if(override.data.empty() || override.append)
|
|
|
|
|
{
|
|
|
|
|
ROCP_INFO << "Loading Counter Config: " << filename;
|
|
|
|
|
std::ifstream file(filename);
|
|
|
|
|
counter_data << file.rdbuf();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!override.data.empty())
|
|
|
|
|
{
|
|
|
|
|
ROCP_INFO << "Adding Override Config Data: " << override.data;
|
|
|
|
|
counter_data << override.data;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-14 01:07:16 -07:00
|
|
|
auto yaml = YAML::Load(counter_data.str());
|
|
|
|
|
uint64_t current_id = 0;
|
2023-09-26 11:50:57 -07:00
|
|
|
|
2024-07-12 16:20:33 -07:00
|
|
|
for(auto it = yaml.begin(); it != yaml.end(); ++it)
|
2023-09-26 11:50:57 -07:00
|
|
|
{
|
2024-07-12 16:20:33 -07:00
|
|
|
auto counter_name = it->first.as<std::string>();
|
2024-11-22 01:55:47 -08:00
|
|
|
if(counter_name == "schema-version") continue;
|
2024-07-12 16:20:33 -07:00
|
|
|
auto counter_def = it->second;
|
|
|
|
|
auto def_iterator = counter_def["architectures"];
|
2023-11-17 01:49:51 -08:00
|
|
|
|
2024-07-12 16:20:33 -07:00
|
|
|
for(auto def_it = def_iterator.begin(); def_it != def_iterator.end(); ++def_it)
|
2023-11-17 01:49:51 -08:00
|
|
|
{
|
2024-07-12 16:20:33 -07:00
|
|
|
auto archs = def_it->first.as<std::string>();
|
|
|
|
|
auto def = def_it->second;
|
|
|
|
|
// To save space in the YAML file, we combine architectures with the same
|
|
|
|
|
// definition into a single entry. Split these out into separate entries.
|
|
|
|
|
// architectures:
|
|
|
|
|
// gfx10/gfx1010/gfx1030/gfx1031/.....9:
|
|
|
|
|
// expression: 400*SQ_WAIT_INST_LDS/SQ_WAVES/GRBM_GUI_ACTIVE
|
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
std::stringstream ss(archs);
|
|
|
|
|
std::string arch_name;
|
|
|
|
|
|
|
|
|
|
while(std::getline(ss, arch_name, '/'))
|
|
|
|
|
{
|
|
|
|
|
auto& metricVec = ret.emplace(arch_name, std::vector<Metric>()).first->second;
|
2025-03-14 01:07:16 -07:00
|
|
|
if(metricVec.empty())
|
2024-07-12 16:20:33 -07:00
|
|
|
{
|
2025-03-14 01:07:16 -07:00
|
|
|
const auto constants = get_constants(current_id);
|
|
|
|
|
metricVec.insert(metricVec.end(), constants.begin(), constants.end());
|
|
|
|
|
current_id += constants.size();
|
2024-07-12 16:20:33 -07:00
|
|
|
}
|
|
|
|
|
|
2025-03-14 01:07:16 -07:00
|
|
|
std::string description;
|
|
|
|
|
if(def["description"])
|
|
|
|
|
description = def["description"].as<std::string>();
|
|
|
|
|
else if(counter_def["description"])
|
|
|
|
|
description = counter_def["description"].as<std::string>();
|
|
|
|
|
metricVec.emplace_back(
|
|
|
|
|
arch_name,
|
|
|
|
|
counter_name,
|
|
|
|
|
(def["block"] ? def["block"].as<std::string>() : ""),
|
|
|
|
|
(def["event"] ? def["event"].as<std::string>() : ""),
|
|
|
|
|
description,
|
|
|
|
|
(def["expression"] ? def["expression"].as<std::string>() : ""),
|
|
|
|
|
"",
|
|
|
|
|
current_id);
|
|
|
|
|
current_id++;
|
|
|
|
|
ROCP_TRACE << fmt::format("Inserted info {}: {}", arch_name, metricVec.back());
|
2024-07-12 16:20:33 -07:00
|
|
|
}
|
2023-09-26 11:50:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-14 01:07:16 -07:00
|
|
|
|
|
|
|
|
// Add custom counters after adding the above counters, ensures that the mapping is
|
|
|
|
|
// deterministic when generated.
|
|
|
|
|
for(const auto& [arch, metrics] : added_metrics)
|
|
|
|
|
{
|
|
|
|
|
auto& metricVec = ret.emplace(arch, std::vector<Metric>()).first->second;
|
|
|
|
|
metricVec.insert(metricVec.end(), metrics.begin(), metrics.end());
|
|
|
|
|
current_id += metrics.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(add_metric)
|
|
|
|
|
{
|
|
|
|
|
Metric with_id = Metric(add_metric->first,
|
|
|
|
|
add_metric->second.name(),
|
|
|
|
|
add_metric->second.block(),
|
|
|
|
|
add_metric->second.event(),
|
|
|
|
|
add_metric->second.description(),
|
|
|
|
|
add_metric->second.expression(),
|
|
|
|
|
"",
|
|
|
|
|
current_id);
|
|
|
|
|
added_metrics.emplace(add_metric->first, std::vector<Metric>{})
|
|
|
|
|
.first->second.push_back(with_id);
|
|
|
|
|
ret.emplace(add_metric->first, std::vector<Metric>{}).first->second.push_back(with_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ROCP_FATAL_IF(current_id > 65536)
|
2023-11-17 01:49:51 -08:00
|
|
|
<< "Counter count exceeds 16 bits, which may break counter id output";
|
2025-03-14 01:07:16 -07:00
|
|
|
|
|
|
|
|
return {.arch_to_metric = ret,
|
|
|
|
|
.id_to_metric =
|
|
|
|
|
[&]() {
|
|
|
|
|
MetricIdMap map;
|
|
|
|
|
for(const auto& [agent_name, metrics] : ret)
|
|
|
|
|
{
|
|
|
|
|
for(const auto& m : metrics)
|
|
|
|
|
{
|
|
|
|
|
map.emplace(m.id(), m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}(),
|
|
|
|
|
.arch_to_id =
|
|
|
|
|
[&]() {
|
|
|
|
|
ArchToId map;
|
|
|
|
|
for(const auto& [agent_name, metrics] : ret)
|
|
|
|
|
{
|
|
|
|
|
std::unordered_set<uint64_t> ids;
|
|
|
|
|
for(const auto& m : metrics)
|
|
|
|
|
{
|
|
|
|
|
ids.insert(m.id());
|
|
|
|
|
}
|
|
|
|
|
map.emplace(agent_name, std::move(ids));
|
|
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}()};
|
2023-09-26 11:50:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string
|
|
|
|
|
findViaInstallPath(const std::string& filename)
|
|
|
|
|
{
|
2023-10-16 13:41:40 -07:00
|
|
|
Dl_info dl_info = {};
|
2024-04-02 17:15:30 -07:00
|
|
|
ROCP_INFO << filename << " is being looked up via install path";
|
2023-09-26 11:50:57 -07:00
|
|
|
if(dladdr(reinterpret_cast<const void*>(rocprofiler_query_available_agents), &dl_info) != 0)
|
|
|
|
|
{
|
2023-11-28 10:04:37 -06:00
|
|
|
return common::filesystem::path{dl_info.dli_fname}.parent_path().parent_path() /
|
2023-11-29 20:43:18 -06:00
|
|
|
fmt::format("share/rocprofiler-sdk/{}", filename);
|
2023-09-26 11:50:57 -07:00
|
|
|
}
|
|
|
|
|
return filename;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string
|
|
|
|
|
findViaEnvironment(const std::string& filename)
|
|
|
|
|
{
|
2023-10-16 13:41:40 -07:00
|
|
|
if(const char* metrics_path = nullptr; (metrics_path = getenv("ROCPROFILER_METRICS_PATH")))
|
2023-09-26 11:50:57 -07:00
|
|
|
{
|
2024-04-02 17:15:30 -07:00
|
|
|
ROCP_INFO << filename << " is being looked up via env variable ROCPROFILER_METRICS_PATH";
|
2023-11-28 10:04:37 -06:00
|
|
|
return common::filesystem::path{std::string{metrics_path}} / filename;
|
2023-09-26 11:50:57 -07:00
|
|
|
}
|
|
|
|
|
// No environment variable, lookup via install path
|
|
|
|
|
return findViaInstallPath(filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2024-11-22 01:55:47 -08:00
|
|
|
rocprofiler_status_t
|
|
|
|
|
setCustomCounterDefinition(const CustomCounterDefinition& def)
|
|
|
|
|
{
|
|
|
|
|
return getCustomCounterDefinition().wlock([&](auto& data) {
|
|
|
|
|
// Counter definition already loaded, cannot override anymore
|
|
|
|
|
if(data.loaded) return ROCPROFILER_STATUS_ERROR;
|
|
|
|
|
data.data = def.data;
|
|
|
|
|
data.append = def.append;
|
|
|
|
|
return ROCPROFILER_STATUS_SUCCESS;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-14 01:07:16 -07:00
|
|
|
std::shared_ptr<const counter_metrics_t>
|
|
|
|
|
loadMetrics(bool reload, const std::optional<ArchMetric> add_metric)
|
2023-09-26 11:50:57 -07:00
|
|
|
{
|
2025-03-14 01:07:16 -07:00
|
|
|
using sync_metric = common::Synchronized<std::shared_ptr<const counter_metrics_t>>;
|
2023-09-26 11:50:57 -07:00
|
|
|
|
2025-03-14 01:07:16 -07:00
|
|
|
if(!reload && add_metric)
|
|
|
|
|
{
|
|
|
|
|
ROCP_FATAL << "Adding a metric without reloading metric list, this should not happen and "
|
|
|
|
|
"will result in custom metrics not being added";
|
|
|
|
|
}
|
2023-09-26 11:50:57 -07:00
|
|
|
|
2025-03-14 01:07:16 -07:00
|
|
|
auto reload_func = [&]() {
|
|
|
|
|
auto counters_path = findViaEnvironment("counter_defs.yaml");
|
|
|
|
|
ROCP_FATAL_IF(!common::filesystem::exists(counters_path))
|
|
|
|
|
<< "metric xml file '" << counters_path << "' does not exist";
|
|
|
|
|
return std::make_shared<counter_metrics_t>(loadYAML(counters_path, add_metric));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static sync_metric*& id_map =
|
|
|
|
|
common::static_object<sync_metric>::construct([&]() { return reload_func(); }());
|
|
|
|
|
|
|
|
|
|
if(!id_map) return nullptr;
|
|
|
|
|
|
|
|
|
|
if(!reload)
|
|
|
|
|
{
|
|
|
|
|
return id_map->rlock([](const auto& data) {
|
|
|
|
|
CHECK(data);
|
|
|
|
|
return data;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id_map->wlock([&](auto& data) {
|
|
|
|
|
data = reload_func();
|
|
|
|
|
CHECK(data);
|
|
|
|
|
return data;
|
|
|
|
|
});
|
2023-10-16 13:41:40 -07:00
|
|
|
}
|
|
|
|
|
|
2024-07-04 03:58:45 -03:00
|
|
|
std::unordered_map<uint64_t, int>
|
2024-06-12 16:25:06 -07:00
|
|
|
getPerfCountersIdMap()
|
|
|
|
|
{
|
2024-07-04 03:58:45 -03:00
|
|
|
std::unordered_map<uint64_t, int> map;
|
2025-03-14 01:07:16 -07:00
|
|
|
auto mets = loadMetrics();
|
2024-07-04 03:58:45 -03:00
|
|
|
|
2025-03-14 01:07:16 -07:00
|
|
|
for(const auto& [agent, list] : mets->arch_to_metric)
|
2024-07-04 03:58:45 -03:00
|
|
|
{
|
|
|
|
|
if(agent.find("gfx9") == std::string::npos) continue;
|
|
|
|
|
for(const auto& metric : list)
|
|
|
|
|
{
|
|
|
|
|
if(metric.name().find("SQ_") == 0 && !metric.event().empty())
|
|
|
|
|
map.emplace(metric.id(), std::stoi(metric.event()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return map;
|
2024-06-12 16:25:06 -07:00
|
|
|
}
|
|
|
|
|
|
2024-01-24 16:32:54 -06:00
|
|
|
std::vector<Metric>
|
2023-10-16 13:41:40 -07:00
|
|
|
getMetricsForAgent(const std::string& agent)
|
|
|
|
|
{
|
2025-03-14 01:07:16 -07:00
|
|
|
auto mets = loadMetrics();
|
|
|
|
|
if(const auto* metric_ptr = rocprofiler::common::get_val(mets->arch_to_metric, agent))
|
2023-10-16 13:41:40 -07:00
|
|
|
{
|
|
|
|
|
return *metric_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 16:32:54 -06:00
|
|
|
return std::vector<Metric>{};
|
2023-10-16 13:41:40 -07:00
|
|
|
}
|
|
|
|
|
|
2024-01-16 19:47:45 -08:00
|
|
|
bool
|
|
|
|
|
checkValidMetric(const std::string& agent, const Metric& metric)
|
|
|
|
|
{
|
2025-03-14 01:07:16 -07:00
|
|
|
auto metrics = loadMetrics();
|
|
|
|
|
const auto* agent_map = common::get_val(metrics->arch_to_id, agent);
|
2024-01-16 19:47:45 -08:00
|
|
|
return agent_map != nullptr && agent_map->count(metric.id()) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 13:41:40 -07:00
|
|
|
bool
|
|
|
|
|
operator<(Metric const& lhs, Metric const& rhs)
|
|
|
|
|
{
|
2024-07-01 21:56:41 -03:00
|
|
|
return std::tie(lhs.id_, lhs.flags_) < std::tie(rhs.id_, rhs.flags_);
|
2023-10-16 13:41:40 -07:00
|
|
|
}
|
2024-01-11 23:35:12 -08:00
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
operator==(Metric const& lhs, Metric const& rhs)
|
|
|
|
|
{
|
|
|
|
|
auto get_tie = [](auto& x) {
|
|
|
|
|
return std::tie(x.name_,
|
|
|
|
|
x.block_,
|
|
|
|
|
x.event_,
|
|
|
|
|
x.description_,
|
|
|
|
|
x.expression_,
|
2025-03-14 01:07:16 -07:00
|
|
|
x.constant_,
|
2024-01-11 23:35:12 -08:00
|
|
|
x.id_,
|
2024-07-01 21:56:41 -03:00
|
|
|
x.empty_,
|
|
|
|
|
x.flags_);
|
2024-01-11 23:35:12 -08:00
|
|
|
};
|
|
|
|
|
return get_tie(lhs) == get_tie(rhs);
|
|
|
|
|
}
|
2023-10-16 13:41:40 -07:00
|
|
|
} // namespace counters
|
|
|
|
|
} // namespace rocprofiler
|