man: improvements to language for flowtop.8
[netsniff-ng.git] / ifpps.c
blobe5988e6d7a8840b68b4f2c83cedd8a613bc5ad8d
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 <signal.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include <time.h>
20 #include <dirent.h>
22 #include "die.h"
23 #include "dev.h"
24 #include "sig.h"
25 #include "link.h"
26 #include "xmalloc.h"
27 #include "ioops.h"
28 #include "promisc.h"
29 #include "cpus.h"
30 #include "built_in.h"
32 struct wifi_stat {
33 uint32_t bitrate;
34 int16_t link_qual, link_qual_max;
35 int signal_level /*, noise_level*/;
38 struct ifstat {
39 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
40 long long unsigned int rx_fifo, rx_frame, rx_multi;
41 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
42 long long unsigned int tx_fifo, tx_colls, tx_carrier;
43 uint64_t mem_free, mem_total, mem_active, mem_inactive;
44 uint64_t swap_total, swap_free, swap_cached;
45 uint32_t irq_nr, procs_total, procs_run, procs_iow, cswitch;
46 struct wifi_stat wifi;
48 * Pointer members need to be last in order for stats_zero() to work
49 * properly.
51 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
52 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
55 struct cpu_hit {
56 unsigned int idx;
57 uint64_t hit;
58 long long unsigned int irqs_rel, irqs_abs;
61 static volatile sig_atomic_t sigint = 0;
62 static struct ifstat stats_old, stats_new, stats_delta;
63 static struct cpu_hit *cpu_hits;
64 static int stats_loop = 0;
65 static WINDOW *stats_screen = NULL;
67 static const char *short_options = "d:t:n:vhclp";
68 static const struct option long_options[] = {
69 {"dev", required_argument, NULL, 'd'},
70 {"interval", required_argument, NULL, 't'},
71 {"num-cpus", required_argument, NULL, 'n'},
72 {"promisc", no_argument, NULL, 'p'},
73 {"csv", no_argument, NULL, 'c'},
74 {"loop", no_argument, NULL, 'l'},
75 {"version", no_argument, NULL, 'v'},
76 {"help", no_argument, NULL, 'h'},
77 {NULL, 0, NULL, 0}
80 static void signal_handler(int number)
82 switch (number) {
83 case SIGINT:
84 sigint = 1;
85 break;
86 case SIGHUP:
87 default:
88 break;
92 static inline int iswireless(const struct ifstat *stats)
94 return stats->wifi.bitrate > 0;
97 static void __noreturn help(void)
99 printf("\nifpps %s, top-like kernel networking and system statistics\n",
100 VERSION_STRING);
101 puts("http://www.netsniff-ng.org\n\n"
102 "Usage: ifpps [options] || ifpps <netdev>\n"
103 "Options:\n"
104 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
105 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
106 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
107 " in ncurses mode (default 10)\n"
108 " -p|--promisc Promiscuous mode\n"
109 " -c|--csv Output to terminal as Gnuplot-ready data\n"
110 " -l|--loop Continuous CSV output\n"
111 " -v|--version Print version and exit\n"
112 " -h|--help Print this help and exit\n\n"
113 "Examples:\n"
114 " ifpps eth0\n"
115 " ifpps -pd eth0\n"
116 " ifpps -lpcd wlan0 > plot.dat\n\n"
117 "Note:\n"
118 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
119 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
120 "Please report bugs to <bugs@netsniff-ng.org>\n"
121 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
122 "Swiss federal institute of technology (ETH Zurich)\n"
123 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
124 "License: GNU GPL version 2.0\n"
125 "This is free software: you are free to change and redistribute it.\n"
126 "There is NO WARRANTY, to the extent permitted by law.\n");
127 die();
130 static void __noreturn version(void)
132 printf("\nifpps %s, top-like kernel networking and system statistics\n",
133 VERSION_LONG);
134 puts("http://www.netsniff-ng.org\n\n"
135 "Please report bugs to <bugs@netsniff-ng.org>\n"
136 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
137 "Swiss federal institute of technology (ETH Zurich)\n"
138 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
139 "License: GNU GPL version 2.0\n"
140 "This is free software: you are free to change and redistribute it.\n"
141 "There is NO WARRANTY, to the extent permitted by law.\n");
142 die();
145 static inline int padding_from_num(int n)
147 int i = 0;
148 do i++;
149 while ((n /= 10) > 0);
150 return i;
153 #define STATS_ALLOC1(member) \
154 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
156 static void stats_alloc(struct ifstat *stats, int cpus)
158 STATS_ALLOC1(irqs);
159 STATS_ALLOC1(irqs_srx);
160 STATS_ALLOC1(irqs_stx);
162 STATS_ALLOC1(cpu_user);
163 STATS_ALLOC1(cpu_sys);
164 STATS_ALLOC1(cpu_nice);
165 STATS_ALLOC1(cpu_idle);
166 STATS_ALLOC1(cpu_iow);
169 #define STATS_ZERO1(member) \
170 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
172 static void stats_zero(struct ifstat *stats, int cpus)
174 /* Only clear the non-pointer members */
175 memset(stats, 0, offsetof(struct ifstat, irqs));
177 STATS_ZERO1(irqs);
178 STATS_ZERO1(irqs_srx);
179 STATS_ZERO1(irqs_stx);
181 STATS_ZERO1(cpu_user);
182 STATS_ZERO1(cpu_sys);
183 STATS_ZERO1(cpu_nice);
184 STATS_ZERO1(cpu_idle);
185 STATS_ZERO1(cpu_iow);
188 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
190 int ret = -EINVAL;
191 char buff[256];
192 FILE *fp;
194 fp = fopen("/proc/net/dev", "r");
195 if (!fp)
196 panic("Cannot open /proc/net/dev!\n");
198 if (fgets(buff, sizeof(buff), fp)) { ; }
199 if (fgets(buff, sizeof(buff), fp)) { ; }
201 memset(buff, 0, sizeof(buff));
203 while (fgets(buff, sizeof(buff), fp) != NULL) {
204 buff[sizeof(buff) -1] = 0;
206 if (strstr(buff, ifname) == NULL)
207 continue;
209 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
210 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
211 &stats->rx_bytes, &stats->rx_packets,
212 &stats->rx_errors, &stats->rx_drops,
213 &stats->rx_fifo, &stats->rx_frame,
214 &stats->rx_multi, &stats->tx_bytes,
215 &stats->tx_packets, &stats->tx_errors,
216 &stats->tx_drops, &stats->tx_fifo,
217 &stats->tx_colls, &stats->tx_carrier) == 14) {
218 ret = 0;
219 break;
222 memset(buff, 0, sizeof(buff));
225 fclose(fp);
226 return ret;
229 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
231 int ret = -EINVAL, i, cpus, try = 0;
232 char *ptr, *buff;
233 bool seen = false;
234 size_t buff_len;
235 struct ethtool_drvinfo drvinf;
236 FILE *fp;
238 fp = fopen("/proc/interrupts", "r");
239 if (!fp)
240 panic("Cannot open /proc/interrupts!\n");
242 cpus = get_number_cpus();
243 buff_len = cpus * 128;
244 buff = xmalloc(buff_len);
245 retry:
246 fseek(fp, 0, SEEK_SET);
247 memset(buff, 0, buff_len);
249 while (fgets(buff, buff_len, fp) != NULL) {
250 buff[buff_len - 1] = 0;
251 ptr = buff;
253 if (strstr(buff, ifname) == NULL)
254 continue;
256 /* XXX: remove this one here */
257 stats->irq_nr = strtol(ptr, &ptr, 10);
258 bug_on(stats->irq_nr == 0);
260 if (ptr)
261 ptr++;
262 for (i = 0; i < cpus && ptr; ++i) {
263 if (seen)
264 stats->irqs[i] += strtol(ptr, &ptr, 10);
265 else
266 stats->irqs[i] = strtol(ptr, &ptr, 10);
267 if (i == cpus - 1) {
268 ret = 0;
269 seen = true;
273 memset(buff, 0, buff_len);
276 if (ret == -EINVAL && try == 0) {
277 memset(&drvinf, 0, sizeof(drvinf));
278 if (ethtool_drvinf(ifname, &drvinf) < 0)
279 goto done;
281 ifname = drvinf.driver;
282 try++;
284 goto retry;
286 done:
287 xfree(buff);
288 fclose(fp);
289 return ret;
292 static int stats_proc_softirqs(struct ifstat *stats)
294 int i, cpus;
295 char *ptr, *buff;
296 size_t buff_len;
297 FILE *fp;
298 enum {
299 softirqs_net_rx,
300 softirqs_net_tx,
301 } net_type;
303 fp = fopen("/proc/softirqs", "r");
304 if (!fp)
305 panic("Cannot open /proc/softirqs!\n");
307 cpus = get_number_cpus();
308 buff_len = cpus * 128;
309 buff = xmalloc(buff_len);
311 memset(buff, 0, buff_len);
313 while (fgets(buff, buff_len, fp) != NULL) {
314 buff[buff_len - 1] = 0;
316 if ((ptr = strstr(buff, "NET_TX:")))
317 net_type = softirqs_net_tx;
318 else if ((ptr = strstr(buff, "NET_RX:")))
319 net_type = softirqs_net_rx;
320 else
321 continue;
323 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
324 switch (net_type) {
325 case softirqs_net_tx:
326 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
327 break;
328 case softirqs_net_rx:
329 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
330 break;
334 memset(buff, 0, buff_len);
337 xfree(buff);
338 fclose(fp);
339 return 0;
342 static int stats_proc_memory(struct ifstat *stats)
344 char *ptr, buff[256];
345 FILE *fp;
347 fp = fopen("/proc/meminfo", "r");
348 if (!fp)
349 panic("Cannot open /proc/meminfo!\n");
351 memset(buff, 0, sizeof(buff));
353 while (fgets(buff, sizeof(buff), fp) != NULL) {
354 buff[sizeof(buff) - 1] = 0;
356 if ((ptr = strstr(buff, "MemTotal:"))) {
357 ptr += strlen("MemTotal:");
358 stats->mem_total = strtoul(ptr, &ptr, 10);
359 } else if ((ptr = strstr(buff, "MemFree:"))) {
360 ptr += strlen("MemFree:");
361 stats->mem_free = strtoul(ptr, &ptr, 10);
362 } else if ((ptr = strstr(buff, "Active:"))) {
363 ptr += strlen("Active:");
364 stats->mem_active = strtoul(ptr, &ptr, 10);
365 } else if ((ptr = strstr(buff, "Inactive:"))) {
366 ptr += strlen("Inactive:");
367 stats->mem_inactive = strtoul(ptr, &ptr, 10);
368 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
369 ptr += strlen("SwapTotal:");
370 stats->swap_total = strtoul(ptr, &ptr, 10);
371 } else if ((ptr = strstr(buff, "SwapFree:"))) {
372 ptr += strlen("SwapFree:");
373 stats->swap_free = strtoul(ptr, &ptr, 10);
374 } else if ((ptr = strstr(buff, "SwapCached:"))) {
375 ptr += strlen("SwapCached:");
376 stats->swap_cached = strtoul(ptr, &ptr, 10);
379 memset(buff, 0, sizeof(buff));
382 fclose(fp);
383 return 0;
386 static int stats_proc_system(struct ifstat *stats)
388 int cpu, cpus;
389 char *ptr, buff[256];
390 FILE *fp;
392 fp = fopen("/proc/stat", "r");
393 if (!fp)
394 panic("Cannot open /proc/stat!\n");
396 cpus = get_number_cpus();
398 memset(buff, 0, sizeof(buff));
400 while (fgets(buff, sizeof(buff), fp) != NULL) {
401 buff[sizeof(buff) - 1] = 0;
403 if ((ptr = strstr(buff, "cpu"))) {
404 ptr += strlen("cpu");
405 if (isblank(*ptr))
406 goto next;
408 cpu = strtol(ptr, &ptr, 10);
409 bug_on(cpu > cpus);
411 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
412 &stats->cpu_user[cpu],
413 &stats->cpu_nice[cpu],
414 &stats->cpu_sys[cpu],
415 &stats->cpu_idle[cpu],
416 &stats->cpu_iow[cpu]) != 5)
417 goto next;
418 } else if ((ptr = strstr(buff, "ctxt"))) {
419 ptr += strlen("ctxt");
420 stats->cswitch = strtoul(ptr, &ptr, 10);
421 } else if ((ptr = strstr(buff, "procs_running"))) {
422 ptr += strlen("procs_running");
423 stats->procs_run = strtoul(ptr, &ptr, 10);
424 } else if ((ptr = strstr(buff, "procs_blocked"))) {
425 ptr += strlen("procs_blocked");
426 stats->procs_iow = strtoul(ptr, &ptr, 10);
428 next:
429 memset(buff, 0, sizeof(buff));
432 fclose(fp);
433 return 0;
436 static int stats_proc_procs(struct ifstat *stats)
438 DIR *dir;
439 struct dirent *e;
441 dir = opendir("/proc");
442 if (!dir)
443 panic("Cannot open /proc\n");
445 stats->procs_total = 0;
447 while ((e = readdir(dir)) != NULL) {
448 const char *name = e->d_name;
449 char *end;
450 unsigned int pid = strtoul(name, &end, 10);
452 /* not a number */
453 if (pid == 0 && end == name)
454 continue;
456 stats->procs_total++;
459 closedir(dir);
461 return 0;
464 static int adjust_dbm_level(int in_dbm, int dbm_val)
466 if (!in_dbm)
467 return dbm_val;
469 return dbm_val - 0x100;
472 static int stats_wireless(const char *ifname, struct ifstat *stats)
474 int ret;
475 struct iw_statistics ws;
477 ret = wireless_sigqual(ifname, &ws);
478 if (ret != 0) {
479 stats->wifi.bitrate = 0;
480 return -EINVAL;
483 stats->wifi.bitrate = wireless_bitrate(ifname);
485 stats->wifi.signal_level =
486 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
488 stats->wifi.link_qual = ws.qual.qual;
489 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
491 return ret;
494 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
495 #define DIFF(member) do { \
496 if (sizeof(diff->member) != sizeof(new->member) || \
497 sizeof(diff->member) != sizeof(old->member)) \
498 bug(); \
499 bug_on((new->member - old->member) > (new->member)); \
500 DIFF1(member); \
501 } while (0)
503 static void stats_diff(struct ifstat *old, struct ifstat *new,
504 struct ifstat *diff)
506 int cpus, i;
508 DIFF(rx_bytes);
509 DIFF(rx_packets);
510 DIFF(rx_drops);
511 DIFF(rx_errors);
512 DIFF(rx_fifo);
513 DIFF(rx_frame);
514 DIFF(rx_multi);
516 DIFF(tx_bytes);
517 DIFF(tx_packets);
518 DIFF(tx_drops);
519 DIFF(tx_errors);
520 DIFF(tx_fifo);
521 DIFF(tx_colls);
522 DIFF(tx_carrier);
524 DIFF1(wifi.signal_level);
525 DIFF1(wifi.link_qual);
527 DIFF1(cswitch);
529 cpus = get_number_cpus();
531 for (i = 0; i < cpus; ++i) {
532 DIFF(irqs[i]);
533 DIFF(irqs_srx[i]);
534 DIFF(irqs_stx[i]);
536 DIFF1(cpu_user[i]);
537 DIFF1(cpu_nice[i]);
538 DIFF1(cpu_sys[i]);
539 DIFF1(cpu_idle[i]);
540 DIFF1(cpu_iow[i]);
544 static void stats_fetch(const char *ifname, struct ifstat *stats)
546 if (stats_proc_net_dev(ifname, stats) < 0)
547 panic("Cannot fetch device stats!\n");
548 if (stats_proc_softirqs(stats) < 0)
549 panic("Cannot fetch software interrupts!\n");
550 if (stats_proc_memory(stats) < 0)
551 panic("Cannot fetch memory stats!\n");
552 if (stats_proc_system(stats) < 0)
553 panic("Cannot fetch system stats!\n");
554 if (stats_proc_procs(stats) < 0)
555 panic("Cannot fetch process stats!\n");
557 stats_proc_interrupts((char *) ifname, stats);
559 stats_wireless(ifname, stats);
562 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
564 int cpus = get_number_cpus();
566 stats_zero(&stats_old, cpus);
567 stats_zero(&stats_new, cpus);
568 stats_zero(&stats_delta, cpus);
570 stats_fetch(ifname, &stats_old);
571 usleep(ms_interval * 1000);
572 stats_fetch(ifname, &stats_new);
574 stats_diff(&stats_old, &stats_new, &stats_delta);
577 static int cmp_hits(const void *p1, const void *p2)
579 const struct cpu_hit *h1 = p1, *h2 = p2;
582 * We want the hits sorted in descending order, thus reverse the return
583 * values.
585 if (h1->hit == h2->hit)
586 return 0;
587 else if (h1->hit < h2->hit)
588 return 1;
589 else
590 return -1;
593 static int cmp_irqs_rel(const void *p1, const void *p2)
595 const struct cpu_hit *h1 = p1, *h2 = p2;
598 * We want the hits sorted in descending order, thus reverse the return
599 * values.
601 if (h1->irqs_rel == h2->irqs_rel)
602 return 0;
603 else if (h1->irqs_rel < h2->irqs_rel)
604 return 1;
605 else
606 return -1;
609 static int cmp_irqs_abs(const void *p1, const void *p2)
611 const struct cpu_hit *h1 = p1, *h2 = p2;
614 * We want the hits sorted in descending order, thus reverse the return
615 * values.
617 if (h1->irqs_abs == h2->irqs_abs)
618 return 0;
619 else if (h1->irqs_abs < h2->irqs_abs)
620 return 1;
621 else
622 return -1;
625 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
626 int cpus)
628 int i;
630 for (i = 0; i < cpus; ++i) {
631 cpu_hits[i].idx = i;
632 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
633 cpu_hits[i].irqs_rel = rel->irqs[i];
634 cpu_hits[i].irqs_abs = abs->irqs[i];
638 static void screen_init(WINDOW **screen)
640 (*screen) = initscr();
642 raw();
643 noecho();
644 cbreak();
645 nodelay((*screen), TRUE);
647 keypad(stdscr, TRUE);
649 refresh();
650 wrefresh((*screen));
653 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
654 uint64_t ms_interval, unsigned int top_cpus)
656 size_t len = 0;
657 char buff[64];
658 struct ethtool_drvinfo drvinf;
659 u32 rate = device_bitrate(ifname);
660 int link = ethtool_link(ifname);
661 unsigned int cpus = get_number_cpus();
663 memset(&drvinf, 0, sizeof(drvinf));
664 ethtool_drvinf(ifname, &drvinf);
666 memset(buff, 0, sizeof(buff));
667 if (rate)
668 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
669 if (link >= 0)
670 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
671 link == 0 ? "no" : "yes");
673 mvwprintw(screen, (*voff)++, 2,
674 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u%s/%u"
675 " ",
676 ifname, drvinf.driver, buff, ms_interval, top_cpus,
677 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
680 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
681 int *voff)
683 attron(A_REVERSE);
685 mvwprintw(screen, (*voff)++, 0,
686 " rx: %16.3llf MiB/t "
687 "%10llu pkts/t "
688 "%10llu drops/t "
689 "%10llu errors/t ",
690 ((long double) rel->rx_bytes) / (1LLU << 20),
691 rel->rx_packets, rel->rx_drops, rel->rx_errors);
693 mvwprintw(screen, (*voff)++, 0,
694 " tx: %16.3llf MiB/t "
695 "%10llu pkts/t "
696 "%10llu drops/t "
697 "%10llu errors/t ",
698 ((long double) rel->tx_bytes) / (1LLU << 20),
699 rel->tx_packets, rel->tx_drops, rel->tx_errors);
701 attroff(A_REVERSE);
704 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
705 int *voff)
707 mvwprintw(screen, (*voff)++, 2,
708 "rx: %16.3llf MiB "
709 "%10llu pkts "
710 "%10llu drops "
711 "%10llu errors",
712 ((long double) abs->rx_bytes) / (1LLU << 20),
713 abs->rx_packets, abs->rx_drops, abs->rx_errors);
715 mvwprintw(screen, (*voff)++, 2,
716 "tx: %16.3llf MiB "
717 "%10llu pkts "
718 "%10llu drops "
719 "%10llu errors",
720 ((long double) abs->tx_bytes) / (1LLU << 20),
721 abs->tx_packets, abs->tx_drops, abs->tx_errors);
724 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
725 const struct ifstat *abs, int *voff)
727 mvwprintw(screen, (*voff)++, 2,
728 "sys: %14u cs/t "
729 "%11u procs "
730 "%11u running "
731 "%10u iowait",
732 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
735 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
737 mvwprintw(screen, (*voff)++, 2,
738 "mem: %13uM total "
739 "%9uM used "
740 "%11uM active "
741 "%10uM inactive",
742 abs->mem_total / 1024,
743 (abs->mem_total - abs->mem_free) / 1024,
744 abs->mem_active / 1024,
745 abs->mem_inactive / 1024);
747 mvwprintw(screen, (*voff)++, 2,
748 "swap: %12uM total "
749 "%9uM used "
750 "%11uM cached",
751 abs->swap_total / 1024,
752 (abs->swap_total - abs->swap_free) / 1024,
753 abs->swap_cached / 1024);
756 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
757 int *voff, unsigned int idx, char *tag)
759 int max_padd = padding_from_num(get_number_cpus());
760 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
761 rel->cpu_idle[idx] + rel->cpu_iow[idx];
763 mvwprintw(screen, (*voff)++, 2,
764 "cpu%*d%s:%s %12.1lf%% usr/t "
765 "%9.1lf%% sys/t "
766 "%10.1lf%% idl/t "
767 "%11.1lf%% iow/t ", max_padd, idx,
768 tag, strlen(tag) == 0 ? " " : "",
769 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
770 100.0 * rel->cpu_sys[idx] / all,
771 100.0 * rel->cpu_idle[idx] / all,
772 100.0 * rel->cpu_iow[idx] / all);
775 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
776 int top_cpus, int *voff)
778 int i;
779 int cpus = get_number_cpus();
781 if (top_cpus == 0)
782 return;
784 /* Display top hitter */
785 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
787 /* Make sure we don't display the min. hitter twice */
788 if (top_cpus == cpus)
789 top_cpus--;
791 for (i = 1; i < top_cpus; ++i)
792 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "");
794 /* Display minimum hitter */
795 if (cpus != 1)
796 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
799 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
800 int *voff, unsigned int idx, char *tag)
802 int max_padd = padding_from_num(get_number_cpus());
804 mvwprintw(screen, (*voff)++, 2,
805 "cpu%*d%s:%s %13llu irqs/t "
806 "%15llu sirq rx/t "
807 "%15llu sirq tx/t ", max_padd, idx,
808 tag, strlen(tag) == 0 ? " " : "",
809 rel->irqs[idx],
810 rel->irqs_srx[idx],
811 rel->irqs_stx[idx]);
814 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
815 int top_cpus, int *voff)
817 int i;
818 int cpus = get_number_cpus();
820 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
822 if (top_cpus == cpus)
823 top_cpus--;
825 for (i = 1; i < top_cpus; ++i)
826 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "");
828 if (cpus != 1)
829 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
832 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
833 int *voff, unsigned int idx, char *tag)
835 int max_padd = padding_from_num(get_number_cpus());
837 mvwprintw(screen, (*voff)++, 2,
838 "cpu%*d%s:%s %13llu irqs", max_padd, idx,
839 tag, strlen(tag) == 0 ? " " : "",
840 abs->irqs[idx]);
843 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
844 int top_cpus, int *voff)
846 int i;
847 int cpus = get_number_cpus();
849 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
851 if (top_cpus == cpus)
852 top_cpus--;
854 for (i = 1; i < top_cpus; ++i)
855 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "");
857 if (cpus != 1)
858 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
861 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
862 const struct ifstat *abs, int *voff)
864 if (iswireless(abs)) {
865 mvwprintw(screen, (*voff)++, 2,
866 "linkqual: %7d/%d (%d/t) ",
867 abs->wifi.link_qual,
868 abs->wifi.link_qual_max,
869 rel->wifi.link_qual);
871 mvwprintw(screen, (*voff)++, 2,
872 "signal: %8d dBm (%d dBm/t) ",
873 abs->wifi.signal_level,
874 rel->wifi.signal_level);
878 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
879 const struct ifstat *abs, int *first, uint64_t ms_interval,
880 unsigned int top_cpus)
882 int cpus, top, voff = 1, cvoff = 2;
884 curs_set(0);
886 cpus = get_number_cpus();
887 top = min(cpus, top_cpus);
889 stats_top(rel, abs, cpus);
891 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
893 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
895 voff++;
896 screen_net_dev_rel(screen, rel, &voff);
898 voff++;
899 screen_net_dev_abs(screen, abs, &voff);
901 voff++;
902 screen_sys(screen, rel, abs, &voff);
904 voff++;
905 screen_mem_swap(screen, abs, &voff);
907 voff++;
908 screen_percpu_states(screen, rel, top, &voff);
910 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
912 voff++;
913 screen_percpu_irqs_rel(screen, rel, top, &voff);
915 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
917 voff++;
918 screen_percpu_irqs_abs(screen, abs, top, &voff);
920 voff++;
921 screen_wireless(screen, rel, abs, &voff);
923 if (*first) {
924 mvwprintw(screen, cvoff, 2, "Collecting data ...");
925 *first = 0;
926 } else {
927 mvwprintw(screen, cvoff, 2, " ");
930 wrefresh(screen);
931 refresh();
934 static void screen_end(void)
936 endwin();
939 static int screen_main(const char *ifname, uint64_t ms_interval,
940 unsigned int top_cpus)
942 int first = 1, key;
944 screen_init(&stats_screen);
946 while (!sigint) {
947 key = getch();
948 if (key == 'q' || key == 0x1b || key == KEY_F(10))
949 break;
951 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
952 &first, ms_interval, top_cpus);
954 stats_sample_generic(ifname, ms_interval);
957 screen_end();
959 return 0;
962 static void term_csv(const char *ifname, const struct ifstat *rel,
963 const struct ifstat *abs, uint64_t ms_interval)
965 int cpus, i;
967 printf("%ld ", time(0));
969 printf("%llu ", rel->rx_bytes);
970 printf("%llu ", rel->rx_packets);
971 printf("%llu ", rel->rx_drops);
972 printf("%llu ", rel->rx_errors);
974 printf("%llu ", abs->rx_bytes);
975 printf("%llu ", abs->rx_packets);
976 printf("%llu ", abs->rx_drops);
977 printf("%llu ", abs->rx_errors);
979 printf("%llu ", rel->tx_bytes);
980 printf("%llu ", rel->tx_packets);
981 printf("%llu ", rel->tx_drops);
982 printf("%llu ", rel->tx_errors);
984 printf("%llu ", abs->tx_bytes);
985 printf("%llu ", abs->tx_packets);
986 printf("%llu ", abs->tx_drops);
987 printf("%llu ", abs->tx_errors);
989 printf("%u ", rel->cswitch);
990 printf("%lu ", abs->mem_free);
991 printf("%lu ", abs->mem_total - abs->mem_free);
992 printf("%lu ", abs->mem_total);
993 printf("%lu ", abs->swap_free);
994 printf("%lu ", abs->swap_total - abs->swap_free);
995 printf("%lu ", abs->swap_total);
996 printf("%u ", abs->procs_total);
997 printf("%u ", abs->procs_run);
998 printf("%u ", abs->procs_iow);
1000 cpus = get_number_cpus();
1002 for (i = 0; i < cpus; ++i) {
1003 printf("%lu ", rel->cpu_user[i]);
1004 printf("%lu ", rel->cpu_nice[i]);
1005 printf("%lu ", rel->cpu_sys[i]);
1006 printf("%lu ", rel->cpu_idle[i]);
1007 printf("%lu ", rel->cpu_iow[i]);
1009 printf("%llu ", rel->irqs[i]);
1010 printf("%llu ", abs->irqs[i]);
1012 printf("%llu ", rel->irqs_srx[i]);
1013 printf("%llu ", abs->irqs_srx[i]);
1015 printf("%llu ", rel->irqs_stx[i]);
1016 printf("%llu ", abs->irqs_stx[i]);
1019 if (iswireless(abs)) {
1020 printf("%u ", rel->wifi.link_qual);
1021 printf("%u ", abs->wifi.link_qual);
1022 printf("%u ", abs->wifi.link_qual_max);
1024 printf("%d ", rel->wifi.signal_level);
1025 printf("%d ", abs->wifi.signal_level);
1028 puts("");
1029 fflush(stdout);
1032 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1033 uint64_t ms_interval)
1035 int cpus, i, j = 1;
1037 printf("# gnuplot dump (#col:description)\n");
1038 printf("# networking interface: %s\n", ifname);
1039 printf("# sampling interval (t): %lu ms\n", ms_interval);
1040 printf("# %d:unixtime ", j++);
1042 printf("%d:rx-bytes-per-t ", j++);
1043 printf("%d:rx-pkts-per-t ", j++);
1044 printf("%d:rx-drops-per-t ", j++);
1045 printf("%d:rx-errors-per-t ", j++);
1047 printf("%d:rx-bytes ", j++);
1048 printf("%d:rx-pkts ", j++);
1049 printf("%d:rx-drops ", j++);
1050 printf("%d:rx-errors ", j++);
1052 printf("%d:tx-bytes-per-t ", j++);
1053 printf("%d:tx-pkts-per-t ", j++);
1054 printf("%d:tx-drops-per-t ", j++);
1055 printf("%d:tx-errors-per-t ", j++);
1057 printf("%d:tx-bytes ", j++);
1058 printf("%d:tx-pkts ", j++);
1059 printf("%d:tx-drops ", j++);
1060 printf("%d:tx-errors ", j++);
1062 printf("%d:context-switches-per-t ", j++);
1063 printf("%d:mem-free ", j++);
1064 printf("%d:mem-used ", j++);
1065 printf("%d:mem-total ", j++);
1066 printf("%d:swap-free ", j++);
1067 printf("%d:swap-used ", j++);
1068 printf("%d:swap-total ", j++);
1069 printf("%d:procs-total ", j++);
1070 printf("%d:procs-in-run ", j++);
1071 printf("%d:procs-in-iow ", j++);
1073 cpus = get_number_cpus();
1075 for (i = 0; i < cpus; ++i) {
1076 printf("%d:cpu%i-usr-per-t ", j++, i);
1077 printf("%d:cpu%i-nice-per-t ", j++, i);
1078 printf("%d:cpu%i-sys-per-t ", j++, i);
1079 printf("%d:cpu%i-idle-per-t ", j++, i);
1080 printf("%d:cpu%i-iow-per-t ", j++, i);
1082 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1083 printf("%d:cpu%i-net-irqs ", j++, i);
1085 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1086 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1087 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1088 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1091 if (iswireless(abs)) {
1092 printf("%d:wifi-link-qual-per-t ", j++);
1093 printf("%d:wifi-link-qual ", j++);
1094 printf("%d:wifi-link-qual-max ", j++);
1096 printf("%d:wifi-signal-dbm-per-t ", j++);
1097 printf("%d:wifi-signal-dbm ", j++);
1100 puts("");
1101 printf("# data:\n");
1102 fflush(stdout);
1105 static int term_main(const char *ifname, uint64_t ms_interval,
1106 unsigned int top_cpus __maybe_unused)
1108 int first = 1;
1110 do {
1111 stats_sample_generic(ifname, ms_interval);
1113 if (first) {
1114 first = 0;
1115 term_csv_header(ifname, &stats_new, ms_interval);
1118 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1119 } while (stats_loop && !sigint);
1121 return 0;
1124 int main(int argc, char **argv)
1126 short ifflags = 0;
1127 int c, opt_index, ret, cpus, promisc = 0;
1128 unsigned int top_cpus = 10;
1129 uint64_t interval = 1000;
1130 char *ifname = NULL;
1131 int (*func_main)(const char *ifname, uint64_t ms_interval,
1132 unsigned int top_cpus) = screen_main;
1134 setfsuid(getuid());
1135 setfsgid(getgid());
1137 while ((c = getopt_long(argc, argv, short_options, long_options,
1138 &opt_index)) != EOF) {
1139 switch (c) {
1140 case 'h':
1141 help();
1142 break;
1143 case 'v':
1144 version();
1145 break;
1146 case 'd':
1147 ifname = xstrndup(optarg, IFNAMSIZ);
1148 break;
1149 case 't':
1150 interval = strtoul(optarg, NULL, 10);
1151 break;
1152 case 'n':
1153 top_cpus = strtoul(optarg, NULL, 10);
1154 if (top_cpus < 1)
1155 panic("Number of top hitter CPUs must be greater than 0");
1156 break;
1157 case 'l':
1158 stats_loop = 1;
1159 break;
1160 case 'p':
1161 promisc = 1;
1162 break;
1163 case 'c':
1164 func_main = term_main;
1165 break;
1166 case '?':
1167 switch (optopt) {
1168 case 'd':
1169 case 't':
1170 panic("Option -%c requires an argument!\n",
1171 optopt);
1172 default:
1173 if (isprint(optopt))
1174 printf("Unknown option character `0x%X\'!\n", optopt);
1175 die();
1177 default:
1178 break;
1182 if (argc == 1)
1183 help();
1185 if (argc == 2)
1186 ifname = xstrndup(argv[1], IFNAMSIZ);
1187 if (ifname == NULL)
1188 panic("No networking device given!\n");
1190 if (!strncmp("lo", ifname, IFNAMSIZ))
1191 panic("lo is not supported!\n");
1192 if (device_mtu(ifname) == 0)
1193 panic("This is no networking device!\n");
1195 register_signal(SIGINT, signal_handler);
1196 register_signal(SIGHUP, signal_handler);
1198 cpus = get_number_cpus();
1199 top_cpus = min(top_cpus, cpus);
1201 stats_alloc(&stats_old, cpus);
1202 stats_alloc(&stats_new, cpus);
1203 stats_alloc(&stats_delta, cpus);
1205 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1207 if (promisc)
1208 ifflags = enter_promiscuous_mode(ifname);
1209 ret = func_main(ifname, interval, top_cpus);
1210 if (promisc)
1211 leave_promiscuous_mode(ifname, ifflags);
1213 xfree(ifname);
1214 return ret;