Belle II Software  release-08-01-10
StatisticsTimingHLTDQMModule.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <hlt/softwaretrigger/modules/dqm/StatisticsTimingHLTDQMModule.h>
10 
11 #include <framework/datastore/StoreObjPtr.h>
12 #include <framework/core/ProcessStatistics.h>
13 #include <framework/core/ModuleStatistics.h>
14 #include <framework/gearbox/Unit.h>
15 #include <hlt/utilities/Units.h>
16 
17 #include <TDirectory.h>
18 
19 #include <TH1F.h>
20 
21 #include <fstream>
22 
23 using namespace Belle2;
24 using namespace SoftwareTrigger;
25 
26 REG_MODULE(StatisticsTimingHLTDQM);
27 
29 {
30  setDescription("Monitor reconstruction runtime on HLT");
32 
33  addParam("histogramDirectoryName", m_param_histogramDirectoryName,
34  "Runtime DQM histograms on HLT will be put into this directory", m_param_histogramDirectoryName);
35 
36  addParam("m_param_overviewModuleList", m_param_overviewModuleList,
37  "Which modules should be shown in the overview mean list", m_param_overviewModuleList);
38 
39  addParam("createHLTUnitHistograms", m_param_create_hlt_unit_histograms,
40  "Create HLT unit histograms?",
41  false);
42 }
43 
45 {
46  // Create a separate histogram directory and cd into it.
47  TDirectory* oldDirectory = nullptr;
48 
49  if (!m_param_histogramDirectoryName.empty()) {
50  oldDirectory = gDirectory;
51  TDirectory* histDir = oldDirectory->mkdir(m_param_histogramDirectoryName.c_str());
52  histDir->cd();
53  }
54 
55  m_meanTimeHistogram = new TH1F("meanTimeHistogram", "Mean Processing Time [ms]", m_param_overviewModuleList.size(), 0,
57  m_meanTimeHistogram->SetStats(false);
58  m_meanMemoryHistogram = new TH1F("meanMemoryHistogram", "Mean Memory Change [MB]", m_param_overviewModuleList.size(), 0,
60  m_meanMemoryHistogram->SetStats(false);
61  m_fullTimeHistogram = new TH1F("fullTimeHistogram", "Budget Time [ms]", m_fullTimeNBins, 0, m_fullTimeMax);
62  m_fullTimeHistogram->StatOverflows(true);
63  m_processingTimeHistogram = new TH1F("processingTimeHistogram", "Processing Time [ms]", m_processingTimeNBins, 0,
65  m_processingTimeHistogram->StatOverflows(true);
66  m_fullMemoryHistogram = new TH1F("fullMemoryHistogram", "Total memory used [MB]", m_fullMemoryNBins, 0,
68  m_fullMemoryHistogram->StatOverflows(true);
69 
70  for (unsigned int index = 0; index < m_param_overviewModuleList.size(); index++) {
71  const std::string& moduleName = m_param_overviewModuleList[index];
72  m_meanTimeHistogram->GetXaxis()->SetBinLabel(index + 1, moduleName.c_str());
73  m_meanMemoryHistogram->GetXaxis()->SetBinLabel(index + 1, moduleName.c_str());
74  m_moduleTimeHistograms.emplace(moduleName, new TH1F((moduleName + "_time").c_str(),
75  ("Time spent in: " + moduleName + " [ms]").c_str(), m_processingTimeNBins, 0, m_processingTimeMax));
76  m_moduleTimeHistograms[moduleName]->StatOverflows(true);
77  m_lastModuleTimeSum.emplace(moduleName, 0);
78  m_moduleMemoryHistograms.emplace(moduleName, new TH1F((moduleName + "_memory").c_str(),
79  ("Memory used in: " + moduleName + " [MB]").c_str(), m_fullMemoryNBins, 0, m_fullMemoryMax));
80  m_moduleMemoryHistograms[moduleName]->StatOverflows(true);
81  }
82 
84  m_fullTimeMeanPerUnitHistogram = new TH1F("fullTimeMeanPerUnitHistogram", "Mean Budget Time Per Unit [ms]",
85  HLTUnits::max_hlt_units + 1, 0,
86  HLTUnits::max_hlt_units + 1);
87  m_fullTimeMeanPerUnitHistogram->SetStats(false);
88  m_fullTimeMeanPerUnitHistogram->SetXTitle("HLT unit number");
89  m_processingTimeMeanPerUnitHistogram = new TH1F("processingTimeMeanPerUnitHistogram", "Mean Processing Time Per Unit [ms]",
90  HLTUnits::max_hlt_units + 1, 0,
91  HLTUnits::max_hlt_units + 1);
92  m_processingTimeMeanPerUnitHistogram->SetStats(false);
93  m_processingTimeMeanPerUnitHistogram->SetXTitle("HLT unit number");
94 
95  for (unsigned int index = 1; index <= HLTUnits::max_hlt_units; index++) {
96  m_fullTimePerUnitHistograms.emplace(index, new TH1F(("fullTimePerUnitHistogram_HLT" + std::to_string(index)).c_str(),
97  ("Budget Time Per Unit: HLT" + std::to_string(index) + " [ms]").c_str(), m_fullTimeNBins, 0, m_fullTimeMax));
98  m_fullTimePerUnitHistograms[index]->StatOverflows(true);
99  m_lastFullTimeSumPerUnit.emplace(index, 0);
100  m_processingTimePerUnitHistograms.emplace(index, new TH1F(("processingTimePerUnitHistogram_HLT" + std::to_string(index)).c_str(),
101  ("Processing Time Per Unit: HLT" + std::to_string(index) + " [ms]").c_str(), m_processingTimeNBins, 0, m_processingTimeMax));
102  m_processingTimePerUnitHistograms[index]->StatOverflows(true);
103  m_lastProcessingTimeSumPerUnit.emplace(index, 0);
104  m_fullMemoryPerUnitHistograms.emplace(index, new TH1F(("fullMemoryPerUnitHistogram_HLT" + std::to_string(index)).c_str(),
105  ("Total Memory Used Per Unit: HLT" + std::to_string(index) + " [MB]").c_str(), m_fullMemoryNBins, 0, m_fullMemoryMax));
106  m_fullMemoryPerUnitHistograms[index]->StatOverflows(true);
107  }
108 
109  m_processesPerUnitHistogram = new TH1F("processesPerUnitHistogram", "Number of Processes Per Unit",
110  HLTUnits::max_hlt_units + 1, 0,
111  HLTUnits::max_hlt_units + 1);
112  m_processesPerUnitHistogram->SetXTitle("HLT unit number");
113  }
114 
115  if (oldDirectory) {
116  oldDirectory->cd();
117  }
118 }
119 
120 
122 {
123  // Register histograms (calls back defineHisto)
124  REG_HISTOGRAM
125 
127  std::ifstream file;
128  file.open(HLTUnits::hlt_unit_file);
129  if (file.good()) {
130  std::string host;
131  getline(file, host);
132  m_hlt_unit = atoi(host.substr(3, 2).c_str());
133  file.close();
134  } else {
135  B2WARNING("HLT unit number not found");
136  }
137  }
138 }
139 
141 {
143 
144  if (not stats.isValid()) {
145  return;
146  }
147 
148  const std::vector<ModuleStatistics>& moduleStatisticsList = stats->getAll();
149 
150  std::vector<double> meanTimes(m_param_overviewModuleList.size(), 0);
151  std::vector<double> meanMemories(m_param_overviewModuleList.size(), 0);
152 
153  for (const ModuleStatistics& moduleStatistics : moduleStatisticsList) {
154  const std::string& statisticsName = moduleStatistics.getName();
155  const auto m_param_overviewModuleListIterator = std::find(m_param_overviewModuleList.begin(), m_param_overviewModuleList.end(),
156  statisticsName);
157  if (m_param_overviewModuleListIterator == m_param_overviewModuleList.end()) {
158  continue;
159  }
160 
161  const double statisticsTime = moduleStatistics.getTimeMean(ModuleStatistics::EStatisticCounters::c_Event) / Unit::ms;
162  const double statisticsMemory = moduleStatistics.getMemoryMean(ModuleStatistics::EStatisticCounters::c_Total) / 1024;
163  const double statisticsTime_sum = moduleStatistics.getTimeSum(ModuleStatistics::EStatisticCounters::c_Event) / Unit::ms;
164  const double statisticsMemory_sum = moduleStatistics.getMemorySum(ModuleStatistics::EStatisticCounters::c_Total) / 1024;
165 
166  const int m_param_overviewModuleListIndex = std::distance(m_param_overviewModuleList.begin(), m_param_overviewModuleListIterator);
167  meanTimes[m_param_overviewModuleListIndex] += statisticsTime;
168  meanMemories[m_param_overviewModuleListIndex] += statisticsMemory;
169 
170  m_moduleTimeHistograms[statisticsName]->Fill(statisticsTime_sum - m_lastModuleTimeSum[statisticsName]);
171  m_lastModuleTimeSum[statisticsName] = statisticsTime_sum;
172  m_moduleMemoryHistograms[statisticsName]->Fill(statisticsMemory_sum);
173  }
174 
175  for (unsigned int index = 0; index < m_param_overviewModuleList.size(); index++) {
176  m_meanTimeHistogram->SetBinContent(index + 1, meanTimes[index]);
177  m_meanMemoryHistogram->SetBinContent(index + 1, meanMemories[index]);
178  }
179 
180  double processingTimeSum = 0.0;
181  double processingTimeMean = 0.0;
182 
183  for (const ModuleStatistics& moduleStatistics : moduleStatisticsList) {
184  const std::string& statisticsName = moduleStatistics.getName();
185  const auto m_summaryModuleListIterator = std::find(m_summaryModuleList.begin(), m_summaryModuleList.end(),
186  statisticsName);
187  if (m_summaryModuleListIterator == m_summaryModuleList.end()) {
188  continue;
189  }
190  processingTimeSum += moduleStatistics.getTimeSum(ModuleStatistics::EStatisticCounters::c_Event) / Unit::ms;
191  processingTimeMean += moduleStatistics.getTimeMean(ModuleStatistics::EStatisticCounters::c_Event) / Unit::ms;
192  }
193  m_processingTimeHistogram->Fill(processingTimeSum - m_lastProcessingTimeSum);
194  m_lastProcessingTimeSum = processingTimeSum;
195 
196  const ModuleStatistics& fullStatistics = stats->getGlobal();
197  const double fullTimeSum = fullStatistics.getTimeSum(ModuleStatistics::EStatisticCounters::c_Event) / Unit::ms;
198  m_fullTimeHistogram->Fill(fullTimeSum - m_lastFullTimeSum);
199  m_lastFullTimeSum = fullTimeSum;
200  const double fullMemorySum = fullStatistics.getMemorySum(ModuleStatistics::EStatisticCounters::c_Total) / 1024;
201  m_fullMemoryHistogram->Fill(fullMemorySum);
202 
204  if (0 < m_hlt_unit) {
205  m_processingTimeMeanPerUnitHistogram->SetBinContent(m_hlt_unit + 1, processingTimeMean);
206 
208  m_lastProcessingTimeSumPerUnit[m_hlt_unit] = processingTimeSum;
209 
210  const double fullTimeMean = fullStatistics.getTimeMean(ModuleStatistics::EStatisticCounters::c_Event) / Unit::ms;
211  m_fullTimeMeanPerUnitHistogram->SetBinContent(m_hlt_unit + 1, fullTimeMean);
212 
214  m_lastFullTimeSumPerUnit[m_hlt_unit] = fullTimeSum;
215 
216  m_fullMemoryPerUnitHistograms[m_hlt_unit]->Fill(fullMemorySum);
217  }
218  }
219 }
220 
222 {
224  B2FATAL("Histograms were not created. Did you setup a HistoManager?");
225  }
226 
227  m_meanTimeHistogram->Reset();
228  m_meanMemoryHistogram->Reset();
229  std::for_each(m_moduleTimeHistograms.begin(), m_moduleTimeHistograms.end(),
230  [](auto & it) { it.second->Reset(); });
231  std::for_each(m_moduleMemoryHistograms.begin(), m_moduleMemoryHistograms.end(),
232  [](auto & it) { it.second->Reset(); });
233  m_fullTimeHistogram->Reset();
234  m_processingTimeHistogram->Reset();
235  m_fullMemoryHistogram->Reset();
239  std::for_each(m_fullTimePerUnitHistograms.begin(), m_fullTimePerUnitHistograms.end(),
240  [](auto & it) { it.second->Reset(); });
242  [](auto & it) { it.second->Reset(); });
244  [](auto & it) { it.second->Reset(); });
246 
248  }
249 }
250 
@ c_Persistent
Object is available during entire execution time.
Definition: DataStore.h:60
HistoModule.h is supposed to be used instead of Module.h for the modules with histogram definitions t...
Definition: HistoModule.h:29
Keep track of time and memory consumption during processing.
value_type getMemorySum(EStatisticCounters type=c_Total) const
return the total used memory for a given counter
value_type getTimeSum(EStatisticCounters type=c_Total) const
return the sum of all execution times for a given counter
value_type getTimeMean(EStatisticCounters type=c_Total) const
return the mean execution time for a given counter
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
std::map< std::string, TH1F * > m_moduleTimeHistograms
Time distribution of certain modules.
TH1F * m_fullMemoryHistogram
Total memory usage distribution of all events.
const double m_processingTimeNBins
Number of bins for the histograms of processingTime.
std::map< unsigned int, double > m_lastProcessingTimeSumPerUnit
Storage for the last processing time sum per unit.
int m_hlt_unit
Store HLT unit number on initialization.
const double m_processingTimeMax
Maximum for the histograms of processingTime.
std::map< unsigned int, TH1F * > m_processingTimePerUnitHistograms
Processing time distribution of events per unit.
double m_lastProcessingTimeSum
Storage for the last processing time sum.
const double m_fullMemoryMax
Maximum for the histograms of fullMemory.
std::vector< std::string > m_param_overviewModuleList
Parameter: which modules should be shown in the overview list.
TH1F * m_fullTimeMeanPerUnitHistogram
Mean budget time of events per unit.
const double m_fullTimeNBins
Number of bins for the histograms of fullTime.
TH1F * m_processingTimeHistogram
Processing time distribution of all events.
std::map< std::string, double > m_lastModuleTimeSum
Storage for the last time sum of certain modules.
TH1F * m_processingTimeMeanPerUnitHistogram
Mean processing time of events per unit.
bool m_param_create_hlt_unit_histograms
Parameter: Create HLT unit number histograms?
std::map< unsigned int, double > m_lastFullTimeSumPerUnit
Storage for the last full time sum per unit.
double m_lastFullTimeSum
Storage for the last full time sum.
std::map< unsigned int, TH1F * > m_fullTimePerUnitHistograms
Budget time distribution of events per unit.
const double m_fullTimeMax
Maximum for the histograms of fullTime.
std::string m_param_histogramDirectoryName
Parameter: Directory to put the generated histograms.
std::vector< std::string > m_summaryModuleList
Summary modules of the actual processing.
std::map< std::string, TH1F * > m_moduleMemoryHistograms
Memory distribution of certain modules.
const double m_fullMemoryNBins
Number of bins for the histograms of fullMemory.
TH1F * m_fullTimeHistogram
Budget time distribution of all events.
std::map< unsigned int, TH1F * > m_fullMemoryPerUnitHistograms
Total memory distribution of events per unit.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
static const double ms
[millisecond]
Definition: Unit.h:96
REG_MODULE(arichBtest)
Register the Module.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
Abstract base class for different kinds of events.