artdaq_utilities  v1_06_03
MetricData.hh
1 #ifndef ARTDAQ_UTILITIES_PLUGINS_METRICDATA_HH
2 #define ARTDAQ_UTILITIES_PLUGINS_METRICDATA_HH
3 
4 #include "fhiclcpp/fwd.h"
5 #include "messagefacility/MessageLogger/MessageLogger.h"
6 
7 #include <atomic>
8 #include <condition_variable>
9 #include <list>
10 #include <sstream>
11 
12 namespace artdaq {
16 enum class MetricType
17 {
19  StringMetric,
20  IntMetric,
21  DoubleMetric,
22  FloatMetric,
24 };
25 
29 enum class MetricMode : uint32_t
30 {
31  None = 0x0,
32  LastPoint = 0x1,
33  Accumulate = 0x2,
34  Average = 0x4,
35  Rate = 0x8,
36  Minimum = 0x10,
38  Maximum = 0x20,
39  Persist = 0x40,
40 };
48 {
49  return static_cast<MetricMode>(static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
50 }
58 {
59  return static_cast<MetricMode>(static_cast<uint32_t>(a) & static_cast<uint32_t>(b));
60 }
61 
65 struct MetricData
66 {
71  MetricData(const MetricData& r) = default;
72 
77  MetricData(MetricData&& r) noexcept = default;
78 
84  MetricData& operator=(const MetricData& r) = default;
85 
91  MetricData& operator=(MetricData&& r) noexcept = default;
92 
96  std::string Name;
100  std::string StringValue;
101 
106  {
107  int i;
108  double d;
109  float f;
110  uint64_t u;
111 
116  : i(0) {}
122  : i(v) {}
127  MetricDataValue(double v)
128  : d(v) {}
134  : f(v) {}
139  MetricDataValue(uint64_t v)
140  : u(v) {}
141  };
142 
150 
158  std::string Unit;
162  int Level;
170  std::string MetricPrefix;
178  size_t DataPointCount{0};
179 
190  MetricData(std::string const& name, std::string const& value, std::string const& unit, int level, MetricMode mode,
191  std::string const& metricPrefix, bool useNameOverride)
192  : Name(name), StringValue(value), Type(MetricType::StringMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
193 
204  MetricData(std::string const& name, int const& value, std::string const& unit, int level, MetricMode mode,
205  std::string const& metricPrefix, bool useNameOverride)
206  : Name(name), Value(value), Last(value), Min(value), Max(value), Type(MetricType::IntMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
207 
218  MetricData(std::string const& name, double const& value, std::string const& unit, int level, MetricMode mode,
219  std::string const& metricPrefix, bool useNameOverride)
220  : Name(name), Value(value), Last(value), Min(value), Max(value), Type(MetricType::DoubleMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
221 
232  MetricData(std::string const& name, float const& value, std::string const& unit, int level, MetricMode mode,
233  std::string const& metricPrefix, bool useNameOverride)
234  : Name(name), Value(value), Last(value), Min(value), Max(value), Type(MetricType::FloatMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
235 
246  MetricData(std::string const& name, uint64_t const& value, std::string const& unit, int level,
247  MetricMode mode, std::string const& metricPrefix, bool useNameOverride)
248  : Name(name), Value(value), Last(value), Min(value), Max(value), Type(MetricType::UnsignedMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
249 
254  : Name("") {}
255 
261  bool Add(MetricData other)
262  {
263  if (other.Name == Name && other.Type == Type && other.Unit == Unit && other.Level == Level)
264  {
265  if (other.DataPointCount == 0) return true;
266  if (DataPointCount == 0)
267  {
268  switch (Type)
269  {
271  StringValue = other.StringValue;
272  break;
274  Value.i = other.Value.i;
275  Last.i = other.Last.i;
276  Min.i = other.Min.i;
277  Max.i = other.Max.i;
278  break;
280  Value.d = other.Value.d;
281  Last.d = other.Last.d;
282  Min.d = other.Min.d;
283  Max.d = other.Max.d;
284  break;
286  Value.f = other.Value.f;
287  Last.f = other.Last.f;
288  Min.f = other.Min.f;
289  Max.f = other.Max.f;
290  break;
292  Value.u = other.Value.u;
293  Last.u = other.Last.u;
294  Min.u = other.Min.u;
295  Max.u = other.Max.u;
296  break;
298  break;
299  }
301  return true;
302  }
303  else
304  {
305  switch (Type)
306  {
308  StringValue += " " + other.StringValue;
309  break;
311  Value.i += other.Value.i;
312  Last.i = other.Last.i;
313  if (other.Min.i < Min.i) Min.i = other.Min.i;
314  if (other.Max.i > Max.i) Max.i = other.Max.i;
315  break;
317  Value.d += other.Value.d;
318  Last.d = other.Last.d;
319  if (other.Min.d < Min.d) Min.d = other.Min.d;
320  if (other.Max.d > Max.d) Max.d = other.Max.d;
321  break;
323  Value.f += other.Value.f;
324  Last.f = other.Last.f;
325  if (other.Min.f < Min.f) Min.f = other.Min.f;
326  if (other.Max.f > Max.f) Max.f = other.Max.f;
327  break;
329  Value.u += other.Value.u;
330  Last.u = other.Last.u;
331  if (other.Min.u < Min.u) Min.u = other.Min.u;
332  if (other.Max.u > Max.u) Max.u = other.Max.u;
333  break;
335  break;
336  }
338  return true;
339  }
340  }
341  return false;
342  }
343 
348  void AddPoint(int point)
349  {
350  Last.i = point;
351  Value.i += point;
352  DataPointCount++;
353  if (point > Max.i) Max.i = point;
354  if (point < Min.i) Min.i = point;
355  }
360  void AddPoint(double point)
361  {
362  Last.d = point;
363  Value.d += point;
364  DataPointCount++;
365  if (point > Max.d) Max.d = point;
366  if (point < Min.d) Min.d = point;
367  }
372  void AddPoint(float point)
373  {
374  Last.f = point;
375  Value.f += point;
376  DataPointCount++;
377  if (point > Max.f) Max.f = point;
378  if (point < Min.f) Min.f = point;
379  }
384  void AddPoint(uint64_t point)
385  {
386  Last.u = point;
387  Value.u += point;
388  DataPointCount++;
389  if (point > Max.u) Max.u = point;
390  if (point < Min.u) Min.u = point;
391  }
392 
398  void Reset()
399  {
400  switch (Type)
401  {
403  StringValue = "";
404  break;
406  Value.i = 0;
407  Last.i = 0;
408  Min.i = std::numeric_limits<int>::max();
409  Max.i = std::numeric_limits<int>::min();
410  break;
412  Value.d = 0;
413  Last.d = 0;
414  Min.d = std::numeric_limits<double>::max();
415  Max.d = std::numeric_limits<double>::min();
416  break;
418  Value.f = 0;
419  Last.f = 0;
420  Min.f = std::numeric_limits<float>::max();
421  Max.f = std::numeric_limits<float>::min();
422  break;
424  Value.u = 0;
425  Last.u = 0;
426  Min.u = std::numeric_limits<uint64_t>::max();
427  Max.u = std::numeric_limits<uint64_t>::min();
428  break;
430  break;
431  }
432  DataPointCount = 0;
433  }
434 };
435 } // namespace artdaq
436 
437 #endif /* ARTDAQ_UTILITIES_PLUGINS_METRICDATA_HH */
std::string StringValue
Value of the metric, if it is a MetricType::StringMetric
Definition: MetricData.hh:100
std::string MetricPrefix
Name prefix for the metric
Definition: MetricData.hh:170
MetricDataValue(double v)
Construct a MetricDataValue as double
Definition: MetricData.hh:127
MetricData(std::string const &name, uint64_t const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix, bool useNameOverride)
Construct a MetricData point using a uint64_t value
Definition: MetricData.hh:246
MetricDataValue(float v)
Construct a MetricDataValue as fload
Definition: MetricData.hh:133
size_t DataPointCount
Number of data points accumulated in this MetricData
Definition: MetricData.hh:178
Report the sum of all values. Use for counters to report accurate results.
std::string Unit
Units of the metric
Definition: MetricData.hh:158
float f
Value of the metric, if it is a MetricType::FloatMetric.
Definition: MetricData.hh:109
MetricDataValue()
Construct a MetricDataValue
Definition: MetricData.hh:115
Metric is a std::string (not in union)
std::string Name
Name of the metric
Definition: MetricData.hh:96
MetricData(std::string const &name, std::string const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix, bool useNameOverride)
Construct a MetricData point using a string value
Definition: MetricData.hh:190
bool UseNameOverride
Whether to override the default naming convention for this metric
Definition: MetricData.hh:174
MetricMode Mode
Accumulation mode of the metric
Definition: MetricData.hh:166
bool Add(MetricData other)
Add two MetricData instances together
Definition: MetricData.hh:261
constexpr MetricMode operator&(MetricMode a, MetricMode b)
Bitwise AND operator for MetricMode
Definition: MetricData.hh:57
MetricType
This enumeration is used to identify the type of the metric instance (which value should be extraced ...
Definition: MetricData.hh:16
MetricDataValue Max
Maximum recorded vaule of this MetricData.
Definition: MetricData.hh:149
MetricData(std::string const &name, double const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix, bool useNameOverride)
Construct a MetricData point using a double value
Definition: MetricData.hh:218
void AddPoint(uint64_t point)
Add an uint64_t point to this MetricData
Definition: MetricData.hh:384
Reports the minimum value recorded.
Metric is a long unsigned int.
over. Use to create rates from counters.
void AddPoint(int point)
Add an integer point to this MetricData
Definition: MetricData.hh:348
Report only the last value recorded. Useful for event counters, run numbers, etc. ...
Repots the maximum value recorded.
MetricData(std::string const &name, float const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix, bool useNameOverride)
Construct a MetricData point using a float value
Definition: MetricData.hh:232
MetricDataValue Value
Accumulated value of this MetricData
Definition: MetricData.hh:146
int i
Value of the metric, if it is a MetricType::IntMetric.
Definition: MetricData.hh:107
MetricDataValue(uint64_t v)
Construct a MetricDataValue as unsigned int
Definition: MetricData.hh:139
Keep previous metric value in memory.
constexpr MetricMode operator|(MetricMode a, MetricMode b)
Bitwise OR operator for MetricMode
Definition: MetricData.hh:47
MetricType Type
Type of the metric
Definition: MetricData.hh:154
MetricMode
The Mode of the metric indicates how multiple metric values should be combined within a reporting int...
Definition: MetricData.hh:29
void AddPoint(double point)
Add a double point to this MetricData
Definition: MetricData.hh:360
MetricDataValue(int v)
Construct a MetricDataValue as integer
Definition: MetricData.hh:121
This union holds the values for all other metric types
Definition: MetricData.hh:105
MetricDataValue Last
Last value of this MetricData.
Definition: MetricData.hh:147
void Reset()
Reset this MetricData instance to the initial state
Definition: MetricData.hh:398
void AddPoint(float point)
Add a float point to this MetricData
Definition: MetricData.hh:372
MetricData()
Default constructor, constructs an MetricType::InvalidMetric
Definition: MetricData.hh:253
MetricData & operator=(const MetricData &r)=default
Default copy assignment operator
Small structure used to hold a metric data point before sending to the metric plugins ...
Definition: MetricData.hh:65
MetricData(std::string const &name, int const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix, bool useNameOverride)
Construct a MetricData point using a int value
Definition: MetricData.hh:204
Default, invalid value.
uint64_t u
Value of the metric, if it is a MetricType::UnsignedMetric.
Definition: MetricData.hh:110
Report the average of all values. Use for rates to report accurate results.
MetricDataValue Min
Minimum recorded value of this MetricData.
Definition: MetricData.hh:148
double d
Value of the metric, if it is a MetricType::DoubleMetric.
Definition: MetricData.hh:108
int Level
Reporting level of the metric
Definition: MetricData.hh:162