Skip to content

Commit 92170c1

Browse files
committed
add CPUInfo::Time struct
1 parent 73d95f8 commit 92170c1

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

worker/src/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
#include "metrics-collector.hpp"
2+
#include "file.hpp"
3+
14
#include <iostream>
25
#include <cstring>
36
#include <thread>
47
#include <signal.h>
5-
#include "file.hpp"
6-
#include "metrics-collector.hpp"
78

89
void print_usage(const std::string& program_name) {
910
std::cerr << "Usage: " << program_name << " hash"

worker/src/metrics-collector.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ MetricsCollector::MetricsCollector(const char *gateway_address, const char *gate
4747
while (true) {
4848
CPUInfo cpu;
4949

50-
file >> cpu_name >> cpu.last_total_user >> cpu.last_total_user_low
51-
>> cpu.last_total_sys >> cpu.last_total_idle
50+
file >> cpu_name >> cpu.time.user >> cpu.time.user_low
51+
>> cpu.time.sys >> cpu.time.idle
5252
>> ign >> ign >> ign >> ign >> ign >> ign;
5353

5454
if (cpu_name.find("cpu") != 0)
@@ -100,40 +100,35 @@ MetricsCollector::~MetricsCollector()
100100
void MetricsCollector::GetCPUUsage()
101101
{
102102
std::ifstream file("/proc/stat");
103-
uint64_t total_user, total_user_low, total_sys, total_idle, total;
103+
CPUInfo::Time cur_time;
104104
double percent;
105105

106106
std::string cpu_name;
107107
int ign;
108108

109109
while (true) {
110-
file >> cpu_name >> total_user >> total_user_low >> total_sys >> total_idle
110+
file >> cpu_name >> cur_time.user >> cur_time.user_low >> cur_time.sys >> cur_time.idle
111111
>> ign >> ign >> ign >> ign >> ign >> ign;
112112

113113
if (cpu_name.find("cpu") != 0)
114114
break;
115115

116116
CPUInfo &cpu = cpu_usage[cpu_name];
117-
if (total_user < cpu.last_total_user || total_user_low < cpu.last_total_user_low ||
118-
total_sys < cpu.last_total_sys || total_idle < cpu.last_total_idle) {
117+
if (cur_time.user < cpu.time.user || cur_time.user_low < cpu.time.user_low ||
118+
cur_time.sys < cpu.time.sys || cur_time.idle < cpu.time.idle) {
119119
// overflow detection
120120
percent = -1.0;
121121
}
122122
else {
123-
total = (total_user - cpu.last_total_user) + (total_user_low - cpu.last_total_user_low) +
124-
(total_sys - cpu.last_total_sys);
123+
uint64_t total = (cur_time.user - cpu.time.user) + (cur_time.user_low - cpu.time.user_low) +
124+
(cur_time.sys - cpu.time.sys);
125125

126126
percent = total;
127-
total += (total_idle - cpu.last_total_idle);
128-
percent /= total;
129-
percent *= 100;
127+
total += (cur_time.idle - cpu.time.idle);
128+
percent = (total == 0) ? -1.0 : (percent / total) * 100.0;
130129
}
131130

132-
cpu.last_total_user = total_user;
133-
cpu.last_total_user_low = total_user_low;
134-
cpu.last_total_sys = total_sys;
135-
cpu.last_total_idle = total_idle;
136-
131+
cpu.time = cur_time;
137132
cpu.gauge->Set(percent);
138133
}
139134

worker/src/metrics-collector.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ class MetricsCollector {
99

1010
struct CPUInfo {
1111
prometheus::Gauge *gauge;
12-
uint64_t last_total_user;
13-
uint64_t last_total_user_low;
14-
uint64_t last_total_sys;
15-
uint64_t last_total_idle;
12+
13+
struct Time {
14+
uint64_t user;
15+
uint64_t user_low;
16+
uint64_t sys;
17+
uint64_t idle;
18+
};
19+
20+
Time time;
1621
};
1722

1823
std::unordered_map<std::string, CPUInfo> cpu_usage;

0 commit comments

Comments
 (0)