Files
rocm-systems/source/lib/rocprofiler-sdk/counters/dimensions.cpp
T

151 lines
4.8 KiB
C++
Raw Normal View History

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-11-03 23:10:40 -05:00
#include "dimensions.hpp"
#include <cstdint>
#include <unordered_map>
#include <vector>
#include <fmt/core.h>
2024-04-29 15:48:50 -07:00
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/rocprofiler.h>
2024-02-07 20:03:21 -08:00
#include "lib/common/static_object.hpp"
2023-11-03 23:10:40 -05:00
#include "lib/common/utility.hpp"
#include "lib/rocprofiler-sdk/aql/helpers.hpp"
#include "lib/rocprofiler-sdk/aql/packet_construct.hpp"
2024-02-07 20:03:21 -08:00
#include "lib/rocprofiler-sdk/counters/evaluate_ast.hpp"
2023-11-03 23:10:40 -05:00
namespace rocprofiler
{
namespace counters
{
std::vector<MetricDimension>
2023-11-17 01:49:51 -08:00
getBlockDimensions(std::string_view agent, const Metric& metric)
2023-11-03 23:10:40 -05:00
{
if(!metric.constant().empty())
2023-11-03 23:10:40 -05:00
{
// Special non-hardware counters without dimension data
2024-02-07 20:03:21 -08:00
return std::vector<MetricDimension>{{dimension_map().at(ROCPROFILER_DIMENSION_INSTANCE),
1,
ROCPROFILER_DIMENSION_INSTANCE}};
2023-11-03 23:10:40 -05:00
}
2024-02-07 20:03:21 -08:00
std::unordered_map<rocprofiler_profile_counter_instance_types, uint64_t> count;
std::vector<MetricDimension> ret;
for(const auto* maybe_agent : rocprofiler::agent::get_agents())
2023-11-03 23:10:40 -05:00
{
if(std::string(maybe_agent->name) == agent)
2023-11-03 23:10:40 -05:00
{
aql::CounterPacketConstruct pkt_gen(maybe_agent->id, {metric});
2024-04-08 16:43:02 -03:00
const auto& events = pkt_gen.get_counter_events(metric);
2024-02-07 20:03:21 -08:00
for(const auto& event : events)
{
std::map<int, uint64_t> dims;
auto status = aql::get_dim_info(maybe_agent->id, event, 0, dims);
2024-02-07 20:03:21 -08:00
CHECK_EQ(status, ROCPROFILER_STATUS_SUCCESS)
<< rocprofiler_get_status_string(status);
for(const auto& [id, extent] : dims)
{
if(const auto* inst_type =
rocprofiler::common::get_val(aqlprofile_id_to_rocprof_instance(), id))
{
count.emplace(*inst_type, 0).first->second = extent;
}
else
{
2024-05-20 15:38:18 -05:00
ROCP_WARNING << "Unknown AQL Profiler Dimension " << id << " " << extent;
2024-02-07 20:03:21 -08:00
}
}
}
2023-11-03 23:10:40 -05:00
}
}
2024-02-07 20:03:21 -08:00
ret.reserve(count.size());
for(const auto& [dim, size] : count)
{
ret.emplace_back(dimension_map().at(dim), size, dim);
}
return ret;
}
namespace
2024-02-07 20:03:21 -08:00
{
metric_dims
generate_dimensions()
{
std::unordered_map<uint64_t, std::vector<MetricDimension>> dims;
2024-02-07 20:03:21 -08:00
const auto asts = counters::get_ast_map();
for(const auto& [gfx, metrics] : asts->arch_to_counter_asts)
{
for(const auto& [metric, ast] : metrics)
{
auto ast_copy = ast;
try
{
dims.emplace(ast.out_id().handle, ast_copy.set_dimensions());
} catch(std::runtime_error& e)
{
ROCP_FATAL << metric << " has improper dimensions"
<< " " << e.what();
}
}
}
return {.id_to_dim = dims};
}
} // namespace
std::shared_ptr<const metric_dims>
get_dimension_cache(bool reload)
{
using DimSync = common::Synchronized<std::shared_ptr<const metric_dims>>;
static DimSync*& dim_data = common::static_object<DimSync>::construct(
[&]() { return std::make_shared<const metric_dims>(generate_dimensions()); }());
if(!dim_data) return nullptr;
if(!reload)
{
return dim_data->rlock([](const auto& data) {
CHECK(data);
return data;
});
}
return dim_data->wlock([&](auto& data) {
data = std::make_shared<const metric_dims>(generate_dimensions());
CHECK(data);
return data;
});
2023-11-03 23:10:40 -05:00
}
} // namespace counters
2023-11-06 21:59:31 -06:00
} // namespace rocprofiler