-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.cpp
34 lines (31 loc) · 1.16 KB
/
cpu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <fstream>
#include <iostream>
#include <numeric>
#include <unistd.h>
#include <vector>
std::vector<size_t> get_cpu_times() {
std::ifstream proc_stat("/proc/stat");
proc_stat.ignore(5, ' '); // Skip the 'cpu' prefix.
std::vector<size_t> times;
for (size_t time; proc_stat >> time; times.push_back(time));
return times;
}
bool get_cpu_times(size_t &idle_time, size_t &total_time) {
const std::vector<size_t> cpu_times = get_cpu_times();
if (cpu_times.size() < 4)
return false;
idle_time = cpu_times[3];
total_time = std::accumulate(cpu_times.begin(), cpu_times.end(), 0);
return true;
}
int main(int, char *[]) {
size_t previous_idle_time=0, previous_total_time=0;
for (size_t idle_time, total_time; get_cpu_times(idle_time, total_time); sleep(1)) {
const float idle_time_delta = idle_time - previous_idle_time;
const float total_time_delta = total_time - previous_total_time;
const float utilization = 100.0 * (1.0 - idle_time_delta / total_time_delta);
std::cout << utilization << '%' << std::endl;
previous_idle_time = idle_time;
previous_total_time = total_time;
}
}