cpus: break out cpu related functions from xutils
[netsniff-ng.git] / ifpps.c
blob9ff01b332d332b63ea0b839e02860b5422fb0bae
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 "xio.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 volatile sig_atomic_t sigint = 0;
57 static struct ifstat stats_old, stats_new, stats_delta;
59 static struct cpu_hit *cpu_hits;
61 static int stats_loop = 0;
63 static WINDOW *stats_screen = NULL;
65 static const char *short_options = "d:t:n:vhclp";
66 static const struct option long_options[] = {
67 {"dev", required_argument, NULL, 'd'},
68 {"interval", required_argument, NULL, 't'},
69 {"num-cpus", required_argument, NULL, 'n'},
70 {"promisc", no_argument, NULL, 'p'},
71 {"csv", no_argument, NULL, 'c'},
72 {"loop", no_argument, NULL, 'l'},
73 {"version", no_argument, NULL, 'v'},
74 {"help", no_argument, NULL, 'h'},
75 {NULL, 0, NULL, 0}
78 static void signal_handler(int number)
80 switch (number) {
81 case SIGINT:
82 sigint = 1;
83 break;
84 case SIGHUP:
85 default:
86 break;
90 static inline int iswireless(const struct ifstat *stats)
92 return stats->wifi.bitrate > 0;
95 static void __noreturn help(void)
97 printf("\nifpps %s, top-like kernel networking and system statistics\n",
98 VERSION_STRING);
99 puts("http://www.netsniff-ng.org\n\n"
100 "Usage: ifpps [options] || ifpps <netdev>\n"
101 "Options:\n"
102 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
103 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
104 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
105 " in ncurses mode (default 10)\n"
106 " -p|--promisc Promiscuous mode\n"
107 " -c|--csv Output to terminal as Gnuplot-ready data\n"
108 " -l|--loop Continuous CSV output\n"
109 " -v|--version Print version and exit\n"
110 " -h|--help Print this help and exit\n\n"
111 "Examples:\n"
112 " ifpps eth0\n"
113 " ifpps -pd eth0\n"
114 " ifpps -lpcd wlan0 > plot.dat\n\n"
115 "Note:\n"
116 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
117 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
118 "Please report bugs to <bugs@netsniff-ng.org>\n"
119 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
120 "Swiss federal institute of technology (ETH Zurich)\n"
121 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
122 "License: GNU GPL version 2.0\n"
123 "This is free software: you are free to change and redistribute it.\n"
124 "There is NO WARRANTY, to the extent permitted by law.\n");
125 die();
128 static void __noreturn version(void)
130 printf("\nifpps %s, top-like kernel networking and system statistics\n",
131 VERSION_LONG);
132 puts("http://www.netsniff-ng.org\n\n"
133 "Please report bugs to <bugs@netsniff-ng.org>\n"
134 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
135 "Swiss federal institute of technology (ETH Zurich)\n"
136 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
137 "License: GNU GPL version 2.0\n"
138 "This is free software: you are free to change and redistribute it.\n"
139 "There is NO WARRANTY, to the extent permitted by law.\n");
140 die();
143 static inline int padding_from_num(int n)
145 int i = 0;
146 do i++;
147 while ((n /= 10) > 0);
148 return i;
151 #define STATS_ALLOC1(member) \
152 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
154 static void stats_alloc(struct ifstat *stats, int cpus)
156 STATS_ALLOC1(irqs);
157 STATS_ALLOC1(irqs_srx);
158 STATS_ALLOC1(irqs_stx);
160 STATS_ALLOC1(cpu_user);
161 STATS_ALLOC1(cpu_sys);
162 STATS_ALLOC1(cpu_nice);
163 STATS_ALLOC1(cpu_idle);
164 STATS_ALLOC1(cpu_iow);
167 #define STATS_ZERO1(member) \
168 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
170 static void stats_zero(struct ifstat *stats, int cpus)
172 /* Only clear the non-pointer members */
173 memset(stats, 0, offsetof(struct ifstat, irqs));
175 STATS_ZERO1(irqs);
176 STATS_ZERO1(irqs_srx);
177 STATS_ZERO1(irqs_stx);
179 STATS_ZERO1(cpu_user);
180 STATS_ZERO1(cpu_sys);
181 STATS_ZERO1(cpu_nice);
182 STATS_ZERO1(cpu_idle);
183 STATS_ZERO1(cpu_iow);
186 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
188 int ret = -EINVAL;
189 char buff[256];
190 FILE *fp;
192 fp = fopen("/proc/net/dev", "r");
193 if (!fp)
194 panic("Cannot open /proc/net/dev!\n");
196 if (fgets(buff, sizeof(buff), fp)) { ; }
197 if (fgets(buff, sizeof(buff), fp)) { ; }
199 memset(buff, 0, sizeof(buff));
201 while (fgets(buff, sizeof(buff), fp) != NULL) {
202 buff[sizeof(buff) -1] = 0;
204 if (strstr(buff, ifname) == NULL)
205 continue;
207 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
208 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
209 &stats->rx_bytes, &stats->rx_packets,
210 &stats->rx_errors, &stats->rx_drops,
211 &stats->rx_fifo, &stats->rx_frame,
212 &stats->rx_multi, &stats->tx_bytes,
213 &stats->tx_packets, &stats->tx_errors,
214 &stats->tx_drops, &stats->tx_fifo,
215 &stats->tx_colls, &stats->tx_carrier) == 14) {
216 ret = 0;
217 break;
220 memset(buff, 0, sizeof(buff));
223 fclose(fp);
224 return ret;
227 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
229 int ret = -EINVAL, i, cpus, try = 0;
230 char *ptr, *buff;
231 bool seen = false;
232 size_t buff_len;
233 struct ethtool_drvinfo drvinf;
234 FILE *fp;
236 fp = fopen("/proc/interrupts", "r");
237 if (!fp)
238 panic("Cannot open /proc/interrupts!\n");
240 cpus = get_number_cpus();
241 buff_len = cpus * 128;
242 buff = xmalloc(buff_len);
243 retry:
244 fseek(fp, 0, SEEK_SET);
245 memset(buff, 0, buff_len);
247 while (fgets(buff, buff_len, fp) != NULL) {
248 buff[buff_len - 1] = 0;
249 ptr = buff;
251 if (strstr(buff, ifname) == NULL)
252 continue;
254 /* XXX: remove this one here */
255 stats->irq_nr = strtol(ptr, &ptr, 10);
256 bug_on(stats->irq_nr == 0);
258 if (ptr)
259 ptr++;
260 for (i = 0; i < cpus && ptr; ++i) {
261 if (seen)
262 stats->irqs[i] += strtol(ptr, &ptr, 10);
263 else
264 stats->irqs[i] = strtol(ptr, &ptr, 10);
265 if (i == cpus - 1) {
266 ret = 0;
267 seen = true;
271 memset(buff, 0, buff_len);
274 if (ret == -EINVAL && try == 0) {
275 memset(&drvinf, 0, sizeof(drvinf));
276 if (ethtool_drvinf(ifname, &drvinf) < 0)
277 goto done;
279 ifname = drvinf.driver;
280 try++;
282 goto retry;
284 done:
285 xfree(buff);
286 fclose(fp);
287 return ret;
290 static int stats_proc_softirqs(struct ifstat *stats)
292 int i, cpus;
293 char *ptr, *buff;
294 size_t buff_len;
295 FILE *fp;
296 enum {
297 softirqs_net_rx,
298 softirqs_net_tx,
299 } net_type;
301 fp = fopen("/proc/softirqs", "r");
302 if (!fp)
303 panic("Cannot open /proc/softirqs!\n");
305 cpus = get_number_cpus();
306 buff_len = cpus * 128;
307 buff = xmalloc(buff_len);
309 memset(buff, 0, buff_len);
311 while (fgets(buff, buff_len, fp) != NULL) {
312 buff[buff_len - 1] = 0;
314 if ((ptr = strstr(buff, "NET_TX:")))
315 net_type = softirqs_net_tx;
316 else if ((ptr = strstr(buff, "NET_RX:")))
317 net_type = softirqs_net_rx;
318 else
319 continue;
321 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
322 switch (net_type) {
323 case softirqs_net_tx:
324 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
325 break;
326 case softirqs_net_rx:
327 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
328 break;
332 memset(buff, 0, buff_len);
335 xfree(buff);
336 fclose(fp);
337 return 0;
340 static int stats_proc_memory(struct ifstat *stats)
342 char *ptr, buff[256];
343 FILE *fp;
345 fp = fopen("/proc/meminfo", "r");
346 if (!fp)
347 panic("Cannot open /proc/meminfo!\n");
349 memset(buff, 0, sizeof(buff));
351 while (fgets(buff, sizeof(buff), fp) != NULL) {
352 buff[sizeof(buff) - 1] = 0;
354 if ((ptr = strstr(buff, "MemTotal:"))) {
355 ptr += strlen("MemTotal:");
356 stats->mem_total = strtoul(ptr, &ptr, 10);
357 } else if ((ptr = strstr(buff, "MemFree:"))) {
358 ptr += strlen("MemFree:");
359 stats->mem_free = strtoul(ptr, &ptr, 10);
362 memset(buff, 0, sizeof(buff));
365 fclose(fp);
366 return 0;
369 static int stats_proc_system(struct ifstat *stats)
371 int cpu, cpus;
372 char *ptr, buff[256];
373 FILE *fp;
375 fp = fopen("/proc/stat", "r");
376 if (!fp)
377 panic("Cannot open /proc/stat!\n");
379 cpus = get_number_cpus();
381 memset(buff, 0, sizeof(buff));
383 while (fgets(buff, sizeof(buff), fp) != NULL) {
384 buff[sizeof(buff) - 1] = 0;
386 if ((ptr = strstr(buff, "cpu"))) {
387 ptr += strlen("cpu");
388 if (isblank(*ptr))
389 goto next;
391 cpu = strtol(ptr, &ptr, 10);
392 bug_on(cpu > cpus);
394 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
395 &stats->cpu_user[cpu],
396 &stats->cpu_nice[cpu],
397 &stats->cpu_sys[cpu],
398 &stats->cpu_idle[cpu],
399 &stats->cpu_iow[cpu]) != 5)
400 goto next;
401 } else if ((ptr = strstr(buff, "ctxt"))) {
402 ptr += strlen("ctxt");
403 stats->cswitch = strtoul(ptr, &ptr, 10);
404 } else if ((ptr = strstr(buff, "processes"))) {
405 ptr += strlen("processes");
406 stats->forks = strtoul(ptr, &ptr, 10);
407 } else if ((ptr = strstr(buff, "procs_running"))) {
408 ptr += strlen("procs_running");
409 stats->procs_run = strtoul(ptr, &ptr, 10);
410 } else if ((ptr = strstr(buff, "procs_blocked"))) {
411 ptr += strlen("procs_blocked");
412 stats->procs_iow = strtoul(ptr, &ptr, 10);
414 next:
415 memset(buff, 0, sizeof(buff));
418 fclose(fp);
419 return 0;
422 static int adjust_dbm_level(int in_dbm, int dbm_val)
424 if (!in_dbm)
425 return dbm_val;
427 return dbm_val - 0x100;
430 static int stats_wireless(const char *ifname, struct ifstat *stats)
432 int ret;
433 struct iw_statistics ws;
435 ret = wireless_sigqual(ifname, &ws);
436 if (ret != 0) {
437 stats->wifi.bitrate = 0;
438 return -EINVAL;
441 stats->wifi.bitrate = wireless_bitrate(ifname);
443 stats->wifi.signal_level =
444 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
446 stats->wifi.link_qual = ws.qual.qual;
447 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
449 return ret;
452 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
453 #define DIFF(member) do { \
454 if (sizeof(diff->member) != sizeof(new->member) || \
455 sizeof(diff->member) != sizeof(old->member)) \
456 bug(); \
457 bug_on((new->member - old->member) > (new->member)); \
458 DIFF1(member); \
459 } while (0)
461 static void stats_diff(struct ifstat *old, struct ifstat *new,
462 struct ifstat *diff)
464 int cpus, i;
466 DIFF(rx_bytes);
467 DIFF(rx_packets);
468 DIFF(rx_drops);
469 DIFF(rx_errors);
470 DIFF(rx_fifo);
471 DIFF(rx_frame);
472 DIFF(rx_multi);
474 DIFF(tx_bytes);
475 DIFF(tx_bytes);
476 DIFF(tx_packets);
477 DIFF(tx_drops);
478 DIFF(tx_errors);
479 DIFF(tx_fifo);
480 DIFF(tx_colls);
481 DIFF(tx_carrier);
483 DIFF1(procs_run);
484 DIFF1(procs_iow);
486 DIFF1(wifi.signal_level);
487 DIFF1(wifi.link_qual);
489 DIFF1(cswitch);
490 DIFF1(forks);
492 cpus = get_number_cpus();
494 for (i = 0; i < cpus; ++i) {
495 DIFF(irqs[i]);
496 DIFF(irqs_srx[i]);
497 DIFF(irqs_stx[i]);
499 DIFF1(cpu_user[i]);
500 DIFF1(cpu_nice[i]);
501 DIFF1(cpu_sys[i]);
502 DIFF1(cpu_idle[i]);
503 DIFF1(cpu_iow[i]);
507 static void stats_fetch(const char *ifname, struct ifstat *stats)
509 if (stats_proc_net_dev(ifname, stats) < 0)
510 panic("Cannot fetch device stats!\n");
511 if (stats_proc_softirqs(stats) < 0)
512 panic("Cannot fetch software interrupts!\n");
513 if (stats_proc_memory(stats) < 0)
514 panic("Cannot fetch memory stats!\n");
515 if (stats_proc_system(stats) < 0)
516 panic("Cannot fetch system stats!\n");
518 stats_proc_interrupts((char *) ifname, stats);
520 stats_wireless(ifname, stats);
523 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
525 int cpus = get_number_cpus();
527 stats_zero(&stats_old, cpus);
528 stats_zero(&stats_new, cpus);
529 stats_zero(&stats_delta, cpus);
531 stats_fetch(ifname, &stats_old);
532 usleep(ms_interval * 1000);
533 stats_fetch(ifname, &stats_new);
535 stats_diff(&stats_old, &stats_new, &stats_delta);
538 static int cmp_hits(const void *p1, const void *p2)
540 const struct cpu_hit *h1 = p1, *h2 = p2;
543 * We want the hits sorted in descending order, thus reverse the return
544 * values.
546 if (h1->hit == h2->hit)
547 return 0;
548 else if (h1->hit < h2->hit)
549 return 1;
550 else
551 return -1;
554 static int cmp_irqs_rel(const void *p1, const void *p2)
556 const struct cpu_hit *h1 = p1, *h2 = p2;
559 * We want the hits sorted in descending order, thus reverse the return
560 * values.
562 if (h1->irqs_rel == h2->irqs_rel)
563 return 0;
564 else if (h1->irqs_rel < h2->irqs_rel)
565 return 1;
566 else
567 return -1;
570 static int cmp_irqs_abs(const void *p1, const void *p2)
572 const struct cpu_hit *h1 = p1, *h2 = p2;
575 * We want the hits sorted in descending order, thus reverse the return
576 * values.
578 if (h1->irqs_abs == h2->irqs_abs)
579 return 0;
580 else if (h1->irqs_abs < h2->irqs_abs)
581 return 1;
582 else
583 return -1;
586 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
587 int top_cpus)
589 int i;
591 for (i = 0; i < top_cpus; ++i) {
592 cpu_hits[i].idx = i;
593 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
594 cpu_hits[i].irqs_rel = rel->irqs[i];
595 cpu_hits[i].irqs_abs = abs->irqs[i];
599 static void screen_init(WINDOW **screen)
601 (*screen) = initscr();
603 raw();
604 noecho();
605 cbreak();
606 nodelay((*screen), TRUE);
608 keypad(stdscr, TRUE);
610 refresh();
611 wrefresh((*screen));
614 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
615 uint64_t ms_interval, unsigned int top_cpus)
617 size_t len = 0;
618 char buff[64];
619 struct ethtool_drvinfo drvinf;
620 u32 rate = device_bitrate(ifname);
621 int link = ethtool_link(ifname);
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/%u"
635 " ",
636 ifname, drvinf.driver, buff, ms_interval, top_cpus, get_number_cpus());
639 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
640 int *voff)
642 attron(A_REVERSE);
644 mvwprintw(screen, (*voff)++, 0,
645 " rx: %16.3llf MiB/t "
646 "%10llu pkts/t "
647 "%10llu drops/t "
648 "%10llu errors/t ",
649 ((long double) rel->rx_bytes) / (1LLU << 20),
650 rel->rx_packets, rel->rx_drops, rel->rx_errors);
652 mvwprintw(screen, (*voff)++, 0,
653 " tx: %16.3llf MiB/t "
654 "%10llu pkts/t "
655 "%10llu drops/t "
656 "%10llu errors/t ",
657 ((long double) rel->tx_bytes) / (1LLU << 20),
658 rel->tx_packets, rel->tx_drops, rel->tx_errors);
660 attroff(A_REVERSE);
663 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
664 int *voff)
666 mvwprintw(screen, (*voff)++, 2,
667 "rx: %16.3llf MiB "
668 "%10llu pkts "
669 "%10llu drops "
670 "%10llu errors",
671 ((long double) abs->rx_bytes) / (1LLU << 20),
672 abs->rx_packets, abs->rx_drops, abs->rx_errors);
674 mvwprintw(screen, (*voff)++, 2,
675 "tx: %16.3llf MiB "
676 "%10llu pkts "
677 "%10llu drops "
678 "%10llu errors",
679 ((long double) abs->tx_bytes) / (1LLU << 20),
680 abs->tx_packets, abs->tx_drops, abs->tx_errors);
683 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
684 const struct ifstat *abs, int *voff)
686 mvwprintw(screen, (*voff)++, 2,
687 "sys: %14u cs/t "
688 "%10.1lf%% mem "
689 "%13u running "
690 "%10u iowait",
691 rel->cswitch,
692 (100.0 * (abs->mem_total - abs->mem_free)) / abs->mem_total,
693 abs->procs_run, abs->procs_iow);
696 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
697 int top_cpus, int *voff)
699 int i;
700 uint64_t all;
701 int max_padd = padding_from_num(get_number_cpus());
703 for (i = 0; i < top_cpus; ++i) {
704 unsigned int idx = cpu_hits[i].idx;
706 all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
707 rel->cpu_idle[idx] + rel->cpu_iow[idx];
709 mvwprintw(screen, (*voff)++, 2,
710 "cpu%*d: %13.1lf%% usr/t "
711 "%9.1lf%% sys/t "
712 "%10.1lf%% idl/t "
713 "%11.1lf%% iow/t ", max_padd, idx,
714 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
715 100.0 * rel->cpu_sys[idx] / all,
716 100.0 * rel->cpu_idle[idx] / all,
717 100.0 * rel->cpu_iow[idx] / all);
721 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
722 int top_cpus, int *voff)
724 int i;
725 int max_padd = padding_from_num(get_number_cpus());
727 for (i = 0; i < top_cpus; ++i) {
728 unsigned int idx = cpu_hits[i].idx;
730 mvwprintw(screen, (*voff)++, 2,
731 "cpu%*d: %14llu irqs/t "
732 "%15llu sirq rx/t "
733 "%15llu sirq tx/t ", max_padd, idx,
734 rel->irqs[idx],
735 rel->irqs_srx[idx],
736 rel->irqs_stx[idx]);
740 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
741 int top_cpus, int *voff)
743 int i;
744 int max_padd = padding_from_num(get_number_cpus());
746 for (i = 0; i < top_cpus; ++i) {
747 unsigned int idx = cpu_hits[i].idx;
749 mvwprintw(screen, (*voff)++, 2,
750 "cpu%*d: %14llu irqs", max_padd, idx,
751 abs->irqs[idx]);
755 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
756 const struct ifstat *abs, int *voff)
758 if (iswireless(abs)) {
759 mvwprintw(screen, (*voff)++, 2,
760 "linkqual: %7d/%d (%d/t) ",
761 abs->wifi.link_qual,
762 abs->wifi.link_qual_max,
763 rel->wifi.link_qual);
765 mvwprintw(screen, (*voff)++, 2,
766 "signal: %8d dBm (%d dBm/t) ",
767 abs->wifi.signal_level,
768 rel->wifi.signal_level);
772 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
773 const struct ifstat *abs, int *first, uint64_t ms_interval,
774 unsigned int top_cpus)
776 int cpus, top, voff = 1, cvoff = 2;
778 curs_set(0);
780 cpus = get_number_cpus();
781 top = min(cpus, top_cpus);
783 stats_top(rel, abs, cpus);
785 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
787 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
789 voff++;
790 screen_net_dev_rel(screen, rel, &voff);
792 voff++;
793 screen_net_dev_abs(screen, abs, &voff);
795 voff++;
796 screen_sys_mem(screen, rel, abs, &voff);
798 voff++;
799 screen_percpu_states(screen, rel, top, &voff);
801 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
803 voff++;
804 screen_percpu_irqs_rel(screen, rel, top, &voff);
806 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
808 voff++;
809 screen_percpu_irqs_abs(screen, abs, top, &voff);
811 voff++;
812 screen_wireless(screen, rel, abs, &voff);
814 if (*first) {
815 mvwprintw(screen, cvoff, 2, "Collecting data ...");
816 *first = 0;
817 } else {
818 mvwprintw(screen, cvoff, 2, " ");
821 wrefresh(screen);
822 refresh();
825 static void screen_end(void)
827 endwin();
830 static int screen_main(const char *ifname, uint64_t ms_interval,
831 unsigned int top_cpus)
833 int first = 1, key;
835 screen_init(&stats_screen);
837 while (!sigint) {
838 key = getch();
839 if (key == 'q' || key == 0x1b || key == KEY_F(10))
840 break;
842 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
843 &first, ms_interval, top_cpus);
845 stats_sample_generic(ifname, ms_interval);
848 screen_end();
850 return 0;
853 static void term_csv(const char *ifname, const struct ifstat *rel,
854 const struct ifstat *abs, uint64_t ms_interval)
856 int cpus, i;
858 printf("%ld ", time(0));
860 printf("%llu ", rel->rx_bytes);
861 printf("%llu ", rel->rx_packets);
862 printf("%llu ", rel->rx_drops);
863 printf("%llu ", rel->rx_errors);
865 printf("%llu ", abs->rx_bytes);
866 printf("%llu ", abs->rx_packets);
867 printf("%llu ", abs->rx_drops);
868 printf("%llu ", abs->rx_errors);
870 printf("%llu ", rel->tx_bytes);
871 printf("%llu ", rel->tx_packets);
872 printf("%llu ", rel->tx_drops);
873 printf("%llu ", rel->tx_errors);
875 printf("%llu ", abs->tx_bytes);
876 printf("%llu ", abs->tx_packets);
877 printf("%llu ", abs->tx_drops);
878 printf("%llu ", abs->tx_errors);
880 printf("%u ", rel->cswitch);
881 printf("%lu ", abs->mem_free);
882 printf("%lu ", abs->mem_total - abs->mem_free);
883 printf("%lu ", abs->mem_total);
884 printf("%u ", abs->procs_run);
885 printf("%u ", abs->procs_iow);
887 cpus = get_number_cpus();
889 for (i = 0; i < cpus; ++i) {
890 printf("%lu ", rel->cpu_user[i]);
891 printf("%lu ", rel->cpu_nice[i]);
892 printf("%lu ", rel->cpu_sys[i]);
893 printf("%lu ", rel->cpu_idle[i]);
894 printf("%lu ", rel->cpu_iow[i]);
896 printf("%llu ", rel->irqs[i]);
897 printf("%llu ", abs->irqs[i]);
899 printf("%llu ", rel->irqs_srx[i]);
900 printf("%llu ", abs->irqs_srx[i]);
902 printf("%llu ", rel->irqs_stx[i]);
903 printf("%llu ", abs->irqs_stx[i]);
906 if (iswireless(abs)) {
907 printf("%u ", rel->wifi.link_qual);
908 printf("%u ", abs->wifi.link_qual);
909 printf("%u ", abs->wifi.link_qual_max);
911 printf("%d ", rel->wifi.signal_level);
912 printf("%d ", abs->wifi.signal_level);
915 puts("");
916 fflush(stdout);
919 static void term_csv_header(const char *ifname, const struct ifstat *abs,
920 uint64_t ms_interval)
922 int cpus, i, j = 1;
924 printf("# gnuplot dump (#col:description)\n");
925 printf("# networking interface: %s\n", ifname);
926 printf("# sampling interval (t): %lu ms\n", ms_interval);
927 printf("# %d:unixtime ", j++);
929 printf("%d:rx-bytes-per-t ", j++);
930 printf("%d:rx-pkts-per-t ", j++);
931 printf("%d:rx-drops-per-t ", j++);
932 printf("%d:rx-errors-per-t ", j++);
934 printf("%d:rx-bytes ", j++);
935 printf("%d:rx-pkts ", j++);
936 printf("%d:rx-drops ", j++);
937 printf("%d:rx-errors ", j++);
939 printf("%d:tx-bytes-per-t ", j++);
940 printf("%d:tx-pkts-per-t ", j++);
941 printf("%d:tx-drops-per-t ", j++);
942 printf("%d:tx-errors-per-t ", j++);
944 printf("%d:tx-bytes ", j++);
945 printf("%d:tx-pkts ", j++);
946 printf("%d:tx-drops ", j++);
947 printf("%d:tx-errors ", j++);
949 printf("%d:context-switches-per-t ", j++);
950 printf("%d:mem-free ", j++);
951 printf("%d:mem-used ", j++);
952 printf("%d:mem-total ", j++);
953 printf("%d:procs-in-run ", j++);
954 printf("%d:procs-in-iow ", j++);
956 cpus = get_number_cpus();
958 for (i = 0, j = 22; i < cpus; ++i) {
959 printf("%d:cpu%i-usr-per-t ", j++, i);
960 printf("%d:cpu%i-nice-per-t ", j++, i);
961 printf("%d:cpu%i-sys-per-t ", j++, i);
962 printf("%d:cpu%i-idle-per-t ", j++, i);
963 printf("%d:cpu%i-iow-per-t ", j++, i);
965 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
966 printf("%d:cpu%i-net-irqs ", j++, i);
968 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
969 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
970 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
971 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
974 if (iswireless(abs)) {
975 printf("%d:wifi-link-qual-per-t ", j++);
976 printf("%d:wifi-link-qual ", j++);
977 printf("%d:wifi-link-qual-max ", j++);
979 printf("%d:wifi-signal-dbm-per-t ", j++);
980 printf("%d:wifi-signal-dbm ", j++);
983 puts("");
984 printf("# data:\n");
985 fflush(stdout);
988 static int term_main(const char *ifname, uint64_t ms_interval,
989 unsigned int top_cpus __maybe_unused)
991 int first = 1;
993 do {
994 stats_sample_generic(ifname, ms_interval);
996 if (first) {
997 first = 0;
998 term_csv_header(ifname, &stats_new, ms_interval);
1001 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1002 } while (stats_loop && !sigint);
1004 return 0;
1007 int main(int argc, char **argv)
1009 short ifflags = 0;
1010 int c, opt_index, ret, cpus, promisc = 0;
1011 unsigned int top_cpus = 10;
1012 uint64_t interval = 1000;
1013 char *ifname = NULL;
1014 int (*func_main)(const char *ifname, uint64_t ms_interval,
1015 unsigned int top_cpus) = screen_main;
1017 setfsuid(getuid());
1018 setfsgid(getgid());
1020 while ((c = getopt_long(argc, argv, short_options, long_options,
1021 &opt_index)) != EOF) {
1022 switch (c) {
1023 case 'h':
1024 help();
1025 break;
1026 case 'v':
1027 version();
1028 break;
1029 case 'd':
1030 ifname = xstrndup(optarg, IFNAMSIZ);
1031 break;
1032 case 't':
1033 interval = strtoul(optarg, NULL, 10);
1034 break;
1035 case 'n':
1036 top_cpus = strtoul(optarg, NULL, 10);
1037 break;
1038 case 'l':
1039 stats_loop = 1;
1040 break;
1041 case 'p':
1042 promisc = 1;
1043 break;
1044 case 'c':
1045 func_main = term_main;
1046 break;
1047 case '?':
1048 switch (optopt) {
1049 case 'd':
1050 case 't':
1051 panic("Option -%c requires an argument!\n",
1052 optopt);
1053 default:
1054 if (isprint(optopt))
1055 printf("Unknown option character `0x%X\'!\n", optopt);
1056 die();
1058 default:
1059 break;
1063 if (argc == 1)
1064 help();
1066 if (argc == 2)
1067 ifname = xstrndup(argv[1], IFNAMSIZ);
1068 if (ifname == NULL)
1069 panic("No networking device given!\n");
1071 if (!strncmp("lo", ifname, IFNAMSIZ))
1072 panic("lo is not supported!\n");
1073 if (device_mtu(ifname) == 0)
1074 panic("This is no networking device!\n");
1076 register_signal(SIGINT, signal_handler);
1077 register_signal(SIGHUP, signal_handler);
1079 cpus = get_number_cpus();
1080 top_cpus = min(top_cpus, cpus);
1082 stats_alloc(&stats_old, cpus);
1083 stats_alloc(&stats_new, cpus);
1084 stats_alloc(&stats_delta, cpus);
1086 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1088 if (promisc)
1089 ifflags = enter_promiscuous_mode(ifname);
1090 ret = func_main(ifname, interval, top_cpus);
1091 if (promisc)
1092 leave_promiscuous_mode(ifname, ifflags);
1094 xfree(ifname);
1095 return ret;