00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "artdaq-utilities/Plugins/MetricManager.hh"
00010 #include "artdaq-utilities/Plugins/makeMetricPlugin.hh"
00011 #include "messagefacility/MessageLogger/MessageLogger.h"
00012 #include "fhiclcpp/ParameterSet.h"
00013
00014 #include <chrono>
00015
00016 artdaq::MetricManager::
00017 MetricManager() : metric_plugins_(0)
00018 , initialized_(false)
00019 , running_(false)
00020 , active_(false) { }
00021
00022 artdaq::MetricManager::~MetricManager()
00023 {
00024 shutdown();
00025 }
00026
00027 void artdaq::MetricManager::initialize(fhicl::ParameterSet const& pset, std::string prefix)
00028 {
00029 prefix_ = prefix;
00030 if (initialized_)
00031 {
00032 shutdown();
00033 }
00034 mf::LogDebug("MetricManager") << "Configuring metrics with parameter set:\n" << pset.to_string();
00035
00036 std::vector<std::string> names = pset.get_pset_names();
00037
00038 for (auto name : names)
00039 {
00040 try
00041 {
00042 mf::LogDebug("MetricManager") << "Constructing metric plugin with name " << name;
00043 fhicl::ParameterSet plugin_pset = pset.get<fhicl::ParameterSet>(name);
00044 metric_plugins_.push_back(makeMetricPlugin(
00045 plugin_pset.get<std::string>("metricPluginType", ""), plugin_pset));
00046 }
00047 catch (...)
00048 {
00049 mf::LogError("MetricManager") << "Exception caught in MetricManager::initialize, error loading plugin with name " << name;
00050 }
00051 }
00052
00053 initialized_ = true;
00054 }
00055
00056 void artdaq::MetricManager::do_start()
00057 {
00058 if (!running_)
00059 {
00060 mf::LogDebug("MetricManager") << "Starting MetricManager";
00061 for (auto& metric : metric_plugins_)
00062 {
00063 try
00064 {
00065 metric->startMetrics();
00066 mf::LogDebug("MetricManager") << "Metric Plugin " << metric->getLibName() << " started.";
00067 active_ = true;
00068 }
00069 catch (...)
00070 {
00071 mf::LogError("MetricManager") <<
00072 "Exception caught in MetricManager::do_start(), error starting plugin with name " <<
00073 metric->getLibName();
00074 }
00075 }
00076 running_ = true;
00077 startMetricLoop_();
00078 }
00079 }
00080
00081 void artdaq::MetricManager::do_stop()
00082 {
00083 running_ = false;
00084 metric_cv_.notify_all();
00085 if (metric_sending_thread_.joinable()) metric_sending_thread_.join();
00086 }
00087
00088 void artdaq::MetricManager::do_pause() { }
00089 void artdaq::MetricManager::do_resume() { }
00090
00091 void artdaq::MetricManager::reinitialize(fhicl::ParameterSet const& pset, std::string prefix)
00092 {
00093 shutdown();
00094 initialize(pset, prefix);
00095 }
00096
00097 void artdaq::MetricManager::shutdown()
00098 {
00099 mf::LogDebug("MetricManager") << "MetricManager is shutting down...";
00100 do_stop();
00101
00102 if (initialized_)
00103 {
00104 for (auto& i : metric_plugins_)
00105 {
00106 try
00107 {
00108 std::string name = i->getLibName();
00109 i.reset(nullptr);
00110 mf::LogDebug("MetricManager") << "Metric Plugin " << name << " shutdown.";
00111 }
00112 catch (...)
00113 {
00114 mf::LogError("MetricManager") <<
00115 "Exception caught in MetricManager::shutdown(), error shutting down metric with name " <<
00116 i->getLibName();
00117 }
00118 }
00119 initialized_ = false;
00120 }
00121 }
00122
00123 void artdaq::MetricManager::sendMetric(std::string const& name, std::string const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00124 {
00125 if (!initialized_) mf::LogWarning("MetricManager") << "Attempted to send metric when MetricManager has not yet been initialized!";
00126 else if (!running_) mf::LogWarning("MetricManager") << "Attempted to send metric when MetricManager stopped!";
00127 else if (active_)
00128 {
00129 std::unique_ptr<MetricData> metric(new MetricData(name, value, unit, level, accumulate, metricPrefix, useNameOverride));
00130 {
00131 std::unique_lock<std::mutex> lk(metric_queue_mutex_);
00132 metric_queue_.push_back(std::move(metric));
00133 }
00134 metric_cv_.notify_all();
00135 }
00136 }
00137
00138 void artdaq::MetricManager::sendMetric(std::string const& name, int const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00139 {
00140 if (!initialized_) mf::LogWarning("MetricManager") << "Attempted to send metric when MetricManager has not yet been initialized!";
00141 else if (!running_) mf::LogWarning("MetricManager") << "Attempted to send metric when MetricManager stopped!";
00142 else if (active_)
00143 {
00144 std::unique_ptr<MetricData> metric(new MetricData(name, value, unit, level, accumulate, metricPrefix, useNameOverride));
00145 {
00146 std::unique_lock<std::mutex> lk(metric_queue_mutex_);
00147 metric_queue_.push_back(std::move(metric));
00148 }
00149 metric_cv_.notify_all();
00150 }
00151 }
00152
00153 void artdaq::MetricManager::sendMetric(std::string const& name, double const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00154 {
00155 if (!initialized_) mf::LogWarning("MetricManager") << "Attempted to send metric when MetricManager has not yet been initialized!";
00156 else if (!running_) mf::LogWarning("MetricManager") << "Attempted to send metric when MetricManager stopped!";
00157 else if (active_)
00158 {
00159 std::unique_ptr<MetricData> metric(new MetricData(name, value, unit, level, accumulate, metricPrefix, useNameOverride));
00160 {
00161 std::unique_lock<std::mutex> lk(metric_queue_mutex_);
00162 metric_queue_.push_back(std::move(metric));
00163 }
00164 metric_cv_.notify_all();
00165 }
00166 }
00167
00168 void artdaq::MetricManager::sendMetric(std::string const& name, float const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00169 {
00170 if (!initialized_) mf::LogWarning("MetricManager") << "Attempted to send metric when MetricManager has not yet been initialized!";
00171 else if (!running_) mf::LogWarning("MetricManager") << "Attempted to send metric when MetricManager stopped!";
00172 else if (active_)
00173 {
00174 std::unique_ptr<MetricData> metric(new MetricData(name, value, unit, level, accumulate, metricPrefix, useNameOverride));
00175 {
00176 std::unique_lock<std::mutex> lk(metric_queue_mutex_);
00177 metric_queue_.push_back(std::move(metric));
00178 }
00179 metric_cv_.notify_all();
00180 }
00181 }
00182
00183 void artdaq::MetricManager::sendMetric(std::string const& name, long unsigned int const& value, std::string const& unit, int level, bool accumulate, std::string const& metricPrefix, bool useNameOverride)
00184 {
00185 if (!initialized_) mf::LogWarning("MetricManager") << "Attempted to send metric when MetricManager has not yet been initialized!";
00186 else if (!running_) mf::LogWarning("MetricManager") << "Attempted to send metric when MetricManager stopped!";
00187 else if (active_)
00188 {
00189 std::unique_ptr<MetricData> metric(new MetricData(name, value, unit, level, accumulate, metricPrefix, useNameOverride));
00190 {
00191 std::unique_lock<std::mutex> lk(metric_queue_mutex_);
00192 metric_queue_.push_back(std::move(metric));
00193 }
00194 metric_cv_.notify_all();
00195 }
00196 }
00197
00198 void artdaq::MetricManager::startMetricLoop_()
00199 {
00200 if (metric_sending_thread_.joinable()) metric_sending_thread_.join();
00201 mf::LogInfo("MetricManager") << "Starting Metric Sending Thread" << std::endl;
00202 metric_sending_thread_ = std::thread(&MetricManager::sendMetricLoop_, this);
00203 }
00204
00205 void artdaq::MetricManager::sendMetricLoop_()
00206 {
00207 while (running_)
00208 {
00209 while (metric_queue_.size() == 0 && running_)
00210 {
00211 std::unique_lock<std::mutex> lk(metric_mutex_);
00212 metric_cv_.wait_for(lk, std::chrono::milliseconds(100));
00213 }
00214
00215 auto temp_list = std::list<std::unique_ptr<MetricData>>();
00216 {
00217 std::unique_lock<std::mutex> lk(metric_queue_mutex_);
00218 temp_list.swap(metric_queue_);
00219 }
00220
00221 while (temp_list.size() > 0)
00222 {
00223 auto data_ = std::move(temp_list.front());
00224 temp_list.pop_front();
00225 if (data_->type_ == MetricData::InvalidMetric) continue;
00226 std::string nameTemp = data_->name_;
00227 if (!data_->useNameOverride_)
00228 {
00229 if (data_->metricPrefix_.size() > 0)
00230 {
00231 nameTemp = prefix_ + "." + data_->metricPrefix_ + "." + data_->name_;
00232 }
00233 else
00234 {
00235 nameTemp = prefix_ + "." + data_->name_;
00236 }
00237 }
00238
00239 for (auto& metric : metric_plugins_)
00240 {
00241 if (metric->getRunLevel() >= data_->level_)
00242 {
00243 try
00244 {
00245 switch (data_->type_)
00246 {
00247 case MetricData::StringMetric:
00248 metric->sendMetric(nameTemp, data_->stringValue_, data_->unit_, data_->accumulate_);
00249 break;
00250 case MetricData::IntMetric:
00251 metric->sendMetric(nameTemp, data_->intValue_, data_->unit_, data_->accumulate_);
00252 break;
00253 case MetricData::DoubleMetric:
00254 metric->sendMetric(nameTemp, data_->doubleValue_, data_->unit_, data_->accumulate_);
00255 break;
00256 case MetricData::FloatMetric:
00257 metric->sendMetric(nameTemp, data_->floatValue_, data_->unit_, data_->accumulate_);
00258 break;
00259 case MetricData::UnsignedMetric:
00260 metric->sendMetric(nameTemp, data_->unsignedValue_, data_->unit_, data_->accumulate_);
00261 break;
00262 case MetricData::InvalidMetric:
00263 break;
00264 }
00265 }
00266 catch (...)
00267 {
00268 mf::LogError("MetricManager") <<
00269 "Error in MetricManager::sendMetric: error sending value to metric plugin with name "
00270 << metric->getLibName();
00271 }
00272 }
00273 }
00274 }
00275 }
00276
00277 for (auto& metric : metric_plugins_)
00278 {
00279 try
00280 {
00281 metric->stopMetrics();
00282 mf::LogDebug("MetricManager") << "Metric Plugin " << metric->getLibName() << " stopped.";
00283 }
00284 catch (...)
00285 {
00286 mf::LogError("MetricManager") <<
00287 "Exception caught in MetricManager::do_stop(), error stopping plugin with name " <<
00288 metric->getLibName();
00289 }
00290 }
00291 mf::LogDebug("MetricManager") << "MetricManager has been stopped.";
00292 }