ifpps: show machine type only if not in already release name
[netsniff-ng.git] / ifpps.c
blob1a68c2375a9238245abf16784d2d3c75482540e7
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:vhclp";
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 {"version", no_argument, NULL, 'v'},
87 {"help", no_argument, NULL, 'h'},
88 {NULL, 0, NULL, 0}
91 static void signal_handler(int number)
93 switch (number) {
94 case SIGINT:
95 sigint = 1;
96 break;
97 case SIGHUP:
98 default:
99 break;
103 static inline int iswireless(const struct ifstat *stats)
105 return stats->wifi.bitrate > 0;
108 static void __noreturn help(void)
110 printf("\nifpps %s, top-like kernel networking and system statistics\n",
111 VERSION_STRING);
112 puts("http://www.netsniff-ng.org\n\n"
113 "Usage: ifpps [options] || ifpps <netdev>\n"
114 "Options:\n"
115 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
116 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
117 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
118 " in ncurses mode (default 5)\n"
119 " -p|--promisc Promiscuous mode\n"
120 " -c|--csv Output to terminal as Gnuplot-ready data\n"
121 " -l|--loop Continuous CSV output\n"
122 " -v|--version Print version and exit\n"
123 " -h|--help Print this help and exit\n\n"
124 "Examples:\n"
125 " ifpps eth0\n"
126 " ifpps -pd eth0\n"
127 " ifpps -lpcd wlan0 > plot.dat\n\n"
128 "Note:\n"
129 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
130 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
131 "Please report bugs to <bugs@netsniff-ng.org>\n"
132 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
133 "Swiss federal institute of technology (ETH Zurich)\n"
134 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
135 "License: GNU GPL version 2.0\n"
136 "This is free software: you are free to change and redistribute it.\n"
137 "There is NO WARRANTY, to the extent permitted by law.\n");
138 die();
141 static void __noreturn version(void)
143 printf("\nifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
144 puts("top-like kernel networking and system statistics\n"
145 "http://www.netsniff-ng.org\n\n"
146 "Please report bugs to <bugs@netsniff-ng.org>\n"
147 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
148 "Swiss federal institute of technology (ETH Zurich)\n"
149 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
150 "License: GNU GPL version 2.0\n"
151 "This is free software: you are free to change and redistribute it.\n"
152 "There is NO WARRANTY, to the extent permitted by law.\n");
153 die();
156 static inline int padding_from_num(int n)
158 int i = 0;
159 do i++;
160 while ((n /= 10) > 0);
161 return i;
164 #define STATS_ALLOC1(member) \
165 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
167 static void stats_alloc(struct ifstat *stats, int cpus)
169 STATS_ALLOC1(irqs);
170 STATS_ALLOC1(irqs_srx);
171 STATS_ALLOC1(irqs_stx);
173 STATS_ALLOC1(cpu_user);
174 STATS_ALLOC1(cpu_sys);
175 STATS_ALLOC1(cpu_nice);
176 STATS_ALLOC1(cpu_idle);
177 STATS_ALLOC1(cpu_iow);
180 #define STATS_ZERO1(member) \
181 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
183 static void stats_zero(struct ifstat *stats, int cpus)
185 /* Only clear the non-pointer members */
186 memset(stats, 0, offsetof(struct ifstat, irqs));
188 STATS_ZERO1(irqs);
189 STATS_ZERO1(irqs_srx);
190 STATS_ZERO1(irqs_stx);
192 STATS_ZERO1(cpu_user);
193 STATS_ZERO1(cpu_sys);
194 STATS_ZERO1(cpu_nice);
195 STATS_ZERO1(cpu_idle);
196 STATS_ZERO1(cpu_iow);
199 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
201 int ret = -EINVAL;
202 char buff[256];
203 FILE *fp;
205 fp = fopen("/proc/net/dev", "r");
206 if (!fp)
207 panic("Cannot open /proc/net/dev!\n");
209 if (fgets(buff, sizeof(buff), fp)) { ; }
210 if (fgets(buff, sizeof(buff), fp)) { ; }
212 memset(buff, 0, sizeof(buff));
214 while (fgets(buff, sizeof(buff), fp) != NULL) {
215 buff[sizeof(buff) -1] = 0;
217 if (strstr(buff, ifname) == NULL)
218 continue;
220 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
221 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
222 &stats->rx_bytes, &stats->rx_packets,
223 &stats->rx_errors, &stats->rx_drops,
224 &stats->rx_fifo, &stats->rx_frame,
225 &stats->rx_multi, &stats->tx_bytes,
226 &stats->tx_packets, &stats->tx_errors,
227 &stats->tx_drops, &stats->tx_fifo,
228 &stats->tx_colls, &stats->tx_carrier) == 14) {
229 ret = 0;
230 break;
233 memset(buff, 0, sizeof(buff));
236 fclose(fp);
237 return ret;
240 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
242 int ret = -EINVAL, i, cpus, try = 0;
243 char *ptr, *buff;
244 bool seen = false;
245 size_t buff_len;
246 struct ethtool_drvinfo drvinf;
247 FILE *fp;
249 fp = fopen("/proc/interrupts", "r");
250 if (!fp)
251 panic("Cannot open /proc/interrupts!\n");
253 cpus = get_number_cpus();
254 buff_len = cpus * 128;
255 buff = xmalloc(buff_len);
256 retry:
257 fseek(fp, 0, SEEK_SET);
258 memset(buff, 0, buff_len);
260 while (fgets(buff, buff_len, fp) != NULL) {
261 buff[buff_len - 1] = 0;
262 ptr = buff;
264 if (strstr(buff, ifname) == NULL)
265 continue;
267 /* XXX: remove this one here */
268 stats->irq_nr = strtol(ptr, &ptr, 10);
269 bug_on(stats->irq_nr == 0);
271 if (ptr)
272 ptr++;
273 for (i = 0; i < cpus && ptr; ++i) {
274 if (seen)
275 stats->irqs[i] += strtol(ptr, &ptr, 10);
276 else
277 stats->irqs[i] = strtol(ptr, &ptr, 10);
278 if (i == cpus - 1) {
279 ret = 0;
280 seen = true;
284 memset(buff, 0, buff_len);
287 if (ret == -EINVAL && try == 0) {
288 memset(&drvinf, 0, sizeof(drvinf));
289 if (ethtool_drvinf(ifname, &drvinf) < 0)
290 goto done;
292 ifname = drvinf.driver;
293 try++;
295 goto retry;
297 done:
298 xfree(buff);
299 fclose(fp);
300 return ret;
303 static int stats_proc_softirqs(struct ifstat *stats)
305 int i, cpus;
306 char *ptr, *buff;
307 size_t buff_len;
308 FILE *fp;
309 enum {
310 softirqs_net_rx,
311 softirqs_net_tx,
312 } net_type;
314 fp = fopen("/proc/softirqs", "r");
315 if (!fp)
316 panic("Cannot open /proc/softirqs!\n");
318 cpus = get_number_cpus();
319 buff_len = cpus * 128;
320 buff = xmalloc(buff_len);
322 memset(buff, 0, buff_len);
324 while (fgets(buff, buff_len, fp) != NULL) {
325 buff[buff_len - 1] = 0;
327 if ((ptr = strstr(buff, "NET_TX:")))
328 net_type = softirqs_net_tx;
329 else if ((ptr = strstr(buff, "NET_RX:")))
330 net_type = softirqs_net_rx;
331 else
332 continue;
334 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
335 switch (net_type) {
336 case softirqs_net_tx:
337 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
338 break;
339 case softirqs_net_rx:
340 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
341 break;
345 memset(buff, 0, buff_len);
348 xfree(buff);
349 fclose(fp);
350 return 0;
353 static int stats_proc_memory(struct ifstat *stats)
355 char *ptr, buff[256];
356 FILE *fp;
358 fp = fopen("/proc/meminfo", "r");
359 if (!fp)
360 panic("Cannot open /proc/meminfo!\n");
362 memset(buff, 0, sizeof(buff));
364 while (fgets(buff, sizeof(buff), fp) != NULL) {
365 buff[sizeof(buff) - 1] = 0;
367 if ((ptr = strstr(buff, "MemTotal:"))) {
368 ptr += strlen("MemTotal:");
369 stats->mem_total = strtoul(ptr, &ptr, 10);
370 } else if ((ptr = strstr(buff, "MemFree:"))) {
371 ptr += strlen("MemFree:");
372 stats->mem_free = strtoul(ptr, &ptr, 10);
373 } else if ((ptr = strstr(buff, "Active:"))) {
374 ptr += strlen("Active:");
375 stats->mem_active = strtoul(ptr, &ptr, 10);
376 } else if ((ptr = strstr(buff, "Inactive:"))) {
377 ptr += strlen("Inactive:");
378 stats->mem_inactive = strtoul(ptr, &ptr, 10);
379 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
380 ptr += strlen("SwapTotal:");
381 stats->swap_total = strtoul(ptr, &ptr, 10);
382 } else if ((ptr = strstr(buff, "SwapFree:"))) {
383 ptr += strlen("SwapFree:");
384 stats->swap_free = strtoul(ptr, &ptr, 10);
385 } else if ((ptr = strstr(buff, "SwapCached:"))) {
386 ptr += strlen("SwapCached:");
387 stats->swap_cached = strtoul(ptr, &ptr, 10);
390 memset(buff, 0, sizeof(buff));
393 fclose(fp);
394 return 0;
397 static int stats_proc_system(struct ifstat *stats)
399 int cpu, cpus;
400 char *ptr, buff[256];
401 FILE *fp;
403 fp = fopen("/proc/stat", "r");
404 if (!fp)
405 panic("Cannot open /proc/stat!\n");
407 cpus = get_number_cpus();
409 memset(buff, 0, sizeof(buff));
411 while (fgets(buff, sizeof(buff), fp) != NULL) {
412 buff[sizeof(buff) - 1] = 0;
414 if ((ptr = strstr(buff, "cpu"))) {
415 ptr += strlen("cpu");
416 if (isblank(*ptr))
417 goto next;
419 cpu = strtol(ptr, &ptr, 10);
420 bug_on(cpu > cpus);
422 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
423 &stats->cpu_user[cpu],
424 &stats->cpu_nice[cpu],
425 &stats->cpu_sys[cpu],
426 &stats->cpu_idle[cpu],
427 &stats->cpu_iow[cpu]) != 5)
428 goto next;
429 } else if ((ptr = strstr(buff, "ctxt"))) {
430 ptr += strlen("ctxt");
431 stats->cswitch = strtoul(ptr, &ptr, 10);
432 } else if ((ptr = strstr(buff, "procs_running"))) {
433 ptr += strlen("procs_running");
434 stats->procs_run = strtoul(ptr, &ptr, 10);
435 } else if ((ptr = strstr(buff, "procs_blocked"))) {
436 ptr += strlen("procs_blocked");
437 stats->procs_iow = strtoul(ptr, &ptr, 10);
439 next:
440 memset(buff, 0, sizeof(buff));
443 fclose(fp);
444 return 0;
447 static int stats_proc_procs(struct ifstat *stats)
449 DIR *dir;
450 struct dirent *e;
452 dir = opendir("/proc");
453 if (!dir)
454 panic("Cannot open /proc\n");
456 stats->procs_total = 0;
458 while ((e = readdir(dir)) != NULL) {
459 const char *name = e->d_name;
460 char *end;
461 unsigned int pid = strtoul(name, &end, 10);
463 /* not a number */
464 if (pid == 0 && end == name)
465 continue;
467 stats->procs_total++;
470 closedir(dir);
472 return 0;
475 static int adjust_dbm_level(int in_dbm, int dbm_val)
477 if (!in_dbm)
478 return dbm_val;
480 return dbm_val - 0x100;
483 static int stats_wireless(const char *ifname, struct ifstat *stats)
485 int ret;
486 struct iw_statistics ws;
488 ret = wireless_sigqual(ifname, &ws);
489 if (ret != 0) {
490 stats->wifi.bitrate = 0;
491 return -EINVAL;
494 stats->wifi.bitrate = wireless_bitrate(ifname);
496 stats->wifi.signal_level =
497 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
499 stats->wifi.link_qual = ws.qual.qual;
500 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
502 return ret;
505 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
506 #define DIFF(member) do { \
507 if (sizeof(diff->member) != sizeof(new->member) || \
508 sizeof(diff->member) != sizeof(old->member)) \
509 bug(); \
510 bug_on((new->member - old->member) > (new->member)); \
511 DIFF1(member); \
512 } while (0)
514 static void stats_diff(struct ifstat *old, struct ifstat *new,
515 struct ifstat *diff)
517 int cpus, i;
519 DIFF(rx_bytes);
520 DIFF(rx_packets);
521 DIFF(rx_drops);
522 DIFF(rx_errors);
523 DIFF(rx_fifo);
524 DIFF(rx_frame);
525 DIFF(rx_multi);
527 DIFF(tx_bytes);
528 DIFF(tx_packets);
529 DIFF(tx_drops);
530 DIFF(tx_errors);
531 DIFF(tx_fifo);
532 DIFF(tx_colls);
533 DIFF(tx_carrier);
535 DIFF1(wifi.signal_level);
536 DIFF1(wifi.link_qual);
538 DIFF1(cswitch);
540 cpus = get_number_cpus();
542 for (i = 0; i < cpus; ++i) {
543 DIFF(irqs[i]);
544 DIFF(irqs_srx[i]);
545 DIFF(irqs_stx[i]);
547 DIFF1(cpu_user[i]);
548 DIFF1(cpu_nice[i]);
549 DIFF1(cpu_sys[i]);
550 DIFF1(cpu_idle[i]);
551 DIFF1(cpu_iow[i]);
555 static void stats_fetch(const char *ifname, struct ifstat *stats)
557 if (stats_proc_net_dev(ifname, stats) < 0)
558 panic("Cannot fetch device stats!\n");
559 if (stats_proc_softirqs(stats) < 0)
560 panic("Cannot fetch software interrupts!\n");
561 if (stats_proc_memory(stats) < 0)
562 panic("Cannot fetch memory stats!\n");
563 if (stats_proc_system(stats) < 0)
564 panic("Cannot fetch system stats!\n");
565 if (stats_proc_procs(stats) < 0)
566 panic("Cannot fetch process stats!\n");
568 stats_proc_interrupts((char *) ifname, stats);
570 stats_wireless(ifname, stats);
573 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
575 int cpus = get_number_cpus();
577 stats_zero(&stats_old, cpus);
578 stats_zero(&stats_new, cpus);
579 stats_zero(&stats_delta, cpus);
581 stats_fetch(ifname, &stats_old);
582 usleep(ms_interval * 1000);
583 stats_fetch(ifname, &stats_new);
585 stats_diff(&stats_old, &stats_new, &stats_delta);
588 static int cmp_hits(const void *p1, const void *p2)
590 const struct cpu_hit *h1 = p1, *h2 = p2;
593 * We want the hits sorted in descending order, thus reverse the return
594 * values.
596 if (h1->hit == h2->hit)
597 return 0;
598 else if (h1->hit < h2->hit)
599 return 1;
600 else
601 return -1;
604 static int cmp_irqs_rel(const void *p1, const void *p2)
606 const struct cpu_hit *h1 = p1, *h2 = p2;
609 * We want the hits sorted in descending order, thus reverse the return
610 * values.
612 if (h1->irqs_rel == h2->irqs_rel)
613 return 0;
614 else if (h1->irqs_rel < h2->irqs_rel)
615 return 1;
616 else
617 return -1;
620 static int cmp_irqs_abs(const void *p1, const void *p2)
622 const struct cpu_hit *h1 = p1, *h2 = p2;
625 * We want the hits sorted in descending order, thus reverse the return
626 * values.
628 if (h1->irqs_abs == h2->irqs_abs)
629 return 0;
630 else if (h1->irqs_abs < h2->irqs_abs)
631 return 1;
632 else
633 return -1;
636 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
637 int cpus)
639 int i;
641 memset(&stats_avg, 0, sizeof(stats_avg));
643 for (i = 0; i < cpus; ++i) {
644 cpu_hits[i].idx = i;
645 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
646 cpu_hits[i].irqs_rel = rel->irqs[i];
647 cpu_hits[i].irqs_abs = abs->irqs[i];
649 stats_avg.cpu_user += rel->cpu_user[i];
650 stats_avg.cpu_sys += rel->cpu_sys[i];
651 stats_avg.cpu_nice += rel->cpu_nice[i];
652 stats_avg.cpu_idle += rel->cpu_idle[i];
653 stats_avg.cpu_iow += rel->cpu_iow[i];
655 stats_avg.irqs_abs += abs->irqs[i];
656 stats_avg.irqs_rel += rel->irqs[i];
657 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
658 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
661 stats_avg.cpu_user /= cpus;
662 stats_avg.cpu_sys /= cpus;
663 stats_avg.cpu_nice /= cpus;
664 stats_avg.cpu_idle /= cpus;
665 stats_avg.cpu_iow /= cpus;
666 stats_avg.irqs_abs /= cpus;
667 stats_avg.irqs_rel /= cpus;
668 stats_avg.irqs_srx_rel /= cpus;
669 stats_avg.irqs_stx_rel /= cpus;
672 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
673 uint64_t ms_interval, unsigned int top_cpus)
675 size_t len = 0;
676 char buff[64], machine[64];
677 struct ethtool_drvinfo drvinf;
678 u32 rate = device_bitrate(ifname);
679 int link = ethtool_link(ifname);
680 unsigned int cpus = get_number_cpus();
682 memset(&drvinf, 0, sizeof(drvinf));
683 ethtool_drvinf(ifname, &drvinf);
685 memset(buff, 0, sizeof(buff));
686 memset(machine, 0, sizeof(machine));
688 if (rate)
689 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
690 if (link >= 0)
691 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
692 link == 0 ? "no" : "yes");
694 if (!strstr(uts.release, uts.machine))
695 slprintf(machine, sizeof(machine), " %s,", uts.machine);
697 mvwprintw(screen, (*voff)++, 2,
698 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
699 " ", uts.release, machine,
700 ifname, drvinf.driver, buff, ms_interval, top_cpus,
701 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
704 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
705 int *voff)
707 attron(A_REVERSE);
709 mvwprintw(screen, (*voff)++, 0,
710 " rx: %16.3llf MiB/t "
711 "%10llu pkts/t "
712 "%10llu drops/t "
713 "%10llu errors/t ",
714 ((long double) rel->rx_bytes) / (1LLU << 20),
715 rel->rx_packets, rel->rx_drops, rel->rx_errors);
717 mvwprintw(screen, (*voff)++, 0,
718 " tx: %16.3llf MiB/t "
719 "%10llu pkts/t "
720 "%10llu drops/t "
721 "%10llu errors/t ",
722 ((long double) rel->tx_bytes) / (1LLU << 20),
723 rel->tx_packets, rel->tx_drops, rel->tx_errors);
725 attroff(A_REVERSE);
728 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
729 int *voff)
731 mvwprintw(screen, (*voff)++, 2,
732 "rx: %16.3llf MiB "
733 "%10llu pkts "
734 "%10llu drops "
735 "%10llu errors",
736 ((long double) abs->rx_bytes) / (1LLU << 20),
737 abs->rx_packets, abs->rx_drops, abs->rx_errors);
739 mvwprintw(screen, (*voff)++, 2,
740 "tx: %16.3llf MiB "
741 "%10llu pkts "
742 "%10llu drops "
743 "%10llu errors",
744 ((long double) abs->tx_bytes) / (1LLU << 20),
745 abs->tx_packets, abs->tx_drops, abs->tx_errors);
748 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
749 const struct ifstat *abs, int *voff)
751 mvwprintw(screen, (*voff)++, 2,
752 "sys: %14u cs/t "
753 "%11u procs "
754 "%11u running "
755 "%10u iowait",
756 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
759 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
761 mvwprintw(screen, (*voff)++, 2,
762 "mem: %13uM total "
763 "%9uM used "
764 "%11uM active "
765 "%10uM inactive",
766 abs->mem_total / 1024,
767 (abs->mem_total - abs->mem_free) / 1024,
768 abs->mem_active / 1024,
769 abs->mem_inactive / 1024);
771 mvwprintw(screen, (*voff)++, 2,
772 "swap: %12uM total "
773 "%9uM used "
774 "%11uM cached",
775 abs->swap_total / 1024,
776 (abs->swap_total - abs->swap_free) / 1024,
777 abs->swap_cached / 1024);
780 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
781 int *voff, unsigned int idx, char *tag)
783 int max_padd = padding_from_num(get_number_cpus());
784 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
785 rel->cpu_idle[idx] + rel->cpu_iow[idx];
787 mvwprintw(screen, (*voff)++, 2,
788 "cpu%*d %s: %11.1lf%% usr/t "
789 "%9.1lf%% sys/t "
790 "%10.1lf%% idl/t "
791 "%11.1lf%% iow/t",
792 max_padd, idx, tag,
793 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
794 100.0 * rel->cpu_sys[idx] / all,
795 100.0 * rel->cpu_idle[idx] / all,
796 100.0 * rel->cpu_iow[idx] / all);
799 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
800 const struct avg_stat *avg, int top_cpus,
801 int *voff)
803 int i;
804 int cpus = get_number_cpus();
805 uint64_t all;
807 if (top_cpus == 0)
808 return;
810 /* Display top hitter */
811 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
813 /* Make sure we don't display the min. hitter twice */
814 if (top_cpus == cpus)
815 top_cpus--;
817 for (i = 1; i < top_cpus; ++i)
818 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "|");
820 /* Display minimum hitter */
821 if (cpus != 1)
822 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
824 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
825 mvwprintw(screen, (*voff)++, 2,
826 "avg: %14.1lf%% "
827 "%9.1lf%% "
828 "%10.1lf%% "
829 "%11.1lf%%",
830 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
831 100.0 * avg->cpu_sys / all,
832 100.0 * avg->cpu_idle /all,
833 100.0 * avg->cpu_iow / all);
836 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
837 int *voff, unsigned int idx, char *tag)
839 int max_padd = padding_from_num(get_number_cpus());
841 mvwprintw(screen, (*voff)++, 2,
842 "cpu%*d %s: %12llu irqs/t "
843 "%17llu sirq rx/t "
844 "%17llu sirq tx/t",
845 max_padd, idx, tag,
846 rel->irqs[idx],
847 rel->irqs_srx[idx],
848 rel->irqs_stx[idx]);
851 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
852 const struct avg_stat *avg, int top_cpus,
853 int *voff)
855 int i;
856 int cpus = get_number_cpus();
858 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
860 if (top_cpus == cpus)
861 top_cpus--;
863 for (i = 1; i < top_cpus; ++i)
864 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
866 if (cpus != 1)
867 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
869 mvwprintw(screen, (*voff)++, 2,
870 "avg: %17.1Lf "
871 "%17.1Lf "
872 "%17.1Lf",
873 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
876 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
877 int *voff, unsigned int idx, char *tag)
879 int max_padd = padding_from_num(get_number_cpus());
881 mvwprintw(screen, (*voff)++, 2,
882 "cpu%*d %s: %12llu irqs",
883 max_padd, idx, tag, abs->irqs[idx]);
886 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
887 const struct avg_stat *avg, int top_cpus,
888 int *voff)
890 int i;
891 int cpus = get_number_cpus();
893 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
895 if (top_cpus == cpus)
896 top_cpus--;
898 for (i = 1; i < top_cpus; ++i)
899 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
901 if (cpus != 1)
902 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
904 mvwprintw(screen, (*voff)++, 2,
905 "avg: %17.1Lf", avg->irqs_abs);
908 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
909 const struct ifstat *abs, int *voff)
911 if (iswireless(abs)) {
912 mvwprintw(screen, (*voff)++, 2,
913 "linkqual: %7d/%d (%d/t) ",
914 abs->wifi.link_qual,
915 abs->wifi.link_qual_max,
916 rel->wifi.link_qual);
918 mvwprintw(screen, (*voff)++, 2,
919 "signal: %8d dBm (%d dBm/t) ",
920 abs->wifi.signal_level,
921 rel->wifi.signal_level);
925 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
926 const struct ifstat *abs, const struct avg_stat *avg,
927 int *first, uint64_t ms_interval, unsigned int top_cpus)
929 int cpus, top, voff = 1, cvoff = 2;
931 curs_set(0);
933 cpus = get_number_cpus();
934 top = min(cpus, top_cpus);
936 stats_top(rel, abs, cpus);
938 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
940 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
942 voff++;
943 screen_net_dev_rel(screen, rel, &voff);
945 voff++;
946 screen_net_dev_abs(screen, abs, &voff);
948 voff++;
949 screen_sys(screen, rel, abs, &voff);
951 voff++;
952 screen_mem_swap(screen, abs, &voff);
954 voff++;
955 screen_percpu_states(screen, rel, avg, top, &voff);
957 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
959 voff++;
960 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
962 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
964 voff++;
965 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
967 voff++;
968 screen_wireless(screen, rel, abs, &voff);
970 if (*first) {
971 mvwprintw(screen, cvoff, 2, "Collecting data ...");
972 *first = 0;
973 } else {
974 mvwprintw(screen, cvoff, 2, " ");
977 wrefresh(screen);
978 refresh();
981 static int screen_main(const char *ifname, uint64_t ms_interval,
982 unsigned int top_cpus)
984 int first = 1, key;
986 stats_screen = screen_init(true);
988 while (!sigint) {
989 key = getch();
990 if (key == 'q' || key == 0x1b || key == KEY_F(10))
991 break;
993 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
994 &first, ms_interval, top_cpus);
996 stats_sample_generic(ifname, ms_interval);
999 screen_end();
1001 return 0;
1004 static void term_csv(const char *ifname, const struct ifstat *rel,
1005 const struct ifstat *abs, uint64_t ms_interval)
1007 int cpus, i;
1009 printf("%ld ", time(0));
1011 printf("%llu ", rel->rx_bytes);
1012 printf("%llu ", rel->rx_packets);
1013 printf("%llu ", rel->rx_drops);
1014 printf("%llu ", rel->rx_errors);
1016 printf("%llu ", abs->rx_bytes);
1017 printf("%llu ", abs->rx_packets);
1018 printf("%llu ", abs->rx_drops);
1019 printf("%llu ", abs->rx_errors);
1021 printf("%llu ", rel->tx_bytes);
1022 printf("%llu ", rel->tx_packets);
1023 printf("%llu ", rel->tx_drops);
1024 printf("%llu ", rel->tx_errors);
1026 printf("%llu ", abs->tx_bytes);
1027 printf("%llu ", abs->tx_packets);
1028 printf("%llu ", abs->tx_drops);
1029 printf("%llu ", abs->tx_errors);
1031 printf("%u ", rel->cswitch);
1032 printf("%lu ", abs->mem_free);
1033 printf("%lu ", abs->mem_total - abs->mem_free);
1034 printf("%lu ", abs->mem_total);
1035 printf("%lu ", abs->swap_free);
1036 printf("%lu ", abs->swap_total - abs->swap_free);
1037 printf("%lu ", abs->swap_total);
1038 printf("%u ", abs->procs_total);
1039 printf("%u ", abs->procs_run);
1040 printf("%u ", abs->procs_iow);
1042 cpus = get_number_cpus();
1044 for (i = 0; i < cpus; ++i) {
1045 printf("%lu ", rel->cpu_user[i]);
1046 printf("%lu ", rel->cpu_nice[i]);
1047 printf("%lu ", rel->cpu_sys[i]);
1048 printf("%lu ", rel->cpu_idle[i]);
1049 printf("%lu ", rel->cpu_iow[i]);
1051 printf("%llu ", rel->irqs[i]);
1052 printf("%llu ", abs->irqs[i]);
1054 printf("%llu ", rel->irqs_srx[i]);
1055 printf("%llu ", abs->irqs_srx[i]);
1057 printf("%llu ", rel->irqs_stx[i]);
1058 printf("%llu ", abs->irqs_stx[i]);
1061 if (iswireless(abs)) {
1062 printf("%u ", rel->wifi.link_qual);
1063 printf("%u ", abs->wifi.link_qual);
1064 printf("%u ", abs->wifi.link_qual_max);
1066 printf("%d ", rel->wifi.signal_level);
1067 printf("%d ", abs->wifi.signal_level);
1070 puts("");
1071 fflush(stdout);
1074 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1075 uint64_t ms_interval)
1077 int cpus, i, j = 1;
1079 printf("# gnuplot dump (#col:description)\n");
1080 printf("# networking interface: %s\n", ifname);
1081 printf("# sampling interval (t): %lu ms\n", ms_interval);
1082 printf("# %d:unixtime ", j++);
1084 printf("%d:rx-bytes-per-t ", j++);
1085 printf("%d:rx-pkts-per-t ", j++);
1086 printf("%d:rx-drops-per-t ", j++);
1087 printf("%d:rx-errors-per-t ", j++);
1089 printf("%d:rx-bytes ", j++);
1090 printf("%d:rx-pkts ", j++);
1091 printf("%d:rx-drops ", j++);
1092 printf("%d:rx-errors ", j++);
1094 printf("%d:tx-bytes-per-t ", j++);
1095 printf("%d:tx-pkts-per-t ", j++);
1096 printf("%d:tx-drops-per-t ", j++);
1097 printf("%d:tx-errors-per-t ", j++);
1099 printf("%d:tx-bytes ", j++);
1100 printf("%d:tx-pkts ", j++);
1101 printf("%d:tx-drops ", j++);
1102 printf("%d:tx-errors ", j++);
1104 printf("%d:context-switches-per-t ", j++);
1105 printf("%d:mem-free ", j++);
1106 printf("%d:mem-used ", j++);
1107 printf("%d:mem-total ", j++);
1108 printf("%d:swap-free ", j++);
1109 printf("%d:swap-used ", j++);
1110 printf("%d:swap-total ", j++);
1111 printf("%d:procs-total ", j++);
1112 printf("%d:procs-in-run ", j++);
1113 printf("%d:procs-in-iow ", j++);
1115 cpus = get_number_cpus();
1117 for (i = 0; i < cpus; ++i) {
1118 printf("%d:cpu%i-usr-per-t ", j++, i);
1119 printf("%d:cpu%i-nice-per-t ", j++, i);
1120 printf("%d:cpu%i-sys-per-t ", j++, i);
1121 printf("%d:cpu%i-idle-per-t ", j++, i);
1122 printf("%d:cpu%i-iow-per-t ", j++, i);
1124 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1125 printf("%d:cpu%i-net-irqs ", j++, i);
1127 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1128 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1129 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1130 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1133 if (iswireless(abs)) {
1134 printf("%d:wifi-link-qual-per-t ", j++);
1135 printf("%d:wifi-link-qual ", j++);
1136 printf("%d:wifi-link-qual-max ", j++);
1138 printf("%d:wifi-signal-dbm-per-t ", j++);
1139 printf("%d:wifi-signal-dbm ", j++);
1142 puts("");
1143 printf("# data:\n");
1144 fflush(stdout);
1147 static int term_main(const char *ifname, uint64_t ms_interval,
1148 unsigned int top_cpus __maybe_unused)
1150 int first = 1;
1152 do {
1153 stats_sample_generic(ifname, ms_interval);
1155 if (first) {
1156 first = 0;
1157 term_csv_header(ifname, &stats_new, ms_interval);
1160 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1161 } while (stats_loop && !sigint);
1163 return 0;
1166 int main(int argc, char **argv)
1168 short ifflags = 0;
1169 int c, opt_index, ret, cpus, promisc = 0;
1170 unsigned int top_cpus = 5;
1171 uint64_t interval = 1000;
1172 char *ifname = NULL;
1173 int (*func_main)(const char *ifname, uint64_t ms_interval,
1174 unsigned int top_cpus) = screen_main;
1176 setfsuid(getuid());
1177 setfsgid(getgid());
1179 while ((c = getopt_long(argc, argv, short_options, long_options,
1180 &opt_index)) != EOF) {
1181 switch (c) {
1182 case 'h':
1183 help();
1184 break;
1185 case 'v':
1186 version();
1187 break;
1188 case 'd':
1189 ifname = xstrndup(optarg, IFNAMSIZ);
1190 break;
1191 case 't':
1192 interval = strtoul(optarg, NULL, 10);
1193 break;
1194 case 'n':
1195 top_cpus = strtoul(optarg, NULL, 10);
1196 if (top_cpus < 1)
1197 panic("Number of top hitter CPUs must be greater than 0");
1198 break;
1199 case 'l':
1200 stats_loop = 1;
1201 break;
1202 case 'p':
1203 promisc = 1;
1204 break;
1205 case 'c':
1206 func_main = term_main;
1207 break;
1208 case '?':
1209 switch (optopt) {
1210 case 'd':
1211 case 't':
1212 panic("Option -%c requires an argument!\n",
1213 optopt);
1214 default:
1215 if (isprint(optopt))
1216 printf("Unknown option character `0x%X\'!\n", optopt);
1217 die();
1219 default:
1220 break;
1224 if (argc == 1)
1225 help();
1227 if (argc == 2)
1228 ifname = xstrndup(argv[1], IFNAMSIZ);
1229 if (ifname == NULL)
1230 panic("No networking device given!\n");
1232 if (!strncmp("lo", ifname, IFNAMSIZ))
1233 panic("lo is not supported!\n");
1234 if (device_mtu(ifname) == 0)
1235 panic("This is no networking device!\n");
1237 register_signal(SIGINT, signal_handler);
1238 register_signal(SIGHUP, signal_handler);
1240 cpus = get_number_cpus();
1241 top_cpus = min(top_cpus, cpus);
1242 if (uname(&uts) < 0)
1243 panic("Cannot execute uname!\n");
1245 stats_alloc(&stats_old, cpus);
1246 stats_alloc(&stats_new, cpus);
1247 stats_alloc(&stats_delta, cpus);
1249 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1251 if (promisc)
1252 ifflags = enter_promiscuous_mode(ifname);
1253 ret = func_main(ifname, interval, top_cpus);
1254 if (promisc)
1255 leave_promiscuous_mode(ifname, ifflags);
1257 xfree(ifname);
1258 return ret;