artdaq_utilities  v1_05_02
SystemMetricCollector.hh
1 #include <sys/times.h>
2 #include <list>
3 #include <memory>
4 #include <string>
5 #include "artdaq-utilities/Plugins/MetricData.hh"
6 
7 namespace artdaq {
12 {
13 public:
19  SystemMetricCollector(bool processMetrics, bool systemMetrics);
20 
25  double GetSystemCPUUsagePercent();
31 
36  unsigned long GetAvailableRAM();
41  unsigned long GetBufferedRAM();
46  unsigned long GetTotalRAM();
52  double GetAvailableRAMPercent(bool buffers);
57  unsigned long GetProcessMemUsage();
63 
68  unsigned long GetNetworkReceiveBytes();
73  unsigned long GetNetworkSendBytes();
78  unsigned long GetNetworkReceiveErrors();
83  unsigned long GetNetworkSendErrors();
84 
89  std::list<std::unique_ptr<MetricData>> SendMetrics();
90 
91 private:
92  struct cpustat
93  {
94  unsigned long long user, nice, system, idle, iowait, irq, softirq;
95  unsigned long long totalUsage, total;
96  cpustat()
97  : user(0), nice(0), system(0), idle(0), iowait(0), irq(0), softirq(0), totalUsage(0), total(0) {}
98  };
99  cpustat ReadProcStat_();
100 
101  struct netstat
102  {
103  unsigned long long send_bytes, recv_bytes, send_errs, recv_errs;
104  std::chrono::steady_clock::time_point collectionTime;
105  netstat()
106  : send_bytes(0), recv_bytes(0), send_errs(0), recv_errs(0) {}
107  };
108  netstat ReadProcNetDev_();
109  void UpdateNetstat_();
110 
111  cpustat lastCPU_;
112  struct tms lastProcessCPUTimes_;
113  clock_t lastProcessCPUTime_;
114  netstat thisNetStat_;
115  netstat lastNetStat_;
116  bool sendProcessMetrics_;
117  bool sendSystemMetrics_;
118 };
119 } // namespace artdaq
unsigned long GetAvailableRAM()
Get the amount of available RAM in the system
unsigned long GetProcessMemUsage()
Get the amount of RAM being used by this process
unsigned long GetNetworkReceiveErrors()
Get the number of network receive errors in the last network collection interval (1.0 s)
unsigned long GetBufferedRAM()
Get the amount of RAM currently being used for cache
std::list< std::unique_ptr< MetricData > > SendMetrics()
Send the configured metrics
unsigned long GetNetworkSendBytes()
Get the amount of data sent to the network in the last network collection interval (1...
Collects metrics from the system, using proc filesystem or kernel API calls
double GetAvailableRAMPercent(bool buffers)
Get the percentage of available RAM
double GetProcessCPUUsagePercent()
Return the current amount of CPU usage for the current process, %
unsigned long GetTotalRAM()
Get the total amount of RAM in the system
SystemMetricCollector(bool processMetrics, bool systemMetrics)
SystemMetricCollector Constructor
double GetProcessMemUsagePercent()
Get the amount of RAM being used by this process
double GetSystemCPUUsagePercent()
Return the current overall system CPU usage in %
unsigned long GetNetworkReceiveBytes()
Get the amount of data received from the network in the last network collection interval (1...
unsigned long GetNetworkSendErrors()
Get the number of network send errors in the last network collection interval (1.0 s) ...