ifpps: Consistently list command line options
[netsniff-ng.git] / ifpps.c
blob77dfa6eec9a845d36832ab820a955ac03129d0d5
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 "promisc.h"
31 #include "cpus.h"
32 #include "config.h"
33 #include "built_in.h"
34 #include "screen.h"
36 struct wifi_stat {
37 uint32_t bitrate;
38 int16_t link_qual, link_qual_max;
39 int signal_level /*, noise_level*/;
42 struct ifstat {
43 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
44 long long unsigned int rx_fifo, rx_frame, rx_multi;
45 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
46 long long unsigned int tx_fifo, tx_colls, tx_carrier;
47 uint64_t mem_free, mem_total, mem_active, mem_inactive;
48 uint64_t swap_total, swap_free, swap_cached;
49 uint32_t irq_nr, procs_total, procs_run, procs_iow, cswitch;
50 struct wifi_stat wifi;
52 * Pointer members need to be last in order for stats_zero() to work
53 * properly.
55 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
56 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
59 struct cpu_hit {
60 unsigned int idx;
61 uint64_t hit;
62 long long unsigned int irqs_rel, irqs_abs;
65 struct avg_stat {
66 uint64_t cpu_user, cpu_sys, cpu_nice, cpu_idle, cpu_iow;
67 long double irqs_abs, irqs_rel, irqs_srx_rel, irqs_stx_rel;
70 static volatile sig_atomic_t sigint = 0;
71 static struct ifstat stats_old, stats_new, stats_delta;
72 static struct cpu_hit *cpu_hits;
73 static struct avg_stat stats_avg;
74 static int stats_loop = 0;
75 static int show_median = 0;
76 static WINDOW *stats_screen = NULL;
77 static struct utsname uts;
79 static const char *short_options = "d:n:t:clmpWvh";
80 static const struct option long_options[] = {
81 {"dev", required_argument, NULL, 'd'},
82 {"num-cpus", required_argument, NULL, 'n'},
83 {"interval", required_argument, NULL, 't'},
84 {"csv", no_argument, NULL, 'c'},
85 {"loop", no_argument, NULL, 'l'},
86 {"median", no_argument, NULL, 'm'},
87 {"promisc", no_argument, NULL, 'p'},
88 {"no-warn", no_argument, NULL, 'W'},
89 {"version", no_argument, NULL, 'v'},
90 {"help", no_argument, NULL, 'h'},
91 {NULL, 0, NULL, 0}
94 static void signal_handler(int number)
96 switch (number) {
97 case SIGINT:
98 sigint = 1;
99 break;
100 case SIGHUP:
101 default:
102 break;
106 static inline int iswireless(const struct ifstat *stats)
108 return stats->wifi.bitrate > 0;
111 static void __noreturn help(void)
113 printf("\nifpps %s, top-like kernel networking and system statistics\n",
114 VERSION_STRING);
115 puts("http://www.netsniff-ng.org\n\n"
116 "Usage: ifpps [options] || ifpps <netdev>\n"
117 "Options:\n"
118 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
119 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
120 " in ncurses mode (default 5)\n"
121 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
122 " -c|--csv Output to terminal as Gnuplot-ready data\n"
123 " -l|--loop Continuous CSV output\n"
124 " -m|--median Display median values\n"
125 " -p|--promisc Promiscuous mode\n"
126 " -W|--no-warn Suppress warnings\n"
127 " -v|--version Print version and exit\n"
128 " -h|--help Print this help and exit\n\n"
129 "Examples:\n"
130 " ifpps eth0\n"
131 " ifpps -pd eth0\n"
132 " ifpps -lpcd wlan0 > plot.dat\n\n"
133 "Note:\n"
134 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
135 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
136 "Please report bugs to <bugs@netsniff-ng.org>\n"
137 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
138 "Swiss federal institute of technology (ETH Zurich)\n"
139 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
140 "License: GNU GPL version 2.0\n"
141 "This is free software: you are free to change and redistribute it.\n"
142 "There is NO WARRANTY, to the extent permitted by law.\n");
143 die();
146 static void __noreturn version(void)
148 printf("\nifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
149 puts("top-like kernel networking and system statistics\n"
150 "http://www.netsniff-ng.org\n\n"
151 "Please report bugs to <bugs@netsniff-ng.org>\n"
152 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
153 "Swiss federal institute of technology (ETH Zurich)\n"
154 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
155 "License: GNU GPL version 2.0\n"
156 "This is free software: you are free to change and redistribute it.\n"
157 "There is NO WARRANTY, to the extent permitted by law.\n");
158 die();
161 static inline int padding_from_num(int n)
163 int i = 0;
164 do i++;
165 while ((n /= 10) > 0);
166 return i;
169 #define STATS_ALLOC1(member) \
170 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
172 static void stats_alloc(struct ifstat *stats, int cpus)
174 STATS_ALLOC1(irqs);
175 STATS_ALLOC1(irqs_srx);
176 STATS_ALLOC1(irqs_stx);
178 STATS_ALLOC1(cpu_user);
179 STATS_ALLOC1(cpu_sys);
180 STATS_ALLOC1(cpu_nice);
181 STATS_ALLOC1(cpu_idle);
182 STATS_ALLOC1(cpu_iow);
185 #define STATS_ZERO1(member) \
186 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
188 static void stats_zero(struct ifstat *stats, int cpus)
190 /* Only clear the non-pointer members */
191 memset(stats, 0, offsetof(struct ifstat, irqs));
193 STATS_ZERO1(irqs);
194 STATS_ZERO1(irqs_srx);
195 STATS_ZERO1(irqs_stx);
197 STATS_ZERO1(cpu_user);
198 STATS_ZERO1(cpu_sys);
199 STATS_ZERO1(cpu_nice);
200 STATS_ZERO1(cpu_idle);
201 STATS_ZERO1(cpu_iow);
204 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
206 int ret = -EINVAL;
207 char buff[256];
208 FILE *fp;
210 fp = fopen("/proc/net/dev", "r");
211 if (!fp)
212 panic("Cannot open /proc/net/dev!\n");
214 if (fgets(buff, sizeof(buff), fp)) { ; }
215 if (fgets(buff, sizeof(buff), fp)) { ; }
217 memset(buff, 0, sizeof(buff));
219 while (fgets(buff, sizeof(buff), fp) != NULL) {
220 buff[sizeof(buff) -1] = 0;
222 if (strstr(buff, ifname) == NULL)
223 continue;
225 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
226 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
227 &stats->rx_bytes, &stats->rx_packets,
228 &stats->rx_errors, &stats->rx_drops,
229 &stats->rx_fifo, &stats->rx_frame,
230 &stats->rx_multi, &stats->tx_bytes,
231 &stats->tx_packets, &stats->tx_errors,
232 &stats->tx_drops, &stats->tx_fifo,
233 &stats->tx_colls, &stats->tx_carrier) == 14) {
234 ret = 0;
235 break;
238 memset(buff, 0, sizeof(buff));
241 fclose(fp);
242 return ret;
245 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
247 int ret = -EINVAL, i, cpus, try = 0;
248 char *ptr, *buff;
249 bool seen = false;
250 size_t buff_len;
251 struct ethtool_drvinfo drvinf;
252 FILE *fp;
254 fp = fopen("/proc/interrupts", "r");
255 if (!fp)
256 panic("Cannot open /proc/interrupts!\n");
258 cpus = get_number_cpus();
259 buff_len = cpus * 128;
260 buff = xmalloc(buff_len);
261 retry:
262 fseek(fp, 0, SEEK_SET);
263 memset(buff, 0, buff_len);
265 while (fgets(buff, buff_len, fp) != NULL) {
266 buff[buff_len - 1] = 0;
267 ptr = buff;
269 if (strstr(buff, ifname) == NULL)
270 continue;
272 /* XXX: remove this one here */
273 stats->irq_nr = strtol(ptr, &ptr, 10);
274 bug_on(stats->irq_nr == 0);
276 if (ptr)
277 ptr++;
278 for (i = 0; i < cpus && ptr; ++i) {
279 if (seen)
280 stats->irqs[i] += strtol(ptr, &ptr, 10);
281 else
282 stats->irqs[i] = strtol(ptr, &ptr, 10);
283 if (i == cpus - 1) {
284 ret = 0;
285 seen = true;
289 memset(buff, 0, buff_len);
292 if (ret == -EINVAL && try == 0) {
293 memset(&drvinf, 0, sizeof(drvinf));
294 if (ethtool_drvinf(ifname, &drvinf) < 0)
295 goto done;
297 ifname = drvinf.driver;
298 try++;
300 goto retry;
302 done:
303 xfree(buff);
304 fclose(fp);
305 return ret;
308 static int stats_proc_softirqs(struct ifstat *stats)
310 int i, cpus;
311 char *ptr, *buff;
312 size_t buff_len;
313 FILE *fp;
314 enum {
315 softirqs_net_rx,
316 softirqs_net_tx,
317 } net_type;
319 fp = fopen("/proc/softirqs", "r");
320 if (!fp)
321 panic("Cannot open /proc/softirqs!\n");
323 cpus = get_number_cpus();
324 buff_len = cpus * 128;
325 buff = xmalloc(buff_len);
327 memset(buff, 0, buff_len);
329 while (fgets(buff, buff_len, fp) != NULL) {
330 buff[buff_len - 1] = 0;
332 if ((ptr = strstr(buff, "NET_TX:")))
333 net_type = softirqs_net_tx;
334 else if ((ptr = strstr(buff, "NET_RX:")))
335 net_type = softirqs_net_rx;
336 else
337 continue;
339 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
340 switch (net_type) {
341 case softirqs_net_tx:
342 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
343 break;
344 case softirqs_net_rx:
345 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
346 break;
350 memset(buff, 0, buff_len);
353 xfree(buff);
354 fclose(fp);
355 return 0;
358 static int stats_proc_memory(struct ifstat *stats)
360 char *ptr, buff[256];
361 FILE *fp;
363 fp = fopen("/proc/meminfo", "r");
364 if (!fp)
365 panic("Cannot open /proc/meminfo!\n");
367 memset(buff, 0, sizeof(buff));
369 while (fgets(buff, sizeof(buff), fp) != NULL) {
370 buff[sizeof(buff) - 1] = 0;
372 if ((ptr = strstr(buff, "MemTotal:"))) {
373 ptr += strlen("MemTotal:");
374 stats->mem_total = strtoul(ptr, &ptr, 10);
375 } else if ((ptr = strstr(buff, "MemFree:"))) {
376 ptr += strlen("MemFree:");
377 stats->mem_free = strtoul(ptr, &ptr, 10);
378 } else if ((ptr = strstr(buff, "Active:"))) {
379 ptr += strlen("Active:");
380 stats->mem_active = strtoul(ptr, &ptr, 10);
381 } else if ((ptr = strstr(buff, "Inactive:"))) {
382 ptr += strlen("Inactive:");
383 stats->mem_inactive = strtoul(ptr, &ptr, 10);
384 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
385 ptr += strlen("SwapTotal:");
386 stats->swap_total = strtoul(ptr, &ptr, 10);
387 } else if ((ptr = strstr(buff, "SwapFree:"))) {
388 ptr += strlen("SwapFree:");
389 stats->swap_free = strtoul(ptr, &ptr, 10);
390 } else if ((ptr = strstr(buff, "SwapCached:"))) {
391 ptr += strlen("SwapCached:");
392 stats->swap_cached = strtoul(ptr, &ptr, 10);
395 memset(buff, 0, sizeof(buff));
398 fclose(fp);
399 return 0;
402 static int stats_proc_system(struct ifstat *stats)
404 int cpu, cpus;
405 char *ptr, buff[256];
406 FILE *fp;
408 fp = fopen("/proc/stat", "r");
409 if (!fp)
410 panic("Cannot open /proc/stat!\n");
412 cpus = get_number_cpus();
414 memset(buff, 0, sizeof(buff));
416 while (fgets(buff, sizeof(buff), fp) != NULL) {
417 buff[sizeof(buff) - 1] = 0;
419 if ((ptr = strstr(buff, "cpu"))) {
420 ptr += strlen("cpu");
421 if (isblank(*ptr))
422 goto next;
424 cpu = strtol(ptr, &ptr, 10);
425 bug_on(cpu > cpus);
427 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
428 &stats->cpu_user[cpu],
429 &stats->cpu_nice[cpu],
430 &stats->cpu_sys[cpu],
431 &stats->cpu_idle[cpu],
432 &stats->cpu_iow[cpu]) != 5)
433 goto next;
434 } else if ((ptr = strstr(buff, "ctxt"))) {
435 ptr += strlen("ctxt");
436 stats->cswitch = strtoul(ptr, &ptr, 10);
437 } else if ((ptr = strstr(buff, "procs_running"))) {
438 ptr += strlen("procs_running");
439 stats->procs_run = strtoul(ptr, &ptr, 10);
440 } else if ((ptr = strstr(buff, "procs_blocked"))) {
441 ptr += strlen("procs_blocked");
442 stats->procs_iow = strtoul(ptr, &ptr, 10);
444 next:
445 memset(buff, 0, sizeof(buff));
448 fclose(fp);
449 return 0;
452 static int stats_proc_procs(struct ifstat *stats)
454 DIR *dir;
455 struct dirent *e;
457 dir = opendir("/proc");
458 if (!dir)
459 panic("Cannot open /proc\n");
461 stats->procs_total = 0;
463 while ((e = readdir(dir)) != NULL) {
464 const char *name = e->d_name;
465 char *end;
466 unsigned int pid = strtoul(name, &end, 10);
468 /* not a number */
469 if (pid == 0 && end == name)
470 continue;
472 stats->procs_total++;
475 closedir(dir);
477 return 0;
480 static int adjust_dbm_level(int in_dbm, int dbm_val)
482 if (!in_dbm)
483 return dbm_val;
485 return dbm_val - 0x100;
488 static int stats_wireless(const char *ifname, struct ifstat *stats)
490 int ret;
491 struct iw_statistics ws;
493 ret = wireless_sigqual(ifname, &ws);
494 if (ret != 0) {
495 stats->wifi.bitrate = 0;
496 return -EINVAL;
499 stats->wifi.bitrate = wireless_bitrate(ifname);
501 stats->wifi.signal_level =
502 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
504 stats->wifi.link_qual = ws.qual.qual;
505 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
507 return ret;
510 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
511 #define DIFF(member) do { \
512 if (sizeof(diff->member) != sizeof(new->member) || \
513 sizeof(diff->member) != sizeof(old->member)) \
514 bug(); \
515 bug_on((new->member - old->member) > (new->member)); \
516 DIFF1(member); \
517 } while (0)
519 static void stats_diff(struct ifstat *old, struct ifstat *new,
520 struct ifstat *diff)
522 int cpus, i;
524 DIFF(rx_bytes);
525 DIFF(rx_packets);
526 DIFF(rx_drops);
527 DIFF(rx_errors);
528 DIFF(rx_fifo);
529 DIFF(rx_frame);
530 DIFF(rx_multi);
532 DIFF(tx_bytes);
533 DIFF(tx_packets);
534 DIFF(tx_drops);
535 DIFF(tx_errors);
536 DIFF(tx_fifo);
537 DIFF(tx_colls);
538 DIFF(tx_carrier);
540 DIFF1(wifi.signal_level);
541 DIFF1(wifi.link_qual);
543 DIFF1(cswitch);
545 cpus = get_number_cpus();
547 for (i = 0; i < cpus; ++i) {
548 DIFF(irqs[i]);
549 DIFF(irqs_srx[i]);
550 DIFF(irqs_stx[i]);
552 DIFF1(cpu_user[i]);
553 DIFF1(cpu_nice[i]);
554 DIFF1(cpu_sys[i]);
555 DIFF1(cpu_idle[i]);
556 DIFF1(cpu_iow[i]);
560 static void stats_fetch(const char *ifname, struct ifstat *stats)
562 if (stats_proc_net_dev(ifname, stats) < 0)
563 panic("Cannot fetch device stats!\n");
564 if (stats_proc_softirqs(stats) < 0)
565 panic("Cannot fetch software interrupts!\n");
566 if (stats_proc_memory(stats) < 0)
567 panic("Cannot fetch memory stats!\n");
568 if (stats_proc_system(stats) < 0)
569 panic("Cannot fetch system stats!\n");
570 if (stats_proc_procs(stats) < 0)
571 panic("Cannot fetch process stats!\n");
573 stats_proc_interrupts((char *) ifname, stats);
575 stats_wireless(ifname, stats);
578 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
580 int cpus = get_number_cpus();
582 stats_zero(&stats_old, cpus);
583 stats_zero(&stats_new, cpus);
584 stats_zero(&stats_delta, cpus);
586 stats_fetch(ifname, &stats_old);
587 usleep(ms_interval * 1000);
588 stats_fetch(ifname, &stats_new);
590 stats_diff(&stats_old, &stats_new, &stats_delta);
593 static int cmp_hits(const void *p1, const void *p2)
595 const struct cpu_hit *h1 = p1, *h2 = p2;
598 * We want the hits sorted in descending order, thus reverse the return
599 * values.
601 if (h1->hit == h2->hit)
602 return 0;
603 else if (h1->hit < h2->hit)
604 return 1;
605 else
606 return -1;
609 static int cmp_irqs_rel(const void *p1, const void *p2)
611 const struct cpu_hit *h1 = p1, *h2 = p2;
614 * We want the hits sorted in descending order, thus reverse the return
615 * values.
617 if (h1->irqs_rel == h2->irqs_rel)
618 return 0;
619 else if (h1->irqs_rel < h2->irqs_rel)
620 return 1;
621 else
622 return -1;
625 static int cmp_irqs_abs(const void *p1, const void *p2)
627 const struct cpu_hit *h1 = p1, *h2 = p2;
630 * We want the hits sorted in descending order, thus reverse the return
631 * values.
633 if (h1->irqs_abs == h2->irqs_abs)
634 return 0;
635 else if (h1->irqs_abs < h2->irqs_abs)
636 return 1;
637 else
638 return -1;
641 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
642 int cpus)
644 int i;
646 memset(&stats_avg, 0, sizeof(stats_avg));
648 for (i = 0; i < cpus; ++i) {
649 cpu_hits[i].idx = i;
650 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
651 cpu_hits[i].irqs_rel = rel->irqs[i];
652 cpu_hits[i].irqs_abs = abs->irqs[i];
654 stats_avg.cpu_user += rel->cpu_user[i];
655 stats_avg.cpu_sys += rel->cpu_sys[i];
656 stats_avg.cpu_nice += rel->cpu_nice[i];
657 stats_avg.cpu_idle += rel->cpu_idle[i];
658 stats_avg.cpu_iow += rel->cpu_iow[i];
660 stats_avg.irqs_abs += abs->irqs[i];
661 stats_avg.irqs_rel += rel->irqs[i];
662 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
663 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
666 stats_avg.cpu_user /= cpus;
667 stats_avg.cpu_sys /= cpus;
668 stats_avg.cpu_nice /= cpus;
669 stats_avg.cpu_idle /= cpus;
670 stats_avg.cpu_iow /= cpus;
671 stats_avg.irqs_abs /= cpus;
672 stats_avg.irqs_rel /= cpus;
673 stats_avg.irqs_srx_rel /= cpus;
674 stats_avg.irqs_stx_rel /= cpus;
677 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
678 uint64_t ms_interval, unsigned int top_cpus)
680 size_t len = 0;
681 char buff[64], machine[64];
682 struct ethtool_drvinfo drvinf;
683 u32 rate = device_bitrate(ifname);
684 int link = ethtool_link(ifname);
685 unsigned int cpus = get_number_cpus();
687 memset(&drvinf, 0, sizeof(drvinf));
688 ethtool_drvinf(ifname, &drvinf);
690 memset(buff, 0, sizeof(buff));
691 memset(machine, 0, sizeof(machine));
693 if (rate)
694 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
695 if (link >= 0)
696 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
697 link == 0 ? "no" : "yes");
699 if (!strstr(uts.release, uts.machine))
700 slprintf(machine, sizeof(machine), " %s,", uts.machine);
702 mvwprintw(screen, (*voff)++, 2,
703 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
704 " ", uts.release, machine,
705 ifname, drvinf.driver, buff, ms_interval, top_cpus,
706 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
709 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
710 int *voff)
712 attron(A_REVERSE);
714 mvwprintw(screen, (*voff)++, 0,
715 " rx: %16.3llf MiB/t "
716 "%10llu pkts/t "
717 "%10llu drops/t "
718 "%10llu errors/t ",
719 ((long double) rel->rx_bytes) / (1LLU << 20),
720 rel->rx_packets, rel->rx_drops, rel->rx_errors);
722 mvwprintw(screen, (*voff)++, 0,
723 " tx: %16.3llf MiB/t "
724 "%10llu pkts/t "
725 "%10llu drops/t "
726 "%10llu errors/t ",
727 ((long double) rel->tx_bytes) / (1LLU << 20),
728 rel->tx_packets, rel->tx_drops, rel->tx_errors);
730 attroff(A_REVERSE);
733 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
734 int *voff)
736 mvwprintw(screen, (*voff)++, 2,
737 "rx: %16.3llf MiB "
738 "%10llu pkts "
739 "%10llu drops "
740 "%10llu errors",
741 ((long double) abs->rx_bytes) / (1LLU << 20),
742 abs->rx_packets, abs->rx_drops, abs->rx_errors);
744 mvwprintw(screen, (*voff)++, 2,
745 "tx: %16.3llf MiB "
746 "%10llu pkts "
747 "%10llu drops "
748 "%10llu errors",
749 ((long double) abs->tx_bytes) / (1LLU << 20),
750 abs->tx_packets, abs->tx_drops, abs->tx_errors);
753 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
754 const struct ifstat *abs, int *voff)
756 mvwprintw(screen, (*voff)++, 2,
757 "sys: %14u cs/t "
758 "%11u procs "
759 "%11u running "
760 "%10u iowait",
761 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
764 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
766 mvwprintw(screen, (*voff)++, 2,
767 "mem: %13uM total "
768 "%9uM used "
769 "%11uM active "
770 "%10uM inactive",
771 abs->mem_total / 1024,
772 (abs->mem_total - abs->mem_free) / 1024,
773 abs->mem_active / 1024,
774 abs->mem_inactive / 1024);
776 mvwprintw(screen, (*voff)++, 2,
777 "swap: %12uM total "
778 "%9uM used "
779 "%11uM cached",
780 abs->swap_total / 1024,
781 (abs->swap_total - abs->swap_free) / 1024,
782 abs->swap_cached / 1024);
785 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
786 int *voff, unsigned int idx, char *tag)
788 int max_padd = padding_from_num(get_number_cpus());
789 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
790 rel->cpu_idle[idx] + rel->cpu_iow[idx];
792 mvwprintw(screen, (*voff)++, 2,
793 "cpu%*d %s: %11.1lf%% usr/t "
794 "%9.1lf%% sys/t "
795 "%10.1lf%% idl/t "
796 "%11.1lf%% iow/t",
797 max_padd, idx, tag,
798 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
799 100.0 * rel->cpu_sys[idx] / all,
800 100.0 * rel->cpu_idle[idx] / all,
801 100.0 * rel->cpu_iow[idx] / all);
804 #define MEDIAN_EVEN(member) do { \
805 m_##member = (rel->member[i] + rel->member[j]) / 2; \
806 } while (0)
808 #define MEDIAN_ODD(member) do { \
809 m_##member = rel->member[i]; \
810 } while (0)
812 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
813 const struct avg_stat *avg, int top_cpus,
814 int *voff)
816 int i;
817 int cpus = get_number_cpus();
818 int max_padd = padding_from_num(cpus);
819 uint64_t all;
821 if (top_cpus == 0)
822 return;
824 /* Display top hitter */
825 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
827 /* Make sure we don't display the min. hitter twice */
828 if (top_cpus == cpus)
829 top_cpus--;
831 for (i = 1; i < top_cpus; ++i)
832 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "|");
834 /* Display minimum hitter */
835 if (cpus != 1)
836 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
838 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
839 mvwprintw(screen, (*voff)++, 2,
840 "avg:%*s%14.1lf%% "
841 "%9.1lf%% "
842 "%10.1lf%% "
843 "%11.1lf%%", max_padd, "",
844 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
845 100.0 * avg->cpu_sys / all,
846 100.0 * avg->cpu_idle /all,
847 100.0 * avg->cpu_iow / all);
849 if (show_median) {
850 long double m_cpu_user, m_cpu_nice, m_cpu_sys, m_cpu_idle, m_cpu_iow;
852 i = cpu_hits[cpus / 2].idx;
853 if (cpus % 2 == 0) {
854 /* take the mean of the 2 middle entries */
855 int j = cpu_hits[(cpus / 2) - 1].idx;
857 MEDIAN_EVEN(cpu_user);
858 MEDIAN_EVEN(cpu_nice);
859 MEDIAN_EVEN(cpu_sys);
860 MEDIAN_EVEN(cpu_idle);
861 MEDIAN_EVEN(cpu_iow);
862 } else {
863 /* take the middle entry as is */
864 MEDIAN_ODD(cpu_user);
865 MEDIAN_ODD(cpu_nice);
866 MEDIAN_ODD(cpu_sys);
867 MEDIAN_ODD(cpu_idle);
868 MEDIAN_ODD(cpu_iow);
871 all = m_cpu_user + m_cpu_sys + m_cpu_nice + m_cpu_idle + m_cpu_iow;
872 mvwprintw(screen, (*voff)++, 2,
873 "med:%*s%14.1lf%% "
874 "%9.1lf%% "
875 "%10.1lf%% "
876 "%11.1lf%%", max_padd, "",
877 100.0 * (m_cpu_user + m_cpu_nice) / all,
878 100.0 * m_cpu_sys / all,
879 100.0 * m_cpu_idle /all,
880 100.0 * m_cpu_iow / all);
884 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
885 int *voff, unsigned int idx, char *tag)
887 int max_padd = padding_from_num(get_number_cpus());
889 mvwprintw(screen, (*voff)++, 2,
890 "cpu%*d %s: %12llu irqs/t "
891 "%17llu sirq rx/t "
892 "%17llu sirq tx/t",
893 max_padd, idx, tag,
894 rel->irqs[idx],
895 rel->irqs_srx[idx],
896 rel->irqs_stx[idx]);
899 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
900 const struct avg_stat *avg, int top_cpus,
901 int *voff)
903 int i;
904 int cpus = get_number_cpus();
905 int max_padd = padding_from_num(cpus);
907 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
909 if (top_cpus == cpus)
910 top_cpus--;
912 for (i = 1; i < top_cpus; ++i)
913 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
915 if (cpus != 1)
916 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
918 mvwprintw(screen, (*voff)++, 2,
919 "avg:%*s%17.1Lf "
920 "%17.1Lf "
921 "%17.1Lf", max_padd, "",
922 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
924 if (show_median) {
925 long double m_irqs, m_irqs_srx, m_irqs_stx;
927 i = cpu_hits[cpus / 2].idx;
928 if (cpus % 2 == 0) {
929 /* take the mean of the 2 middle entries */
930 int j = cpu_hits[(cpus / 2) - 1].idx;
932 MEDIAN_EVEN(irqs);
933 MEDIAN_EVEN(irqs_srx);
934 MEDIAN_EVEN(irqs_stx);
935 } else {
936 /* take the middle entry as is */
937 MEDIAN_ODD(irqs);
938 MEDIAN_ODD(irqs_srx);
939 MEDIAN_ODD(irqs_stx);
942 mvwprintw(screen, (*voff)++, 2,
943 "med:%*s%17.1Lf "
944 "%17.1Lf "
945 "%17.1Lf", max_padd, "",
946 m_irqs, m_irqs_srx, m_irqs_stx);
950 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
951 int *voff, unsigned int idx, char *tag)
953 int max_padd = padding_from_num(get_number_cpus());
955 mvwprintw(screen, (*voff)++, 2,
956 "cpu%*d %s: %12llu irqs",
957 max_padd, idx, tag, abs->irqs[idx]);
960 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
961 const struct avg_stat *avg, int top_cpus,
962 int *voff)
964 int i;
965 int cpus = get_number_cpus();
966 int max_padd = padding_from_num(cpus);
968 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
970 if (top_cpus == cpus)
971 top_cpus--;
973 for (i = 1; i < top_cpus; ++i)
974 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
976 if (cpus != 1)
977 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
979 mvwprintw(screen, (*voff)++, 2,
980 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
982 if (show_median) {
983 long double m_irqs;
985 i = cpu_hits[cpus / 2].idx;
986 if (cpus % 2 == 0) {
987 /* take the mean of the 2 middle entries */
988 int j = cpu_hits[(cpus / 2) - 1].idx;
990 m_irqs = (abs->irqs[i] + abs->irqs[j]) / 2;
991 } else {
992 /* take the middle entry as is */
993 m_irqs = abs->irqs[i];
996 mvwprintw(screen, (*voff)++, 2,
997 "med:%*s%17.1Lf", max_padd, "", m_irqs);
1001 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
1002 const struct ifstat *abs, int *voff)
1004 if (iswireless(abs)) {
1005 mvwprintw(screen, (*voff)++, 2,
1006 "linkqual: %7d/%d (%d/t) ",
1007 abs->wifi.link_qual,
1008 abs->wifi.link_qual_max,
1009 rel->wifi.link_qual);
1011 mvwprintw(screen, (*voff)++, 2,
1012 "signal: %8d dBm (%d dBm/t) ",
1013 abs->wifi.signal_level,
1014 rel->wifi.signal_level);
1018 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
1019 const struct ifstat *abs, const struct avg_stat *avg,
1020 int *first, uint64_t ms_interval, unsigned int top_cpus,
1021 bool need_info)
1023 int cpus, top, voff = 1, cvoff = 2;
1025 curs_set(0);
1027 cpus = get_number_cpus();
1028 top = min(cpus, top_cpus);
1030 stats_top(rel, abs, cpus);
1032 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
1034 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
1036 voff++;
1037 screen_net_dev_rel(screen, rel, &voff);
1039 voff++;
1040 screen_net_dev_abs(screen, abs, &voff);
1042 voff++;
1043 screen_sys(screen, rel, abs, &voff);
1045 voff++;
1046 screen_mem_swap(screen, abs, &voff);
1048 voff++;
1049 screen_percpu_states(screen, rel, avg, top, &voff);
1051 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
1053 voff++;
1054 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
1056 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
1058 voff++;
1059 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
1061 voff++;
1062 screen_wireless(screen, rel, abs, &voff);
1064 if (*first) {
1065 mvwprintw(screen, cvoff, 2, "Collecting data ...");
1066 *first = 0;
1067 } else {
1068 if (need_info)
1069 mvwprintw(screen, cvoff, 2, "(consider to increase "
1070 "your sampling interval, e.g. -t 10000)");
1071 else
1072 mvwprintw(screen, cvoff, 2, " "
1073 " ");
1076 wrefresh(screen);
1077 refresh();
1080 static int screen_main(const char *ifname, uint64_t ms_interval,
1081 unsigned int top_cpus, bool suppress_warnings)
1083 int first = 1, key;
1084 u32 rate = device_bitrate(ifname);
1085 bool need_info = false;
1087 stats_screen = screen_init(true);
1089 if (rate > SPEED_1000 && ms_interval <= 1000 && !suppress_warnings)
1090 need_info = true;
1092 while (!sigint) {
1093 key = getch();
1094 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1095 break;
1097 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1098 &first, ms_interval, top_cpus, need_info);
1100 stats_sample_generic(ifname, ms_interval);
1103 screen_end();
1105 return 0;
1108 static void term_csv(const char *ifname, const struct ifstat *rel,
1109 const struct ifstat *abs, uint64_t ms_interval)
1111 int cpus, i;
1113 printf("%ld ", time(0));
1115 printf("%llu ", rel->rx_bytes);
1116 printf("%llu ", rel->rx_packets);
1117 printf("%llu ", rel->rx_drops);
1118 printf("%llu ", rel->rx_errors);
1120 printf("%llu ", abs->rx_bytes);
1121 printf("%llu ", abs->rx_packets);
1122 printf("%llu ", abs->rx_drops);
1123 printf("%llu ", abs->rx_errors);
1125 printf("%llu ", rel->tx_bytes);
1126 printf("%llu ", rel->tx_packets);
1127 printf("%llu ", rel->tx_drops);
1128 printf("%llu ", rel->tx_errors);
1130 printf("%llu ", abs->tx_bytes);
1131 printf("%llu ", abs->tx_packets);
1132 printf("%llu ", abs->tx_drops);
1133 printf("%llu ", abs->tx_errors);
1135 printf("%u ", rel->cswitch);
1136 printf("%lu ", abs->mem_free);
1137 printf("%lu ", abs->mem_total - abs->mem_free);
1138 printf("%lu ", abs->mem_total);
1139 printf("%lu ", abs->swap_free);
1140 printf("%lu ", abs->swap_total - abs->swap_free);
1141 printf("%lu ", abs->swap_total);
1142 printf("%u ", abs->procs_total);
1143 printf("%u ", abs->procs_run);
1144 printf("%u ", abs->procs_iow);
1146 cpus = get_number_cpus();
1148 for (i = 0; i < cpus; ++i) {
1149 printf("%lu ", rel->cpu_user[i]);
1150 printf("%lu ", rel->cpu_nice[i]);
1151 printf("%lu ", rel->cpu_sys[i]);
1152 printf("%lu ", rel->cpu_idle[i]);
1153 printf("%lu ", rel->cpu_iow[i]);
1155 printf("%llu ", rel->irqs[i]);
1156 printf("%llu ", abs->irqs[i]);
1158 printf("%llu ", rel->irqs_srx[i]);
1159 printf("%llu ", abs->irqs_srx[i]);
1161 printf("%llu ", rel->irqs_stx[i]);
1162 printf("%llu ", abs->irqs_stx[i]);
1165 if (iswireless(abs)) {
1166 printf("%u ", rel->wifi.link_qual);
1167 printf("%u ", abs->wifi.link_qual);
1168 printf("%u ", abs->wifi.link_qual_max);
1170 printf("%d ", rel->wifi.signal_level);
1171 printf("%d ", abs->wifi.signal_level);
1174 puts("");
1175 fflush(stdout);
1178 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1179 uint64_t ms_interval)
1181 int cpus, i, j = 1;
1183 printf("# gnuplot dump (#col:description)\n");
1184 printf("# networking interface: %s\n", ifname);
1185 printf("# sampling interval (t): %lu ms\n", ms_interval);
1186 printf("# %d:unixtime ", j++);
1188 printf("%d:rx-bytes-per-t ", j++);
1189 printf("%d:rx-pkts-per-t ", j++);
1190 printf("%d:rx-drops-per-t ", j++);
1191 printf("%d:rx-errors-per-t ", j++);
1193 printf("%d:rx-bytes ", j++);
1194 printf("%d:rx-pkts ", j++);
1195 printf("%d:rx-drops ", j++);
1196 printf("%d:rx-errors ", j++);
1198 printf("%d:tx-bytes-per-t ", j++);
1199 printf("%d:tx-pkts-per-t ", j++);
1200 printf("%d:tx-drops-per-t ", j++);
1201 printf("%d:tx-errors-per-t ", j++);
1203 printf("%d:tx-bytes ", j++);
1204 printf("%d:tx-pkts ", j++);
1205 printf("%d:tx-drops ", j++);
1206 printf("%d:tx-errors ", j++);
1208 printf("%d:context-switches-per-t ", j++);
1209 printf("%d:mem-free ", j++);
1210 printf("%d:mem-used ", j++);
1211 printf("%d:mem-total ", j++);
1212 printf("%d:swap-free ", j++);
1213 printf("%d:swap-used ", j++);
1214 printf("%d:swap-total ", j++);
1215 printf("%d:procs-total ", j++);
1216 printf("%d:procs-in-run ", j++);
1217 printf("%d:procs-in-iow ", j++);
1219 cpus = get_number_cpus();
1221 for (i = 0; i < cpus; ++i) {
1222 printf("%d:cpu%i-usr-per-t ", j++, i);
1223 printf("%d:cpu%i-nice-per-t ", j++, i);
1224 printf("%d:cpu%i-sys-per-t ", j++, i);
1225 printf("%d:cpu%i-idle-per-t ", j++, i);
1226 printf("%d:cpu%i-iow-per-t ", j++, i);
1228 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1229 printf("%d:cpu%i-net-irqs ", j++, i);
1231 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1232 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1233 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1234 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1237 if (iswireless(abs)) {
1238 printf("%d:wifi-link-qual-per-t ", j++);
1239 printf("%d:wifi-link-qual ", j++);
1240 printf("%d:wifi-link-qual-max ", j++);
1242 printf("%d:wifi-signal-dbm-per-t ", j++);
1243 printf("%d:wifi-signal-dbm ", j++);
1246 puts("");
1247 printf("# data:\n");
1248 fflush(stdout);
1251 static int term_main(const char *ifname, uint64_t ms_interval,
1252 unsigned int top_cpus __maybe_unused,
1253 bool suppress_warnings __maybe_unused)
1255 int first = 1;
1257 do {
1258 stats_sample_generic(ifname, ms_interval);
1260 if (first) {
1261 first = 0;
1262 term_csv_header(ifname, &stats_new, ms_interval);
1265 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1266 } while (stats_loop && !sigint);
1268 return 0;
1271 int main(int argc, char **argv)
1273 short ifflags = 0;
1274 int c, opt_index, ret, cpus, promisc = 0;
1275 unsigned int top_cpus = 5;
1276 uint64_t interval = 1000;
1277 char *ifname = NULL;
1278 bool suppress_warnings = false;
1279 int (*func_main)(const char *ifname, uint64_t ms_interval,
1280 unsigned int top_cpus, bool suppress_warnings);
1282 func_main = screen_main;
1284 setfsuid(getuid());
1285 setfsgid(getgid());
1287 while ((c = getopt_long(argc, argv, short_options, long_options,
1288 &opt_index)) != EOF) {
1289 switch (c) {
1290 case 'h':
1291 help();
1292 break;
1293 case 'v':
1294 version();
1295 break;
1296 case 'W':
1297 suppress_warnings = true;
1298 break;
1299 case 'd':
1300 ifname = xstrndup(optarg, IFNAMSIZ);
1301 break;
1302 case 't':
1303 interval = strtoul(optarg, NULL, 10);
1304 break;
1305 case 'n':
1306 top_cpus = strtoul(optarg, NULL, 10);
1307 if (top_cpus < 1)
1308 panic("Number of top hitter CPUs must be greater than 0");
1309 break;
1310 case 'l':
1311 stats_loop = 1;
1312 break;
1313 case 'p':
1314 promisc = 1;
1315 break;
1316 case 'm':
1317 show_median = 1;
1318 break;
1319 case 'c':
1320 func_main = term_main;
1321 break;
1322 case '?':
1323 switch (optopt) {
1324 case 'd':
1325 case 't':
1326 panic("Option -%c requires an argument!\n",
1327 optopt);
1328 default:
1329 if (isprint(optopt))
1330 printf("Unknown option character `0x%X\'!\n", optopt);
1331 die();
1333 default:
1334 break;
1338 if (argc == 1)
1339 help();
1341 if (argc == 2)
1342 ifname = xstrndup(argv[1], IFNAMSIZ);
1343 if (ifname == NULL)
1344 panic("No networking device given!\n");
1346 if (!strncmp("lo", ifname, IFNAMSIZ))
1347 panic("lo is not supported!\n");
1348 if (device_mtu(ifname) == 0)
1349 panic("This is no networking device!\n");
1351 register_signal(SIGINT, signal_handler);
1352 register_signal(SIGHUP, signal_handler);
1354 cpus = get_number_cpus();
1355 top_cpus = min(top_cpus, cpus);
1356 if (uname(&uts) < 0)
1357 panic("Cannot execute uname!\n");
1359 stats_alloc(&stats_old, cpus);
1360 stats_alloc(&stats_new, cpus);
1361 stats_alloc(&stats_delta, cpus);
1363 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1365 if (promisc)
1366 ifflags = enter_promiscuous_mode(ifname);
1367 ret = func_main(ifname, interval, top_cpus, suppress_warnings);
1368 if (promisc)
1369 leave_promiscuous_mode(ifname, ifflags);
1371 xfree(ifname);
1372 return ret;