/
sysmon.c
402 lines (333 loc) · 13 KB
/
sysmon.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
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
393
394
395
396
397
398
399
400
401
#include "common.h"
#include "rd_stats.h"
#include "print_flags.h"
#include "utils.h"
#include "ext_stats.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <time.h>
#include <limits.h> /* For ULONG_MAX */
#include <float.h> /* For DBL_MAX */
#define INTERVAL_SEC 1
#define SEC_TO_MSEC 1000000
#define LOG_FILE "./log"
/* local type definition */
struct stats {
struct stats_cpu cpu;
unsigned long long uptime, uptime0;
struct stats_sysfs_disk disk;
struct stats_net_dev net;
struct stats_memory mem;
};
struct ext_stats { /* extended stats */
struct ext_stats_cpu cpu;
struct ext_stats_disk disk;
struct ext_stats_net net;
struct ext_stats_mem mem;
};
/* base stats/time */
static struct stats base_stats;
static struct timespec base_time;
/* min/max/mean/var calculation for ext_stats */
static int num_samples;
static struct ext_stats min, max, mean, var;
/* log */
static FILE *fp = NULL;
static int realtime_logging = 0;
static unsigned int print_flag = UTIL_ALL;
static char *print_headers[]=
{/* CPU */
"cpu_user", "cpu_nice", "cpu_sys", "cpu_idle", "cpu_iowait",
"cpu_user[%],cpu_nice[%],cpu_sys[%],cpu_idle[%],cpu_iowait[%]",
/* Disk */
"rd_ios", "wr_ios", "rd_sect", "wr_sect", "rd_ticks", "wr_ticks", "tot_ticks",
"tps", "rd_kB", "wr_kB", "rd_kBps", "wr_kBps", "disk_util[%]",
/* Net */
"rx_pkt", "tx_pkt", "rx_kB", "tx_kB",
"rx_pktps","tx_pktps", "rx_kBps", "tx_kBps", "net_util[%]",
/* Memory */
"tlmkb",
"mem_used", "mem_util[%]"};
#define FPRINTF_DATA(fp, print_flag, flag, fmt, val) if (print_flag & flag) { \
fprintf(fp, fmt, val); \
if ((flag << 1) <= print_flag) fprintf(fp, ","); \
}
void read_stats(struct stats *st)
{
read_stat_cpu(&st->cpu, 2, &st->uptime, &st->uptime0);
read_sysfs_disk(&st->disk, "sda");
read_net_dev(&st->net, 1);
read_meminfo(&st->mem);
}
#if 0
/* st1 += st2 */
void add_ext_stats(struct ext_stats *st1, struct ext_stats *st2)
{
int i;
for (i = 0; i < CPU_STATES; i++)
st1->cpu.utils[i] += st2->cpu.utils[i];
st1->disk.tps += st2->disk.tps;
st1->disk.rd_kBps += st2->disk.rd_kBps;
st1->disk.wr_kBps += st2->disk.wr_kBps;
st1->disk.util += st2->disk.util;
st1->net.rx_pktps += st2->net.rx_pktps;
st1->net.tx_pktps += st2->net.tx_pktps;
st1->net.rx_kBps += st2->net.rx_kBps;
st1->net.tx_kBps += st2->net.tx_kBps;
st1->net.util += st2->net.util;
st1->mem.mem_used += st2->mem.mem_used;
st1->mem.util += st2->mem.util;
}
#endif
void compute_ext_stats(unsigned long long interval,
struct stats *st, struct stats *st_prev,
struct ext_stats *ext_st)
{
compute_ext_cpu_stats(&st->cpu, &st_prev->cpu, &ext_st->cpu);
compute_ext_sysfs_disk_stats(&st->disk, &st_prev->disk, interval, 2 /* KB */, &ext_st->disk);
compute_ext_net_stats(&st->net, &st_prev->net, interval, &ext_st->net);
compute_ext_mem_stats(&st->mem, &ext_st->mem);
}
void update_ext_stats(int num, /* IN */
struct ext_stats *st, /* IN */
struct ext_stats *min, /* OUT */
struct ext_stats *max, /* OUT */
struct ext_stats *mean, /* OUT */
struct ext_stats *var) /* OUT */
{
update_ext_cpu_stats(num, &st->cpu, &min->cpu, &max->cpu, &mean->cpu, &var->cpu);
update_ext_disk_stats(num, &st->disk, &min->disk, &max->disk, &mean->disk, &var->disk);
update_ext_net_stats(num, &st->net, &min->net, &max->net, &mean->net, &var->net);
update_ext_mem_stats(num, &st->mem, &min->mem, &max->mem, &mean->mem, &var->mem);
}
void set_max_ext_stats(struct ext_stats *st)
{
int i;
/* TODO: maybe should do this in ext_stat.c */
/* CPU */
for (i = 0; i < CPU_STATES; i++)
st->cpu.utils[i] = DBL_MAX;
/* Disk */
st->disk.tps= DBL_MAX;
st->disk.rd_kB = st->disk.wr_kB = DBL_MAX;
st->disk.rd_kBps = st->disk.wr_kBps = DBL_MAX;
st->disk.util = DBL_MAX;
/* Net */
st->net.rx_pktps = st->net.tx_pktps = DBL_MAX;
st->net.rx_kBps = st->net.rx_kBps = DBL_MAX;
st->net.util = DBL_MAX;
/* Memory */
st->mem.u.mem_used = ULONG_MAX;
st->mem.util = DBL_MAX;
}
int fprint_header(FILE *fp, unsigned int f)
{
if (fp == NULL)
return EXIT_FAILURE;
fprintf(fp, " time: ");
int i = 0;
while (f) {
if (f & 0x00000001) {
fprintf(fp, "%s", print_headers[i]);
if (1 < f) fprintf(fp, ",");
}
f = f >> 1;
i++;
}
fprintf(fp, "\n");
return EXIT_SUCCESS;
}
double get_elapsed_time(struct timespec* base)
{
// returns elapsed time in sec
struct timespec now;
double elapsed;
clock_gettime(CLOCK_MONOTONIC, &now);
elapsed = (now.tv_sec - base->tv_sec) + (double)(now.tv_nsec - base->tv_nsec) / 1.0e9;;
return elapsed;
}
int fprint_stats(FILE *fp, unsigned int print_flag,
struct stats *st, struct ext_stats *ext_st)
{
if (fp == NULL)
return EXIT_FAILURE;
fprintf(fp, "%8.3f: ", get_elapsed_time(&base_time));
if (print_flag & CPU_ALL) {
FPRINTF_DATA(fp, print_flag, CPU_USER, "%llu", st->cpu.cpu_user);
FPRINTF_DATA(fp, print_flag, CPU_NICE, "%llu", st->cpu.cpu_user);
FPRINTF_DATA(fp, print_flag, CPU_SYS, "%llu", st->cpu.cpu_sys);
FPRINTF_DATA(fp, print_flag, CPU_IDLE, "%llu", st->cpu.cpu_idle);
FPRINTF_DATA(fp, print_flag, CPU_IOWAIT,"%llu", st->cpu.cpu_iowait);
if (print_flag & CPU_UTIL) {
fprintf(fp, "%.3f,%.3f,%.3f,%.3f,%.3f",
ext_st->cpu.utils[0], ext_st->cpu.utils[1], ext_st->cpu.utils[2],
ext_st->cpu.utils[3], ext_st->cpu.utils[4]);
if (CPU_UTIL < print_flag) fprintf(fp, ",");
}
}
if (print_flag & DISK_ALL) {
FPRINTF_DATA(fp, print_flag, RD_IOS, "%lu", st->disk.rd_ios);
FPRINTF_DATA(fp, print_flag, WR_IOS, "%lu", st->disk.wr_ios);
FPRINTF_DATA(fp, print_flag, RD_SECT, "%lu", st->disk.rd_sectors);
FPRINTF_DATA(fp, print_flag, WR_SECT, "%lu", st->disk.wr_sectors);
FPRINTF_DATA(fp, print_flag, RD_TICKS, "%u", st->disk.rd_ticks);
FPRINTF_DATA(fp, print_flag, WR_TICKS, "%u", st->disk.wr_ticks);
FPRINTF_DATA(fp, print_flag, TOT_TICKS, "%u", st->disk.tot_ticks);
FPRINTF_DATA(fp, print_flag, TPS, "%.3f", ext_st->disk.tps);
FPRINTF_DATA(fp, print_flag, RD_KBYTES, "%.3f", ext_st->disk.rd_kB);
FPRINTF_DATA(fp, print_flag, WR_KBYTES, "%.3f", ext_st->disk.wr_kB);
FPRINTF_DATA(fp, print_flag, RD_KBPS, "%.3f", ext_st->disk.rd_kBps);
FPRINTF_DATA(fp, print_flag, WR_KBPS, "%.3f", ext_st->disk.wr_kBps);
FPRINTF_DATA(fp, print_flag, DISK_UTIL, "%.3f", ext_st->disk.util);
}
if (print_flag & NET_ALL) {
FPRINTF_DATA(fp, print_flag, RX_PACKETS,"%llu", st->net.rx_packets);
FPRINTF_DATA(fp, print_flag, TX_PACKETS,"%llu", st->net.tx_packets);
FPRINTF_DATA(fp, print_flag, RX_KBYTES, "%.3f", (double)st->net.rx_bytes / 1024);
FPRINTF_DATA(fp, print_flag, TX_KBYTES, "%.3f", (double)st->net.tx_bytes / 1024);
FPRINTF_DATA(fp, print_flag, RX_PKTPS, "%.3f", ext_st->net.rx_pktps);
FPRINTF_DATA(fp, print_flag, TX_PKTPS, "%.3f", ext_st->net.tx_pktps);
FPRINTF_DATA(fp, print_flag, RX_KBPS, "%.3f", ext_st->net.rx_kBps);
FPRINTF_DATA(fp, print_flag, TX_KBPS, "%.3f", ext_st->net.tx_kBps);
FPRINTF_DATA(fp, print_flag, NET_UTIL, "%.3f", ext_st->net.util);
}
if (print_flag & MEM_ALL) {
FPRINTF_DATA(fp, print_flag, MEM_TOTAL, "%lu", st->mem.tlmkb);
if (print_flag & MEM_USED_DBL) {
FPRINTF_DATA(fp, print_flag, MEM_USED, "%.3f", ext_st->mem.u.mem_used_); /* mean/var */
}
else {
FPRINTF_DATA(fp, print_flag, MEM_USED, "%llu", ext_st->mem.u.mem_used);
}
FPRINTF_DATA(fp, print_flag, MEM_UTIL, "%.3f", ext_st->mem.util);
}
fprintf(fp, "\n");
return EXIT_SUCCESS;
}
/* st1 -= st2 */
void subst_stats(struct stats *st1, struct stats *st2)
{
st1->cpu.cpu_user -= st2->cpu.cpu_user;
st1->cpu.cpu_nice -= st2->cpu.cpu_nice;
st1->cpu.cpu_sys -= st2->cpu.cpu_sys;
st1->cpu.cpu_idle -= st2->cpu.cpu_idle;
st1->cpu.cpu_iowait -= st2->cpu.cpu_iowait;
st1->disk.rd_ios -= st2->disk.rd_ios;
st1->disk.wr_ios -= st2->disk.wr_ios;
st1->disk.rd_sectors-= st2->disk.rd_sectors;
st1->disk.wr_sectors-= st2->disk.wr_sectors;
st1->disk.rd_ticks -= st2->disk.rd_ticks;
st1->disk.wr_ticks -= st2->disk.wr_ticks;
st1->disk.tot_ticks -= st2->disk.tot_ticks;
st1->net.rx_packets -= st2->net.rx_packets;
st1->net.tx_packets -= st2->net.tx_packets;
st1->net.rx_bytes -= st2->net.rx_bytes;
st1->net.tx_bytes -= st2->net.tx_bytes;
/* For memory, does not make sense to compare st1 and st2 */
}
/* "pkill --signal 2 sysmon" will be captured here. */
void signal_callback_handler(int signum)
{
struct stats st;
struct ext_stats ext_st;
unsigned long long interval;
printf("\nCaught signal %d\n", signum);
/* Overall stats calculation */
read_stats(&st);
interval = get_interval(base_stats.uptime, st.uptime);
/* Overall stats output */
fprintf(fp, "Mean:\n");
#if 1
subst_stats(&st, &base_stats); /* st -= base_stats */
fprint_stats(fp, print_flag | MEM_USED_DBL, &st, &mean);
#else
compute_ext_stats(interval, &st, &base_stats, &ext_st);
ext_st.mem = mean.mem;
subst_stats(&st, &base_stats); /* st -= base_stats */
fprint_stats(fp, print_flag | MEM_USED_DBL, &st, &ext_st);
#endif
#if 0
fprintf(fp, "Minimum:\n");
fprint_stats(fp, print_flag, &st, &min);
fprintf(fp, "Maximum:\n");
fprint_stats(fp, print_flag, &st, &max);
fprintf(fp, "Variance:\n");
fprint_stats(fp, print_flag | MEM_USED_DBL, &st, &var);
#endif
/* All done! */
#ifndef DEBUG
fclose(fp);
#endif
exit(signum);
}
int main(int argc, char* argv[])
{
struct stats st, st_prev;
struct ext_stats ext_st;
unsigned long long interval;
char *logfile = LOG_FILE;
int speed = -1, duplex = -1;
/* Usage: ./sysmon [real-time logging switch] [log filename] [print flag] (netif speed) (netif duplex)
netif duplex takes C_DUPLEX_HALF=1 or C_DUPLEX_FULL=2 */
if (2 <= argc) realtime_logging = (strcmp("1", argv[1]) == 0);
if (3 <= argc) logfile = argv[2];
if (4 <= argc && (strncmp("0x", argv[3], 2) == 0))
print_flag = (int)strtol(argv[3], NULL, 0); /* Expecting 0x... */
if (5 <= argc) speed = atoi(argv[4]);
if (6 <= argc) duplex = atoi(argv[5]);
printf ("realtime logging : %s\n", realtime_logging ? "enabled" : "disabled");
printf ("log file : %s\n", logfile);
printf ("print flag : 0x%08x\n", print_flag);
printf ("speed : %d\n", speed);
printf ("duplex : %d\n", duplex);
#ifndef DEBUG
if ((fp = fopen(logfile, "w+")) == NULL) {
fprintf(stderr, "opening logfile failed\n");
exit(EXIT_FAILURE);
}
#else
fp = stdout;
#endif
/* Initialization */
signal(SIGINT, signal_callback_handler); /* Set up a signal handler */
read_stats(&base_stats);
clock_gettime(CLOCK_MONOTONIC, &base_time);
/* Obtain speed/duplex info for eth0 */
read_net_dev(&st.net, 1);
read_if_info(&st.net, 1);
/* VMs on Google Clouds do not let you read files under /sys/class/net/eth0/
See also: http://unix.stackexchange.com/questions/77750/checking-the-speed-of-active-network-connections */
base_stats.net.speed = (0 < speed) ? speed : st.net.speed;
base_stats.net.duplex = (0 < duplex) ? duplex : st.net.duplex;
st = st_prev = base_stats;
/* Initialize variables for min/max/mean/var computation */
num_samples = 1;
set_max_ext_stats(&min);
memset((void *)&max, 0x00, sizeof(struct ext_stats));
memset((void *)&mean, 0x00, sizeof(struct ext_stats));
memset((void *)&var, 0x00, sizeof(struct ext_stats));
/* For real-time logging */
if (realtime_logging) fprint_header(fp, print_flag);
/* Get HZ */
get_HZ();
while (1) { /* Anyways, we record stats for average computation */
usleep(INTERVAL_SEC * SEC_TO_MSEC);
read_stats(&st);
interval = get_interval(st_prev.uptime, st.uptime);
/* Compute extended stats from raw stats */
compute_ext_stats(interval, &st, &st_prev, &ext_st);
update_ext_stats(num_samples, &ext_st, &min, &max, &mean, &var);
if (realtime_logging) fprint_stats(fp, print_flag, &st, &ext_st);
/* Update stats (min/max/mean/var) for ext_stats */
st_prev = st;
num_samples++;
}
/* Program never gets here */
printf("Waiting for the SIGINT signal\n");
pause();
return EXIT_SUCCESS;
}