-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathDeviceMetricsHelper.cxx
More file actions
354 lines (328 loc) · 12.9 KB
/
DeviceMetricsHelper.cxx
File metadata and controls
354 lines (328 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "Framework/DeviceMetricsHelper.h"
#include "Framework/DriverInfo.h"
#include "Framework/RuntimeError.h"
#include <cassert>
#include <cinttypes>
#include <cstdlib>
#include <algorithm>
#include <regex>
#include <string_view>
#include <tuple>
#include <iostream>
#include <limits>
#include <unordered_set>
namespace o2::framework
{
// Parses a metric in the form
//
// [METRIC] <name>,<type> <value> <timestamp> [<tag>,<tag>]
bool DeviceMetricsHelper::parseMetric(std::string_view const s, ParsedMetricMatch& match)
{
/// Must start with "[METRIC] "
/// 012345678
constexpr size_t PREFIXSIZE = 9;
if (s.size() < PREFIXSIZE) {
return false;
}
if (memcmp(s.data(), "[METRIC] ", 9) != 0) {
return false;
}
char const* comma; // first comma
char const* spaces[256]; // list of spaces
char const** space = spaces; // first element to fill
comma = (char const*)memchr((void*)s.data(), ',', s.size());
if (comma == nullptr) {
return false;
}
// Find all spaces
char const* nextSpace = s.data();
while (space - spaces < 256) {
*space = strchr(nextSpace, ' ');
if (*space == nullptr) {
break;
}
nextSpace = *space + 1;
space++;
}
// First space should always come before comma
if (spaces[0] > comma) {
return false;
}
match.beginKey = spaces[0] + 1;
match.endKey = comma;
// type is alway 1 char after the comma, followed by space
if ((spaces[1] - comma) != 2) {
return false;
}
char* ep = nullptr;
match.type = static_cast<MetricType>(strtol(comma + 1, &ep, 10));
if (ep != spaces[1]) {
return false;
}
// We need at least 4 spaces
if (space - spaces < 4) {
return false;
}
// Value is between the second and the last but one space
switch (match.type) {
case MetricType::Int:
match.intValue = strtol(spaces[1] + 1, &ep, 10);
if (ep != *(space - 2)) {
return false;
}
match.floatValue = (float)match.intValue;
break;
case MetricType::Float:
match.floatValue = strtof(spaces[1] + 1, &ep);
if (ep != *(space - 2)) {
return false;
}
break;
case MetricType::String:
match.beginStringValue = spaces[1] + 1;
match.endStringValue = *(space - 2);
match.floatValue = 0;
break;
case MetricType::Uint64:
match.uint64Value = strtoul(spaces[1] + 1, &ep, 10);
if (ep != *(space - 2)) {
return false;
}
match.floatValue = (float)match.uint64Value;
break;
default:
return false;
}
// Timestamp is between the last but one and the last space
match.timestamp = strtol(*(space - 2) + 1, &ep, 10);
if (ep != *(space - 1)) {
return false;
}
return true;
}
size_t DeviceMetricsHelper::bookMetricInfo(DeviceMetricsInfo& info, char const* name)
{
// Add the index by name in the correct position
// this will require moving the tail of the index,
// but inserting should happen only once for each metric,
// so who cares.
// Add the the actual Metric info to the store
MetricLabel metricLabel;
strncpy(metricLabel.label, name, MetricLabel::MAX_METRIC_LABEL_SIZE - 1);
metricLabel.label[MetricLabel::MAX_METRIC_LABEL_SIZE - 1] = '\0';
metricLabel.size = strlen(metricLabel.label);
// Find the insertion point for the sorted index
auto cmpFn = [namePtr = metricLabel.label,
&labels = info.metricLabels,
nameSize = metricLabel.size](MetricLabelIndex const& a, MetricLabelIndex const& b)
-> bool {
return strncmp(labels[a.index].label, namePtr, nameSize) < 0;
};
auto mi = std::lower_bound(info.metricLabelsAlphabeticallySortedIdx.begin(),
info.metricLabelsAlphabeticallySortedIdx.end(),
MetricLabelIndex{},
cmpFn);
// If it was already there, return the old index.
if (mi != info.metricLabelsAlphabeticallySortedIdx.end() && (strncmp(info.metricLabels[mi->index].label, metricLabel.label, std::min((size_t)metricLabel.size, (size_t)MetricLabel::MAX_METRIC_LABEL_SIZE - 1)) == 0)) {
return mi->index;
}
// Add the the actual Metric info to the store
auto metricIndex = info.metrics.size();
// Insert the sorted location where it belongs to.
MetricLabelIndex metricLabelIdx{metricIndex};
info.metricLabelsAlphabeticallySortedIdx.insert(mi, metricLabelIdx);
info.metrics.push_back(MetricInfo{});
// Create a new metric
auto& metricInfo = info.metrics.back();
metricInfo.pos = 0;
metricInfo.filledMetrics = 0;
// Add the timestamp buffer for it
info.timestamps.emplace_back(std::array<size_t, 1024>{});
info.max.push_back(std::numeric_limits<float>::lowest());
info.min.push_back(std::numeric_limits<float>::max());
info.average.push_back(0);
info.maxDomain.push_back(std::numeric_limits<size_t>::lowest());
info.minDomain.push_back(std::numeric_limits<size_t>::max());
info.changed.push_back(true);
info.metricLabels.push_back(metricLabel);
return metricIndex;
}
bool DeviceMetricsHelper::processMetric(ParsedMetricMatch& match,
DeviceMetricsInfo& info,
DeviceMetricsHelper::NewMetricCallback newMetricsCallback)
{
// get the type
size_t metricIndex = -1;
StringMetric stringValue;
switch (match.type) {
case MetricType::Float:
case MetricType::Int:
case MetricType::Uint64:
break;
case MetricType::String: {
auto lastChar = std::min(match.endStringValue - match.beginStringValue, StringMetric::MAX_SIZE - 1);
memcpy(stringValue.data, match.beginStringValue, lastChar);
stringValue.data[lastChar] = '\0';
} break;
default:
return false;
break;
};
// Find the metric based on the label. Create it if not found.
auto cmpFn = [namePtr = match.beginKey,
&labels = info.metricLabels,
nameSize = match.endKey - match.beginKey](MetricLabelIndex const& a, MetricLabelIndex const& b)
-> bool {
return strncmp(labels[a.index].label, namePtr, nameSize) < 0;
};
auto mi = std::lower_bound(info.metricLabelsAlphabeticallySortedIdx.begin(),
info.metricLabelsAlphabeticallySortedIdx.end(),
MetricLabelIndex{},
cmpFn);
// We could not find the metric, lets insert a new one.
auto matchSize = match.endKey - match.beginKey;
if (mi == info.metricLabelsAlphabeticallySortedIdx.end() || (strncmp(info.metricLabels[mi->index].label, match.beginKey, std::min(matchSize, (long)MetricLabel::MAX_METRIC_LABEL_SIZE - 1)) != 0)) {
// Create a new metric
MetricInfo metricInfo;
metricInfo.pos = 0;
metricInfo.type = match.type;
metricInfo.filledMetrics = 0;
// Add a new empty buffer for it of the correct kind
switch (match.type) {
case MetricType::Int:
metricInfo.storeIdx = info.intMetrics.size();
info.intMetrics.emplace_back(std::array<int, 1024>{});
break;
case MetricType::String:
metricInfo.storeIdx = info.stringMetrics.size();
info.stringMetrics.emplace_back(std::array<StringMetric, 32>{});
break;
case MetricType::Float:
metricInfo.storeIdx = info.floatMetrics.size();
info.floatMetrics.emplace_back(std::array<float, 1024>{});
break;
case MetricType::Uint64:
metricInfo.storeIdx = info.uint64Metrics.size();
info.uint64Metrics.emplace_back(std::array<uint64_t, 1024>{});
break;
default:
return false;
};
// Add the timestamp buffer for it
info.timestamps.emplace_back(std::array<size_t, 1024>{});
info.max.push_back(std::numeric_limits<float>::lowest());
info.min.push_back(std::numeric_limits<float>::max());
info.average.push_back(0);
info.maxDomain.push_back(std::numeric_limits<size_t>::lowest());
info.minDomain.push_back(std::numeric_limits<size_t>::max());
info.changed.push_back(false);
// Add the index by name in the correct position
// this will require moving the tail of the index,
// but inserting should happen only once for each metric,
// so who cares.
MetricLabel metricLabel;
auto lastChar = std::min(match.endKey - match.beginKey, (ptrdiff_t)MetricLabel::MAX_METRIC_LABEL_SIZE - 1);
memcpy(metricLabel.label, match.beginKey, lastChar);
metricLabel.label[lastChar] = '\0';
metricLabel.size = lastChar;
MetricLabelIndex metricLabelIdx;
metricLabelIdx.index = info.metrics.size();
info.metricLabels.push_back(metricLabel);
info.metricLabelsAlphabeticallySortedIdx.insert(mi, metricLabelIdx);
// Add the the actual Metric info to the store
metricIndex = info.metrics.size();
assert(metricInfo.storeIdx != -1);
assert(metricLabel.label[0] != '\0');
if (newMetricsCallback != nullptr) {
newMetricsCallback(metricLabel.label, metricInfo, match.intValue, metricIndex);
}
info.metrics.push_back(metricInfo);
} else {
metricIndex = mi->index;
}
assert(metricIndex != -1);
// We are now guaranteed our metric is present at metricIndex.
MetricInfo& metricInfo = info.metrics[metricIndex];
// auto mod = info.timestamps[metricIndex].size();
auto sizeOfCollection = 0;
switch (metricInfo.type) {
case MetricType::Int: {
info.intMetrics[metricInfo.storeIdx][metricInfo.pos] = match.intValue;
sizeOfCollection = info.intMetrics[metricInfo.storeIdx].size();
} break;
case MetricType::String: {
info.stringMetrics[metricInfo.storeIdx][metricInfo.pos] = stringValue;
sizeOfCollection = info.stringMetrics[metricInfo.storeIdx].size();
} break;
case MetricType::Float: {
info.floatMetrics[metricInfo.storeIdx][metricInfo.pos] = match.floatValue;
sizeOfCollection = info.floatMetrics[metricInfo.storeIdx].size();
} break;
case MetricType::Uint64: {
info.uint64Metrics[metricInfo.storeIdx][metricInfo.pos] = match.uint64Value;
sizeOfCollection = info.uint64Metrics[metricInfo.storeIdx].size();
} break;
default:
return false;
break;
};
// We do all the updates here, so that not update timestamps for broken metrics
// Notice how we always fill floatValue with the float equivalent of the metric
// regardless of it's type.
info.minDomain[metricIndex] = std::min(info.minDomain[metricIndex], (size_t)match.timestamp);
info.maxDomain[metricIndex] = std::max(info.maxDomain[metricIndex], (size_t)match.timestamp);
info.max[metricIndex] = std::max(info.max[metricIndex], match.floatValue);
info.min[metricIndex] = std::min(info.min[metricIndex], match.floatValue);
auto onlineAverage = [](float nextValue, float previousAverage, float previousCount) {
return previousAverage + (nextValue - previousAverage) / (previousCount + 1);
};
info.average[metricIndex] = onlineAverage(match.floatValue, info.average[metricIndex], metricInfo.filledMetrics);
info.timestamps[metricIndex][metricInfo.pos] = match.timestamp;
// We point to the next metric
metricInfo.pos = (metricInfo.pos + 1) % sizeOfCollection;
++metricInfo.filledMetrics;
// Note that we updated a given metric.
info.changed[metricIndex] = true;
return true;
}
size_t DeviceMetricsHelper::metricIdxByName(const std::string& name, const DeviceMetricsInfo& info)
{
size_t i = 0;
while (i < info.metricLabels.size()) {
auto& metricName = info.metricLabels[i];
// We check the size first and then the last character because that's
// likely to be different for multi-index metrics
if (metricName.size == name.size() && metricName.label[metricName.size - 1] == name[metricName.size - 1] && memcmp(metricName.label, name.c_str(), metricName.size) == 0) {
return i;
}
++i;
}
return i;
}
void DeviceMetricsHelper::updateMetricsNames(DriverInfo& driverInfo, std::vector<DeviceMetricsInfo> const& metricsInfos)
{
// Calculate the unique set of metrics, as available in the metrics service
static std::unordered_set<std::string> allMetricsNames;
for (const auto& metricsInfo : metricsInfos) {
for (const auto& labelsPairs : metricsInfo.metricLabels) {
allMetricsNames.insert(std::string(labelsPairs.label));
}
}
for (const auto& labelsPairs : driverInfo.metrics.metricLabels) {
allMetricsNames.insert(std::string(labelsPairs.label));
}
std::vector<std::string> result(allMetricsNames.begin(), allMetricsNames.end());
std::sort(result.begin(), result.end());
driverInfo.availableMetrics.swap(result);
}
} // namespace o2::framework