-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
350 lines (315 loc) · 9.78 KB
/
main.c
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
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#include "logger.h"
#include "system.h"
#include "util.h"
#include "cpu.h"
#include "disk.h"
#include "interface.h"
#include "battery.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <termios.h>
#include <signal.h>
#define error(...) { fprintf(stderr, __VA_ARGS__); exit(-1); }
#define TERM_CLEAR_SCREEN "\e[2J"
#define TERM_POSITION_HOME "\e[H"
#define TERM_ERASE_REST_OF_LINE "\e[K"
#define TERM_ERASE_DOWN "\e[J"
static struct termios orig_termios;
static void reset_terminal_mode(void)
{
tcsetattr(0, TCSANOW, &orig_termios);
}
static void set_conio_terminal_mode(void)
{
struct termios new_termios;
// take two copies - one for now, one for later
tcgetattr(0, &orig_termios);
memcpy(&new_termios, &orig_termios, sizeof(new_termios));
// register cleanup handler, and set the new terminal mode
atexit(reset_terminal_mode);
cfmakeraw(&new_termios);
tcsetattr(0, TCSANOW, &new_termios);
}
static int getch(void)
{
int r;
unsigned char c;
if ((r = read(STDIN_FILENO, &c, sizeof(c))) < 0)
return r;
return c;
}
static int wait_for_keypress(void)
{
set_conio_terminal_mode();
// Set timeout to 1.0 seconds
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
// Initialize file descriptor sets
fd_set read_fds, write_fds, except_fds;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
FD_ZERO(&except_fds);
FD_SET(STDIN_FILENO, &read_fds);
int c = -1;
if (select(STDIN_FILENO + 1, &read_fds, &write_fds, &except_fds, &timeout) == 1)
c = getch();
reset_terminal_mode();
return c;
}
volatile sig_atomic_t must_exit = 0;
void signal_handler(int signum)
{
must_exit = 1;
}
int main(int argc, char **argv)
{
// Catch SIGTERM
{
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = signal_handler;
sigaction(SIGTERM, &action, NULL);
}
struct system_t system = system_init();
struct logger_t logger;
struct logger_stat_t log_stats[128];
int log_stats_count = 0;
const char *log_filename = NULL;
// Parse command line arguments
for (int i = 1; i < argc; ++i) {
const char *arg = argv[i];
if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) {
printf(
"-h --help Print this help message\n"
"-l --log filename stat0 stat1 ... Log stats to csv file, where each stat can be:\n"
" cpuX{usage,temp,freq}\n"
" ram_{used,buffers,cached}\n"
" disk_NAME_{read,write}\n"
" iface_NAME_{read,write}\n"
" battery_NAME_{charge,current,voltage}\n");
return 0;
} else if (!strcmp(arg, "-l") || !strcmp(arg, "--log")) {
++i;
if (i == argc)
error("Log file name required\n");
log_filename = argv[i++];
while (i < argc) {
const char *stat_name = argv[i];
struct logger_stat_t stat;
int ok = 1;
if (!strncmp(stat_name, "cpu", 3)) {
char *id_end;
int id = strtol(stat_name + 3, &id_end, 10);
if (id_end != stat_name + 3) {
stat.data.cpu_id = id;
if (!strcmp(id_end, "u") || !strcmp(id_end, "usage"))
stat.type = LOGGER_CPU_USAGE;
else if (!strcmp(id_end, "t") || !strcmp(id_end, "temp"))
stat.type = LOGGER_CPU_TEMPERATURE;
else if (!strcmp(id_end, "f") || !strcmp(id_end, "freq"))
stat.type = LOGGER_CPU_FREQUENCY;
else
ok = 0;
if (ok)
log_stats[log_stats_count++] = stat;
}
} else if (!strncmp(stat_name, "ram_", 4)) {
const char *s = stat_name + 4;
if (!strcmp(s, "u") || !strcmp(s, "used"))
stat.type = LOGGER_RAM_USED;
else if (!strcmp(s, "b") || !strcmp(s, "buffers"))
stat.type = LOGGER_RAM_BUFFERS;
else if (!strcmp(s, "c") || !strcmp(s, "cached"))
stat.type = LOGGER_RAM_CACHED;
else
ok = 0;
if (ok)
log_stats[log_stats_count++] = stat;
} else if (!strncmp(stat_name, "disk_", 5)) {
const char *name_end = stat_name + 5;
while (*name_end && *name_end != '_')
++name_end;
if (*name_end == '\0') {
ok = 0;
} else {
int len = name_end - stat_name - 5;
strncpy(stat.data.disk_name, stat_name + 5, len);
stat.data.disk_name[len] = '\0';
++name_end;
if (!strcmp(name_end, "r") || !strcmp(name_end, "read"))
stat.type = LOGGER_DISK_READ;
else if (!strcmp(name_end, "w") || !strcmp(name_end, "write"))
stat.type = LOGGER_DISK_WRITE;
else
ok = 0;
if (ok)
log_stats[log_stats_count++] = stat;
}
} else if (!strncmp(stat_name, "iface_", 6)) {
const char *name_end = stat_name + 6;
while (*name_end && *name_end != '_')
++name_end;
if (*name_end == '\0') {
ok = 0;
} else {
int len = name_end - stat_name - 6;
strncpy(stat.data.iface_name, stat_name + 6, len);
stat.data.iface_name[len] = '\0';
++name_end;
if (!strcmp(name_end, "r") || !strcmp(name_end, "read"))
stat.type = LOGGER_IFACE_READ;
else if (!strcmp(name_end, "w") || !strcmp(name_end, "write"))
stat.type = LOGGER_IFACE_WRITE;
else
ok = 0;
if (ok)
log_stats[log_stats_count++] = stat;
}
} else if (!strncmp(stat_name, "battery_", 8)) {
const char *name_end = stat_name + 8;
while (*name_end && *name_end != '_')
++name_end;
if (*name_end == '\0') {
ok = 0;
} else {
int len = name_end - stat_name - 8;
strncpy(stat.data.battery_name, stat_name + 8, len);
stat.data.battery_name[len] = '\0';
++name_end;
if (!strcmp(name_end, "c") || !strcmp(name_end, "charge"))
stat.type = LOGGER_BAT_CHARGE;
else if (!strcmp(name_end, "cu") || !strcmp(name_end, "current"))
stat.type = LOGGER_BAT_CURRENT;
else if (!strcmp(name_end, "v") || !strcmp(name_end, "voltage"))
stat.type = LOGGER_BAT_VOLTAGE;
else
ok = 0;
if (ok)
log_stats[log_stats_count++] = stat;
}
} else
ok = 0;
if (!ok) {
--i;
break;
}
++i;
}
} else {
error("Unknown argument %s. Try %s --help\n", argv[i], argv[0]);
}
}
int logger_ret = logger_init(&logger, CSV, log_filename, log_stats_count,
log_stats);
if (logger_ret != 0) {
fprintf(stderr, "Failed to initialize logger: %d\n", logger_ret);
return 1;
}
// Loop forever, show CPU usage and frequency and disk usage
printf(TERM_CLEAR_SCREEN TERM_POSITION_HOME);
for (;;) {
system_refresh_info(&system);
logger_log(&logger, &system);
fflush(logger.file);
int max_name_length = 9;
for (int i = 0; i < system.disk_count; ++i) {
int len = strlen(system.disks[i].name);
if (len > max_name_length)
max_name_length = len;
}
for (int i = 0; i < system.interface_count; ++i) {
int len = strlen(system.interfaces[i].name);
if (len > max_name_length)
max_name_length = len;
}
for (int i = 0; i < system.battery_count; ++i) {
int len = strlen(system.batteries[i].name);
if (len > max_name_length)
max_name_length = len;
}
// CPU frequency and usage
for (int c = 0; c < system.cpu_count; ++c) {
const struct cpu_t *cpu = &system.cpus[c];
if (c == 0 || cpu->core_id != system.cpus[c - 1].core_id) {
// CPU info with temperature
printf("CPU %d : %4d MHz %3d%% usage %3dC"
TERM_ERASE_REST_OF_LINE "\n",
c + 1,
cpu->cur_freq / 1000,
(int)(cpu->total_usage * 100),
cpu->cur_temp / 1000);
} else {
// CPU info without temperature
printf("CPU %d : %4d MHz %3d%% usage"
TERM_ERASE_REST_OF_LINE "\n",
c + 1,
cpu->cur_freq / 1000,
(int)(cpu->total_usage * 100));
}
}
printf(TERM_ERASE_REST_OF_LINE "\n");
// RAM usage
{
char used[10], buffers[10], cached[10];
bytes_to_human_readable(system.ram_used, used);
bytes_to_human_readable(system.ram_buffers, buffers);
bytes_to_human_readable(system.ram_cached, cached);
printf( "Used: %8s\n" TERM_ERASE_REST_OF_LINE
"Buffers: %8s\n" TERM_ERASE_REST_OF_LINE
"Cached: %8s\n" TERM_ERASE_REST_OF_LINE,
used, buffers, cached);
}
printf(TERM_ERASE_REST_OF_LINE "\n");
// Disk usage
printf("%-*s Read Write\n", max_name_length, "Disk");
for (int d = 0; d < system.disk_count; ++d) {
const struct disk_t *disk = &system.disks[d];
char read[10], write[10];
// TODO: find a way to check actual sector size
bytes_to_human_readable(
disk->stats_delta[DISK_READ_SECTORS] * 512, read);
bytes_to_human_readable(
disk->stats_delta[DISK_WRITE_SECTORS] * 512, write);
printf("%-*s %9s/s %9s/s" TERM_ERASE_REST_OF_LINE "\n",
max_name_length, disk->name, read, write);
}
printf(TERM_ERASE_REST_OF_LINE "\n");
// Network usage
printf("%-*s Download Upload\n", max_name_length, "Interface");
for (int i = 0; i < system.interface_count; ++i) {
const struct interface_t *interface = &system.interfaces[i];
char down[10], up[10];
bytes_to_human_readable(interface->delta_rx_bytes, down);
bytes_to_human_readable(interface->delta_tx_bytes, up);
printf("%-*s %9s/s %9s/s" TERM_ERASE_REST_OF_LINE "\n",
max_name_length, interface->name, down, up);
}
if (system.battery_count > 0) {
printf(TERM_ERASE_REST_OF_LINE "\n");
// Battery info
printf("%-*s Charge Current Voltage\n", max_name_length, "Battery");
for (int i = 0; i < system.battery_count; ++i) {
const struct battery_t *battery = &system.batteries[i];
printf("%-*s %6d%% %6.2fA %6.2fV" TERM_ERASE_REST_OF_LINE "\n",
max_name_length, battery->name, battery->charge,
battery->current / 1000000.f, battery->voltage / 1000000.f);
}
}
printf(TERM_ERASE_REST_OF_LINE
TERM_ERASE_DOWN
TERM_POSITION_HOME);
fflush(stdout);
int c = wait_for_keypress();
if (c == 'q' || c == 'Q' || c == 3 || must_exit)
break;
}
logger_destroy(&logger);
system_delete(system);
return 0;
}