Skip to content

Instantly share code, notes, and snippets.

@offlinemark
Created August 6, 2021 17:43
Show Gist options
  • Save offlinemark/a18ea2dbe2f08fc6e409395a590fa6b2 to your computer and use it in GitHub Desktop.
Save offlinemark/a18ea2dbe2f08fc6e409395a590fa6b2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <thread>
#include <vector>
#include <chrono>
using namespace std::chrono_literals;
void burst(unsigned long n) {
std::cout << "burst\n";
while (n--);
}
void burst_n_threads(unsigned n) {
std::vector<std::thread> threads;
/* auto x = 1000000000000; */
auto x = 100000000;
for (unsigned i = 0; i < n; i++) {
auto th = std::thread(burst, x);
threads.push_back(std::move(th));
}
for (auto& th : threads) {
th.join();
}
}
void burst_n_sec(unsigned n) {
auto interv = std::chrono::seconds(n);
auto start = std::chrono::high_resolution_clock::now();
while( (std::chrono::high_resolution_clock::now() - start) < interv );
return;
}
void burst_nn_sec(unsigned n) {
auto interv = std::chrono::seconds(n);
auto start = std::chrono::high_resolution_clock::now();
while( (std::chrono::high_resolution_clock::now() - start) < interv ) {
/* std::this_thread::sleep_for(10us); // 30 */
/* std::this_thread::sleep_for(1us); // 63 */
/* std::this_thread::sleep_for(900ns); //56 */
/* std::this_thread::sleep_for(100ns); // 38 -- why does it go down? */
// this is smaller than 1us. therefore, it should have a higher util
// yet it has a lower util.
/* std::this_thread::sleep_for(10ns); // 39. doesnt matter. too small? */
/* std::this_thread::sleep_for(1000ns); //55 */
/* std::this_thread::sleep_for(1100ns); // 55 */
/* std::this_thread::sleep_for(1999ns); // 58 */
std::this_thread::sleep_for(3000ns); //51
/* std::this_thread::sleep_for(1000ns); // wtf why does a smaller sleep i */
/* std::this_thread::sleep_for(10000ns); // 30ish */
// the smaller sleep (100ns) causes cpu util to go up
}
return;
}
void full_burst(unsigned sec, unsigned thread_count=8) {
std::cout << "burst " << sec << " " << thread_count << "\n";
std::vector<std::thread> threads;
for (unsigned i = 0; i < thread_count; i++) {
auto th = std::thread(burst_n_sec, sec);
threads.push_back(std::move(th));
}
for (auto& th : threads) {
th.join();
}
}
int main() {
/* std::cout << "5\n"; */
/* std::this_thread::sleep_for(1s); */
/* std::cout << "4\n"; */
/* std::this_thread::sleep_for(1s); */
/* std::cout << "3\n"; */
/* std::this_thread::sleep_for(1s); */
/* std::cout << "2\n"; */
/* std::this_thread::sleep_for(1s); */
/* std::cout << "1\n"; */
/* std::this_thread::sleep_for(1s); */
/* std::cout << "PRESS\n"; */
/* burst_n_sec(10); */
// square wave
/* for (int i = 0; i < 5; i++) { */
/* full_burst(30); */
/* std::this_thread::sleep_for(30s); */
/* } */
// chunkish rise and fall
/* for (unsigned i = 1; i < 8; i++) { */
/* std::cout << "burst 15 " << i << "\n"; */
/* full_burst(15, i); */
/* } */
/* std::cout << "burst 15 " << 8 << "\n"; */
/* full_burst(15, 8); */
/* for (unsigned i = 7; i > 0; i--) { */
/* std::cout << "burst 15 " << i << "\n"; */
/* full_burst(15, i); */
/* } */
// steep rise
/* std::cout << "burst 15 1"; */
/* full_burst(15, 1); */
/* std::cout << "burst 15 4"; */
/* full_burst(15, 4); */
/* std::cout << "burst 15 8"; */
/* full_burst(15, 8); */
// smooth saw
/* for (int x = 0; x < 4; x++) { */
/* for (unsigned i = 1; i < 8; i++) { */
/* std::cout << "burst 5 " << i << "\n"; */
/* full_burst(5, i); */
/* } */
/* std::cout << "burst 5 " << 8 << "\n"; */
/* full_burst(5, 8); */
/* for (unsigned i = 7; i > 0; i--) { */
/* std::cout << "burst 5 " << i << "\n"; */
/* full_burst(5, i); */
/* } */
/* } */
/* for (int x = 0; x < 3; x++) { */
/* for (unsigned i = 1; i < 8; i++) { */
/* full_burst(4, i); */
/* } */
/* full_burst(4, 8); */
/* for (unsigned i = 7; i > 0; i--) { */
/* full_burst(4, i); */
/* } */
/* } */
/* for (int x = 0; x < 3; x++) { */
/* for (unsigned i = 1; i < 8; i++) { */
/* full_burst(3, i); */
/* } */
/* full_burst(3, 8); */
/* for (unsigned i = 7; i > 0; i--) { */
/* full_burst(3, i); */
/* } */
/* } */
// chunk. each gets 2 pts on the graph
/* for (int x = 0; x < 3; x++) { */
/* for (unsigned i = 1; i < 8; i++) { */
/* full_burst(10, i); */
/* } */
/* full_burst(10, 8); */
/* for (unsigned i = 7; i > 0; i--) { */
/* full_burst(10, i); */
/* } */
/* } */
// lil routine
/* for (unsigned i = 1; i < 8; i++) { */
/* full_burst(4, i); */
/* } */
/* full_burst(4, 8); */
/* full_burst(4, 8); */
/* std::this_thread::sleep_for(16s); */
/* full_burst(10, 8); */
/* std::this_thread::sleep_for(16s); */
/* full_burst(4, 8); */
/* for (unsigned i = 7; i > 0; i--) { */
/* full_burst(4, i); */
/* } */
// goes 100%
auto th = std::thread(burst_n_sec, 20);
auto th2 = std::thread(burst_n_sec, 20);
burst_nn_sec(20);
th.join();
th2.join();
return 0;
// can prob use steady clock if we just need sec granularity/resolution
// w/e we never know
auto t1 = std::chrono::high_resolution_clock::now();
/* auto foursec = std::chrono::seconds(4); */
std::this_thread::sleep_for(3s);
/* std::this_thread::sleep_for(foursec); */
auto t2 = std::chrono::high_resolution_clock::now();
/* std::chrono::duration<double, std::chrono::seconds> dur = t2 - t1; */
auto ff = std::chrono::duration_cast<std::chrono::seconds>(t2 - t1).count();
std::cout << "waited " << ff << " sec\n";
// todo next -- figure out how to pass the 4s into a function.
// that function then take the start time.
// then busy looop, checking if the now time, duration casted
// is >= the 4s. then stop the loop.
return 0;
int ee;
int xx = ee+33;
burst_n_threads(9);
/* /1* burst(100000000000000); *1/ */
/* /1* auto x = 100000000000000; *1/ */
/* auto x = 1000000000000; */
/* auto th1 = std::thread(burst, x); */
/* auto th2 = std::thread(burst, x); */
/* auto th3 = std::thread(burst, x); */
/* th1.join(); */
/* th2.join(); */
/* th3.join(); */
std::cout << "done\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment