-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathDeviceMetricsHelper.cxx
More file actions
312 lines (292 loc) · 11.5 KB
/
DeviceMetricsHelper.cxx
File metadata and controls
312 lines (292 loc) · 11.5 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
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// 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/RuntimeError.h"
#include <cassert>
#include <cinttypes>
#include <cstdlib>
#include <algorithm>
#include <regex>
#include <string_view>
#include <tuple>
#include <iostream>
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;
}
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);
break;
case MetricType::Uint64:
match.uint64Value = strtoul(spaces[1] + 1, &ep, 10);
if (ep != *(space - 2)) {
return false;
}
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& metrics, char const* name)
{
// Add the the actual Metric info to the store
auto metricIndex = metrics.metrics.size();
metrics.metrics.push_back(MetricInfo{});
// Create a new metric
auto& metricInfo = metrics.metrics.back();
metricInfo.pos = 0;
metricInfo.filledMetrics = 0;
// Add the timestamp buffer for it
metrics.timestamps.emplace_back(std::array<size_t, 1024>{});
metrics.max.push_back(std::numeric_limits<float>::lowest());
metrics.min.push_back(std::numeric_limits<float>::max());
metrics.maxDomain.push_back(std::numeric_limits<size_t>::lowest());
metrics.minDomain.push_back(std::numeric_limits<size_t>::max());
metrics.changed.push_back(true);
// 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
MetricLabelIndex metricLabelIdx;
strncpy(metricLabelIdx.label, name, MetricLabelIndex::MAX_METRIC_LABEL_SIZE - 1);
metricLabelIdx.label[MetricLabelIndex::MAX_METRIC_LABEL_SIZE - 1] = '\0';
metricLabelIdx.index = metricIndex;
metricLabelIdx.size = strlen(metricLabelIdx.label);
metrics.metricLabelsIdx.push_back(metricLabelIdx);
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,
nameSize = match.endKey - match.beginKey](MetricLabelIndex const& a, MetricLabelIndex const& b)
-> bool {
return strncmp(a.label, namePtr, nameSize) < 0;
};
auto mi = std::lower_bound(info.metricLabelsIdx.begin(),
info.metricLabelsIdx.end(),
MetricLabelIndex{},
cmpFn);
// We could not find the metric, lets insert a new one.
if (mi == info.metricLabelsIdx.end() || (strncmp(mi->label, match.beginKey, std::min(match.endKey - match.beginKey, (long)MetricLabelIndex::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.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.
MetricLabelIndex metricLabelIdx;
auto lastChar = std::min(match.endKey - match.beginKey, (ptrdiff_t)MetricLabelIndex::MAX_METRIC_LABEL_SIZE - 1);
memcpy(metricLabelIdx.label, match.beginKey, lastChar);
metricLabelIdx.label[lastChar] = '\0';
metricLabelIdx.size = lastChar;
metricLabelIdx.index = info.metrics.size();
info.metricLabelsIdx.insert(mi, metricLabelIdx);
// Add the the actual Metric info to the store
metricIndex = info.metrics.size();
assert(metricInfo.storeIdx != -1);
assert(metricLabelIdx.label[0] != '\0');
if (newMetricsCallback != nullptr) {
newMetricsCallback(metricLabelIdx.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();
info.minDomain[metricIndex] = std::min(info.minDomain[metricIndex], (size_t)match.timestamp);
info.maxDomain[metricIndex] = std::max(info.maxDomain[metricIndex], (size_t)match.timestamp);
switch (metricInfo.type) {
case MetricType::Int: {
info.intMetrics[metricInfo.storeIdx][metricInfo.pos] = match.intValue;
info.max[metricIndex] = std::max(info.max[metricIndex], (float)match.intValue);
info.min[metricIndex] = std::min(info.min[metricIndex], (float)match.intValue);
// Save the timestamp for the current metric we do it here
// so that we do not update timestamps for broken metrics
info.timestamps[metricIndex][metricInfo.pos] = match.timestamp;
// Update the position where to write the next metric
metricInfo.pos = (metricInfo.pos + 1) % info.intMetrics[metricInfo.storeIdx].size();
++metricInfo.filledMetrics;
} break;
case MetricType::String: {
info.stringMetrics[metricInfo.storeIdx][metricInfo.pos] = stringValue;
// Save the timestamp for the current metric we do it here
// so that we do not update timestamps for broken metrics
info.timestamps[metricIndex][metricInfo.pos] = match.timestamp;
metricInfo.pos = (metricInfo.pos + 1) % info.stringMetrics[metricInfo.storeIdx].size();
++metricInfo.filledMetrics;
} break;
case MetricType::Float: {
info.floatMetrics[metricInfo.storeIdx][metricInfo.pos] = match.floatValue;
info.max[metricIndex] = std::max(info.max[metricIndex], match.floatValue);
info.min[metricIndex] = std::min(info.min[metricIndex], match.floatValue);
// Save the timestamp for the current metric we do it here
// so that we do not update timestamps for broken metrics
info.timestamps[metricIndex][metricInfo.pos] = match.timestamp;
metricInfo.pos = (metricInfo.pos + 1) % info.floatMetrics[metricInfo.storeIdx].size();
++metricInfo.filledMetrics;
} break;
case MetricType::Uint64: {
info.uint64Metrics[metricInfo.storeIdx][metricInfo.pos] = match.uint64Value;
info.max[metricIndex] = std::max(info.max[metricIndex], (float)match.uint64Value);
info.min[metricIndex] = std::min(info.min[metricIndex], (float)match.uint64Value);
// Save the timestamp for the current metric we do it here
// so that we do not update timestamps for broken metrics
info.timestamps[metricIndex][metricInfo.pos] = match.timestamp;
// Update the position where to write the next metric
metricInfo.pos = (metricInfo.pos + 1) % info.uint64Metrics[metricInfo.storeIdx].size();
++metricInfo.filledMetrics;
} break;
default:
return false;
break;
};
// 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.metricLabelsIdx.size()) {
auto& metricName = info.metricLabelsIdx[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 metricName.index;
}
++i;
}
return i;
}
} // namespace o2::framework