artdaq_utilities  v1_02_04a
 All Classes Namespaces Functions Variables Typedefs
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 
33 {
34 public:
38  MetricManager();
39 
43  MetricManager(MetricManager const&) = delete;
44 
50  virtual ~MetricManager() noexcept;
51 
56  MetricManager& operator=(MetricManager const&) = delete;
57 
66  void initialize(fhicl::ParameterSet const& pset, std::string prefix = "");
67 
71  void do_start();
72 
76  void do_stop();
77 
81  void do_pause();
82 
86  void do_resume();
87 
95  void reinitialize(fhicl::ParameterSet const& pset, std::string prefix = "");
96 
100  void shutdown();
101 
112  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);
113 
124  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);
125 
136  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);
137 
148  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);
149 
160  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);
161 
166  void setPrefix(std::string prefix) { prefix_ = prefix; }
167 
168 private:
169  void sendMetricLoop_();
170 
171  void startMetricLoop_();
172 
173  std::vector<std::unique_ptr<artdaq::MetricPlugin>> metric_plugins_;
174  std::thread metric_sending_thread_;
175  std::mutex metric_mutex_;
176  std::condition_variable metric_cv_;
177 
178  bool initialized_;
179  bool running_;
180  bool active_;
181  std::string prefix_;
182 
183 
184  struct MetricData
185  {
186  MetricData(const MetricData&) = default;
187 
188  MetricData(MetricData&&) noexcept = default;
189 
190  MetricData& operator=(const MetricData&) = default;
191 
192  MetricData& operator=(MetricData&&) noexcept = default;
193 
194  std::string name_;
195  std::string stringValue_;
196 
197  union
198  {
199  int intValue_;
200  double doubleValue_;
201  float floatValue_;
202  long unsigned int unsignedValue_;
203  };
204 
205  enum MetricType
206  {
207  InvalidMetric,
208  StringMetric,
209  IntMetric,
210  DoubleMetric,
211  FloatMetric,
212  UnsignedMetric
213  };
214 
215  MetricType type_;
216  std::string unit_;
217  int level_;
218  bool accumulate_;
219  std::string metricPrefix_;
220  bool useNameOverride_;
221 
222  MetricData(std::string const& name, std::string const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
223  : name_(name)
224  , stringValue_(value)
225  , type_(StringMetric)
226  , unit_(unit)
227  , level_(level)
228  , accumulate_(accumulate)
229  , metricPrefix_(metricPrefix)
230  , useNameOverride_(useNameOverride) {}
231 
232  MetricData(std::string const& name, int const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
233  : name_(name)
234  , intValue_(value)
235  , type_(IntMetric)
236  , unit_(unit)
237  , level_(level)
238  , accumulate_(accumulate)
239  , metricPrefix_(metricPrefix)
240  , useNameOverride_(useNameOverride) {}
241 
242  MetricData(std::string const& name, double const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
243  : name_(name)
244  , doubleValue_(value)
245  , type_(DoubleMetric)
246  , unit_(unit)
247  , level_(level)
248  , accumulate_(accumulate)
249  , metricPrefix_(metricPrefix)
250  , useNameOverride_(useNameOverride) {}
251 
252  MetricData(std::string const& name, float const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
253  : name_(name)
254  , floatValue_(value)
255  , type_(FloatMetric)
256  , unit_(unit)
257  , level_(level)
258  , accumulate_(accumulate)
259  , metricPrefix_(metricPrefix)
260  , useNameOverride_(useNameOverride) {}
261 
262  MetricData(std::string const& name, long unsigned int const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
263  : name_(name)
264  , unsignedValue_(value)
265  , type_(UnsignedMetric)
266  , unit_(unit)
267  , level_(level)
268  , accumulate_(accumulate)
269  , metricPrefix_(metricPrefix)
270  , useNameOverride_(useNameOverride) {}
271 
272  MetricData() : name_("")
273  , type_(InvalidMetric) {}
274  };
275 
276  std::list<std::unique_ptr<MetricData>> metric_queue_;
277  std::mutex metric_queue_mutex_;
278 };
279 
280 #endif /* artdaq_DAQrate_MetricManager_hh */
void reinitialize(fhicl::ParameterSet const &pset, std::string prefix="")
Reinitialize all MetricPlugin Instances.
void shutdown()
Call the destructors for all configured MetricPlugin instances.
void initialize(fhicl::ParameterSet const &pset, std::string prefix="")
Initialize the MetricPlugin instances.
MetricManager()
Construct an instance of the MetricManager class.
void do_start()
Perform startup actions for each configured MetricPlugin.
void do_stop()
Stop sending metrics to the MetricPlugin instances.
virtual ~MetricManager() noexcept
MetricManager destructor.
The MetricManager class handles loading metric plugins and asynchronously sending metric data to them...
MetricManager & operator=(MetricManager const &)=delete
Copy Assignment operator is deleted.
void setPrefix(std::string prefix)
Sets the prefix prepended to all metrics without useNameOverride set.
void do_resume()
Resume metric sending. Currently a No-Op.
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)
Send a metric with the given parameters to any MetricPlugins with a threshold level &gt;= to level...
void do_pause()
Pause metric sending. Currently a No-Op.