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
00032 class artdaq::MetricManager
00033 {
00034 public:
00038 MetricManager();
00039
00043 MetricManager(MetricManager const&) = delete;
00044
00050 virtual ~MetricManager() noexcept;
00051
00056 MetricManager& operator=(MetricManager const&) = delete;
00057
00066 void initialize(fhicl::ParameterSet const& pset, std::string prefix = "");
00067
00071 void do_start();
00072
00076 void do_stop();
00077
00081 void do_pause();
00082
00086 void do_resume();
00087
00095 void reinitialize(fhicl::ParameterSet const& pset, std::string prefix = "");
00096
00100 void shutdown();
00101
00112 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);
00113
00124 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);
00125
00136 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);
00137
00148 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);
00149
00160 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);
00161
00166 void setPrefix(std::string prefix) { prefix_ = prefix; }
00167
00168 private:
00169 void sendMetricLoop_();
00170
00171 void startMetricLoop_();
00172
00173 std::vector<std::unique_ptr<artdaq::MetricPlugin>> metric_plugins_;
00174 std::thread metric_sending_thread_;
00175 std::mutex metric_mutex_;
00176 std::condition_variable metric_cv_;
00177
00178 bool initialized_;
00179 bool running_;
00180 bool active_;
00181 std::string prefix_;
00182
00183
00184 struct MetricData
00185 {
00186 MetricData(const MetricData&) = default;
00187
00188 MetricData(MetricData&&) noexcept = default;
00189
00190 MetricData& operator=(const MetricData&) = default;
00191
00192 MetricData& operator=(MetricData&&) noexcept = default;
00193
00194 std::string name_;
00195 std::string stringValue_;
00196
00197 union
00198 {
00199 int intValue_;
00200 double doubleValue_;
00201 float floatValue_;
00202 long unsigned int unsignedValue_;
00203 };
00204
00205 enum MetricType
00206 {
00207 InvalidMetric,
00208 StringMetric,
00209 IntMetric,
00210 DoubleMetric,
00211 FloatMetric,
00212 UnsignedMetric
00213 };
00214
00215 MetricType type_;
00216 std::string unit_;
00217 int level_;
00218 bool accumulate_;
00219 std::string metricPrefix_;
00220 bool useNameOverride_;
00221
00222 MetricData(std::string const& name, std::string const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00223 : name_(name)
00224 , stringValue_(value)
00225 , type_(StringMetric)
00226 , unit_(unit)
00227 , level_(level)
00228 , accumulate_(accumulate)
00229 , metricPrefix_(metricPrefix)
00230 , useNameOverride_(useNameOverride) {}
00231
00232 MetricData(std::string const& name, int const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00233 : name_(name)
00234 , intValue_(value)
00235 , type_(IntMetric)
00236 , unit_(unit)
00237 , level_(level)
00238 , accumulate_(accumulate)
00239 , metricPrefix_(metricPrefix)
00240 , useNameOverride_(useNameOverride) {}
00241
00242 MetricData(std::string const& name, double const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00243 : name_(name)
00244 , doubleValue_(value)
00245 , type_(DoubleMetric)
00246 , unit_(unit)
00247 , level_(level)
00248 , accumulate_(accumulate)
00249 , metricPrefix_(metricPrefix)
00250 , useNameOverride_(useNameOverride) {}
00251
00252 MetricData(std::string const& name, float const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00253 : name_(name)
00254 , floatValue_(value)
00255 , type_(FloatMetric)
00256 , unit_(unit)
00257 , level_(level)
00258 , accumulate_(accumulate)
00259 , metricPrefix_(metricPrefix)
00260 , useNameOverride_(useNameOverride) {}
00261
00262 MetricData(std::string const& name, long unsigned int const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00263 : name_(name)
00264 , unsignedValue_(value)
00265 , type_(UnsignedMetric)
00266 , unit_(unit)
00267 , level_(level)
00268 , accumulate_(accumulate)
00269 , metricPrefix_(metricPrefix)
00270 , useNameOverride_(useNameOverride) {}
00271
00272 MetricData() : name_("")
00273 , type_(InvalidMetric) {}
00274 };
00275
00276 std::list<std::unique_ptr<MetricData>> metric_queue_;
00277 std::mutex metric_queue_mutex_;
00278 };
00279
00280 #endif