Skip to content

Instantly share code, notes, and snippets.

@adrian17
Last active June 18, 2017 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrian17/519cca9faf7d3cebd963a58726169670 to your computer and use it in GitHub Desktop.
Save adrian17/519cca9faf7d3cebd963a58726169670 to your computer and use it in GitHub Desktop.
#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