Skip to content

Instantly share code, notes, and snippets.

@jsburklund
Created July 26, 2019 08:26
Show Gist options
  • Save jsburklund/d6f100382113add8ac39f5a880c43ef0 to your computer and use it in GitHub Desktop.
Save jsburklund/d6f100382113add8ac39f5a880c43ef0 to your computer and use it in GitHub Desktop.
Simple process to discover how CPU usage stats are aggregated with different tools
// Simple test to discover how process stats aggregate CPU usage
// Building: g++ -std=c++11 main.cpp -o mymain -lpthread
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <thread>
volatile float a, b;
void be_lazy() {
while(true) {
b = rand();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void do_work() {
while(true) {
a = sqrt(rand()/2.0);
}
}
int main() {
std::thread worker {do_work};
std::thread busy_worker {do_work};
std::thread lazy_worker {be_lazy};
do_work();
worker.join();
busy_worker.join();
lazy_worker.join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment