ifpps: remove unused function snr_to_str
[netsniff-ng.git] / ifpps.c
blobc411b3edd3ecfb8bde6bc0d29e03b5f865714396
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 "built_in.h"
26 /* Number of top hitter CPUs to display */
27 #define TOP_CPUS 10
29 struct wifi_stat {
30 uint32_t bitrate;
31 int16_t link_qual, link_qual_max;
32 int signal_level /*, noise_level*/;
35 struct ifstat {
36 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
37 long long unsigned int rx_fifo, rx_frame, rx_multi;
38 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
39 long long unsigned int tx_fifo, tx_colls, tx_carrier;
40 uint64_t mem_free, mem_total;
41 uint32_t irq_nr, procs_run, procs_iow, cswitch, forks;
42 struct wifi_stat wifi;
44 * Pointer members need to be last in order for stats_zero() to work
45 * properly.
47 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
48 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
51 struct cpu_hit {
52 unsigned int idx;
53 uint64_t hit;
54 long long unsigned int irqs_rel, irqs_abs;
57 volatile sig_atomic_t sigint = 0;
59 static struct ifstat stats_old, stats_new, stats_delta;
61 static struct cpu_hit *cpu_hits;
63 static int stats_loop = 0;
65 static WINDOW *stats_screen = NULL;
67 static const char *short_options = "d:t:vhclp";
68 static const struct option long_options[] = {
69 {"dev", required_argument, NULL, 'd'},
70 {"interval", required_argument, NULL, 't'},
71 {"promisc", no_argument, NULL, 'p'},
72 {"csv", no_argument, NULL, 'c'},
73 {"loop", no_argument, NULL, 'l'},
74 {"version", no_argument, NULL, 'v'},
75 {"help", no_argument, NULL, 'h'},
76 {NULL, 0, NULL, 0}
79 static void signal_handler(int number)
81 switch (number) {
82 case SIGINT:
83 sigint = 1;
84 break;
85 case SIGHUP:
86 default:
87 break;
91 static inline int iswireless(const struct ifstat *stats)
93 return stats->wifi.bitrate > 0;
96 static void __noreturn help(void)
98 printf("\nifpps %s, top-like kernel networking and system statistics\n",
99 VERSION_STRING);
100 puts("http://www.netsniff-ng.org\n\n"
101 "Usage: ifpps [options] || ifpps <netdev>\n"
102 "Options:\n"
103 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
104 " -t|--interval <time> Refresh time in ms (default 1000 ms)\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, 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 size_t buff_len;
231 struct ethtool_drvinfo drvinf;
232 FILE *fp;
234 fp = fopen("/proc/interrupts", "r");
235 if (!fp)
236 panic("Cannot open /proc/interrupts!\n");
238 cpus = get_number_cpus();
239 buff_len = cpus * 128;
240 buff = xmalloc(buff_len);
241 retry:
242 fseek(fp, 0, SEEK_SET);
243 memset(buff, 0, buff_len);
245 while (fgets(buff, buff_len, fp) != NULL) {
246 buff[buff_len - 1] = 0;
247 ptr = buff;
249 if (strstr(buff, ifname) == NULL)
250 continue;
252 stats->irq_nr = strtol(ptr, &ptr, 10);
253 bug_on(stats->irq_nr == 0);
255 if (ptr)
256 ptr++;
257 for (i = 0; i < cpus && ptr; ++i) {
258 stats->irqs[i] = strtol(ptr, &ptr, 10);
259 if (i == cpus - 1) {
260 ret = 0;
261 goto done;
265 memset(buff, 0, buff_len);
268 if (ret == -EINVAL && try == 0) {
269 memset(&drvinf, 0, sizeof(drvinf));
270 if (ethtool_drvinf(ifname, &drvinf) < 0)
271 goto done;
273 ifname = drvinf.driver;
274 try++;
276 goto retry;
278 done:
279 xfree(buff);
280 fclose(fp);
281 return ret;
284 static int stats_proc_softirqs(struct ifstat *stats)
286 int i, cpus;
287 char *ptr, *buff;
288 size_t buff_len;
289 FILE *fp;
290 enum {
291 softirqs_net_rx,
292 softirqs_net_tx,
293 } net_type;
295 fp = fopen("/proc/softirqs", "r");
296 if (!fp)
297 panic("Cannot open /proc/softirqs!\n");
299 cpus = get_number_cpus();
300 buff_len = cpus * 128;
301 buff = xmalloc(buff_len);
303 memset(buff, 0, buff_len);
305 while (fgets(buff, buff_len, fp) != NULL) {
306 buff[buff_len - 1] = 0;
308 if ((ptr = strstr(buff, "NET_TX:")))
309 net_type = softirqs_net_tx;
310 else if ((ptr = strstr(buff, "NET_RX:")))
311 net_type = softirqs_net_rx;
312 else
313 continue;
315 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
316 switch (net_type) {
317 case softirqs_net_tx:
318 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
319 break;
320 case softirqs_net_rx:
321 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
322 break;
326 memset(buff, 0, buff_len);
329 xfree(buff);
330 fclose(fp);
331 return 0;
334 static int stats_proc_memory(struct ifstat *stats)
336 char *ptr, buff[256];
337 FILE *fp;
339 fp = fopen("/proc/meminfo", "r");
340 if (!fp)
341 panic("Cannot open /proc/meminfo!\n");
343 memset(buff, 0, sizeof(buff));
345 while (fgets(buff, sizeof(buff), fp) != NULL) {
346 buff[sizeof(buff) - 1] = 0;
348 if ((ptr = strstr(buff, "MemTotal:"))) {
349 ptr += strlen("MemTotal:");
350 stats->mem_total = strtoul(ptr, &ptr, 10);
351 } else if ((ptr = strstr(buff, "MemFree:"))) {
352 ptr += strlen("MemFree:");
353 stats->mem_free = strtoul(ptr, &ptr, 10);
356 memset(buff, 0, sizeof(buff));
359 fclose(fp);
360 return 0;
363 static int stats_proc_system(struct ifstat *stats)
365 int cpu, cpus;
366 char *ptr, buff[256];
367 FILE *fp;
369 fp = fopen("/proc/stat", "r");
370 if (!fp)
371 panic("Cannot open /proc/stat!\n");
373 cpus = get_number_cpus();
375 memset(buff, 0, sizeof(buff));
377 while (fgets(buff, sizeof(buff), fp) != NULL) {
378 buff[sizeof(buff) - 1] = 0;
380 if ((ptr = strstr(buff, "cpu"))) {
381 ptr += strlen("cpu");
382 if (isblank(*ptr))
383 goto next;
385 cpu = strtol(ptr, &ptr, 10);
386 bug_on(cpu > cpus);
388 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
389 &stats->cpu_user[cpu],
390 &stats->cpu_nice[cpu],
391 &stats->cpu_sys[cpu],
392 &stats->cpu_idle[cpu],
393 &stats->cpu_iow[cpu]) != 5)
394 goto next;
395 } else if ((ptr = strstr(buff, "ctxt"))) {
396 ptr += strlen("ctxt");
397 stats->cswitch = strtoul(ptr, &ptr, 10);
398 } else if ((ptr = strstr(buff, "processes"))) {
399 ptr += strlen("processes");
400 stats->forks = strtoul(ptr, &ptr, 10);
401 } else if ((ptr = strstr(buff, "procs_running"))) {
402 ptr += strlen("procs_running");
403 stats->procs_run = strtoul(ptr, &ptr, 10);
404 } else if ((ptr = strstr(buff, "procs_blocked"))) {
405 ptr += strlen("procs_blocked");
406 stats->procs_iow = strtoul(ptr, &ptr, 10);
408 next:
409 memset(buff, 0, sizeof(buff));
412 fclose(fp);
413 return 0;
416 static int adjust_dbm_level(int in_dbm, int dbm_val)
418 if (!in_dbm)
419 return dbm_val;
421 return dbm_val - 0x100;
424 static int stats_wireless(const char *ifname, struct ifstat *stats)
426 int ret;
427 struct iw_statistics ws;
429 ret = wireless_sigqual(ifname, &ws);
430 if (ret != 0) {
431 stats->wifi.bitrate = 0;
432 return -EINVAL;
435 stats->wifi.bitrate = wireless_bitrate(ifname);
437 stats->wifi.signal_level =
438 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
440 stats->wifi.link_qual = ws.qual.qual;
441 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
443 return ret;
446 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
447 #define DIFF(member) do { \
448 if (sizeof(diff->member) != sizeof(new->member) || \
449 sizeof(diff->member) != sizeof(old->member)) \
450 bug(); \
451 bug_on((new->member - old->member) > (new->member)); \
452 DIFF1(member); \
453 } while (0)
455 static void stats_diff(struct ifstat *old, struct ifstat *new,
456 struct ifstat *diff)
458 int cpus, i;
460 DIFF(rx_bytes);
461 DIFF(rx_packets);
462 DIFF(rx_drops);
463 DIFF(rx_errors);
464 DIFF(rx_fifo);
465 DIFF(rx_frame);
466 DIFF(rx_multi);
468 DIFF(tx_bytes);
469 DIFF(tx_bytes);
470 DIFF(tx_packets);
471 DIFF(tx_drops);
472 DIFF(tx_errors);
473 DIFF(tx_fifo);
474 DIFF(tx_colls);
475 DIFF(tx_carrier);
477 DIFF1(procs_run);
478 DIFF1(procs_iow);
480 DIFF1(wifi.signal_level);
481 DIFF1(wifi.link_qual);
483 DIFF1(cswitch);
484 DIFF1(forks);
486 cpus = get_number_cpus();
488 for (i = 0; i < cpus; ++i) {
489 DIFF(irqs[i]);
490 DIFF(irqs_srx[i]);
491 DIFF(irqs_stx[i]);
493 DIFF1(cpu_user[i]);
494 DIFF1(cpu_nice[i]);
495 DIFF1(cpu_sys[i]);
496 DIFF1(cpu_idle[i]);
497 DIFF1(cpu_iow[i]);
501 static void stats_fetch(const char *ifname, struct ifstat *stats)
503 if (stats_proc_net_dev(ifname, stats) < 0)
504 panic("Cannot fetch device stats!\n");
505 if (stats_proc_softirqs(stats) < 0)
506 panic("Cannot fetch software interrupts!\n");
507 if (stats_proc_memory(stats) < 0)
508 panic("Cannot fetch memory stats!\n");
509 if (stats_proc_system(stats) < 0)
510 panic("Cannot fetch system stats!\n");
512 stats_proc_interrupts((char *) ifname, stats);
514 stats_wireless(ifname, stats);
517 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
519 int cpus = get_number_cpus();
521 stats_zero(&stats_old, cpus);
522 stats_zero(&stats_new, cpus);
523 stats_zero(&stats_delta, cpus);
525 stats_fetch(ifname, &stats_old);
526 usleep(ms_interval * 1000);
527 stats_fetch(ifname, &stats_new);
529 stats_diff(&stats_old, &stats_new, &stats_delta);
532 static int cmp_hits(const void *p1, const void *p2)
534 const struct cpu_hit *h1 = p1, *h2 = p2;
537 * We want the hits sorted in descending order, thus reverse the return
538 * values.
540 if (h1->hit == h2->hit)
541 return 0;
542 else if (h1->hit < h2->hit)
543 return 1;
544 else
545 return -1;
548 static int cmp_irqs_rel(const void *p1, const void *p2)
550 const struct cpu_hit *h1 = p1, *h2 = p2;
553 * We want the hits sorted in descending order, thus reverse the return
554 * values.
556 if (h1->irqs_rel == h2->irqs_rel)
557 return 0;
558 else if (h1->irqs_rel < h2->irqs_rel)
559 return 1;
560 else
561 return -1;
564 static int cmp_irqs_abs(const void *p1, const void *p2)
566 const struct cpu_hit *h1 = p1, *h2 = p2;
569 * We want the hits sorted in descending order, thus reverse the return
570 * values.
572 if (h1->irqs_abs == h2->irqs_abs)
573 return 0;
574 else if (h1->irqs_abs < h2->irqs_abs)
575 return 1;
576 else
577 return -1;
580 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
581 int cpus)
583 int i;
585 for (i = 0; i < cpus; ++i) {
586 cpu_hits[i].idx = i;
587 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
588 cpu_hits[i].irqs_rel = rel->irqs[i];
589 cpu_hits[i].irqs_abs = abs->irqs[i];
593 static void screen_init(WINDOW **screen)
595 (*screen) = initscr();
597 raw();
598 noecho();
599 cbreak();
600 nodelay((*screen), TRUE);
602 keypad(stdscr, TRUE);
604 refresh();
605 wrefresh((*screen));
608 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
609 uint64_t ms_interval)
611 size_t len = 0;
612 char buff[64];
613 struct ethtool_drvinfo drvinf;
614 u32 rate = device_bitrate(ifname);
615 int link = ethtool_link(ifname);
617 memset(&drvinf, 0, sizeof(drvinf));
618 ethtool_drvinf(ifname, &drvinf);
620 memset(buff, 0, sizeof(buff));
621 if (rate)
622 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
623 if (link >= 0)
624 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
625 link == 0 ? "no" : "yes");
627 mvwprintw(screen, (*voff)++, 2,
628 "Kernel net/sys statistics for %s (%s%s), t=%lums"
629 " ",
630 ifname, drvinf.driver, buff, ms_interval);
633 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
634 int *voff)
636 attron(A_REVERSE);
638 mvwprintw(screen, (*voff)++, 0,
639 " rx: %16.3llf MiB/t "
640 "%10llu pkts/t "
641 "%10llu drops/t "
642 "%10llu errors/t ",
643 ((long double) rel->rx_bytes) / (1LLU << 20),
644 rel->rx_packets, rel->rx_drops, rel->rx_errors);
646 mvwprintw(screen, (*voff)++, 0,
647 " tx: %16.3llf MiB/t "
648 "%10llu pkts/t "
649 "%10llu drops/t "
650 "%10llu errors/t ",
651 ((long double) rel->tx_bytes) / (1LLU << 20),
652 rel->tx_packets, rel->tx_drops, rel->tx_errors);
654 attroff(A_REVERSE);
657 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
658 int *voff)
660 mvwprintw(screen, (*voff)++, 2,
661 "rx: %16.3llf MiB "
662 "%10llu pkts "
663 "%10llu drops "
664 "%10llu errors",
665 ((long double) abs->rx_bytes) / (1LLU << 20),
666 abs->rx_packets, abs->rx_drops, abs->rx_errors);
668 mvwprintw(screen, (*voff)++, 2,
669 "tx: %16.3llf MiB "
670 "%10llu pkts "
671 "%10llu drops "
672 "%10llu errors",
673 ((long double) abs->tx_bytes) / (1LLU << 20),
674 abs->tx_packets, abs->tx_drops, abs->tx_errors);
677 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
678 const struct ifstat *abs, int *voff)
680 mvwprintw(screen, (*voff)++, 2,
681 "sys: %14u cs/t "
682 "%10.1lf%% mem "
683 "%13u running "
684 "%10u iowait",
685 rel->cswitch,
686 (100.0 * (abs->mem_total - abs->mem_free)) / abs->mem_total,
687 abs->procs_run, abs->procs_iow);
690 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
691 int cpus, int *voff)
693 int i;
694 uint64_t all;
695 int max_padd = padding_from_num(get_number_cpus());
697 for (i = 0; i < cpus; ++i) {
698 unsigned int idx = cpu_hits[i].idx;
700 all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
701 rel->cpu_idle[idx] + rel->cpu_iow[idx];
703 mvwprintw(screen, (*voff)++, 2,
704 "cpu%*d: %13.1lf%% usr/t "
705 "%9.1lf%% sys/t "
706 "%10.1lf%% idl/t "
707 "%11.1lf%% iow/t ", max_padd, idx,
708 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
709 100.0 * rel->cpu_sys[idx] / all,
710 100.0 * rel->cpu_idle[idx] / all,
711 100.0 * rel->cpu_iow[idx] / all);
715 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
716 int cpus, int *voff)
718 int i;
719 int max_padd = padding_from_num(get_number_cpus());
721 for (i = 0; i < cpus; ++i) {
722 unsigned int idx = cpu_hits[i].idx;
724 mvwprintw(screen, (*voff)++, 2,
725 "cpu%*d: %14llu irqs/t "
726 "%15llu sirq rx/t "
727 "%15llu sirq tx/t ", max_padd, idx,
728 rel->irqs[idx],
729 rel->irqs_srx[idx],
730 rel->irqs_stx[idx]);
734 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
735 int cpus, int *voff)
737 int i;
738 int max_padd = padding_from_num(get_number_cpus());
740 for (i = 0; i < cpus; ++i) {
741 unsigned int idx = cpu_hits[i].idx;
743 mvwprintw(screen, (*voff)++, 2,
744 "cpu%*d: %14llu irqs", max_padd, idx,
745 abs->irqs[idx]);
749 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
750 const struct ifstat *abs, int *voff)
752 if (iswireless(abs)) {
753 mvwprintw(screen, (*voff)++, 2,
754 "linkqual: %7d/%d (%d/t) ",
755 abs->wifi.link_qual,
756 abs->wifi.link_qual_max,
757 rel->wifi.link_qual);
759 mvwprintw(screen, (*voff)++, 2,
760 "signal: %8d dBm (%d dBm/t) ",
761 abs->wifi.signal_level,
762 rel->wifi.signal_level);
766 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
767 const struct ifstat *abs, int *first, uint64_t ms_interval)
769 int cpus, top, voff = 1, cvoff = 2;
771 curs_set(0);
773 cpus = get_number_cpus();
774 top = min(cpus, TOP_CPUS);
776 stats_top(rel, abs, cpus);
778 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
780 screen_header(screen, ifname, &voff, ms_interval);
782 voff++;
783 screen_net_dev_rel(screen, rel, &voff);
785 voff++;
786 screen_net_dev_abs(screen, abs, &voff);
788 voff++;
789 screen_sys_mem(screen, rel, abs, &voff);
791 voff++;
792 screen_percpu_states(screen, rel, top, &voff);
794 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
796 voff++;
797 screen_percpu_irqs_rel(screen, rel, top, &voff);
799 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
801 voff++;
802 screen_percpu_irqs_abs(screen, abs, top, &voff);
804 voff++;
805 screen_wireless(screen, rel, abs, &voff);
807 if (*first) {
808 mvwprintw(screen, cvoff, 2, "Collecting data ...");
809 *first = 0;
810 } else {
811 mvwprintw(screen, cvoff, 2, " ");
814 wrefresh(screen);
815 refresh();
818 static void screen_end(void)
820 endwin();
823 static int screen_main(const char *ifname, uint64_t ms_interval)
825 int first = 1, key;
826 int cpus = get_number_cpus();
828 stats_alloc(&stats_old, cpus);
829 stats_alloc(&stats_new, cpus);
830 stats_alloc(&stats_delta, cpus);
832 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
834 screen_init(&stats_screen);
836 while (!sigint) {
837 key = getch();
838 if (key == 'q' || key == 0x1b || key == KEY_F(10))
839 break;
841 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
842 &first, ms_interval);
844 stats_sample_generic(ifname, ms_interval);
847 screen_end();
849 return 0;
852 static void term_csv(const char *ifname, const struct ifstat *rel,
853 const struct ifstat *abs, uint64_t ms_interval)
855 int cpus, i;
857 printf("%ld ", time(0));
859 printf("%llu ", rel->rx_bytes);
860 printf("%llu ", rel->rx_packets);
861 printf("%llu ", rel->rx_drops);
862 printf("%llu ", rel->rx_errors);
864 printf("%llu ", abs->rx_bytes);
865 printf("%llu ", abs->rx_packets);
866 printf("%llu ", abs->rx_drops);
867 printf("%llu ", abs->rx_errors);
869 printf("%llu ", rel->tx_bytes);
870 printf("%llu ", rel->tx_packets);
871 printf("%llu ", rel->tx_drops);
872 printf("%llu ", rel->tx_errors);
874 printf("%llu ", abs->tx_bytes);
875 printf("%llu ", abs->tx_packets);
876 printf("%llu ", abs->tx_drops);
877 printf("%llu ", abs->tx_errors);
879 printf("%u ", rel->cswitch);
880 printf("%lu ", abs->mem_free);
881 printf("%lu ", abs->mem_total - abs->mem_free);
882 printf("%lu ", abs->mem_total);
883 printf("%u ", abs->procs_run);
884 printf("%u ", abs->procs_iow);
886 cpus = get_number_cpus();
888 for (i = 0; i < cpus; ++i) {
889 printf("%lu ", rel->cpu_user[i]);
890 printf("%lu ", rel->cpu_nice[i]);
891 printf("%lu ", rel->cpu_sys[i]);
892 printf("%lu ", rel->cpu_idle[i]);
893 printf("%lu ", rel->cpu_iow[i]);
895 printf("%llu ", rel->irqs[i]);
896 printf("%llu ", abs->irqs[i]);
898 printf("%llu ", rel->irqs_srx[i]);
899 printf("%llu ", abs->irqs_srx[i]);
901 printf("%llu ", rel->irqs_stx[i]);
902 printf("%llu ", abs->irqs_stx[i]);
905 if (iswireless(abs)) {
906 printf("%u ", rel->wifi.link_qual);
907 printf("%u ", abs->wifi.link_qual);
908 printf("%u ", abs->wifi.link_qual_max);
910 printf("%d ", rel->wifi.signal_level);
911 printf("%d ", abs->wifi.signal_level);
914 puts("");
915 fflush(stdout);
918 static void term_csv_header(const char *ifname, const struct ifstat *abs,
919 uint64_t ms_interval)
921 int cpus, i, j = 1;
923 printf("# gnuplot dump (#col:description)\n");
924 printf("# networking interface: %s\n", ifname);
925 printf("# sampling interval (t): %lu ms\n", ms_interval);
926 printf("# %d:unixtime ", j++);
928 printf("%d:rx-bytes-per-t ", j++);
929 printf("%d:rx-pkts-per-t ", j++);
930 printf("%d:rx-drops-per-t ", j++);
931 printf("%d:rx-errors-per-t ", j++);
933 printf("%d:rx-bytes ", j++);
934 printf("%d:rx-pkts ", j++);
935 printf("%d:rx-drops ", j++);
936 printf("%d:rx-errors ", j++);
938 printf("%d:tx-bytes-per-t ", j++);
939 printf("%d:tx-pkts-per-t ", j++);
940 printf("%d:tx-drops-per-t ", j++);
941 printf("%d:tx-errors-per-t ", j++);
943 printf("%d:tx-bytes ", j++);
944 printf("%d:tx-pkts ", j++);
945 printf("%d:tx-drops ", j++);
946 printf("%d:tx-errors ", j++);
948 printf("%d:context-switches-per-t ", j++);
949 printf("%d:mem-free ", j++);
950 printf("%d:mem-used ", j++);
951 printf("%d:mem-total ", j++);
952 printf("%d:procs-in-run ", j++);
953 printf("%d:procs-in-iow ", j++);
955 cpus = get_number_cpus();
957 for (i = 0, j = 22; i < cpus; ++i) {
958 printf("%d:cpu%i-usr-per-t ", j++, i);
959 printf("%d:cpu%i-nice-per-t ", j++, i);
960 printf("%d:cpu%i-sys-per-t ", j++, i);
961 printf("%d:cpu%i-idle-per-t ", j++, i);
962 printf("%d:cpu%i-iow-per-t ", j++, i);
964 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
965 printf("%d:cpu%i-net-irqs ", j++, i);
967 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
968 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
969 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
970 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
973 if (iswireless(abs)) {
974 printf("%d:wifi-link-qual-per-t ", j++);
975 printf("%d:wifi-link-qual ", j++);
976 printf("%d:wifi-link-qual-max ", j++);
978 printf("%d:wifi-signal-dbm-per-t ", j++);
979 printf("%d:wifi-signal-dbm ", j++);
982 puts("");
983 printf("# data:\n");
984 fflush(stdout);
987 static int term_main(const char *ifname, uint64_t ms_interval)
989 int first = 1;
991 do {
992 stats_sample_generic(ifname, ms_interval);
994 if (first) {
995 first = 0;
996 term_csv_header(ifname, &stats_new, ms_interval);
999 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1000 } while (stats_loop && !sigint);
1002 return 0;
1005 int main(int argc, char **argv)
1007 short ifflags = 0;
1008 int c, opt_index, ret, promisc = 0;
1009 uint64_t interval = 1000;
1010 char *ifname = NULL;
1011 int (*func_main)(const char *ifname, uint64_t ms_interval) = 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 'l':
1032 stats_loop = 1;
1033 break;
1034 case 'p':
1035 promisc = 1;
1036 break;
1037 case 'c':
1038 func_main = term_main;
1039 break;
1040 case '?':
1041 switch (optopt) {
1042 case 'd':
1043 case 't':
1044 panic("Option -%c requires an argument!\n",
1045 optopt);
1046 default:
1047 if (isprint(optopt))
1048 printf("Unknown option character `0x%X\'!\n", optopt);
1049 die();
1051 default:
1052 break;
1056 if (argc == 1)
1057 help();
1059 if (argc == 2)
1060 ifname = xstrndup(argv[1], IFNAMSIZ);
1061 if (ifname == NULL)
1062 panic("No networking device given!\n");
1064 if (!strncmp("lo", ifname, IFNAMSIZ))
1065 panic("lo is not supported!\n");
1066 if (device_mtu(ifname) == 0)
1067 panic("This is no networking device!\n");
1069 register_signal(SIGINT, signal_handler);
1070 register_signal(SIGHUP, signal_handler);
1072 if (promisc)
1073 ifflags = enter_promiscuous_mode(ifname);
1074 ret = func_main(ifname, interval);
1075 if (promisc)
1076 leave_promiscuous_mode(ifname, ifflags);
1078 xfree(ifname);
1079 return ret;