xio: rename xio to ioops and reduce its includes
[netsniff-ng.git] / ifpps.c
blobe835660fa8ae9ac8208eb2290a0850fb9f2971e9
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 <signal.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <time.h>
20 #include "die.h"
21 #include "xmalloc.h"
22 #include "xutils.h"
23 #include "ioops.h"
24 #include "cpus.h"
25 #include "built_in.h"
27 struct wifi_stat {
28 uint32_t bitrate;
29 int16_t link_qual, link_qual_max;
30 int signal_level /*, noise_level*/;
33 struct ifstat {
34 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
35 long long unsigned int rx_fifo, rx_frame, rx_multi;
36 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
37 long long unsigned int tx_fifo, tx_colls, tx_carrier;
38 uint64_t mem_free, mem_total;
39 uint32_t irq_nr, procs_run, procs_iow, cswitch, forks;
40 struct wifi_stat wifi;
42 * Pointer members need to be last in order for stats_zero() to work
43 * properly.
45 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
46 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
49 struct cpu_hit {
50 unsigned int idx;
51 uint64_t hit;
52 long long unsigned int irqs_rel, irqs_abs;
55 static volatile sig_atomic_t sigint = 0;
56 static struct ifstat stats_old, stats_new, stats_delta;
57 static struct cpu_hit *cpu_hits;
58 static int stats_loop = 0;
59 static WINDOW *stats_screen = NULL;
61 static const char *short_options = "d:t:n:vhclp";
62 static const struct option long_options[] = {
63 {"dev", required_argument, NULL, 'd'},
64 {"interval", required_argument, NULL, 't'},
65 {"num-cpus", required_argument, NULL, 'n'},
66 {"promisc", no_argument, NULL, 'p'},
67 {"csv", no_argument, NULL, 'c'},
68 {"loop", no_argument, NULL, 'l'},
69 {"version", no_argument, NULL, 'v'},
70 {"help", no_argument, NULL, 'h'},
71 {NULL, 0, NULL, 0}
74 static void signal_handler(int number)
76 switch (number) {
77 case SIGINT:
78 sigint = 1;
79 break;
80 case SIGHUP:
81 default:
82 break;
86 static inline int iswireless(const struct ifstat *stats)
88 return stats->wifi.bitrate > 0;
91 static void __noreturn help(void)
93 printf("\nifpps %s, top-like kernel networking and system statistics\n",
94 VERSION_STRING);
95 puts("http://www.netsniff-ng.org\n\n"
96 "Usage: ifpps [options] || ifpps <netdev>\n"
97 "Options:\n"
98 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
99 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
100 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
101 " in ncurses mode (default 10)\n"
102 " -p|--promisc Promiscuous mode\n"
103 " -c|--csv Output to terminal as Gnuplot-ready data\n"
104 " -l|--loop Continuous CSV output\n"
105 " -v|--version Print version and exit\n"
106 " -h|--help Print this help and exit\n\n"
107 "Examples:\n"
108 " ifpps eth0\n"
109 " ifpps -pd eth0\n"
110 " ifpps -lpcd wlan0 > plot.dat\n\n"
111 "Note:\n"
112 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
113 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
114 "Please report bugs to <bugs@netsniff-ng.org>\n"
115 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
116 "Swiss federal institute of technology (ETH Zurich)\n"
117 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
118 "License: GNU GPL version 2.0\n"
119 "This is free software: you are free to change and redistribute it.\n"
120 "There is NO WARRANTY, to the extent permitted by law.\n");
121 die();
124 static void __noreturn version(void)
126 printf("\nifpps %s, top-like kernel networking and system statistics\n",
127 VERSION_LONG);
128 puts("http://www.netsniff-ng.org\n\n"
129 "Please report bugs to <bugs@netsniff-ng.org>\n"
130 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
131 "Swiss federal institute of technology (ETH Zurich)\n"
132 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
133 "License: GNU GPL version 2.0\n"
134 "This is free software: you are free to change and redistribute it.\n"
135 "There is NO WARRANTY, to the extent permitted by law.\n");
136 die();
139 static inline int padding_from_num(int n)
141 int i = 0;
142 do i++;
143 while ((n /= 10) > 0);
144 return i;
147 #define STATS_ALLOC1(member) \
148 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
150 static void stats_alloc(struct ifstat *stats, int cpus)
152 STATS_ALLOC1(irqs);
153 STATS_ALLOC1(irqs_srx);
154 STATS_ALLOC1(irqs_stx);
156 STATS_ALLOC1(cpu_user);
157 STATS_ALLOC1(cpu_sys);
158 STATS_ALLOC1(cpu_nice);
159 STATS_ALLOC1(cpu_idle);
160 STATS_ALLOC1(cpu_iow);
163 #define STATS_ZERO1(member) \
164 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
166 static void stats_zero(struct ifstat *stats, int cpus)
168 /* Only clear the non-pointer members */
169 memset(stats, 0, offsetof(struct ifstat, irqs));
171 STATS_ZERO1(irqs);
172 STATS_ZERO1(irqs_srx);
173 STATS_ZERO1(irqs_stx);
175 STATS_ZERO1(cpu_user);
176 STATS_ZERO1(cpu_sys);
177 STATS_ZERO1(cpu_nice);
178 STATS_ZERO1(cpu_idle);
179 STATS_ZERO1(cpu_iow);
182 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
184 int ret = -EINVAL;
185 char buff[256];
186 FILE *fp;
188 fp = fopen("/proc/net/dev", "r");
189 if (!fp)
190 panic("Cannot open /proc/net/dev!\n");
192 if (fgets(buff, sizeof(buff), fp)) { ; }
193 if (fgets(buff, sizeof(buff), fp)) { ; }
195 memset(buff, 0, sizeof(buff));
197 while (fgets(buff, sizeof(buff), fp) != NULL) {
198 buff[sizeof(buff) -1] = 0;
200 if (strstr(buff, ifname) == NULL)
201 continue;
203 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
204 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
205 &stats->rx_bytes, &stats->rx_packets,
206 &stats->rx_errors, &stats->rx_drops,
207 &stats->rx_fifo, &stats->rx_frame,
208 &stats->rx_multi, &stats->tx_bytes,
209 &stats->tx_packets, &stats->tx_errors,
210 &stats->tx_drops, &stats->tx_fifo,
211 &stats->tx_colls, &stats->tx_carrier) == 14) {
212 ret = 0;
213 break;
216 memset(buff, 0, sizeof(buff));
219 fclose(fp);
220 return ret;
223 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
225 int ret = -EINVAL, i, cpus, try = 0;
226 char *ptr, *buff;
227 bool seen = false;
228 size_t buff_len;
229 struct ethtool_drvinfo drvinf;
230 FILE *fp;
232 fp = fopen("/proc/interrupts", "r");
233 if (!fp)
234 panic("Cannot open /proc/interrupts!\n");
236 cpus = get_number_cpus();
237 buff_len = cpus * 128;
238 buff = xmalloc(buff_len);
239 retry:
240 fseek(fp, 0, SEEK_SET);
241 memset(buff, 0, buff_len);
243 while (fgets(buff, buff_len, fp) != NULL) {
244 buff[buff_len - 1] = 0;
245 ptr = buff;
247 if (strstr(buff, ifname) == NULL)
248 continue;
250 /* XXX: remove this one here */
251 stats->irq_nr = strtol(ptr, &ptr, 10);
252 bug_on(stats->irq_nr == 0);
254 if (ptr)
255 ptr++;
256 for (i = 0; i < cpus && ptr; ++i) {
257 if (seen)
258 stats->irqs[i] += strtol(ptr, &ptr, 10);
259 else
260 stats->irqs[i] = strtol(ptr, &ptr, 10);
261 if (i == cpus - 1) {
262 ret = 0;
263 seen = true;
267 memset(buff, 0, buff_len);
270 if (ret == -EINVAL && try == 0) {
271 memset(&drvinf, 0, sizeof(drvinf));
272 if (ethtool_drvinf(ifname, &drvinf) < 0)
273 goto done;
275 ifname = drvinf.driver;
276 try++;
278 goto retry;
280 done:
281 xfree(buff);
282 fclose(fp);
283 return ret;
286 static int stats_proc_softirqs(struct ifstat *stats)
288 int i, cpus;
289 char *ptr, *buff;
290 size_t buff_len;
291 FILE *fp;
292 enum {
293 softirqs_net_rx,
294 softirqs_net_tx,
295 } net_type;
297 fp = fopen("/proc/softirqs", "r");
298 if (!fp)
299 panic("Cannot open /proc/softirqs!\n");
301 cpus = get_number_cpus();
302 buff_len = cpus * 128;
303 buff = xmalloc(buff_len);
305 memset(buff, 0, buff_len);
307 while (fgets(buff, buff_len, fp) != NULL) {
308 buff[buff_len - 1] = 0;
310 if ((ptr = strstr(buff, "NET_TX:")))
311 net_type = softirqs_net_tx;
312 else if ((ptr = strstr(buff, "NET_RX:")))
313 net_type = softirqs_net_rx;
314 else
315 continue;
317 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
318 switch (net_type) {
319 case softirqs_net_tx:
320 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
321 break;
322 case softirqs_net_rx:
323 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
324 break;
328 memset(buff, 0, buff_len);
331 xfree(buff);
332 fclose(fp);
333 return 0;
336 static int stats_proc_memory(struct ifstat *stats)
338 char *ptr, buff[256];
339 FILE *fp;
341 fp = fopen("/proc/meminfo", "r");
342 if (!fp)
343 panic("Cannot open /proc/meminfo!\n");
345 memset(buff, 0, sizeof(buff));
347 while (fgets(buff, sizeof(buff), fp) != NULL) {
348 buff[sizeof(buff) - 1] = 0;
350 if ((ptr = strstr(buff, "MemTotal:"))) {
351 ptr += strlen("MemTotal:");
352 stats->mem_total = strtoul(ptr, &ptr, 10);
353 } else if ((ptr = strstr(buff, "MemFree:"))) {
354 ptr += strlen("MemFree:");
355 stats->mem_free = strtoul(ptr, &ptr, 10);
358 memset(buff, 0, sizeof(buff));
361 fclose(fp);
362 return 0;
365 static int stats_proc_system(struct ifstat *stats)
367 int cpu, cpus;
368 char *ptr, buff[256];
369 FILE *fp;
371 fp = fopen("/proc/stat", "r");
372 if (!fp)
373 panic("Cannot open /proc/stat!\n");
375 cpus = get_number_cpus();
377 memset(buff, 0, sizeof(buff));
379 while (fgets(buff, sizeof(buff), fp) != NULL) {
380 buff[sizeof(buff) - 1] = 0;
382 if ((ptr = strstr(buff, "cpu"))) {
383 ptr += strlen("cpu");
384 if (isblank(*ptr))
385 goto next;
387 cpu = strtol(ptr, &ptr, 10);
388 bug_on(cpu > cpus);
390 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
391 &stats->cpu_user[cpu],
392 &stats->cpu_nice[cpu],
393 &stats->cpu_sys[cpu],
394 &stats->cpu_idle[cpu],
395 &stats->cpu_iow[cpu]) != 5)
396 goto next;
397 } else if ((ptr = strstr(buff, "ctxt"))) {
398 ptr += strlen("ctxt");
399 stats->cswitch = strtoul(ptr, &ptr, 10);
400 } else if ((ptr = strstr(buff, "processes"))) {
401 ptr += strlen("processes");
402 stats->forks = strtoul(ptr, &ptr, 10);
403 } else if ((ptr = strstr(buff, "procs_running"))) {
404 ptr += strlen("procs_running");
405 stats->procs_run = strtoul(ptr, &ptr, 10);
406 } else if ((ptr = strstr(buff, "procs_blocked"))) {
407 ptr += strlen("procs_blocked");
408 stats->procs_iow = strtoul(ptr, &ptr, 10);
410 next:
411 memset(buff, 0, sizeof(buff));
414 fclose(fp);
415 return 0;
418 static int adjust_dbm_level(int in_dbm, int dbm_val)
420 if (!in_dbm)
421 return dbm_val;
423 return dbm_val - 0x100;
426 static int stats_wireless(const char *ifname, struct ifstat *stats)
428 int ret;
429 struct iw_statistics ws;
431 ret = wireless_sigqual(ifname, &ws);
432 if (ret != 0) {
433 stats->wifi.bitrate = 0;
434 return -EINVAL;
437 stats->wifi.bitrate = wireless_bitrate(ifname);
439 stats->wifi.signal_level =
440 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
442 stats->wifi.link_qual = ws.qual.qual;
443 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
445 return ret;
448 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
449 #define DIFF(member) do { \
450 if (sizeof(diff->member) != sizeof(new->member) || \
451 sizeof(diff->member) != sizeof(old->member)) \
452 bug(); \
453 bug_on((new->member - old->member) > (new->member)); \
454 DIFF1(member); \
455 } while (0)
457 static void stats_diff(struct ifstat *old, struct ifstat *new,
458 struct ifstat *diff)
460 int cpus, i;
462 DIFF(rx_bytes);
463 DIFF(rx_packets);
464 DIFF(rx_drops);
465 DIFF(rx_errors);
466 DIFF(rx_fifo);
467 DIFF(rx_frame);
468 DIFF(rx_multi);
470 DIFF(tx_bytes);
471 DIFF(tx_bytes);
472 DIFF(tx_packets);
473 DIFF(tx_drops);
474 DIFF(tx_errors);
475 DIFF(tx_fifo);
476 DIFF(tx_colls);
477 DIFF(tx_carrier);
479 DIFF1(procs_run);
480 DIFF1(procs_iow);
482 DIFF1(wifi.signal_level);
483 DIFF1(wifi.link_qual);
485 DIFF1(cswitch);
486 DIFF1(forks);
488 cpus = get_number_cpus();
490 for (i = 0; i < cpus; ++i) {
491 DIFF(irqs[i]);
492 DIFF(irqs_srx[i]);
493 DIFF(irqs_stx[i]);
495 DIFF1(cpu_user[i]);
496 DIFF1(cpu_nice[i]);
497 DIFF1(cpu_sys[i]);
498 DIFF1(cpu_idle[i]);
499 DIFF1(cpu_iow[i]);
503 static void stats_fetch(const char *ifname, struct ifstat *stats)
505 if (stats_proc_net_dev(ifname, stats) < 0)
506 panic("Cannot fetch device stats!\n");
507 if (stats_proc_softirqs(stats) < 0)
508 panic("Cannot fetch software interrupts!\n");
509 if (stats_proc_memory(stats) < 0)
510 panic("Cannot fetch memory stats!\n");
511 if (stats_proc_system(stats) < 0)
512 panic("Cannot fetch system stats!\n");
514 stats_proc_interrupts((char *) ifname, stats);
516 stats_wireless(ifname, stats);
519 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
521 int cpus = get_number_cpus();
523 stats_zero(&stats_old, cpus);
524 stats_zero(&stats_new, cpus);
525 stats_zero(&stats_delta, cpus);
527 stats_fetch(ifname, &stats_old);
528 usleep(ms_interval * 1000);
529 stats_fetch(ifname, &stats_new);
531 stats_diff(&stats_old, &stats_new, &stats_delta);
534 static int cmp_hits(const void *p1, const void *p2)
536 const struct cpu_hit *h1 = p1, *h2 = p2;
539 * We want the hits sorted in descending order, thus reverse the return
540 * values.
542 if (h1->hit == h2->hit)
543 return 0;
544 else if (h1->hit < h2->hit)
545 return 1;
546 else
547 return -1;
550 static int cmp_irqs_rel(const void *p1, const void *p2)
552 const struct cpu_hit *h1 = p1, *h2 = p2;
555 * We want the hits sorted in descending order, thus reverse the return
556 * values.
558 if (h1->irqs_rel == h2->irqs_rel)
559 return 0;
560 else if (h1->irqs_rel < h2->irqs_rel)
561 return 1;
562 else
563 return -1;
566 static int cmp_irqs_abs(const void *p1, const void *p2)
568 const struct cpu_hit *h1 = p1, *h2 = p2;
571 * We want the hits sorted in descending order, thus reverse the return
572 * values.
574 if (h1->irqs_abs == h2->irqs_abs)
575 return 0;
576 else if (h1->irqs_abs < h2->irqs_abs)
577 return 1;
578 else
579 return -1;
582 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
583 int top_cpus)
585 int i;
587 for (i = 0; i < top_cpus; ++i) {
588 cpu_hits[i].idx = i;
589 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
590 cpu_hits[i].irqs_rel = rel->irqs[i];
591 cpu_hits[i].irqs_abs = abs->irqs[i];
595 static void screen_init(WINDOW **screen)
597 (*screen) = initscr();
599 raw();
600 noecho();
601 cbreak();
602 nodelay((*screen), TRUE);
604 keypad(stdscr, TRUE);
606 refresh();
607 wrefresh((*screen));
610 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
611 uint64_t ms_interval, unsigned int top_cpus)
613 size_t len = 0;
614 char buff[64];
615 struct ethtool_drvinfo drvinf;
616 u32 rate = device_bitrate(ifname);
617 int link = ethtool_link(ifname);
619 memset(&drvinf, 0, sizeof(drvinf));
620 ethtool_drvinf(ifname, &drvinf);
622 memset(buff, 0, sizeof(buff));
623 if (rate)
624 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
625 if (link >= 0)
626 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
627 link == 0 ? "no" : "yes");
629 mvwprintw(screen, (*voff)++, 2,
630 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u/%u"
631 " ",
632 ifname, drvinf.driver, buff, ms_interval, top_cpus, get_number_cpus());
635 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
636 int *voff)
638 attron(A_REVERSE);
640 mvwprintw(screen, (*voff)++, 0,
641 " rx: %16.3llf MiB/t "
642 "%10llu pkts/t "
643 "%10llu drops/t "
644 "%10llu errors/t ",
645 ((long double) rel->rx_bytes) / (1LLU << 20),
646 rel->rx_packets, rel->rx_drops, rel->rx_errors);
648 mvwprintw(screen, (*voff)++, 0,
649 " tx: %16.3llf MiB/t "
650 "%10llu pkts/t "
651 "%10llu drops/t "
652 "%10llu errors/t ",
653 ((long double) rel->tx_bytes) / (1LLU << 20),
654 rel->tx_packets, rel->tx_drops, rel->tx_errors);
656 attroff(A_REVERSE);
659 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
660 int *voff)
662 mvwprintw(screen, (*voff)++, 2,
663 "rx: %16.3llf MiB "
664 "%10llu pkts "
665 "%10llu drops "
666 "%10llu errors",
667 ((long double) abs->rx_bytes) / (1LLU << 20),
668 abs->rx_packets, abs->rx_drops, abs->rx_errors);
670 mvwprintw(screen, (*voff)++, 2,
671 "tx: %16.3llf MiB "
672 "%10llu pkts "
673 "%10llu drops "
674 "%10llu errors",
675 ((long double) abs->tx_bytes) / (1LLU << 20),
676 abs->tx_packets, abs->tx_drops, abs->tx_errors);
679 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
680 const struct ifstat *abs, int *voff)
682 mvwprintw(screen, (*voff)++, 2,
683 "sys: %14u cs/t "
684 "%10.1lf%% mem "
685 "%13u running "
686 "%10u iowait",
687 rel->cswitch,
688 (100.0 * (abs->mem_total - abs->mem_free)) / abs->mem_total,
689 abs->procs_run, abs->procs_iow);
692 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
693 int top_cpus, int *voff)
695 int i;
696 uint64_t all;
697 int max_padd = padding_from_num(get_number_cpus());
699 for (i = 0; i < top_cpus; ++i) {
700 unsigned int idx = cpu_hits[i].idx;
702 all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
703 rel->cpu_idle[idx] + rel->cpu_iow[idx];
705 mvwprintw(screen, (*voff)++, 2,
706 "cpu%*d: %13.1lf%% usr/t "
707 "%9.1lf%% sys/t "
708 "%10.1lf%% idl/t "
709 "%11.1lf%% iow/t ", max_padd, idx,
710 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
711 100.0 * rel->cpu_sys[idx] / all,
712 100.0 * rel->cpu_idle[idx] / all,
713 100.0 * rel->cpu_iow[idx] / all);
717 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
718 int top_cpus, int *voff)
720 int i;
721 int max_padd = padding_from_num(get_number_cpus());
723 for (i = 0; i < top_cpus; ++i) {
724 unsigned int idx = cpu_hits[i].idx;
726 mvwprintw(screen, (*voff)++, 2,
727 "cpu%*d: %14llu irqs/t "
728 "%15llu sirq rx/t "
729 "%15llu sirq tx/t ", max_padd, idx,
730 rel->irqs[idx],
731 rel->irqs_srx[idx],
732 rel->irqs_stx[idx]);
736 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
737 int top_cpus, int *voff)
739 int i;
740 int max_padd = padding_from_num(get_number_cpus());
742 for (i = 0; i < top_cpus; ++i) {
743 unsigned int idx = cpu_hits[i].idx;
745 mvwprintw(screen, (*voff)++, 2,
746 "cpu%*d: %14llu irqs", max_padd, idx,
747 abs->irqs[idx]);
751 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
752 const struct ifstat *abs, int *voff)
754 if (iswireless(abs)) {
755 mvwprintw(screen, (*voff)++, 2,
756 "linkqual: %7d/%d (%d/t) ",
757 abs->wifi.link_qual,
758 abs->wifi.link_qual_max,
759 rel->wifi.link_qual);
761 mvwprintw(screen, (*voff)++, 2,
762 "signal: %8d dBm (%d dBm/t) ",
763 abs->wifi.signal_level,
764 rel->wifi.signal_level);
768 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
769 const struct ifstat *abs, int *first, uint64_t ms_interval,
770 unsigned int top_cpus)
772 int cpus, top, voff = 1, cvoff = 2;
774 curs_set(0);
776 cpus = get_number_cpus();
777 top = min(cpus, top_cpus);
779 stats_top(rel, abs, cpus);
781 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
783 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
785 voff++;
786 screen_net_dev_rel(screen, rel, &voff);
788 voff++;
789 screen_net_dev_abs(screen, abs, &voff);
791 voff++;
792 screen_sys_mem(screen, rel, abs, &voff);
794 voff++;
795 screen_percpu_states(screen, rel, top, &voff);
797 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
799 voff++;
800 screen_percpu_irqs_rel(screen, rel, top, &voff);
802 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
804 voff++;
805 screen_percpu_irqs_abs(screen, abs, top, &voff);
807 voff++;
808 screen_wireless(screen, rel, abs, &voff);
810 if (*first) {
811 mvwprintw(screen, cvoff, 2, "Collecting data ...");
812 *first = 0;
813 } else {
814 mvwprintw(screen, cvoff, 2, " ");
817 wrefresh(screen);
818 refresh();
821 static void screen_end(void)
823 endwin();
826 static int screen_main(const char *ifname, uint64_t ms_interval,
827 unsigned int top_cpus)
829 int first = 1, key;
831 screen_init(&stats_screen);
833 while (!sigint) {
834 key = getch();
835 if (key == 'q' || key == 0x1b || key == KEY_F(10))
836 break;
838 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
839 &first, ms_interval, top_cpus);
841 stats_sample_generic(ifname, ms_interval);
844 screen_end();
846 return 0;
849 static void term_csv(const char *ifname, const struct ifstat *rel,
850 const struct ifstat *abs, uint64_t ms_interval)
852 int cpus, i;
854 printf("%ld ", time(0));
856 printf("%llu ", rel->rx_bytes);
857 printf("%llu ", rel->rx_packets);
858 printf("%llu ", rel->rx_drops);
859 printf("%llu ", rel->rx_errors);
861 printf("%llu ", abs->rx_bytes);
862 printf("%llu ", abs->rx_packets);
863 printf("%llu ", abs->rx_drops);
864 printf("%llu ", abs->rx_errors);
866 printf("%llu ", rel->tx_bytes);
867 printf("%llu ", rel->tx_packets);
868 printf("%llu ", rel->tx_drops);
869 printf("%llu ", rel->tx_errors);
871 printf("%llu ", abs->tx_bytes);
872 printf("%llu ", abs->tx_packets);
873 printf("%llu ", abs->tx_drops);
874 printf("%llu ", abs->tx_errors);
876 printf("%u ", rel->cswitch);
877 printf("%lu ", abs->mem_free);
878 printf("%lu ", abs->mem_total - abs->mem_free);
879 printf("%lu ", abs->mem_total);
880 printf("%u ", abs->procs_run);
881 printf("%u ", abs->procs_iow);
883 cpus = get_number_cpus();
885 for (i = 0; i < cpus; ++i) {
886 printf("%lu ", rel->cpu_user[i]);
887 printf("%lu ", rel->cpu_nice[i]);
888 printf("%lu ", rel->cpu_sys[i]);
889 printf("%lu ", rel->cpu_idle[i]);
890 printf("%lu ", rel->cpu_iow[i]);
892 printf("%llu ", rel->irqs[i]);
893 printf("%llu ", abs->irqs[i]);
895 printf("%llu ", rel->irqs_srx[i]);
896 printf("%llu ", abs->irqs_srx[i]);
898 printf("%llu ", rel->irqs_stx[i]);
899 printf("%llu ", abs->irqs_stx[i]);
902 if (iswireless(abs)) {
903 printf("%u ", rel->wifi.link_qual);
904 printf("%u ", abs->wifi.link_qual);
905 printf("%u ", abs->wifi.link_qual_max);
907 printf("%d ", rel->wifi.signal_level);
908 printf("%d ", abs->wifi.signal_level);
911 puts("");
912 fflush(stdout);
915 static void term_csv_header(const char *ifname, const struct ifstat *abs,
916 uint64_t ms_interval)
918 int cpus, i, j = 1;
920 printf("# gnuplot dump (#col:description)\n");
921 printf("# networking interface: %s\n", ifname);
922 printf("# sampling interval (t): %lu ms\n", ms_interval);
923 printf("# %d:unixtime ", j++);
925 printf("%d:rx-bytes-per-t ", j++);
926 printf("%d:rx-pkts-per-t ", j++);
927 printf("%d:rx-drops-per-t ", j++);
928 printf("%d:rx-errors-per-t ", j++);
930 printf("%d:rx-bytes ", j++);
931 printf("%d:rx-pkts ", j++);
932 printf("%d:rx-drops ", j++);
933 printf("%d:rx-errors ", j++);
935 printf("%d:tx-bytes-per-t ", j++);
936 printf("%d:tx-pkts-per-t ", j++);
937 printf("%d:tx-drops-per-t ", j++);
938 printf("%d:tx-errors-per-t ", j++);
940 printf("%d:tx-bytes ", j++);
941 printf("%d:tx-pkts ", j++);
942 printf("%d:tx-drops ", j++);
943 printf("%d:tx-errors ", j++);
945 printf("%d:context-switches-per-t ", j++);
946 printf("%d:mem-free ", j++);
947 printf("%d:mem-used ", j++);
948 printf("%d:mem-total ", j++);
949 printf("%d:procs-in-run ", j++);
950 printf("%d:procs-in-iow ", j++);
952 cpus = get_number_cpus();
954 for (i = 0, j = 22; i < cpus; ++i) {
955 printf("%d:cpu%i-usr-per-t ", j++, i);
956 printf("%d:cpu%i-nice-per-t ", j++, i);
957 printf("%d:cpu%i-sys-per-t ", j++, i);
958 printf("%d:cpu%i-idle-per-t ", j++, i);
959 printf("%d:cpu%i-iow-per-t ", j++, i);
961 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
962 printf("%d:cpu%i-net-irqs ", j++, i);
964 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
965 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
966 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
967 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
970 if (iswireless(abs)) {
971 printf("%d:wifi-link-qual-per-t ", j++);
972 printf("%d:wifi-link-qual ", j++);
973 printf("%d:wifi-link-qual-max ", j++);
975 printf("%d:wifi-signal-dbm-per-t ", j++);
976 printf("%d:wifi-signal-dbm ", j++);
979 puts("");
980 printf("# data:\n");
981 fflush(stdout);
984 static int term_main(const char *ifname, uint64_t ms_interval,
985 unsigned int top_cpus __maybe_unused)
987 int first = 1;
989 do {
990 stats_sample_generic(ifname, ms_interval);
992 if (first) {
993 first = 0;
994 term_csv_header(ifname, &stats_new, ms_interval);
997 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
998 } while (stats_loop && !sigint);
1000 return 0;
1003 int main(int argc, char **argv)
1005 short ifflags = 0;
1006 int c, opt_index, ret, cpus, promisc = 0;
1007 unsigned int top_cpus = 10;
1008 uint64_t interval = 1000;
1009 char *ifname = NULL;
1010 int (*func_main)(const char *ifname, uint64_t ms_interval,
1011 unsigned int top_cpus) = screen_main;
1013 setfsuid(getuid());
1014 setfsgid(getgid());
1016 while ((c = getopt_long(argc, argv, short_options, long_options,
1017 &opt_index)) != EOF) {
1018 switch (c) {
1019 case 'h':
1020 help();
1021 break;
1022 case 'v':
1023 version();
1024 break;
1025 case 'd':
1026 ifname = xstrndup(optarg, IFNAMSIZ);
1027 break;
1028 case 't':
1029 interval = strtoul(optarg, NULL, 10);
1030 break;
1031 case 'n':
1032 top_cpus = strtoul(optarg, NULL, 10);
1033 break;
1034 case 'l':
1035 stats_loop = 1;
1036 break;
1037 case 'p':
1038 promisc = 1;
1039 break;
1040 case 'c':
1041 func_main = term_main;
1042 break;
1043 case '?':
1044 switch (optopt) {
1045 case 'd':
1046 case 't':
1047 panic("Option -%c requires an argument!\n",
1048 optopt);
1049 default:
1050 if (isprint(optopt))
1051 printf("Unknown option character `0x%X\'!\n", optopt);
1052 die();
1054 default:
1055 break;
1059 if (argc == 1)
1060 help();
1062 if (argc == 2)
1063 ifname = xstrndup(argv[1], IFNAMSIZ);
1064 if (ifname == NULL)
1065 panic("No networking device given!\n");
1067 if (!strncmp("lo", ifname, IFNAMSIZ))
1068 panic("lo is not supported!\n");
1069 if (device_mtu(ifname) == 0)
1070 panic("This is no networking device!\n");
1072 register_signal(SIGINT, signal_handler);
1073 register_signal(SIGHUP, signal_handler);
1075 cpus = get_number_cpus();
1076 top_cpus = min(top_cpus, cpus);
1078 stats_alloc(&stats_old, cpus);
1079 stats_alloc(&stats_new, cpus);
1080 stats_alloc(&stats_delta, cpus);
1082 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1084 if (promisc)
1085 ifflags = enter_promiscuous_mode(ifname);
1086 ret = func_main(ifname, interval, top_cpus);
1087 if (promisc)
1088 leave_promiscuous_mode(ifname, ifflags);
1090 xfree(ifname);
1091 return ret;