Fix init order for getMetricIdMap (#341)

* Fix init order for getMetricIdMap

Ensure that getMetricIdMap() stays valid until the destruction
of the counting service. Adds a test to ensure that this behavior
is maintained (this test fails without this change).

* source formatting (clang-format v11) (#343)

Co-authored-by: bwelton <bwelton@users.noreply.github.com>

* cmake formatting (cmake-format) (#342)

Co-authored-by: bwelton <bwelton@users.noreply.github.com>

* Remove threading

* Update usage of rocprofiler::counters::get{Metric,MetricId}Map()

- uses common::static_object

* Update counter init_order testing

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: bwelton <bwelton@users.noreply.github.com>
Co-authored-by: Jonathan R. Madsen <jonathanrmadsen@gmail.com>
This commit is contained in:
Benjamin Welton
2024-01-11 23:35:12 -08:00
committed by GitHub
parent 78dd471110
commit ef71cc38c1
9 changed files with 225 additions and 15 deletions
@@ -26,6 +26,7 @@
#include "lib/common/defines.hpp"
#include "lib/common/filesystem.hpp"
#include "lib/common/static_object.hpp"
#include "lib/common/synchronized.hpp"
#include "lib/common/utility.hpp"
#include "lib/common/xml.hpp"
@@ -182,12 +183,12 @@ getBaseHardwareMetrics()
return loadXml(counters_path, true);
}
const MetricIdMap&
const MetricIdMap*
getMetricIdMap()
{
static MetricIdMap id_map = []() {
static MetricIdMap*& id_map = common::static_object<MetricIdMap>::construct([]() {
MetricIdMap map;
for(const auto& [_, val] : getMetricMap())
for(const auto& [_, val] : *CHECK_NOTNULL(getMetricMap()))
{
for(const auto& metric : val)
{
@@ -195,14 +196,14 @@ getMetricIdMap()
}
}
return map;
}();
}());
return id_map;
}
const MetricMap&
const MetricMap*
getMetricMap()
{
static MetricMap map = []() {
static MetricMap*& map = common::static_object<MetricMap>::construct([]() {
MetricMap ret = getBaseHardwareMetrics();
for(auto& [key, val] : getDerivedHardwareMetrics())
{
@@ -213,7 +214,7 @@ getMetricMap()
}
}
return ret;
}();
}());
return map;
}
@@ -221,7 +222,7 @@ const std::vector<Metric>&
getMetricsForAgent(const std::string& agent)
{
static const std::vector<Metric> empty;
const auto& map = getMetricMap();
const auto& map = *CHECK_NOTNULL(getMetricMap());
if(const auto* metric_ptr = rocprofiler::common::get_val(map, agent))
{
return *metric_ptr;
@@ -235,5 +236,21 @@ operator<(Metric const& lhs, Metric const& rhs)
{
return lhs.id() < rhs.id();
}
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_,
x.special_,
x.id_,
x.empty_);
};
return get_tie(lhs) == get_tie(rhs);
}
} // namespace counters
} // namespace rocprofiler