artdaq_utilities  v1_05_00
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 };
47 {
48  return static_cast<MetricMode>(static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
49 }
57 {
58  return static_cast<MetricMode>(static_cast<uint32_t>(a) & static_cast<uint32_t>(b));
59 }
60 
64 struct MetricData
65 {
70  MetricData(const MetricData& r) = default;
71 
76  MetricData(MetricData&& r) noexcept = default;
77 
83  MetricData& operator=(const MetricData& r) = default;
84 
90  MetricData& operator=(MetricData&& r) noexcept = default;
91 
95  std::string Name;
99  std::string StringValue;
100 
105  {
106  int i;
107  double d;
108  float f;
109  long unsigned int u;
110 
115  : i(0) {}
121  : i(v) {}
126  MetricDataValue(double v)
127  : d(v) {}
133  : f(v) {}
138  MetricDataValue(long unsigned int v)
139  : u(v) {}
140  };
141 
149 
157  std::string Unit;
161  int Level;
169  std::string MetricPrefix;
178 
189  MetricData(std::string const& name, std::string const& value, std::string const& unit, int level, MetricMode mode,
190  std::string const& metricPrefix, bool useNameOverride)
191  : Name(name), StringValue(value), Type(MetricType::StringMetric), Unit(unit), Level(level), Mode(mode), MetricPrefix(metricPrefix), UseNameOverride(useNameOverride), DataPointCount(1) {}
192 
203  MetricData(std::string const& name, int const& value, std::string const& unit, int level, MetricMode mode,
204  std::string const& metricPrefix, bool useNameOverride)
205  : 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) {}
206 
217  MetricData(std::string const& name, double const& value, std::string const& unit, int level, MetricMode mode,
218  std::string const& metricPrefix, bool useNameOverride)
219  : 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) {}
220 
231  MetricData(std::string const& name, float const& value, std::string const& unit, int level, MetricMode mode,
232  std::string const& metricPrefix, bool useNameOverride)
233  : 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) {}
234 
245  MetricData(std::string const& name, long unsigned int const& value, std::string const& unit, int level,
246  MetricMode mode, std::string const& metricPrefix, bool useNameOverride)
247  : 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) {}
248 
254 
260  bool Add(MetricData other)
261  {
262  if (other.Name == Name && other.Type == Type && other.Unit == Unit && other.Level == Level)
263  {
264  switch (Type)
265  {
267  StringValue += " " + other.StringValue;
268  break;
270  Value.i += other.Value.i;
271  Last.i = other.Last.i;
272  if (other.Min.i < Min.i) Min.i = other.Min.i;
273  if (other.Max.i > Max.i) Max.i = other.Max.i;
274  break;
276  Value.d += other.Value.d;
277  Last.d = other.Last.d;
278  if (other.Min.d < Min.d) Min.d = other.Min.d;
279  if (other.Max.d > Max.d) Max.d = other.Max.d;
280  break;
282  Value.f += other.Value.f;
283  Last.f = other.Last.f;
284  if (other.Min.f < Min.f) Min.f = other.Min.f;
285  if (other.Max.f > Max.f) Max.f = other.Max.f;
286  break;
288  Value.u += other.Value.u;
289  Last.u = other.Last.u;
290  if (other.Min.u < Min.u) Min.u = other.Min.u;
291  if (other.Max.u > Max.u) Max.u = other.Max.u;
292  break;
294  break;
295  }
297  return true;
298  }
299  return false;
300  }
301 
306  void AddPoint(int point)
307  {
308  Last.i = point;
309  Value.i += point;
310  DataPointCount++;
311  if (point > Max.i) Max.i = point;
312  if (point < Min.i) Min.i = point;
313  }
318  void AddPoint(double point)
319  {
320  Last.d = point;
321  Value.d += point;
322  DataPointCount++;
323  if (point > Max.d) Max.d = point;
324  if (point < Min.d) Min.d = point;
325  }
330  void AddPoint(float point)
331  {
332  Last.f = point;
333  Value.f += point;
334  DataPointCount++;
335  if (point > Max.f) Max.f = point;
336  if (point < Min.f) Min.f = point;
337  }
342  void AddPoint(unsigned long int point)
343  {
344  Last.u = point;
345  Value.u += point;
346  DataPointCount++;
347  if (point > Max.u) Max.u = point;
348  if (point < Min.u) Min.u = point;
349  }
350 };
351 } // namespace artdaq
352 
353 #endif /* ARTDAQ_UTILITIES_PLUGINS_METRICDATA_HH */
std::string StringValue
Value of the metric, if it is a MetricType::StringMetric
Definition: MetricData.hh:99
std::string MetricPrefix
Name prefix for the metric
Definition: MetricData.hh:169
MetricDataValue(double v)
Construct a MetricDataValue as double
Definition: MetricData.hh:126
MetricDataValue(float v)
Construct a MetricDataValue as fload
Definition: MetricData.hh:132
size_t DataPointCount
Number of data points accumulated in this MetricData
Definition: MetricData.hh:177
Report the sum of all values. Use for counters to report accurate results.
std::string Unit
Units of the metric
Definition: MetricData.hh:157
float f
Value of the metric, if it is a MetricType::FloatMetric.
Definition: MetricData.hh:108
MetricDataValue()
Construct a MetricDataValue
Definition: MetricData.hh:114
Metric is a std::string (not in union)
std::string Name
Name of the metric
Definition: MetricData.hh:95
void AddPoint(unsigned long int point)
Add an unsigned int point to this MetricData
Definition: MetricData.hh:342
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:189
MetricDataValue(long unsigned int v)
Construct a MetricDataValue as unsigned int
Definition: MetricData.hh:138
bool UseNameOverride
Whether to override the default naming convention for this metric
Definition: MetricData.hh:173
MetricMode Mode
Accumulation mode of the metric
Definition: MetricData.hh:165
bool Add(MetricData other)
Add two MetricData instances together
Definition: MetricData.hh:260
constexpr MetricMode operator&(MetricMode a, MetricMode b)
Bitwise AND operator for MetricMode
Definition: MetricData.hh:56
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:148
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:217
Reports the minimum value recorded.
Metric is a long unsigned int.
void AddPoint(int point)
Add an integer point to this MetricData
Definition: MetricData.hh:306
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:231
MetricDataValue Value
Accumulated value of this MetricData
Definition: MetricData.hh:145
int i
Value of the metric, if it is a MetricType::IntMetric.
Definition: MetricData.hh:106
constexpr MetricMode operator|(MetricMode a, MetricMode b)
Bitwise OR operator for MetricMode
Definition: MetricData.hh:46
MetricType Type
Type of the metric
Definition: MetricData.hh:153
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:318
MetricDataValue(int v)
Construct a MetricDataValue as integer
Definition: MetricData.hh:120
long unsigned int u
Value of the metric, if it is a MetricType::UnsignedMetric.
Definition: MetricData.hh:109
This union holds the values for all other metric types
Definition: MetricData.hh:104
MetricDataValue Last
Last value of this MetricData.
Definition: MetricData.hh:146
void AddPoint(float point)
Add a float point to this MetricData
Definition: MetricData.hh:330
MetricData()
Default constructor, constructs an MetricType::InvalidMetric
Definition: MetricData.hh:252
MetricData(std::string const &name, long unsigned int const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix, bool useNameOverride)
Construct a MetricData point using a unsigned long int value
Definition: MetricData.hh:245
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:64
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:203
Default, invalid value.
Report the average of all values. Use for rates to report accurate results.
MetricDataValue Min
Minimum recorded value of this MetricData.
Definition: MetricData.hh:147
double d
Value of the metric, if it is a MetricType::DoubleMetric.
Definition: MetricData.hh:107
int Level
Reporting level of the metric
Definition: MetricData.hh:161