-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.cpp
49 lines (41 loc) · 1.49 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <chrono>
#include <iostream>
#include <sstream>
#include <thread>
#include "cpu_usage.h"
int main(int argc, char* argv[]) {
int sleep_ms = 1000;
// parse sampling duration from command line argument
if (argc == 2) {
std::istringstream ss(argv[1]);
ss >> sleep_ms;
}
cpu_usage usage;
cpu_usage::cpu_info_map_t cpu_usage = usage.get_cpu_usage();
std::cout << "average CPU usage since start:" << std::endl;
for (auto const& kv : cpu_usage) {
std::cout << kv.first << " "
<< kv.second.usage_ << "% usage "
<< kv.second.user_ << "% user "
<< kv.second.system_ << "% system "
<< kv.second.iowait_ << "% iowait "
<< kv.second.irq_ << "% irq "
<< kv.second.softirq_ << "% softirq"
<< std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
cpu_usage = usage.get_cpu_usage();
std::cout << std::endl
<< "current CPU usage:" << std::endl;
for (auto const& kv : cpu_usage) {
std::cout << kv.first << " "
<< kv.second.usage_ << "% usage "
<< kv.second.user_ << "% user "
<< kv.second.system_ << "% system "
<< kv.second.iowait_ << "% iowait "
<< kv.second.irq_ << "% irq "
<< kv.second.softirq_ << "% softirq"
<< std::endl;
}
return 0;
}