artdaq_utilities  v1_07_01
SystemMetricCollector.hh
1 #include <sys/times.h>
2 #include <list>
3 #include <memory>
4 #include <string>
5 #include <unordered_map>
6 #include "artdaq-utilities/Plugins/MetricData.hh"
7 
8 namespace artdaq {
13 {
14 public:
20  SystemMetricCollector(bool processMetrics, bool systemMetrics);
21 
25  void GetSystemCPUUsage();
31 
36  uint64_t GetAvailableRAM();
41  uint64_t GetBufferedRAM();
46  uint64_t GetTotalRAM();
52  double GetAvailableRAMPercent(bool buffers);
57  uint64_t GetProcessMemUsage();
63 
69  uint64_t GetNetworkReceiveBytes(std::string ifname);
75  uint64_t GetNetworkSendBytes(std::string ifname);
81  uint64_t GetNetworkReceiveErrors(std::string ifname);
87  uint64_t GetNetworkSendErrors(std::string ifname);
92  uint64_t GetNetworkTCPRetransSegs();
93 
98  std::list<std::string> GetNetworkInterfaceNames();
99 
104  std::list<std::unique_ptr<MetricData>> SendMetrics();
105 
106 private:
107  struct cpustat
108  {
109  uint64_t user{0}, nice{0}, system{0}, idle{0}, iowait{0}, irq{0}, softirq{0};
110  uint64_t totalUsage{0}, total{0};
111  };
112  cpustat ReadProcStat_();
113  static size_t GetCPUCount_(); // Read /proc/stat, count lines beyond the first that start with "cpu"
114  size_t cpuCount_;
115  double nonIdleCPUPercent_; // user + nice + system + iowait + irq + softirq
116  double userCPUPercent_; // Includes nice
117  double systemCPUPercent_;
118  double idleCPUPercent_;
119  double iowaitCPUPercent_;
120  double irqCPUPercent_; // includes softirq
121 
122  struct netstat
123  {
124  uint64_t send_bytes{0}, recv_bytes{0}, send_errs{0}, recv_errs{0};
125  std::chrono::steady_clock::time_point collectionTime;
126  };
127  struct netstats
128  {
129  std::unordered_map<std::string, netstat> stats;
130  std::chrono::steady_clock::time_point collectionTime;
131  };
132  netstats ReadProcNetDev_();
133  void UpdateNetstat_();
134 
135  cpustat lastCPU_;
136  struct tms lastProcessCPUTimes_;
137  clock_t lastProcessCPUTime_;
138  netstats thisNetStat_;
139  netstats lastNetStat_;
140  bool sendProcessMetrics_;
141  bool sendSystemMetrics_;
142 };
143 } // namespace artdaq
uint64_t GetNetworkTCPRetransSegs()
Return the current number of TCP (total) segments retransmitted, segments
uint64_t GetTotalRAM()
Get the total amount of RAM in the system
uint64_t GetNetworkReceiveErrors(std::string ifname)
Get the number of network receive errors in the last network collection interval (1.0 s)
uint64_t GetNetworkSendBytes(std::string ifname)
Get the amount of data sent to the network in the last network collection interval (1...
uint64_t GetProcessMemUsage()
Get the amount of RAM being used by this process
std::list< std::unique_ptr< MetricData > > SendMetrics()
Send the configured metrics
uint64_t GetNetworkSendErrors(std::string ifname)
Get the number of network send errors in the last network collection interval (1.0 s) ...
Collects metrics from the system, using proc filesystem or kernel API calls
double GetAvailableRAMPercent(bool buffers)
Get the percentage of available RAM
std::list< std::string > GetNetworkInterfaceNames()
Get the names of the local network interfaces.
void GetSystemCPUUsage()
Calculate the system CPU usage percentages
uint64_t GetAvailableRAM()
Get the amount of available RAM in the system
double GetProcessCPUUsagePercent()
Return the current amount of CPU usage for the current process, %
SystemMetricCollector(bool processMetrics, bool systemMetrics)
SystemMetricCollector Constructor
double GetProcessMemUsagePercent()
Get the amount of RAM being used by this process
uint64_t GetNetworkReceiveBytes(std::string ifname)
Get the amount of data received from the network in the last network collection interval (1...
uint64_t GetBufferedRAM()
Get the amount of RAM currently being used for cache