9#include <framework/modules/profile/ProfileModule.h>
10#include <framework/core/MetadataService.h>
11#include <framework/dataobjects/ProfileInfo.h>
12#include <framework/datastore/StoreObjPtr.h>
13#include <framework/utilities/Utils.h>
14#include <framework/gearbox/Unit.h>
34 m_nEvents(0), m_step(1),
35 m_eventInfo(k_maxPoints)
38 setDescription(
"Records execution time and memory usage in ProfileInfo objects for each event. Can also graph memory usage.");
62 profileInfoStartPtr.
create(
true);
65 profileInfoStartPtr->getRssMemory(),
66 profileInfoStartPtr->getTimeInSec());
74 profileInfoPtr.
create(
true);
76 B2DEBUG(100,
"Virtual Memory usage [MB]: " << profileInfoPtr->getVirtualMemory() / 1024);
81 profileInfoPtr->getRssMemory(),
82 profileInfoPtr->getTimeInSec());
85 profileInfoPtr->getRssMemory(),
86 profileInfoPtr->getTimeInSec());
102 profileInfoPtr->getRssMemory(),
103 profileInfoPtr->getTimeInSec());
114 double factorMB = 1. / 1024;
115 if (!imgOutput.empty()) {
116 TDirectory* saveDir = gDirectory;
118 gROOT->SetBatch(
true);
119 auto* can =
new TCanvas();
120 auto* graph =
new TGraph(nPoints);
121 for (
int i = 0; i < nPoints; i++) {
124 graph->SetMarkerStyle(kMultiply);
126 TH1* histo = graph->GetHistogram();
128 histo->GetYaxis()->SetTitle(yAxisName.c_str());
129 histo->GetXaxis()->SetTitle(
"Time [s]");
130 can->Print(imgOutput.c_str());
139 profileHistogram->SetDirectory(
nullptr);
140 profileHistogram->SetStats(
false);
141 profileHistogram->GetYaxis()->SetTitle(yAxisName.c_str());
142 profileHistogram->GetXaxis()->SetTitle(
"Event");
143 profileHistogram->SetBinContent(1, lmdMemoryExtract(
m_initializeInfo) * factorMB);
144 for (
int i = 0; i < nPoints; i++) {
145 profileHistogram->SetBinContent(i + 2, lmdMemoryExtract(
m_eventInfo[i]) * factorMB);
153 profileInfoEndPtr.
create(
true);
156 profileInfoEndPtr->getRssMemory(),
157 profileInfoEndPtr->getTimeInSec());
170 storeMemoryGraph(
"VirtualMemoryProfile",
"Virtual Memory usage",
"Virtual Memory Usage [MB]",
172 storeMemoryGraph(
"RssMemoryProfile",
"Rss Memory usage",
"Rss Memory Usage [MB]",
@ c_Persistent
Object is available during entire execution time.
void setDescription(const std::string &description)
Sets the description of the module.
const MemoryExtractLambda m_extractVirtualMem
Lambda expression to return the virtual memory from a MemTime data structure.
MemTime m_startInfo
memory usage and time at start of event loop + burn in
static const int k_maxPoints
maximal number of profile points
static const int k_burnIn
number of events before the average time measurement is started
virtual void initialize() override
Initializes the Module.
MemTime m_endInfo
memory usage and time at end of event loop
virtual void event() override
Event profiling.
std::function< unsigned long(MemTime const &) > MemoryExtractLambda
Signature of the lambda functions, which are used to extract the memory usage from teh MemTime struct...
virtual void terminate() override
Terminate the Module.
int m_step
number of events between profile points
std::vector< MemTime > m_eventInfo
memory usage and time at individual events
const MemoryExtractLambda m_extractRssMem
Lambda expression to return the Rss memory from a MemTime data structure.
MemTime m_terminateInfo
memory usage and time at termination
ProfileModule()
Constructor.
void storeMemoryGraph(const std::string &name, const std::string &title, const std::string &xAxisName, const std::string &imgOutput, const MemoryExtractLambda &lmdMemoryExtract)
Stores the memory usage of the application over time in plots.
std::string m_rssOutputFileName
Name for rss image output file.
double m_timeOffset
time at module creation
MemTime m_initializeInfo
memory usage and time at initialization
std::string m_outputFileName
Name for image output file.
int m_nEvents
event counter
bool assign(TObject *object, bool replace=false)
Assign 'object' to this accessor.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
bool create(bool replace=false)
Create a default object in the data store.
Type-safe access to single objects in the data store.
static const double s
[second]
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
double getClock()
Return current value of the real-time clock.
Abstract base class for different kinds of events.
An internal struct to store pairs of memory usage and time.
unsigned long virtualMem
virtual memory usage
double time
execution time
unsigned long rssMem
rss memory usage