dissector: ip_auth_hdr: Fix compiler warning
[netsniff-ng.git] / ifpps.c
blob93446259c771c1bb15e45b037e20ed7a25115c1c
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 in ncurses mode (def: 5)\n"
120 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
121 " -c|--csv Output to terminal as Gnuplot-ready data\n"
122 " -l|--loop Continuous CSV output\n"
123 " -m|--median Display median values\n"
124 " -p|--promisc Promiscuous mode\n"
125 " -W|--no-warn Suppress warnings\n"
126 " -v|--version Print version and exit\n"
127 " -h|--help Print this help and exit\n\n"
128 "Examples:\n"
129 " ifpps eth0\n"
130 " ifpps -pd eth0\n"
131 " ifpps -lpcd wlan0 > plot.dat\n\n"
132 "Note:\n"
133 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
134 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
135 "Please report bugs to <bugs@netsniff-ng.org>\n"
136 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
137 "Swiss federal institute of technology (ETH Zurich)\n"
138 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
139 "License: GNU GPL version 2.0\n"
140 "This is free software: you are free to change and redistribute it.\n"
141 "There is NO WARRANTY, to the extent permitted by law.\n");
142 die();
145 static void __noreturn version(void)
147 printf("\nifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
148 puts("top-like kernel networking and system statistics\n"
149 "http://www.netsniff-ng.org\n\n"
150 "Please report bugs to <bugs@netsniff-ng.org>\n"
151 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
152 "Swiss federal institute of technology (ETH Zurich)\n"
153 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
154 "License: GNU GPL version 2.0\n"
155 "This is free software: you are free to change and redistribute it.\n"
156 "There is NO WARRANTY, to the extent permitted by law.\n");
157 die();
160 static inline int padding_from_num(int n)
162 int i = 0;
163 do 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, 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, 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, i, cpus, try = 0;
263 char *ptr, *buff;
264 bool seen = false;
265 size_t buff_len;
266 struct ethtool_drvinfo drvinf;
267 FILE *fp;
269 fp = fopen("/proc/interrupts", "r");
270 if (!fp)
271 panic("Cannot open /proc/interrupts!\n");
273 cpus = get_number_cpus();
274 buff_len = cpus * 128;
275 buff = xmalloc(buff_len);
276 retry:
277 fseek(fp, 0, SEEK_SET);
278 memset(buff, 0, buff_len);
280 while (fgets(buff, buff_len, fp) != NULL) {
281 buff[buff_len - 1] = 0;
282 ptr = buff;
284 if (strstr(buff, ifname) == NULL)
285 continue;
287 /* XXX: remove this one here */
288 stats->irq_nr = strtol(ptr, &ptr, 10);
289 bug_on(stats->irq_nr == 0);
291 if (ptr)
292 ptr++;
293 for (i = 0; i < cpus && ptr; ++i) {
294 if (seen)
295 stats->irqs[i] += strtol(ptr, &ptr, 10);
296 else
297 stats->irqs[i] = strtol(ptr, &ptr, 10);
298 if (i == cpus - 1) {
299 ret = 0;
300 seen = true;
304 memset(buff, 0, buff_len);
307 if (ret == -EINVAL && try == 0) {
308 memset(&drvinf, 0, sizeof(drvinf));
309 if (ethtool_drvinf(ifname, &drvinf) < 0)
310 goto done;
312 ifname = drvinf.driver;
313 try++;
315 goto retry;
317 done:
318 xfree(buff);
319 fclose(fp);
320 return ret;
323 static int stats_proc_softirqs(struct ifstat *stats)
325 int i, cpus;
326 char *ptr, *buff;
327 size_t buff_len;
328 FILE *fp;
329 enum {
330 softirqs_net_rx,
331 softirqs_net_tx,
332 } net_type;
334 fp = fopen("/proc/softirqs", "r");
335 if (!fp)
336 panic("Cannot open /proc/softirqs!\n");
338 cpus = get_number_cpus();
339 buff_len = cpus * 128;
340 buff = xmalloc(buff_len);
342 memset(buff, 0, buff_len);
344 while (fgets(buff, buff_len, fp) != NULL) {
345 buff[buff_len - 1] = 0;
347 if ((ptr = strstr(buff, "NET_TX:")))
348 net_type = softirqs_net_tx;
349 else if ((ptr = strstr(buff, "NET_RX:")))
350 net_type = softirqs_net_rx;
351 else
352 continue;
354 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
355 switch (net_type) {
356 case softirqs_net_tx:
357 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
358 break;
359 case softirqs_net_rx:
360 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
361 break;
365 memset(buff, 0, buff_len);
368 xfree(buff);
369 fclose(fp);
370 return 0;
373 static int stats_proc_memory(struct ifstat *stats)
375 char *ptr, buff[256];
376 FILE *fp;
378 fp = fopen("/proc/meminfo", "r");
379 if (!fp)
380 panic("Cannot open /proc/meminfo!\n");
382 memset(buff, 0, sizeof(buff));
384 while (fgets(buff, sizeof(buff), fp) != NULL) {
385 buff[sizeof(buff) - 1] = 0;
387 if ((ptr = strstr(buff, "MemTotal:"))) {
388 ptr += strlen("MemTotal:");
389 stats->mem_total = strtoul(ptr, &ptr, 10);
390 } else if ((ptr = strstr(buff, "MemFree:"))) {
391 ptr += strlen("MemFree:");
392 stats->mem_free = strtoul(ptr, &ptr, 10);
393 } else if ((ptr = strstr(buff, "Active:"))) {
394 ptr += strlen("Active:");
395 stats->mem_active = strtoul(ptr, &ptr, 10);
396 } else if ((ptr = strstr(buff, "Inactive:"))) {
397 ptr += strlen("Inactive:");
398 stats->mem_inactive = strtoul(ptr, &ptr, 10);
399 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
400 ptr += strlen("SwapTotal:");
401 stats->swap_total = strtoul(ptr, &ptr, 10);
402 } else if ((ptr = strstr(buff, "SwapFree:"))) {
403 ptr += strlen("SwapFree:");
404 stats->swap_free = strtoul(ptr, &ptr, 10);
405 } else if ((ptr = strstr(buff, "SwapCached:"))) {
406 ptr += strlen("SwapCached:");
407 stats->swap_cached = strtoul(ptr, &ptr, 10);
410 memset(buff, 0, sizeof(buff));
413 fclose(fp);
414 return 0;
417 static int stats_proc_system(struct ifstat *stats)
419 int cpu, cpus;
420 char *ptr, buff[256];
421 FILE *fp;
423 fp = fopen("/proc/stat", "r");
424 if (!fp)
425 panic("Cannot open /proc/stat!\n");
427 cpus = get_number_cpus();
429 memset(buff, 0, sizeof(buff));
431 while (fgets(buff, sizeof(buff), fp) != NULL) {
432 buff[sizeof(buff) - 1] = 0;
434 if ((ptr = strstr(buff, "cpu"))) {
435 ptr += strlen("cpu");
436 if (isblank(*ptr))
437 goto next;
439 cpu = strtol(ptr, &ptr, 10);
440 bug_on(cpu > cpus);
442 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
443 &stats->cpu_user[cpu],
444 &stats->cpu_nice[cpu],
445 &stats->cpu_sys[cpu],
446 &stats->cpu_idle[cpu],
447 &stats->cpu_iow[cpu]) != 5)
448 goto next;
449 } else if ((ptr = strstr(buff, "ctxt"))) {
450 ptr += strlen("ctxt");
451 stats->cswitch = strtoul(ptr, &ptr, 10);
452 } else if ((ptr = strstr(buff, "procs_running"))) {
453 ptr += strlen("procs_running");
454 stats->procs_run = strtoul(ptr, &ptr, 10);
455 } else if ((ptr = strstr(buff, "procs_blocked"))) {
456 ptr += strlen("procs_blocked");
457 stats->procs_iow = strtoul(ptr, &ptr, 10);
459 next:
460 memset(buff, 0, sizeof(buff));
463 fclose(fp);
464 return 0;
467 static int stats_proc_procs(struct ifstat *stats)
469 DIR *dir;
470 struct dirent *e;
472 dir = opendir("/proc");
473 if (!dir)
474 panic("Cannot open /proc\n");
476 stats->procs_total = 0;
478 while ((e = readdir(dir)) != NULL) {
479 const char *name = e->d_name;
480 char *end;
481 unsigned int pid = strtoul(name, &end, 10);
483 /* not a number */
484 if (pid == 0 && end == name)
485 continue;
487 stats->procs_total++;
490 closedir(dir);
492 return 0;
495 static int adjust_dbm_level(int in_dbm, int dbm_val)
497 if (!in_dbm)
498 return dbm_val;
500 return dbm_val - 0x100;
503 static int stats_wireless(const char *ifname, struct ifstat *stats)
505 int ret;
506 struct iw_statistics ws;
508 ret = wireless_sigqual(ifname, &ws);
509 if (ret != 0) {
510 stats->wifi.bitrate = 0;
511 return -EINVAL;
514 stats->wifi.bitrate = wireless_bitrate(ifname);
516 stats->wifi.signal_level =
517 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
519 stats->wifi.link_qual = ws.qual.qual;
520 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
522 return ret;
525 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
526 #define DIFF(member) do { \
527 if (sizeof(diff->member) != sizeof(new->member) || \
528 sizeof(diff->member) != sizeof(old->member)) \
529 bug(); \
530 bug_on((new->member - old->member) > (new->member)); \
531 DIFF1(member); \
532 } while (0)
534 static void stats_diff(struct ifstat *old, struct ifstat *new,
535 struct ifstat *diff)
537 int cpus, i;
539 DIFF(rx_bytes);
540 DIFF(rx_packets);
541 DIFF(rx_drops);
542 DIFF(rx_errors);
543 DIFF(rx_fifo);
544 DIFF(rx_frame);
545 DIFF(rx_multi);
547 DIFF(tx_bytes);
548 DIFF(tx_packets);
549 DIFF(tx_drops);
550 DIFF(tx_errors);
551 DIFF(tx_fifo);
552 DIFF(tx_colls);
553 DIFF(tx_carrier);
555 DIFF1(wifi.signal_level);
556 DIFF1(wifi.link_qual);
558 DIFF1(cswitch);
560 cpus = get_number_cpus();
562 for (i = 0; i < cpus; ++i) {
563 DIFF(irqs[i]);
564 DIFF(irqs_srx[i]);
565 DIFF(irqs_stx[i]);
567 DIFF1(cpu_user[i]);
568 DIFF1(cpu_nice[i]);
569 DIFF1(cpu_sys[i]);
570 DIFF1(cpu_idle[i]);
571 DIFF1(cpu_iow[i]);
575 static void stats_fetch(const char *ifname, struct ifstat *stats)
577 if (stats_proc_net_dev(ifname, stats) < 0)
578 panic("Cannot fetch device stats!\n");
579 if (stats_proc_softirqs(stats) < 0)
580 panic("Cannot fetch software interrupts!\n");
581 if (stats_proc_memory(stats) < 0)
582 panic("Cannot fetch memory stats!\n");
583 if (stats_proc_system(stats) < 0)
584 panic("Cannot fetch system stats!\n");
585 if (stats_proc_procs(stats) < 0)
586 panic("Cannot fetch process stats!\n");
588 stats_proc_interrupts((char *) ifname, stats);
590 stats_wireless(ifname, stats);
593 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
595 int cpus = get_number_cpus();
597 stats_zero(&stats_old, cpus);
598 stats_zero(&stats_new, cpus);
599 stats_zero(&stats_delta, cpus);
601 stats_fetch(ifname, &stats_old);
602 usleep(ms_interval * 1000);
603 stats_fetch(ifname, &stats_new);
605 stats_diff(&stats_old, &stats_new, &stats_delta);
608 static int cmp_hits(const void *p1, const void *p2)
610 const struct cpu_hit *h1 = p1, *h2 = p2;
613 * We want the hits sorted in descending order, thus reverse the return
614 * values.
616 if (h1->hit == h2->hit)
617 return 0;
618 else if (h1->hit < h2->hit)
619 return 1;
620 else
621 return -1;
624 static int cmp_irqs_rel(const void *p1, const void *p2)
626 const struct cpu_hit *h1 = p1, *h2 = p2;
629 * We want the hits sorted in descending order, thus reverse the return
630 * values.
632 if (h1->irqs_rel == h2->irqs_rel)
633 return 0;
634 else if (h1->irqs_rel < h2->irqs_rel)
635 return 1;
636 else
637 return -1;
640 static int cmp_irqs_abs(const void *p1, const void *p2)
642 const struct cpu_hit *h1 = p1, *h2 = p2;
645 * We want the hits sorted in descending order, thus reverse the return
646 * values.
648 if (h1->irqs_abs == h2->irqs_abs)
649 return 0;
650 else if (h1->irqs_abs < h2->irqs_abs)
651 return 1;
652 else
653 return -1;
656 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
657 int cpus)
659 int i;
661 memset(&stats_avg, 0, sizeof(stats_avg));
663 for (i = 0; i < cpus; ++i) {
664 cpu_hits[i].idx = i;
665 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
666 cpu_hits[i].irqs_rel = rel->irqs[i];
667 cpu_hits[i].irqs_abs = abs->irqs[i];
669 stats_avg.cpu_user += rel->cpu_user[i];
670 stats_avg.cpu_sys += rel->cpu_sys[i];
671 stats_avg.cpu_nice += rel->cpu_nice[i];
672 stats_avg.cpu_idle += rel->cpu_idle[i];
673 stats_avg.cpu_iow += rel->cpu_iow[i];
675 stats_avg.irqs_abs += abs->irqs[i];
676 stats_avg.irqs_rel += rel->irqs[i];
677 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
678 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
681 stats_avg.cpu_user /= cpus;
682 stats_avg.cpu_sys /= cpus;
683 stats_avg.cpu_nice /= cpus;
684 stats_avg.cpu_idle /= cpus;
685 stats_avg.cpu_iow /= cpus;
686 stats_avg.irqs_abs /= cpus;
687 stats_avg.irqs_rel /= cpus;
688 stats_avg.irqs_srx_rel /= cpus;
689 stats_avg.irqs_stx_rel /= cpus;
692 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
693 uint64_t ms_interval, unsigned int top_cpus)
695 size_t len = 0;
696 char buff[64], machine[64];
697 struct ethtool_drvinfo drvinf;
698 u32 rate = device_bitrate(ifname);
699 int link = ethtool_link(ifname);
700 unsigned int cpus = get_number_cpus();
702 memset(&drvinf, 0, sizeof(drvinf));
703 ethtool_drvinf(ifname, &drvinf);
705 memset(buff, 0, sizeof(buff));
706 memset(machine, 0, sizeof(machine));
708 if (rate)
709 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
710 if (link >= 0)
711 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
712 link == 0 ? "no" : "yes");
714 if (!strstr(uts.release, uts.machine))
715 slprintf(machine, sizeof(machine), " %s,", uts.machine);
717 mvwprintw(screen, (*voff)++, 2,
718 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
719 " ", uts.release, machine,
720 ifname, drvinf.driver, buff, ms_interval, top_cpus,
721 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
724 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
725 int *voff)
727 attron(A_REVERSE);
729 mvwprintw(screen, (*voff)++, 0,
730 " rx: %16.3llf MiB/t "
731 "%10llu pkts/t "
732 "%10llu drops/t "
733 "%10llu errors/t ",
734 ((long double) rel->rx_bytes) / (1LLU << 20),
735 rel->rx_packets, rel->rx_drops, rel->rx_errors);
737 mvwprintw(screen, (*voff)++, 0,
738 " tx: %16.3llf MiB/t "
739 "%10llu pkts/t "
740 "%10llu drops/t "
741 "%10llu errors/t ",
742 ((long double) rel->tx_bytes) / (1LLU << 20),
743 rel->tx_packets, rel->tx_drops, rel->tx_errors);
745 attroff(A_REVERSE);
748 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
749 int *voff)
751 mvwprintw(screen, (*voff)++, 2,
752 "rx: %16.3llf MiB "
753 "%10llu pkts "
754 "%10llu drops "
755 "%10llu errors",
756 ((long double) abs->rx_bytes) / (1LLU << 20),
757 abs->rx_packets, abs->rx_drops, abs->rx_errors);
759 mvwprintw(screen, (*voff)++, 2,
760 "tx: %16.3llf MiB "
761 "%10llu pkts "
762 "%10llu drops "
763 "%10llu errors",
764 ((long double) abs->tx_bytes) / (1LLU << 20),
765 abs->tx_packets, abs->tx_drops, abs->tx_errors);
768 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
769 const struct ifstat *abs, int *voff)
771 mvwprintw(screen, (*voff)++, 2,
772 "sys: %14u cs/t "
773 "%11u procs "
774 "%11u running "
775 "%10u iowait",
776 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
779 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
781 mvwprintw(screen, (*voff)++, 2,
782 "mem: %13uM total "
783 "%9uM used "
784 "%11uM active "
785 "%10uM inactive",
786 abs->mem_total / 1024,
787 (abs->mem_total - abs->mem_free) / 1024,
788 abs->mem_active / 1024,
789 abs->mem_inactive / 1024);
791 mvwprintw(screen, (*voff)++, 2,
792 "swap: %12uM total "
793 "%9uM used "
794 "%11uM cached",
795 abs->swap_total / 1024,
796 (abs->swap_total - abs->swap_free) / 1024,
797 abs->swap_cached / 1024);
800 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
801 int *voff, unsigned int idx, char *tag)
803 int max_padd = padding_from_num(get_number_cpus());
804 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
805 rel->cpu_idle[idx] + rel->cpu_iow[idx];
807 mvwprintw(screen, (*voff)++, 2,
808 "cpu%*d %s: %11.1lf%% usr/t "
809 "%9.1lf%% sys/t "
810 "%10.1lf%% idl/t "
811 "%11.1lf%% iow/t",
812 max_padd, idx, tag,
813 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
814 100.0 * rel->cpu_sys[idx] / all,
815 100.0 * rel->cpu_idle[idx] / all,
816 100.0 * rel->cpu_iow[idx] / all);
819 #define MEDIAN_EVEN(member) do { \
820 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
821 } while (0)
823 #define MEDIAN_ODD(member) do { \
824 m_##member = rel->member[i]; \
825 } while (0)
827 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
828 const struct avg_stat *avg, int top_cpus,
829 int *voff)
831 int i;
832 int cpus = get_number_cpus();
833 int max_padd = padding_from_num(cpus);
834 uint64_t all;
836 if (top_cpus == 0)
837 return;
839 /* Display top hitter */
840 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
842 /* Make sure we don't display the min. hitter twice */
843 if (top_cpus == cpus)
844 top_cpus--;
846 for (i = 1; i < top_cpus; ++i)
847 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "|");
849 /* Display minimum hitter */
850 if (cpus != 1)
851 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
853 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
854 mvwprintw(screen, (*voff)++, 2,
855 "avg:%*s%14.1lf%% "
856 "%9.1lf%% "
857 "%10.1lf%% "
858 "%11.1lf%%", max_padd, "",
859 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
860 100.0 * avg->cpu_sys / all,
861 100.0 * avg->cpu_idle /all,
862 100.0 * avg->cpu_iow / all);
864 if (show_median) {
865 long double m_cpu_user, m_cpu_nice, m_cpu_sys, m_cpu_idle, m_cpu_iow;
866 long double m_all;
868 i = cpu_hits[cpus / 2].idx;
869 if (cpus % 2 == 0) {
870 /* take the mean of the 2 middle entries */
871 int j = cpu_hits[(cpus / 2) - 1].idx;
873 MEDIAN_EVEN(cpu_user);
874 MEDIAN_EVEN(cpu_nice);
875 MEDIAN_EVEN(cpu_sys);
876 MEDIAN_EVEN(cpu_idle);
877 MEDIAN_EVEN(cpu_iow);
878 } else {
879 /* take the middle entry as is */
880 MEDIAN_ODD(cpu_user);
881 MEDIAN_ODD(cpu_nice);
882 MEDIAN_ODD(cpu_sys);
883 MEDIAN_ODD(cpu_idle);
884 MEDIAN_ODD(cpu_iow);
887 m_all = m_cpu_user + m_cpu_sys + m_cpu_nice + m_cpu_idle + m_cpu_iow;
888 mvwprintw(screen, (*voff)++, 2,
889 "med:%*s%14.1Lf%% "
890 "%9.1Lf%% "
891 "%10.1Lf%% "
892 "%11.1Lf%%", max_padd, "",
893 100.0 * (m_cpu_user + m_cpu_nice) / m_all,
894 100.0 * m_cpu_sys / m_all,
895 100.0 * m_cpu_idle /m_all,
896 100.0 * m_cpu_iow / m_all);
900 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
901 int *voff, unsigned int idx, char *tag)
903 int max_padd = padding_from_num(get_number_cpus());
905 mvwprintw(screen, (*voff)++, 2,
906 "cpu%*d %s: %12llu irqs/t "
907 "%17llu sirq rx/t "
908 "%17llu sirq tx/t",
909 max_padd, idx, tag,
910 rel->irqs[idx],
911 rel->irqs_srx[idx],
912 rel->irqs_stx[idx]);
915 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
916 const struct avg_stat *avg, int top_cpus,
917 int *voff)
919 int i;
920 int cpus = get_number_cpus();
921 int max_padd = padding_from_num(cpus);
923 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
925 if (top_cpus == cpus)
926 top_cpus--;
928 for (i = 1; i < top_cpus; ++i)
929 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
931 if (cpus != 1)
932 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
934 mvwprintw(screen, (*voff)++, 2,
935 "avg:%*s%17.1Lf "
936 "%17.1Lf "
937 "%17.1Lf", max_padd, "",
938 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
940 if (show_median) {
941 long double m_irqs, m_irqs_srx, m_irqs_stx;
943 i = cpu_hits[cpus / 2].idx;
944 if (cpus % 2 == 0) {
945 /* take the mean of the 2 middle entries */
946 int j = cpu_hits[(cpus / 2) - 1].idx;
948 MEDIAN_EVEN(irqs);
949 MEDIAN_EVEN(irqs_srx);
950 MEDIAN_EVEN(irqs_stx);
951 } else {
952 /* take the middle entry as is */
953 MEDIAN_ODD(irqs);
954 MEDIAN_ODD(irqs_srx);
955 MEDIAN_ODD(irqs_stx);
958 mvwprintw(screen, (*voff)++, 2,
959 "med:%*s%17.1Lf "
960 "%17.1Lf "
961 "%17.1Lf", max_padd, "",
962 m_irqs, m_irqs_srx, m_irqs_stx);
966 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
967 int *voff, unsigned int idx, char *tag)
969 int max_padd = padding_from_num(get_number_cpus());
971 mvwprintw(screen, (*voff)++, 2,
972 "cpu%*d %s: %12llu irqs",
973 max_padd, idx, tag, abs->irqs[idx]);
976 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
977 const struct avg_stat *avg, int top_cpus,
978 int *voff)
980 int i;
981 int cpus = get_number_cpus();
982 int max_padd = padding_from_num(cpus);
984 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
986 if (top_cpus == cpus)
987 top_cpus--;
989 for (i = 1; i < top_cpus; ++i)
990 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
992 if (cpus != 1)
993 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
995 mvwprintw(screen, (*voff)++, 2,
996 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
998 if (show_median) {
999 long double m_irqs;
1001 i = cpu_hits[cpus / 2].idx;
1002 if (cpus % 2 == 0) {
1003 /* take the mean of the 2 middle entries */
1004 int j = cpu_hits[(cpus / 2) - 1].idx;
1006 m_irqs = (abs->irqs[i] + abs->irqs[j]) / 2;
1007 } else {
1008 /* take the middle entry as is */
1009 m_irqs = abs->irqs[i];
1012 mvwprintw(screen, (*voff)++, 2,
1013 "med:%*s%17.1Lf", max_padd, "", m_irqs);
1017 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
1018 const struct ifstat *abs, int *voff)
1020 if (iswireless(abs)) {
1021 mvwprintw(screen, (*voff)++, 2,
1022 "linkqual: %7d/%d (%d/t) ",
1023 abs->wifi.link_qual,
1024 abs->wifi.link_qual_max,
1025 rel->wifi.link_qual);
1027 mvwprintw(screen, (*voff)++, 2,
1028 "signal: %8d dBm (%d dBm/t) ",
1029 abs->wifi.signal_level,
1030 rel->wifi.signal_level);
1034 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
1035 const struct ifstat *abs, const struct avg_stat *avg,
1036 int *first, uint64_t ms_interval, unsigned int top_cpus,
1037 bool need_info)
1039 unsigned int cpus, top;
1040 int voff = 1, cvoff = 2;
1041 u32 rate = device_bitrate(ifname);
1043 curs_set(0);
1045 cpus = get_number_cpus();
1046 top = min(cpus, top_cpus);
1048 stats_top(rel, abs, cpus);
1050 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
1052 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
1054 voff++;
1055 screen_net_dev_rel(screen, rel, &voff);
1057 voff++;
1058 screen_net_dev_abs(screen, abs, &voff);
1060 voff++;
1061 screen_sys(screen, rel, abs, &voff);
1063 voff++;
1064 screen_mem_swap(screen, abs, &voff);
1066 voff++;
1067 screen_percpu_states(screen, rel, avg, top, &voff);
1069 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
1071 voff++;
1072 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
1074 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
1076 voff++;
1077 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
1079 voff++;
1080 screen_wireless(screen, rel, abs, &voff);
1082 if (*first) {
1083 mvwprintw(screen, cvoff, 2, "Collecting data ...");
1084 *first = 0;
1085 } else {
1086 if (need_info)
1087 mvwprintw(screen, cvoff, 2, "(consider to increase "
1088 "your sampling interval, e.g. -t %d)",
1089 rate > SPEED_1000 ? 10000 : 1000);
1090 else
1091 mvwprintw(screen, cvoff, 2, " "
1092 " ");
1095 wrefresh(screen);
1096 refresh();
1099 static int screen_main(const char *ifname, uint64_t ms_interval,
1100 unsigned int top_cpus, bool suppress_warnings)
1102 int first = 1, key;
1103 u32 rate = device_bitrate(ifname);
1104 bool need_info = false;
1106 stats_screen = screen_init(true);
1108 if (((rate > SPEED_1000 && ms_interval <= 1000) ||
1109 (rate = SPEED_1000 && ms_interval < 1000)) &&
1110 !suppress_warnings)
1111 need_info = true;
1113 while (!sigint) {
1114 key = getch();
1115 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1116 break;
1118 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1119 &first, ms_interval, top_cpus, need_info);
1121 stats_sample_generic(ifname, ms_interval);
1124 screen_end();
1126 return 0;
1129 static void term_csv(const struct ifstat *rel, const struct ifstat *abs)
1131 int cpus, i;
1133 printf("%ld ", time(NULL));
1135 printf("%llu ", rel->rx_bytes);
1136 printf("%llu ", rel->rx_packets);
1137 printf("%llu ", rel->rx_drops);
1138 printf("%llu ", rel->rx_errors);
1140 printf("%llu ", abs->rx_bytes);
1141 printf("%llu ", abs->rx_packets);
1142 printf("%llu ", abs->rx_drops);
1143 printf("%llu ", abs->rx_errors);
1145 printf("%llu ", rel->tx_bytes);
1146 printf("%llu ", rel->tx_packets);
1147 printf("%llu ", rel->tx_drops);
1148 printf("%llu ", rel->tx_errors);
1150 printf("%llu ", abs->tx_bytes);
1151 printf("%llu ", abs->tx_packets);
1152 printf("%llu ", abs->tx_drops);
1153 printf("%llu ", abs->tx_errors);
1155 printf("%u ", rel->cswitch);
1156 printf("%lu ", abs->mem_free);
1157 printf("%lu ", abs->mem_total - abs->mem_free);
1158 printf("%lu ", abs->mem_total);
1159 printf("%lu ", abs->swap_free);
1160 printf("%lu ", abs->swap_total - abs->swap_free);
1161 printf("%lu ", abs->swap_total);
1162 printf("%u ", abs->procs_total);
1163 printf("%u ", abs->procs_run);
1164 printf("%u ", abs->procs_iow);
1166 cpus = get_number_cpus();
1168 for (i = 0; i < cpus; ++i) {
1169 printf("%lu ", rel->cpu_user[i]);
1170 printf("%lu ", rel->cpu_nice[i]);
1171 printf("%lu ", rel->cpu_sys[i]);
1172 printf("%lu ", rel->cpu_idle[i]);
1173 printf("%lu ", rel->cpu_iow[i]);
1175 printf("%llu ", rel->irqs[i]);
1176 printf("%llu ", abs->irqs[i]);
1178 printf("%llu ", rel->irqs_srx[i]);
1179 printf("%llu ", abs->irqs_srx[i]);
1181 printf("%llu ", rel->irqs_stx[i]);
1182 printf("%llu ", abs->irqs_stx[i]);
1185 if (iswireless(abs)) {
1186 printf("%u ", rel->wifi.link_qual);
1187 printf("%u ", abs->wifi.link_qual);
1188 printf("%u ", abs->wifi.link_qual_max);
1190 printf("%d ", rel->wifi.signal_level);
1191 printf("%d ", abs->wifi.signal_level);
1194 puts("");
1195 fflush(stdout);
1198 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1199 uint64_t ms_interval)
1201 int cpus, i, j = 1;
1203 printf("# gnuplot dump (#col:description)\n");
1204 printf("# networking interface: %s\n", ifname);
1205 printf("# sampling interval (t): %lu ms\n", ms_interval);
1206 printf("# %d:unixtime ", j++);
1208 printf("%d:rx-bytes-per-t ", j++);
1209 printf("%d:rx-pkts-per-t ", j++);
1210 printf("%d:rx-drops-per-t ", j++);
1211 printf("%d:rx-errors-per-t ", j++);
1213 printf("%d:rx-bytes ", j++);
1214 printf("%d:rx-pkts ", j++);
1215 printf("%d:rx-drops ", j++);
1216 printf("%d:rx-errors ", j++);
1218 printf("%d:tx-bytes-per-t ", j++);
1219 printf("%d:tx-pkts-per-t ", j++);
1220 printf("%d:tx-drops-per-t ", j++);
1221 printf("%d:tx-errors-per-t ", j++);
1223 printf("%d:tx-bytes ", j++);
1224 printf("%d:tx-pkts ", j++);
1225 printf("%d:tx-drops ", j++);
1226 printf("%d:tx-errors ", j++);
1228 printf("%d:context-switches-per-t ", j++);
1229 printf("%d:mem-free ", j++);
1230 printf("%d:mem-used ", j++);
1231 printf("%d:mem-total ", j++);
1232 printf("%d:swap-free ", j++);
1233 printf("%d:swap-used ", j++);
1234 printf("%d:swap-total ", j++);
1235 printf("%d:procs-total ", j++);
1236 printf("%d:procs-in-run ", j++);
1237 printf("%d:procs-in-iow ", j++);
1239 cpus = get_number_cpus();
1241 for (i = 0; i < cpus; ++i) {
1242 printf("%d:cpu%i-usr-per-t ", j++, i);
1243 printf("%d:cpu%i-nice-per-t ", j++, i);
1244 printf("%d:cpu%i-sys-per-t ", j++, i);
1245 printf("%d:cpu%i-idle-per-t ", j++, i);
1246 printf("%d:cpu%i-iow-per-t ", j++, i);
1248 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1249 printf("%d:cpu%i-net-irqs ", j++, i);
1251 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1252 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1253 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1254 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1257 if (iswireless(abs)) {
1258 printf("%d:wifi-link-qual-per-t ", j++);
1259 printf("%d:wifi-link-qual ", j++);
1260 printf("%d:wifi-link-qual-max ", j++);
1262 printf("%d:wifi-signal-dbm-per-t ", j++);
1263 printf("%d:wifi-signal-dbm ", j++);
1266 puts("");
1267 printf("# data:\n");
1268 fflush(stdout);
1271 static int term_main(const char *ifname, uint64_t ms_interval,
1272 unsigned int top_cpus __maybe_unused,
1273 bool suppress_warnings __maybe_unused)
1275 int first = 1;
1277 do {
1278 stats_sample_generic(ifname, ms_interval);
1280 if (first) {
1281 first = 0;
1282 term_csv_header(ifname, &stats_new, ms_interval);
1285 term_csv(&stats_delta, &stats_new);
1286 } while (stats_loop && !sigint);
1288 return 0;
1291 int main(int argc, char **argv)
1293 short ifflags = 0;
1294 int c, opt_index, ret, promisc = 0;
1295 unsigned int cpus, top_cpus = 5;
1296 uint64_t interval = 1000;
1297 char *ifname = NULL;
1298 bool suppress_warnings = false;
1299 int (*func_main)(const char *ifname, uint64_t ms_interval,
1300 unsigned int top_cpus, bool suppress_warnings);
1302 func_main = screen_main;
1304 setfsuid(getuid());
1305 setfsgid(getgid());
1307 while ((c = getopt_long(argc, argv, short_options, long_options,
1308 &opt_index)) != EOF) {
1309 switch (c) {
1310 case 'h':
1311 help();
1312 break;
1313 case 'v':
1314 version();
1315 break;
1316 case 'W':
1317 suppress_warnings = true;
1318 break;
1319 case 'd':
1320 ifname = xstrndup(optarg, IFNAMSIZ);
1321 break;
1322 case 't':
1323 interval = strtoul(optarg, NULL, 10);
1324 break;
1325 case 'n':
1326 top_cpus = strtoul(optarg, NULL, 10);
1327 if (top_cpus < 1)
1328 panic("Number of top hitter CPUs must be greater than 0");
1329 break;
1330 case 'l':
1331 stats_loop = 1;
1332 break;
1333 case 'p':
1334 promisc = 1;
1335 break;
1336 case 'm':
1337 show_median = 1;
1338 break;
1339 case 'c':
1340 func_main = term_main;
1341 break;
1342 case '?':
1343 switch (optopt) {
1344 case 'd':
1345 case 't':
1346 panic("Option -%c requires an argument!\n",
1347 optopt);
1348 default:
1349 if (isprint(optopt))
1350 printf("Unknown option character `0x%X\'!\n", optopt);
1351 die();
1353 default:
1354 break;
1358 if (argc == 1)
1359 help();
1361 if (argc == 2)
1362 ifname = xstrndup(argv[1], IFNAMSIZ);
1363 if (ifname == NULL)
1364 panic("No networking device given!\n");
1366 if (!strncmp("lo", ifname, IFNAMSIZ))
1367 panic("lo is not supported!\n");
1368 if (device_mtu(ifname) == 0)
1369 panic("This is no networking device!\n");
1371 register_signal(SIGINT, signal_handler);
1372 register_signal(SIGHUP, signal_handler);
1374 cpus = get_number_cpus();
1375 top_cpus = min(top_cpus, cpus);
1376 if (uname(&uts) < 0)
1377 panic("Cannot execute uname!\n");
1379 stats_alloc(&stats_old, cpus);
1380 stats_alloc(&stats_new, cpus);
1381 stats_alloc(&stats_delta, cpus);
1383 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1385 if (promisc)
1386 ifflags = enter_promiscuous_mode(ifname);
1387 ret = func_main(ifname, interval, top_cpus, suppress_warnings);
1388 if (promisc)
1389 leave_promiscuous_mode(ifname, ifflags);
1391 stats_release(&stats_old);
1392 stats_release(&stats_new);
1393 stats_release(&stats_delta);
1395 xfree(ifname);
1396 return ret;