-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiostat.cpp
More file actions
133 lines (116 loc) · 4.65 KB
/
Copy pathiostat.cpp
File metadata and controls
133 lines (116 loc) · 4.65 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <cstdio>
#include <cstdlib>
#include <thread>
#include <chrono>
#include <string>
#include <vector>
#include <cfbox/applet.hpp>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/proc.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "iostat",
.version = CFBOX_VERSION_STRING,
.one_line = "report CPU and I/O statistics",
.usage = "iostat [-c COUNT] [-d DELAY]",
.options = " -c N number of reports (default 1)\n"
" -d N delay in seconds between reports (default 1)",
.extra = "",
};
auto print_iostat(const std::vector<cfbox::proc::DiskStat>& stats) -> void {
std::printf("%-10s %10s %10s %10s %10s %10s\n",
"Device", "r/s", "w/s", "rkB/s", "wkB/s", "await");
for (const auto& ds : stats) {
if (ds.device.find("ram") != std::string::npos ||
ds.device.find("loop") != std::string::npos) continue;
std::printf("%-10s %10llu %10llu %10llu %10llu %10llu\n",
ds.device.c_str(),
static_cast<unsigned long long>(ds.reads),
static_cast<unsigned long long>(ds.writes),
static_cast<unsigned long long>(ds.sectors_read / 2),
static_cast<unsigned long long>(ds.sectors_written / 2),
static_cast<unsigned long long>(ds.ms_ios));
}
}
auto print_delta(const std::vector<cfbox::proc::DiskStat>& prev,
const std::vector<cfbox::proc::DiskStat>& curr,
double interval) -> void {
std::printf("%-10s %10s %10s %10s %10s\n",
"Device", "r/s", "w/s", "rkB/s", "wkB/s");
for (const auto& c : curr) {
if (c.device.find("ram") != std::string::npos ||
c.device.find("loop") != std::string::npos) continue;
double dr = static_cast<double>(c.reads);
double dw = static_cast<double>(c.writes);
double drkb = static_cast<double>(c.sectors_read / 2);
double dwkb = static_cast<double>(c.sectors_written / 2);
for (const auto& p : prev) {
if (p.device == c.device) {
dr = static_cast<double>(c.reads - p.reads) / interval;
dw = static_cast<double>(c.writes - p.writes) / interval;
drkb = static_cast<double>((c.sectors_read - p.sectors_read) / 2) / interval;
dwkb = static_cast<double>((c.sectors_written - p.sectors_written) / 2) / interval;
break;
}
}
std::printf("%-10s %10.0f %10.0f %10.0f %10.0f\n",
c.device.c_str(), dr, dw, drkb, dwkb);
}
}
} // anonymous namespace
auto iostat_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'c', true, "count"},
cfbox::args::OptSpec{'d', true, "delay"},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
int count = 1;
double delay = 1.0;
if (auto v = parsed.get('c')) {
auto parsed_count = cfbox::args::parse_int(std::string(*v));
if (!parsed_count) {
CFBOX_ERR("iostat", "%s", parsed_count.error().msg.c_str());
return 2;
}
count = *parsed_count;
}
if (auto v = parsed.get('d')) delay = std::stod(std::string(*v));
auto first = cfbox::proc::read_diskstats();
if (!first) {
CFBOX_ERR("iostat", "%s", first.error().msg.c_str());
return 1;
}
auto cpu = cfbox::proc::read_cpu_stats();
if (cpu) {
double total = static_cast<double>(cpu->total());
if (total > 0.0) {
auto pct = [&](std::uint64_t v) -> double {
return 100.0 * static_cast<double>(v) / total;
};
double idle_pct = pct(cpu->idle_time());
std::printf("avg-cpu: %%user %%nice %%system %%iowait %%steal %%idle\n");
std::printf(" %6.1f %6.1f %9.1f %8.1f %7.1f %7.1f\n",
pct(cpu->user), pct(cpu->nice), pct(cpu->system),
pct(cpu->iowait), pct(cpu->steal), idle_pct);
}
std::printf("\n");
}
if (count <= 1) {
print_iostat(*first);
return 0;
}
auto prev = *first;
for (int i = 1; i < count; ++i) {
std::this_thread::sleep_for(
std::chrono::milliseconds(static_cast<int>(delay * 1000)));
auto curr = cfbox::proc::read_diskstats();
if (!curr) break;
std::printf("\n");
print_delta(prev, *curr, delay);
prev = *curr;
}
return 0;
}