ifpps: Optionally display median values for CPU load and interrupts
[netsniff-ng.git] / ifpps.c
blob0c684e67a7f7ff6b6819268de4070291b5f98a85
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:t:n:vhclpmW";
80 static const struct option long_options[] = {
81 {"dev", required_argument, NULL, 'd'},
82 {"interval", required_argument, NULL, 't'},
83 {"num-cpus", required_argument, NULL, 'n'},
84 {"promisc", no_argument, NULL, 'p'},
85 {"csv", no_argument, NULL, 'c'},
86 {"loop", no_argument, NULL, 'l'},
87 {"median", no_argument, NULL, 'm'},
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 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
120 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
121 " in ncurses mode (default 5)\n"
122 " -p|--promisc Promiscuous mode\n"
123 " -c|--csv Output to terminal as Gnuplot-ready data\n"
124 " -l|--loop Continuous CSV output\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 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
205 int ret = -EINVAL;
206 char buff[256];
207 FILE *fp;
209 fp = fopen("/proc/net/dev", "r");
210 if (!fp)
211 panic("Cannot open /proc/net/dev!\n");
213 if (fgets(buff, sizeof(buff), fp)) { ; }
214 if (fgets(buff, sizeof(buff), fp)) { ; }
216 memset(buff, 0, sizeof(buff));
218 while (fgets(buff, sizeof(buff), fp) != NULL) {
219 buff[sizeof(buff) -1] = 0;
221 if (strstr(buff, ifname) == NULL)
222 continue;
224 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
225 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
226 &stats->rx_bytes, &stats->rx_packets,
227 &stats->rx_errors, &stats->rx_drops,
228 &stats->rx_fifo, &stats->rx_frame,
229 &stats->rx_multi, &stats->tx_bytes,
230 &stats->tx_packets, &stats->tx_errors,
231 &stats->tx_drops, &stats->tx_fifo,
232 &stats->tx_colls, &stats->tx_carrier) == 14) {
233 ret = 0;
234 break;
237 memset(buff, 0, sizeof(buff));
240 fclose(fp);
241 return ret;
244 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
246 int ret = -EINVAL, i, cpus, try = 0;
247 char *ptr, *buff;
248 bool seen = false;
249 size_t buff_len;
250 struct ethtool_drvinfo drvinf;
251 FILE *fp;
253 fp = fopen("/proc/interrupts", "r");
254 if (!fp)
255 panic("Cannot open /proc/interrupts!\n");
257 cpus = get_number_cpus();
258 buff_len = cpus * 128;
259 buff = xmalloc(buff_len);
260 retry:
261 fseek(fp, 0, SEEK_SET);
262 memset(buff, 0, buff_len);
264 while (fgets(buff, buff_len, fp) != NULL) {
265 buff[buff_len - 1] = 0;
266 ptr = buff;
268 if (strstr(buff, ifname) == NULL)
269 continue;
271 /* XXX: remove this one here */
272 stats->irq_nr = strtol(ptr, &ptr, 10);
273 bug_on(stats->irq_nr == 0);
275 if (ptr)
276 ptr++;
277 for (i = 0; i < cpus && ptr; ++i) {
278 if (seen)
279 stats->irqs[i] += strtol(ptr, &ptr, 10);
280 else
281 stats->irqs[i] = strtol(ptr, &ptr, 10);
282 if (i == cpus - 1) {
283 ret = 0;
284 seen = true;
288 memset(buff, 0, buff_len);
291 if (ret == -EINVAL && try == 0) {
292 memset(&drvinf, 0, sizeof(drvinf));
293 if (ethtool_drvinf(ifname, &drvinf) < 0)
294 goto done;
296 ifname = drvinf.driver;
297 try++;
299 goto retry;
301 done:
302 xfree(buff);
303 fclose(fp);
304 return ret;
307 static int stats_proc_softirqs(struct ifstat *stats)
309 int i, cpus;
310 char *ptr, *buff;
311 size_t buff_len;
312 FILE *fp;
313 enum {
314 softirqs_net_rx,
315 softirqs_net_tx,
316 } net_type;
318 fp = fopen("/proc/softirqs", "r");
319 if (!fp)
320 panic("Cannot open /proc/softirqs!\n");
322 cpus = get_number_cpus();
323 buff_len = cpus * 128;
324 buff = xmalloc(buff_len);
326 memset(buff, 0, buff_len);
328 while (fgets(buff, buff_len, fp) != NULL) {
329 buff[buff_len - 1] = 0;
331 if ((ptr = strstr(buff, "NET_TX:")))
332 net_type = softirqs_net_tx;
333 else if ((ptr = strstr(buff, "NET_RX:")))
334 net_type = softirqs_net_rx;
335 else
336 continue;
338 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
339 switch (net_type) {
340 case softirqs_net_tx:
341 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
342 break;
343 case softirqs_net_rx:
344 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
345 break;
349 memset(buff, 0, buff_len);
352 xfree(buff);
353 fclose(fp);
354 return 0;
357 static int stats_proc_memory(struct ifstat *stats)
359 char *ptr, buff[256];
360 FILE *fp;
362 fp = fopen("/proc/meminfo", "r");
363 if (!fp)
364 panic("Cannot open /proc/meminfo!\n");
366 memset(buff, 0, sizeof(buff));
368 while (fgets(buff, sizeof(buff), fp) != NULL) {
369 buff[sizeof(buff) - 1] = 0;
371 if ((ptr = strstr(buff, "MemTotal:"))) {
372 ptr += strlen("MemTotal:");
373 stats->mem_total = strtoul(ptr, &ptr, 10);
374 } else if ((ptr = strstr(buff, "MemFree:"))) {
375 ptr += strlen("MemFree:");
376 stats->mem_free = strtoul(ptr, &ptr, 10);
377 } else if ((ptr = strstr(buff, "Active:"))) {
378 ptr += strlen("Active:");
379 stats->mem_active = strtoul(ptr, &ptr, 10);
380 } else if ((ptr = strstr(buff, "Inactive:"))) {
381 ptr += strlen("Inactive:");
382 stats->mem_inactive = strtoul(ptr, &ptr, 10);
383 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
384 ptr += strlen("SwapTotal:");
385 stats->swap_total = strtoul(ptr, &ptr, 10);
386 } else if ((ptr = strstr(buff, "SwapFree:"))) {
387 ptr += strlen("SwapFree:");
388 stats->swap_free = strtoul(ptr, &ptr, 10);
389 } else if ((ptr = strstr(buff, "SwapCached:"))) {
390 ptr += strlen("SwapCached:");
391 stats->swap_cached = strtoul(ptr, &ptr, 10);
394 memset(buff, 0, sizeof(buff));
397 fclose(fp);
398 return 0;
401 static int stats_proc_system(struct ifstat *stats)
403 int cpu, cpus;
404 char *ptr, buff[256];
405 FILE *fp;
407 fp = fopen("/proc/stat", "r");
408 if (!fp)
409 panic("Cannot open /proc/stat!\n");
411 cpus = get_number_cpus();
413 memset(buff, 0, sizeof(buff));
415 while (fgets(buff, sizeof(buff), fp) != NULL) {
416 buff[sizeof(buff) - 1] = 0;
418 if ((ptr = strstr(buff, "cpu"))) {
419 ptr += strlen("cpu");
420 if (isblank(*ptr))
421 goto next;
423 cpu = strtol(ptr, &ptr, 10);
424 bug_on(cpu > cpus);
426 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
427 &stats->cpu_user[cpu],
428 &stats->cpu_nice[cpu],
429 &stats->cpu_sys[cpu],
430 &stats->cpu_idle[cpu],
431 &stats->cpu_iow[cpu]) != 5)
432 goto next;
433 } else if ((ptr = strstr(buff, "ctxt"))) {
434 ptr += strlen("ctxt");
435 stats->cswitch = strtoul(ptr, &ptr, 10);
436 } else if ((ptr = strstr(buff, "procs_running"))) {
437 ptr += strlen("procs_running");
438 stats->procs_run = strtoul(ptr, &ptr, 10);
439 } else if ((ptr = strstr(buff, "procs_blocked"))) {
440 ptr += strlen("procs_blocked");
441 stats->procs_iow = strtoul(ptr, &ptr, 10);
443 next:
444 memset(buff, 0, sizeof(buff));
447 fclose(fp);
448 return 0;
451 static int stats_proc_procs(struct ifstat *stats)
453 DIR *dir;
454 struct dirent *e;
456 dir = opendir("/proc");
457 if (!dir)
458 panic("Cannot open /proc\n");
460 stats->procs_total = 0;
462 while ((e = readdir(dir)) != NULL) {
463 const char *name = e->d_name;
464 char *end;
465 unsigned int pid = strtoul(name, &end, 10);
467 /* not a number */
468 if (pid == 0 && end == name)
469 continue;
471 stats->procs_total++;
474 closedir(dir);
476 return 0;
479 static int adjust_dbm_level(int in_dbm, int dbm_val)
481 if (!in_dbm)
482 return dbm_val;
484 return dbm_val - 0x100;
487 static int stats_wireless(const char *ifname, struct ifstat *stats)
489 int ret;
490 struct iw_statistics ws;
492 ret = wireless_sigqual(ifname, &ws);
493 if (ret != 0) {
494 stats->wifi.bitrate = 0;
495 return -EINVAL;
498 stats->wifi.bitrate = wireless_bitrate(ifname);
500 stats->wifi.signal_level =
501 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
503 stats->wifi.link_qual = ws.qual.qual;
504 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
506 return ret;
509 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
510 #define DIFF(member) do { \
511 if (sizeof(diff->member) != sizeof(new->member) || \
512 sizeof(diff->member) != sizeof(old->member)) \
513 bug(); \
514 bug_on((new->member - old->member) > (new->member)); \
515 DIFF1(member); \
516 } while (0)
518 static void stats_diff(struct ifstat *old, struct ifstat *new,
519 struct ifstat *diff)
521 int cpus, i;
523 DIFF(rx_bytes);
524 DIFF(rx_packets);
525 DIFF(rx_drops);
526 DIFF(rx_errors);
527 DIFF(rx_fifo);
528 DIFF(rx_frame);
529 DIFF(rx_multi);
531 DIFF(tx_bytes);
532 DIFF(tx_packets);
533 DIFF(tx_drops);
534 DIFF(tx_errors);
535 DIFF(tx_fifo);
536 DIFF(tx_colls);
537 DIFF(tx_carrier);
539 DIFF1(wifi.signal_level);
540 DIFF1(wifi.link_qual);
542 DIFF1(cswitch);
544 cpus = get_number_cpus();
546 for (i = 0; i < cpus; ++i) {
547 DIFF(irqs[i]);
548 DIFF(irqs_srx[i]);
549 DIFF(irqs_stx[i]);
551 DIFF1(cpu_user[i]);
552 DIFF1(cpu_nice[i]);
553 DIFF1(cpu_sys[i]);
554 DIFF1(cpu_idle[i]);
555 DIFF1(cpu_iow[i]);
559 static void stats_fetch(const char *ifname, struct ifstat *stats)
561 if (stats_proc_net_dev(ifname, stats) < 0)
562 panic("Cannot fetch device stats!\n");
563 if (stats_proc_softirqs(stats) < 0)
564 panic("Cannot fetch software interrupts!\n");
565 if (stats_proc_memory(stats) < 0)
566 panic("Cannot fetch memory stats!\n");
567 if (stats_proc_system(stats) < 0)
568 panic("Cannot fetch system stats!\n");
569 if (stats_proc_procs(stats) < 0)
570 panic("Cannot fetch process stats!\n");
572 stats_proc_interrupts((char *) ifname, stats);
574 stats_wireless(ifname, stats);
577 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
579 int cpus = get_number_cpus();
581 stats_zero(&stats_old, cpus);
582 stats_zero(&stats_new, cpus);
583 stats_zero(&stats_delta, cpus);
585 stats_fetch(ifname, &stats_old);
586 usleep(ms_interval * 1000);
587 stats_fetch(ifname, &stats_new);
589 stats_diff(&stats_old, &stats_new, &stats_delta);
592 static int cmp_hits(const void *p1, const void *p2)
594 const struct cpu_hit *h1 = p1, *h2 = p2;
597 * We want the hits sorted in descending order, thus reverse the return
598 * values.
600 if (h1->hit == h2->hit)
601 return 0;
602 else if (h1->hit < h2->hit)
603 return 1;
604 else
605 return -1;
608 static int cmp_irqs_rel(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->irqs_rel == h2->irqs_rel)
617 return 0;
618 else if (h1->irqs_rel < h2->irqs_rel)
619 return 1;
620 else
621 return -1;
624 static int cmp_irqs_abs(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_abs == h2->irqs_abs)
633 return 0;
634 else if (h1->irqs_abs < h2->irqs_abs)
635 return 1;
636 else
637 return -1;
640 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
641 int cpus)
643 int i;
645 memset(&stats_avg, 0, sizeof(stats_avg));
647 for (i = 0; i < cpus; ++i) {
648 cpu_hits[i].idx = i;
649 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
650 cpu_hits[i].irqs_rel = rel->irqs[i];
651 cpu_hits[i].irqs_abs = abs->irqs[i];
653 stats_avg.cpu_user += rel->cpu_user[i];
654 stats_avg.cpu_sys += rel->cpu_sys[i];
655 stats_avg.cpu_nice += rel->cpu_nice[i];
656 stats_avg.cpu_idle += rel->cpu_idle[i];
657 stats_avg.cpu_iow += rel->cpu_iow[i];
659 stats_avg.irqs_abs += abs->irqs[i];
660 stats_avg.irqs_rel += rel->irqs[i];
661 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
662 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
665 stats_avg.cpu_user /= cpus;
666 stats_avg.cpu_sys /= cpus;
667 stats_avg.cpu_nice /= cpus;
668 stats_avg.cpu_idle /= cpus;
669 stats_avg.cpu_iow /= cpus;
670 stats_avg.irqs_abs /= cpus;
671 stats_avg.irqs_rel /= cpus;
672 stats_avg.irqs_srx_rel /= cpus;
673 stats_avg.irqs_stx_rel /= cpus;
676 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
677 uint64_t ms_interval, unsigned int top_cpus)
679 size_t len = 0;
680 char buff[64], machine[64];
681 struct ethtool_drvinfo drvinf;
682 u32 rate = device_bitrate(ifname);
683 int link = ethtool_link(ifname);
684 unsigned int cpus = get_number_cpus();
686 memset(&drvinf, 0, sizeof(drvinf));
687 ethtool_drvinf(ifname, &drvinf);
689 memset(buff, 0, sizeof(buff));
690 memset(machine, 0, sizeof(machine));
692 if (rate)
693 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
694 if (link >= 0)
695 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
696 link == 0 ? "no" : "yes");
698 if (!strstr(uts.release, uts.machine))
699 slprintf(machine, sizeof(machine), " %s,", uts.machine);
701 mvwprintw(screen, (*voff)++, 2,
702 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
703 " ", uts.release, machine,
704 ifname, drvinf.driver, buff, ms_interval, top_cpus,
705 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
708 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
709 int *voff)
711 attron(A_REVERSE);
713 mvwprintw(screen, (*voff)++, 0,
714 " rx: %16.3llf MiB/t "
715 "%10llu pkts/t "
716 "%10llu drops/t "
717 "%10llu errors/t ",
718 ((long double) rel->rx_bytes) / (1LLU << 20),
719 rel->rx_packets, rel->rx_drops, rel->rx_errors);
721 mvwprintw(screen, (*voff)++, 0,
722 " tx: %16.3llf MiB/t "
723 "%10llu pkts/t "
724 "%10llu drops/t "
725 "%10llu errors/t ",
726 ((long double) rel->tx_bytes) / (1LLU << 20),
727 rel->tx_packets, rel->tx_drops, rel->tx_errors);
729 attroff(A_REVERSE);
732 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
733 int *voff)
735 mvwprintw(screen, (*voff)++, 2,
736 "rx: %16.3llf MiB "
737 "%10llu pkts "
738 "%10llu drops "
739 "%10llu errors",
740 ((long double) abs->rx_bytes) / (1LLU << 20),
741 abs->rx_packets, abs->rx_drops, abs->rx_errors);
743 mvwprintw(screen, (*voff)++, 2,
744 "tx: %16.3llf MiB "
745 "%10llu pkts "
746 "%10llu drops "
747 "%10llu errors",
748 ((long double) abs->tx_bytes) / (1LLU << 20),
749 abs->tx_packets, abs->tx_drops, abs->tx_errors);
752 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
753 const struct ifstat *abs, int *voff)
755 mvwprintw(screen, (*voff)++, 2,
756 "sys: %14u cs/t "
757 "%11u procs "
758 "%11u running "
759 "%10u iowait",
760 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
763 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
765 mvwprintw(screen, (*voff)++, 2,
766 "mem: %13uM total "
767 "%9uM used "
768 "%11uM active "
769 "%10uM inactive",
770 abs->mem_total / 1024,
771 (abs->mem_total - abs->mem_free) / 1024,
772 abs->mem_active / 1024,
773 abs->mem_inactive / 1024);
775 mvwprintw(screen, (*voff)++, 2,
776 "swap: %12uM total "
777 "%9uM used "
778 "%11uM cached",
779 abs->swap_total / 1024,
780 (abs->swap_total - abs->swap_free) / 1024,
781 abs->swap_cached / 1024);
784 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
785 int *voff, unsigned int idx, char *tag)
787 int max_padd = padding_from_num(get_number_cpus());
788 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
789 rel->cpu_idle[idx] + rel->cpu_iow[idx];
791 mvwprintw(screen, (*voff)++, 2,
792 "cpu%*d %s: %11.1lf%% usr/t "
793 "%9.1lf%% sys/t "
794 "%10.1lf%% idl/t "
795 "%11.1lf%% iow/t",
796 max_padd, idx, tag,
797 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
798 100.0 * rel->cpu_sys[idx] / all,
799 100.0 * rel->cpu_idle[idx] / all,
800 100.0 * rel->cpu_iow[idx] / all);
803 #define MEDIAN_EVEN(member) do { \
804 m_##member = (rel->member[i] + rel->member[j]) / 2; \
805 } while (0)
807 #define MEDIAN_ODD(member) do { \
808 m_##member = rel->member[i]; \
809 } while (0)
811 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
812 const struct avg_stat *avg, int top_cpus,
813 int *voff)
815 int i;
816 int cpus = get_number_cpus();
817 int max_padd = padding_from_num(cpus);
818 uint64_t all;
820 if (top_cpus == 0)
821 return;
823 /* Display top hitter */
824 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
826 /* Make sure we don't display the min. hitter twice */
827 if (top_cpus == cpus)
828 top_cpus--;
830 for (i = 1; i < top_cpus; ++i)
831 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "|");
833 /* Display minimum hitter */
834 if (cpus != 1)
835 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
837 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
838 mvwprintw(screen, (*voff)++, 2,
839 "avg:%*s%14.1lf%% "
840 "%9.1lf%% "
841 "%10.1lf%% "
842 "%11.1lf%%", max_padd, "",
843 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
844 100.0 * avg->cpu_sys / all,
845 100.0 * avg->cpu_idle /all,
846 100.0 * avg->cpu_iow / all);
848 if (show_median) {
849 long double m_cpu_user, m_cpu_nice, m_cpu_sys, m_cpu_idle, m_cpu_iow;
851 i = cpu_hits[cpus / 2].idx;
852 if (cpus % 2 == 0) {
853 /* take the mean of the 2 middle entries */
854 int j = cpu_hits[(cpus / 2) - 1].idx;
856 MEDIAN_EVEN(cpu_user);
857 MEDIAN_EVEN(cpu_nice);
858 MEDIAN_EVEN(cpu_sys);
859 MEDIAN_EVEN(cpu_idle);
860 MEDIAN_EVEN(cpu_iow);
861 } else {
862 /* take the middle entry as is */
863 MEDIAN_ODD(cpu_user);
864 MEDIAN_ODD(cpu_nice);
865 MEDIAN_ODD(cpu_sys);
866 MEDIAN_ODD(cpu_idle);
867 MEDIAN_ODD(cpu_iow);
870 all = m_cpu_user + m_cpu_sys + m_cpu_nice + m_cpu_idle + m_cpu_iow;
871 mvwprintw(screen, (*voff)++, 2,
872 "med: %14.1lf%% "
873 "%9.1lf%% "
874 "%10.1lf%% "
875 "%11.1lf%%",
876 100.0 * (m_cpu_user + m_cpu_nice) / all,
877 100.0 * m_cpu_sys / all,
878 100.0 * m_cpu_idle /all,
879 100.0 * m_cpu_iow / all);
883 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
884 int *voff, unsigned int idx, char *tag)
886 int max_padd = padding_from_num(get_number_cpus());
888 mvwprintw(screen, (*voff)++, 2,
889 "cpu%*d %s: %12llu irqs/t "
890 "%17llu sirq rx/t "
891 "%17llu sirq tx/t",
892 max_padd, idx, tag,
893 rel->irqs[idx],
894 rel->irqs_srx[idx],
895 rel->irqs_stx[idx]);
898 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
899 const struct avg_stat *avg, int top_cpus,
900 int *voff)
902 int i;
903 int cpus = get_number_cpus();
904 int max_padd = padding_from_num(cpus);
906 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
908 if (top_cpus == cpus)
909 top_cpus--;
911 for (i = 1; i < top_cpus; ++i)
912 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
914 if (cpus != 1)
915 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
917 mvwprintw(screen, (*voff)++, 2,
918 "avg:%*s%17.1Lf "
919 "%17.1Lf "
920 "%17.1Lf", max_padd, "",
921 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
923 if (show_median) {
924 long double m_irqs, m_irqs_srx, m_irqs_stx;
926 i = cpu_hits[cpus / 2].idx;
927 if (cpus % 2 == 0) {
928 /* take the mean of the 2 middle entries */
929 int j = cpu_hits[(cpus / 2) - 1].idx;
931 MEDIAN_EVEN(irqs);
932 MEDIAN_EVEN(irqs_srx);
933 MEDIAN_EVEN(irqs_stx);
934 } else {
935 /* take the middle entry as is */
936 MEDIAN_ODD(irqs);
937 MEDIAN_ODD(irqs_srx);
938 MEDIAN_ODD(irqs_stx);
941 mvwprintw(screen, (*voff)++, 2,
942 "med:%*s%17.1Lf "
943 "%17.1Lf "
944 "%17.1Lf", max_padd, "",
945 m_irqs, m_irqs_srx, m_irqs_stx);
949 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
950 int *voff, unsigned int idx, char *tag)
952 int max_padd = padding_from_num(get_number_cpus());
954 mvwprintw(screen, (*voff)++, 2,
955 "cpu%*d %s: %12llu irqs",
956 max_padd, idx, tag, abs->irqs[idx]);
959 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
960 const struct avg_stat *avg, int top_cpus,
961 int *voff)
963 int i;
964 int cpus = get_number_cpus();
965 int max_padd = padding_from_num(cpus);
967 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
969 if (top_cpus == cpus)
970 top_cpus--;
972 for (i = 1; i < top_cpus; ++i)
973 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
975 if (cpus != 1)
976 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
978 mvwprintw(screen, (*voff)++, 2,
979 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
981 if (show_median) {
982 long double m_irqs;
984 i = cpu_hits[cpus / 2].idx;
985 if (cpus % 2 == 0) {
986 /* take the mean of the 2 middle entries */
987 int j = cpu_hits[(cpus / 2) - 1].idx;
989 m_irqs = (abs->irqs[i] + abs->irqs[j]) / 2;
990 } else {
991 /* take the middle entry as is */
992 m_irqs = abs->irqs[i];
995 mvwprintw(screen, (*voff)++, 2,
996 "med:%*s%17.1Lf", max_padd, "", m_irqs);
1000 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
1001 const struct ifstat *abs, int *voff)
1003 if (iswireless(abs)) {
1004 mvwprintw(screen, (*voff)++, 2,
1005 "linkqual: %7d/%d (%d/t) ",
1006 abs->wifi.link_qual,
1007 abs->wifi.link_qual_max,
1008 rel->wifi.link_qual);
1010 mvwprintw(screen, (*voff)++, 2,
1011 "signal: %8d dBm (%d dBm/t) ",
1012 abs->wifi.signal_level,
1013 rel->wifi.signal_level);
1017 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
1018 const struct ifstat *abs, const struct avg_stat *avg,
1019 int *first, uint64_t ms_interval, unsigned int top_cpus,
1020 bool need_info)
1022 int cpus, top, voff = 1, cvoff = 2;
1024 curs_set(0);
1026 cpus = get_number_cpus();
1027 top = min(cpus, top_cpus);
1029 stats_top(rel, abs, cpus);
1031 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
1033 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
1035 voff++;
1036 screen_net_dev_rel(screen, rel, &voff);
1038 voff++;
1039 screen_net_dev_abs(screen, abs, &voff);
1041 voff++;
1042 screen_sys(screen, rel, abs, &voff);
1044 voff++;
1045 screen_mem_swap(screen, abs, &voff);
1047 voff++;
1048 screen_percpu_states(screen, rel, avg, top, &voff);
1050 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
1052 voff++;
1053 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
1055 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
1057 voff++;
1058 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
1060 voff++;
1061 screen_wireless(screen, rel, abs, &voff);
1063 if (*first) {
1064 mvwprintw(screen, cvoff, 2, "Collecting data ...");
1065 *first = 0;
1066 } else {
1067 if (need_info)
1068 mvwprintw(screen, cvoff, 2, "(consider to increase "
1069 "your sampling interval, e.g. -t 10000)");
1070 else
1071 mvwprintw(screen, cvoff, 2, " "
1072 " ");
1075 wrefresh(screen);
1076 refresh();
1079 static int screen_main(const char *ifname, uint64_t ms_interval,
1080 unsigned int top_cpus, bool suppress_warnings)
1082 int first = 1, key;
1083 u32 rate = device_bitrate(ifname);
1084 bool need_info = false;
1086 stats_screen = screen_init(true);
1088 if (rate > SPEED_1000 && ms_interval <= 1000 && !suppress_warnings)
1089 need_info = true;
1091 while (!sigint) {
1092 key = getch();
1093 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1094 break;
1096 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1097 &first, ms_interval, top_cpus, need_info);
1099 stats_sample_generic(ifname, ms_interval);
1102 screen_end();
1104 return 0;
1107 static void term_csv(const char *ifname, const struct ifstat *rel,
1108 const struct ifstat *abs, uint64_t ms_interval)
1110 int cpus, i;
1112 printf("%ld ", time(0));
1114 printf("%llu ", rel->rx_bytes);
1115 printf("%llu ", rel->rx_packets);
1116 printf("%llu ", rel->rx_drops);
1117 printf("%llu ", rel->rx_errors);
1119 printf("%llu ", abs->rx_bytes);
1120 printf("%llu ", abs->rx_packets);
1121 printf("%llu ", abs->rx_drops);
1122 printf("%llu ", abs->rx_errors);
1124 printf("%llu ", rel->tx_bytes);
1125 printf("%llu ", rel->tx_packets);
1126 printf("%llu ", rel->tx_drops);
1127 printf("%llu ", rel->tx_errors);
1129 printf("%llu ", abs->tx_bytes);
1130 printf("%llu ", abs->tx_packets);
1131 printf("%llu ", abs->tx_drops);
1132 printf("%llu ", abs->tx_errors);
1134 printf("%u ", rel->cswitch);
1135 printf("%lu ", abs->mem_free);
1136 printf("%lu ", abs->mem_total - abs->mem_free);
1137 printf("%lu ", abs->mem_total);
1138 printf("%lu ", abs->swap_free);
1139 printf("%lu ", abs->swap_total - abs->swap_free);
1140 printf("%lu ", abs->swap_total);
1141 printf("%u ", abs->procs_total);
1142 printf("%u ", abs->procs_run);
1143 printf("%u ", abs->procs_iow);
1145 cpus = get_number_cpus();
1147 for (i = 0; i < cpus; ++i) {
1148 printf("%lu ", rel->cpu_user[i]);
1149 printf("%lu ", rel->cpu_nice[i]);
1150 printf("%lu ", rel->cpu_sys[i]);
1151 printf("%lu ", rel->cpu_idle[i]);
1152 printf("%lu ", rel->cpu_iow[i]);
1154 printf("%llu ", rel->irqs[i]);
1155 printf("%llu ", abs->irqs[i]);
1157 printf("%llu ", rel->irqs_srx[i]);
1158 printf("%llu ", abs->irqs_srx[i]);
1160 printf("%llu ", rel->irqs_stx[i]);
1161 printf("%llu ", abs->irqs_stx[i]);
1164 if (iswireless(abs)) {
1165 printf("%u ", rel->wifi.link_qual);
1166 printf("%u ", abs->wifi.link_qual);
1167 printf("%u ", abs->wifi.link_qual_max);
1169 printf("%d ", rel->wifi.signal_level);
1170 printf("%d ", abs->wifi.signal_level);
1173 puts("");
1174 fflush(stdout);
1177 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1178 uint64_t ms_interval)
1180 int cpus, i, j = 1;
1182 printf("# gnuplot dump (#col:description)\n");
1183 printf("# networking interface: %s\n", ifname);
1184 printf("# sampling interval (t): %lu ms\n", ms_interval);
1185 printf("# %d:unixtime ", j++);
1187 printf("%d:rx-bytes-per-t ", j++);
1188 printf("%d:rx-pkts-per-t ", j++);
1189 printf("%d:rx-drops-per-t ", j++);
1190 printf("%d:rx-errors-per-t ", j++);
1192 printf("%d:rx-bytes ", j++);
1193 printf("%d:rx-pkts ", j++);
1194 printf("%d:rx-drops ", j++);
1195 printf("%d:rx-errors ", j++);
1197 printf("%d:tx-bytes-per-t ", j++);
1198 printf("%d:tx-pkts-per-t ", j++);
1199 printf("%d:tx-drops-per-t ", j++);
1200 printf("%d:tx-errors-per-t ", j++);
1202 printf("%d:tx-bytes ", j++);
1203 printf("%d:tx-pkts ", j++);
1204 printf("%d:tx-drops ", j++);
1205 printf("%d:tx-errors ", j++);
1207 printf("%d:context-switches-per-t ", j++);
1208 printf("%d:mem-free ", j++);
1209 printf("%d:mem-used ", j++);
1210 printf("%d:mem-total ", j++);
1211 printf("%d:swap-free ", j++);
1212 printf("%d:swap-used ", j++);
1213 printf("%d:swap-total ", j++);
1214 printf("%d:procs-total ", j++);
1215 printf("%d:procs-in-run ", j++);
1216 printf("%d:procs-in-iow ", j++);
1218 cpus = get_number_cpus();
1220 for (i = 0; i < cpus; ++i) {
1221 printf("%d:cpu%i-usr-per-t ", j++, i);
1222 printf("%d:cpu%i-nice-per-t ", j++, i);
1223 printf("%d:cpu%i-sys-per-t ", j++, i);
1224 printf("%d:cpu%i-idle-per-t ", j++, i);
1225 printf("%d:cpu%i-iow-per-t ", j++, i);
1227 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1228 printf("%d:cpu%i-net-irqs ", j++, i);
1230 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1231 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1232 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1233 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1236 if (iswireless(abs)) {
1237 printf("%d:wifi-link-qual-per-t ", j++);
1238 printf("%d:wifi-link-qual ", j++);
1239 printf("%d:wifi-link-qual-max ", j++);
1241 printf("%d:wifi-signal-dbm-per-t ", j++);
1242 printf("%d:wifi-signal-dbm ", j++);
1245 puts("");
1246 printf("# data:\n");
1247 fflush(stdout);
1250 static int term_main(const char *ifname, uint64_t ms_interval,
1251 unsigned int top_cpus __maybe_unused,
1252 bool suppress_warnings __maybe_unused)
1254 int first = 1;
1256 do {
1257 stats_sample_generic(ifname, ms_interval);
1259 if (first) {
1260 first = 0;
1261 term_csv_header(ifname, &stats_new, ms_interval);
1264 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1265 } while (stats_loop && !sigint);
1267 return 0;
1270 int main(int argc, char **argv)
1272 short ifflags = 0;
1273 int c, opt_index, ret, cpus, promisc = 0;
1274 unsigned int top_cpus = 5;
1275 uint64_t interval = 1000;
1276 char *ifname = NULL;
1277 bool suppress_warnings = false;
1278 int (*func_main)(const char *ifname, uint64_t ms_interval,
1279 unsigned int top_cpus, bool suppress_warnings);
1281 func_main = screen_main;
1283 setfsuid(getuid());
1284 setfsgid(getgid());
1286 while ((c = getopt_long(argc, argv, short_options, long_options,
1287 &opt_index)) != EOF) {
1288 switch (c) {
1289 case 'h':
1290 help();
1291 break;
1292 case 'v':
1293 version();
1294 break;
1295 case 'W':
1296 suppress_warnings = true;
1297 break;
1298 case 'd':
1299 ifname = xstrndup(optarg, IFNAMSIZ);
1300 break;
1301 case 't':
1302 interval = strtoul(optarg, NULL, 10);
1303 break;
1304 case 'n':
1305 top_cpus = strtoul(optarg, NULL, 10);
1306 if (top_cpus < 1)
1307 panic("Number of top hitter CPUs must be greater than 0");
1308 break;
1309 case 'l':
1310 stats_loop = 1;
1311 break;
1312 case 'p':
1313 promisc = 1;
1314 break;
1315 case 'm':
1316 show_median = 1;
1317 break;
1318 case 'c':
1319 func_main = term_main;
1320 break;
1321 case '?':
1322 switch (optopt) {
1323 case 'd':
1324 case 't':
1325 panic("Option -%c requires an argument!\n",
1326 optopt);
1327 default:
1328 if (isprint(optopt))
1329 printf("Unknown option character `0x%X\'!\n", optopt);
1330 die();
1332 default:
1333 break;
1337 if (argc == 1)
1338 help();
1340 if (argc == 2)
1341 ifname = xstrndup(argv[1], IFNAMSIZ);
1342 if (ifname == NULL)
1343 panic("No networking device given!\n");
1345 if (!strncmp("lo", ifname, IFNAMSIZ))
1346 panic("lo is not supported!\n");
1347 if (device_mtu(ifname) == 0)
1348 panic("This is no networking device!\n");
1350 register_signal(SIGINT, signal_handler);
1351 register_signal(SIGHUP, signal_handler);
1353 cpus = get_number_cpus();
1354 top_cpus = min(top_cpus, cpus);
1355 if (uname(&uts) < 0)
1356 panic("Cannot execute uname!\n");
1358 stats_alloc(&stats_old, cpus);
1359 stats_alloc(&stats_new, cpus);
1360 stats_alloc(&stats_delta, cpus);
1362 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1364 if (promisc)
1365 ifflags = enter_promiscuous_mode(ifname);
1366 ret = func_main(ifname, interval, top_cpus, suppress_warnings);
1367 if (promisc)
1368 leave_promiscuous_mode(ifname, ifflags);
1370 xfree(ifname);
1371 return ret;