9#include <framework/core/ProcessStatistics.h>
11#include <framework/logging/Logger.h>
12#include <framework/pcore/ProcHandler.h>
13#include <framework/gearbox/Unit.h>
14#include <framework/utilities/Utils.h>
16#include <boost/algorithm/string/replace.hpp>
18#include <boost/format.hpp>
37 return indexIt->second;
44 if (module and stats.getName().empty()) {
45 const string& type = module->getType();
46 if (type ==
"Tx" or type ==
"Rx")
49 stats.setName(module->getName());
51 stats.setIndex(index);
55 const std::vector<ModuleStatistics>* modules,
bool html)
const
58 if (!modules) modules = &(
getAll());
59 int moduleNameLength = 21;
60 const int lengthOfRest = 80 - moduleNameLength;
62 int len = stats.getName().length();
63 if (len > moduleNameLength)
64 moduleNameLength = len;
66 const std::string numTabsModule = (boost::format(
"%d") % (moduleNameLength + 1)).str();
67 const std::string numWidth = (boost::format(
"%d") % (moduleNameLength + 1 + lengthOfRest)).str();
68 boost::format outputheader(
"%s %|" + numTabsModule +
"t|| %10s | %10s | %10s | %17s\n");
69 boost::format output(
"%s %|" + numTabsModule +
"t|| %10.0f | %10.0f | %10.2f | %7.2f +-%7.2f\n");
71 outputheader = boost::format(
"<thead><tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr></thead>");
72 output = boost::format(
"<tr><td>%s</td><td>%.0f</td><td>%.0f</td><td>%.2f</td><td>%.2f ± %.2f</td></tr>");
77 out << boost::format(
"%|" + numWidth +
"T=|\n");
78 out << outputheader %
"Name" %
"Calls" %
"Memory(MB)" %
"Time(s)" %
"Time(ms)/Call";
79 out << boost::format(
"%|" + numWidth +
"T=|\n");
81 out <<
"<table border=0>";
82 out << outputheader %
"Name" %
"Calls" %
"Memory(MB)" %
"Time(s)" %
"Time(ms)/Call";
86 std::vector<ModuleStatistics> modulesSortedByIndex(*modules);
87 sort(modulesSortedByIndex.begin(), modulesSortedByIndex.end(), [](
const ModuleStatistics & a,
const ModuleStatistics & b) { return a.getIndex() < b.getIndex(); });
92 % stats.getCalls(mode)
93 % (stats.getMemorySum(mode) / 1024)
94 % (stats.getTimeSum(mode) /
Unit::s)
95 % (stats.getTimeMean(mode) /
Unit::ms)
96 % (stats.getTimeStddev(mode) /
Unit::ms);
100 out << boost::format(
"%|" + numWidth +
"T=|\n");
102 out <<
"</tbody><tfoot>";
112 out << boost::format(
"%|" + numWidth +
"T=|\n");
114 out <<
"</tfoot></table>";
121 unsigned int minIndexUnmerged = 0;
123 B2WARNING(
"ProcessStatistics::appendUnmergedModules(): Module -> index list is empty? This might produce wrong results");
127 if (pair.second < (
int)minIndexUnmerged)
128 minIndexUnmerged = pair.second;
131 if (minIndexUnmerged >
m_stats.size())
132 B2FATAL(
"(minIndexUnmerged > m_stats.size()) :( ");
133 if (minIndexUnmerged > otherObject->
m_stats.size())
134 B2FATAL(
"(minIndexUnmerged > otherObject->m_stats.size()) :( ");
138 for (
unsigned int i = 0; i < minIndexUnmerged; i++) {
142 myStats.
update(otherStats);
144 B2ERROR(
"mismatch in module names in statistics (" << myStats.
getName() <<
" vs. " << otherStats.
getName() <<
145 "). ProcessStatistics::merge() can only merge statistics that contain exactly the same modules.");
150 for (
unsigned int i = minIndexUnmerged; i < otherObject->
m_stats.size(); i++) {
152 m_stats.emplace_back(otherStats);
159 B2FATAL(
"shift negative:" <<
LogVar(
"shift", shift));
168 std::ofstream output(filename);
180 if (
m_stats == otherObject->m_stats) {
182 for (
unsigned int i = 0; i < otherObject->m_stats.size(); i++)
183 m_stats[i].update(otherObject->m_stats[i]);
193 if (!otherObject->m_modulesToStatsIndex.empty())
210 for (
auto& stats :
m_stats) { stats.clear(); }
214 double startTime,
double startMemory)
229 return "Event Statistics:<br />" + s;
Abstract base class for objects that can be merged.
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.
void csv(std::ostream &output) const
write data to the given stream in csv format
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 clear()
Clear all statistics.
void update(const ModuleStatistics &other)
Add statistics for each category.
void csv_header(std::ostream &output) const
write csv header to the given stream
static bool isOutputProcess()
Return true if the process is an output process.
Class to collect call statistics for all modules.
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.
ProcessStatistics()
Constructor.
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)
double m_moduleTime
(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]
static const double s
[second]
Class to store variables with their name which were sent to the logging service.
double getClock()
Return current value of the real-time clock.
unsigned long getRssMemoryKB()
Returns the amount of memory the process actually occupies in the physical RAM of the machine.
Abstract base class for different kinds of events.