artdaq_utilities  v1_04_06
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 "artdaq-utilities/Plugins/MetricData.hh"
14 #include "fhiclcpp/fwd.h"
15 #include "fhiclcpp/types/OptionalTable.h"
16 #include "messagefacility/MessageLogger/MessageLogger.h"
17 
18 #include <sstream>
19 #include <queue>
20 #include <condition_variable>
21 #include <atomic>
22 #include <boost/thread.hpp>
23 
24 namespace artdaq
25 {
26  class MetricManager;
27 }
28 
35 {
36 public:
37  struct Config
38  {
39  fhicl::Atom<size_t> metric_queue_size{ fhicl::Name{"metric_queue_size"}, fhicl::Comment{"The maximum number of metric entries which can be stored in the metric queue."}, 1000 };
40  fhicl::Atom<size_t> metric_queue_notify_size{ fhicl::Name{"metric_queue_notify_size"}, fhicl::Comment{"The number of metric entries in the list which will cause reports of the queue size to be printed."}, 10 };
41  fhicl::Atom<int> metric_send_maximum_delay_ms{ fhicl::Name{"metric_send_maximum_delay_ms"}, fhicl::Comment{"The maximum amount of time between metric send calls (will send 0s for metrics which have not reported in this interval)"}, 15000 };
42  fhicl::OptionalTable<artdaq::MetricPlugin::Config> metricConfig{ fhicl::Name{"metricConfig"} };
43  };
44 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
45  using Parameters = fhicl::WrappedTable<Config>;
46 #endif
47 
51  MetricManager();
52 
56  MetricManager(MetricManager const&) = delete;
57 
63  virtual ~MetricManager() noexcept;
64 
69  MetricManager& operator=(MetricManager const&) = delete;
70 
83  void initialize(fhicl::ParameterSet const& pset, std::string prefix = "");
84 
88  void do_start();
89 
93  void do_stop();
94 
98  void do_pause();
99 
103  void do_resume();
104 
112  void reinitialize(fhicl::ParameterSet const& pset, std::string prefix = "");
113 
117  void shutdown();
118 
132  void sendMetric(std::string const& name, std::string const& value, std::string const& unit, int level, MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
133 
147  void sendMetric(std::string const& name, int const& value, std::string const& unit, int level, MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
148 
162  void sendMetric(std::string const& name, double const& value, std::string const& unit, int level, MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
163 
177  void sendMetric(std::string const& name, float const& value, std::string const& unit, int level, MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
178 
192  void sendMetric(std::string const& name, long unsigned int const& value, std::string const& unit, int level, MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
193 
198  void setPrefix(std::string prefix) { prefix_ = prefix; }
199 
204  bool Initialized() { return initialized_; }
205 
210  bool Running() { return running_; }
211 
216  bool Active() { return active_; }
217 
222  bool metricQueueEmpty();
223 
229  size_t metricQueueSize(std::string name = "");
230 
231 private:
232  void sendMetricLoop_();
233 
234  void startMetricLoop_();
235 
236  std::vector<std::unique_ptr<artdaq::MetricPlugin>> metric_plugins_;
237  boost::thread metric_sending_thread_;
238  std::mutex metric_mutex_;
239  std::condition_variable metric_cv_;
240  int metric_send_interval_ms_;
241 
242  bool initialized_;
243  bool running_;
244  bool active_;
245  std::string prefix_;
246 
247  //https://stackoverflow.com/questions/228908/is-listsize-really-on
248  // e10 does NOT properly have std::list::size as O(1)!!! Keep track of size separately while we still support e10.
249  typedef std::unique_ptr<MetricData> metric_data_ptr;
250  typedef std::pair<size_t, std::list<metric_data_ptr>> queue_entry;
251 
252  std::unordered_map<std::string, queue_entry> metric_queue_;
253  std::mutex metric_queue_mutex_;
254  std::atomic<size_t> missed_metric_calls_;
255  size_t metric_queue_max_size_;
256  size_t metric_queue_notify_size_;
257 };
258 
259 #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.
bool Initialized()
Returns whether the MetricManager has been initialized (configured)
void sendMetric(std::string const &name, std::string const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix="", bool useNameOverride=false)
Send a metric with the given parameters to any MetricPlugins with a threshold level &gt;= to level...
MetricMode
The Mode of the metric indicates how multiple metric values should be combined within a reporting int...
Definition: MetricData.hh:30
size_t metricQueueSize(std::string name="")
Return the size of the named metric queue
MetricManager()
Construct an instance of the MetricManager class.
void do_start()
Perform startup actions for each configured MetricPlugin.
bool Running()
Returns whether the MetricManager is running (accepting metric calls)
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...
void setPrefix(std::string prefix)
Sets the prefix prepended to all metrics without useNameOverride set.
bool Active()
Returns whether any Metric Plugins are defined and configured
void do_resume()
Resume metric sending. Currently a No-Op.
bool metricQueueEmpty()
Returns whether the metric queue is completely empty
void do_pause()
Pause metric sending. Currently a No-Op.