artdaq_utilities  v1_02_02
 All Classes
MetricManager.hh
1 #ifndef artdaq_DAQrate_MetricManager_hh
2 #define artdaq_DAQrate_MetricManager_hh
3 
4 // MetricManager class definition file
5 // Author: Eric Flumerfelt
6 // Last Modified: 11/14/2014
7 //
8 // MetricManager loads a user-specified set of plugins, sends them their configuration,
9 // and sends them data as it is recieved. It also maintains the state of the plugins
10 // relative to the application state.
11 
12 #include "artdaq-utilities/Plugins/MetricPlugin.hh"
13 #include "fhiclcpp/fwd.h"
14 #include "messagefacility/MessageLogger/MessageLogger.h"
15 
16 #include <sstream>
17 #include <list>
18 #include <thread>
19 #include <condition_variable>
20 #include <atomic>
21 
22 namespace artdaq
23 {
24  class MetricManager;
25 }
26 
28 {
29 public:
30  MetricManager();
31 
32  MetricManager(MetricManager const&) = delete;
33 
34  virtual ~MetricManager() noexcept;
35 
36  MetricManager& operator=(MetricManager const&) = delete;
37 
38  void initialize(fhicl::ParameterSet const&, std::string prefix = "");
39 
40  void do_start();
41 
42  void do_stop();
43 
44  void do_pause();
45 
46  void do_resume();
47 
48  void reinitialize(fhicl::ParameterSet const&, std::string prefix = "");
49 
50  void shutdown();
51 
52  void sendMetric(std::string const& name, std::string const& value, std::string const& unit, int level, bool accumulate = true, std::string const& metricPrefix = "", bool useNameOverride = false);
53 
54  void sendMetric(std::string const& name, int const& value, std::string const& unit, int level, bool accumulate = true, std::string const& metricPrefix = "", bool useNameOverride = false);
55 
56  void sendMetric(std::string const& name, double const& value, std::string const& unit, int level, bool accumulate = true, std::string const& metricPrefix = "", bool useNameOverride = false);
57 
58  void sendMetric(std::string const& name, float const& value, std::string const& unit, int level, bool accumulate = true, std::string const& metricPrefix = "", bool useNameOverride = false);
59 
60  void sendMetric(std::string const& name, long unsigned int const& value, std::string const& unit, int level, bool accumulate = true, std::string const& metricPrefix = "", bool useNameOverride = false);
61 
62  void setPrefix(std::string prefix) { prefix_ = prefix; }
63 
64 private:
65  void sendMetricLoop_();
66 
67  void startMetricLoop_();
68 
69  std::vector<std::unique_ptr<artdaq::MetricPlugin>> metric_plugins_;
70  std::thread metric_sending_thread_;
71  std::mutex metric_mutex_;
72  std::condition_variable metric_cv_;
73 
74  bool initialized_;
75  bool running_;
76  bool active_;
77  std::string prefix_;
78 
79 
80  struct MetricData
81  {
82  MetricData(const MetricData&) = default;
83 
84  MetricData(MetricData&&) noexcept = default;
85 
86  MetricData& operator=(const MetricData&) = default;
87 
88  MetricData& operator=(MetricData&&) noexcept = default;
89 
90  std::string name_;
91  std::string stringValue_;
92 
93  union
94  {
95  int intValue_;
96  double doubleValue_;
97  float floatValue_;
98  long unsigned int unsignedValue_;
99  };
100 
101  enum MetricType
102  {
103  InvalidMetric,
104  StringMetric,
105  IntMetric,
106  DoubleMetric,
107  FloatMetric,
108  UnsignedMetric
109  };
110 
111  MetricType type_;
112  std::string unit_;
113  int level_;
114  bool accumulate_;
115  std::string metricPrefix_;
116  bool useNameOverride_;
117 
118  MetricData(std::string const& name, std::string const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
119  : name_(name)
120  , stringValue_(value)
121  , type_(StringMetric)
122  , unit_(unit)
123  , level_(level)
124  , accumulate_(accumulate)
125  , metricPrefix_(metricPrefix)
126  , useNameOverride_(useNameOverride) {}
127 
128  MetricData(std::string const& name, int const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
129  : name_(name)
130  , intValue_(value)
131  , type_(IntMetric)
132  , unit_(unit)
133  , level_(level)
134  , accumulate_(accumulate)
135  , metricPrefix_(metricPrefix)
136  , useNameOverride_(useNameOverride) {}
137 
138  MetricData(std::string const& name, double const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
139  : name_(name)
140  , doubleValue_(value)
141  , type_(DoubleMetric)
142  , unit_(unit)
143  , level_(level)
144  , accumulate_(accumulate)
145  , metricPrefix_(metricPrefix)
146  , useNameOverride_(useNameOverride) {}
147 
148  MetricData(std::string const& name, float const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
149  : name_(name)
150  , floatValue_(value)
151  , type_(FloatMetric)
152  , unit_(unit)
153  , level_(level)
154  , accumulate_(accumulate)
155  , metricPrefix_(metricPrefix)
156  , useNameOverride_(useNameOverride) {}
157 
158  MetricData(std::string const& name, long unsigned int const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
159  : name_(name)
160  , unsignedValue_(value)
161  , type_(UnsignedMetric)
162  , unit_(unit)
163  , level_(level)
164  , accumulate_(accumulate)
165  , metricPrefix_(metricPrefix)
166  , useNameOverride_(useNameOverride) {}
167 
168  MetricData() : name_("")
169  , type_(InvalidMetric) {}
170  };
171 
172  std::list<std::unique_ptr<MetricData>> metric_queue_;
173  std::mutex metric_queue_mutex_;
174 };
175 
176 #endif /* artdaq_DAQrate_MetricManager_hh */