ifpps: Always display the least heavy hitter CPU
[netsniff-ng.git] / ifpps.c
blobcc659b26d32ea6138ec472fae3b43bca3cb880be
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 "dev.h"
22 #include "sig.h"
23 #include "link.h"
24 #include "xmalloc.h"
25 #include "ioops.h"
26 #include "promisc.h"
27 #include "cpus.h"
28 #include "built_in.h"
30 struct wifi_stat {
31 uint32_t bitrate;
32 int16_t link_qual, link_qual_max;
33 int signal_level /*, noise_level*/;
36 struct ifstat {
37 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
38 long long unsigned int rx_fifo, rx_frame, rx_multi;
39 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
40 long long unsigned int tx_fifo, tx_colls, tx_carrier;
41 uint64_t mem_free, mem_total;
42 uint32_t irq_nr, procs_run, procs_iow, cswitch, forks;
43 struct wifi_stat wifi;
45 * Pointer members need to be last in order for stats_zero() to work
46 * properly.
48 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
49 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
52 struct cpu_hit {
53 unsigned int idx;
54 uint64_t hit;
55 long long unsigned int irqs_rel, irqs_abs;
58 static volatile sig_atomic_t sigint = 0;
59 static struct ifstat stats_old, stats_new, stats_delta;
60 static struct cpu_hit *cpu_hits;
61 static int stats_loop = 0;
62 static WINDOW *stats_screen = NULL;
64 static const char *short_options = "d:t:n:vhclp";
65 static const struct option long_options[] = {
66 {"dev", required_argument, NULL, 'd'},
67 {"interval", required_argument, NULL, 't'},
68 {"num-cpus", required_argument, NULL, 'n'},
69 {"promisc", no_argument, NULL, 'p'},
70 {"csv", no_argument, NULL, 'c'},
71 {"loop", no_argument, NULL, 'l'},
72 {"version", no_argument, NULL, 'v'},
73 {"help", no_argument, NULL, 'h'},
74 {NULL, 0, NULL, 0}
77 static void signal_handler(int number)
79 switch (number) {
80 case SIGINT:
81 sigint = 1;
82 break;
83 case SIGHUP:
84 default:
85 break;
89 static inline int iswireless(const struct ifstat *stats)
91 return stats->wifi.bitrate > 0;
94 static void __noreturn help(void)
96 printf("\nifpps %s, top-like kernel networking and system statistics\n",
97 VERSION_STRING);
98 puts("http://www.netsniff-ng.org\n\n"
99 "Usage: ifpps [options] || ifpps <netdev>\n"
100 "Options:\n"
101 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
102 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
103 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
104 " in ncurses mode (default 10)\n"
105 " -p|--promisc Promiscuous mode\n"
106 " -c|--csv Output to terminal as Gnuplot-ready data\n"
107 " -l|--loop Continuous CSV output\n"
108 " -v|--version Print version and exit\n"
109 " -h|--help Print this help and exit\n\n"
110 "Examples:\n"
111 " ifpps eth0\n"
112 " ifpps -pd eth0\n"
113 " ifpps -lpcd wlan0 > plot.dat\n\n"
114 "Note:\n"
115 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
116 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
117 "Please report bugs to <bugs@netsniff-ng.org>\n"
118 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
119 "Swiss federal institute of technology (ETH Zurich)\n"
120 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
121 "License: GNU GPL version 2.0\n"
122 "This is free software: you are free to change and redistribute it.\n"
123 "There is NO WARRANTY, to the extent permitted by law.\n");
124 die();
127 static void __noreturn version(void)
129 printf("\nifpps %s, top-like kernel networking and system statistics\n",
130 VERSION_LONG);
131 puts("http://www.netsniff-ng.org\n\n"
132 "Please report bugs to <bugs@netsniff-ng.org>\n"
133 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
134 "Swiss federal institute of technology (ETH Zurich)\n"
135 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
136 "License: GNU GPL version 2.0\n"
137 "This is free software: you are free to change and redistribute it.\n"
138 "There is NO WARRANTY, to the extent permitted by law.\n");
139 die();
142 static inline int padding_from_num(int n)
144 int i = 0;
145 do i++;
146 while ((n /= 10) > 0);
147 return i;
150 #define STATS_ALLOC1(member) \
151 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
153 static void stats_alloc(struct ifstat *stats, int cpus)
155 STATS_ALLOC1(irqs);
156 STATS_ALLOC1(irqs_srx);
157 STATS_ALLOC1(irqs_stx);
159 STATS_ALLOC1(cpu_user);
160 STATS_ALLOC1(cpu_sys);
161 STATS_ALLOC1(cpu_nice);
162 STATS_ALLOC1(cpu_idle);
163 STATS_ALLOC1(cpu_iow);
166 #define STATS_ZERO1(member) \
167 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
169 static void stats_zero(struct ifstat *stats, int cpus)
171 /* Only clear the non-pointer members */
172 memset(stats, 0, offsetof(struct ifstat, irqs));
174 STATS_ZERO1(irqs);
175 STATS_ZERO1(irqs_srx);
176 STATS_ZERO1(irqs_stx);
178 STATS_ZERO1(cpu_user);
179 STATS_ZERO1(cpu_sys);
180 STATS_ZERO1(cpu_nice);
181 STATS_ZERO1(cpu_idle);
182 STATS_ZERO1(cpu_iow);
185 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
187 int ret = -EINVAL;
188 char buff[256];
189 FILE *fp;
191 fp = fopen("/proc/net/dev", "r");
192 if (!fp)
193 panic("Cannot open /proc/net/dev!\n");
195 if (fgets(buff, sizeof(buff), fp)) { ; }
196 if (fgets(buff, sizeof(buff), fp)) { ; }
198 memset(buff, 0, sizeof(buff));
200 while (fgets(buff, sizeof(buff), fp) != NULL) {
201 buff[sizeof(buff) -1] = 0;
203 if (strstr(buff, ifname) == NULL)
204 continue;
206 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
207 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
208 &stats->rx_bytes, &stats->rx_packets,
209 &stats->rx_errors, &stats->rx_drops,
210 &stats->rx_fifo, &stats->rx_frame,
211 &stats->rx_multi, &stats->tx_bytes,
212 &stats->tx_packets, &stats->tx_errors,
213 &stats->tx_drops, &stats->tx_fifo,
214 &stats->tx_colls, &stats->tx_carrier) == 14) {
215 ret = 0;
216 break;
219 memset(buff, 0, sizeof(buff));
222 fclose(fp);
223 return ret;
226 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
228 int ret = -EINVAL, i, cpus, try = 0;
229 char *ptr, *buff;
230 bool seen = false;
231 size_t buff_len;
232 struct ethtool_drvinfo drvinf;
233 FILE *fp;
235 fp = fopen("/proc/interrupts", "r");
236 if (!fp)
237 panic("Cannot open /proc/interrupts!\n");
239 cpus = get_number_cpus();
240 buff_len = cpus * 128;
241 buff = xmalloc(buff_len);
242 retry:
243 fseek(fp, 0, SEEK_SET);
244 memset(buff, 0, buff_len);
246 while (fgets(buff, buff_len, fp) != NULL) {
247 buff[buff_len - 1] = 0;
248 ptr = buff;
250 if (strstr(buff, ifname) == NULL)
251 continue;
253 /* XXX: remove this one here */
254 stats->irq_nr = strtol(ptr, &ptr, 10);
255 bug_on(stats->irq_nr == 0);
257 if (ptr)
258 ptr++;
259 for (i = 0; i < cpus && ptr; ++i) {
260 if (seen)
261 stats->irqs[i] += strtol(ptr, &ptr, 10);
262 else
263 stats->irqs[i] = strtol(ptr, &ptr, 10);
264 if (i == cpus - 1) {
265 ret = 0;
266 seen = true;
270 memset(buff, 0, buff_len);
273 if (ret == -EINVAL && try == 0) {
274 memset(&drvinf, 0, sizeof(drvinf));
275 if (ethtool_drvinf(ifname, &drvinf) < 0)
276 goto done;
278 ifname = drvinf.driver;
279 try++;
281 goto retry;
283 done:
284 xfree(buff);
285 fclose(fp);
286 return ret;
289 static int stats_proc_softirqs(struct ifstat *stats)
291 int i, cpus;
292 char *ptr, *buff;
293 size_t buff_len;
294 FILE *fp;
295 enum {
296 softirqs_net_rx,
297 softirqs_net_tx,
298 } net_type;
300 fp = fopen("/proc/softirqs", "r");
301 if (!fp)
302 panic("Cannot open /proc/softirqs!\n");
304 cpus = get_number_cpus();
305 buff_len = cpus * 128;
306 buff = xmalloc(buff_len);
308 memset(buff, 0, buff_len);
310 while (fgets(buff, buff_len, fp) != NULL) {
311 buff[buff_len - 1] = 0;
313 if ((ptr = strstr(buff, "NET_TX:")))
314 net_type = softirqs_net_tx;
315 else if ((ptr = strstr(buff, "NET_RX:")))
316 net_type = softirqs_net_rx;
317 else
318 continue;
320 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
321 switch (net_type) {
322 case softirqs_net_tx:
323 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
324 break;
325 case softirqs_net_rx:
326 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
327 break;
331 memset(buff, 0, buff_len);
334 xfree(buff);
335 fclose(fp);
336 return 0;
339 static int stats_proc_memory(struct ifstat *stats)
341 char *ptr, buff[256];
342 FILE *fp;
344 fp = fopen("/proc/meminfo", "r");
345 if (!fp)
346 panic("Cannot open /proc/meminfo!\n");
348 memset(buff, 0, sizeof(buff));
350 while (fgets(buff, sizeof(buff), fp) != NULL) {
351 buff[sizeof(buff) - 1] = 0;
353 if ((ptr = strstr(buff, "MemTotal:"))) {
354 ptr += strlen("MemTotal:");
355 stats->mem_total = strtoul(ptr, &ptr, 10);
356 } else if ((ptr = strstr(buff, "MemFree:"))) {
357 ptr += strlen("MemFree:");
358 stats->mem_free = strtoul(ptr, &ptr, 10);
361 memset(buff, 0, sizeof(buff));
364 fclose(fp);
365 return 0;
368 static int stats_proc_system(struct ifstat *stats)
370 int cpu, cpus;
371 char *ptr, buff[256];
372 FILE *fp;
374 fp = fopen("/proc/stat", "r");
375 if (!fp)
376 panic("Cannot open /proc/stat!\n");
378 cpus = get_number_cpus();
380 memset(buff, 0, sizeof(buff));
382 while (fgets(buff, sizeof(buff), fp) != NULL) {
383 buff[sizeof(buff) - 1] = 0;
385 if ((ptr = strstr(buff, "cpu"))) {
386 ptr += strlen("cpu");
387 if (isblank(*ptr))
388 goto next;
390 cpu = strtol(ptr, &ptr, 10);
391 bug_on(cpu > cpus);
393 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
394 &stats->cpu_user[cpu],
395 &stats->cpu_nice[cpu],
396 &stats->cpu_sys[cpu],
397 &stats->cpu_idle[cpu],
398 &stats->cpu_iow[cpu]) != 5)
399 goto next;
400 } else if ((ptr = strstr(buff, "ctxt"))) {
401 ptr += strlen("ctxt");
402 stats->cswitch = strtoul(ptr, &ptr, 10);
403 } else if ((ptr = strstr(buff, "processes"))) {
404 ptr += strlen("processes");
405 stats->forks = strtoul(ptr, &ptr, 10);
406 } else if ((ptr = strstr(buff, "procs_running"))) {
407 ptr += strlen("procs_running");
408 stats->procs_run = strtoul(ptr, &ptr, 10);
409 } else if ((ptr = strstr(buff, "procs_blocked"))) {
410 ptr += strlen("procs_blocked");
411 stats->procs_iow = strtoul(ptr, &ptr, 10);
413 next:
414 memset(buff, 0, sizeof(buff));
417 fclose(fp);
418 return 0;
421 static int adjust_dbm_level(int in_dbm, int dbm_val)
423 if (!in_dbm)
424 return dbm_val;
426 return dbm_val - 0x100;
429 static int stats_wireless(const char *ifname, struct ifstat *stats)
431 int ret;
432 struct iw_statistics ws;
434 ret = wireless_sigqual(ifname, &ws);
435 if (ret != 0) {
436 stats->wifi.bitrate = 0;
437 return -EINVAL;
440 stats->wifi.bitrate = wireless_bitrate(ifname);
442 stats->wifi.signal_level =
443 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
445 stats->wifi.link_qual = ws.qual.qual;
446 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
448 return ret;
451 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
452 #define DIFF(member) do { \
453 if (sizeof(diff->member) != sizeof(new->member) || \
454 sizeof(diff->member) != sizeof(old->member)) \
455 bug(); \
456 bug_on((new->member - old->member) > (new->member)); \
457 DIFF1(member); \
458 } while (0)
460 static void stats_diff(struct ifstat *old, struct ifstat *new,
461 struct ifstat *diff)
463 int cpus, i;
465 DIFF(rx_bytes);
466 DIFF(rx_packets);
467 DIFF(rx_drops);
468 DIFF(rx_errors);
469 DIFF(rx_fifo);
470 DIFF(rx_frame);
471 DIFF(rx_multi);
473 DIFF(tx_bytes);
474 DIFF(tx_bytes);
475 DIFF(tx_packets);
476 DIFF(tx_drops);
477 DIFF(tx_errors);
478 DIFF(tx_fifo);
479 DIFF(tx_colls);
480 DIFF(tx_carrier);
482 DIFF1(procs_run);
483 DIFF1(procs_iow);
485 DIFF1(wifi.signal_level);
486 DIFF1(wifi.link_qual);
488 DIFF1(cswitch);
489 DIFF1(forks);
491 cpus = get_number_cpus();
493 for (i = 0; i < cpus; ++i) {
494 DIFF(irqs[i]);
495 DIFF(irqs_srx[i]);
496 DIFF(irqs_stx[i]);
498 DIFF1(cpu_user[i]);
499 DIFF1(cpu_nice[i]);
500 DIFF1(cpu_sys[i]);
501 DIFF1(cpu_idle[i]);
502 DIFF1(cpu_iow[i]);
506 static void stats_fetch(const char *ifname, struct ifstat *stats)
508 if (stats_proc_net_dev(ifname, stats) < 0)
509 panic("Cannot fetch device stats!\n");
510 if (stats_proc_softirqs(stats) < 0)
511 panic("Cannot fetch software interrupts!\n");
512 if (stats_proc_memory(stats) < 0)
513 panic("Cannot fetch memory stats!\n");
514 if (stats_proc_system(stats) < 0)
515 panic("Cannot fetch system stats!\n");
517 stats_proc_interrupts((char *) ifname, stats);
519 stats_wireless(ifname, stats);
522 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
524 int cpus = get_number_cpus();
526 stats_zero(&stats_old, cpus);
527 stats_zero(&stats_new, cpus);
528 stats_zero(&stats_delta, cpus);
530 stats_fetch(ifname, &stats_old);
531 usleep(ms_interval * 1000);
532 stats_fetch(ifname, &stats_new);
534 stats_diff(&stats_old, &stats_new, &stats_delta);
537 static int cmp_hits(const void *p1, const void *p2)
539 const struct cpu_hit *h1 = p1, *h2 = p2;
542 * We want the hits sorted in descending order, thus reverse the return
543 * values.
545 if (h1->hit == h2->hit)
546 return 0;
547 else if (h1->hit < h2->hit)
548 return 1;
549 else
550 return -1;
553 static int cmp_irqs_rel(const void *p1, const void *p2)
555 const struct cpu_hit *h1 = p1, *h2 = p2;
558 * We want the hits sorted in descending order, thus reverse the return
559 * values.
561 if (h1->irqs_rel == h2->irqs_rel)
562 return 0;
563 else if (h1->irqs_rel < h2->irqs_rel)
564 return 1;
565 else
566 return -1;
569 static int cmp_irqs_abs(const void *p1, const void *p2)
571 const struct cpu_hit *h1 = p1, *h2 = p2;
574 * We want the hits sorted in descending order, thus reverse the return
575 * values.
577 if (h1->irqs_abs == h2->irqs_abs)
578 return 0;
579 else if (h1->irqs_abs < h2->irqs_abs)
580 return 1;
581 else
582 return -1;
585 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
586 int top_cpus)
588 int i;
590 for (i = 0; i < top_cpus; ++i) {
591 cpu_hits[i].idx = i;
592 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
593 cpu_hits[i].irqs_rel = rel->irqs[i];
594 cpu_hits[i].irqs_abs = abs->irqs[i];
598 static void screen_init(WINDOW **screen)
600 (*screen) = initscr();
602 raw();
603 noecho();
604 cbreak();
605 nodelay((*screen), TRUE);
607 keypad(stdscr, TRUE);
609 refresh();
610 wrefresh((*screen));
613 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
614 uint64_t ms_interval, unsigned int top_cpus)
616 size_t len = 0;
617 char buff[64];
618 struct ethtool_drvinfo drvinf;
619 u32 rate = device_bitrate(ifname);
620 int link = ethtool_link(ifname);
621 unsigned int cpus = get_number_cpus();
623 memset(&drvinf, 0, sizeof(drvinf));
624 ethtool_drvinf(ifname, &drvinf);
626 memset(buff, 0, sizeof(buff));
627 if (rate)
628 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
629 if (link >= 0)
630 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
631 link == 0 ? "no" : "yes");
633 mvwprintw(screen, (*voff)++, 2,
634 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u%s/%u"
635 " ",
636 ifname, drvinf.driver, buff, ms_interval, top_cpus,
637 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
640 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
641 int *voff)
643 attron(A_REVERSE);
645 mvwprintw(screen, (*voff)++, 0,
646 " rx: %16.3llf MiB/t "
647 "%10llu pkts/t "
648 "%10llu drops/t "
649 "%10llu errors/t ",
650 ((long double) rel->rx_bytes) / (1LLU << 20),
651 rel->rx_packets, rel->rx_drops, rel->rx_errors);
653 mvwprintw(screen, (*voff)++, 0,
654 " tx: %16.3llf MiB/t "
655 "%10llu pkts/t "
656 "%10llu drops/t "
657 "%10llu errors/t ",
658 ((long double) rel->tx_bytes) / (1LLU << 20),
659 rel->tx_packets, rel->tx_drops, rel->tx_errors);
661 attroff(A_REVERSE);
664 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
665 int *voff)
667 mvwprintw(screen, (*voff)++, 2,
668 "rx: %16.3llf MiB "
669 "%10llu pkts "
670 "%10llu drops "
671 "%10llu errors",
672 ((long double) abs->rx_bytes) / (1LLU << 20),
673 abs->rx_packets, abs->rx_drops, abs->rx_errors);
675 mvwprintw(screen, (*voff)++, 2,
676 "tx: %16.3llf MiB "
677 "%10llu pkts "
678 "%10llu drops "
679 "%10llu errors",
680 ((long double) abs->tx_bytes) / (1LLU << 20),
681 abs->tx_packets, abs->tx_drops, abs->tx_errors);
684 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
685 const struct ifstat *abs, int *voff)
687 mvwprintw(screen, (*voff)++, 2,
688 "sys: %14u cs/t "
689 "%10.1lf%% mem "
690 "%13u running "
691 "%10u iowait",
692 rel->cswitch,
693 (100.0 * (abs->mem_total - abs->mem_free)) / abs->mem_total,
694 abs->procs_run, abs->procs_iow);
697 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
698 int *voff, unsigned int idx, char *tag)
700 int max_padd = padding_from_num(get_number_cpus());
701 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
702 rel->cpu_idle[idx] + rel->cpu_iow[idx];
704 mvwprintw(screen, (*voff)++, 2,
705 "cpu%*d%s:%s %13.1lf%% usr/t "
706 "%9.1lf%% sys/t "
707 "%10.1lf%% idl/t "
708 "%11.1lf%% iow/t ", max_padd, idx,
709 tag, strlen(tag) == 0 ? " " : "",
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);
716 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
717 int top_cpus, int *voff)
719 int i;
720 int cpus = get_number_cpus();
722 if (top_cpus == 0)
723 return;
725 /* Display top hitter */
726 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
728 /* Make sure we don't display the min. hitter twice */
729 if (top_cpus == cpus)
730 top_cpus--;
732 for (i = 1; i < top_cpus; ++i) {
733 unsigned int idx = cpu_hits[i].idx;
735 screen_percpu_states_one(screen, rel, voff, idx, "");
738 /* Display minimum hitter */
739 if (cpus != 1)
740 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
743 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
744 int top_cpus, int *voff)
746 int i;
747 int max_padd = padding_from_num(get_number_cpus());
749 for (i = 0; i < top_cpus; ++i) {
750 unsigned int idx = cpu_hits[i].idx;
752 mvwprintw(screen, (*voff)++, 2,
753 "cpu%*d: %14llu irqs/t "
754 "%15llu sirq rx/t "
755 "%15llu sirq tx/t ", max_padd, idx,
756 rel->irqs[idx],
757 rel->irqs_srx[idx],
758 rel->irqs_stx[idx]);
762 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
763 int top_cpus, int *voff)
765 int i;
766 int max_padd = padding_from_num(get_number_cpus());
768 for (i = 0; i < top_cpus; ++i) {
769 unsigned int idx = cpu_hits[i].idx;
771 mvwprintw(screen, (*voff)++, 2,
772 "cpu%*d: %14llu irqs", max_padd, idx,
773 abs->irqs[idx]);
777 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
778 const struct ifstat *abs, int *voff)
780 if (iswireless(abs)) {
781 mvwprintw(screen, (*voff)++, 2,
782 "linkqual: %7d/%d (%d/t) ",
783 abs->wifi.link_qual,
784 abs->wifi.link_qual_max,
785 rel->wifi.link_qual);
787 mvwprintw(screen, (*voff)++, 2,
788 "signal: %8d dBm (%d dBm/t) ",
789 abs->wifi.signal_level,
790 rel->wifi.signal_level);
794 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
795 const struct ifstat *abs, int *first, uint64_t ms_interval,
796 unsigned int top_cpus)
798 int cpus, top, voff = 1, cvoff = 2;
800 curs_set(0);
802 cpus = get_number_cpus();
803 top = min(cpus, top_cpus);
805 stats_top(rel, abs, cpus);
807 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
809 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
811 voff++;
812 screen_net_dev_rel(screen, rel, &voff);
814 voff++;
815 screen_net_dev_abs(screen, abs, &voff);
817 voff++;
818 screen_sys_mem(screen, rel, abs, &voff);
820 voff++;
821 screen_percpu_states(screen, rel, top, &voff);
823 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
825 voff++;
826 screen_percpu_irqs_rel(screen, rel, top, &voff);
828 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
830 voff++;
831 screen_percpu_irqs_abs(screen, abs, top, &voff);
833 voff++;
834 screen_wireless(screen, rel, abs, &voff);
836 if (*first) {
837 mvwprintw(screen, cvoff, 2, "Collecting data ...");
838 *first = 0;
839 } else {
840 mvwprintw(screen, cvoff, 2, " ");
843 wrefresh(screen);
844 refresh();
847 static void screen_end(void)
849 endwin();
852 static int screen_main(const char *ifname, uint64_t ms_interval,
853 unsigned int top_cpus)
855 int first = 1, key;
857 screen_init(&stats_screen);
859 while (!sigint) {
860 key = getch();
861 if (key == 'q' || key == 0x1b || key == KEY_F(10))
862 break;
864 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
865 &first, ms_interval, top_cpus);
867 stats_sample_generic(ifname, ms_interval);
870 screen_end();
872 return 0;
875 static void term_csv(const char *ifname, const struct ifstat *rel,
876 const struct ifstat *abs, uint64_t ms_interval)
878 int cpus, i;
880 printf("%ld ", time(0));
882 printf("%llu ", rel->rx_bytes);
883 printf("%llu ", rel->rx_packets);
884 printf("%llu ", rel->rx_drops);
885 printf("%llu ", rel->rx_errors);
887 printf("%llu ", abs->rx_bytes);
888 printf("%llu ", abs->rx_packets);
889 printf("%llu ", abs->rx_drops);
890 printf("%llu ", abs->rx_errors);
892 printf("%llu ", rel->tx_bytes);
893 printf("%llu ", rel->tx_packets);
894 printf("%llu ", rel->tx_drops);
895 printf("%llu ", rel->tx_errors);
897 printf("%llu ", abs->tx_bytes);
898 printf("%llu ", abs->tx_packets);
899 printf("%llu ", abs->tx_drops);
900 printf("%llu ", abs->tx_errors);
902 printf("%u ", rel->cswitch);
903 printf("%lu ", abs->mem_free);
904 printf("%lu ", abs->mem_total - abs->mem_free);
905 printf("%lu ", abs->mem_total);
906 printf("%u ", abs->procs_run);
907 printf("%u ", abs->procs_iow);
909 cpus = get_number_cpus();
911 for (i = 0; i < cpus; ++i) {
912 printf("%lu ", rel->cpu_user[i]);
913 printf("%lu ", rel->cpu_nice[i]);
914 printf("%lu ", rel->cpu_sys[i]);
915 printf("%lu ", rel->cpu_idle[i]);
916 printf("%lu ", rel->cpu_iow[i]);
918 printf("%llu ", rel->irqs[i]);
919 printf("%llu ", abs->irqs[i]);
921 printf("%llu ", rel->irqs_srx[i]);
922 printf("%llu ", abs->irqs_srx[i]);
924 printf("%llu ", rel->irqs_stx[i]);
925 printf("%llu ", abs->irqs_stx[i]);
928 if (iswireless(abs)) {
929 printf("%u ", rel->wifi.link_qual);
930 printf("%u ", abs->wifi.link_qual);
931 printf("%u ", abs->wifi.link_qual_max);
933 printf("%d ", rel->wifi.signal_level);
934 printf("%d ", abs->wifi.signal_level);
937 puts("");
938 fflush(stdout);
941 static void term_csv_header(const char *ifname, const struct ifstat *abs,
942 uint64_t ms_interval)
944 int cpus, i, j = 1;
946 printf("# gnuplot dump (#col:description)\n");
947 printf("# networking interface: %s\n", ifname);
948 printf("# sampling interval (t): %lu ms\n", ms_interval);
949 printf("# %d:unixtime ", j++);
951 printf("%d:rx-bytes-per-t ", j++);
952 printf("%d:rx-pkts-per-t ", j++);
953 printf("%d:rx-drops-per-t ", j++);
954 printf("%d:rx-errors-per-t ", j++);
956 printf("%d:rx-bytes ", j++);
957 printf("%d:rx-pkts ", j++);
958 printf("%d:rx-drops ", j++);
959 printf("%d:rx-errors ", j++);
961 printf("%d:tx-bytes-per-t ", j++);
962 printf("%d:tx-pkts-per-t ", j++);
963 printf("%d:tx-drops-per-t ", j++);
964 printf("%d:tx-errors-per-t ", j++);
966 printf("%d:tx-bytes ", j++);
967 printf("%d:tx-pkts ", j++);
968 printf("%d:tx-drops ", j++);
969 printf("%d:tx-errors ", j++);
971 printf("%d:context-switches-per-t ", j++);
972 printf("%d:mem-free ", j++);
973 printf("%d:mem-used ", j++);
974 printf("%d:mem-total ", j++);
975 printf("%d:procs-in-run ", j++);
976 printf("%d:procs-in-iow ", j++);
978 cpus = get_number_cpus();
980 for (i = 0, j = 22; i < cpus; ++i) {
981 printf("%d:cpu%i-usr-per-t ", j++, i);
982 printf("%d:cpu%i-nice-per-t ", j++, i);
983 printf("%d:cpu%i-sys-per-t ", j++, i);
984 printf("%d:cpu%i-idle-per-t ", j++, i);
985 printf("%d:cpu%i-iow-per-t ", j++, i);
987 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
988 printf("%d:cpu%i-net-irqs ", j++, i);
990 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
991 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
992 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
993 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
996 if (iswireless(abs)) {
997 printf("%d:wifi-link-qual-per-t ", j++);
998 printf("%d:wifi-link-qual ", j++);
999 printf("%d:wifi-link-qual-max ", j++);
1001 printf("%d:wifi-signal-dbm-per-t ", j++);
1002 printf("%d:wifi-signal-dbm ", j++);
1005 puts("");
1006 printf("# data:\n");
1007 fflush(stdout);
1010 static int term_main(const char *ifname, uint64_t ms_interval,
1011 unsigned int top_cpus __maybe_unused)
1013 int first = 1;
1015 do {
1016 stats_sample_generic(ifname, ms_interval);
1018 if (first) {
1019 first = 0;
1020 term_csv_header(ifname, &stats_new, ms_interval);
1023 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1024 } while (stats_loop && !sigint);
1026 return 0;
1029 int main(int argc, char **argv)
1031 short ifflags = 0;
1032 int c, opt_index, ret, cpus, promisc = 0;
1033 unsigned int top_cpus = 10;
1034 uint64_t interval = 1000;
1035 char *ifname = NULL;
1036 int (*func_main)(const char *ifname, uint64_t ms_interval,
1037 unsigned int top_cpus) = screen_main;
1039 setfsuid(getuid());
1040 setfsgid(getgid());
1042 while ((c = getopt_long(argc, argv, short_options, long_options,
1043 &opt_index)) != EOF) {
1044 switch (c) {
1045 case 'h':
1046 help();
1047 break;
1048 case 'v':
1049 version();
1050 break;
1051 case 'd':
1052 ifname = xstrndup(optarg, IFNAMSIZ);
1053 break;
1054 case 't':
1055 interval = strtoul(optarg, NULL, 10);
1056 break;
1057 case 'n':
1058 top_cpus = strtoul(optarg, NULL, 10);
1059 break;
1060 case 'l':
1061 stats_loop = 1;
1062 break;
1063 case 'p':
1064 promisc = 1;
1065 break;
1066 case 'c':
1067 func_main = term_main;
1068 break;
1069 case '?':
1070 switch (optopt) {
1071 case 'd':
1072 case 't':
1073 panic("Option -%c requires an argument!\n",
1074 optopt);
1075 default:
1076 if (isprint(optopt))
1077 printf("Unknown option character `0x%X\'!\n", optopt);
1078 die();
1080 default:
1081 break;
1085 if (argc == 1)
1086 help();
1088 if (argc == 2)
1089 ifname = xstrndup(argv[1], IFNAMSIZ);
1090 if (ifname == NULL)
1091 panic("No networking device given!\n");
1093 if (!strncmp("lo", ifname, IFNAMSIZ))
1094 panic("lo is not supported!\n");
1095 if (device_mtu(ifname) == 0)
1096 panic("This is no networking device!\n");
1098 register_signal(SIGINT, signal_handler);
1099 register_signal(SIGHUP, signal_handler);
1101 cpus = get_number_cpus();
1102 top_cpus = min(top_cpus, cpus);
1104 stats_alloc(&stats_old, cpus);
1105 stats_alloc(&stats_new, cpus);
1106 stats_alloc(&stats_delta, cpus);
1108 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1110 if (promisc)
1111 ifflags = enter_promiscuous_mode(ifname);
1112 ret = func_main(ifname, interval, top_cpus);
1113 if (promisc)
1114 leave_promiscuous_mode(ifname, ifflags);
1116 xfree(ifname);
1117 return ret;