9#include <boost/python.hpp>
10#include <framework/pybasf2/ProcessStatisticsPython.h>
11#include <framework/core/Module.h>
12#include <framework/core/Environment.h>
13#include <framework/datastore/StoreObjPtr.h>
14#include <framework/logging/Logger.h>
15#include <framework/core/PyObjConvUtils.h>
19using namespace boost::python;
26 B2ERROR(
"ProcessStatistics data object is not available, you either disabled statistics with --no-stats or didn't run process(path) yet.");
48 const boost::python::list& modulesPyList)
53 std::vector<ModuleStatistics> moduleStats;
58 if (stats.getName().empty()) stats.setName(ptr->getName());
59 moduleStats.push_back(stats);
66 boost::python::list result;
70 result.append(module);
100 namespace bp = boost::python;
105 docstring_options options(
true,
true,
false);
108 class_<ProcessStatisticsPython> stats(
"ProcessStatistics", R
"DOCSTRING(
109Interface for retrieving statistics about module execution at runtime or after
110:py:func:`basf2.process()` returns. Should be accessed through a global instance `basf2.statistics`.
112Statistics for `event() <Module.event()>` calls are available as a string representation of the object:
114>>> from basf2 import statistics
116=================================================================================
117Name | Calls | Memory(MB) | Time(s) | Time(ms)/Call
118=================================================================================
119RootInput | 101 | 0 | 0.01 | 0.05 +- 0.02
120RootOutput | 100 | 0 | 0.02 | 0.20 +- 0.87
121ProgressBar | 100 | 0 | 0.00 | 0.00 +- 0.00
122=================================================================================
123Total | 101 | 0 | 0.03 | 0.26 +- 0.86
124=================================================================================
126This provides information on the number of calls, elapsed time, and the average
127difference in resident memory before and after the `event() <Module.event>` call.
131 The module responsible for reading (or generating) events usually has one
132 additional event() call which is used to determine whether event processing
137 Memory consumption is reporting the difference in memory usage as reported
138 by the kernel before and after the call. This is not the maximum memory the
139 module has consumed. Negative values indicate that this module has freed
140 momemory which was allocated in other modules or function calls.
142Information on other calls like `initialize() <Module.initialize>`,
143`terminate() <Module.terminate>`, etc. are also available through the different
144counters defined in `StatisticCounters`:
146>>> print(statistics(statistics.INIT))
147>>> print(statistics(statistics.BEGIN_RUN))
148>>> print(statistics(statistics.END_RUN))
149>>> print(statistics(statistics.TERM))
150)DOCSTRING", no_init);
154 "Get `ModuleStatistics` for given Module.")
156 "Get global `ModuleStatistics` containing total elapsed time etc.")
162 scope statistics{stats};
164 enum_<ModuleStatistics::EStatisticCounters>(
"StatisticCounters", R
"DOCSTRING(
165Available types of statistic counters (corresponds to Module functions)
169Time spent or memory used in the `initialize() <Module.initialize>` function
171.. attribute:: BEGIN_RUN
173Time spent or memory used in the `beginRun() <Module.beginRun>` function
177Time spent or memory used in the `event() <Module.event>` function
179.. attribute:: END_RUN
181Time spent or memory used in the `endRun() <Module.endRun>` function
185Time spent or memory used in the `terminate() <Module.terminate>` function
189Time spent or memory used in any module function. This is the sum of all of the above.
203 docstring_options custom_options(
true,
false,
false);
206 "Return the event statistics as a string in a human readable form")
208 "Return an html represenation of the statistics (used by ipython/jupyter)")
210 R
"DOCSTRING(__call__(counter=StatisticCounters.EVENT, modules=None)
212Calling the statistics object directly like a function will return a string
213with the execution statistics in human readable form.
216 counter (StatisticCounters): Which counter to use
217 modules (list[Module]): A list of modules to include in the returned string.
218 If omitted the statistics for all modules will be included.
220* print the `beginRun() <Module.beginRun>` statistics for all modules:
222 >>> print(statistics(statistics.BEGIN_RUN))
224* print the total execution times and memory consumption but only for the
225 modules ``module1`` and ``module2``
227 >>> print(statistics(statistics.TOTAL, [module1, module2]))
229* print the event statistics (default) for only two modules
231 >>> print(statistics(modules=[module1, module2]))
238 docstring_options new_options(
true,
false,
false);
239 class_<ModuleStatistics>(
"ModuleStatistics",
"Execution statistics for a single module. "
240 "All member functions take exactly one argument to select which "
241 "counter to query which defaults to `StatisticCounters.TOTAL` if omitted.")
243 "property to get the name of the module to be displayed in the statistics")
245 "time_sum(counter=StatisticCounters.TOTAL)\nReturn the sum of all execution times")
247 "time_mean(counter=StatisticCounters.TOTAL)\nReturn the mean of all execution times")
249 "time_stddev(counter=StatisticCounters.TOTAL)\nReturn the standard deviation of all execution times")
251 "memory_sum(counter=StatisticCounters.TOTAL)\nReturn the sum of the total memory usage")
253 "memory_mean(counter=StatisticCounters.TOTAL)\nReturn the mean of the memory usage")
255 "memory_stddev(counter=StatisticCounters.TOTAL)\nReturn the standard deviation of the memory usage")
257 "time_memory_corr(counter=StatisticCounters.TOTAL)\nReturn the correlaction factor between time and memory consumption")
259 "calls(counter=StatisticCounters.TOTAL)\nReturn the total number of calls")
@ c_Persistent
Object is available during entire execution time.
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
value_type getTimeMemoryCorrelation(EStatisticCounters type=c_Total) const
return the pearson correlation coefficient between execution times and memory consumption changes
value_type getMemoryStddev(EStatisticCounters type=c_Total) const
return the stddev of the memory consumption changes per call
const std::string & getName() const
Return the previously set name.
value_type getMemoryMean(EStatisticCounters type=c_Total) const
return the average memory change per call
EStatisticCounters
Enum to define all counter types.
@ c_Init
Counting time/calls in initialize()
@ c_EndRun
Counting time/calls in endRun()
@ c_Term
Counting time/calls in terminate()
@ c_BeginRun
Counting time/calls in beginRun()
@ c_Event
Counting time/calls in event()
@ c_Total
Sum of the above.
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
Python interface for ProcessStatistics.
std::string getStatisticsStringHTML()
Return string with statistics for all selected modules as html table.
ProcessStatisticsPython getModuleStatistics(ModuleStatistics::EStatisticCounters type, const boost::python::list &modulesPyList)
Get a new statistics object for a different counter/different list of modules.
std::vector< ModuleStatistics > m_modules
Which modules to show.
const ModuleStatistics * get(const std::shared_ptr< Module > &module)
Get statistics for given module.
std::string getStatisticsString()
Return string with statistics for all selected modules.
static void exposePythonAPI()
Define python wrappers to make functionality avaiable in python.
ModuleStatistics::EStatisticCounters m_type
Which counter to show when printing the statistics.
ProcessStatistics * getWrapped()
Get wrapped ProcessStatistics object.
void clear()
Clear collected statistics but keep names of modules.
boost::python::list getAll()
Get statistics for all modules as python list.
const ModuleStatistics * getGlobal()
Get statistics for the framework itself.
Class to collect call statistics for all modules.
const ModuleStatistics & getGlobal() const
Get global statistics.
ModuleStatistics & getStatistics(const Module *module)
Get statistics for single module.
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.
virtual void clear() override
Clear collected statistics but keep names of modules.
Type-safe access to single objects in the data store.
std::shared_ptr< Module > ModulePtr
Defines a pointer to a module object as a boost shared pointer.
Scalar convertPythonObject(const boost::python::object &pyObject, Scalar)
Convert from Python to given type.
Abstract base class for different kinds of events.