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 "fhiclcpp/fwd.h"
00014 #include "messagefacility/MessageLogger/MessageLogger.h"
00015
00016 #include <sstream>
00017 #include <list>
00018 #include <thread>
00019 #include <condition_variable>
00020 #include <atomic>
00021
00022 namespace artdaq
00023 {
00024 class MetricManager;
00025 }
00026
00027 class artdaq::MetricManager
00028 {
00029 public:
00030 MetricManager();
00031
00032 MetricManager(MetricManager const&) = delete;
00033
00034 virtual ~MetricManager() noexcept;
00035
00036 MetricManager& operator=(MetricManager const&) = delete;
00037
00038 void initialize(fhicl::ParameterSet const&, std::string prefix = "");
00039
00040 void do_start();
00041
00042 void do_stop();
00043
00044 void do_pause();
00045
00046 void do_resume();
00047
00048 void reinitialize(fhicl::ParameterSet const&, std::string prefix = "");
00049
00050 void shutdown();
00051
00052 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);
00053
00054 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);
00055
00056 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);
00057
00058 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);
00059
00060 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);
00061
00062 void setPrefix(std::string prefix) { prefix_ = prefix; }
00063
00064 private:
00065 void sendMetricLoop_();
00066
00067 void startMetricLoop_();
00068
00069 std::vector<std::unique_ptr<artdaq::MetricPlugin>> metric_plugins_;
00070 std::thread metric_sending_thread_;
00071 std::mutex metric_mutex_;
00072 std::condition_variable metric_cv_;
00073
00074 bool initialized_;
00075 bool running_;
00076 bool active_;
00077 std::string prefix_;
00078
00079
00080 struct MetricData
00081 {
00082 MetricData(const MetricData&) = default;
00083
00084 MetricData(MetricData&&) noexcept = default;
00085
00086 MetricData& operator=(const MetricData&) = default;
00087
00088 MetricData& operator=(MetricData&&) noexcept = default;
00089
00090 std::string name_;
00091 std::string stringValue_;
00092
00093 union
00094 {
00095 int intValue_;
00096 double doubleValue_;
00097 float floatValue_;
00098 long unsigned int unsignedValue_;
00099 };
00100
00101 enum MetricType
00102 {
00103 InvalidMetric,
00104 StringMetric,
00105 IntMetric,
00106 DoubleMetric,
00107 FloatMetric,
00108 UnsignedMetric
00109 };
00110
00111 MetricType type_;
00112 std::string unit_;
00113 int level_;
00114 bool accumulate_;
00115 std::string metricPrefix_;
00116 bool useNameOverride_;
00117
00118 MetricData(std::string const& name, std::string const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00119 : name_(name)
00120 , stringValue_(value)
00121 , type_(StringMetric)
00122 , unit_(unit)
00123 , level_(level)
00124 , accumulate_(accumulate)
00125 , metricPrefix_(metricPrefix)
00126 , useNameOverride_(useNameOverride) {}
00127
00128 MetricData(std::string const& name, int const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00129 : name_(name)
00130 , intValue_(value)
00131 , type_(IntMetric)
00132 , unit_(unit)
00133 , level_(level)
00134 , accumulate_(accumulate)
00135 , metricPrefix_(metricPrefix)
00136 , useNameOverride_(useNameOverride) {}
00137
00138 MetricData(std::string const& name, double const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00139 : name_(name)
00140 , doubleValue_(value)
00141 , type_(DoubleMetric)
00142 , unit_(unit)
00143 , level_(level)
00144 , accumulate_(accumulate)
00145 , metricPrefix_(metricPrefix)
00146 , useNameOverride_(useNameOverride) {}
00147
00148 MetricData(std::string const& name, float const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00149 : name_(name)
00150 , floatValue_(value)
00151 , type_(FloatMetric)
00152 , unit_(unit)
00153 , level_(level)
00154 , accumulate_(accumulate)
00155 , metricPrefix_(metricPrefix)
00156 , useNameOverride_(useNameOverride) {}
00157
00158 MetricData(std::string const& name, long unsigned int const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00159 : name_(name)
00160 , unsignedValue_(value)
00161 , type_(UnsignedMetric)
00162 , unit_(unit)
00163 , level_(level)
00164 , accumulate_(accumulate)
00165 , metricPrefix_(metricPrefix)
00166 , useNameOverride_(useNameOverride) {}
00167
00168 MetricData() : name_("")
00169 , type_(InvalidMetric) {}
00170 };
00171
00172 std::list<std::unique_ptr<MetricData>> metric_queue_;
00173 std::mutex metric_queue_mutex_;
00174 };
00175
00176 #endif