-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpi-stats.cpp
392 lines (338 loc) · 11.8 KB
/
pi-stats.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include <thread>
using namespace std::chrono_literals;
extern "C" {
#include <getopt.h>
#include <unistd.h>
#include <bcm_host.h>
#include <interface/vmcs_host/vc_vchi_gencmd.h>
}
void fatal(const std::string& msg) {
std::cerr << msg << std::endl;
std::exit(EXIT_FAILURE);
}
bool has_prefix(const std::string& s, const std::string& prefix) {
return s.compare(0, prefix.length(), prefix) == 0;
}
bool has_prefix(const std::string& s, const char prefix) {
return s.length() > 0 && s.at(0) == prefix;
}
void trim_prefix(std::string& s, const std::string& prefix) {
if (has_prefix(s, prefix)) {
s.erase(0, prefix.length());
}
}
void trim_prefix(std::string& s, const char prefix) {
if (has_prefix(s, prefix)) {
s.erase(0, 1);
}
}
bool has_suffix(const std::string& s, const std::string& suffix) {
return s.compare(s.length() - suffix.length(), suffix.length(), suffix) ==
0;
}
bool has_suffix(const std::string& s, const char prefix) {
return s.length() > 0 && s.at(s.length() - 1) == prefix;
}
void trim_suffix(std::string& s, const char suffix) {
if (has_suffix(s, suffix)) {
s.pop_back();
}
}
void trim_suffix(std::string& s, const std::string& suffix) {
if (has_suffix(s, suffix)) {
s.erase(s.length() - suffix.length());
}
}
void trim_right(std::string& s, const std::string& cutset) {
s.erase(std::find_if(s.rbegin(), s.rend(),
[&cutset](int ch) {
return cutset.find(ch) == std::string::npos;
})
.base(),
s.end());
}
void trim_right(std::string& s, const char c) {
s.erase(std::find_if(s.rbegin(), s.rend(), [c](int ch) { return c != ch; })
.base(),
s.end());
}
void trim_left(std::string& s, const std::string& cutset) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [&cutset](int ch) {
return cutset.find(ch) == std::string::npos;
}));
}
void trim_left(std::string& s, const char c) {
s.erase(s.begin(),
std::find_if(s.begin(), s.end(), [c](int ch) { return c != ch; }));
}
std::optional<std::string> find_line(const std::string& s,
const std::string& prefix) {
size_t i = 0, j = 0;
while ((i = s.find('\n', j)) != std::string::npos) {
auto line = s.substr(j, i - j);
if (has_prefix(line, prefix)) {
return line;
}
j = i + 1;
}
return {};
}
void mark_int(std::string& n) { n += "i"; }
using VCFunc =
std::function<void(std::string&, const std::string&, char* buf, size_t)>;
struct Cmd {
Cmd(const std::string name, const std::string arg, VCFunc func)
: name(name), arg(arg), func(func) {}
Cmd(const std::string name, VCFunc func) : name(name), func(func) {}
std::string name;
std::optional<std::string> arg;
VCFunc func;
};
std::string hostname() {
auto buf = (char*)calloc(HOST_NAME_MAX, 1);
if (gethostname(buf, HOST_NAME_MAX - 1)) {
return "???";
}
return std::string(buf);
}
void measure_temp(std::string& dst, const std::string&, char* buf, size_t len) {
if (vc_gencmd(buf, len, "measure_temp")) {
throw std::runtime_error("unable to measure CPU temperature");
}
int n;
char* val;
if (vc_gencmd_string_property(buf, "temp", &val, &n) == 0) {
throw std::runtime_error("unable to parse vc_gencmd output");
}
auto&& t = std::string(val, (size_t)n);
trim_suffix(t, "'C");
dst += t;
}
void measure_clock(std::string& dst, const std::string& arg, char* buf,
size_t len) {
if (vc_gencmd(buf, len, "%s %s", "measure_clock", arg.c_str())) {
throw std::runtime_error("unable to measure " + arg + " frequency");
}
// TODO(eric): vc_gencmd_string_property does not work with
// frequency(dd)=ddddd.
auto&& t = std::string(buf);
const auto i = t.find('=');
if (i != std::string::npos) {
t.erase(0, i + 1);
}
mark_int(t);
dst += t;
}
void measure_volts(std::string& dst, const std::string& arg, char* buf,
size_t len) {
if (vc_gencmd(buf, len, "%s %s", "measure_volts", arg.c_str())) {
throw std::runtime_error("unable to measure " + arg + " voltage");
}
int n;
char* val;
if (vc_gencmd_string_property(buf, "volt", &val, &n) == 0) {
throw std::runtime_error("unable to parse vc_gencmd output");
}
auto&& t = std::string(val, (size_t)n);
trim_suffix(t, 'V');
trim_right(t, '0');
dst += t;
}
void get_config(std::string& dst, const std::string& arg, char* buf,
size_t len) {
if (vc_gencmd(buf, len, "%s %s", "get_config", arg.c_str())) {
throw std::runtime_error("unable to measure " + arg + " voltage");
}
int n;
char* val;
if (vc_gencmd_string_property(buf, arg.c_str(), &val, &n) == 0) {
throw std::runtime_error("unable to parse vc_gencmd output");
}
auto&& t = std::string(val, n);
if (t != "0") {
t += "000000";
}
mark_int(t);
dst += t;
}
void get_mem(std::string& dst, const std::string& arg, char* buf, size_t len) {
if (vc_gencmd(buf, len, "%s %s", "get_mem", arg.c_str())) {
throw std::runtime_error("unable to measure " + arg + " voltage");
}
int n;
char* val;
if (vc_gencmd_string_property(buf, arg.c_str(), &val, &n) == 0) {
throw std::runtime_error("unable to parse vc_gencmd output");
}
auto&& t = std::string(val, (size_t)n);
trim_suffix(t, 'M');
if (t != "0") {
t += "000000i";
} else {
t += 'i';
}
dst += t;
}
void mem_oom_count(std::string& dst, const std::string&, char* buf,
size_t len) {
if (vc_gencmd(buf, len, "mem_oom")) {
throw std::runtime_error("unable to measure OOM errors");
}
static const std::string prefix = "oom events";
auto&& line = find_line(std::string(buf), prefix);
if (!line.has_value()) {
throw std::runtime_error("unable to find '" + prefix + "' line");
}
auto&& t = line.value();
trim_prefix(t, prefix);
trim_prefix(t, ':');
trim_left(t, ' ');
mark_int(t);
dst += t;
}
void mem_oom_ms(std::string& dst, const std::string&, char* buf, size_t len) {
if (vc_gencmd(buf, len, "mem_oom")) {
throw std::runtime_error("unable to measure time spent in OOM handler");
}
static const std::string prefix = "total time in oom handler";
auto&& line = find_line(std::string(buf), prefix);
if (!line.has_value()) {
throw std::runtime_error("unable to find '" + prefix + "' line");
}
auto&& t = line.value();
trim_prefix(t, prefix);
trim_prefix(t, ':');
trim_left(t, ' ');
trim_suffix(t, " ms");
mark_int(t);
dst += t;
}
VCFunc mem_reloc_stats(const std::string& prefix) {
auto fn = [=](std::string& dst, const std::string&, char* buf,
size_t len) -> void {
if (vc_gencmd(buf, len, "mem_reloc_stats")) {
throw std::runtime_error("unable to measure memory reloc stats");
}
auto&& line = find_line(std::string(buf), prefix);
if (!line.has_value()) {
throw std::runtime_error("unable to find '" + prefix + "' line");
}
auto&& t = line.value();
trim_prefix(t, prefix);
trim_prefix(t, ':');
trim_left(t, ' ');
mark_int(t);
dst += t;
};
return fn;
}
const std::map<std::string, Cmd> cmds = {
{"soc_temp", Cmd("measure_temp", measure_temp)},
{"arm_freq", Cmd("measure_clock", "arm", measure_clock)},
{"core_freq", Cmd("measure_clock", "core", measure_clock)},
{"h264_freq", Cmd("measure_clock", "h264", measure_clock)},
{"isp_freq", Cmd("measure_clock", "isp", measure_clock)},
{"v3d_freq", Cmd("measure_clock", "v3d", measure_clock)},
{"uart_freq", Cmd("measure_clock", "uart", measure_clock)},
{"pwm_freq", Cmd("measure_clock", "pwm", measure_clock)},
{"emmc_freq", Cmd("measure_clock", "emmc", measure_clock)},
{"pixel_freq", Cmd("measure_clock", "pixel", measure_clock)},
{"vec_freq", Cmd("measure_clock", "vec", measure_clock)},
{"hdmi_freq", Cmd("measure_clock", "hdmi", measure_clock)},
{"dpi_freq", Cmd("measure_clock", "dpi", measure_clock)},
{"core_volts", Cmd("measure_volts", "core", measure_volts)},
{"sdram_c_volts", Cmd("measure_volts", "sdram_c", measure_volts)},
{"sdram_i_volts", Cmd("measure_volts", "sdram_i", measure_volts)},
{"sdram_p_volts", Cmd("measure_volts", "sdram_p", measure_volts)},
{"config_arm_freq", Cmd("get_config", "arm_freq", get_config)},
{"config_core_freq", Cmd("get_config", "core_freq", get_config)},
{"config_gpu_freq", Cmd("get_config", "gpu_freq", get_config)},
{"config_sdram_freq", Cmd("get_config", "sdram_freq", get_config)},
{"arm_mem", Cmd("get_mem", "arm", get_mem)},
{"gpu_mem", Cmd("get_mem", "gpu", get_mem)},
{"malloc_total_mem", Cmd("get_mem", "malloc_total", get_mem)},
{"malloc_mem", Cmd("get_mem", "malloc", get_mem)},
{"reloc_total_mem", Cmd("get_mem", "reloc_total", get_mem)},
{"reloc_mem", Cmd("get_mem", "reloc", get_mem)},
{"oom_count", Cmd("mem_oom", mem_oom_count)},
{"oom_ms", Cmd("mem_oom", mem_oom_ms)},
{"mem_reloc_allocation_failures",
Cmd("mem_reloc_stats", mem_reloc_stats("alloc failures"))},
{"mem_reloc_compactions",
Cmd("mem_reloc_stats", mem_reloc_stats("compactions"))},
{"mem_reloc_legacy_block_failures",
Cmd("mem_reloc_stats", mem_reloc_stats("legacy block fails"))},
};
const struct option long_opts[] = {{"step", required_argument, NULL, 's'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}};
int main(int argc, char* argv[]) {
auto step = 1s;
int ch;
while ((ch = getopt_long(argc, argv, "s:h", long_opts, NULL)) != -1) {
switch (ch) {
case 's':
step = std::chrono::seconds(std::stoi(optarg));
break;
case 'h':
fatal("Usage: " + std::string(argv[0]) + " [-step seconds]");
break;
default:
return 1;
}
}
vcos_init();
VCHI_INSTANCE_T vchi_instance;
if (vchi_initialise(&vchi_instance) != 0) {
fatal("unable to initialize VCHI instance");
}
if (vchi_connect(NULL, 0, vchi_instance) != 0) {
fatal("unable to create VCHI connection");
}
VCHI_CONNECTION_T* vchi_connection = NULL;
vc_vchi_gencmd_init(vchi_instance, &vchi_connection, 1);
auto host = hostname();
constexpr size_t N = 4096;
char tmp[N];
std::string buf;
while (true) {
// Measurement and tag set.
buf += "raspberry_pi,host=";
buf += host;
buf += ' ';
// Field set.
for (const auto& e : cmds) {
buf += e.first;
buf += '=';
auto&& val = e.second;
try {
val.func(buf, val.arg.value_or(""), tmp, N);
} catch (const std::exception& e) {
fatal(e.what());
}
buf += ',';
}
trim_suffix(buf, ',');
// Timestamp.
buf += ' ';
buf += std::to_string(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
.count());
std::cout << buf << std::endl;
buf.clear();
std::this_thread::sleep_for(std::chrono::seconds(step));
}
vc_gencmd_stop();
if (vchi_disconnect(vchi_instance) != 0) {
fatal("VCHI disconnect failed");
}
}