$treeview $search $mathjax $extrastylesheet
artdaq_core
v3_06_01
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "artdaq-core/Core/StatisticsCollection.hh" 00002 #include <iostream> 00003 #include <utility> 00004 00005 namespace artdaq { 00006 StatisticsCollection& StatisticsCollection::getInstance() 00007 { 00008 static StatisticsCollection singletonInstance; 00009 return singletonInstance; 00010 } 00011 00012 StatisticsCollection::StatisticsCollection() 00013 { 00014 thread_stop_requested_ = false; 00015 try 00016 { 00017 calculation_thread_ = std::make_unique<boost::thread>(boost::bind(&StatisticsCollection::run, this)); 00018 } 00019 catch (const boost::exception& e) 00020 { 00021 std::cerr << "Caught boost::exception starting Statistics Collection thread: " << boost::diagnostic_information(e) << ", errno=" << errno << std::endl; 00022 exit(5); 00023 } 00024 } 00025 00026 StatisticsCollection::~StatisticsCollection() noexcept 00027 { 00028 // stop and clean up the thread 00029 thread_stop_requested_ = true; 00030 00031 try 00032 { 00033 // Having issues where ~StatisticsCollection is being called from within thread due to signal handlers 00034 if (calculation_thread_ && calculation_thread_->joinable() && calculation_thread_->get_id() != boost::this_thread::get_id()) 00035 { 00036 calculation_thread_->join(); 00037 } 00038 } 00039 catch (...) 00040 { 00041 // IGNORED 00042 } 00043 } 00044 00045 void StatisticsCollection:: 00046 addMonitoredQuantity(const std::string& name, 00047 MonitoredQuantityPtr mqPtr) 00048 { 00049 std::lock_guard<std::mutex> scopedLock(map_mutex_); 00050 monitoredQuantityMap_[name] = std::move(mqPtr); 00051 } 00052 00053 MonitoredQuantityPtr 00054 StatisticsCollection::getMonitoredQuantity(const std::string& name) const 00055 { 00056 std::lock_guard<std::mutex> scopedLock(map_mutex_); 00057 MonitoredQuantityPtr emptyResult; 00058 std::map<std::string, MonitoredQuantityPtr>::const_iterator iter; 00059 iter = monitoredQuantityMap_.find(name); 00060 if (iter == monitoredQuantityMap_.end()) { return emptyResult; } 00061 return iter->second; 00062 } 00063 00064 void StatisticsCollection::reset() 00065 { 00066 std::lock_guard<std::mutex> scopedLock(map_mutex_); 00067 std::map<std::string, MonitoredQuantityPtr>::const_iterator iter; 00068 std::map<std::string, MonitoredQuantityPtr>::const_iterator iterEnd; 00069 iterEnd = monitoredQuantityMap_.end(); 00070 for (iter = monitoredQuantityMap_.begin(); iter != iterEnd; ++iter) 00071 { 00072 iter->second->reset(); 00073 } 00074 } 00075 00076 void StatisticsCollection::requestStop() 00077 { 00078 thread_stop_requested_ = true; 00079 } 00080 00081 void StatisticsCollection::run() 00082 { 00083 while (!thread_stop_requested_) 00084 { 00085 auto useconds = static_cast<uint64_t>(calculationInterval_ * 1000000); 00086 usleep(useconds); 00087 { 00088 std::lock_guard<std::mutex> scopedLock(map_mutex_); 00089 std::map<std::string, MonitoredQuantityPtr>::const_iterator iter; 00090 std::map<std::string, MonitoredQuantityPtr>::const_iterator iterEnd; 00091 iterEnd = monitoredQuantityMap_.end(); 00092 for (iter = monitoredQuantityMap_.begin(); iter != iterEnd; ++iter) 00093 { 00094 iter->second->calculateStatistics(); 00095 } 00096 } 00097 } 00098 } 00099 } // namespace artdaq