5 #include "NResourceMonitor.h"
24 double wallStart = std::chrono::duration<double>(
fWallStart.time_since_epoch()).count();
25 double wallEnd = std::chrono::duration<double>(
fWallEnd.time_since_epoch()).count();
27 NLogInfo(
"Resource usage:");
28 NLogInfo(
" User time: %.6f s", userEnd - userStart);
29 NLogInfo(
" System time: %.6f s", sysEnd - sysStart);
30 NLogInfo(
" Wall time: %.6f s", wallEnd - wallStart);
35 NLogInfo(
" Min RSS: %ld KB",
fUsageStart.ru_maxrss);
36 NLogInfo(
" Max RSS: %ld KB",
fUsageEnd.ru_maxrss);
47 std::vector<TAxis *> axes;
48 int nThreads = ROOT::GetThreadPoolSize();
49 if (nThreads <= 0) nThreads = 1;
51 NLogTrace(
"NResourceMonitor::Initialize: Initializing resource monitor for %d threads", nThreads);
53 TAxis * threadAxis =
new TAxis(nThreads, 0, nThreads);
54 threadAxis->SetNameTitle(
"thread",
"Thread");
56 for (
int i = 0; i < nThreads; ++i) {
57 threadAxis->SetBinLabel(i + 1, TString::Format(
"%d", i).Data());
59 axes.push_back(threadAxis);
61 axes.push_back(aStat);
64 NLogWarning(
"NResourceMonitor::Initialize: THnSparse is already initialized, overwriting ...");
69 fHnSparse->SetNameTitle(
"resource_monitor",
"Resource Monitor");
80 auto statBinCoords = std::make_unique<Int_t[]>(
fHnSparse->GetNdimensions());
83 for (Int_t i = 0; i <
fHnSparse->GetNdimensions() - 2; ++i) {
84 statBinCoords[i] = coords[i];
89 statBinCoords[
fHnSparse->GetNdimensions() - 2] = threadId + 1;
91 statBinCoords[
fHnSparse->GetNdimensions() - 1] = 1;
92 statBin =
fHnSparse->GetBin(statBinCoords.get());
96 statBinCoords[
fHnSparse->GetNdimensions() - 1] = 2;
97 statBin =
fHnSparse->GetBin(statBinCoords.get());
101 statBinCoords[
fHnSparse->GetNdimensions() - 1] = 3;
102 statBin =
fHnSparse->GetBin(statBinCoords.get());
108 fWallStart = std::chrono::high_resolution_clock::now();
111 NLogError(
"NResourceMonitor::Start: getrusage failed at start");
117 fWallEnd = std::chrono::high_resolution_clock::now();
119 if (getrusage(RUSAGE_SELF, &
fUsageEnd) == -1) {
120 NLogError(
"NResourceMonitor::End: getrusage failed at end");
137 double wallStart = std::chrono::duration<double>(
fWallStart.time_since_epoch()).count();
138 double wallEnd = std::chrono::duration<double>(
fWallEnd.time_since_epoch()).count();
140 double usage = ((userEnd - userStart) + (sysEnd - sysStart)) / (wallEnd - wallStart) * 100.0;
Monitors and records resource usage (CPU, memory, wall time) for processes or threads.
void Start()
Records the starting resource usage and wall time.
THnSparse * fHnSparse
THnSparse histogram for resource data.
virtual void Print(Option_t *option="") const
Prints the resource monitor information.
double GetTimeDiffInSeconds() const
Returns the time difference in seconds since the last measurement or reset.
std::chrono::high_resolution_clock::time_point fWallStart
Wall clock start time.
void End()
Records the ending resource usage and wall time.
virtual ~NResourceMonitor()
Destructor.
double timevalToDouble(const timeval &tv) const
Helper function to convert timeval to double seconds.
std::vector< std::string > fNames
Axis names.
void Fill(Int_t *coords, int threadId)
Fills resource usage data into the histogram.
double GetCpuUsage() const
Calculates and returns the CPU usage between Start and End.
THnSparse * Initialize(THnSparse *hns)
Initializes the THnSparse histogram for resource data.
NResourceMonitor()
Default constructor.
rusage fUsageStart
Resource usage at start.
rusage fUsageEnd
Resource usage at end.
std::chrono::high_resolution_clock::time_point fWallEnd
Wall clock end time.
long GetMemoryUsageDiff() const
Returns the difference in memory usage (in kilobytes) between Start and End.
static THnSparse * ReshapeSparseAxes(THnSparse *hns, std::vector< int > order, std::vector< TAxis * > newAxes={}, std::vector< int > newPoint={}, Option_t *option="E")
Reshape axes of THnSparse.
static TAxis * CreateAxisFromLabels(const std::string &name, const std::string &title, const std::vector< std::string > &labels)
Create a TAxis from a list of labels.