artdaq_utilities  v1_05_08
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 "artdaq-utilities/Plugins/SystemMetricCollector.hh"
15 #include "fhiclcpp/fwd.h"
16 #include "fhiclcpp/types/OptionalTable.h"
17 #include "messagefacility/MessageLogger/MessageLogger.h"
18 
19 #include <atomic>
20 #include <boost/thread.hpp>
21 #include <condition_variable>
22 #include <queue>
23 #include <sstream>
24 
25 namespace artdaq {
26 class MetricManager;
27 }
28 
35 {
36 public:
40  struct Config
41  {
44  fhicl::Atom<size_t> metric_queue_size{
45  fhicl::Name{"metric_queue_size"},
46  fhicl::Comment{"The maximum number of metric entries which can be stored in the metric queue."}, 1000};
49  fhicl::Atom<size_t> metric_queue_notify_size{
50  fhicl::Name{"metric_queue_notify_size"},
51  fhicl::Comment{
52  "The number of metric entries in the list which will cause reports of the queue size to be printed."},
53  10};
56  fhicl::Atom<int> metric_send_maximum_delay_ms{
57  fhicl::Name{"metric_send_maximum_delay_ms"},
58  fhicl::Comment{"The maximum amount of time between metric send calls (will send 0s for metrics which have not "
59  "reported in this interval)"},
60  15000};
62  fhicl::Atom<bool> send_system_metrics{fhicl::Name{"send_system_metrics"}, fhicl::Comment{"Whether to collect and send system metrics such as CPU usage, Memory usage and network activity."}, false};
64  fhicl::Atom<bool> send_process_metrics{fhicl::Name{"send_process_metrics"}, fhicl::Comment{"Whether to collect and send process CPU usage and Memory usage"}, false};
66  fhicl::OptionalTable<artdaq::MetricPlugin::Config> metricConfig{fhicl::Name{"metricConfig"}};
67  };
69  using Parameters = fhicl::WrappedTable<Config>;
70 
74  MetricManager();
75 
79  MetricManager(MetricManager const&) = delete;
80 
86  virtual ~MetricManager() noexcept;
87 
92  MetricManager& operator=(MetricManager const&) = delete;
93 
97  MetricManager(MetricManager&&) = delete;
102  MetricManager& operator=(MetricManager&&) = delete;
103 
117  void initialize(fhicl::ParameterSet const& pset, std::string const& prefix = "");
118 
122  void do_start();
123 
127  void do_stop();
128 
132  void do_pause();
133 
137  void do_resume();
138 
146  void reinitialize(fhicl::ParameterSet const& pset, std::string const& prefix = "");
147 
151  void shutdown();
152 
166  void sendMetric(std::string const& name, std::string const& value, std::string const& unit, int level,
167  MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
168 
182  void sendMetric(std::string const& name, int const& value, std::string const& unit, int level, MetricMode mode,
183  std::string const& metricPrefix = "", bool useNameOverride = false);
184 
198  void sendMetric(std::string const& name, double const& value, std::string const& unit, int level, MetricMode mode,
199  std::string const& metricPrefix = "", bool useNameOverride = false);
200 
214  void sendMetric(std::string const& name, float const& value, std::string const& unit, int level, MetricMode mode,
215  std::string const& metricPrefix = "", bool useNameOverride = false);
216 
230  void sendMetric(std::string const& name, uint64_t const& value, std::string const& unit, int level,
231  MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
232 
237  void setPrefix(std::string const& prefix) { prefix_ = prefix; }
238 
243  bool Initialized() { return initialized_; }
244 
249  bool Running() { return running_; }
250 
255  bool Active() { return active_; }
256 
261  bool metricQueueEmpty();
262 
267  bool metricManagerBusy();
268 
274  size_t metricQueueSize(std::string const& name = "");
275 
276 private:
277  void sendMetricLoop_();
278 
279  void startMetricLoop_();
280 
281  std::vector<std::unique_ptr<artdaq::MetricPlugin>> metric_plugins_;
282  boost::thread metric_sending_thread_;
283  std::mutex metric_mutex_;
284  std::condition_variable metric_cv_;
285  int metric_send_interval_ms_{15000};
286  int metric_holdoff_us_{1000};
287  std::chrono::steady_clock::time_point last_metric_received_;
288  std::unique_ptr<SystemMetricCollector> system_metric_collector_;
289 
290  std::atomic<bool> initialized_;
291  std::atomic<bool> running_;
292  std::atomic<bool> active_;
293  std::atomic<bool> busy_;
294  std::string prefix_;
295 
296  std::unordered_map<std::string, std::unique_ptr<MetricData>> metric_cache_;
297  std::mutex metric_cache_mutex_;
298  std::atomic<size_t> missed_metric_calls_;
299  std::atomic<size_t> metric_calls_;
300  size_t metric_cache_max_size_{1000};
301  size_t metric_cache_notify_size_{10};
302 };
303 
304 #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...
bool metricManagerBusy()
Determine whether the MetricManager or any of its plugins are currently processing metrics...
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.
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.
MetricMode
The Mode of the metric indicates how multiple metric values should be combined within a reporting int...
Definition: MetricData.hh:29
fhicl::OptionalTable< artdaq::MetricPlugin::Config > metricConfig
Example MetricPlugin Configuration.
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation (if desired)
bool Active()
Returns whether any Metric Plugins are defined and configured
fhicl::Atom< bool > send_system_metrics
&quot;send_system_metrics&quot;: (Default: false): Whether to collect and send system metrics such as CPU usage...
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
fhicl::Atom< bool > send_process_metrics
&quot;send_process_metrics&quot; (Default: false): Whether to collect and send process CPU usage and Memory usa...