artdaq_utilities  v1_04_10
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/MetricData.hh"
13 #include "artdaq-utilities/Plugins/MetricPlugin.hh"
14 #include "fhiclcpp/fwd.h"
15 #include "fhiclcpp/types/OptionalTable.h"
16 #include "messagefacility/MessageLogger/MessageLogger.h"
17 
18 #include <atomic>
19 #include <boost/thread.hpp>
20 #include <condition_variable>
21 #include <queue>
22 #include <sstream>
23 
24 namespace artdaq {
25  class MetricManager;
26 }
27 
34 public:
38  struct Config {
41  fhicl::Atom<size_t> metric_queue_size{
42  fhicl::Name{"metric_queue_size"},
43  fhicl::Comment{"The maximum number of metric entries which can be stored in the metric queue."}, 1000};
46  fhicl::Atom<size_t> metric_queue_notify_size{
47  fhicl::Name{"metric_queue_notify_size"},
48  fhicl::Comment{
49  "The number of metric entries in the list which will cause reports of the queue size to be printed."},
50  10};
53  fhicl::Atom<int> metric_send_maximum_delay_ms{
54  fhicl::Name{"metric_send_maximum_delay_ms"},
55  fhicl::Comment{"The maximum amount of time between metric send calls (will send 0s for metrics which have not "
56  "reported in this interval)"},
57  15000};
59  fhicl::OptionalTable<artdaq::MetricPlugin::Config> metricConfig{ fhicl::Name{"metricConfig"} };
60  };
61  using Parameters = fhicl::WrappedTable<Config>;
62 
66  MetricManager();
67 
71  MetricManager(MetricManager const&) = delete;
72 
78  virtual ~MetricManager() noexcept;
79 
84  MetricManager& operator=(MetricManager const&) = delete;
85 
99  void initialize(fhicl::ParameterSet const& pset, std::string const& prefix = "");
100 
104  void do_start();
105 
109  void do_stop();
110 
114  void do_pause();
115 
119  void do_resume();
120 
128  void reinitialize(fhicl::ParameterSet const& pset, std::string const& prefix = "");
129 
133  void shutdown();
134 
148  void sendMetric(std::string const& name, std::string const& value, std::string const& unit, int level,
149  MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
150 
164  void sendMetric(std::string const& name, int const& value, std::string const& unit, int level, MetricMode mode,
165  std::string const& metricPrefix = "", bool useNameOverride = false);
166 
180  void sendMetric(std::string const& name, double const& value, std::string const& unit, int level, MetricMode mode,
181  std::string const& metricPrefix = "", bool useNameOverride = false);
182 
196  void sendMetric(std::string const& name, float const& value, std::string const& unit, int level, MetricMode mode,
197  std::string const& metricPrefix = "", bool useNameOverride = false);
198 
212  void sendMetric(std::string const& name, long unsigned int const& value, std::string const& unit, int level,
213  MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
214 
219  void setPrefix(std::string const& prefix) { prefix_ = prefix; }
220 
225  bool Initialized() { return initialized_; }
226 
231  bool Running() { return running_; }
232 
237  bool Active() { return active_; }
238 
243  bool metricQueueEmpty();
244 
250  size_t metricQueueSize(std::string const& name = "");
251 
252 private:
253  void sendMetricLoop_();
254 
255  void startMetricLoop_();
256 
257  std::vector<std::unique_ptr<artdaq::MetricPlugin>> metric_plugins_;
258  boost::thread metric_sending_thread_;
259  std::mutex metric_mutex_;
260  std::condition_variable metric_cv_;
261  int metric_send_interval_ms_;
262 
263  std::atomic<bool> initialized_;
264  std::atomic<bool> running_;
265  std::atomic<bool> active_;
266  std::string prefix_;
267 
268  std::unordered_map<std::string, std::unique_ptr<MetricData>> metric_cache_;
269  std::mutex metric_cache_mutex_;
270  std::atomic<size_t> missed_metric_calls_;
271  std::atomic<size_t> metric_calls_;
272  size_t metric_cache_max_size_;
273  size_t metric_cache_notify_size_;
274 };
275 
276 #endif /* artdaq_DAQrate_MetricManager_hh */
fhicl::Atom< int > metric_send_maximum_delay_ms
void shutdown()
Call the destructors for all configured MetricPlugin instances.
void initialize(fhicl::ParameterSet const &pset, std::string const &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...
size_t metricQueueSize(std::string const &name="")
Return the size of the named metric queue
void reinitialize(fhicl::ParameterSet const &pset, std::string const &prefix="")
Reinitialize all MetricPlugin Instances.
MetricMode
The Mode of the metric indicates how multiple metric values should be combined within a reporting int...
Definition: MetricData.hh:30
MetricManager()
Construct an instance of the MetricManager class.
void do_start()
Perform startup actions for each configured MetricPlugin.
fhicl::Atom< size_t > metric_queue_notify_size
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.
void setPrefix(std::string const &prefix)
Sets the prefix prepended to all metrics without useNameOverride set.
The MetricManager class handles loading metric plugins and asynchronously sending metric data to them...
The Config struct defines the accepted configuration parameters for this class.
fhicl::OptionalTable< artdaq::MetricPlugin::Config > metricConfig
Example MetricPlugin Configuration.
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.
fhicl::Atom< size_t > metric_queue_size