configure: fix libnetfilter_conntrack dependency removal
[netsniff-ng.git] / ifpps.c
blobeb9b8991e776867236a94cd37a6aa296fa03b07e
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009 - 2013 Daniel Borkmann.
4 * Copyright 2013 Tobias Klauser
5 * Subject to the GPL, version 2.
6 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <curses.h>
11 #include <getopt.h>
12 #include <ctype.h>
13 #include <sys/socket.h>
14 #include <sys/fsuid.h>
15 #include <sys/types.h>
16 #include <sys/utsname.h>
17 #include <signal.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <time.h>
21 #include <dirent.h>
23 #include "die.h"
24 #include "dev.h"
25 #include "sig.h"
26 #include "str.h"
27 #include "link.h"
28 #include "xmalloc.h"
29 #include "ioops.h"
30 #include "promisc.h"
31 #include "cpus.h"
32 #include "config.h"
33 #include "built_in.h"
34 #include "screen.h"
36 struct wifi_stat {
37 uint32_t bitrate;
38 int16_t link_qual, link_qual_max;
39 int signal_level /*, noise_level*/;
42 struct ifstat {
43 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
44 long long unsigned int rx_fifo, rx_frame, rx_multi;
45 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
46 long long unsigned int tx_fifo, tx_colls, tx_carrier;
47 uint64_t mem_free, mem_total, mem_active, mem_inactive;
48 uint64_t swap_total, swap_free, swap_cached;
49 uint32_t irq_nr, procs_total, procs_run, procs_iow, cswitch;
50 struct wifi_stat wifi;
52 * Pointer members need to be last in order for stats_zero() to work
53 * properly.
55 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
56 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
59 struct cpu_hit {
60 unsigned int idx;
61 uint64_t hit;
62 long long unsigned int irqs_rel, irqs_abs;
65 struct avg_stat {
66 uint64_t cpu_user, cpu_sys, cpu_nice, cpu_idle, cpu_iow;
67 long double irqs_abs, irqs_rel, irqs_srx_rel, irqs_stx_rel;
70 static volatile sig_atomic_t sigint = 0;
71 static struct ifstat stats_old, stats_new, stats_delta;
72 static struct cpu_hit *cpu_hits;
73 static struct avg_stat stats_avg;
74 static int stats_loop = 0;
75 static WINDOW *stats_screen = NULL;
76 static struct utsname uts;
78 static const char *short_options = "d:t:n:vhclpW";
79 static const struct option long_options[] = {
80 {"dev", required_argument, NULL, 'd'},
81 {"interval", required_argument, NULL, 't'},
82 {"num-cpus", required_argument, NULL, 'n'},
83 {"promisc", no_argument, NULL, 'p'},
84 {"csv", no_argument, NULL, 'c'},
85 {"loop", no_argument, NULL, 'l'},
86 {"no-warn", no_argument, NULL, 'W'},
87 {"version", no_argument, NULL, 'v'},
88 {"help", no_argument, NULL, 'h'},
89 {NULL, 0, NULL, 0}
92 static void signal_handler(int number)
94 switch (number) {
95 case SIGINT:
96 sigint = 1;
97 break;
98 case SIGHUP:
99 default:
100 break;
104 static inline int iswireless(const struct ifstat *stats)
106 return stats->wifi.bitrate > 0;
109 static void __noreturn help(void)
111 printf("\nifpps %s, top-like kernel networking and system statistics\n",
112 VERSION_STRING);
113 puts("http://www.netsniff-ng.org\n\n"
114 "Usage: ifpps [options] || ifpps <netdev>\n"
115 "Options:\n"
116 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
117 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
118 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
119 " in ncurses mode (default 5)\n"
120 " -p|--promisc Promiscuous mode\n"
121 " -c|--csv Output to terminal as Gnuplot-ready data\n"
122 " -l|--loop Continuous CSV output\n"
123 " -W|--no-warn Suppress warnings\n"
124 " -v|--version Print version and exit\n"
125 " -h|--help Print this help and exit\n\n"
126 "Examples:\n"
127 " ifpps eth0\n"
128 " ifpps -pd eth0\n"
129 " ifpps -lpcd wlan0 > plot.dat\n\n"
130 "Note:\n"
131 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
132 " Thus, in those situations, it's good to use a -t of 10sec.\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 void __noreturn version(void)
145 printf("\nifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
146 puts("top-like kernel networking and system statistics\n"
147 "http://www.netsniff-ng.org\n\n"
148 "Please report bugs to <bugs@netsniff-ng.org>\n"
149 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
150 "Swiss federal institute of technology (ETH Zurich)\n"
151 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
152 "License: GNU GPL version 2.0\n"
153 "This is free software: you are free to change and redistribute it.\n"
154 "There is NO WARRANTY, to the extent permitted by law.\n");
155 die();
158 static inline int padding_from_num(int n)
160 int i = 0;
161 do i++;
162 while ((n /= 10) > 0);
163 return i;
166 #define STATS_ALLOC1(member) \
167 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
169 static void stats_alloc(struct ifstat *stats, int cpus)
171 STATS_ALLOC1(irqs);
172 STATS_ALLOC1(irqs_srx);
173 STATS_ALLOC1(irqs_stx);
175 STATS_ALLOC1(cpu_user);
176 STATS_ALLOC1(cpu_sys);
177 STATS_ALLOC1(cpu_nice);
178 STATS_ALLOC1(cpu_idle);
179 STATS_ALLOC1(cpu_iow);
182 #define STATS_ZERO1(member) \
183 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
185 static void stats_zero(struct ifstat *stats, int cpus)
187 /* Only clear the non-pointer members */
188 memset(stats, 0, offsetof(struct ifstat, irqs));
190 STATS_ZERO1(irqs);
191 STATS_ZERO1(irqs_srx);
192 STATS_ZERO1(irqs_stx);
194 STATS_ZERO1(cpu_user);
195 STATS_ZERO1(cpu_sys);
196 STATS_ZERO1(cpu_nice);
197 STATS_ZERO1(cpu_idle);
198 STATS_ZERO1(cpu_iow);
201 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
203 int ret = -EINVAL;
204 char buff[256];
205 FILE *fp;
207 fp = fopen("/proc/net/dev", "r");
208 if (!fp)
209 panic("Cannot open /proc/net/dev!\n");
211 if (fgets(buff, sizeof(buff), fp)) { ; }
212 if (fgets(buff, sizeof(buff), fp)) { ; }
214 memset(buff, 0, sizeof(buff));
216 while (fgets(buff, sizeof(buff), fp) != NULL) {
217 buff[sizeof(buff) -1] = 0;
219 if (strstr(buff, ifname) == NULL)
220 continue;
222 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
223 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
224 &stats->rx_bytes, &stats->rx_packets,
225 &stats->rx_errors, &stats->rx_drops,
226 &stats->rx_fifo, &stats->rx_frame,
227 &stats->rx_multi, &stats->tx_bytes,
228 &stats->tx_packets, &stats->tx_errors,
229 &stats->tx_drops, &stats->tx_fifo,
230 &stats->tx_colls, &stats->tx_carrier) == 14) {
231 ret = 0;
232 break;
235 memset(buff, 0, sizeof(buff));
238 fclose(fp);
239 return ret;
242 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
244 int ret = -EINVAL, i, cpus, try = 0;
245 char *ptr, *buff;
246 bool seen = false;
247 size_t buff_len;
248 struct ethtool_drvinfo drvinf;
249 FILE *fp;
251 fp = fopen("/proc/interrupts", "r");
252 if (!fp)
253 panic("Cannot open /proc/interrupts!\n");
255 cpus = get_number_cpus();
256 buff_len = cpus * 128;
257 buff = xmalloc(buff_len);
258 retry:
259 fseek(fp, 0, SEEK_SET);
260 memset(buff, 0, buff_len);
262 while (fgets(buff, buff_len, fp) != NULL) {
263 buff[buff_len - 1] = 0;
264 ptr = buff;
266 if (strstr(buff, ifname) == NULL)
267 continue;
269 /* XXX: remove this one here */
270 stats->irq_nr = strtol(ptr, &ptr, 10);
271 bug_on(stats->irq_nr == 0);
273 if (ptr)
274 ptr++;
275 for (i = 0; i < cpus && ptr; ++i) {
276 if (seen)
277 stats->irqs[i] += strtol(ptr, &ptr, 10);
278 else
279 stats->irqs[i] = strtol(ptr, &ptr, 10);
280 if (i == cpus - 1) {
281 ret = 0;
282 seen = true;
286 memset(buff, 0, buff_len);
289 if (ret == -EINVAL && try == 0) {
290 memset(&drvinf, 0, sizeof(drvinf));
291 if (ethtool_drvinf(ifname, &drvinf) < 0)
292 goto done;
294 ifname = drvinf.driver;
295 try++;
297 goto retry;
299 done:
300 xfree(buff);
301 fclose(fp);
302 return ret;
305 static int stats_proc_softirqs(struct ifstat *stats)
307 int i, cpus;
308 char *ptr, *buff;
309 size_t buff_len;
310 FILE *fp;
311 enum {
312 softirqs_net_rx,
313 softirqs_net_tx,
314 } net_type;
316 fp = fopen("/proc/softirqs", "r");
317 if (!fp)
318 panic("Cannot open /proc/softirqs!\n");
320 cpus = get_number_cpus();
321 buff_len = cpus * 128;
322 buff = xmalloc(buff_len);
324 memset(buff, 0, buff_len);
326 while (fgets(buff, buff_len, fp) != NULL) {
327 buff[buff_len - 1] = 0;
329 if ((ptr = strstr(buff, "NET_TX:")))
330 net_type = softirqs_net_tx;
331 else if ((ptr = strstr(buff, "NET_RX:")))
332 net_type = softirqs_net_rx;
333 else
334 continue;
336 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
337 switch (net_type) {
338 case softirqs_net_tx:
339 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
340 break;
341 case softirqs_net_rx:
342 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
343 break;
347 memset(buff, 0, buff_len);
350 xfree(buff);
351 fclose(fp);
352 return 0;
355 static int stats_proc_memory(struct ifstat *stats)
357 char *ptr, buff[256];
358 FILE *fp;
360 fp = fopen("/proc/meminfo", "r");
361 if (!fp)
362 panic("Cannot open /proc/meminfo!\n");
364 memset(buff, 0, sizeof(buff));
366 while (fgets(buff, sizeof(buff), fp) != NULL) {
367 buff[sizeof(buff) - 1] = 0;
369 if ((ptr = strstr(buff, "MemTotal:"))) {
370 ptr += strlen("MemTotal:");
371 stats->mem_total = strtoul(ptr, &ptr, 10);
372 } else if ((ptr = strstr(buff, "MemFree:"))) {
373 ptr += strlen("MemFree:");
374 stats->mem_free = strtoul(ptr, &ptr, 10);
375 } else if ((ptr = strstr(buff, "Active:"))) {
376 ptr += strlen("Active:");
377 stats->mem_active = strtoul(ptr, &ptr, 10);
378 } else if ((ptr = strstr(buff, "Inactive:"))) {
379 ptr += strlen("Inactive:");
380 stats->mem_inactive = strtoul(ptr, &ptr, 10);
381 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
382 ptr += strlen("SwapTotal:");
383 stats->swap_total = strtoul(ptr, &ptr, 10);
384 } else if ((ptr = strstr(buff, "SwapFree:"))) {
385 ptr += strlen("SwapFree:");
386 stats->swap_free = strtoul(ptr, &ptr, 10);
387 } else if ((ptr = strstr(buff, "SwapCached:"))) {
388 ptr += strlen("SwapCached:");
389 stats->swap_cached = strtoul(ptr, &ptr, 10);
392 memset(buff, 0, sizeof(buff));
395 fclose(fp);
396 return 0;
399 static int stats_proc_system(struct ifstat *stats)
401 int cpu, cpus;
402 char *ptr, buff[256];
403 FILE *fp;
405 fp = fopen("/proc/stat", "r");
406 if (!fp)
407 panic("Cannot open /proc/stat!\n");
409 cpus = get_number_cpus();
411 memset(buff, 0, sizeof(buff));
413 while (fgets(buff, sizeof(buff), fp) != NULL) {
414 buff[sizeof(buff) - 1] = 0;
416 if ((ptr = strstr(buff, "cpu"))) {
417 ptr += strlen("cpu");
418 if (isblank(*ptr))
419 goto next;
421 cpu = strtol(ptr, &ptr, 10);
422 bug_on(cpu > cpus);
424 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
425 &stats->cpu_user[cpu],
426 &stats->cpu_nice[cpu],
427 &stats->cpu_sys[cpu],
428 &stats->cpu_idle[cpu],
429 &stats->cpu_iow[cpu]) != 5)
430 goto next;
431 } else if ((ptr = strstr(buff, "ctxt"))) {
432 ptr += strlen("ctxt");
433 stats->cswitch = strtoul(ptr, &ptr, 10);
434 } else if ((ptr = strstr(buff, "procs_running"))) {
435 ptr += strlen("procs_running");
436 stats->procs_run = strtoul(ptr, &ptr, 10);
437 } else if ((ptr = strstr(buff, "procs_blocked"))) {
438 ptr += strlen("procs_blocked");
439 stats->procs_iow = strtoul(ptr, &ptr, 10);
441 next:
442 memset(buff, 0, sizeof(buff));
445 fclose(fp);
446 return 0;
449 static int stats_proc_procs(struct ifstat *stats)
451 DIR *dir;
452 struct dirent *e;
454 dir = opendir("/proc");
455 if (!dir)
456 panic("Cannot open /proc\n");
458 stats->procs_total = 0;
460 while ((e = readdir(dir)) != NULL) {
461 const char *name = e->d_name;
462 char *end;
463 unsigned int pid = strtoul(name, &end, 10);
465 /* not a number */
466 if (pid == 0 && end == name)
467 continue;
469 stats->procs_total++;
472 closedir(dir);
474 return 0;
477 static int adjust_dbm_level(int in_dbm, int dbm_val)
479 if (!in_dbm)
480 return dbm_val;
482 return dbm_val - 0x100;
485 static int stats_wireless(const char *ifname, struct ifstat *stats)
487 int ret;
488 struct iw_statistics ws;
490 ret = wireless_sigqual(ifname, &ws);
491 if (ret != 0) {
492 stats->wifi.bitrate = 0;
493 return -EINVAL;
496 stats->wifi.bitrate = wireless_bitrate(ifname);
498 stats->wifi.signal_level =
499 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
501 stats->wifi.link_qual = ws.qual.qual;
502 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
504 return ret;
507 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
508 #define DIFF(member) do { \
509 if (sizeof(diff->member) != sizeof(new->member) || \
510 sizeof(diff->member) != sizeof(old->member)) \
511 bug(); \
512 bug_on((new->member - old->member) > (new->member)); \
513 DIFF1(member); \
514 } while (0)
516 static void stats_diff(struct ifstat *old, struct ifstat *new,
517 struct ifstat *diff)
519 int cpus, i;
521 DIFF(rx_bytes);
522 DIFF(rx_packets);
523 DIFF(rx_drops);
524 DIFF(rx_errors);
525 DIFF(rx_fifo);
526 DIFF(rx_frame);
527 DIFF(rx_multi);
529 DIFF(tx_bytes);
530 DIFF(tx_packets);
531 DIFF(tx_drops);
532 DIFF(tx_errors);
533 DIFF(tx_fifo);
534 DIFF(tx_colls);
535 DIFF(tx_carrier);
537 DIFF1(wifi.signal_level);
538 DIFF1(wifi.link_qual);
540 DIFF1(cswitch);
542 cpus = get_number_cpus();
544 for (i = 0; i < cpus; ++i) {
545 DIFF(irqs[i]);
546 DIFF(irqs_srx[i]);
547 DIFF(irqs_stx[i]);
549 DIFF1(cpu_user[i]);
550 DIFF1(cpu_nice[i]);
551 DIFF1(cpu_sys[i]);
552 DIFF1(cpu_idle[i]);
553 DIFF1(cpu_iow[i]);
557 static void stats_fetch(const char *ifname, struct ifstat *stats)
559 if (stats_proc_net_dev(ifname, stats) < 0)
560 panic("Cannot fetch device stats!\n");
561 if (stats_proc_softirqs(stats) < 0)
562 panic("Cannot fetch software interrupts!\n");
563 if (stats_proc_memory(stats) < 0)
564 panic("Cannot fetch memory stats!\n");
565 if (stats_proc_system(stats) < 0)
566 panic("Cannot fetch system stats!\n");
567 if (stats_proc_procs(stats) < 0)
568 panic("Cannot fetch process stats!\n");
570 stats_proc_interrupts((char *) ifname, stats);
572 stats_wireless(ifname, stats);
575 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
577 int cpus = get_number_cpus();
579 stats_zero(&stats_old, cpus);
580 stats_zero(&stats_new, cpus);
581 stats_zero(&stats_delta, cpus);
583 stats_fetch(ifname, &stats_old);
584 usleep(ms_interval * 1000);
585 stats_fetch(ifname, &stats_new);
587 stats_diff(&stats_old, &stats_new, &stats_delta);
590 static int cmp_hits(const void *p1, const void *p2)
592 const struct cpu_hit *h1 = p1, *h2 = p2;
595 * We want the hits sorted in descending order, thus reverse the return
596 * values.
598 if (h1->hit == h2->hit)
599 return 0;
600 else if (h1->hit < h2->hit)
601 return 1;
602 else
603 return -1;
606 static int cmp_irqs_rel(const void *p1, const void *p2)
608 const struct cpu_hit *h1 = p1, *h2 = p2;
611 * We want the hits sorted in descending order, thus reverse the return
612 * values.
614 if (h1->irqs_rel == h2->irqs_rel)
615 return 0;
616 else if (h1->irqs_rel < h2->irqs_rel)
617 return 1;
618 else
619 return -1;
622 static int cmp_irqs_abs(const void *p1, const void *p2)
624 const struct cpu_hit *h1 = p1, *h2 = p2;
627 * We want the hits sorted in descending order, thus reverse the return
628 * values.
630 if (h1->irqs_abs == h2->irqs_abs)
631 return 0;
632 else if (h1->irqs_abs < h2->irqs_abs)
633 return 1;
634 else
635 return -1;
638 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
639 int cpus)
641 int i;
643 memset(&stats_avg, 0, sizeof(stats_avg));
645 for (i = 0; i < cpus; ++i) {
646 cpu_hits[i].idx = i;
647 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
648 cpu_hits[i].irqs_rel = rel->irqs[i];
649 cpu_hits[i].irqs_abs = abs->irqs[i];
651 stats_avg.cpu_user += rel->cpu_user[i];
652 stats_avg.cpu_sys += rel->cpu_sys[i];
653 stats_avg.cpu_nice += rel->cpu_nice[i];
654 stats_avg.cpu_idle += rel->cpu_idle[i];
655 stats_avg.cpu_iow += rel->cpu_iow[i];
657 stats_avg.irqs_abs += abs->irqs[i];
658 stats_avg.irqs_rel += rel->irqs[i];
659 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
660 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
663 stats_avg.cpu_user /= cpus;
664 stats_avg.cpu_sys /= cpus;
665 stats_avg.cpu_nice /= cpus;
666 stats_avg.cpu_idle /= cpus;
667 stats_avg.cpu_iow /= cpus;
668 stats_avg.irqs_abs /= cpus;
669 stats_avg.irqs_rel /= cpus;
670 stats_avg.irqs_srx_rel /= cpus;
671 stats_avg.irqs_stx_rel /= cpus;
674 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
675 uint64_t ms_interval, unsigned int top_cpus)
677 size_t len = 0;
678 char buff[64], machine[64];
679 struct ethtool_drvinfo drvinf;
680 u32 rate = device_bitrate(ifname);
681 int link = ethtool_link(ifname);
682 unsigned int cpus = get_number_cpus();
684 memset(&drvinf, 0, sizeof(drvinf));
685 ethtool_drvinf(ifname, &drvinf);
687 memset(buff, 0, sizeof(buff));
688 memset(machine, 0, sizeof(machine));
690 if (rate)
691 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
692 if (link >= 0)
693 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
694 link == 0 ? "no" : "yes");
696 if (!strstr(uts.release, uts.machine))
697 slprintf(machine, sizeof(machine), " %s,", uts.machine);
699 mvwprintw(screen, (*voff)++, 2,
700 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
701 " ", uts.release, machine,
702 ifname, drvinf.driver, buff, ms_interval, top_cpus,
703 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
706 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
707 int *voff)
709 attron(A_REVERSE);
711 mvwprintw(screen, (*voff)++, 0,
712 " rx: %16.3llf MiB/t "
713 "%10llu pkts/t "
714 "%10llu drops/t "
715 "%10llu errors/t ",
716 ((long double) rel->rx_bytes) / (1LLU << 20),
717 rel->rx_packets, rel->rx_drops, rel->rx_errors);
719 mvwprintw(screen, (*voff)++, 0,
720 " tx: %16.3llf MiB/t "
721 "%10llu pkts/t "
722 "%10llu drops/t "
723 "%10llu errors/t ",
724 ((long double) rel->tx_bytes) / (1LLU << 20),
725 rel->tx_packets, rel->tx_drops, rel->tx_errors);
727 attroff(A_REVERSE);
730 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
731 int *voff)
733 mvwprintw(screen, (*voff)++, 2,
734 "rx: %16.3llf MiB "
735 "%10llu pkts "
736 "%10llu drops "
737 "%10llu errors",
738 ((long double) abs->rx_bytes) / (1LLU << 20),
739 abs->rx_packets, abs->rx_drops, abs->rx_errors);
741 mvwprintw(screen, (*voff)++, 2,
742 "tx: %16.3llf MiB "
743 "%10llu pkts "
744 "%10llu drops "
745 "%10llu errors",
746 ((long double) abs->tx_bytes) / (1LLU << 20),
747 abs->tx_packets, abs->tx_drops, abs->tx_errors);
750 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
751 const struct ifstat *abs, int *voff)
753 mvwprintw(screen, (*voff)++, 2,
754 "sys: %14u cs/t "
755 "%11u procs "
756 "%11u running "
757 "%10u iowait",
758 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
761 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
763 mvwprintw(screen, (*voff)++, 2,
764 "mem: %13uM total "
765 "%9uM used "
766 "%11uM active "
767 "%10uM inactive",
768 abs->mem_total / 1024,
769 (abs->mem_total - abs->mem_free) / 1024,
770 abs->mem_active / 1024,
771 abs->mem_inactive / 1024);
773 mvwprintw(screen, (*voff)++, 2,
774 "swap: %12uM total "
775 "%9uM used "
776 "%11uM cached",
777 abs->swap_total / 1024,
778 (abs->swap_total - abs->swap_free) / 1024,
779 abs->swap_cached / 1024);
782 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
783 int *voff, unsigned int idx, char *tag)
785 int max_padd = padding_from_num(get_number_cpus());
786 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
787 rel->cpu_idle[idx] + rel->cpu_iow[idx];
789 mvwprintw(screen, (*voff)++, 2,
790 "cpu%*d %s: %11.1lf%% usr/t "
791 "%9.1lf%% sys/t "
792 "%10.1lf%% idl/t "
793 "%11.1lf%% iow/t",
794 max_padd, idx, tag,
795 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
796 100.0 * rel->cpu_sys[idx] / all,
797 100.0 * rel->cpu_idle[idx] / all,
798 100.0 * rel->cpu_iow[idx] / all);
801 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
802 const struct avg_stat *avg, int top_cpus,
803 int *voff)
805 int i;
806 int cpus = get_number_cpus();
807 int max_padd = padding_from_num(cpus);
808 uint64_t all;
810 if (top_cpus == 0)
811 return;
813 /* Display top hitter */
814 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
816 /* Make sure we don't display the min. hitter twice */
817 if (top_cpus == cpus)
818 top_cpus--;
820 for (i = 1; i < top_cpus; ++i)
821 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "|");
823 /* Display minimum hitter */
824 if (cpus != 1)
825 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
827 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
828 mvwprintw(screen, (*voff)++, 2,
829 "avg:%*s%14.1lf%% "
830 "%9.1lf%% "
831 "%10.1lf%% "
832 "%11.1lf%%", max_padd, "",
833 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
834 100.0 * avg->cpu_sys / all,
835 100.0 * avg->cpu_idle /all,
836 100.0 * avg->cpu_iow / all);
839 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
840 int *voff, unsigned int idx, char *tag)
842 int max_padd = padding_from_num(get_number_cpus());
844 mvwprintw(screen, (*voff)++, 2,
845 "cpu%*d %s: %12llu irqs/t "
846 "%17llu sirq rx/t "
847 "%17llu sirq tx/t",
848 max_padd, idx, tag,
849 rel->irqs[idx],
850 rel->irqs_srx[idx],
851 rel->irqs_stx[idx]);
854 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
855 const struct avg_stat *avg, int top_cpus,
856 int *voff)
858 int i;
859 int cpus = get_number_cpus();
860 int max_padd = padding_from_num(cpus);
862 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
864 if (top_cpus == cpus)
865 top_cpus--;
867 for (i = 1; i < top_cpus; ++i)
868 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
870 if (cpus != 1)
871 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
873 mvwprintw(screen, (*voff)++, 2,
874 "avg:%*s%17.1Lf "
875 "%17.1Lf "
876 "%17.1Lf", max_padd, "",
877 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
880 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
881 int *voff, unsigned int idx, char *tag)
883 int max_padd = padding_from_num(get_number_cpus());
885 mvwprintw(screen, (*voff)++, 2,
886 "cpu%*d %s: %12llu irqs",
887 max_padd, idx, tag, abs->irqs[idx]);
890 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
891 const struct avg_stat *avg, int top_cpus,
892 int *voff)
894 int i;
895 int cpus = get_number_cpus();
896 int max_padd = padding_from_num(cpus);
898 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
900 if (top_cpus == cpus)
901 top_cpus--;
903 for (i = 1; i < top_cpus; ++i)
904 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
906 if (cpus != 1)
907 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
909 mvwprintw(screen, (*voff)++, 2,
910 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
913 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
914 const struct ifstat *abs, int *voff)
916 if (iswireless(abs)) {
917 mvwprintw(screen, (*voff)++, 2,
918 "linkqual: %7d/%d (%d/t) ",
919 abs->wifi.link_qual,
920 abs->wifi.link_qual_max,
921 rel->wifi.link_qual);
923 mvwprintw(screen, (*voff)++, 2,
924 "signal: %8d dBm (%d dBm/t) ",
925 abs->wifi.signal_level,
926 rel->wifi.signal_level);
930 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
931 const struct ifstat *abs, const struct avg_stat *avg,
932 int *first, uint64_t ms_interval, unsigned int top_cpus,
933 bool need_info)
935 int cpus, top, voff = 1, cvoff = 2;
937 curs_set(0);
939 cpus = get_number_cpus();
940 top = min(cpus, top_cpus);
942 stats_top(rel, abs, cpus);
944 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
946 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
948 voff++;
949 screen_net_dev_rel(screen, rel, &voff);
951 voff++;
952 screen_net_dev_abs(screen, abs, &voff);
954 voff++;
955 screen_sys(screen, rel, abs, &voff);
957 voff++;
958 screen_mem_swap(screen, abs, &voff);
960 voff++;
961 screen_percpu_states(screen, rel, avg, top, &voff);
963 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
965 voff++;
966 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
968 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
970 voff++;
971 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
973 voff++;
974 screen_wireless(screen, rel, abs, &voff);
976 if (*first) {
977 mvwprintw(screen, cvoff, 2, "Collecting data ...");
978 *first = 0;
979 } else {
980 if (need_info)
981 mvwprintw(screen, cvoff, 2, "(consider to increase "
982 "your sampling interval, e.g. -t 10000)");
983 else
984 mvwprintw(screen, cvoff, 2, " "
985 " ");
988 wrefresh(screen);
989 refresh();
992 static int screen_main(const char *ifname, uint64_t ms_interval,
993 unsigned int top_cpus, bool suppress_warnings)
995 int first = 1, key;
996 u32 rate = device_bitrate(ifname);
997 bool need_info = false;
999 stats_screen = screen_init(true);
1001 if (rate > SPEED_1000 && ms_interval <= 1000 && !suppress_warnings)
1002 need_info = true;
1004 while (!sigint) {
1005 key = getch();
1006 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1007 break;
1009 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1010 &first, ms_interval, top_cpus, need_info);
1012 stats_sample_generic(ifname, ms_interval);
1015 screen_end();
1017 return 0;
1020 static void term_csv(const char *ifname, const struct ifstat *rel,
1021 const struct ifstat *abs, uint64_t ms_interval)
1023 int cpus, i;
1025 printf("%ld ", time(0));
1027 printf("%llu ", rel->rx_bytes);
1028 printf("%llu ", rel->rx_packets);
1029 printf("%llu ", rel->rx_drops);
1030 printf("%llu ", rel->rx_errors);
1032 printf("%llu ", abs->rx_bytes);
1033 printf("%llu ", abs->rx_packets);
1034 printf("%llu ", abs->rx_drops);
1035 printf("%llu ", abs->rx_errors);
1037 printf("%llu ", rel->tx_bytes);
1038 printf("%llu ", rel->tx_packets);
1039 printf("%llu ", rel->tx_drops);
1040 printf("%llu ", rel->tx_errors);
1042 printf("%llu ", abs->tx_bytes);
1043 printf("%llu ", abs->tx_packets);
1044 printf("%llu ", abs->tx_drops);
1045 printf("%llu ", abs->tx_errors);
1047 printf("%u ", rel->cswitch);
1048 printf("%lu ", abs->mem_free);
1049 printf("%lu ", abs->mem_total - abs->mem_free);
1050 printf("%lu ", abs->mem_total);
1051 printf("%lu ", abs->swap_free);
1052 printf("%lu ", abs->swap_total - abs->swap_free);
1053 printf("%lu ", abs->swap_total);
1054 printf("%u ", abs->procs_total);
1055 printf("%u ", abs->procs_run);
1056 printf("%u ", abs->procs_iow);
1058 cpus = get_number_cpus();
1060 for (i = 0; i < cpus; ++i) {
1061 printf("%lu ", rel->cpu_user[i]);
1062 printf("%lu ", rel->cpu_nice[i]);
1063 printf("%lu ", rel->cpu_sys[i]);
1064 printf("%lu ", rel->cpu_idle[i]);
1065 printf("%lu ", rel->cpu_iow[i]);
1067 printf("%llu ", rel->irqs[i]);
1068 printf("%llu ", abs->irqs[i]);
1070 printf("%llu ", rel->irqs_srx[i]);
1071 printf("%llu ", abs->irqs_srx[i]);
1073 printf("%llu ", rel->irqs_stx[i]);
1074 printf("%llu ", abs->irqs_stx[i]);
1077 if (iswireless(abs)) {
1078 printf("%u ", rel->wifi.link_qual);
1079 printf("%u ", abs->wifi.link_qual);
1080 printf("%u ", abs->wifi.link_qual_max);
1082 printf("%d ", rel->wifi.signal_level);
1083 printf("%d ", abs->wifi.signal_level);
1086 puts("");
1087 fflush(stdout);
1090 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1091 uint64_t ms_interval)
1093 int cpus, i, j = 1;
1095 printf("# gnuplot dump (#col:description)\n");
1096 printf("# networking interface: %s\n", ifname);
1097 printf("# sampling interval (t): %lu ms\n", ms_interval);
1098 printf("# %d:unixtime ", j++);
1100 printf("%d:rx-bytes-per-t ", j++);
1101 printf("%d:rx-pkts-per-t ", j++);
1102 printf("%d:rx-drops-per-t ", j++);
1103 printf("%d:rx-errors-per-t ", j++);
1105 printf("%d:rx-bytes ", j++);
1106 printf("%d:rx-pkts ", j++);
1107 printf("%d:rx-drops ", j++);
1108 printf("%d:rx-errors ", j++);
1110 printf("%d:tx-bytes-per-t ", j++);
1111 printf("%d:tx-pkts-per-t ", j++);
1112 printf("%d:tx-drops-per-t ", j++);
1113 printf("%d:tx-errors-per-t ", j++);
1115 printf("%d:tx-bytes ", j++);
1116 printf("%d:tx-pkts ", j++);
1117 printf("%d:tx-drops ", j++);
1118 printf("%d:tx-errors ", j++);
1120 printf("%d:context-switches-per-t ", j++);
1121 printf("%d:mem-free ", j++);
1122 printf("%d:mem-used ", j++);
1123 printf("%d:mem-total ", j++);
1124 printf("%d:swap-free ", j++);
1125 printf("%d:swap-used ", j++);
1126 printf("%d:swap-total ", j++);
1127 printf("%d:procs-total ", j++);
1128 printf("%d:procs-in-run ", j++);
1129 printf("%d:procs-in-iow ", j++);
1131 cpus = get_number_cpus();
1133 for (i = 0; i < cpus; ++i) {
1134 printf("%d:cpu%i-usr-per-t ", j++, i);
1135 printf("%d:cpu%i-nice-per-t ", j++, i);
1136 printf("%d:cpu%i-sys-per-t ", j++, i);
1137 printf("%d:cpu%i-idle-per-t ", j++, i);
1138 printf("%d:cpu%i-iow-per-t ", j++, i);
1140 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1141 printf("%d:cpu%i-net-irqs ", j++, i);
1143 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1144 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1145 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1146 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1149 if (iswireless(abs)) {
1150 printf("%d:wifi-link-qual-per-t ", j++);
1151 printf("%d:wifi-link-qual ", j++);
1152 printf("%d:wifi-link-qual-max ", j++);
1154 printf("%d:wifi-signal-dbm-per-t ", j++);
1155 printf("%d:wifi-signal-dbm ", j++);
1158 puts("");
1159 printf("# data:\n");
1160 fflush(stdout);
1163 static int term_main(const char *ifname, uint64_t ms_interval,
1164 unsigned int top_cpus __maybe_unused,
1165 bool suppress_warnings __maybe_unused)
1167 int first = 1;
1169 do {
1170 stats_sample_generic(ifname, ms_interval);
1172 if (first) {
1173 first = 0;
1174 term_csv_header(ifname, &stats_new, ms_interval);
1177 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1178 } while (stats_loop && !sigint);
1180 return 0;
1183 int main(int argc, char **argv)
1185 short ifflags = 0;
1186 int c, opt_index, ret, cpus, promisc = 0;
1187 unsigned int top_cpus = 5;
1188 uint64_t interval = 1000;
1189 char *ifname = NULL;
1190 bool suppress_warnings = false;
1191 int (*func_main)(const char *ifname, uint64_t ms_interval,
1192 unsigned int top_cpus, bool suppress_warnings);
1194 func_main = screen_main;
1196 setfsuid(getuid());
1197 setfsgid(getgid());
1199 while ((c = getopt_long(argc, argv, short_options, long_options,
1200 &opt_index)) != EOF) {
1201 switch (c) {
1202 case 'h':
1203 help();
1204 break;
1205 case 'v':
1206 version();
1207 break;
1208 case 'W':
1209 suppress_warnings = true;
1210 break;
1211 case 'd':
1212 ifname = xstrndup(optarg, IFNAMSIZ);
1213 break;
1214 case 't':
1215 interval = strtoul(optarg, NULL, 10);
1216 break;
1217 case 'n':
1218 top_cpus = strtoul(optarg, NULL, 10);
1219 if (top_cpus < 1)
1220 panic("Number of top hitter CPUs must be greater than 0");
1221 break;
1222 case 'l':
1223 stats_loop = 1;
1224 break;
1225 case 'p':
1226 promisc = 1;
1227 break;
1228 case 'c':
1229 func_main = term_main;
1230 break;
1231 case '?':
1232 switch (optopt) {
1233 case 'd':
1234 case 't':
1235 panic("Option -%c requires an argument!\n",
1236 optopt);
1237 default:
1238 if (isprint(optopt))
1239 printf("Unknown option character `0x%X\'!\n", optopt);
1240 die();
1242 default:
1243 break;
1247 if (argc == 1)
1248 help();
1250 if (argc == 2)
1251 ifname = xstrndup(argv[1], IFNAMSIZ);
1252 if (ifname == NULL)
1253 panic("No networking device given!\n");
1255 if (!strncmp("lo", ifname, IFNAMSIZ))
1256 panic("lo is not supported!\n");
1257 if (device_mtu(ifname) == 0)
1258 panic("This is no networking device!\n");
1260 register_signal(SIGINT, signal_handler);
1261 register_signal(SIGHUP, signal_handler);
1263 cpus = get_number_cpus();
1264 top_cpus = min(top_cpus, cpus);
1265 if (uname(&uts) < 0)
1266 panic("Cannot execute uname!\n");
1268 stats_alloc(&stats_old, cpus);
1269 stats_alloc(&stats_new, cpus);
1270 stats_alloc(&stats_delta, cpus);
1272 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1274 if (promisc)
1275 ifflags = enter_promiscuous_mode(ifname);
1276 ret = func_main(ifname, interval, top_cpus, suppress_warnings);
1277 if (promisc)
1278 leave_promiscuous_mode(ifname, ifflags);
1280 xfree(ifname);
1281 return ret;