Created
July 26, 2019 08:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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