00001 #ifndef artdaq_DAQrate_MetricManager_hh
00002 #define artdaq_DAQrate_MetricManager_hh
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "artdaq-utilities/Plugins/MetricPlugin.hh"
00013 #include "artdaq-utilities/Plugins/MetricData.hh"
00014 #include "fhiclcpp/fwd.h"
00015 #include "fhiclcpp/types/OptionalTable.h"
00016 #include "messagefacility/MessageLogger/MessageLogger.h"
00017
00018 #include <sstream>
00019 #include <queue>
00020 #include <condition_variable>
00021 #include <atomic>
00022 #include <boost/thread.hpp>
00023
00024 namespace artdaq
00025 {
00026 class MetricManager;
00027 }
00028
00034 class artdaq::MetricManager
00035 {
00036 public:
00037 struct Config
00038 {
00039 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 };
00040 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 };
00041 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 };
00042 fhicl::OptionalTable<artdaq::MetricPlugin::Config> metricConfig{ fhicl::Name{"metricConfig"} };
00043 };
00044 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
00045 using Parameters = fhicl::WrappedTable<Config>;
00046 #endif
00047
00051 MetricManager();
00052
00056 MetricManager(MetricManager const&) = delete;
00057
00063 virtual ~MetricManager() noexcept;
00064
00069 MetricManager& operator=(MetricManager const&) = delete;
00070
00083 void initialize(fhicl::ParameterSet const& pset, std::string prefix = "");
00084
00088 void do_start();
00089
00093 void do_stop();
00094
00098 void do_pause();
00099
00103 void do_resume();
00104
00112 void reinitialize(fhicl::ParameterSet const& pset, std::string prefix = "");
00113
00117 void shutdown();
00118
00132 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);
00133
00147 void sendMetric(std::string const& name, int const& value, std::string const& unit, int level, MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
00148
00162 void sendMetric(std::string const& name, double const& value, std::string const& unit, int level, MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
00163
00177 void sendMetric(std::string const& name, float const& value, std::string const& unit, int level, MetricMode mode, std::string const& metricPrefix = "", bool useNameOverride = false);
00178
00192 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);
00193
00198 void setPrefix(std::string prefix) { prefix_ = prefix; }
00199
00204 bool Initialized() { return initialized_; }
00205
00210 bool Running() { return running_; }
00211
00216 bool Active() { return active_; }
00217
00222 bool metricQueueEmpty();
00223
00229 size_t metricQueueSize(std::string name = "");
00230
00231 private:
00232 void sendMetricLoop_();
00233
00234 void startMetricLoop_();
00235
00236 std::vector<std::unique_ptr<artdaq::MetricPlugin>> metric_plugins_;
00237 boost::thread metric_sending_thread_;
00238 std::mutex metric_mutex_;
00239 std::condition_variable metric_cv_;
00240 int metric_send_interval_ms_;
00241
00242 bool initialized_;
00243 bool running_;
00244 bool active_;
00245 std::string prefix_;
00246
00247
00248
00249 typedef std::unique_ptr<MetricData> metric_data_ptr;
00250 typedef std::pair<size_t, std::list<metric_data_ptr>> queue_entry;
00251
00252 std::unordered_map<std::string, queue_entry> metric_queue_;
00253 std::mutex metric_queue_mutex_;
00254 std::atomic<size_t> missed_metric_calls_;
00255 size_t metric_queue_max_size_;
00256 size_t metric_queue_notify_size_;
00257 };
00258
00259 #endif