ndmspc  v1.2.0-0.1.rc3
NResourceMonitor.h
1 #ifndef Ndmspc_NResourceMonitor_H
2 #define Ndmspc_NResourceMonitor_H
3 #include <sys/resource.h>
4 #include <chrono>
5 #include <TObject.h>
6 #include <THnSparse.h>
7 
8 namespace Ndmspc {
9 
20 class NResourceMonitor : public TObject {
21  public:
26 
30  virtual ~NResourceMonitor();
31 
36  virtual void Print(Option_t * option = "") const;
37 
43  THnSparse * Initialize(THnSparse * hns);
44 
50  void Fill(Int_t * coords, int threadId);
51 
55  THnSparse * GetHnSparse() const { return fHnSparse; }
56 
60  rusage & GetUsageStart() { return fUsageStart; }
61 
65  rusage & GetUsageEnd() { return fUsageEnd; }
66 
72  double GetTimeDiffInSeconds() const;
73 
77  double GetCpuUsage() const;
78 
82  long GetMemoryUsageDiff() const { return fUsageEnd.ru_maxrss - fUsageStart.ru_maxrss; }
83 
87  void Start();
88 
92  void End();
93 
94  private:
95  THnSparse * fHnSparse{nullptr};
96  rusage fUsageStart;
97  rusage fUsageEnd;
98  std::chrono::high_resolution_clock::time_point fWallStart;
99  std::chrono::high_resolution_clock::time_point fWallEnd;
100  std::vector<std::string> fNames = {"time", "cpu", "mem"};
101 
107  double timevalToDouble(const timeval & tv) const
108  {
109  return static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) / 1000000.0;
110  }
111 
113  ClassDef(NResourceMonitor, 1);
115 };
116 } // namespace Ndmspc
117 #endif
Monitors and records resource usage (CPU, memory, wall time) for processes or threads.
rusage & GetUsageStart()
Returns the starting resource usage structure.
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.
rusage & GetUsageEnd()
Returns the ending resource usage structure.
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 * GetHnSparse() const
Returns the pointer to the THnSparse histogram.
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.