Project
Loading...
Searching...
No Matches
DeviceMetricsInfo.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11//
12#ifndef O2_FRAMEWORK_DEVICEMETRICSINFO_H_
13#define O2_FRAMEWORK_DEVICEMETRICSINFO_H_
14
17#include "Framework/Traits.h"
18#include <array>
19#include <cstddef>
20#include <cstdint>
21#include <gsl/span>
22#include <string>
23#include <vector>
24
25namespace o2::framework
26{
27
28enum class MetricType {
29 Int = 0,
30 String = 1,
31 Float = 2,
32 Uint64 = 3,
33 // DPL specific type, used for the GUI. Maximum 8 bits
34 // and we keep only the last 8 entries in the history.
35 Enum = 16,
37};
38
39std::ostream& operator<<(std::ostream& oss, MetricType const& val);
40
41struct MetricInfo {
43 size_t storeIdx = -1; // Index in the actual store
44 size_t pos = 0; // Last position in the circular buffer
45 size_t filledMetrics = 0; // How many metrics were filled
46};
47
48// We keep only fixed lenght strings for metrics, as in the end this is not
49// really needed. They should be nevertheless 0 terminated.
51 static constexpr ptrdiff_t MAX_SIZE = 512;
53};
54
55// Also for the keys it does not make much sense to keep more than 255 chars.
56// They should be nevertheless 0 terminated.
58 static constexpr size_t MAX_METRIC_LABEL_SIZE = 256 - sizeof(unsigned char); // Maximum size for a metric name.
59 unsigned char size = 0;
61};
62
64 static constexpr size_t MAX_METRIC_PREFIX_SIZE = 256 - sizeof(unsigned char); // Maximum size for a metric name.
65 unsigned char size = 0;
67 int begin = 0;
68 int end = 0;
69};
70
72 size_t index;
73};
74
76 size_t index;
77};
78
81 // Pointer to the first character of the metric name.
82 char const* beginKey;
83 // Pointer to the last character of the metric name.
84 char const* endKey;
85 // If the metric is in the form name/<begin>-<end>, this is /
86 // nullptr otherwise. If not nullptr, the actual
87 // metric name is given by the range [beginKey, beginRange) applied
88 // the key. I.e.:
89 // name/<begin>-<end> -> name/<begin>...name/<end>
90 // If the metric is in the form name/<begin>-<end>, this is <begin>
91 int firstIndex = -1;
92 // If the metric is in the form name/<begin>-<end>, this is <end>
93 int lastIndex = -1;
94 size_t timestamp;
98 uint64_t uint64Value;
99 char const* beginStringValue;
100 char const* endStringValue;
101};
102
103template <typename T>
104inline constexpr size_t metricStorageSize()
105{
106 if constexpr (std::is_same_v<T, int>) {
107 return 1024;
108 } else if constexpr (std::is_same_v<T, uint64_t>) {
109 return 1024;
110 } else if constexpr (std::is_same_v<T, StringMetric>) {
111 return 32;
112 } else if constexpr (std::is_same_v<T, float>) {
113 return 1024;
114 } else if constexpr (std::is_same_v<T, int8_t>) {
115 return 8;
116 } else {
117 static_assert(always_static_assert_v<T>, "Unsupported type");
118 }
119}
120
121static inline constexpr size_t metricStorageSize(enum MetricType type)
122{
123 switch (type) {
124 case MetricType::Int:
125 return 1024;
127 return 1024;
129 return 1024;
131 return 32;
132 case MetricType::Enum:
133 return 8;
135 return 0;
136 }
138}
139
140template <typename T>
141using MetricsStorage = std::array<T, metricStorageSize<T>()>;
142
143template <typename T>
144using TimestampsStorage = std::array<size_t, metricStorageSize<T>()>;
145
150 // We keep the size of each metric to 4096 bytes. No need for more
151 // for the debug GUI
152 std::vector<MetricsStorage<int>> intMetrics;
153 std::vector<MetricsStorage<uint64_t>> uint64Metrics;
154 std::vector<MetricsStorage<StringMetric>> stringMetrics; // We do not keep so many strings as metrics as history is less relevant.
155 std::vector<MetricsStorage<float>> floatMetrics;
156 std::vector<MetricsStorage<int8_t>> enumMetrics;
157 std::vector<std::array<size_t, metricStorageSize<int>()>> intTimestamps;
158 std::vector<std::array<size_t, metricStorageSize<uint64_t>()>> uint64Timestamps;
159 std::vector<std::array<size_t, metricStorageSize<float>()>> floatTimestamps;
160 std::vector<std::array<size_t, metricStorageSize<StringMetric>()>> stringTimestamps;
161 std::vector<std::array<size_t, metricStorageSize<int8_t>()>> enumTimestamps;
162 std::vector<float> max;
163 std::vector<float> min;
164 std::vector<float> average;
165 std::vector<size_t> minDomain;
166 std::vector<size_t> maxDomain;
167 std::vector<MetricLabel> metricLabels;
168 std::vector<MetricPrefix> metricPrefixes;
169 std::vector<MetricLabelIndex> metricLabelsAlphabeticallySortedIdx;
170 std::vector<MetricPrefixIndex> metricLabelsPrefixesSortedIdx;
171 std::vector<MetricInfo> metrics;
172 std::vector<bool> changed;
173};
174
176 template <typename T, size_t I = metricStorageSize<T>()>
177 static std::array<T, I> const& get(DeviceMetricsInfo const& info, size_t metricIdx)
178 {
179 if constexpr (std::is_same_v<T, int>) {
180 return info.intMetrics[metricIdx];
181 } else if constexpr (std::is_same_v<T, uint64_t>) {
182 return info.uint64Metrics[metricIdx];
183 } else if constexpr (std::is_same_v<T, StringMetric>) {
184 return info.stringMetrics[metricIdx];
185 } else if constexpr (std::is_same_v<T, float>) {
186 return info.floatMetrics[metricIdx];
187 } else if constexpr (std::is_same_v<T, int8_t>) {
188 return info.enumMetrics[metricIdx];
189 } else {
190 static_assert(always_static_assert_v<T>, "Unsupported type");
191 }
192 }
193
194 static void clearMetrics(std::vector<DeviceMetricsInfo>& infos)
195 {
196 for (auto& info : infos) {
197 info.intMetrics.clear();
198 info.uint64Metrics.clear();
199 info.stringMetrics.clear(); // We do not keep so many strings as metrics as history is less relevant.
200 info.floatMetrics.clear();
201 info.enumMetrics.clear();
202 info.intTimestamps.clear();
203 info.uint64Timestamps.clear();
204 info.floatTimestamps.clear();
205 info.stringTimestamps.clear();
206 info.enumTimestamps.clear();
207 info.max.clear();
208 info.min.clear();
209 info.average.clear();
210 info.minDomain.clear();
211 info.maxDomain.clear();
212 info.metricLabels.clear();
213 info.metricPrefixes.clear();
214 info.metricLabelsAlphabeticallySortedIdx.clear();
215 info.metricLabelsPrefixesSortedIdx.clear();
216 info.metrics.clear();
217 info.changed.clear();
218 }
219 }
220 static size_t metricsStorageSize(gsl::span<DeviceMetricsInfo const> infos)
221 {
222 // Count the size of the metrics storage
223 size_t totalSize = 0;
224 for (auto& info : infos) {
225 totalSize += info.intMetrics.size() * sizeof(MetricsStorage<int>);
226 totalSize += info.uint64Metrics.size() * sizeof(MetricsStorage<uint64_t>);
227 totalSize += info.stringMetrics.size() * sizeof(MetricsStorage<StringMetric>);
228 totalSize += info.floatMetrics.size() * sizeof(MetricsStorage<float>);
229 totalSize += info.enumMetrics.size() * sizeof(MetricsStorage<int8_t>);
230 totalSize += info.intTimestamps.size() * sizeof(TimestampsStorage<int>);
231 totalSize += info.uint64Timestamps.size() * sizeof(TimestampsStorage<uint64_t>);
232 totalSize += info.floatTimestamps.size() * sizeof(TimestampsStorage<float>);
233 totalSize += info.stringTimestamps.size() * sizeof(TimestampsStorage<StringMetric>);
234 totalSize += info.enumTimestamps.size() * sizeof(TimestampsStorage<int8_t>);
235 totalSize += info.max.size() * sizeof(float);
236 totalSize += info.min.size() * sizeof(float);
237 totalSize += info.average.size() * sizeof(float);
238 totalSize += info.minDomain.size() * sizeof(size_t);
239 totalSize += info.maxDomain.size() * sizeof(size_t);
240 totalSize += info.metricLabels.size() * sizeof(MetricLabel);
241 totalSize += info.metricPrefixes.size() * sizeof(MetricPrefix);
242 totalSize += info.metricLabelsAlphabeticallySortedIdx.size() * sizeof(MetricLabelIndex);
243 totalSize += info.metricLabelsPrefixesSortedIdx.size() * sizeof(MetricPrefixIndex);
244 totalSize += info.metrics.size() * sizeof(MetricInfo);
245 totalSize += info.changed.size() * sizeof(bool);
246 }
247
248 return totalSize;
249 }
250};
251
252} // namespace o2::framework
253
254#endif // O2_FRAMEWORK_DEVICEMETRICSINFO_H_
#define O2_BUILTIN_UNREACHABLE
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLuint end
Definition glcorearb.h:469
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
GLboolean * data
Definition glcorearb.h:298
GLuint GLsizei const GLchar * label
Definition glcorearb.h:2519
GLuint GLfloat * val
Definition glcorearb.h:1582
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::array< size_t, metricStorageSize< T >()> TimestampsStorage
constexpr size_t metricStorageSize()
std::ostream & operator<<(std::ostream &s, ChannelType const &type)
Stream operators so that we can use ChannelType with Boost.Test.
std::array< T, metricStorageSize< T >()> MetricsStorage
static std::array< T, I > const & get(DeviceMetricsInfo const &info, size_t metricIdx)
static size_t metricsStorageSize(gsl::span< DeviceMetricsInfo const > infos)
static void clearMetrics(std::vector< DeviceMetricsInfo > &infos)
std::vector< MetricsStorage< float > > floatMetrics
std::vector< MetricsStorage< StringMetric > > stringMetrics
std::vector< std::array< size_t, metricStorageSize< int8_t >()> > enumTimestamps
std::vector< MetricsStorage< uint64_t > > uint64Metrics
std::vector< MetricsStorage< int > > intMetrics
std::vector< MetricPrefix > metricPrefixes
std::vector< std::array< size_t, metricStorageSize< float >()> > floatTimestamps
std::vector< MetricLabel > metricLabels
std::vector< std::array< size_t, metricStorageSize< uint64_t >()> > uint64Timestamps
std::vector< std::array< size_t, metricStorageSize< StringMetric >()> > stringTimestamps
std::vector< MetricPrefixIndex > metricLabelsPrefixesSortedIdx
std::vector< MetricLabelIndex > metricLabelsAlphabeticallySortedIdx
std::vector< MetricInfo > metrics
std::vector< MetricsStorage< int8_t > > enumMetrics
std::vector< std::array< size_t, metricStorageSize< int >()> > intTimestamps
static constexpr size_t MAX_METRIC_LABEL_SIZE
static constexpr size_t MAX_METRIC_PREFIX_SIZE
char prefix[MAX_METRIC_PREFIX_SIZE]
Temporary struct to hold a metric after it has been parsed.
static constexpr ptrdiff_t MAX_SIZE