bpf: ldi,ldxi: allow for '#' prefix in numbers
[netsniff-ng.git] / ifpps.c
blobb45b05f84b0bba8b7db11907e5aab6bb42c9fa53
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 <sys/utsname.h>
17 #include <signal.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <time.h>
21 #include <dirent.h>
23 #include "die.h"
24 #include "dev.h"
25 #include "sig.h"
26 #include "str.h"
27 #include "link.h"
28 #include "xmalloc.h"
29 #include "ioops.h"
30 #include "cpus.h"
31 #include "config.h"
32 #include "built_in.h"
33 #include "screen.h"
35 struct wifi_stat {
36 uint32_t bitrate;
37 int16_t link_qual, link_qual_max;
38 int signal_level /*, noise_level*/;
41 struct ifstat {
42 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
43 long long unsigned int rx_fifo, rx_frame, rx_multi;
44 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
45 long long unsigned int tx_fifo, tx_colls, tx_carrier;
46 uint64_t mem_free, mem_total, mem_active, mem_inactive;
47 uint64_t swap_total, swap_free, swap_cached;
48 uint32_t procs_total, procs_run, procs_iow, cswitch;
49 struct wifi_stat wifi;
51 * Pointer members need to be last in order for stats_zero() to work
52 * properly.
54 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
55 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
58 struct cpu_hit {
59 unsigned int idx;
60 uint64_t hit;
61 long long unsigned int irqs_rel, irqs_abs;
64 struct avg_stat {
65 uint64_t cpu_user, cpu_sys, cpu_nice, cpu_idle, cpu_iow;
66 long double irqs_abs, irqs_rel, irqs_srx_rel, irqs_stx_rel;
69 static volatile sig_atomic_t sigint = 0;
70 static struct ifstat stats_old, stats_new, stats_delta;
71 static struct cpu_hit *cpu_hits;
72 static struct avg_stat stats_avg;
73 static int stats_loop = 0;
74 static int show_median = 0;
75 static WINDOW *stats_screen = NULL;
76 static struct utsname uts;
78 static const char *short_options = "d:n:t:clmpWvh";
79 static const struct option long_options[] = {
80 {"dev", required_argument, NULL, 'd'},
81 {"num-cpus", required_argument, NULL, 'n'},
82 {"interval", required_argument, NULL, 't'},
83 {"csv", no_argument, NULL, 'c'},
84 {"loop", no_argument, NULL, 'l'},
85 {"median", no_argument, NULL, 'm'},
86 {"promisc", no_argument, NULL, 'p'},
87 {"no-warn", no_argument, NULL, 'W'},
88 {"version", no_argument, NULL, 'v'},
89 {"help", no_argument, NULL, 'h'},
90 {NULL, 0, NULL, 0}
93 static void signal_handler(int number)
95 switch (number) {
96 case SIGINT:
97 sigint = 1;
98 break;
99 case SIGHUP:
100 default:
101 break;
105 static inline int iswireless(const struct ifstat *stats)
107 return stats->wifi.bitrate > 0;
110 static void __noreturn help(void)
112 printf("\nifpps %s, top-like kernel networking and system statistics\n",
113 VERSION_STRING);
114 puts("http://www.netsniff-ng.org\n\n"
115 "Usage: ifpps [options] || ifpps <netdev>\n"
116 "Options:\n"
117 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
118 " -n|--num-cpus <num> Number of top hitter CPUs in ncurses mode (def: 5)\n"
119 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
120 " -c|--csv Output to terminal as Gnuplot-ready data\n"
121 " -l|--loop Continuous CSV output\n"
122 " -m|--median Display median values\n"
123 " -p|--promisc Promiscuous mode\n"
124 " -W|--no-warn Suppress warnings\n"
125 " -v|--version Print version and exit\n"
126 " -h|--help Print this help and exit\n\n"
127 "Examples:\n"
128 " ifpps eth0\n"
129 " ifpps -pd eth0\n"
130 " ifpps -lpcd wlan0 > plot.dat\n\n"
131 "Note:\n"
132 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
133 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
134 "Please report bugs to <bugs@netsniff-ng.org>\n"
135 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
136 "Swiss federal institute of technology (ETH Zurich)\n"
137 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
138 "License: GNU GPL version 2.0\n"
139 "This is free software: you are free to change and redistribute it.\n"
140 "There is NO WARRANTY, to the extent permitted by law.\n");
141 die();
144 static void __noreturn version(void)
146 printf("\nifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
147 puts("top-like kernel networking and system statistics\n"
148 "http://www.netsniff-ng.org\n\n"
149 "Please report bugs to <bugs@netsniff-ng.org>\n"
150 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
151 "Swiss federal institute of technology (ETH Zurich)\n"
152 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
153 "License: GNU GPL version 2.0\n"
154 "This is free software: you are free to change and redistribute it.\n"
155 "There is NO WARRANTY, to the extent permitted by law.\n");
156 die();
159 static inline int padding_from_num(int n)
161 int i = 0;
162 do {
163 i++;
164 } while ((n /= 10) > 0);
165 return i;
168 #define STATS_ALLOC1(member) \
169 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
171 static void stats_alloc(struct ifstat *stats, unsigned int cpus)
173 STATS_ALLOC1(irqs);
174 STATS_ALLOC1(irqs_srx);
175 STATS_ALLOC1(irqs_stx);
177 STATS_ALLOC1(cpu_user);
178 STATS_ALLOC1(cpu_sys);
179 STATS_ALLOC1(cpu_nice);
180 STATS_ALLOC1(cpu_idle);
181 STATS_ALLOC1(cpu_iow);
184 #define STATS_ZERO1(member) \
185 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
187 static void stats_zero(struct ifstat *stats, unsigned int cpus)
189 /* Only clear the non-pointer members */
190 memset(stats, 0, offsetof(struct ifstat, irqs));
192 STATS_ZERO1(irqs);
193 STATS_ZERO1(irqs_srx);
194 STATS_ZERO1(irqs_stx);
196 STATS_ZERO1(cpu_user);
197 STATS_ZERO1(cpu_sys);
198 STATS_ZERO1(cpu_nice);
199 STATS_ZERO1(cpu_idle);
200 STATS_ZERO1(cpu_iow);
203 #define STATS_RELEASE(member) \
204 do { xfree(stats->member); } while (0)
206 static void stats_release(struct ifstat *stats)
208 STATS_RELEASE(irqs);
209 STATS_RELEASE(irqs_srx);
210 STATS_RELEASE(irqs_stx);
212 STATS_RELEASE(cpu_user);
213 STATS_RELEASE(cpu_sys);
214 STATS_RELEASE(cpu_nice);
215 STATS_RELEASE(cpu_idle);
216 STATS_RELEASE(cpu_iow);
219 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
221 int ret = -EINVAL;
222 char buff[256];
223 FILE *fp;
225 fp = fopen("/proc/net/dev", "r");
226 if (!fp)
227 panic("Cannot open /proc/net/dev!\n");
229 if (fgets(buff, sizeof(buff), fp)) { ; }
230 if (fgets(buff, sizeof(buff), fp)) { ; }
232 memset(buff, 0, sizeof(buff));
234 while (fgets(buff, sizeof(buff), fp) != NULL) {
235 buff[sizeof(buff) -1] = 0;
237 if (strstr(buff, ifname) == NULL)
238 continue;
240 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
241 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
242 &stats->rx_bytes, &stats->rx_packets,
243 &stats->rx_errors, &stats->rx_drops,
244 &stats->rx_fifo, &stats->rx_frame,
245 &stats->rx_multi, &stats->tx_bytes,
246 &stats->tx_packets, &stats->tx_errors,
247 &stats->tx_drops, &stats->tx_fifo,
248 &stats->tx_colls, &stats->tx_carrier) == 14) {
249 ret = 0;
250 break;
253 memset(buff, 0, sizeof(buff));
256 fclose(fp);
257 return ret;
260 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
262 int ret = -EINVAL, try = 0;
263 unsigned int i, cpus;
264 char *ptr, *buff;
265 bool seen = false;
266 size_t buff_len;
267 struct ethtool_drvinfo drvinf;
268 FILE *fp;
270 fp = fopen("/proc/interrupts", "r");
271 if (!fp)
272 panic("Cannot open /proc/interrupts!\n");
274 cpus = get_number_cpus();
275 buff_len = cpus * 128;
276 buff = xmalloc(buff_len);
277 retry:
278 fseek(fp, 0, SEEK_SET);
279 memset(buff, 0, buff_len);
281 while (fgets(buff, buff_len, fp) != NULL) {
282 buff[buff_len - 1] = 0;
284 if (strstr(buff, ifname) == NULL)
285 continue;
287 ptr = strchr(buff, ':');
288 if (!ptr)
289 continue;
290 ptr++;
292 for (i = 0; i < cpus && ptr; ++i) {
293 if (seen)
294 stats->irqs[i] += strtol(ptr, &ptr, 10);
295 else
296 stats->irqs[i] = strtol(ptr, &ptr, 10);
297 if (i == cpus - 1) {
298 ret = 0;
299 seen = true;
303 memset(buff, 0, buff_len);
306 if (ret == -EINVAL && try == 0) {
307 memset(&drvinf, 0, sizeof(drvinf));
308 if (ethtool_drvinf(ifname, &drvinf) < 0)
309 goto done;
311 ifname = drvinf.driver;
312 try++;
314 goto retry;
316 done:
317 xfree(buff);
318 fclose(fp);
319 return ret;
322 static int stats_proc_softirqs(struct ifstat *stats)
324 unsigned int i, cpus;
325 char *ptr, *buff;
326 size_t buff_len;
327 FILE *fp;
328 enum {
329 softirqs_net_rx,
330 softirqs_net_tx,
331 } net_type;
333 fp = fopen("/proc/softirqs", "r");
334 if (!fp)
335 panic("Cannot open /proc/softirqs!\n");
337 cpus = get_number_cpus();
338 buff_len = cpus * 128;
339 buff = xmalloc(buff_len);
341 memset(buff, 0, buff_len);
343 while (fgets(buff, buff_len, fp) != NULL) {
344 buff[buff_len - 1] = 0;
346 if ((ptr = strstr(buff, "NET_TX:")))
347 net_type = softirqs_net_tx;
348 else if ((ptr = strstr(buff, "NET_RX:")))
349 net_type = softirqs_net_rx;
350 else
351 continue;
353 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
354 switch (net_type) {
355 case softirqs_net_tx:
356 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
357 break;
358 case softirqs_net_rx:
359 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
360 break;
364 memset(buff, 0, buff_len);
367 xfree(buff);
368 fclose(fp);
369 return 0;
372 static int stats_proc_memory(struct ifstat *stats)
374 char *ptr, buff[256];
375 FILE *fp;
377 fp = fopen("/proc/meminfo", "r");
378 if (!fp)
379 panic("Cannot open /proc/meminfo!\n");
381 memset(buff, 0, sizeof(buff));
383 while (fgets(buff, sizeof(buff), fp) != NULL) {
384 buff[sizeof(buff) - 1] = 0;
386 if ((ptr = strstr(buff, "MemTotal:"))) {
387 ptr += strlen("MemTotal:");
388 stats->mem_total = strtoul(ptr, &ptr, 10);
389 } else if ((ptr = strstr(buff, "MemFree:"))) {
390 ptr += strlen("MemFree:");
391 stats->mem_free = strtoul(ptr, &ptr, 10);
392 } else if ((ptr = strstr(buff, "Active:"))) {
393 ptr += strlen("Active:");
394 stats->mem_active = strtoul(ptr, &ptr, 10);
395 } else if ((ptr = strstr(buff, "Inactive:"))) {
396 ptr += strlen("Inactive:");
397 stats->mem_inactive = strtoul(ptr, &ptr, 10);
398 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
399 ptr += strlen("SwapTotal:");
400 stats->swap_total = strtoul(ptr, &ptr, 10);
401 } else if ((ptr = strstr(buff, "SwapFree:"))) {
402 ptr += strlen("SwapFree:");
403 stats->swap_free = strtoul(ptr, &ptr, 10);
404 } else if ((ptr = strstr(buff, "SwapCached:"))) {
405 ptr += strlen("SwapCached:");
406 stats->swap_cached = strtoul(ptr, &ptr, 10);
409 memset(buff, 0, sizeof(buff));
412 fclose(fp);
413 return 0;
416 static int stats_proc_system(struct ifstat *stats)
418 unsigned int cpu, cpus;
419 char *ptr, buff[256];
420 FILE *fp;
422 fp = fopen("/proc/stat", "r");
423 if (!fp)
424 panic("Cannot open /proc/stat!\n");
426 cpus = get_number_cpus();
428 memset(buff, 0, sizeof(buff));
430 while (fgets(buff, sizeof(buff), fp) != NULL) {
431 buff[sizeof(buff) - 1] = 0;
433 if ((ptr = strstr(buff, "cpu"))) {
434 ptr += strlen("cpu");
435 if (isblank(*ptr))
436 goto next;
438 cpu = strtol(ptr, &ptr, 10);
439 bug_on(cpu > cpus);
441 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
442 &stats->cpu_user[cpu],
443 &stats->cpu_nice[cpu],
444 &stats->cpu_sys[cpu],
445 &stats->cpu_idle[cpu],
446 &stats->cpu_iow[cpu]) != 5)
447 goto next;
448 } else if ((ptr = strstr(buff, "ctxt"))) {
449 ptr += strlen("ctxt");
450 stats->cswitch = strtoul(ptr, &ptr, 10);
451 } else if ((ptr = strstr(buff, "procs_running"))) {
452 ptr += strlen("procs_running");
453 stats->procs_run = strtoul(ptr, &ptr, 10);
454 } else if ((ptr = strstr(buff, "procs_blocked"))) {
455 ptr += strlen("procs_blocked");
456 stats->procs_iow = strtoul(ptr, &ptr, 10);
458 next:
459 memset(buff, 0, sizeof(buff));
462 fclose(fp);
463 return 0;
466 static int stats_proc_procs(struct ifstat *stats)
468 DIR *dir;
469 struct dirent *e;
471 dir = opendir("/proc");
472 if (!dir)
473 panic("Cannot open /proc\n");
475 stats->procs_total = 0;
477 while ((e = readdir(dir)) != NULL) {
478 const char *name = e->d_name;
479 char *end;
480 unsigned int pid = strtoul(name, &end, 10);
482 /* not a number */
483 if (pid == 0 && end == name)
484 continue;
486 stats->procs_total++;
489 closedir(dir);
491 return 0;
494 static int adjust_dbm_level(int in_dbm, int dbm_val)
496 if (!in_dbm)
497 return dbm_val;
499 return dbm_val - 0x100;
502 static int stats_wireless(const char *ifname, struct ifstat *stats)
504 int ret;
505 struct iw_statistics ws;
507 ret = wireless_sigqual(ifname, &ws);
508 if (ret != 0) {
509 stats->wifi.bitrate = 0;
510 return -EINVAL;
513 stats->wifi.bitrate = wireless_bitrate(ifname);
515 stats->wifi.signal_level =
516 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
518 stats->wifi.link_qual = ws.qual.qual;
519 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
521 return ret;
524 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
525 #define DIFF(member) do { \
526 if (sizeof(diff->member) != sizeof(new->member) || \
527 sizeof(diff->member) != sizeof(old->member)) \
528 bug(); \
529 if ((new->member - old->member) > (new->member)) { \
530 diff->member = 0; \
531 } else { \
532 DIFF1(member); \
534 } while (0)
536 static void stats_diff(struct ifstat *old, struct ifstat *new,
537 struct ifstat *diff)
539 unsigned int cpus, i;
541 DIFF(rx_bytes);
542 DIFF(rx_packets);
543 DIFF(rx_drops);
544 DIFF(rx_errors);
545 DIFF(rx_fifo);
546 DIFF(rx_frame);
547 DIFF(rx_multi);
549 DIFF(tx_bytes);
550 DIFF(tx_packets);
551 DIFF(tx_drops);
552 DIFF(tx_errors);
553 DIFF(tx_fifo);
554 DIFF(tx_colls);
555 DIFF(tx_carrier);
557 DIFF1(wifi.signal_level);
558 DIFF1(wifi.link_qual);
560 DIFF1(cswitch);
562 cpus = get_number_cpus();
564 for (i = 0; i < cpus; ++i) {
565 DIFF(irqs[i]);
566 DIFF(irqs_srx[i]);
567 DIFF(irqs_stx[i]);
569 DIFF1(cpu_user[i]);
570 DIFF1(cpu_nice[i]);
571 DIFF1(cpu_sys[i]);
572 DIFF1(cpu_idle[i]);
573 DIFF1(cpu_iow[i]);
577 static void stats_fetch(const char *ifname, struct ifstat *stats)
579 if (stats_proc_net_dev(ifname, stats) < 0)
580 panic("Cannot fetch device stats!\n");
581 if (stats_proc_softirqs(stats) < 0)
582 panic("Cannot fetch software interrupts!\n");
583 if (stats_proc_memory(stats) < 0)
584 panic("Cannot fetch memory stats!\n");
585 if (stats_proc_system(stats) < 0)
586 panic("Cannot fetch system stats!\n");
587 if (stats_proc_procs(stats) < 0)
588 panic("Cannot fetch process stats!\n");
590 stats_proc_interrupts((char *) ifname, stats);
592 stats_wireless(ifname, stats);
595 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
597 unsigned int cpus = get_number_cpus();
599 stats_zero(&stats_old, cpus);
600 stats_zero(&stats_new, cpus);
601 stats_zero(&stats_delta, cpus);
603 stats_fetch(ifname, &stats_old);
604 usleep(ms_interval * 1000);
605 stats_fetch(ifname, &stats_new);
607 stats_diff(&stats_old, &stats_new, &stats_delta);
610 static int cmp_hits(const void *p1, const void *p2)
612 const struct cpu_hit *h1 = p1, *h2 = p2;
615 * We want the hits sorted in descending order, thus reverse the return
616 * values.
618 if (h1->hit == h2->hit)
619 return 0;
620 else if (h1->hit < h2->hit)
621 return 1;
622 else
623 return -1;
626 static int cmp_irqs_rel(const void *p1, const void *p2)
628 const struct cpu_hit *h1 = p1, *h2 = p2;
631 * We want the hits sorted in descending order, thus reverse the return
632 * values.
634 if (h1->irqs_rel == h2->irqs_rel)
635 return 0;
636 else if (h1->irqs_rel < h2->irqs_rel)
637 return 1;
638 else
639 return -1;
642 static int cmp_irqs_abs(const void *p1, const void *p2)
644 const struct cpu_hit *h1 = p1, *h2 = p2;
647 * We want the hits sorted in descending order, thus reverse the return
648 * values.
650 if (h1->irqs_abs == h2->irqs_abs)
651 return 0;
652 else if (h1->irqs_abs < h2->irqs_abs)
653 return 1;
654 else
655 return -1;
658 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
659 unsigned int cpus)
661 unsigned int i;
663 memset(&stats_avg, 0, sizeof(stats_avg));
665 for (i = 0; i < cpus; ++i) {
666 cpu_hits[i].idx = i;
667 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
668 cpu_hits[i].irqs_rel = rel->irqs[i];
669 cpu_hits[i].irqs_abs = abs->irqs[i];
671 stats_avg.cpu_user += rel->cpu_user[i];
672 stats_avg.cpu_sys += rel->cpu_sys[i];
673 stats_avg.cpu_nice += rel->cpu_nice[i];
674 stats_avg.cpu_idle += rel->cpu_idle[i];
675 stats_avg.cpu_iow += rel->cpu_iow[i];
677 stats_avg.irqs_abs += abs->irqs[i];
678 stats_avg.irqs_rel += rel->irqs[i];
679 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
680 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
683 stats_avg.cpu_user /= cpus;
684 stats_avg.cpu_sys /= cpus;
685 stats_avg.cpu_nice /= cpus;
686 stats_avg.cpu_idle /= cpus;
687 stats_avg.cpu_iow /= cpus;
688 stats_avg.irqs_abs /= cpus;
689 stats_avg.irqs_rel /= cpus;
690 stats_avg.irqs_srx_rel /= cpus;
691 stats_avg.irqs_stx_rel /= cpus;
694 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
695 uint64_t ms_interval, unsigned int top_cpus)
697 size_t len = 0;
698 char buff[64], machine[64];
699 struct ethtool_drvinfo drvinf;
700 u32 rate = device_bitrate(ifname);
701 int link = ethtool_link(ifname);
702 unsigned int cpus = get_number_cpus();
704 memset(&drvinf, 0, sizeof(drvinf));
705 ethtool_drvinf(ifname, &drvinf);
707 memset(buff, 0, sizeof(buff));
708 memset(machine, 0, sizeof(machine));
710 if (rate)
711 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
712 if (link >= 0)
713 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
714 link == 0 ? "no" : "yes");
716 if (!strstr(uts.release, uts.machine))
717 slprintf(machine, sizeof(machine), " %s,", uts.machine);
719 mvwprintw(screen, (*voff)++, 2,
720 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
721 " ", uts.release, machine,
722 ifname, drvinf.driver, buff, ms_interval, top_cpus,
723 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
726 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
727 int *voff)
729 attron(A_REVERSE);
731 mvwprintw(screen, (*voff)++, 0,
732 " rx: %16.3llf MiB/t "
733 "%10llu pkts/t "
734 "%10llu drops/t "
735 "%10llu errors/t ",
736 ((long double) rel->rx_bytes) / (1LLU << 20),
737 rel->rx_packets, rel->rx_drops, rel->rx_errors);
739 mvwprintw(screen, (*voff)++, 0,
740 " tx: %16.3llf MiB/t "
741 "%10llu pkts/t "
742 "%10llu drops/t "
743 "%10llu errors/t ",
744 ((long double) rel->tx_bytes) / (1LLU << 20),
745 rel->tx_packets, rel->tx_drops, rel->tx_errors);
747 attroff(A_REVERSE);
750 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
751 int *voff)
753 mvwprintw(screen, (*voff)++, 2,
754 "rx: %16.3llf MiB "
755 "%10llu pkts "
756 "%10llu drops "
757 "%10llu errors",
758 ((long double) abs->rx_bytes) / (1LLU << 20),
759 abs->rx_packets, abs->rx_drops, abs->rx_errors);
761 mvwprintw(screen, (*voff)++, 2,
762 "tx: %16.3llf MiB "
763 "%10llu pkts "
764 "%10llu drops "
765 "%10llu errors",
766 ((long double) abs->tx_bytes) / (1LLU << 20),
767 abs->tx_packets, abs->tx_drops, abs->tx_errors);
770 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
771 const struct ifstat *abs, int *voff)
773 mvwprintw(screen, (*voff)++, 2,
774 "sys: %14u cs/t "
775 "%11u procs "
776 "%11u running "
777 "%10u iowait",
778 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
781 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
783 mvwprintw(screen, (*voff)++, 2,
784 "mem: %13uM total "
785 "%9uM used "
786 "%11uM active "
787 "%10uM inactive",
788 abs->mem_total / 1024,
789 (abs->mem_total - abs->mem_free) / 1024,
790 abs->mem_active / 1024,
791 abs->mem_inactive / 1024);
793 mvwprintw(screen, (*voff)++, 2,
794 "swap: %12uM total "
795 "%9uM used "
796 "%11uM cached",
797 abs->swap_total / 1024,
798 (abs->swap_total - abs->swap_free) / 1024,
799 abs->swap_cached / 1024);
802 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
803 int *voff, unsigned int idx, char *tag)
805 int max_padd = padding_from_num(get_number_cpus());
806 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
807 rel->cpu_idle[idx] + rel->cpu_iow[idx];
809 mvwprintw(screen, (*voff)++, 2,
810 "cpu%*d %s: %11.1lf%% usr/t "
811 "%9.1lf%% sys/t "
812 "%10.1lf%% idl/t "
813 "%11.1lf%% iow/t",
814 max_padd, idx, tag,
815 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
816 100.0 * rel->cpu_sys[idx] / all,
817 100.0 * rel->cpu_idle[idx] / all,
818 100.0 * rel->cpu_iow[idx] / all);
821 #define MEDIAN_EVEN(member) do { \
822 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
823 } while (0)
825 #define MEDIAN_ODD(member) do { \
826 m_##member = rel->member[i]; \
827 } while (0)
829 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
830 const struct avg_stat *avg,
831 unsigned int top_cpus, int *voff)
833 unsigned int i;
834 unsigned int cpus = get_number_cpus();
835 int max_padd = padding_from_num(cpus);
836 uint64_t all;
838 if (top_cpus == 0)
839 return;
841 /* Display top hitter */
842 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
844 /* Make sure we don't display the min. hitter twice */
845 if (top_cpus == cpus)
846 top_cpus--;
848 for (i = 1; i < top_cpus; ++i)
849 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "|");
851 /* Display minimum hitter */
852 if (cpus != 1)
853 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
855 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
856 mvwprintw(screen, (*voff)++, 2,
857 "avg:%*s%14.1lf%% "
858 "%9.1lf%% "
859 "%10.1lf%% "
860 "%11.1lf%%", max_padd, "",
861 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
862 100.0 * avg->cpu_sys / all,
863 100.0 * avg->cpu_idle /all,
864 100.0 * avg->cpu_iow / all);
866 if (show_median) {
867 long double m_cpu_user, m_cpu_nice, m_cpu_sys, m_cpu_idle, m_cpu_iow;
868 long double m_all;
870 i = cpu_hits[cpus / 2].idx;
871 if (cpus % 2 == 0) {
872 /* take the mean of the 2 middle entries */
873 int j = cpu_hits[(cpus / 2) - 1].idx;
875 MEDIAN_EVEN(cpu_user);
876 MEDIAN_EVEN(cpu_nice);
877 MEDIAN_EVEN(cpu_sys);
878 MEDIAN_EVEN(cpu_idle);
879 MEDIAN_EVEN(cpu_iow);
880 } else {
881 /* take the middle entry as is */
882 MEDIAN_ODD(cpu_user);
883 MEDIAN_ODD(cpu_nice);
884 MEDIAN_ODD(cpu_sys);
885 MEDIAN_ODD(cpu_idle);
886 MEDIAN_ODD(cpu_iow);
889 m_all = m_cpu_user + m_cpu_sys + m_cpu_nice + m_cpu_idle + m_cpu_iow;
890 mvwprintw(screen, (*voff)++, 2,
891 "med:%*s%14.1Lf%% "
892 "%9.1Lf%% "
893 "%10.1Lf%% "
894 "%11.1Lf%%", max_padd, "",
895 100.0 * (m_cpu_user + m_cpu_nice) / m_all,
896 100.0 * m_cpu_sys / m_all,
897 100.0 * m_cpu_idle /m_all,
898 100.0 * m_cpu_iow / m_all);
902 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
903 int *voff, unsigned int idx, char *tag)
905 int max_padd = padding_from_num(get_number_cpus());
907 mvwprintw(screen, (*voff)++, 2,
908 "cpu%*d %s: %12llu irqs/t "
909 "%17llu sirq rx/t "
910 "%17llu sirq tx/t",
911 max_padd, idx, tag,
912 rel->irqs[idx],
913 rel->irqs_srx[idx],
914 rel->irqs_stx[idx]);
917 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
918 const struct avg_stat *avg,
919 unsigned int top_cpus, int *voff)
921 unsigned int i;
922 unsigned int cpus = get_number_cpus();
923 int max_padd = padding_from_num(cpus);
925 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
927 if (top_cpus == cpus)
928 top_cpus--;
930 for (i = 1; i < top_cpus; ++i)
931 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
933 if (cpus != 1)
934 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
936 mvwprintw(screen, (*voff)++, 2,
937 "avg:%*s%17.1Lf "
938 "%17.1Lf "
939 "%17.1Lf", max_padd, "",
940 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
942 if (show_median) {
943 long double m_irqs, m_irqs_srx, m_irqs_stx;
945 i = cpu_hits[cpus / 2].idx;
946 if (cpus % 2 == 0) {
947 /* take the mean of the 2 middle entries */
948 int j = cpu_hits[(cpus / 2) - 1].idx;
950 MEDIAN_EVEN(irqs);
951 MEDIAN_EVEN(irqs_srx);
952 MEDIAN_EVEN(irqs_stx);
953 } else {
954 /* take the middle entry as is */
955 MEDIAN_ODD(irqs);
956 MEDIAN_ODD(irqs_srx);
957 MEDIAN_ODD(irqs_stx);
960 mvwprintw(screen, (*voff)++, 2,
961 "med:%*s%17.1Lf "
962 "%17.1Lf "
963 "%17.1Lf", max_padd, "",
964 m_irqs, m_irqs_srx, m_irqs_stx);
968 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
969 int *voff, unsigned int idx, char *tag)
971 int max_padd = padding_from_num(get_number_cpus());
973 mvwprintw(screen, (*voff)++, 2,
974 "cpu%*d %s: %12llu irqs",
975 max_padd, idx, tag, abs->irqs[idx]);
978 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
979 const struct avg_stat *avg,
980 unsigned int top_cpus, int *voff)
982 unsigned int i;
983 unsigned int cpus = get_number_cpus();
984 int max_padd = padding_from_num(cpus);
986 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
988 if (top_cpus == cpus)
989 top_cpus--;
991 for (i = 1; i < top_cpus; ++i)
992 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
994 if (cpus != 1)
995 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
997 mvwprintw(screen, (*voff)++, 2,
998 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
1000 if (show_median) {
1001 long double m_irqs;
1003 i = cpu_hits[cpus / 2].idx;
1004 if (cpus % 2 == 0) {
1005 /* take the mean of the 2 middle entries */
1006 int j = cpu_hits[(cpus / 2) - 1].idx;
1008 m_irqs = (abs->irqs[i] + abs->irqs[j]) / 2;
1009 } else {
1010 /* take the middle entry as is */
1011 m_irqs = abs->irqs[i];
1014 mvwprintw(screen, (*voff)++, 2,
1015 "med:%*s%17.1Lf", max_padd, "", m_irqs);
1019 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
1020 const struct ifstat *abs, int *voff)
1022 if (iswireless(abs)) {
1023 mvwprintw(screen, (*voff)++, 2,
1024 "linkqual: %7d/%d (%d/t) ",
1025 abs->wifi.link_qual,
1026 abs->wifi.link_qual_max,
1027 rel->wifi.link_qual);
1029 mvwprintw(screen, (*voff)++, 2,
1030 "signal: %8d dBm (%d dBm/t) ",
1031 abs->wifi.signal_level,
1032 rel->wifi.signal_level);
1036 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
1037 const struct ifstat *abs, const struct avg_stat *avg,
1038 int *first, uint64_t ms_interval, unsigned int top_cpus,
1039 bool need_info)
1041 unsigned int cpus, top;
1042 int voff = 1, cvoff = 2;
1043 u32 rate = device_bitrate(ifname);
1045 curs_set(0);
1047 cpus = get_number_cpus();
1048 top = min(cpus, top_cpus);
1050 stats_top(rel, abs, cpus);
1052 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
1054 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
1056 voff++;
1057 screen_net_dev_rel(screen, rel, &voff);
1059 voff++;
1060 screen_net_dev_abs(screen, abs, &voff);
1062 voff++;
1063 screen_sys(screen, rel, abs, &voff);
1065 voff++;
1066 screen_mem_swap(screen, abs, &voff);
1068 voff++;
1069 screen_percpu_states(screen, rel, avg, top, &voff);
1071 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
1073 voff++;
1074 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
1076 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
1078 voff++;
1079 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
1081 voff++;
1082 screen_wireless(screen, rel, abs, &voff);
1084 if (*first) {
1085 mvwprintw(screen, cvoff, 2, "Collecting data ...");
1086 *first = 0;
1087 } else {
1088 if (need_info)
1089 mvwprintw(screen, cvoff, 2, "(consider to increase "
1090 "your sampling interval, e.g. -t %d)",
1091 rate > SPEED_1000 ? 10000 : 1000);
1092 else
1093 mvwprintw(screen, cvoff, 2, " "
1094 " ");
1097 wrefresh(screen);
1098 refresh();
1101 static int screen_main(const char *ifname, uint64_t ms_interval,
1102 unsigned int top_cpus, bool suppress_warnings)
1104 int first = 1, key;
1105 u32 rate = device_bitrate(ifname);
1106 bool need_info = false;
1108 stats_screen = screen_init(true);
1110 if (((rate > SPEED_1000 && ms_interval <= 1000) ||
1111 (rate = SPEED_1000 && ms_interval < 1000)) &&
1112 !suppress_warnings)
1113 need_info = true;
1115 while (!sigint) {
1116 key = getch();
1117 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1118 break;
1120 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1121 &first, ms_interval, top_cpus, need_info);
1123 stats_sample_generic(ifname, ms_interval);
1126 screen_end();
1128 return 0;
1131 static void term_csv(const struct ifstat *rel, const struct ifstat *abs)
1133 unsigned int cpus, i;
1135 printf("%ld ", time(NULL));
1137 printf("%llu ", rel->rx_bytes);
1138 printf("%llu ", rel->rx_packets);
1139 printf("%llu ", rel->rx_drops);
1140 printf("%llu ", rel->rx_errors);
1142 printf("%llu ", abs->rx_bytes);
1143 printf("%llu ", abs->rx_packets);
1144 printf("%llu ", abs->rx_drops);
1145 printf("%llu ", abs->rx_errors);
1147 printf("%llu ", rel->tx_bytes);
1148 printf("%llu ", rel->tx_packets);
1149 printf("%llu ", rel->tx_drops);
1150 printf("%llu ", rel->tx_errors);
1152 printf("%llu ", abs->tx_bytes);
1153 printf("%llu ", abs->tx_packets);
1154 printf("%llu ", abs->tx_drops);
1155 printf("%llu ", abs->tx_errors);
1157 printf("%u ", rel->cswitch);
1158 printf("%lu ", abs->mem_free);
1159 printf("%lu ", abs->mem_total - abs->mem_free);
1160 printf("%lu ", abs->mem_total);
1161 printf("%lu ", abs->swap_free);
1162 printf("%lu ", abs->swap_total - abs->swap_free);
1163 printf("%lu ", abs->swap_total);
1164 printf("%u ", abs->procs_total);
1165 printf("%u ", abs->procs_run);
1166 printf("%u ", abs->procs_iow);
1168 cpus = get_number_cpus();
1170 for (i = 0; i < cpus; ++i) {
1171 printf("%lu ", rel->cpu_user[i]);
1172 printf("%lu ", rel->cpu_nice[i]);
1173 printf("%lu ", rel->cpu_sys[i]);
1174 printf("%lu ", rel->cpu_idle[i]);
1175 printf("%lu ", rel->cpu_iow[i]);
1177 printf("%llu ", rel->irqs[i]);
1178 printf("%llu ", abs->irqs[i]);
1180 printf("%llu ", rel->irqs_srx[i]);
1181 printf("%llu ", abs->irqs_srx[i]);
1183 printf("%llu ", rel->irqs_stx[i]);
1184 printf("%llu ", abs->irqs_stx[i]);
1187 if (iswireless(abs)) {
1188 printf("%u ", rel->wifi.link_qual);
1189 printf("%u ", abs->wifi.link_qual);
1190 printf("%u ", abs->wifi.link_qual_max);
1192 printf("%d ", rel->wifi.signal_level);
1193 printf("%d ", abs->wifi.signal_level);
1196 puts("");
1197 fflush(stdout);
1200 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1201 uint64_t ms_interval)
1203 unsigned int cpus, i, j = 1;
1205 printf("# gnuplot dump (#col:description)\n");
1206 printf("# networking interface: %s\n", ifname);
1207 printf("# sampling interval (t): %lu ms\n", ms_interval);
1208 printf("# %d:unixtime ", j++);
1210 printf("%d:rx-bytes-per-t ", j++);
1211 printf("%d:rx-pkts-per-t ", j++);
1212 printf("%d:rx-drops-per-t ", j++);
1213 printf("%d:rx-errors-per-t ", j++);
1215 printf("%d:rx-bytes ", j++);
1216 printf("%d:rx-pkts ", j++);
1217 printf("%d:rx-drops ", j++);
1218 printf("%d:rx-errors ", j++);
1220 printf("%d:tx-bytes-per-t ", j++);
1221 printf("%d:tx-pkts-per-t ", j++);
1222 printf("%d:tx-drops-per-t ", j++);
1223 printf("%d:tx-errors-per-t ", j++);
1225 printf("%d:tx-bytes ", j++);
1226 printf("%d:tx-pkts ", j++);
1227 printf("%d:tx-drops ", j++);
1228 printf("%d:tx-errors ", j++);
1230 printf("%d:context-switches-per-t ", j++);
1231 printf("%d:mem-free ", j++);
1232 printf("%d:mem-used ", j++);
1233 printf("%d:mem-total ", j++);
1234 printf("%d:swap-free ", j++);
1235 printf("%d:swap-used ", j++);
1236 printf("%d:swap-total ", j++);
1237 printf("%d:procs-total ", j++);
1238 printf("%d:procs-in-run ", j++);
1239 printf("%d:procs-in-iow ", j++);
1241 cpus = get_number_cpus();
1243 for (i = 0; i < cpus; ++i) {
1244 printf("%d:cpu%i-usr-per-t ", j++, i);
1245 printf("%d:cpu%i-nice-per-t ", j++, i);
1246 printf("%d:cpu%i-sys-per-t ", j++, i);
1247 printf("%d:cpu%i-idle-per-t ", j++, i);
1248 printf("%d:cpu%i-iow-per-t ", j++, i);
1250 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1251 printf("%d:cpu%i-net-irqs ", j++, i);
1253 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1254 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1255 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1256 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1259 if (iswireless(abs)) {
1260 printf("%d:wifi-link-qual-per-t ", j++);
1261 printf("%d:wifi-link-qual ", j++);
1262 printf("%d:wifi-link-qual-max ", j++);
1264 printf("%d:wifi-signal-dbm-per-t ", j++);
1265 printf("%d:wifi-signal-dbm ", j++);
1268 puts("");
1269 printf("# data:\n");
1270 fflush(stdout);
1273 static int term_main(const char *ifname, uint64_t ms_interval,
1274 unsigned int top_cpus __maybe_unused,
1275 bool suppress_warnings __maybe_unused)
1277 int first = 1;
1279 do {
1280 stats_sample_generic(ifname, ms_interval);
1282 if (first) {
1283 first = 0;
1284 term_csv_header(ifname, &stats_new, ms_interval);
1287 term_csv(&stats_delta, &stats_new);
1288 } while (stats_loop && !sigint);
1290 return 0;
1293 int main(int argc, char **argv)
1295 short ifflags = 0;
1296 int c, opt_index, ret, promisc = 0;
1297 unsigned int cpus, top_cpus = 5;
1298 uint64_t interval = 1000;
1299 char *ifname = NULL;
1300 bool suppress_warnings = false;
1301 int (*func_main)(const char *ifname, uint64_t ms_interval,
1302 unsigned int top_cpus, bool suppress_warnings);
1304 func_main = screen_main;
1306 setfsuid(getuid());
1307 setfsgid(getgid());
1309 while ((c = getopt_long(argc, argv, short_options, long_options,
1310 &opt_index)) != EOF) {
1311 switch (c) {
1312 case 'h':
1313 help();
1314 break;
1315 case 'v':
1316 version();
1317 break;
1318 case 'W':
1319 suppress_warnings = true;
1320 break;
1321 case 'd':
1322 ifname = xstrndup(optarg, IFNAMSIZ);
1323 break;
1324 case 't':
1325 interval = strtoul(optarg, NULL, 10);
1326 break;
1327 case 'n':
1328 top_cpus = strtoul(optarg, NULL, 10);
1329 if (top_cpus < 1)
1330 panic("Number of top hitter CPUs must be greater than 0");
1331 break;
1332 case 'l':
1333 stats_loop = 1;
1334 break;
1335 case 'p':
1336 promisc = 1;
1337 break;
1338 case 'm':
1339 show_median = 1;
1340 break;
1341 case 'c':
1342 func_main = term_main;
1343 break;
1344 case '?':
1345 switch (optopt) {
1346 case 'd':
1347 case 't':
1348 panic("Option -%c requires an argument!\n",
1349 optopt);
1350 default:
1351 if (isprint(optopt))
1352 printf("Unknown option character `0x%X\'!\n", optopt);
1353 die();
1355 default:
1356 break;
1360 if (argc == 1)
1361 help();
1363 if (argc == 2)
1364 ifname = xstrndup(argv[1], IFNAMSIZ);
1365 if (ifname == NULL)
1366 panic("No networking device given!\n");
1368 if (!strncmp("lo", ifname, IFNAMSIZ))
1369 panic("lo is not supported!\n");
1370 if (device_mtu(ifname) == 0)
1371 panic("This is no networking device!\n");
1373 register_signal(SIGINT, signal_handler);
1374 register_signal(SIGHUP, signal_handler);
1376 cpus = get_number_cpus();
1377 top_cpus = min(top_cpus, cpus);
1378 if (uname(&uts) < 0)
1379 panic("Cannot execute uname!\n");
1381 stats_alloc(&stats_old, cpus);
1382 stats_alloc(&stats_new, cpus);
1383 stats_alloc(&stats_delta, cpus);
1385 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1387 if (promisc)
1388 ifflags = device_enter_promiscuous_mode(ifname);
1389 ret = func_main(ifname, interval, top_cpus, suppress_warnings);
1390 if (promisc)
1391 device_leave_promiscuous_mode(ifname, ifflags);
1393 stats_release(&stats_old);
1394 stats_release(&stats_new);
1395 stats_release(&stats_delta);
1397 xfree(ifname);
1398 return ret;