ifpps: Remove unnecessary memset()
[netsniff-ng.git] / ifpps.c
blob6e7f4030c57bbca775e214caa34155c471422649
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009 - 2013 Daniel Borkmann.
4 * Copyright 2013 Tobias Klauser
5 * Subject to the GPL, version 2.
6 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <curses.h>
11 #include <getopt.h>
12 #include <ctype.h>
13 #include <sys/socket.h>
14 #include <sys/fsuid.h>
15 #include <sys/types.h>
16 #include <signal.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include <time.h>
20 #include <dirent.h>
22 #include "die.h"
23 #include "dev.h"
24 #include "sig.h"
25 #include "link.h"
26 #include "xmalloc.h"
27 #include "ioops.h"
28 #include "promisc.h"
29 #include "cpus.h"
30 #include "config.h"
31 #include "built_in.h"
32 #include "screen.h"
34 struct wifi_stat {
35 uint32_t bitrate;
36 int16_t link_qual, link_qual_max;
37 int signal_level /*, noise_level*/;
40 struct ifstat {
41 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
42 long long unsigned int rx_fifo, rx_frame, rx_multi;
43 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
44 long long unsigned int tx_fifo, tx_colls, tx_carrier;
45 uint64_t mem_free, mem_total, mem_active, mem_inactive;
46 uint64_t swap_total, swap_free, swap_cached;
47 uint32_t irq_nr, procs_total, procs_run, procs_iow, cswitch;
48 struct wifi_stat wifi;
50 * Pointer members need to be last in order for stats_zero() to work
51 * properly.
53 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
54 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
57 struct cpu_hit {
58 unsigned int idx;
59 uint64_t hit;
60 long long unsigned int irqs_rel, irqs_abs;
63 struct avg_stat {
64 uint64_t cpu_user, cpu_sys, cpu_nice, cpu_idle, cpu_iow;
65 long double irqs_abs, irqs_rel, irqs_srx_rel, irqs_stx_rel;
68 static volatile sig_atomic_t sigint = 0;
69 static struct ifstat stats_old, stats_new, stats_delta;
70 static struct cpu_hit *cpu_hits;
71 static struct avg_stat stats_avg;
72 static int stats_loop = 0;
73 static WINDOW *stats_screen = NULL;
75 static const char *short_options = "d:t:n:vhclp";
76 static const struct option long_options[] = {
77 {"dev", required_argument, NULL, 'd'},
78 {"interval", required_argument, NULL, 't'},
79 {"num-cpus", required_argument, NULL, 'n'},
80 {"promisc", no_argument, NULL, 'p'},
81 {"csv", no_argument, NULL, 'c'},
82 {"loop", no_argument, NULL, 'l'},
83 {"version", no_argument, NULL, 'v'},
84 {"help", no_argument, NULL, 'h'},
85 {NULL, 0, NULL, 0}
88 static void signal_handler(int number)
90 switch (number) {
91 case SIGINT:
92 sigint = 1;
93 break;
94 case SIGHUP:
95 default:
96 break;
100 static inline int iswireless(const struct ifstat *stats)
102 return stats->wifi.bitrate > 0;
105 static void __noreturn help(void)
107 printf("\nifpps %s, top-like kernel networking and system statistics\n",
108 VERSION_STRING);
109 puts("http://www.netsniff-ng.org\n\n"
110 "Usage: ifpps [options] || ifpps <netdev>\n"
111 "Options:\n"
112 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
113 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
114 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
115 " in ncurses mode (default 5)\n"
116 " -p|--promisc Promiscuous mode\n"
117 " -c|--csv Output to terminal as Gnuplot-ready data\n"
118 " -l|--loop Continuous CSV output\n"
119 " -v|--version Print version and exit\n"
120 " -h|--help Print this help and exit\n\n"
121 "Examples:\n"
122 " ifpps eth0\n"
123 " ifpps -pd eth0\n"
124 " ifpps -lpcd wlan0 > plot.dat\n\n"
125 "Note:\n"
126 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
127 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
128 "Please report bugs to <bugs@netsniff-ng.org>\n"
129 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
130 "Swiss federal institute of technology (ETH Zurich)\n"
131 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
132 "License: GNU GPL version 2.0\n"
133 "This is free software: you are free to change and redistribute it.\n"
134 "There is NO WARRANTY, to the extent permitted by law.\n");
135 die();
138 static void __noreturn version(void)
140 printf("\nifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
141 puts("top-like kernel networking and system statistics\n"
142 "http://www.netsniff-ng.org\n\n"
143 "Please report bugs to <bugs@netsniff-ng.org>\n"
144 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
145 "Swiss federal institute of technology (ETH Zurich)\n"
146 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
147 "License: GNU GPL version 2.0\n"
148 "This is free software: you are free to change and redistribute it.\n"
149 "There is NO WARRANTY, to the extent permitted by law.\n");
150 die();
153 static inline int padding_from_num(int n)
155 int i = 0;
156 do i++;
157 while ((n /= 10) > 0);
158 return i;
161 #define STATS_ALLOC1(member) \
162 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
164 static void stats_alloc(struct ifstat *stats, int cpus)
166 STATS_ALLOC1(irqs);
167 STATS_ALLOC1(irqs_srx);
168 STATS_ALLOC1(irqs_stx);
170 STATS_ALLOC1(cpu_user);
171 STATS_ALLOC1(cpu_sys);
172 STATS_ALLOC1(cpu_nice);
173 STATS_ALLOC1(cpu_idle);
174 STATS_ALLOC1(cpu_iow);
177 #define STATS_ZERO1(member) \
178 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
180 static void stats_zero(struct ifstat *stats, int cpus)
182 /* Only clear the non-pointer members */
183 memset(stats, 0, offsetof(struct ifstat, irqs));
185 STATS_ZERO1(irqs);
186 STATS_ZERO1(irqs_srx);
187 STATS_ZERO1(irqs_stx);
189 STATS_ZERO1(cpu_user);
190 STATS_ZERO1(cpu_sys);
191 STATS_ZERO1(cpu_nice);
192 STATS_ZERO1(cpu_idle);
193 STATS_ZERO1(cpu_iow);
196 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
198 int ret = -EINVAL;
199 char buff[256];
200 FILE *fp;
202 fp = fopen("/proc/net/dev", "r");
203 if (!fp)
204 panic("Cannot open /proc/net/dev!\n");
206 if (fgets(buff, sizeof(buff), fp)) { ; }
207 if (fgets(buff, sizeof(buff), fp)) { ; }
209 memset(buff, 0, sizeof(buff));
211 while (fgets(buff, sizeof(buff), fp) != NULL) {
212 buff[sizeof(buff) -1] = 0;
214 if (strstr(buff, ifname) == NULL)
215 continue;
217 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
218 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
219 &stats->rx_bytes, &stats->rx_packets,
220 &stats->rx_errors, &stats->rx_drops,
221 &stats->rx_fifo, &stats->rx_frame,
222 &stats->rx_multi, &stats->tx_bytes,
223 &stats->tx_packets, &stats->tx_errors,
224 &stats->tx_drops, &stats->tx_fifo,
225 &stats->tx_colls, &stats->tx_carrier) == 14) {
226 ret = 0;
227 break;
230 memset(buff, 0, sizeof(buff));
233 fclose(fp);
234 return ret;
237 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
239 int ret = -EINVAL, i, cpus, try = 0;
240 char *ptr, *buff;
241 bool seen = false;
242 size_t buff_len;
243 struct ethtool_drvinfo drvinf;
244 FILE *fp;
246 fp = fopen("/proc/interrupts", "r");
247 if (!fp)
248 panic("Cannot open /proc/interrupts!\n");
250 cpus = get_number_cpus();
251 buff_len = cpus * 128;
252 buff = xmalloc(buff_len);
253 retry:
254 fseek(fp, 0, SEEK_SET);
255 memset(buff, 0, buff_len);
257 while (fgets(buff, buff_len, fp) != NULL) {
258 buff[buff_len - 1] = 0;
259 ptr = buff;
261 if (strstr(buff, ifname) == NULL)
262 continue;
264 /* XXX: remove this one here */
265 stats->irq_nr = strtol(ptr, &ptr, 10);
266 bug_on(stats->irq_nr == 0);
268 if (ptr)
269 ptr++;
270 for (i = 0; i < cpus && ptr; ++i) {
271 if (seen)
272 stats->irqs[i] += strtol(ptr, &ptr, 10);
273 else
274 stats->irqs[i] = strtol(ptr, &ptr, 10);
275 if (i == cpus - 1) {
276 ret = 0;
277 seen = true;
281 memset(buff, 0, buff_len);
284 if (ret == -EINVAL && try == 0) {
285 memset(&drvinf, 0, sizeof(drvinf));
286 if (ethtool_drvinf(ifname, &drvinf) < 0)
287 goto done;
289 ifname = drvinf.driver;
290 try++;
292 goto retry;
294 done:
295 xfree(buff);
296 fclose(fp);
297 return ret;
300 static int stats_proc_softirqs(struct ifstat *stats)
302 int i, cpus;
303 char *ptr, *buff;
304 size_t buff_len;
305 FILE *fp;
306 enum {
307 softirqs_net_rx,
308 softirqs_net_tx,
309 } net_type;
311 fp = fopen("/proc/softirqs", "r");
312 if (!fp)
313 panic("Cannot open /proc/softirqs!\n");
315 cpus = get_number_cpus();
316 buff_len = cpus * 128;
317 buff = xmalloc(buff_len);
319 memset(buff, 0, buff_len);
321 while (fgets(buff, buff_len, fp) != NULL) {
322 buff[buff_len - 1] = 0;
324 if ((ptr = strstr(buff, "NET_TX:")))
325 net_type = softirqs_net_tx;
326 else if ((ptr = strstr(buff, "NET_RX:")))
327 net_type = softirqs_net_rx;
328 else
329 continue;
331 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
332 switch (net_type) {
333 case softirqs_net_tx:
334 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
335 break;
336 case softirqs_net_rx:
337 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
338 break;
342 memset(buff, 0, buff_len);
345 xfree(buff);
346 fclose(fp);
347 return 0;
350 static int stats_proc_memory(struct ifstat *stats)
352 char *ptr, buff[256];
353 FILE *fp;
355 fp = fopen("/proc/meminfo", "r");
356 if (!fp)
357 panic("Cannot open /proc/meminfo!\n");
359 memset(buff, 0, sizeof(buff));
361 while (fgets(buff, sizeof(buff), fp) != NULL) {
362 buff[sizeof(buff) - 1] = 0;
364 if ((ptr = strstr(buff, "MemTotal:"))) {
365 ptr += strlen("MemTotal:");
366 stats->mem_total = strtoul(ptr, &ptr, 10);
367 } else if ((ptr = strstr(buff, "MemFree:"))) {
368 ptr += strlen("MemFree:");
369 stats->mem_free = strtoul(ptr, &ptr, 10);
370 } else if ((ptr = strstr(buff, "Active:"))) {
371 ptr += strlen("Active:");
372 stats->mem_active = strtoul(ptr, &ptr, 10);
373 } else if ((ptr = strstr(buff, "Inactive:"))) {
374 ptr += strlen("Inactive:");
375 stats->mem_inactive = strtoul(ptr, &ptr, 10);
376 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
377 ptr += strlen("SwapTotal:");
378 stats->swap_total = strtoul(ptr, &ptr, 10);
379 } else if ((ptr = strstr(buff, "SwapFree:"))) {
380 ptr += strlen("SwapFree:");
381 stats->swap_free = strtoul(ptr, &ptr, 10);
382 } else if ((ptr = strstr(buff, "SwapCached:"))) {
383 ptr += strlen("SwapCached:");
384 stats->swap_cached = strtoul(ptr, &ptr, 10);
387 memset(buff, 0, sizeof(buff));
390 fclose(fp);
391 return 0;
394 static int stats_proc_system(struct ifstat *stats)
396 int cpu, cpus;
397 char *ptr, buff[256];
398 FILE *fp;
400 fp = fopen("/proc/stat", "r");
401 if (!fp)
402 panic("Cannot open /proc/stat!\n");
404 cpus = get_number_cpus();
406 memset(buff, 0, sizeof(buff));
408 while (fgets(buff, sizeof(buff), fp) != NULL) {
409 buff[sizeof(buff) - 1] = 0;
411 if ((ptr = strstr(buff, "cpu"))) {
412 ptr += strlen("cpu");
413 if (isblank(*ptr))
414 goto next;
416 cpu = strtol(ptr, &ptr, 10);
417 bug_on(cpu > cpus);
419 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
420 &stats->cpu_user[cpu],
421 &stats->cpu_nice[cpu],
422 &stats->cpu_sys[cpu],
423 &stats->cpu_idle[cpu],
424 &stats->cpu_iow[cpu]) != 5)
425 goto next;
426 } else if ((ptr = strstr(buff, "ctxt"))) {
427 ptr += strlen("ctxt");
428 stats->cswitch = strtoul(ptr, &ptr, 10);
429 } else if ((ptr = strstr(buff, "procs_running"))) {
430 ptr += strlen("procs_running");
431 stats->procs_run = strtoul(ptr, &ptr, 10);
432 } else if ((ptr = strstr(buff, "procs_blocked"))) {
433 ptr += strlen("procs_blocked");
434 stats->procs_iow = strtoul(ptr, &ptr, 10);
436 next:
437 memset(buff, 0, sizeof(buff));
440 fclose(fp);
441 return 0;
444 static int stats_proc_procs(struct ifstat *stats)
446 DIR *dir;
447 struct dirent *e;
449 dir = opendir("/proc");
450 if (!dir)
451 panic("Cannot open /proc\n");
453 stats->procs_total = 0;
455 while ((e = readdir(dir)) != NULL) {
456 const char *name = e->d_name;
457 char *end;
458 unsigned int pid = strtoul(name, &end, 10);
460 /* not a number */
461 if (pid == 0 && end == name)
462 continue;
464 stats->procs_total++;
467 closedir(dir);
469 return 0;
472 static int adjust_dbm_level(int in_dbm, int dbm_val)
474 if (!in_dbm)
475 return dbm_val;
477 return dbm_val - 0x100;
480 static int stats_wireless(const char *ifname, struct ifstat *stats)
482 int ret;
483 struct iw_statistics ws;
485 ret = wireless_sigqual(ifname, &ws);
486 if (ret != 0) {
487 stats->wifi.bitrate = 0;
488 return -EINVAL;
491 stats->wifi.bitrate = wireless_bitrate(ifname);
493 stats->wifi.signal_level =
494 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
496 stats->wifi.link_qual = ws.qual.qual;
497 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
499 return ret;
502 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
503 #define DIFF(member) do { \
504 if (sizeof(diff->member) != sizeof(new->member) || \
505 sizeof(diff->member) != sizeof(old->member)) \
506 bug(); \
507 bug_on((new->member - old->member) > (new->member)); \
508 DIFF1(member); \
509 } while (0)
511 static void stats_diff(struct ifstat *old, struct ifstat *new,
512 struct ifstat *diff)
514 int cpus, i;
516 DIFF(rx_bytes);
517 DIFF(rx_packets);
518 DIFF(rx_drops);
519 DIFF(rx_errors);
520 DIFF(rx_fifo);
521 DIFF(rx_frame);
522 DIFF(rx_multi);
524 DIFF(tx_bytes);
525 DIFF(tx_packets);
526 DIFF(tx_drops);
527 DIFF(tx_errors);
528 DIFF(tx_fifo);
529 DIFF(tx_colls);
530 DIFF(tx_carrier);
532 DIFF1(wifi.signal_level);
533 DIFF1(wifi.link_qual);
535 DIFF1(cswitch);
537 cpus = get_number_cpus();
539 for (i = 0; i < cpus; ++i) {
540 DIFF(irqs[i]);
541 DIFF(irqs_srx[i]);
542 DIFF(irqs_stx[i]);
544 DIFF1(cpu_user[i]);
545 DIFF1(cpu_nice[i]);
546 DIFF1(cpu_sys[i]);
547 DIFF1(cpu_idle[i]);
548 DIFF1(cpu_iow[i]);
552 static void stats_fetch(const char *ifname, struct ifstat *stats)
554 if (stats_proc_net_dev(ifname, stats) < 0)
555 panic("Cannot fetch device stats!\n");
556 if (stats_proc_softirqs(stats) < 0)
557 panic("Cannot fetch software interrupts!\n");
558 if (stats_proc_memory(stats) < 0)
559 panic("Cannot fetch memory stats!\n");
560 if (stats_proc_system(stats) < 0)
561 panic("Cannot fetch system stats!\n");
562 if (stats_proc_procs(stats) < 0)
563 panic("Cannot fetch process stats!\n");
565 stats_proc_interrupts((char *) ifname, stats);
567 stats_wireless(ifname, stats);
570 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
572 int cpus = get_number_cpus();
574 stats_zero(&stats_old, cpus);
575 stats_zero(&stats_new, cpus);
576 stats_zero(&stats_delta, cpus);
578 stats_fetch(ifname, &stats_old);
579 usleep(ms_interval * 1000);
580 stats_fetch(ifname, &stats_new);
582 stats_diff(&stats_old, &stats_new, &stats_delta);
585 static int cmp_hits(const void *p1, const void *p2)
587 const struct cpu_hit *h1 = p1, *h2 = p2;
590 * We want the hits sorted in descending order, thus reverse the return
591 * values.
593 if (h1->hit == h2->hit)
594 return 0;
595 else if (h1->hit < h2->hit)
596 return 1;
597 else
598 return -1;
601 static int cmp_irqs_rel(const void *p1, const void *p2)
603 const struct cpu_hit *h1 = p1, *h2 = p2;
606 * We want the hits sorted in descending order, thus reverse the return
607 * values.
609 if (h1->irqs_rel == h2->irqs_rel)
610 return 0;
611 else if (h1->irqs_rel < h2->irqs_rel)
612 return 1;
613 else
614 return -1;
617 static int cmp_irqs_abs(const void *p1, const void *p2)
619 const struct cpu_hit *h1 = p1, *h2 = p2;
622 * We want the hits sorted in descending order, thus reverse the return
623 * values.
625 if (h1->irqs_abs == h2->irqs_abs)
626 return 0;
627 else if (h1->irqs_abs < h2->irqs_abs)
628 return 1;
629 else
630 return -1;
633 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
634 int cpus)
636 int i;
638 memset(&stats_avg, 0, sizeof(stats_avg));
640 for (i = 0; i < cpus; ++i) {
641 cpu_hits[i].idx = i;
642 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
643 cpu_hits[i].irqs_rel = rel->irqs[i];
644 cpu_hits[i].irqs_abs = abs->irqs[i];
646 stats_avg.cpu_user += rel->cpu_user[i];
647 stats_avg.cpu_sys += rel->cpu_sys[i];
648 stats_avg.cpu_nice += rel->cpu_nice[i];
649 stats_avg.cpu_idle += rel->cpu_idle[i];
650 stats_avg.cpu_iow += rel->cpu_iow[i];
652 stats_avg.irqs_abs += abs->irqs[i];
653 stats_avg.irqs_rel += rel->irqs[i];
654 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
655 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
658 stats_avg.cpu_user /= cpus;
659 stats_avg.cpu_sys /= cpus;
660 stats_avg.cpu_nice /= cpus;
661 stats_avg.cpu_idle /= cpus;
662 stats_avg.cpu_iow /= cpus;
663 stats_avg.irqs_abs /= cpus;
664 stats_avg.irqs_rel /= cpus;
665 stats_avg.irqs_srx_rel /= cpus;
666 stats_avg.irqs_stx_rel /= cpus;
669 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
670 uint64_t ms_interval, unsigned int top_cpus)
672 size_t len = 0;
673 char buff[64];
674 struct ethtool_drvinfo drvinf;
675 u32 rate = device_bitrate(ifname);
676 int link = ethtool_link(ifname);
677 unsigned int cpus = get_number_cpus();
679 memset(&drvinf, 0, sizeof(drvinf));
680 ethtool_drvinf(ifname, &drvinf);
682 memset(buff, 0, sizeof(buff));
683 if (rate)
684 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
685 if (link >= 0)
686 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
687 link == 0 ? "no" : "yes");
689 mvwprintw(screen, (*voff)++, 2,
690 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u%s/%u"
691 " ",
692 ifname, drvinf.driver, buff, ms_interval, top_cpus,
693 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
696 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
697 int *voff)
699 attron(A_REVERSE);
701 mvwprintw(screen, (*voff)++, 0,
702 " rx: %16.3llf MiB/t "
703 "%10llu pkts/t "
704 "%10llu drops/t "
705 "%10llu errors/t ",
706 ((long double) rel->rx_bytes) / (1LLU << 20),
707 rel->rx_packets, rel->rx_drops, rel->rx_errors);
709 mvwprintw(screen, (*voff)++, 0,
710 " tx: %16.3llf MiB/t "
711 "%10llu pkts/t "
712 "%10llu drops/t "
713 "%10llu errors/t ",
714 ((long double) rel->tx_bytes) / (1LLU << 20),
715 rel->tx_packets, rel->tx_drops, rel->tx_errors);
717 attroff(A_REVERSE);
720 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
721 int *voff)
723 mvwprintw(screen, (*voff)++, 2,
724 "rx: %16.3llf MiB "
725 "%10llu pkts "
726 "%10llu drops "
727 "%10llu errors",
728 ((long double) abs->rx_bytes) / (1LLU << 20),
729 abs->rx_packets, abs->rx_drops, abs->rx_errors);
731 mvwprintw(screen, (*voff)++, 2,
732 "tx: %16.3llf MiB "
733 "%10llu pkts "
734 "%10llu drops "
735 "%10llu errors",
736 ((long double) abs->tx_bytes) / (1LLU << 20),
737 abs->tx_packets, abs->tx_drops, abs->tx_errors);
740 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
741 const struct ifstat *abs, int *voff)
743 mvwprintw(screen, (*voff)++, 2,
744 "sys: %14u cs/t "
745 "%11u procs "
746 "%11u running "
747 "%10u iowait",
748 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
751 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
753 mvwprintw(screen, (*voff)++, 2,
754 "mem: %13uM total "
755 "%9uM used "
756 "%11uM active "
757 "%10uM inactive",
758 abs->mem_total / 1024,
759 (abs->mem_total - abs->mem_free) / 1024,
760 abs->mem_active / 1024,
761 abs->mem_inactive / 1024);
763 mvwprintw(screen, (*voff)++, 2,
764 "swap: %12uM total "
765 "%9uM used "
766 "%11uM cached",
767 abs->swap_total / 1024,
768 (abs->swap_total - abs->swap_free) / 1024,
769 abs->swap_cached / 1024);
772 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
773 int *voff, unsigned int idx, char *tag)
775 int max_padd = padding_from_num(get_number_cpus());
776 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
777 rel->cpu_idle[idx] + rel->cpu_iow[idx];
779 mvwprintw(screen, (*voff)++, 2,
780 "cpu%*d%s:%s %12.1lf%% usr/t "
781 "%9.1lf%% sys/t "
782 "%10.1lf%% idl/t "
783 "%11.1lf%% iow/t", max_padd, idx,
784 tag, strlen(tag) == 0 ? " " : "",
785 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
786 100.0 * rel->cpu_sys[idx] / all,
787 100.0 * rel->cpu_idle[idx] / all,
788 100.0 * rel->cpu_iow[idx] / all);
791 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
792 const struct avg_stat *avg, int top_cpus,
793 int *voff)
795 int i;
796 int cpus = get_number_cpus();
797 uint64_t all;
799 if (top_cpus == 0)
800 return;
802 /* Display top hitter */
803 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
805 /* Make sure we don't display the min. hitter twice */
806 if (top_cpus == cpus)
807 top_cpus--;
809 for (i = 1; i < top_cpus; ++i)
810 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "");
812 /* Display minimum hitter */
813 if (cpus != 1)
814 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
816 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
817 mvwprintw(screen, (*voff)++, 2,
818 "avg: %14.1lf%% usr/t "
819 "%9.1lf%% sys/t "
820 "%10.1lf%% idl/t "
821 "%11.1lf%% iow/t",
822 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
823 100.0 * avg->cpu_sys / all,
824 100.0 * avg->cpu_idle /all,
825 100.0 * avg->cpu_iow / all);
828 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
829 int *voff, unsigned int idx, char *tag)
831 int max_padd = padding_from_num(get_number_cpus());
833 mvwprintw(screen, (*voff)++, 2,
834 "cpu%*d%s:%s %13llu irqs/t "
835 "%17llu sirq rx/t "
836 "%17llu sirq tx/t", max_padd, idx,
837 tag, strlen(tag) == 0 ? " " : "",
838 rel->irqs[idx],
839 rel->irqs_srx[idx],
840 rel->irqs_stx[idx]);
843 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
844 const struct avg_stat *avg, int top_cpus,
845 int *voff)
847 int i;
848 int cpus = get_number_cpus();
850 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
852 if (top_cpus == cpus)
853 top_cpus--;
855 for (i = 1; i < top_cpus; ++i)
856 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "");
858 if (cpus != 1)
859 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
861 mvwprintw(screen, (*voff)++, 2,
862 "avg: %15.2Lf irqs/t "
863 "%17.2Lf sirq rx/t "
864 "%17.2Lf sirq tx/t",
865 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
868 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
869 int *voff, unsigned int idx, char *tag)
871 int max_padd = padding_from_num(get_number_cpus());
873 mvwprintw(screen, (*voff)++, 2,
874 "cpu%*d%s:%s %13llu irqs", max_padd, idx,
875 tag, strlen(tag) == 0 ? " " : "",
876 abs->irqs[idx]);
879 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
880 const struct avg_stat *avg, int top_cpus,
881 int *voff)
883 int i;
884 int cpus = get_number_cpus();
886 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
888 if (top_cpus == cpus)
889 top_cpus--;
891 for (i = 1; i < top_cpus; ++i)
892 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "");
894 if (cpus != 1)
895 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
897 mvwprintw(screen, (*voff)++, 2,
898 "avg: %15.2Lf irqs", avg->irqs_abs);
901 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
902 const struct ifstat *abs, int *voff)
904 if (iswireless(abs)) {
905 mvwprintw(screen, (*voff)++, 2,
906 "linkqual: %7d/%d (%d/t) ",
907 abs->wifi.link_qual,
908 abs->wifi.link_qual_max,
909 rel->wifi.link_qual);
911 mvwprintw(screen, (*voff)++, 2,
912 "signal: %8d dBm (%d dBm/t) ",
913 abs->wifi.signal_level,
914 rel->wifi.signal_level);
918 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
919 const struct ifstat *abs, const struct avg_stat *avg,
920 int *first, uint64_t ms_interval, unsigned int top_cpus)
922 int cpus, top, voff = 1, cvoff = 2;
924 curs_set(0);
926 cpus = get_number_cpus();
927 top = min(cpus, top_cpus);
929 stats_top(rel, abs, cpus);
931 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
933 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
935 voff++;
936 screen_net_dev_rel(screen, rel, &voff);
938 voff++;
939 screen_net_dev_abs(screen, abs, &voff);
941 voff++;
942 screen_sys(screen, rel, abs, &voff);
944 voff++;
945 screen_mem_swap(screen, abs, &voff);
947 voff++;
948 screen_percpu_states(screen, rel, avg, top, &voff);
950 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
952 voff++;
953 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
955 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
957 voff++;
958 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
960 voff++;
961 screen_wireless(screen, rel, abs, &voff);
963 if (*first) {
964 mvwprintw(screen, cvoff, 2, "Collecting data ...");
965 *first = 0;
966 } else {
967 mvwprintw(screen, cvoff, 2, " ");
970 wrefresh(screen);
971 refresh();
974 static int screen_main(const char *ifname, uint64_t ms_interval,
975 unsigned int top_cpus)
977 int first = 1, key;
979 stats_screen = screen_init(true);
981 while (!sigint) {
982 key = getch();
983 if (key == 'q' || key == 0x1b || key == KEY_F(10))
984 break;
986 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
987 &first, ms_interval, top_cpus);
989 stats_sample_generic(ifname, ms_interval);
992 screen_end();
994 return 0;
997 static void term_csv(const char *ifname, const struct ifstat *rel,
998 const struct ifstat *abs, uint64_t ms_interval)
1000 int cpus, i;
1002 printf("%ld ", time(0));
1004 printf("%llu ", rel->rx_bytes);
1005 printf("%llu ", rel->rx_packets);
1006 printf("%llu ", rel->rx_drops);
1007 printf("%llu ", rel->rx_errors);
1009 printf("%llu ", abs->rx_bytes);
1010 printf("%llu ", abs->rx_packets);
1011 printf("%llu ", abs->rx_drops);
1012 printf("%llu ", abs->rx_errors);
1014 printf("%llu ", rel->tx_bytes);
1015 printf("%llu ", rel->tx_packets);
1016 printf("%llu ", rel->tx_drops);
1017 printf("%llu ", rel->tx_errors);
1019 printf("%llu ", abs->tx_bytes);
1020 printf("%llu ", abs->tx_packets);
1021 printf("%llu ", abs->tx_drops);
1022 printf("%llu ", abs->tx_errors);
1024 printf("%u ", rel->cswitch);
1025 printf("%lu ", abs->mem_free);
1026 printf("%lu ", abs->mem_total - abs->mem_free);
1027 printf("%lu ", abs->mem_total);
1028 printf("%lu ", abs->swap_free);
1029 printf("%lu ", abs->swap_total - abs->swap_free);
1030 printf("%lu ", abs->swap_total);
1031 printf("%u ", abs->procs_total);
1032 printf("%u ", abs->procs_run);
1033 printf("%u ", abs->procs_iow);
1035 cpus = get_number_cpus();
1037 for (i = 0; i < cpus; ++i) {
1038 printf("%lu ", rel->cpu_user[i]);
1039 printf("%lu ", rel->cpu_nice[i]);
1040 printf("%lu ", rel->cpu_sys[i]);
1041 printf("%lu ", rel->cpu_idle[i]);
1042 printf("%lu ", rel->cpu_iow[i]);
1044 printf("%llu ", rel->irqs[i]);
1045 printf("%llu ", abs->irqs[i]);
1047 printf("%llu ", rel->irqs_srx[i]);
1048 printf("%llu ", abs->irqs_srx[i]);
1050 printf("%llu ", rel->irqs_stx[i]);
1051 printf("%llu ", abs->irqs_stx[i]);
1054 if (iswireless(abs)) {
1055 printf("%u ", rel->wifi.link_qual);
1056 printf("%u ", abs->wifi.link_qual);
1057 printf("%u ", abs->wifi.link_qual_max);
1059 printf("%d ", rel->wifi.signal_level);
1060 printf("%d ", abs->wifi.signal_level);
1063 puts("");
1064 fflush(stdout);
1067 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1068 uint64_t ms_interval)
1070 int cpus, i, j = 1;
1072 printf("# gnuplot dump (#col:description)\n");
1073 printf("# networking interface: %s\n", ifname);
1074 printf("# sampling interval (t): %lu ms\n", ms_interval);
1075 printf("# %d:unixtime ", j++);
1077 printf("%d:rx-bytes-per-t ", j++);
1078 printf("%d:rx-pkts-per-t ", j++);
1079 printf("%d:rx-drops-per-t ", j++);
1080 printf("%d:rx-errors-per-t ", j++);
1082 printf("%d:rx-bytes ", j++);
1083 printf("%d:rx-pkts ", j++);
1084 printf("%d:rx-drops ", j++);
1085 printf("%d:rx-errors ", j++);
1087 printf("%d:tx-bytes-per-t ", j++);
1088 printf("%d:tx-pkts-per-t ", j++);
1089 printf("%d:tx-drops-per-t ", j++);
1090 printf("%d:tx-errors-per-t ", j++);
1092 printf("%d:tx-bytes ", j++);
1093 printf("%d:tx-pkts ", j++);
1094 printf("%d:tx-drops ", j++);
1095 printf("%d:tx-errors ", j++);
1097 printf("%d:context-switches-per-t ", j++);
1098 printf("%d:mem-free ", j++);
1099 printf("%d:mem-used ", j++);
1100 printf("%d:mem-total ", j++);
1101 printf("%d:swap-free ", j++);
1102 printf("%d:swap-used ", j++);
1103 printf("%d:swap-total ", j++);
1104 printf("%d:procs-total ", j++);
1105 printf("%d:procs-in-run ", j++);
1106 printf("%d:procs-in-iow ", j++);
1108 cpus = get_number_cpus();
1110 for (i = 0; i < cpus; ++i) {
1111 printf("%d:cpu%i-usr-per-t ", j++, i);
1112 printf("%d:cpu%i-nice-per-t ", j++, i);
1113 printf("%d:cpu%i-sys-per-t ", j++, i);
1114 printf("%d:cpu%i-idle-per-t ", j++, i);
1115 printf("%d:cpu%i-iow-per-t ", j++, i);
1117 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1118 printf("%d:cpu%i-net-irqs ", j++, i);
1120 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1121 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1122 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1123 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1126 if (iswireless(abs)) {
1127 printf("%d:wifi-link-qual-per-t ", j++);
1128 printf("%d:wifi-link-qual ", j++);
1129 printf("%d:wifi-link-qual-max ", j++);
1131 printf("%d:wifi-signal-dbm-per-t ", j++);
1132 printf("%d:wifi-signal-dbm ", j++);
1135 puts("");
1136 printf("# data:\n");
1137 fflush(stdout);
1140 static int term_main(const char *ifname, uint64_t ms_interval,
1141 unsigned int top_cpus __maybe_unused)
1143 int first = 1;
1145 do {
1146 stats_sample_generic(ifname, ms_interval);
1148 if (first) {
1149 first = 0;
1150 term_csv_header(ifname, &stats_new, ms_interval);
1153 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1154 } while (stats_loop && !sigint);
1156 return 0;
1159 int main(int argc, char **argv)
1161 short ifflags = 0;
1162 int c, opt_index, ret, cpus, promisc = 0;
1163 unsigned int top_cpus = 5;
1164 uint64_t interval = 1000;
1165 char *ifname = NULL;
1166 int (*func_main)(const char *ifname, uint64_t ms_interval,
1167 unsigned int top_cpus) = screen_main;
1169 setfsuid(getuid());
1170 setfsgid(getgid());
1172 while ((c = getopt_long(argc, argv, short_options, long_options,
1173 &opt_index)) != EOF) {
1174 switch (c) {
1175 case 'h':
1176 help();
1177 break;
1178 case 'v':
1179 version();
1180 break;
1181 case 'd':
1182 ifname = xstrndup(optarg, IFNAMSIZ);
1183 break;
1184 case 't':
1185 interval = strtoul(optarg, NULL, 10);
1186 break;
1187 case 'n':
1188 top_cpus = strtoul(optarg, NULL, 10);
1189 if (top_cpus < 1)
1190 panic("Number of top hitter CPUs must be greater than 0");
1191 break;
1192 case 'l':
1193 stats_loop = 1;
1194 break;
1195 case 'p':
1196 promisc = 1;
1197 break;
1198 case 'c':
1199 func_main = term_main;
1200 break;
1201 case '?':
1202 switch (optopt) {
1203 case 'd':
1204 case 't':
1205 panic("Option -%c requires an argument!\n",
1206 optopt);
1207 default:
1208 if (isprint(optopt))
1209 printf("Unknown option character `0x%X\'!\n", optopt);
1210 die();
1212 default:
1213 break;
1217 if (argc == 1)
1218 help();
1220 if (argc == 2)
1221 ifname = xstrndup(argv[1], IFNAMSIZ);
1222 if (ifname == NULL)
1223 panic("No networking device given!\n");
1225 if (!strncmp("lo", ifname, IFNAMSIZ))
1226 panic("lo is not supported!\n");
1227 if (device_mtu(ifname) == 0)
1228 panic("This is no networking device!\n");
1230 register_signal(SIGINT, signal_handler);
1231 register_signal(SIGHUP, signal_handler);
1233 cpus = get_number_cpus();
1234 top_cpus = min(top_cpus, cpus);
1236 stats_alloc(&stats_old, cpus);
1237 stats_alloc(&stats_new, cpus);
1238 stats_alloc(&stats_delta, cpus);
1240 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1242 if (promisc)
1243 ifflags = enter_promiscuous_mode(ifname);
1244 ret = func_main(ifname, interval, top_cpus);
1245 if (promisc)
1246 leave_promiscuous_mode(ifname, ifflags);
1248 xfree(ifname);
1249 return ret;