ifpps: Rename cpu number parameter to stats_top()
[netsniff-ng.git] / ifpps.c
blob53e61aa94a87cee7b7e21fa7f61f976ccc492105
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, forks;
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, "processes"))) {
422 ptr += strlen("processes");
423 stats->forks = strtoul(ptr, &ptr, 10);
424 } else if ((ptr = strstr(buff, "procs_running"))) {
425 ptr += strlen("procs_running");
426 stats->procs_run = strtoul(ptr, &ptr, 10);
427 } else if ((ptr = strstr(buff, "procs_blocked"))) {
428 ptr += strlen("procs_blocked");
429 stats->procs_iow = strtoul(ptr, &ptr, 10);
431 next:
432 memset(buff, 0, sizeof(buff));
435 fclose(fp);
436 return 0;
439 static int stats_proc_procs(struct ifstat *stats)
441 DIR *dir;
442 struct dirent *e;
444 dir = opendir("/proc");
445 if (!dir)
446 panic("Cannot open /proc\n");
448 stats->procs_total = 0;
450 while ((e = readdir(dir)) != NULL) {
451 const char *name = e->d_name;
452 char *end;
453 unsigned int pid = strtoul(name, &end, 10);
455 /* not a number */
456 if (pid == 0 && end == name)
457 continue;
459 stats->procs_total++;
462 closedir(dir);
464 return 0;
467 static int adjust_dbm_level(int in_dbm, int dbm_val)
469 if (!in_dbm)
470 return dbm_val;
472 return dbm_val - 0x100;
475 static int stats_wireless(const char *ifname, struct ifstat *stats)
477 int ret;
478 struct iw_statistics ws;
480 ret = wireless_sigqual(ifname, &ws);
481 if (ret != 0) {
482 stats->wifi.bitrate = 0;
483 return -EINVAL;
486 stats->wifi.bitrate = wireless_bitrate(ifname);
488 stats->wifi.signal_level =
489 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
491 stats->wifi.link_qual = ws.qual.qual;
492 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
494 return ret;
497 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
498 #define DIFF(member) do { \
499 if (sizeof(diff->member) != sizeof(new->member) || \
500 sizeof(diff->member) != sizeof(old->member)) \
501 bug(); \
502 bug_on((new->member - old->member) > (new->member)); \
503 DIFF1(member); \
504 } while (0)
506 static void stats_diff(struct ifstat *old, struct ifstat *new,
507 struct ifstat *diff)
509 int cpus, i;
511 DIFF(rx_bytes);
512 DIFF(rx_packets);
513 DIFF(rx_drops);
514 DIFF(rx_errors);
515 DIFF(rx_fifo);
516 DIFF(rx_frame);
517 DIFF(rx_multi);
519 DIFF(tx_bytes);
520 DIFF(tx_packets);
521 DIFF(tx_drops);
522 DIFF(tx_errors);
523 DIFF(tx_fifo);
524 DIFF(tx_colls);
525 DIFF(tx_carrier);
527 DIFF1(wifi.signal_level);
528 DIFF1(wifi.link_qual);
530 DIFF1(cswitch);
532 cpus = get_number_cpus();
534 for (i = 0; i < cpus; ++i) {
535 DIFF(irqs[i]);
536 DIFF(irqs_srx[i]);
537 DIFF(irqs_stx[i]);
539 DIFF1(cpu_user[i]);
540 DIFF1(cpu_nice[i]);
541 DIFF1(cpu_sys[i]);
542 DIFF1(cpu_idle[i]);
543 DIFF1(cpu_iow[i]);
547 static void stats_fetch(const char *ifname, struct ifstat *stats)
549 if (stats_proc_net_dev(ifname, stats) < 0)
550 panic("Cannot fetch device stats!\n");
551 if (stats_proc_softirqs(stats) < 0)
552 panic("Cannot fetch software interrupts!\n");
553 if (stats_proc_memory(stats) < 0)
554 panic("Cannot fetch memory stats!\n");
555 if (stats_proc_system(stats) < 0)
556 panic("Cannot fetch system stats!\n");
557 if (stats_proc_procs(stats) < 0)
558 panic("Cannot fetch process stats!\n");
560 stats_proc_interrupts((char *) ifname, stats);
562 stats_wireless(ifname, stats);
565 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
567 int cpus = get_number_cpus();
569 stats_zero(&stats_old, cpus);
570 stats_zero(&stats_new, cpus);
571 stats_zero(&stats_delta, cpus);
573 stats_fetch(ifname, &stats_old);
574 usleep(ms_interval * 1000);
575 stats_fetch(ifname, &stats_new);
577 stats_diff(&stats_old, &stats_new, &stats_delta);
580 static int cmp_hits(const void *p1, const void *p2)
582 const struct cpu_hit *h1 = p1, *h2 = p2;
585 * We want the hits sorted in descending order, thus reverse the return
586 * values.
588 if (h1->hit == h2->hit)
589 return 0;
590 else if (h1->hit < h2->hit)
591 return 1;
592 else
593 return -1;
596 static int cmp_irqs_rel(const void *p1, const void *p2)
598 const struct cpu_hit *h1 = p1, *h2 = p2;
601 * We want the hits sorted in descending order, thus reverse the return
602 * values.
604 if (h1->irqs_rel == h2->irqs_rel)
605 return 0;
606 else if (h1->irqs_rel < h2->irqs_rel)
607 return 1;
608 else
609 return -1;
612 static int cmp_irqs_abs(const void *p1, const void *p2)
614 const struct cpu_hit *h1 = p1, *h2 = p2;
617 * We want the hits sorted in descending order, thus reverse the return
618 * values.
620 if (h1->irqs_abs == h2->irqs_abs)
621 return 0;
622 else if (h1->irqs_abs < h2->irqs_abs)
623 return 1;
624 else
625 return -1;
628 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
629 int cpus)
631 int i;
633 for (i = 0; i < cpus; ++i) {
634 cpu_hits[i].idx = i;
635 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
636 cpu_hits[i].irqs_rel = rel->irqs[i];
637 cpu_hits[i].irqs_abs = abs->irqs[i];
641 static void screen_init(WINDOW **screen)
643 (*screen) = initscr();
645 raw();
646 noecho();
647 cbreak();
648 nodelay((*screen), TRUE);
650 keypad(stdscr, TRUE);
652 refresh();
653 wrefresh((*screen));
656 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
657 uint64_t ms_interval, unsigned int top_cpus)
659 size_t len = 0;
660 char buff[64];
661 struct ethtool_drvinfo drvinf;
662 u32 rate = device_bitrate(ifname);
663 int link = ethtool_link(ifname);
664 unsigned int cpus = get_number_cpus();
666 memset(&drvinf, 0, sizeof(drvinf));
667 ethtool_drvinf(ifname, &drvinf);
669 memset(buff, 0, sizeof(buff));
670 if (rate)
671 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
672 if (link >= 0)
673 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
674 link == 0 ? "no" : "yes");
676 mvwprintw(screen, (*voff)++, 2,
677 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u%s/%u"
678 " ",
679 ifname, drvinf.driver, buff, ms_interval, top_cpus,
680 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
683 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
684 int *voff)
686 attron(A_REVERSE);
688 mvwprintw(screen, (*voff)++, 0,
689 " rx: %16.3llf MiB/t "
690 "%10llu pkts/t "
691 "%10llu drops/t "
692 "%10llu errors/t ",
693 ((long double) rel->rx_bytes) / (1LLU << 20),
694 rel->rx_packets, rel->rx_drops, rel->rx_errors);
696 mvwprintw(screen, (*voff)++, 0,
697 " tx: %16.3llf MiB/t "
698 "%10llu pkts/t "
699 "%10llu drops/t "
700 "%10llu errors/t ",
701 ((long double) rel->tx_bytes) / (1LLU << 20),
702 rel->tx_packets, rel->tx_drops, rel->tx_errors);
704 attroff(A_REVERSE);
707 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
708 int *voff)
710 mvwprintw(screen, (*voff)++, 2,
711 "rx: %16.3llf MiB "
712 "%10llu pkts "
713 "%10llu drops "
714 "%10llu errors",
715 ((long double) abs->rx_bytes) / (1LLU << 20),
716 abs->rx_packets, abs->rx_drops, abs->rx_errors);
718 mvwprintw(screen, (*voff)++, 2,
719 "tx: %16.3llf MiB "
720 "%10llu pkts "
721 "%10llu drops "
722 "%10llu errors",
723 ((long double) abs->tx_bytes) / (1LLU << 20),
724 abs->tx_packets, abs->tx_drops, abs->tx_errors);
727 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
728 const struct ifstat *abs, int *voff)
730 mvwprintw(screen, (*voff)++, 2,
731 "sys: %14u cs/t "
732 "%11u procs "
733 "%11u running "
734 "%10u iowait",
735 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
738 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
740 mvwprintw(screen, (*voff)++, 2,
741 "mem: %13uM total "
742 "%9uM used "
743 "%11uM active "
744 "%10uM inactive",
745 abs->mem_total / 1024,
746 (abs->mem_total - abs->mem_free) / 1024,
747 abs->mem_active / 1024,
748 abs->mem_inactive / 1024);
750 mvwprintw(screen, (*voff)++, 2,
751 "swap: %12uM total "
752 "%9uM used "
753 "%11uM cached",
754 abs->swap_total / 1024,
755 (abs->swap_total - abs->swap_free) / 1024,
756 abs->swap_cached / 1024);
759 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
760 int *voff, unsigned int idx, char *tag)
762 int max_padd = padding_from_num(get_number_cpus());
763 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
764 rel->cpu_idle[idx] + rel->cpu_iow[idx];
766 mvwprintw(screen, (*voff)++, 2,
767 "cpu%*d%s:%s %13.1lf%% usr/t "
768 "%9.1lf%% sys/t "
769 "%10.1lf%% idl/t "
770 "%11.1lf%% iow/t ", max_padd, idx,
771 tag, strlen(tag) == 0 ? " " : "",
772 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
773 100.0 * rel->cpu_sys[idx] / all,
774 100.0 * rel->cpu_idle[idx] / all,
775 100.0 * rel->cpu_iow[idx] / all);
778 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
779 int top_cpus, int *voff)
781 int i;
782 int cpus = get_number_cpus();
784 if (top_cpus == 0)
785 return;
787 /* Display top hitter */
788 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
790 /* Make sure we don't display the min. hitter twice */
791 if (top_cpus == cpus)
792 top_cpus--;
794 for (i = 1; i < top_cpus; ++i)
795 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "");
797 /* Display minimum hitter */
798 if (cpus != 1)
799 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
802 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
803 int *voff, unsigned int idx, char *tag)
805 int max_padd = padding_from_num(get_number_cpus());
807 mvwprintw(screen, (*voff)++, 2,
808 "cpu%*d%s:%s %14llu irqs/t "
809 "%15llu sirq rx/t "
810 "%15llu sirq tx/t ", max_padd, idx,
811 tag, strlen(tag) == 0 ? " " : "",
812 rel->irqs[idx],
813 rel->irqs_srx[idx],
814 rel->irqs_stx[idx]);
817 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
818 int top_cpus, int *voff)
820 int i;
821 int cpus = get_number_cpus();
823 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
825 if (top_cpus == cpus)
826 top_cpus--;
828 for (i = 1; i < top_cpus; ++i)
829 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "");
831 if (cpus != 1)
832 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
835 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
836 int *voff, unsigned int idx, char *tag)
838 int max_padd = padding_from_num(get_number_cpus());
840 mvwprintw(screen, (*voff)++, 2,
841 "cpu%*d%s:%s %14llu irqs", max_padd, idx,
842 tag, strlen(tag) == 0 ? " " : "",
843 abs->irqs[idx]);
846 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
847 int top_cpus, int *voff)
849 int i;
850 int cpus = get_number_cpus();
852 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
854 if (top_cpus == cpus)
855 top_cpus--;
857 for (i = 1; i < top_cpus; ++i)
858 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "");
860 if (cpus != 1)
861 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
864 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
865 const struct ifstat *abs, int *voff)
867 if (iswireless(abs)) {
868 mvwprintw(screen, (*voff)++, 2,
869 "linkqual: %7d/%d (%d/t) ",
870 abs->wifi.link_qual,
871 abs->wifi.link_qual_max,
872 rel->wifi.link_qual);
874 mvwprintw(screen, (*voff)++, 2,
875 "signal: %8d dBm (%d dBm/t) ",
876 abs->wifi.signal_level,
877 rel->wifi.signal_level);
881 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
882 const struct ifstat *abs, int *first, uint64_t ms_interval,
883 unsigned int top_cpus)
885 int cpus, top, voff = 1, cvoff = 2;
887 curs_set(0);
889 cpus = get_number_cpus();
890 top = min(cpus, top_cpus);
892 stats_top(rel, abs, cpus);
894 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
896 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
898 voff++;
899 screen_net_dev_rel(screen, rel, &voff);
901 voff++;
902 screen_net_dev_abs(screen, abs, &voff);
904 voff++;
905 screen_sys(screen, rel, abs, &voff);
907 voff++;
908 screen_mem_swap(screen, abs, &voff);
910 voff++;
911 screen_percpu_states(screen, rel, top, &voff);
913 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
915 voff++;
916 screen_percpu_irqs_rel(screen, rel, top, &voff);
918 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
920 voff++;
921 screen_percpu_irqs_abs(screen, abs, top, &voff);
923 voff++;
924 screen_wireless(screen, rel, abs, &voff);
926 if (*first) {
927 mvwprintw(screen, cvoff, 2, "Collecting data ...");
928 *first = 0;
929 } else {
930 mvwprintw(screen, cvoff, 2, " ");
933 wrefresh(screen);
934 refresh();
937 static void screen_end(void)
939 endwin();
942 static int screen_main(const char *ifname, uint64_t ms_interval,
943 unsigned int top_cpus)
945 int first = 1, key;
947 screen_init(&stats_screen);
949 while (!sigint) {
950 key = getch();
951 if (key == 'q' || key == 0x1b || key == KEY_F(10))
952 break;
954 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
955 &first, ms_interval, top_cpus);
957 stats_sample_generic(ifname, ms_interval);
960 screen_end();
962 return 0;
965 static void term_csv(const char *ifname, const struct ifstat *rel,
966 const struct ifstat *abs, uint64_t ms_interval)
968 int cpus, i;
970 printf("%ld ", time(0));
972 printf("%llu ", rel->rx_bytes);
973 printf("%llu ", rel->rx_packets);
974 printf("%llu ", rel->rx_drops);
975 printf("%llu ", rel->rx_errors);
977 printf("%llu ", abs->rx_bytes);
978 printf("%llu ", abs->rx_packets);
979 printf("%llu ", abs->rx_drops);
980 printf("%llu ", abs->rx_errors);
982 printf("%llu ", rel->tx_bytes);
983 printf("%llu ", rel->tx_packets);
984 printf("%llu ", rel->tx_drops);
985 printf("%llu ", rel->tx_errors);
987 printf("%llu ", abs->tx_bytes);
988 printf("%llu ", abs->tx_packets);
989 printf("%llu ", abs->tx_drops);
990 printf("%llu ", abs->tx_errors);
992 printf("%u ", rel->cswitch);
993 printf("%lu ", abs->mem_free);
994 printf("%lu ", abs->mem_total - abs->mem_free);
995 printf("%lu ", abs->mem_total);
996 printf("%lu ", abs->swap_free);
997 printf("%lu ", abs->swap_total - abs->swap_free);
998 printf("%lu ", abs->swap_total);
999 printf("%u ", abs->procs_total);
1000 printf("%u ", abs->procs_run);
1001 printf("%u ", abs->procs_iow);
1003 cpus = get_number_cpus();
1005 for (i = 0; i < cpus; ++i) {
1006 printf("%lu ", rel->cpu_user[i]);
1007 printf("%lu ", rel->cpu_nice[i]);
1008 printf("%lu ", rel->cpu_sys[i]);
1009 printf("%lu ", rel->cpu_idle[i]);
1010 printf("%lu ", rel->cpu_iow[i]);
1012 printf("%llu ", rel->irqs[i]);
1013 printf("%llu ", abs->irqs[i]);
1015 printf("%llu ", rel->irqs_srx[i]);
1016 printf("%llu ", abs->irqs_srx[i]);
1018 printf("%llu ", rel->irqs_stx[i]);
1019 printf("%llu ", abs->irqs_stx[i]);
1022 if (iswireless(abs)) {
1023 printf("%u ", rel->wifi.link_qual);
1024 printf("%u ", abs->wifi.link_qual);
1025 printf("%u ", abs->wifi.link_qual_max);
1027 printf("%d ", rel->wifi.signal_level);
1028 printf("%d ", abs->wifi.signal_level);
1031 puts("");
1032 fflush(stdout);
1035 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1036 uint64_t ms_interval)
1038 int cpus, i, j = 1;
1040 printf("# gnuplot dump (#col:description)\n");
1041 printf("# networking interface: %s\n", ifname);
1042 printf("# sampling interval (t): %lu ms\n", ms_interval);
1043 printf("# %d:unixtime ", j++);
1045 printf("%d:rx-bytes-per-t ", j++);
1046 printf("%d:rx-pkts-per-t ", j++);
1047 printf("%d:rx-drops-per-t ", j++);
1048 printf("%d:rx-errors-per-t ", j++);
1050 printf("%d:rx-bytes ", j++);
1051 printf("%d:rx-pkts ", j++);
1052 printf("%d:rx-drops ", j++);
1053 printf("%d:rx-errors ", j++);
1055 printf("%d:tx-bytes-per-t ", j++);
1056 printf("%d:tx-pkts-per-t ", j++);
1057 printf("%d:tx-drops-per-t ", j++);
1058 printf("%d:tx-errors-per-t ", j++);
1060 printf("%d:tx-bytes ", j++);
1061 printf("%d:tx-pkts ", j++);
1062 printf("%d:tx-drops ", j++);
1063 printf("%d:tx-errors ", j++);
1065 printf("%d:context-switches-per-t ", j++);
1066 printf("%d:mem-free ", j++);
1067 printf("%d:mem-used ", j++);
1068 printf("%d:mem-total ", j++);
1069 printf("%d:swap-free ", j++);
1070 printf("%d:swap-used ", j++);
1071 printf("%d:swap-total ", j++);
1072 printf("%d:procs-total ", j++);
1073 printf("%d:procs-in-run ", j++);
1074 printf("%d:procs-in-iow ", j++);
1076 cpus = get_number_cpus();
1078 for (i = 0; i < cpus; ++i) {
1079 printf("%d:cpu%i-usr-per-t ", j++, i);
1080 printf("%d:cpu%i-nice-per-t ", j++, i);
1081 printf("%d:cpu%i-sys-per-t ", j++, i);
1082 printf("%d:cpu%i-idle-per-t ", j++, i);
1083 printf("%d:cpu%i-iow-per-t ", j++, i);
1085 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1086 printf("%d:cpu%i-net-irqs ", j++, i);
1088 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1089 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1090 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1091 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1094 if (iswireless(abs)) {
1095 printf("%d:wifi-link-qual-per-t ", j++);
1096 printf("%d:wifi-link-qual ", j++);
1097 printf("%d:wifi-link-qual-max ", j++);
1099 printf("%d:wifi-signal-dbm-per-t ", j++);
1100 printf("%d:wifi-signal-dbm ", j++);
1103 puts("");
1104 printf("# data:\n");
1105 fflush(stdout);
1108 static int term_main(const char *ifname, uint64_t ms_interval,
1109 unsigned int top_cpus __maybe_unused)
1111 int first = 1;
1113 do {
1114 stats_sample_generic(ifname, ms_interval);
1116 if (first) {
1117 first = 0;
1118 term_csv_header(ifname, &stats_new, ms_interval);
1121 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1122 } while (stats_loop && !sigint);
1124 return 0;
1127 int main(int argc, char **argv)
1129 short ifflags = 0;
1130 int c, opt_index, ret, cpus, promisc = 0;
1131 unsigned int top_cpus = 10;
1132 uint64_t interval = 1000;
1133 char *ifname = NULL;
1134 int (*func_main)(const char *ifname, uint64_t ms_interval,
1135 unsigned int top_cpus) = screen_main;
1137 setfsuid(getuid());
1138 setfsgid(getgid());
1140 while ((c = getopt_long(argc, argv, short_options, long_options,
1141 &opt_index)) != EOF) {
1142 switch (c) {
1143 case 'h':
1144 help();
1145 break;
1146 case 'v':
1147 version();
1148 break;
1149 case 'd':
1150 ifname = xstrndup(optarg, IFNAMSIZ);
1151 break;
1152 case 't':
1153 interval = strtoul(optarg, NULL, 10);
1154 break;
1155 case 'n':
1156 top_cpus = strtoul(optarg, NULL, 10);
1157 if (top_cpus < 1)
1158 panic("Number of top hitter CPUs must be greater than 0");
1159 break;
1160 case 'l':
1161 stats_loop = 1;
1162 break;
1163 case 'p':
1164 promisc = 1;
1165 break;
1166 case 'c':
1167 func_main = term_main;
1168 break;
1169 case '?':
1170 switch (optopt) {
1171 case 'd':
1172 case 't':
1173 panic("Option -%c requires an argument!\n",
1174 optopt);
1175 default:
1176 if (isprint(optopt))
1177 printf("Unknown option character `0x%X\'!\n", optopt);
1178 die();
1180 default:
1181 break;
1185 if (argc == 1)
1186 help();
1188 if (argc == 2)
1189 ifname = xstrndup(argv[1], IFNAMSIZ);
1190 if (ifname == NULL)
1191 panic("No networking device given!\n");
1193 if (!strncmp("lo", ifname, IFNAMSIZ))
1194 panic("lo is not supported!\n");
1195 if (device_mtu(ifname) == 0)
1196 panic("This is no networking device!\n");
1198 register_signal(SIGINT, signal_handler);
1199 register_signal(SIGHUP, signal_handler);
1201 cpus = get_number_cpus();
1202 top_cpus = min(top_cpus, cpus);
1204 stats_alloc(&stats_old, cpus);
1205 stats_alloc(&stats_new, cpus);
1206 stats_alloc(&stats_delta, cpus);
1208 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1210 if (promisc)
1211 ifflags = enter_promiscuous_mode(ifname);
1212 ret = func_main(ifname, interval, top_cpus);
1213 if (promisc)
1214 leave_promiscuous_mode(ifname, ifflags);
1216 xfree(ifname);
1217 return ret;