artdaq_utilities  v1_08_03
MetricData.hh
1 #ifndef ARTDAQ_UTILITIES_PLUGINS_METRICDATA_HH
2 #define ARTDAQ_UTILITIES_PLUGINS_METRICDATA_HH
3 
4 #include <atomic>
5 #include <condition_variable>
6 #include <limits>
7 #include <list>
8 #include <sstream>
9 
10 namespace artdaq {
14 enum class MetricType
15 {
17  StringMetric,
18  IntMetric,
19  DoubleMetric,
20  FloatMetric,
22 };
23 
27 enum class MetricMode : uint32_t
28 {
29  None = 0x0,
30  LastPoint = 0x1,
31  Accumulate = 0x2,
32  Average = 0x4,
33  Rate = 0x8,
34  Minimum = 0x10,
36  Maximum = 0x20,
37  Persist = 0x40,
38 };
46 {
47  return static_cast<MetricMode>(static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
48 }
56 {
57  return static_cast<MetricMode>(static_cast<uint32_t>(a) & static_cast<uint32_t>(b));
58 }
59 
63 struct MetricData
64 {
69  MetricData(const MetricData& r) = default;
70 
75  MetricData(MetricData&& r) noexcept = default;
76 
82  MetricData& operator=(const MetricData& r) = default;
83 
89  MetricData& operator=(MetricData&& r) noexcept = default;
90 
94  std::string Name;
98  std::string StringValue;
99 
104  {
105  int i;
106  double d;
107  float f;
108  uint64_t u;
109 
114  : i(0) {}
120  : i(v) {}
125  MetricDataValue(double v)
126  : d(v) {}
132  : f(v) {}
137  MetricDataValue(uint64_t v)
138  : u(v) {}
139  };
140 
148 
156  std::string Unit;
160  int Level;
168  std::string MetricPrefix;
176  size_t DataPointCount{0};
177 
188  MetricData(std::string const& name, std::string const& value, std::string const& unit, int level, MetricMode mode,
189  std::string const& metricPrefix, bool useNameOverride)
190  : Name(name), StringValue(value), Type(MetricType::StringMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
191 
202  MetricData(std::string const& name, int const& value, std::string const& unit, int level, MetricMode mode,
203  std::string const& metricPrefix, bool useNameOverride)
204  : 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) {}
205 
216  MetricData(std::string const& name, double const& value, std::string const& unit, int level, MetricMode mode,
217  std::string const& metricPrefix, bool useNameOverride)
218  : 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) {}
219 
230  MetricData(std::string const& name, float const& value, std::string const& unit, int level, MetricMode mode,
231  std::string const& metricPrefix, bool useNameOverride)
232  : 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) {}
233 
244  MetricData(std::string const& name, uint64_t const& value, std::string const& unit, int level,
245  MetricMode mode, std::string const& metricPrefix, bool useNameOverride)
246  : 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) {}
247 
252  : Name("") {}
253 
259  bool Add(MetricData other)
260  {
261  if (other.Name == Name && other.Type == Type && other.Unit == Unit && other.Level == Level)
262  {
263  if (other.DataPointCount == 0) return true;
264  if (DataPointCount == 0)
265  {
266  switch (Type)
267  {
269  StringValue = other.StringValue;
270  break;
272  Value.i = other.Value.i;
273  Last.i = other.Last.i;
274  Min.i = other.Min.i;
275  Max.i = other.Max.i;
276  break;
278  Value.d = other.Value.d;
279  Last.d = other.Last.d;
280  Min.d = other.Min.d;
281  Max.d = other.Max.d;
282  break;
284  Value.f = other.Value.f;
285  Last.f = other.Last.f;
286  Min.f = other.Min.f;
287  Max.f = other.Max.f;
288  break;
290  Value.u = other.Value.u;
291  Last.u = other.Last.u;
292  Min.u = other.Min.u;
293  Max.u = other.Max.u;
294  break;
296  break;
297  }
299  return true;
300  }
301  else
302  {
303  switch (Type)
304  {
306  StringValue += " " + other.StringValue;
307  break;
309  Value.i += other.Value.i;
310  Last.i = other.Last.i;
311  if (other.Min.i < Min.i) Min.i = other.Min.i;
312  if (other.Max.i > Max.i) Max.i = other.Max.i;
313  break;
315  Value.d += other.Value.d;
316  Last.d = other.Last.d;
317  if (other.Min.d < Min.d) Min.d = other.Min.d;
318  if (other.Max.d > Max.d) Max.d = other.Max.d;
319  break;
321  Value.f += other.Value.f;
322  Last.f = other.Last.f;
323  if (other.Min.f < Min.f) Min.f = other.Min.f;
324  if (other.Max.f > Max.f) Max.f = other.Max.f;
325  break;
327  Value.u += other.Value.u;
328  Last.u = other.Last.u;
329  if (other.Min.u < Min.u) Min.u = other.Min.u;
330  if (other.Max.u > Max.u) Max.u = other.Max.u;
331  break;
333  break;
334  }
336  return true;
337  }
338  }
339  return false;
340  }
341 
346  void AddPoint(int point)
347  {
348  Last.i = point;
349  Value.i += point;
350  DataPointCount++;
351  if (point > Max.i) Max.i = point;
352  if (point < Min.i) Min.i = point;
353  }
358  void AddPoint(double point)
359  {
360  Last.d = point;
361  Value.d += point;
362  DataPointCount++;
363  if (point > Max.d) Max.d = point;
364  if (point < Min.d) Min.d = point;
365  }
370  void AddPoint(float point)
371  {
372  Last.f = point;
373  Value.f += point;
374  DataPointCount++;
375  if (point > Max.f) Max.f = point;
376  if (point < Min.f) Min.f = point;
377  }
382  void AddPoint(uint64_t point)
383  {
384  Last.u = point;
385  Value.u += point;
386  DataPointCount++;
387  if (point > Max.u) Max.u = point;
388  if (point < Min.u) Min.u = point;
389  }
390 
396  void Reset()
397  {
398  switch (Type)
399  {
401  StringValue = "";
402  break;
404  Value.i = 0;
405  Last.i = 0;
406  Min.i = std::numeric_limits<int>::max();
407  Max.i = std::numeric_limits<int>::min();
408  break;
410  Value.d = 0;
411  Last.d = 0;
412  Min.d = std::numeric_limits<double>::max();
413  Max.d = std::numeric_limits<double>::min();
414  break;
416  Value.f = 0;
417  Last.f = 0;
418  Min.f = std::numeric_limits<float>::max();
419  Max.f = std::numeric_limits<float>::min();
420  break;
422  Value.u = 0;
423  Last.u = 0;
424  Min.u = std::numeric_limits<uint64_t>::max();
425  Max.u = std::numeric_limits<uint64_t>::min();
426  break;
428  break;
429  }
430  DataPointCount = 0;
431  }
432 };
433 } // namespace artdaq
434 
435 #endif /* ARTDAQ_UTILITIES_PLUGINS_METRICDATA_HH */
std::string StringValue
Value of the metric, if it is a MetricType::StringMetric
Definition: MetricData.hh:98
std::string MetricPrefix
Name prefix for the metric
Definition: MetricData.hh:168
MetricDataValue(double v)
Construct a MetricDataValue as double
Definition: MetricData.hh:125
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:244
MetricDataValue(float v)
Construct a MetricDataValue as fload
Definition: MetricData.hh:131
size_t DataPointCount
Number of data points accumulated in this MetricData
Definition: MetricData.hh:176
Report the sum of all values. Use for counters to report accurate results.
std::string Unit
Units of the metric
Definition: MetricData.hh:156
float f
Value of the metric, if it is a MetricType::FloatMetric.
Definition: MetricData.hh:107
MetricDataValue()
Construct a MetricDataValue
Definition: MetricData.hh:113
Metric is a std::string (not in union)
std::string Name
Name of the metric
Definition: MetricData.hh:94
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:188
bool UseNameOverride
Whether to override the default naming convention for this metric
Definition: MetricData.hh:172
MetricMode Mode
Accumulation mode of the metric
Definition: MetricData.hh:164
bool Add(MetricData other)
Add two MetricData instances together
Definition: MetricData.hh:259
constexpr MetricMode operator&(MetricMode a, MetricMode b)
Bitwise AND operator for MetricMode
Definition: MetricData.hh:55
MetricType
This enumeration is used to identify the type of the metric instance (which value should be extraced ...
Definition: MetricData.hh:14
MetricDataValue Max
Maximum recorded vaule of this MetricData.
Definition: MetricData.hh:147
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:216
void AddPoint(uint64_t point)
Add an uint64_t point to this MetricData
Definition: MetricData.hh:382
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:346
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:230
MetricDataValue Value
Accumulated value of this MetricData
Definition: MetricData.hh:144
int i
Value of the metric, if it is a MetricType::IntMetric.
Definition: MetricData.hh:105
MetricDataValue(uint64_t v)
Construct a MetricDataValue as unsigned int
Definition: MetricData.hh:137
Keep previous metric value in memory.
constexpr MetricMode operator|(MetricMode a, MetricMode b)
Bitwise OR operator for MetricMode
Definition: MetricData.hh:45
MetricType Type
Type of the metric
Definition: MetricData.hh:152
MetricMode
The Mode of the metric indicates how multiple metric values should be combined within a reporting int...
Definition: MetricData.hh:27
void AddPoint(double point)
Add a double point to this MetricData
Definition: MetricData.hh:358
MetricDataValue(int v)
Construct a MetricDataValue as integer
Definition: MetricData.hh:119
This union holds the values for all other metric types
Definition: MetricData.hh:103
MetricDataValue Last
Last value of this MetricData.
Definition: MetricData.hh:145
void Reset()
Reset this MetricData instance to the initial state
Definition: MetricData.hh:396
void AddPoint(float point)
Add a float point to this MetricData
Definition: MetricData.hh:370
MetricData()
Default constructor, constructs an MetricType::InvalidMetric
Definition: MetricData.hh:251
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:63
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:202
Default, invalid value.
uint64_t u
Value of the metric, if it is a MetricType::UnsignedMetric.
Definition: MetricData.hh:108
Report the average of all values. Use for rates to report accurate results.
MetricDataValue Min
Minimum recorded value of this MetricData.
Definition: MetricData.hh:146
double d
Value of the metric, if it is a MetricType::DoubleMetric.
Definition: MetricData.hh:106
int Level
Reporting level of the metric
Definition: MetricData.hh:160