Belle II Software development
ProcessStatistics.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 <framework/core/ProcessStatistics.h>
10
11#include <framework/core/Environment.h>
12#include <framework/logging/Logger.h>
13#include <framework/pcore/ProcHandler.h>
14#include <framework/gearbox/Unit.h>
15#include <framework/utilities/Utils.h>
16
17#include <boost/algorithm/string/replace.hpp>
18#include <regex>
19#include <boost/format.hpp>
20
21#include <algorithm>
22#include <sstream>
23#include <fstream>
24
25using namespace std;
26using namespace Belle2;
27
29{
30 auto indexIt = m_modulesToStatsIndex.find(module);
31 if (indexIt == m_modulesToStatsIndex.end()) {
32 int index = m_stats.size();
33 m_modulesToStatsIndex[module] = index;
34 m_stats.emplace_back();
35 initModule(module);
36 return index;
37 } else {
38 return indexIt->second;
39 }
40}
42{
43 int index = getIndex(module);
44 ModuleStatistics& stats = m_stats.at(index);
45 if (module and stats.getName().empty()) {
46 const string& type = module->getType();
47 if (type == "Tx" or type == "Rx")
48 stats.setName(type);
49 else
50 stats.setName(module->getName());
51 }
52 stats.setIndex(index);
53}
54
56 const std::vector<ModuleStatistics>* modules, bool html) const
57{
58 const ModuleStatistics& global = getGlobal();
59 if (!Environment::Instance().getStats()) {
60 B2WARNING("The calculation of the statistics wasn't enabled during processing. The requested table cannot be printed.");
61 return "";
62 }
63 if (!modules) modules = &(getAll());
64 int moduleNameLength = 21; //minimum: 80 characters
65 const int lengthOfRest = 80 - moduleNameLength;
66 for (const ModuleStatistics& stats : *modules) {
67 int len = stats.getName().length();
68 if (len > moduleNameLength)
69 moduleNameLength = len;
70 }
71 const std::string numTabsModule = (boost::format("%d") % (moduleNameLength + 1)).str();
72 const std::string numWidth = (boost::format("%d") % (moduleNameLength + 1 + lengthOfRest)).str();
73 boost::format outputheader("%s %|" + numTabsModule + "t|| %10s | %10s | %10s | %17s\n");
74 boost::format output("%s %|" + numTabsModule + "t|| %10.0f | %10.0f | %10.2f | %7.2f +-%7.2f\n");
75 if (html) {
76 outputheader = boost::format("<thead><tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr></thead>");
77 output = boost::format("<tr><td>%s</td><td>%.0f</td><td>%.0f</td><td>%.2f</td><td>%.2f &plusmn; %.2f</td></tr>");
78 }
79
80 stringstream out;
81 if (!html) {
82 out << boost::format("%|" + numWidth + "T=|\n");
83 out << outputheader % "Name" % "Calls" % "Memory(MB)" % "Time(s)" % "Time(ms)/Call";
84 out << boost::format("%|" + numWidth + "T=|\n");
85 } else {
86 out << "<table border=0>";
87 out << outputheader % "Name" % "Calls" % "Memory(MB)" % "Time(s)" % "Time(ms)/Call";
88 out << "<tbody>";
89 }
90
91 std::vector<ModuleStatistics> modulesSortedByIndex(*modules);
92 sort(modulesSortedByIndex.begin(), modulesSortedByIndex.end(), [](const ModuleStatistics & a, const ModuleStatistics & b) { return a.getIndex() < b.getIndex(); });
93
94 for (const ModuleStatistics& stats : modulesSortedByIndex) {
95 out << output
96 % stats.getName()
97 % stats.getCalls(mode)
98 % (stats.getMemorySum(mode) / 1024)
99 % (stats.getTimeSum(mode) / Unit::s)
100 % (stats.getTimeMean(mode) / Unit::ms)
101 % (stats.getTimeStddev(mode) / Unit::ms);
102 }
103
104 if (!html) {
105 out << boost::format("%|" + numWidth + "T=|\n");
106 } else {
107 out << "</tbody><tfoot>";
108 }
109 out << output
110 % (ProcHandler::isOutputProcess() ? "Total (output proc.)" : "Total")
111 % global.getCalls(mode)
112 % (global.getMemorySum(mode) / 1024)
113 % (global.getTimeSum(mode) / Unit::s)
114 % (global.getTimeMean(mode) / Unit::ms)
115 % (global.getTimeStddev(mode) / Unit::ms);
116 if (!html) {
117 out << boost::format("%|" + numWidth + "T=|\n");
118 } else {
119 out << "</tfoot></table>";
120 }
121 return out.str();
122}
123
125{
126 unsigned int minIndexUnmerged = 0;
127 if (otherObject->m_modulesToStatsIndex.empty()) {
128 B2WARNING("ProcessStatistics::appendUnmergedModules(): Module -> index list is empty? This might produce wrong results");
129 } else {
130 minIndexUnmerged = otherObject->m_modulesToStatsIndex.begin()->second;
131 for (auto pair : otherObject->m_modulesToStatsIndex) {
132 if (pair.second < (int)minIndexUnmerged)
133 minIndexUnmerged = pair.second;
134 }
135 }
136 if (minIndexUnmerged > m_stats.size())
137 B2FATAL("(minIndexUnmerged > m_stats.size()) :( ");
138 if (minIndexUnmerged > otherObject->m_stats.size())
139 B2FATAL("(minIndexUnmerged > otherObject->m_stats.size()) :( ");
140
141
142 //the first minIndexUnmerged entries in m_stats should just be merged...
143 for (unsigned int i = 0; i < minIndexUnmerged; i++) {
144 ModuleStatistics& myStats = m_stats[i];
145 const ModuleStatistics& otherStats = otherObject->m_stats[i];
146 if (myStats.getName() == otherStats.getName()) {
147 myStats.update(otherStats);
148 } else {
149 B2ERROR("mismatch in module names in statistics (" << myStats.getName() << " vs. " << otherStats.getName() <<
150 "). ProcessStatistics::merge() can only merge statistics that contain exactly the same modules.");
151 }
152 }
153
154 //append the rest
155 for (unsigned int i = minIndexUnmerged; i < otherObject->m_stats.size(); i++) {
156 const ModuleStatistics& otherStats = otherObject->m_stats[i];
157 m_stats.emplace_back(otherStats);
158 m_stats.back().setIndex(m_stats.size() - 1);
159 }
160 //copy m_modulesToStatsIndex
161 //shift indices by #entries missing in otherObject
162 const int shift = m_stats.size() - otherObject->m_stats.size();
163 if (shift < 0) {
164 B2FATAL("shift negative:" << LogVar("shift", shift));
165 }
166 for (auto pair : otherObject->m_modulesToStatsIndex) {
167 m_modulesToStatsIndex[pair.first] = pair.second + shift;
168 }
169}
170
171void ProcessStatistics::write_csv(const char* filename) const
172{
173 std::ofstream output(filename);
174 m_global.csv_header(output);
175 for (auto stats : m_stats) {
176 stats.csv(output);
177 }
178 m_global.csv(output);
179}
180
181void ProcessStatistics::merge(const Mergeable* other)
182{
183 const auto* otherObject = static_cast<const ProcessStatistics*>(other);
184
185 if (m_stats == otherObject->m_stats) {
186 //fast version for merging between processes
187 for (unsigned int i = 0; i < otherObject->m_stats.size(); i++)
188 m_stats[i].update(otherObject->m_stats[i]);
189 } else {
190 //note: statistics in m_global are not merged for pp, we use the output process instead
191 //for objects read from file we need to add them though
192 m_global.update(otherObject->m_global);
193
194 appendUnmergedModules(otherObject);
195 }
196
197 //if the other object has transient data on modules, copy remaining counters
198 if (!otherObject->m_modulesToStatsIndex.empty())
199 setTransientCounters(otherObject);
200}
201
203{
204 m_globalTime = otherObject->m_globalTime;
205 m_globalMemory = otherObject->m_globalMemory;
206 m_moduleTime = otherObject->m_moduleTime;
207 m_moduleMemory = otherObject->m_moduleMemory;
208 m_suspendedTime = otherObject->m_suspendedTime;
210}
211
213{
214 m_global.clear();
215 for (auto& stats : m_stats) { stats.clear(); }
216}
217
218void ProcessStatistics::setCounters(double& time, double& memory,
219 double startTime, double startMemory)
220{
221 time = Utils::getClock() - startTime;
222 memory = Utils::getRssMemoryKB() - startMemory;
223}
224
225TObject* ProcessStatistics::Clone(const char*) const
226{
227 auto* p = new ProcessStatistics(*this);
228 return p;
229}
230
232{
233 std::string s = getStatisticsString();
234 return "Event Statistics:<br />" + s;
235}
236
static Environment & Instance()
Static method to get a reference to the Environment instance.
Keep track of time and memory consumption during processing.
value_type getTimeStddev(EStatisticCounters type=c_Total) const
return the stddev of the execution times for a given counter
value_type getCalls(EStatisticCounters type=c_Total) const
return the number of calls for a given counter type
const std::string & getName() const
Return the previously set name.
EStatisticCounters
Enum to define all counter types.
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 update(const ModuleStatistics &other)
Add statistics for each category.
Base class for Modules.
Definition Module.h:72
static bool isOutputProcess()
Return true if the process is an output process.
void setCounters(double &time, double &memory, double startTime=0, double startMemory=0)
Set counters time and memory to contain the current clock value and memory consumption respectively.
ModuleStatistics m_global
Statistics object for global time and memory consumption.
const ModuleStatistics & getGlobal() const
Get global statistics.
const std::vector< Belle2::ModuleStatistics > & getAll() const
Get entire statistics map.
void appendUnmergedModules(const ProcessStatistics *otherObject)
Merge dissimilar objects (mainly loading ProcessStatistics from file).
double m_globalTime
store clock counter for global time consumption
std::string getStatisticsString(ModuleStatistics::EStatisticCounters type=ModuleStatistics::c_Event, const std::vector< Belle2::ModuleStatistics > *modules=nullptr, bool html=false) const
Return string with statistics for all modules.
int getIndex(const Module *module)
get m_stats index for given module, inserting it if not found.
double m_suspendedMemory
(transient)
void initModule(const Module *module)
Init module statistics: Set name from module if still empty and remember initialization index for dis...
virtual void merge(const Mergeable *other) override
Merge other ProcessStatistics object into this one.
std::map< const Module *, int > m_modulesToStatsIndex
transient, maps Module* to m_stats index.
virtual TObject * Clone(const char *newname="") const override
Reimplement TObject::Clone() since we also need m_modulesToStatsIndex.
void write_csv(const char *filename="ProcessStatistics.csv") const
Write process statistics to a csv file.
std::string getInfoHTML() const
Return a short summary of this object's contents in HTML format.
std::vector< Belle2::ModuleStatistics > m_stats
module statistics
double m_suspendedTime
(transient)
virtual void clear() override
Clear collected statistics but keep names of modules.
double m_globalMemory
(transient)
double m_moduleMemory
(transient)
void setTransientCounters(const ProcessStatistics *otherObject)
Set transient counters from otherObject.
static const double ms
[millisecond]
Definition Unit.h:96
static const double s
[second]
Definition Unit.h:95
Class to store variables with their name which were sent to the logging service.
double getClock()
Return current value of the real-time clock.
Definition Utils.cc:70
unsigned long getRssMemoryKB()
Returns the amount of memory the process actually occupies in the physical RAM of the machine.
Definition Utils.cc:88
Abstract base class for different kinds of events.
STL namespace.