artdaq_core  v3_09_01
StatisticsCollection.cc
1 #include "artdaq-core/Core/StatisticsCollection.hh"
2 #include <iostream>
3 #include <utility>
4 
5 #include "TRACE/tracemf.h"
6 
7 namespace artdaq {
9 {
10  static StatisticsCollection singletonInstance;
11  return singletonInstance;
12 }
13 
14 StatisticsCollection::StatisticsCollection()
15 {
16  thread_stop_requested_ = false;
17  try
18  {
19  calculation_thread_ = std::make_unique<boost::thread>(boost::bind(&StatisticsCollection::run, this));
20  char tname[16]; // Size 16 - see man page pthread_setname_np(3) and/or prctl(2)
21  snprintf(tname, sizeof(tname) - 1, "%s", "StatColl"); // NOLINT
22  tname[sizeof(tname) - 1] = '\0'; // assure term. snprintf is not too evil :)
23  auto handle = calculation_thread_->native_handle();
24  pthread_setname_np(handle, tname);
25  }
26  catch (const boost::exception& e)
27  {
28  TLOG(TLVL_ERROR) << "Caught boost::exception starting Statistics Collection thread: " << boost::diagnostic_information(e) << ", errno=" << errno;
29  std::cerr << "Caught boost::exception starting Statistics Collection thread: " << boost::diagnostic_information(e) << ", errno=" << errno << std::endl;
30  exit(5);
31  }
32 }
33 
35 {
36  // stop and clean up the thread
37  thread_stop_requested_ = true;
38 
39  try
40  {
41  // Having issues where ~StatisticsCollection is being called from within thread due to signal handlers
42  if (calculation_thread_ && calculation_thread_->joinable() && calculation_thread_->get_id() != boost::this_thread::get_id())
43  {
44  calculation_thread_->join();
45  }
46  }
47  catch (...)
48  {
49  // IGNORED
50  }
51 }
52 
54  addMonitoredQuantity(const std::string& name,
56 {
57  std::lock_guard<std::mutex> scopedLock(map_mutex_);
58  monitoredQuantityMap_[name] = std::move(mqPtr);
59 }
60 
62 StatisticsCollection::getMonitoredQuantity(const std::string& name) const
63 {
64  std::lock_guard<std::mutex> scopedLock(map_mutex_);
65  MonitoredQuantityPtr emptyResult;
66  std::map<std::string, MonitoredQuantityPtr>::const_iterator iter;
67  iter = monitoredQuantityMap_.find(name);
68  if (iter == monitoredQuantityMap_.end()) { return emptyResult; }
69  return iter->second;
70 }
71 
73 {
74  std::lock_guard<std::mutex> scopedLock(map_mutex_);
75  std::map<std::string, MonitoredQuantityPtr>::const_iterator iter;
76  std::map<std::string, MonitoredQuantityPtr>::const_iterator iterEnd;
77  iterEnd = monitoredQuantityMap_.end();
78  for (iter = monitoredQuantityMap_.begin(); iter != iterEnd; ++iter)
79  {
80  iter->second->reset();
81  }
82 }
83 
85 {
86  thread_stop_requested_ = true;
87 }
88 
90 {
91  while (!thread_stop_requested_)
92  {
93  auto useconds = static_cast<uint64_t>(calculationInterval_ * 1000000);
94  usleep(useconds);
95  {
96  std::lock_guard<std::mutex> scopedLock(map_mutex_);
97  std::map<std::string, MonitoredQuantityPtr>::const_iterator iter;
98  std::map<std::string, MonitoredQuantityPtr>::const_iterator iterEnd;
99  iterEnd = monitoredQuantityMap_.end();
100  for (iter = monitoredQuantityMap_.begin(); iter != iterEnd; ++iter)
101  {
102  iter->second->calculateStatistics();
103  }
104  }
105  }
106 }
107 } // 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.