|
14 | 14 | #include "Framework/RuntimeError.h" |
15 | 15 | #include <array> |
16 | 16 | #include <cstddef> |
17 | | -#include <cstring> |
18 | | -#include <functional> |
19 | 17 | #include <string> |
20 | | -#include <string_view> |
21 | 18 | #include <vector> |
22 | 19 |
|
23 | 20 | namespace o2::framework |
@@ -88,115 +85,6 @@ struct DeviceMetricsInfo { |
88 | 85 | std::vector<bool> changed; |
89 | 86 | }; |
90 | 87 |
|
91 | | -struct DeviceMetricsHelper { |
92 | | - /// Type of the callback which can be provided to be invoked every time a new |
93 | | - /// metric is found by the system. |
94 | | - using NewMetricCallback = std::function<void(std::string const&, MetricInfo const&, int value, size_t metricIndex)>; |
95 | | - |
96 | | - /// Helper function to parse a metric string. |
97 | | - static bool parseMetric(std::string_view const s, ParsedMetricMatch& results); |
98 | | - |
99 | | - /// Processes a parsed metric and stores in the backend store. |
100 | | - /// |
101 | | - /// @matches is the regexp_matches from the metric identifying regex |
102 | | - /// @info is the DeviceInfo associated to the device posting the metric |
103 | | - /// @newMetricsCallback is a callback that will be invoked every time a new metric is added to the list. |
104 | | - static bool processMetric(ParsedMetricMatch& results, |
105 | | - DeviceMetricsInfo& info, |
106 | | - NewMetricCallback newMetricCallback = nullptr); |
107 | | - /// @return the index in metrics for the information of given metric |
108 | | - static size_t metricIdxByName(const std::string& name, |
109 | | - const DeviceMetricsInfo& info); |
110 | | - |
111 | | - /// Typesafe way to get the actual store |
112 | | - template <typename T> |
113 | | - static auto& getMetricsStore(DeviceMetricsInfo& metrics) |
114 | | - { |
115 | | - if constexpr (std::is_same_v<T, int>) { |
116 | | - return metrics.intMetrics; |
117 | | - } else if constexpr (std::is_same_v<T, float>) { |
118 | | - return metrics.floatMetrics; |
119 | | - } else if constexpr (std::is_same_v<T, uint64_t>) { |
120 | | - return metrics.uint64Metrics; |
121 | | - } else { |
122 | | - throw runtime_error("Unhandled metric type"); |
123 | | - }; |
124 | | - } |
125 | | - |
126 | | - template <typename T> |
127 | | - static auto getMetricType() |
128 | | - { |
129 | | - if constexpr (std::is_same_v<T, int>) { |
130 | | - return MetricType::Int; |
131 | | - } else if constexpr (std::is_same_v<T, float>) { |
132 | | - return MetricType::Float; |
133 | | - } else if constexpr (std::is_same_v<T, uint64_t>) { |
134 | | - return MetricType::Uint64; |
135 | | - } else { |
136 | | - throw runtime_error("Unhandled metric type"); |
137 | | - }; |
138 | | - } |
139 | | - |
140 | | - /// @return helper to insert a given value in the metrics |
141 | | - template <typename T> |
142 | | - static std::function<void(DeviceMetricsInfo&, T value, size_t timestamp)> |
143 | | - createNumericMetric(DeviceMetricsInfo& metrics, |
144 | | - char const* name, |
145 | | - NewMetricCallback newMetricsCallback = nullptr) |
146 | | - { |
147 | | - static_assert(std::is_same_v<T, int> || std::is_same_v<T, uint64_t> || std::is_same_v<T, float>, "Unsupported metric type"); |
148 | | - // Create a new metric |
149 | | - MetricInfo metricInfo; |
150 | | - metricInfo.pos = 0; |
151 | | - metricInfo.type = getMetricType<T>(); |
152 | | - metricInfo.filledMetrics = 0; |
153 | | - metricInfo.storeIdx = getMetricsStore<T>(metrics).size(); |
154 | | - getMetricsStore<T>(metrics).emplace_back(std::array<T, 1024>{}); |
155 | | - |
156 | | - // Add the timestamp buffer for it |
157 | | - metrics.timestamps.emplace_back(std::array<size_t, 1024>{}); |
158 | | - metrics.max.push_back(std::numeric_limits<float>::lowest()); |
159 | | - metrics.min.push_back(std::numeric_limits<float>::max()); |
160 | | - metrics.maxDomain.push_back(std::numeric_limits<size_t>::lowest()); |
161 | | - metrics.minDomain.push_back(std::numeric_limits<size_t>::max()); |
162 | | - metrics.changed.push_back(true); |
163 | | - |
164 | | - // Add the index by name in the correct position |
165 | | - // this will require moving the tail of the index, |
166 | | - // but inserting should happen only once for each metric, |
167 | | - // so who cares. |
168 | | - // Add the the actual Metric info to the store |
169 | | - MetricLabelIndex metricLabelIdx; |
170 | | - strncpy(metricLabelIdx.label, name, MetricLabelIndex::MAX_METRIC_LABEL_SIZE - 1); |
171 | | - metricLabelIdx.label[MetricLabelIndex::MAX_METRIC_LABEL_SIZE - 1] = '\0'; |
172 | | - metricLabelIdx.index = metrics.metrics.size(); |
173 | | - metricLabelIdx.size = strlen(metricLabelIdx.label); |
174 | | - metrics.metricLabelsIdx.push_back(metricLabelIdx); |
175 | | - |
176 | | - // Add the the actual Metric info to the store |
177 | | - auto metricIndex = metrics.metrics.size(); |
178 | | - metrics.metrics.push_back(metricInfo); |
179 | | - |
180 | | - if (newMetricsCallback != nullptr) { |
181 | | - newMetricsCallback(metricLabelIdx.label, metricInfo, 0, metricIndex); |
182 | | - } |
183 | | - return [metricIndex](DeviceMetricsInfo& metrics, T value, size_t timestamp) { |
184 | | - MetricInfo& metric = metrics.metrics[metricIndex]; |
185 | | - |
186 | | - metrics.minDomain[metricIndex] = std::min(metrics.minDomain[metricIndex], timestamp); |
187 | | - metrics.maxDomain[metricIndex] = std::max(metrics.maxDomain[metricIndex], timestamp); |
188 | | - metrics.max[metricIndex] = std::max(metrics.max[metricIndex], (float)value); |
189 | | - metrics.min[metricIndex] = std::min(metrics.min[metricIndex], (float)value); |
190 | | - metrics.changed.at(metricIndex) = true; |
191 | | - auto& store = getMetricsStore<T>(metrics); |
192 | | - size_t pos = metric.pos++ % store[metric.storeIdx].size(); |
193 | | - metrics.timestamps[metricIndex][pos] = timestamp; |
194 | | - store[metric.storeIdx][pos] = value; |
195 | | - metric.filledMetrics++; |
196 | | - }; |
197 | | - } |
198 | | -}; |
199 | | - |
200 | 88 | } // namespace o2::framework |
201 | 89 |
|
202 | 90 | #endif // O2_FRAMEWORK_DEVICEMETRICSINFO_H_ |
0 commit comments