ifpps: make display of line rate percentage optional
[netsniff-ng.git] / ifpps.c
blob3c9a6ea2975760927b6df625a62de2dadf1a932a
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, show_percentage = 0;
75 static WINDOW *stats_screen = NULL;
76 static struct utsname uts;
78 static const char *short_options = "d:n:t:clmpPWvh";
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 {"percentage", 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 " -P|--percentage Show percentage of theoretical line rate\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 {
165 i++;
166 } while ((n /= 10) > 0);
167 return i;
170 #define STATS_ALLOC1(member) \
171 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
173 static void stats_alloc(struct ifstat *stats, unsigned int cpus)
175 STATS_ALLOC1(irqs);
176 STATS_ALLOC1(irqs_srx);
177 STATS_ALLOC1(irqs_stx);
179 STATS_ALLOC1(cpu_user);
180 STATS_ALLOC1(cpu_sys);
181 STATS_ALLOC1(cpu_nice);
182 STATS_ALLOC1(cpu_idle);
183 STATS_ALLOC1(cpu_iow);
186 #define STATS_ZERO1(member) \
187 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
189 static void stats_zero(struct ifstat *stats, unsigned int cpus)
191 /* Only clear the non-pointer members */
192 memset(stats, 0, offsetof(struct ifstat, irqs));
194 STATS_ZERO1(irqs);
195 STATS_ZERO1(irqs_srx);
196 STATS_ZERO1(irqs_stx);
198 STATS_ZERO1(cpu_user);
199 STATS_ZERO1(cpu_sys);
200 STATS_ZERO1(cpu_nice);
201 STATS_ZERO1(cpu_idle);
202 STATS_ZERO1(cpu_iow);
205 #define STATS_RELEASE(member) \
206 do { xfree(stats->member); } while (0)
208 static void stats_release(struct ifstat *stats)
210 STATS_RELEASE(irqs);
211 STATS_RELEASE(irqs_srx);
212 STATS_RELEASE(irqs_stx);
214 STATS_RELEASE(cpu_user);
215 STATS_RELEASE(cpu_sys);
216 STATS_RELEASE(cpu_nice);
217 STATS_RELEASE(cpu_idle);
218 STATS_RELEASE(cpu_iow);
221 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
223 int ret = -EINVAL;
224 char buff[256];
225 FILE *fp;
227 fp = fopen("/proc/net/dev", "r");
228 if (!fp)
229 panic("Cannot open /proc/net/dev!\n");
231 if (fgets(buff, sizeof(buff), fp)) { ; }
232 if (fgets(buff, sizeof(buff), fp)) { ; }
234 memset(buff, 0, sizeof(buff));
236 while (fgets(buff, sizeof(buff), fp) != NULL) {
237 buff[sizeof(buff) -1] = 0;
239 if (strstr(buff, ifname) == NULL)
240 continue;
242 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
243 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
244 &stats->rx_bytes, &stats->rx_packets,
245 &stats->rx_errors, &stats->rx_drops,
246 &stats->rx_fifo, &stats->rx_frame,
247 &stats->rx_multi, &stats->tx_bytes,
248 &stats->tx_packets, &stats->tx_errors,
249 &stats->tx_drops, &stats->tx_fifo,
250 &stats->tx_colls, &stats->tx_carrier) == 14) {
251 ret = 0;
252 break;
255 memset(buff, 0, sizeof(buff));
258 fclose(fp);
259 return ret;
262 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
264 int ret = -EINVAL, try = 0;
265 unsigned int i, cpus;
266 char *ptr, *buff;
267 bool seen = false;
268 size_t buff_len;
269 struct ethtool_drvinfo drvinf;
270 FILE *fp;
272 fp = fopen("/proc/interrupts", "r");
273 if (!fp)
274 panic("Cannot open /proc/interrupts!\n");
276 cpus = get_number_cpus();
277 buff_len = cpus * 128;
278 buff = xmalloc(buff_len);
279 retry:
280 fseek(fp, 0, SEEK_SET);
281 memset(buff, 0, buff_len);
283 while (fgets(buff, buff_len, fp) != NULL) {
284 buff[buff_len - 1] = 0;
286 if (strstr(buff, ifname) == NULL)
287 continue;
289 ptr = strchr(buff, ':');
290 if (!ptr)
291 continue;
292 ptr++;
294 for (i = 0; i < cpus && ptr; ++i) {
295 if (seen)
296 stats->irqs[i] += strtol(ptr, &ptr, 10);
297 else
298 stats->irqs[i] = strtol(ptr, &ptr, 10);
299 if (i == cpus - 1) {
300 ret = 0;
301 seen = true;
305 memset(buff, 0, buff_len);
308 if (ret == -EINVAL && try == 0) {
309 memset(&drvinf, 0, sizeof(drvinf));
310 if (ethtool_drvinf(ifname, &drvinf) < 0)
311 goto done;
313 ifname = drvinf.driver;
314 try++;
316 goto retry;
318 done:
319 xfree(buff);
320 fclose(fp);
321 return ret;
324 static int stats_proc_softirqs(struct ifstat *stats)
326 unsigned int i, cpus;
327 char *ptr, *buff;
328 size_t buff_len;
329 FILE *fp;
330 enum {
331 softirqs_net_rx,
332 softirqs_net_tx,
333 } net_type;
335 fp = fopen("/proc/softirqs", "r");
336 if (!fp)
337 panic("Cannot open /proc/softirqs!\n");
339 cpus = get_number_cpus();
340 buff_len = cpus * 128;
341 buff = xmalloc(buff_len);
343 memset(buff, 0, buff_len);
345 while (fgets(buff, buff_len, fp) != NULL) {
346 buff[buff_len - 1] = 0;
348 if ((ptr = strstr(buff, "NET_TX:")))
349 net_type = softirqs_net_tx;
350 else if ((ptr = strstr(buff, "NET_RX:")))
351 net_type = softirqs_net_rx;
352 else
353 continue;
355 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
356 switch (net_type) {
357 case softirqs_net_tx:
358 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
359 break;
360 case softirqs_net_rx:
361 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
362 break;
366 memset(buff, 0, buff_len);
369 xfree(buff);
370 fclose(fp);
371 return 0;
374 static int stats_proc_memory(struct ifstat *stats)
376 char *ptr, buff[256];
377 FILE *fp;
379 fp = fopen("/proc/meminfo", "r");
380 if (!fp)
381 panic("Cannot open /proc/meminfo!\n");
383 memset(buff, 0, sizeof(buff));
385 while (fgets(buff, sizeof(buff), fp) != NULL) {
386 buff[sizeof(buff) - 1] = 0;
388 if ((ptr = strstr(buff, "MemTotal:"))) {
389 ptr += strlen("MemTotal:");
390 stats->mem_total = strtoul(ptr, &ptr, 10);
391 } else if ((ptr = strstr(buff, "MemFree:"))) {
392 ptr += strlen("MemFree:");
393 stats->mem_free = strtoul(ptr, &ptr, 10);
394 } else if ((ptr = strstr(buff, "Active:"))) {
395 ptr += strlen("Active:");
396 stats->mem_active = strtoul(ptr, &ptr, 10);
397 } else if ((ptr = strstr(buff, "Inactive:"))) {
398 ptr += strlen("Inactive:");
399 stats->mem_inactive = strtoul(ptr, &ptr, 10);
400 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
401 ptr += strlen("SwapTotal:");
402 stats->swap_total = strtoul(ptr, &ptr, 10);
403 } else if ((ptr = strstr(buff, "SwapFree:"))) {
404 ptr += strlen("SwapFree:");
405 stats->swap_free = strtoul(ptr, &ptr, 10);
406 } else if ((ptr = strstr(buff, "SwapCached:"))) {
407 ptr += strlen("SwapCached:");
408 stats->swap_cached = strtoul(ptr, &ptr, 10);
411 memset(buff, 0, sizeof(buff));
414 fclose(fp);
415 return 0;
418 static int stats_proc_system(struct ifstat *stats)
420 unsigned int cpu, cpus;
421 char *ptr, buff[256];
422 FILE *fp;
424 fp = fopen("/proc/stat", "r");
425 if (!fp)
426 panic("Cannot open /proc/stat!\n");
428 cpus = get_number_cpus();
430 memset(buff, 0, sizeof(buff));
432 while (fgets(buff, sizeof(buff), fp) != NULL) {
433 buff[sizeof(buff) - 1] = 0;
435 if ((ptr = strstr(buff, "cpu"))) {
436 ptr += strlen("cpu");
437 if (isblank(*ptr))
438 goto next;
440 cpu = strtol(ptr, &ptr, 10);
441 bug_on(cpu > cpus);
443 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
444 &stats->cpu_user[cpu],
445 &stats->cpu_nice[cpu],
446 &stats->cpu_sys[cpu],
447 &stats->cpu_idle[cpu],
448 &stats->cpu_iow[cpu]) != 5)
449 goto next;
450 } else if ((ptr = strstr(buff, "ctxt"))) {
451 ptr += strlen("ctxt");
452 stats->cswitch = strtoul(ptr, &ptr, 10);
453 } else if ((ptr = strstr(buff, "procs_running"))) {
454 ptr += strlen("procs_running");
455 stats->procs_run = strtoul(ptr, &ptr, 10);
456 } else if ((ptr = strstr(buff, "procs_blocked"))) {
457 ptr += strlen("procs_blocked");
458 stats->procs_iow = strtoul(ptr, &ptr, 10);
460 next:
461 memset(buff, 0, sizeof(buff));
464 fclose(fp);
465 return 0;
468 static int stats_proc_procs(struct ifstat *stats)
470 DIR *dir;
471 struct dirent *e;
473 dir = opendir("/proc");
474 if (!dir)
475 panic("Cannot open /proc\n");
477 stats->procs_total = 0;
479 while ((e = readdir(dir)) != NULL) {
480 const char *name = e->d_name;
481 char *end;
482 unsigned int pid = strtoul(name, &end, 10);
484 /* not a number */
485 if (pid == 0 && end == name)
486 continue;
488 stats->procs_total++;
491 closedir(dir);
493 return 0;
496 static int adjust_dbm_level(int in_dbm, int dbm_val)
498 if (!in_dbm)
499 return dbm_val;
501 return dbm_val - 0x100;
504 static int stats_wireless(const char *ifname, struct ifstat *stats)
506 int ret;
507 struct iw_statistics ws;
509 ret = wireless_sigqual(ifname, &ws);
510 if (ret != 0) {
511 stats->wifi.bitrate = 0;
512 return -EINVAL;
515 stats->wifi.bitrate = wireless_bitrate(ifname);
517 stats->wifi.signal_level =
518 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
520 stats->wifi.link_qual = ws.qual.qual;
521 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
523 return ret;
526 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
527 #define DIFF(member) do { \
528 if (sizeof(diff->member) != sizeof(new->member) || \
529 sizeof(diff->member) != sizeof(old->member)) \
530 bug(); \
531 if ((new->member - old->member) > (new->member)) { \
532 diff->member = 0; \
533 } else { \
534 DIFF1(member); \
536 } while (0)
538 static void stats_diff(struct ifstat *old, struct ifstat *new,
539 struct ifstat *diff)
541 unsigned int cpus, i;
543 DIFF(rx_bytes);
544 DIFF(rx_packets);
545 DIFF(rx_drops);
546 DIFF(rx_errors);
547 DIFF(rx_fifo);
548 DIFF(rx_frame);
549 DIFF(rx_multi);
551 DIFF(tx_bytes);
552 DIFF(tx_packets);
553 DIFF(tx_drops);
554 DIFF(tx_errors);
555 DIFF(tx_fifo);
556 DIFF(tx_colls);
557 DIFF(tx_carrier);
559 DIFF1(wifi.signal_level);
560 DIFF1(wifi.link_qual);
562 DIFF1(cswitch);
564 cpus = get_number_cpus();
566 for (i = 0; i < cpus; ++i) {
567 DIFF(irqs[i]);
568 DIFF(irqs_srx[i]);
569 DIFF(irqs_stx[i]);
571 DIFF1(cpu_user[i]);
572 DIFF1(cpu_nice[i]);
573 DIFF1(cpu_sys[i]);
574 DIFF1(cpu_idle[i]);
575 DIFF1(cpu_iow[i]);
579 static void stats_fetch(const char *ifname, struct ifstat *stats)
581 if (stats_proc_net_dev(ifname, stats) < 0)
582 panic("Cannot fetch device stats!\n");
583 if (stats_proc_softirqs(stats) < 0)
584 panic("Cannot fetch software interrupts!\n");
585 if (stats_proc_memory(stats) < 0)
586 panic("Cannot fetch memory stats!\n");
587 if (stats_proc_system(stats) < 0)
588 panic("Cannot fetch system stats!\n");
589 if (stats_proc_procs(stats) < 0)
590 panic("Cannot fetch process stats!\n");
592 stats_proc_interrupts((char *) ifname, stats);
594 stats_wireless(ifname, stats);
597 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
599 unsigned int cpus = get_number_cpus();
601 stats_zero(&stats_old, cpus);
602 stats_zero(&stats_new, cpus);
603 stats_zero(&stats_delta, cpus);
605 stats_fetch(ifname, &stats_old);
606 usleep(ms_interval * 1000);
607 stats_fetch(ifname, &stats_new);
609 stats_diff(&stats_old, &stats_new, &stats_delta);
612 static int cmp_hits(const void *p1, const void *p2)
614 const struct cpu_hit *h1 = p1, *h2 = p2;
617 * We want the hits sorted in descending order, thus reverse the return
618 * values.
620 if (h1->hit == h2->hit)
621 return 0;
622 else if (h1->hit < h2->hit)
623 return 1;
624 else
625 return -1;
628 static int cmp_irqs_rel(const void *p1, const void *p2)
630 const struct cpu_hit *h1 = p1, *h2 = p2;
633 * We want the hits sorted in descending order, thus reverse the return
634 * values.
636 if (h1->irqs_rel == h2->irqs_rel)
637 return 0;
638 else if (h1->irqs_rel < h2->irqs_rel)
639 return 1;
640 else
641 return -1;
644 static int cmp_irqs_abs(const void *p1, const void *p2)
646 const struct cpu_hit *h1 = p1, *h2 = p2;
649 * We want the hits sorted in descending order, thus reverse the return
650 * values.
652 if (h1->irqs_abs == h2->irqs_abs)
653 return 0;
654 else if (h1->irqs_abs < h2->irqs_abs)
655 return 1;
656 else
657 return -1;
660 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
661 unsigned int cpus)
663 unsigned int i;
665 memset(&stats_avg, 0, sizeof(stats_avg));
667 for (i = 0; i < cpus; ++i) {
668 cpu_hits[i].idx = i;
669 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
670 cpu_hits[i].irqs_rel = rel->irqs[i];
671 cpu_hits[i].irqs_abs = abs->irqs[i];
673 stats_avg.cpu_user += rel->cpu_user[i];
674 stats_avg.cpu_sys += rel->cpu_sys[i];
675 stats_avg.cpu_nice += rel->cpu_nice[i];
676 stats_avg.cpu_idle += rel->cpu_idle[i];
677 stats_avg.cpu_iow += rel->cpu_iow[i];
679 stats_avg.irqs_abs += abs->irqs[i];
680 stats_avg.irqs_rel += rel->irqs[i];
681 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
682 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
685 stats_avg.cpu_user /= cpus;
686 stats_avg.cpu_sys /= cpus;
687 stats_avg.cpu_nice /= cpus;
688 stats_avg.cpu_idle /= cpus;
689 stats_avg.cpu_iow /= cpus;
690 stats_avg.irqs_abs /= cpus;
691 stats_avg.irqs_rel /= cpus;
692 stats_avg.irqs_srx_rel /= cpus;
693 stats_avg.irqs_stx_rel /= cpus;
696 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
697 u32 rate, uint64_t ms_interval, unsigned int top_cpus)
699 size_t len = 0;
700 char buff[64], machine[64];
701 struct ethtool_drvinfo drvinf;
702 int link = ethtool_link(ifname);
703 unsigned int cpus = get_number_cpus();
705 memset(&drvinf, 0, sizeof(drvinf));
706 ethtool_drvinf(ifname, &drvinf);
708 memset(buff, 0, sizeof(buff));
709 memset(machine, 0, sizeof(machine));
711 if (rate)
712 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
713 if (link >= 0)
714 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
715 link == 0 ? "no" : "yes");
717 if (!strstr(uts.release, uts.machine))
718 slprintf(machine, sizeof(machine), " %s,", uts.machine);
720 mvwprintw(screen, (*voff)++, 2,
721 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
722 " ", uts.release, machine,
723 ifname, drvinf.driver, buff, ms_interval, top_cpus,
724 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
727 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
728 int *voff)
730 attron(A_REVERSE);
732 mvwprintw(screen, (*voff)++, 0,
733 " rx: %16.3llf MiB/t "
734 "%10llu pkts/t "
735 "%10llu drops/t "
736 "%10llu errors/t ",
737 ((long double) rel->rx_bytes) / (1LLU << 20),
738 rel->rx_packets, rel->rx_drops, rel->rx_errors);
740 mvwprintw(screen, (*voff)++, 0,
741 " tx: %16.3llf MiB/t "
742 "%10llu pkts/t "
743 "%10llu drops/t "
744 "%10llu errors/t ",
745 ((long double) rel->tx_bytes) / (1LLU << 20),
746 rel->tx_packets, rel->tx_drops, rel->tx_errors);
748 attroff(A_REVERSE);
751 static void screen_net_dev_percentage(WINDOW *screen, const struct ifstat *rel,
752 int *voff, u32 rate)
754 mvwprintw(screen, (*voff)++, 0,
755 " rx: %15.2llf%% of line rate "
756 " ",
757 rate ? ((((long double) rel->rx_bytes) / (1LLU << 20)) / rate) * 100.0 : 0.0);
759 mvwprintw(screen, (*voff)++, 0,
760 " tx: %15.2llf%% of line rate "
761 " ",
762 rate ? ((((long double) rel->tx_bytes) / (1LLU << 20)) / rate) * 100.0 : 0.0);
765 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
766 int *voff)
768 mvwprintw(screen, (*voff)++, 2,
769 "rx: %16.3llf MiB "
770 "%10llu pkts "
771 "%10llu drops "
772 "%10llu errors",
773 ((long double) abs->rx_bytes) / (1LLU << 20),
774 abs->rx_packets, abs->rx_drops, abs->rx_errors);
776 mvwprintw(screen, (*voff)++, 2,
777 "tx: %16.3llf MiB "
778 "%10llu pkts "
779 "%10llu drops "
780 "%10llu errors",
781 ((long double) abs->tx_bytes) / (1LLU << 20),
782 abs->tx_packets, abs->tx_drops, abs->tx_errors);
785 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
786 const struct ifstat *abs, int *voff)
788 mvwprintw(screen, (*voff)++, 2,
789 "sys: %14u cs/t "
790 "%11u procs "
791 "%11u running "
792 "%10u iowait",
793 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
796 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
798 mvwprintw(screen, (*voff)++, 2,
799 "mem: %13uM total "
800 "%9uM used "
801 "%11uM active "
802 "%10uM inactive",
803 abs->mem_total / 1024,
804 (abs->mem_total - abs->mem_free) / 1024,
805 abs->mem_active / 1024,
806 abs->mem_inactive / 1024);
808 mvwprintw(screen, (*voff)++, 2,
809 "swap: %12uM total "
810 "%9uM used "
811 "%11uM cached",
812 abs->swap_total / 1024,
813 (abs->swap_total - abs->swap_free) / 1024,
814 abs->swap_cached / 1024);
817 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
818 int *voff, unsigned int idx, char *tag)
820 int max_padd = padding_from_num(get_number_cpus());
821 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
822 rel->cpu_idle[idx] + rel->cpu_iow[idx];
824 mvwprintw(screen, (*voff)++, 2,
825 "cpu%*d %s: %11.1lf%% usr/t "
826 "%9.1lf%% sys/t "
827 "%10.1lf%% idl/t "
828 "%11.1lf%% iow/t",
829 max_padd, idx, tag,
830 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
831 100.0 * rel->cpu_sys[idx] / all,
832 100.0 * rel->cpu_idle[idx] / all,
833 100.0 * rel->cpu_iow[idx] / all);
836 #define MEDIAN_EVEN(member) do { \
837 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
838 } while (0)
840 #define MEDIAN_ODD(member) do { \
841 m_##member = rel->member[i]; \
842 } while (0)
844 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
845 const struct avg_stat *avg,
846 unsigned int top_cpus, int *voff)
848 unsigned int i;
849 unsigned int cpus = get_number_cpus();
850 int max_padd = padding_from_num(cpus);
851 uint64_t all;
853 if (top_cpus == 0)
854 return;
856 /* Display top hitter */
857 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
859 /* Make sure we don't display the min. hitter twice */
860 if (top_cpus == cpus)
861 top_cpus--;
863 for (i = 1; i < top_cpus; ++i)
864 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "|");
866 /* Display minimum hitter */
867 if (cpus != 1)
868 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
870 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
871 mvwprintw(screen, (*voff)++, 2,
872 "avg:%*s%14.1lf%% "
873 "%9.1lf%% "
874 "%10.1lf%% "
875 "%11.1lf%%", max_padd, "",
876 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
877 100.0 * avg->cpu_sys / all,
878 100.0 * avg->cpu_idle /all,
879 100.0 * avg->cpu_iow / all);
881 if (show_median) {
882 long double m_cpu_user, m_cpu_nice, m_cpu_sys, m_cpu_idle, m_cpu_iow;
883 long double m_all;
885 i = cpu_hits[cpus / 2].idx;
886 if (cpus % 2 == 0) {
887 /* take the mean of the 2 middle entries */
888 int j = cpu_hits[(cpus / 2) - 1].idx;
890 MEDIAN_EVEN(cpu_user);
891 MEDIAN_EVEN(cpu_nice);
892 MEDIAN_EVEN(cpu_sys);
893 MEDIAN_EVEN(cpu_idle);
894 MEDIAN_EVEN(cpu_iow);
895 } else {
896 /* take the middle entry as is */
897 MEDIAN_ODD(cpu_user);
898 MEDIAN_ODD(cpu_nice);
899 MEDIAN_ODD(cpu_sys);
900 MEDIAN_ODD(cpu_idle);
901 MEDIAN_ODD(cpu_iow);
904 m_all = m_cpu_user + m_cpu_sys + m_cpu_nice + m_cpu_idle + m_cpu_iow;
905 mvwprintw(screen, (*voff)++, 2,
906 "med:%*s%14.1Lf%% "
907 "%9.1Lf%% "
908 "%10.1Lf%% "
909 "%11.1Lf%%", max_padd, "",
910 100.0 * (m_cpu_user + m_cpu_nice) / m_all,
911 100.0 * m_cpu_sys / m_all,
912 100.0 * m_cpu_idle /m_all,
913 100.0 * m_cpu_iow / m_all);
917 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
918 int *voff, unsigned int idx, char *tag)
920 int max_padd = padding_from_num(get_number_cpus());
922 mvwprintw(screen, (*voff)++, 2,
923 "cpu%*d %s: %12llu irqs/t "
924 "%17llu sirq rx/t "
925 "%17llu sirq tx/t",
926 max_padd, idx, tag,
927 rel->irqs[idx],
928 rel->irqs_srx[idx],
929 rel->irqs_stx[idx]);
932 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
933 const struct avg_stat *avg,
934 unsigned int top_cpus, int *voff)
936 unsigned int i;
937 unsigned int cpus = get_number_cpus();
938 int max_padd = padding_from_num(cpus);
940 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
942 if (top_cpus == cpus)
943 top_cpus--;
945 for (i = 1; i < top_cpus; ++i)
946 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
948 if (cpus != 1)
949 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
951 mvwprintw(screen, (*voff)++, 2,
952 "avg:%*s%17.1Lf "
953 "%17.1Lf "
954 "%17.1Lf", max_padd, "",
955 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
957 if (show_median) {
958 long double m_irqs, m_irqs_srx, m_irqs_stx;
960 i = cpu_hits[cpus / 2].idx;
961 if (cpus % 2 == 0) {
962 /* take the mean of the 2 middle entries */
963 int j = cpu_hits[(cpus / 2) - 1].idx;
965 MEDIAN_EVEN(irqs);
966 MEDIAN_EVEN(irqs_srx);
967 MEDIAN_EVEN(irqs_stx);
968 } else {
969 /* take the middle entry as is */
970 MEDIAN_ODD(irqs);
971 MEDIAN_ODD(irqs_srx);
972 MEDIAN_ODD(irqs_stx);
975 mvwprintw(screen, (*voff)++, 2,
976 "med:%*s%17.1Lf "
977 "%17.1Lf "
978 "%17.1Lf", max_padd, "",
979 m_irqs, m_irqs_srx, m_irqs_stx);
983 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
984 int *voff, unsigned int idx, char *tag)
986 int max_padd = padding_from_num(get_number_cpus());
988 mvwprintw(screen, (*voff)++, 2,
989 "cpu%*d %s: %12llu irqs",
990 max_padd, idx, tag, abs->irqs[idx]);
993 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
994 const struct avg_stat *avg,
995 unsigned int top_cpus, int *voff)
997 unsigned int i;
998 unsigned int cpus = get_number_cpus();
999 int max_padd = padding_from_num(cpus);
1001 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
1003 if (top_cpus == cpus)
1004 top_cpus--;
1006 for (i = 1; i < top_cpus; ++i)
1007 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
1009 if (cpus != 1)
1010 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
1012 mvwprintw(screen, (*voff)++, 2,
1013 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
1015 if (show_median) {
1016 long double m_irqs;
1018 i = cpu_hits[cpus / 2].idx;
1019 if (cpus % 2 == 0) {
1020 /* take the mean of the 2 middle entries */
1021 int j = cpu_hits[(cpus / 2) - 1].idx;
1023 m_irqs = (abs->irqs[i] + abs->irqs[j]) / 2;
1024 } else {
1025 /* take the middle entry as is */
1026 m_irqs = abs->irqs[i];
1029 mvwprintw(screen, (*voff)++, 2,
1030 "med:%*s%17.1Lf", max_padd, "", m_irqs);
1034 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
1035 const struct ifstat *abs, int *voff)
1037 if (iswireless(abs)) {
1038 mvwprintw(screen, (*voff)++, 2,
1039 "linkqual: %7d/%d (%d/t) ",
1040 abs->wifi.link_qual,
1041 abs->wifi.link_qual_max,
1042 rel->wifi.link_qual);
1044 mvwprintw(screen, (*voff)++, 2,
1045 "signal: %8d dBm (%d dBm/t) ",
1046 abs->wifi.signal_level,
1047 rel->wifi.signal_level);
1051 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
1052 const struct ifstat *abs, const struct avg_stat *avg,
1053 int *first, uint64_t ms_interval, unsigned int top_cpus,
1054 bool need_info)
1056 unsigned int cpus, top;
1057 int voff = 1, cvoff = 2;
1058 u32 rate = device_bitrate(ifname);
1060 curs_set(0);
1062 cpus = get_number_cpus();
1063 top = min(cpus, top_cpus);
1065 stats_top(rel, abs, cpus);
1067 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
1069 screen_header(screen, ifname, &voff, rate, ms_interval, top_cpus);
1071 voff++;
1072 screen_net_dev_rel(screen, rel, &voff);
1074 if (show_percentage) {
1075 voff++;
1076 screen_net_dev_percentage(screen, rel, &voff, rate);
1079 voff++;
1080 screen_net_dev_abs(screen, abs, &voff);
1082 voff++;
1083 screen_sys(screen, rel, abs, &voff);
1085 voff++;
1086 screen_mem_swap(screen, abs, &voff);
1088 voff++;
1089 screen_percpu_states(screen, rel, avg, top, &voff);
1091 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
1093 voff++;
1094 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
1096 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
1098 voff++;
1099 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
1101 voff++;
1102 screen_wireless(screen, rel, abs, &voff);
1104 if (*first) {
1105 mvwprintw(screen, cvoff, 2, "Collecting data ...");
1106 *first = 0;
1107 } else {
1108 if (need_info)
1109 mvwprintw(screen, cvoff, 2, "(consider to increase "
1110 "your sampling interval, e.g. -t %d)",
1111 rate > SPEED_1000 ? 10000 : 1000);
1112 else
1113 mvwprintw(screen, cvoff, 2, " "
1114 " ");
1117 wrefresh(screen);
1118 refresh();
1121 static int screen_main(const char *ifname, uint64_t ms_interval,
1122 unsigned int top_cpus, bool suppress_warnings)
1124 int first = 1, key;
1125 u32 rate = device_bitrate(ifname);
1126 bool need_info = false;
1128 stats_screen = screen_init(true);
1130 if (((rate > SPEED_1000 && ms_interval <= 1000) ||
1131 (rate = SPEED_1000 && ms_interval < 1000)) &&
1132 !suppress_warnings)
1133 need_info = true;
1135 while (!sigint) {
1136 key = getch();
1137 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1138 break;
1140 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1141 &first, ms_interval, top_cpus, need_info);
1143 stats_sample_generic(ifname, ms_interval);
1146 screen_end();
1148 return 0;
1151 static void term_csv(const struct ifstat *rel, const struct ifstat *abs)
1153 unsigned int cpus, i;
1155 printf("%ld ", time(NULL));
1157 printf("%llu ", rel->rx_bytes);
1158 printf("%llu ", rel->rx_packets);
1159 printf("%llu ", rel->rx_drops);
1160 printf("%llu ", rel->rx_errors);
1162 printf("%llu ", abs->rx_bytes);
1163 printf("%llu ", abs->rx_packets);
1164 printf("%llu ", abs->rx_drops);
1165 printf("%llu ", abs->rx_errors);
1167 printf("%llu ", rel->tx_bytes);
1168 printf("%llu ", rel->tx_packets);
1169 printf("%llu ", rel->tx_drops);
1170 printf("%llu ", rel->tx_errors);
1172 printf("%llu ", abs->tx_bytes);
1173 printf("%llu ", abs->tx_packets);
1174 printf("%llu ", abs->tx_drops);
1175 printf("%llu ", abs->tx_errors);
1177 printf("%u ", rel->cswitch);
1178 printf("%lu ", abs->mem_free);
1179 printf("%lu ", abs->mem_total - abs->mem_free);
1180 printf("%lu ", abs->mem_total);
1181 printf("%lu ", abs->swap_free);
1182 printf("%lu ", abs->swap_total - abs->swap_free);
1183 printf("%lu ", abs->swap_total);
1184 printf("%u ", abs->procs_total);
1185 printf("%u ", abs->procs_run);
1186 printf("%u ", abs->procs_iow);
1188 cpus = get_number_cpus();
1190 for (i = 0; i < cpus; ++i) {
1191 printf("%lu ", rel->cpu_user[i]);
1192 printf("%lu ", rel->cpu_nice[i]);
1193 printf("%lu ", rel->cpu_sys[i]);
1194 printf("%lu ", rel->cpu_idle[i]);
1195 printf("%lu ", rel->cpu_iow[i]);
1197 printf("%llu ", rel->irqs[i]);
1198 printf("%llu ", abs->irqs[i]);
1200 printf("%llu ", rel->irqs_srx[i]);
1201 printf("%llu ", abs->irqs_srx[i]);
1203 printf("%llu ", rel->irqs_stx[i]);
1204 printf("%llu ", abs->irqs_stx[i]);
1207 if (iswireless(abs)) {
1208 printf("%u ", rel->wifi.link_qual);
1209 printf("%u ", abs->wifi.link_qual);
1210 printf("%u ", abs->wifi.link_qual_max);
1212 printf("%d ", rel->wifi.signal_level);
1213 printf("%d ", abs->wifi.signal_level);
1216 puts("");
1217 fflush(stdout);
1220 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1221 uint64_t ms_interval)
1223 unsigned int cpus, i, j = 1;
1225 printf("# gnuplot dump (#col:description)\n");
1226 printf("# networking interface: %s\n", ifname);
1227 printf("# sampling interval (t): %lu ms\n", ms_interval);
1228 printf("# %d:unixtime ", j++);
1230 printf("%d:rx-bytes-per-t ", j++);
1231 printf("%d:rx-pkts-per-t ", j++);
1232 printf("%d:rx-drops-per-t ", j++);
1233 printf("%d:rx-errors-per-t ", j++);
1235 printf("%d:rx-bytes ", j++);
1236 printf("%d:rx-pkts ", j++);
1237 printf("%d:rx-drops ", j++);
1238 printf("%d:rx-errors ", j++);
1240 printf("%d:tx-bytes-per-t ", j++);
1241 printf("%d:tx-pkts-per-t ", j++);
1242 printf("%d:tx-drops-per-t ", j++);
1243 printf("%d:tx-errors-per-t ", j++);
1245 printf("%d:tx-bytes ", j++);
1246 printf("%d:tx-pkts ", j++);
1247 printf("%d:tx-drops ", j++);
1248 printf("%d:tx-errors ", j++);
1250 printf("%d:context-switches-per-t ", j++);
1251 printf("%d:mem-free ", j++);
1252 printf("%d:mem-used ", j++);
1253 printf("%d:mem-total ", j++);
1254 printf("%d:swap-free ", j++);
1255 printf("%d:swap-used ", j++);
1256 printf("%d:swap-total ", j++);
1257 printf("%d:procs-total ", j++);
1258 printf("%d:procs-in-run ", j++);
1259 printf("%d:procs-in-iow ", j++);
1261 cpus = get_number_cpus();
1263 for (i = 0; i < cpus; ++i) {
1264 printf("%d:cpu%i-usr-per-t ", j++, i);
1265 printf("%d:cpu%i-nice-per-t ", j++, i);
1266 printf("%d:cpu%i-sys-per-t ", j++, i);
1267 printf("%d:cpu%i-idle-per-t ", j++, i);
1268 printf("%d:cpu%i-iow-per-t ", j++, i);
1270 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1271 printf("%d:cpu%i-net-irqs ", j++, i);
1273 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1274 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1275 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1276 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1279 if (iswireless(abs)) {
1280 printf("%d:wifi-link-qual-per-t ", j++);
1281 printf("%d:wifi-link-qual ", j++);
1282 printf("%d:wifi-link-qual-max ", j++);
1284 printf("%d:wifi-signal-dbm-per-t ", j++);
1285 printf("%d:wifi-signal-dbm ", j++);
1288 puts("");
1289 printf("# data:\n");
1290 fflush(stdout);
1293 static int term_main(const char *ifname, uint64_t ms_interval,
1294 unsigned int top_cpus __maybe_unused,
1295 bool suppress_warnings __maybe_unused)
1297 int first = 1;
1299 do {
1300 stats_sample_generic(ifname, ms_interval);
1302 if (first) {
1303 first = 0;
1304 term_csv_header(ifname, &stats_new, ms_interval);
1307 term_csv(&stats_delta, &stats_new);
1308 } while (stats_loop && !sigint);
1310 return 0;
1313 int main(int argc, char **argv)
1315 short ifflags = 0;
1316 int c, opt_index, ret, promisc = 0;
1317 unsigned int cpus, top_cpus = 5;
1318 uint64_t interval = 1000;
1319 char *ifname = NULL;
1320 bool suppress_warnings = false;
1321 int (*func_main)(const char *ifname, uint64_t ms_interval,
1322 unsigned int top_cpus, bool suppress_warnings);
1324 func_main = screen_main;
1326 setfsuid(getuid());
1327 setfsgid(getgid());
1329 while ((c = getopt_long(argc, argv, short_options, long_options,
1330 &opt_index)) != EOF) {
1331 switch (c) {
1332 case 'h':
1333 help();
1334 break;
1335 case 'v':
1336 version();
1337 break;
1338 case 'W':
1339 suppress_warnings = true;
1340 break;
1341 case 'd':
1342 ifname = xstrndup(optarg, IFNAMSIZ);
1343 break;
1344 case 't':
1345 interval = strtoul(optarg, NULL, 10);
1346 break;
1347 case 'n':
1348 top_cpus = strtoul(optarg, NULL, 10);
1349 if (top_cpus < 1)
1350 panic("Number of top hitter CPUs must be greater than 0");
1351 break;
1352 case 'l':
1353 stats_loop = 1;
1354 break;
1355 case 'p':
1356 promisc = 1;
1357 break;
1358 case 'P':
1359 show_percentage = 1;
1360 break;
1361 case 'm':
1362 show_median = 1;
1363 break;
1364 case 'c':
1365 func_main = term_main;
1366 break;
1367 case '?':
1368 switch (optopt) {
1369 case 'd':
1370 case 't':
1371 panic("Option -%c requires an argument!\n",
1372 optopt);
1373 default:
1374 if (isprint(optopt))
1375 printf("Unknown option character `0x%X\'!\n", optopt);
1376 die();
1378 default:
1379 break;
1383 if (argc == 1)
1384 help();
1386 if (argc == 2)
1387 ifname = xstrndup(argv[1], IFNAMSIZ);
1388 if (ifname == NULL)
1389 panic("No networking device given!\n");
1391 if (!strncmp("lo", ifname, IFNAMSIZ))
1392 panic("lo is not supported!\n");
1393 if (device_mtu(ifname) == 0)
1394 panic("This is no networking device!\n");
1396 register_signal(SIGINT, signal_handler);
1397 register_signal(SIGHUP, signal_handler);
1399 cpus = get_number_cpus();
1400 top_cpus = min(top_cpus, cpus);
1401 if (uname(&uts) < 0)
1402 panic("Cannot execute uname!\n");
1404 stats_alloc(&stats_old, cpus);
1405 stats_alloc(&stats_new, cpus);
1406 stats_alloc(&stats_delta, cpus);
1408 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1410 if (promisc)
1411 ifflags = device_enter_promiscuous_mode(ifname);
1412 ret = func_main(ifname, interval, top_cpus, suppress_warnings);
1413 if (promisc)
1414 device_leave_promiscuous_mode(ifname, ifflags);
1416 stats_release(&stats_old);
1417 stats_release(&stats_new);
1418 stats_release(&stats_delta);
1420 xfree(ifname);
1421 return ret;