Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include <iostream>
#include <fstream>
#include <vector>
#include <chrono>
#include <thread>
struct Jiffies {
int work, total;
};
Jiffies jiffies(){
int work, total;
std::ifstream cpu("/proc/stat");
cpu.ignore(4, ' ');
for (int i = 0; i < 10; ++i) {
int value;
cpu >> value;
if(i < 3)
work += value;
total += value;
}
return {work, total};
}
int main(){
auto prev = jiffies();
while(true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
auto current = jiffies();
float usage = (current.work - prev.work) * 1.0 / (current.total - prev.total);
std::cout << usage * 100 << '\n';
prev = current;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment