artdaq_core  v3_06_01
StatisticsCollection.cc
1 #include "artdaq-core/Core/StatisticsCollection.hh"
2 #include <iostream>
3 #include <utility>
4 
5 namespace artdaq {
7 {
8  static StatisticsCollection singletonInstance;
9  return singletonInstance;
10 }
11 
12 StatisticsCollection::StatisticsCollection()
13 {
14  thread_stop_requested_ = false;
15  try
16  {
17  calculation_thread_ = std::make_unique<boost::thread>(boost::bind(&StatisticsCollection::run, this));
18  }
19  catch (const boost::exception& e)
20  {
21  std::cerr << "Caught boost::exception starting Statistics Collection thread: " << boost::diagnostic_information(e) << ", errno=" << errno << std::endl;
22  exit(5);
23  }
24 }
25 
27 {
28  // stop and clean up the thread
29  thread_stop_requested_ = true;
30 
31  try
32  {
33  // Having issues where ~StatisticsCollection is being called from within thread due to signal handlers
34  if (calculation_thread_ && calculation_thread_->joinable() && calculation_thread_->get_id() != boost::this_thread::get_id())
35  {
36  calculation_thread_->join();
37  }
38  }
39  catch (...)
40  {
41  // IGNORED
42  }
43 }
44 
46  addMonitoredQuantity(const std::string& name,
48 {
49  std::lock_guard<std::mutex> scopedLock(map_mutex_);
50  monitoredQuantityMap_[name] = std::move(mqPtr);
51 }
52 
54 StatisticsCollection::getMonitoredQuantity(const std::string& name) const
55 {
56  std::lock_guard<std::mutex> scopedLock(map_mutex_);
57  MonitoredQuantityPtr emptyResult;
58  std::map<std::string, MonitoredQuantityPtr>::const_iterator iter;
59  iter = monitoredQuantityMap_.find(name);
60  if (iter == monitoredQuantityMap_.end()) { return emptyResult; }
61  return iter->second;
62 }
63 
65 {
66  std::lock_guard<std::mutex> scopedLock(map_mutex_);
67  std::map<std::string, MonitoredQuantityPtr>::const_iterator iter;
68  std::map<std::string, MonitoredQuantityPtr>::const_iterator iterEnd;
69  iterEnd = monitoredQuantityMap_.end();
70  for (iter = monitoredQuantityMap_.begin(); iter != iterEnd; ++iter)
71  {
72  iter->second->reset();
73  }
74 }
75 
77 {
78  thread_stop_requested_ = true;
79 }
80 
82 {
83  while (!thread_stop_requested_)
84  {
85  auto useconds = static_cast<uint64_t>(calculationInterval_ * 1000000);
86  usleep(useconds);
87  {
88  std::lock_guard<std::mutex> scopedLock(map_mutex_);
89  std::map<std::string, MonitoredQuantityPtr>::const_iterator iter;
90  std::map<std::string, MonitoredQuantityPtr>::const_iterator iterEnd;
91  iterEnd = monitoredQuantityMap_.end();
92  for (iter = monitoredQuantityMap_.begin(); iter != iterEnd; ++iter)
93  {
94  iter->second->calculateStatistics();
95  }
96  }
97  }
98 }
99 } // namespace artdaq
void addMonitoredQuantity(const std::string &name, MonitoredQuantityPtr mqPtr)
Registers a new MonitoredQuantity to be tracked by the StatisticsCollection.
static StatisticsCollection & getInstance()
Returns the singleton instance of the StatisticsCollection.
void reset()
Reset all MonitoredQuantity object in this StatisticsCollection.
void requestStop()
Stops the statistics calculation thread.
virtual ~StatisticsCollection() noexcept
StatisticsCollection Destructor.
A collection of MonitoredQuantity instances describing low-level statistics of the artdaq system...
MonitoredQuantityPtr getMonitoredQuantity(const std::string &name) const
Lookup and return a MonitoredQuantity from the StatisticsCollection.
void run()
Start the background thread that performs MonitoredQuantity statistics calculation.
std::shared_ptr< MonitoredQuantity > MonitoredQuantityPtr
A shared_ptr to a MonitoredQuantity instance.