artdaq_core  v3_06_10
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  char tname[16]; // Size 16 - see man page pthread_setname_np(3) and/or prctl(2)
19  snprintf(tname, sizeof(tname)-1, "%s", "StatColl"); // NOLINT
20  tname[sizeof(tname)-1] = '\0'; // assure term. snprintf is not too evil :)
21  auto handle = calculation_thread_->native_handle();
22  pthread_setname_np(handle, tname);
23  }
24  catch (const boost::exception& e)
25  {
26  std::cerr << "Caught boost::exception starting Statistics Collection thread: " << boost::diagnostic_information(e) << ", errno=" << errno << std::endl;
27  exit(5);
28  }
29 }
30 
32 {
33  // stop and clean up the thread
34  thread_stop_requested_ = true;
35 
36  try
37  {
38  // Having issues where ~StatisticsCollection is being called from within thread due to signal handlers
39  if (calculation_thread_ && calculation_thread_->joinable() && calculation_thread_->get_id() != boost::this_thread::get_id())
40  {
41  calculation_thread_->join();
42  }
43  }
44  catch (...)
45  {
46  // IGNORED
47  }
48 }
49 
51  addMonitoredQuantity(const std::string& name,
53 {
54  std::lock_guard<std::mutex> scopedLock(map_mutex_);
55  monitoredQuantityMap_[name] = std::move(mqPtr);
56 }
57 
59 StatisticsCollection::getMonitoredQuantity(const std::string& name) const
60 {
61  std::lock_guard<std::mutex> scopedLock(map_mutex_);
62  MonitoredQuantityPtr emptyResult;
63  std::map<std::string, MonitoredQuantityPtr>::const_iterator iter;
64  iter = monitoredQuantityMap_.find(name);
65  if (iter == monitoredQuantityMap_.end()) { return emptyResult; }
66  return iter->second;
67 }
68 
70 {
71  std::lock_guard<std::mutex> scopedLock(map_mutex_);
72  std::map<std::string, MonitoredQuantityPtr>::const_iterator iter;
73  std::map<std::string, MonitoredQuantityPtr>::const_iterator iterEnd;
74  iterEnd = monitoredQuantityMap_.end();
75  for (iter = monitoredQuantityMap_.begin(); iter != iterEnd; ++iter)
76  {
77  iter->second->reset();
78  }
79 }
80 
82 {
83  thread_stop_requested_ = true;
84 }
85 
87 {
88  while (!thread_stop_requested_)
89  {
90  auto useconds = static_cast<uint64_t>(calculationInterval_ * 1000000);
91  usleep(useconds);
92  {
93  std::lock_guard<std::mutex> scopedLock(map_mutex_);
94  std::map<std::string, MonitoredQuantityPtr>::const_iterator iter;
95  std::map<std::string, MonitoredQuantityPtr>::const_iterator iterEnd;
96  iterEnd = monitoredQuantityMap_.end();
97  for (iter = monitoredQuantityMap_.begin(); iter != iterEnd; ++iter)
98  {
99  iter->second->calculateStatistics();
100  }
101  }
102  }
103 }
104 } // 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.