artdaq_utilities  v1_06_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 
94  std::list<std::string> GetNetworkInterfaceNames();
99  std::list<std::unique_ptr<MetricData>> SendMetrics();
100 
101 private:
102  struct cpustat
103  {
104  uint64_t user{0}, nice{0}, system{0}, idle{0}, iowait{0}, irq{0}, softirq{0};
105  uint64_t totalUsage{0}, total{0};
106  };
107  cpustat ReadProcStat_();
108  static size_t GetCPUCount_(); // Read /proc/stat, count lines beyond the first that start with "cpu"
109  size_t cpuCount_;
110  double nonIdleCPUPercent_; // user + nice + system + iowait + irq + softirq
111  double userCPUPercent_; // Includes nice
112  double systemCPUPercent_;
113  double idleCPUPercent_;
114  double iowaitCPUPercent_;
115  double irqCPUPercent_; // includes softirq
116 
117  struct netstat
118  {
119  uint64_t send_bytes{0}, recv_bytes{0}, send_errs{0}, recv_errs{0};
120  std::chrono::steady_clock::time_point collectionTime;
121  };
122  struct netstats
123  {
124  std::unordered_map<std::string, netstat> stats;
125  std::chrono::steady_clock::time_point collectionTime;
126  };
127  netstats ReadProcNetDev_();
128  void UpdateNetstat_();
129 
130  cpustat lastCPU_;
131  struct tms lastProcessCPUTimes_;
132  clock_t lastProcessCPUTime_;
133  netstats thisNetStat_;
134  netstats lastNetStat_;
135  bool sendProcessMetrics_;
136  bool sendSystemMetrics_;
137 };
138 } // 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
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