@@ -47,8 +47,8 @@ MetricsCollector::MetricsCollector(const char *gateway_address, const char *gate
47
47
while (true ) {
48
48
CPUInfo cpu;
49
49
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
52
52
>> ign >> ign >> ign >> ign >> ign >> ign;
53
53
54
54
if (cpu_name.find (" cpu" ) != 0 )
@@ -100,40 +100,35 @@ MetricsCollector::~MetricsCollector()
100
100
void MetricsCollector::GetCPUUsage ()
101
101
{
102
102
std::ifstream file (" /proc/stat" );
103
- uint64_t total_user, total_user_low, total_sys, total_idle, total ;
103
+ CPUInfo::Time cur_time ;
104
104
double percent;
105
105
106
106
std::string cpu_name;
107
107
int ign;
108
108
109
109
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
111
111
>> ign >> ign >> ign >> ign >> ign >> ign;
112
112
113
113
if (cpu_name.find (" cpu" ) != 0 )
114
114
break ;
115
115
116
116
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 ) {
119
119
// overflow detection
120
120
percent = -1.0 ;
121
121
}
122
122
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 );
125
125
126
126
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 ;
130
129
}
131
130
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;
137
132
cpu.gauge ->Set (percent);
138
133
}
139
134
0 commit comments