configure: when we have all deps rather tell "all tools will be built"
[netsniff-ng.git] / ifpps.c
blob0ae77cbd6b0b90482925e1b4fba0752bc497c928
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"
31 #include "screen.h"
33 struct wifi_stat {
34 uint32_t bitrate;
35 int16_t link_qual, link_qual_max;
36 int signal_level /*, noise_level*/;
39 struct ifstat {
40 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
41 long long unsigned int rx_fifo, rx_frame, rx_multi;
42 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
43 long long unsigned int tx_fifo, tx_colls, tx_carrier;
44 uint64_t mem_free, mem_total, mem_active, mem_inactive;
45 uint64_t swap_total, swap_free, swap_cached;
46 uint32_t irq_nr, procs_total, procs_run, procs_iow, cswitch;
47 struct wifi_stat wifi;
49 * Pointer members need to be last in order for stats_zero() to work
50 * properly.
52 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
53 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
56 struct cpu_hit {
57 unsigned int idx;
58 uint64_t hit;
59 long long unsigned int irqs_rel, irqs_abs;
62 static volatile sig_atomic_t sigint = 0;
63 static struct ifstat stats_old, stats_new, stats_delta;
64 static struct cpu_hit *cpu_hits;
65 static int stats_loop = 0;
66 static WINDOW *stats_screen = NULL;
68 static const char *short_options = "d:t:n:vhclp";
69 static const struct option long_options[] = {
70 {"dev", required_argument, NULL, 'd'},
71 {"interval", required_argument, NULL, 't'},
72 {"num-cpus", required_argument, NULL, 'n'},
73 {"promisc", no_argument, NULL, 'p'},
74 {"csv", no_argument, NULL, 'c'},
75 {"loop", no_argument, NULL, 'l'},
76 {"version", no_argument, NULL, 'v'},
77 {"help", no_argument, NULL, 'h'},
78 {NULL, 0, NULL, 0}
81 static void signal_handler(int number)
83 switch (number) {
84 case SIGINT:
85 sigint = 1;
86 break;
87 case SIGHUP:
88 default:
89 break;
93 static inline int iswireless(const struct ifstat *stats)
95 return stats->wifi.bitrate > 0;
98 static void __noreturn help(void)
100 printf("\nifpps %s, top-like kernel networking and system statistics\n",
101 VERSION_STRING);
102 puts("http://www.netsniff-ng.org\n\n"
103 "Usage: ifpps [options] || ifpps <netdev>\n"
104 "Options:\n"
105 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
106 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
107 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
108 " in ncurses mode (default 10)\n"
109 " -p|--promisc Promiscuous mode\n"
110 " -c|--csv Output to terminal as Gnuplot-ready data\n"
111 " -l|--loop Continuous CSV output\n"
112 " -v|--version Print version and exit\n"
113 " -h|--help Print this help and exit\n\n"
114 "Examples:\n"
115 " ifpps eth0\n"
116 " ifpps -pd eth0\n"
117 " ifpps -lpcd wlan0 > plot.dat\n\n"
118 "Note:\n"
119 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
120 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
121 "Please report bugs to <bugs@netsniff-ng.org>\n"
122 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
123 "Swiss federal institute of technology (ETH Zurich)\n"
124 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
125 "License: GNU GPL version 2.0\n"
126 "This is free software: you are free to change and redistribute it.\n"
127 "There is NO WARRANTY, to the extent permitted by law.\n");
128 die();
131 static void __noreturn version(void)
133 printf("\nifpps %s, top-like kernel networking and system statistics\n",
134 VERSION_LONG);
135 puts("http://www.netsniff-ng.org\n\n"
136 "Please report bugs to <bugs@netsniff-ng.org>\n"
137 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
138 "Swiss federal institute of technology (ETH Zurich)\n"
139 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
140 "License: GNU GPL version 2.0\n"
141 "This is free software: you are free to change and redistribute it.\n"
142 "There is NO WARRANTY, to the extent permitted by law.\n");
143 die();
146 static inline int padding_from_num(int n)
148 int i = 0;
149 do i++;
150 while ((n /= 10) > 0);
151 return i;
154 #define STATS_ALLOC1(member) \
155 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
157 static void stats_alloc(struct ifstat *stats, int cpus)
159 STATS_ALLOC1(irqs);
160 STATS_ALLOC1(irqs_srx);
161 STATS_ALLOC1(irqs_stx);
163 STATS_ALLOC1(cpu_user);
164 STATS_ALLOC1(cpu_sys);
165 STATS_ALLOC1(cpu_nice);
166 STATS_ALLOC1(cpu_idle);
167 STATS_ALLOC1(cpu_iow);
170 #define STATS_ZERO1(member) \
171 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
173 static void stats_zero(struct ifstat *stats, int cpus)
175 /* Only clear the non-pointer members */
176 memset(stats, 0, offsetof(struct ifstat, irqs));
178 STATS_ZERO1(irqs);
179 STATS_ZERO1(irqs_srx);
180 STATS_ZERO1(irqs_stx);
182 STATS_ZERO1(cpu_user);
183 STATS_ZERO1(cpu_sys);
184 STATS_ZERO1(cpu_nice);
185 STATS_ZERO1(cpu_idle);
186 STATS_ZERO1(cpu_iow);
189 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
191 int ret = -EINVAL;
192 char buff[256];
193 FILE *fp;
195 fp = fopen("/proc/net/dev", "r");
196 if (!fp)
197 panic("Cannot open /proc/net/dev!\n");
199 if (fgets(buff, sizeof(buff), fp)) { ; }
200 if (fgets(buff, sizeof(buff), fp)) { ; }
202 memset(buff, 0, sizeof(buff));
204 while (fgets(buff, sizeof(buff), fp) != NULL) {
205 buff[sizeof(buff) -1] = 0;
207 if (strstr(buff, ifname) == NULL)
208 continue;
210 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
211 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
212 &stats->rx_bytes, &stats->rx_packets,
213 &stats->rx_errors, &stats->rx_drops,
214 &stats->rx_fifo, &stats->rx_frame,
215 &stats->rx_multi, &stats->tx_bytes,
216 &stats->tx_packets, &stats->tx_errors,
217 &stats->tx_drops, &stats->tx_fifo,
218 &stats->tx_colls, &stats->tx_carrier) == 14) {
219 ret = 0;
220 break;
223 memset(buff, 0, sizeof(buff));
226 fclose(fp);
227 return ret;
230 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
232 int ret = -EINVAL, i, cpus, try = 0;
233 char *ptr, *buff;
234 bool seen = false;
235 size_t buff_len;
236 struct ethtool_drvinfo drvinf;
237 FILE *fp;
239 fp = fopen("/proc/interrupts", "r");
240 if (!fp)
241 panic("Cannot open /proc/interrupts!\n");
243 cpus = get_number_cpus();
244 buff_len = cpus * 128;
245 buff = xmalloc(buff_len);
246 retry:
247 fseek(fp, 0, SEEK_SET);
248 memset(buff, 0, buff_len);
250 while (fgets(buff, buff_len, fp) != NULL) {
251 buff[buff_len - 1] = 0;
252 ptr = buff;
254 if (strstr(buff, ifname) == NULL)
255 continue;
257 /* XXX: remove this one here */
258 stats->irq_nr = strtol(ptr, &ptr, 10);
259 bug_on(stats->irq_nr == 0);
261 if (ptr)
262 ptr++;
263 for (i = 0; i < cpus && ptr; ++i) {
264 if (seen)
265 stats->irqs[i] += strtol(ptr, &ptr, 10);
266 else
267 stats->irqs[i] = strtol(ptr, &ptr, 10);
268 if (i == cpus - 1) {
269 ret = 0;
270 seen = true;
274 memset(buff, 0, buff_len);
277 if (ret == -EINVAL && try == 0) {
278 memset(&drvinf, 0, sizeof(drvinf));
279 if (ethtool_drvinf(ifname, &drvinf) < 0)
280 goto done;
282 ifname = drvinf.driver;
283 try++;
285 goto retry;
287 done:
288 xfree(buff);
289 fclose(fp);
290 return ret;
293 static int stats_proc_softirqs(struct ifstat *stats)
295 int i, cpus;
296 char *ptr, *buff;
297 size_t buff_len;
298 FILE *fp;
299 enum {
300 softirqs_net_rx,
301 softirqs_net_tx,
302 } net_type;
304 fp = fopen("/proc/softirqs", "r");
305 if (!fp)
306 panic("Cannot open /proc/softirqs!\n");
308 cpus = get_number_cpus();
309 buff_len = cpus * 128;
310 buff = xmalloc(buff_len);
312 memset(buff, 0, buff_len);
314 while (fgets(buff, buff_len, fp) != NULL) {
315 buff[buff_len - 1] = 0;
317 if ((ptr = strstr(buff, "NET_TX:")))
318 net_type = softirqs_net_tx;
319 else if ((ptr = strstr(buff, "NET_RX:")))
320 net_type = softirqs_net_rx;
321 else
322 continue;
324 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
325 switch (net_type) {
326 case softirqs_net_tx:
327 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
328 break;
329 case softirqs_net_rx:
330 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
331 break;
335 memset(buff, 0, buff_len);
338 xfree(buff);
339 fclose(fp);
340 return 0;
343 static int stats_proc_memory(struct ifstat *stats)
345 char *ptr, buff[256];
346 FILE *fp;
348 fp = fopen("/proc/meminfo", "r");
349 if (!fp)
350 panic("Cannot open /proc/meminfo!\n");
352 memset(buff, 0, sizeof(buff));
354 while (fgets(buff, sizeof(buff), fp) != NULL) {
355 buff[sizeof(buff) - 1] = 0;
357 if ((ptr = strstr(buff, "MemTotal:"))) {
358 ptr += strlen("MemTotal:");
359 stats->mem_total = strtoul(ptr, &ptr, 10);
360 } else if ((ptr = strstr(buff, "MemFree:"))) {
361 ptr += strlen("MemFree:");
362 stats->mem_free = strtoul(ptr, &ptr, 10);
363 } else if ((ptr = strstr(buff, "Active:"))) {
364 ptr += strlen("Active:");
365 stats->mem_active = strtoul(ptr, &ptr, 10);
366 } else if ((ptr = strstr(buff, "Inactive:"))) {
367 ptr += strlen("Inactive:");
368 stats->mem_inactive = strtoul(ptr, &ptr, 10);
369 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
370 ptr += strlen("SwapTotal:");
371 stats->swap_total = strtoul(ptr, &ptr, 10);
372 } else if ((ptr = strstr(buff, "SwapFree:"))) {
373 ptr += strlen("SwapFree:");
374 stats->swap_free = strtoul(ptr, &ptr, 10);
375 } else if ((ptr = strstr(buff, "SwapCached:"))) {
376 ptr += strlen("SwapCached:");
377 stats->swap_cached = strtoul(ptr, &ptr, 10);
380 memset(buff, 0, sizeof(buff));
383 fclose(fp);
384 return 0;
387 static int stats_proc_system(struct ifstat *stats)
389 int cpu, cpus;
390 char *ptr, buff[256];
391 FILE *fp;
393 fp = fopen("/proc/stat", "r");
394 if (!fp)
395 panic("Cannot open /proc/stat!\n");
397 cpus = get_number_cpus();
399 memset(buff, 0, sizeof(buff));
401 while (fgets(buff, sizeof(buff), fp) != NULL) {
402 buff[sizeof(buff) - 1] = 0;
404 if ((ptr = strstr(buff, "cpu"))) {
405 ptr += strlen("cpu");
406 if (isblank(*ptr))
407 goto next;
409 cpu = strtol(ptr, &ptr, 10);
410 bug_on(cpu > cpus);
412 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
413 &stats->cpu_user[cpu],
414 &stats->cpu_nice[cpu],
415 &stats->cpu_sys[cpu],
416 &stats->cpu_idle[cpu],
417 &stats->cpu_iow[cpu]) != 5)
418 goto next;
419 } else if ((ptr = strstr(buff, "ctxt"))) {
420 ptr += strlen("ctxt");
421 stats->cswitch = strtoul(ptr, &ptr, 10);
422 } else if ((ptr = strstr(buff, "procs_running"))) {
423 ptr += strlen("procs_running");
424 stats->procs_run = strtoul(ptr, &ptr, 10);
425 } else if ((ptr = strstr(buff, "procs_blocked"))) {
426 ptr += strlen("procs_blocked");
427 stats->procs_iow = strtoul(ptr, &ptr, 10);
429 next:
430 memset(buff, 0, sizeof(buff));
433 fclose(fp);
434 return 0;
437 static int stats_proc_procs(struct ifstat *stats)
439 DIR *dir;
440 struct dirent *e;
442 dir = opendir("/proc");
443 if (!dir)
444 panic("Cannot open /proc\n");
446 stats->procs_total = 0;
448 while ((e = readdir(dir)) != NULL) {
449 const char *name = e->d_name;
450 char *end;
451 unsigned int pid = strtoul(name, &end, 10);
453 /* not a number */
454 if (pid == 0 && end == name)
455 continue;
457 stats->procs_total++;
460 closedir(dir);
462 return 0;
465 static int adjust_dbm_level(int in_dbm, int dbm_val)
467 if (!in_dbm)
468 return dbm_val;
470 return dbm_val - 0x100;
473 static int stats_wireless(const char *ifname, struct ifstat *stats)
475 int ret;
476 struct iw_statistics ws;
478 ret = wireless_sigqual(ifname, &ws);
479 if (ret != 0) {
480 stats->wifi.bitrate = 0;
481 return -EINVAL;
484 stats->wifi.bitrate = wireless_bitrate(ifname);
486 stats->wifi.signal_level =
487 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
489 stats->wifi.link_qual = ws.qual.qual;
490 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
492 return ret;
495 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
496 #define DIFF(member) do { \
497 if (sizeof(diff->member) != sizeof(new->member) || \
498 sizeof(diff->member) != sizeof(old->member)) \
499 bug(); \
500 bug_on((new->member - old->member) > (new->member)); \
501 DIFF1(member); \
502 } while (0)
504 static void stats_diff(struct ifstat *old, struct ifstat *new,
505 struct ifstat *diff)
507 int cpus, i;
509 DIFF(rx_bytes);
510 DIFF(rx_packets);
511 DIFF(rx_drops);
512 DIFF(rx_errors);
513 DIFF(rx_fifo);
514 DIFF(rx_frame);
515 DIFF(rx_multi);
517 DIFF(tx_bytes);
518 DIFF(tx_packets);
519 DIFF(tx_drops);
520 DIFF(tx_errors);
521 DIFF(tx_fifo);
522 DIFF(tx_colls);
523 DIFF(tx_carrier);
525 DIFF1(wifi.signal_level);
526 DIFF1(wifi.link_qual);
528 DIFF1(cswitch);
530 cpus = get_number_cpus();
532 for (i = 0; i < cpus; ++i) {
533 DIFF(irqs[i]);
534 DIFF(irqs_srx[i]);
535 DIFF(irqs_stx[i]);
537 DIFF1(cpu_user[i]);
538 DIFF1(cpu_nice[i]);
539 DIFF1(cpu_sys[i]);
540 DIFF1(cpu_idle[i]);
541 DIFF1(cpu_iow[i]);
545 static void stats_fetch(const char *ifname, struct ifstat *stats)
547 if (stats_proc_net_dev(ifname, stats) < 0)
548 panic("Cannot fetch device stats!\n");
549 if (stats_proc_softirqs(stats) < 0)
550 panic("Cannot fetch software interrupts!\n");
551 if (stats_proc_memory(stats) < 0)
552 panic("Cannot fetch memory stats!\n");
553 if (stats_proc_system(stats) < 0)
554 panic("Cannot fetch system stats!\n");
555 if (stats_proc_procs(stats) < 0)
556 panic("Cannot fetch process stats!\n");
558 stats_proc_interrupts((char *) ifname, stats);
560 stats_wireless(ifname, stats);
563 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
565 int cpus = get_number_cpus();
567 stats_zero(&stats_old, cpus);
568 stats_zero(&stats_new, cpus);
569 stats_zero(&stats_delta, cpus);
571 stats_fetch(ifname, &stats_old);
572 usleep(ms_interval * 1000);
573 stats_fetch(ifname, &stats_new);
575 stats_diff(&stats_old, &stats_new, &stats_delta);
578 static int cmp_hits(const void *p1, const void *p2)
580 const struct cpu_hit *h1 = p1, *h2 = p2;
583 * We want the hits sorted in descending order, thus reverse the return
584 * values.
586 if (h1->hit == h2->hit)
587 return 0;
588 else if (h1->hit < h2->hit)
589 return 1;
590 else
591 return -1;
594 static int cmp_irqs_rel(const void *p1, const void *p2)
596 const struct cpu_hit *h1 = p1, *h2 = p2;
599 * We want the hits sorted in descending order, thus reverse the return
600 * values.
602 if (h1->irqs_rel == h2->irqs_rel)
603 return 0;
604 else if (h1->irqs_rel < h2->irqs_rel)
605 return 1;
606 else
607 return -1;
610 static int cmp_irqs_abs(const void *p1, const void *p2)
612 const struct cpu_hit *h1 = p1, *h2 = p2;
615 * We want the hits sorted in descending order, thus reverse the return
616 * values.
618 if (h1->irqs_abs == h2->irqs_abs)
619 return 0;
620 else if (h1->irqs_abs < h2->irqs_abs)
621 return 1;
622 else
623 return -1;
626 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
627 int cpus)
629 int i;
631 for (i = 0; i < cpus; ++i) {
632 cpu_hits[i].idx = i;
633 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
634 cpu_hits[i].irqs_rel = rel->irqs[i];
635 cpu_hits[i].irqs_abs = abs->irqs[i];
639 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
640 uint64_t ms_interval, unsigned int top_cpus)
642 size_t len = 0;
643 char buff[64];
644 struct ethtool_drvinfo drvinf;
645 u32 rate = device_bitrate(ifname);
646 int link = ethtool_link(ifname);
647 unsigned int cpus = get_number_cpus();
649 memset(&drvinf, 0, sizeof(drvinf));
650 ethtool_drvinf(ifname, &drvinf);
652 memset(buff, 0, sizeof(buff));
653 if (rate)
654 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
655 if (link >= 0)
656 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
657 link == 0 ? "no" : "yes");
659 mvwprintw(screen, (*voff)++, 2,
660 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u%s/%u"
661 " ",
662 ifname, drvinf.driver, buff, ms_interval, top_cpus,
663 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
666 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
667 int *voff)
669 attron(A_REVERSE);
671 mvwprintw(screen, (*voff)++, 0,
672 " rx: %16.3llf MiB/t "
673 "%10llu pkts/t "
674 "%10llu drops/t "
675 "%10llu errors/t ",
676 ((long double) rel->rx_bytes) / (1LLU << 20),
677 rel->rx_packets, rel->rx_drops, rel->rx_errors);
679 mvwprintw(screen, (*voff)++, 0,
680 " tx: %16.3llf MiB/t "
681 "%10llu pkts/t "
682 "%10llu drops/t "
683 "%10llu errors/t ",
684 ((long double) rel->tx_bytes) / (1LLU << 20),
685 rel->tx_packets, rel->tx_drops, rel->tx_errors);
687 attroff(A_REVERSE);
690 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
691 int *voff)
693 mvwprintw(screen, (*voff)++, 2,
694 "rx: %16.3llf MiB "
695 "%10llu pkts "
696 "%10llu drops "
697 "%10llu errors",
698 ((long double) abs->rx_bytes) / (1LLU << 20),
699 abs->rx_packets, abs->rx_drops, abs->rx_errors);
701 mvwprintw(screen, (*voff)++, 2,
702 "tx: %16.3llf MiB "
703 "%10llu pkts "
704 "%10llu drops "
705 "%10llu errors",
706 ((long double) abs->tx_bytes) / (1LLU << 20),
707 abs->tx_packets, abs->tx_drops, abs->tx_errors);
710 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
711 const struct ifstat *abs, int *voff)
713 mvwprintw(screen, (*voff)++, 2,
714 "sys: %14u cs/t "
715 "%11u procs "
716 "%11u running "
717 "%10u iowait",
718 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
721 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
723 mvwprintw(screen, (*voff)++, 2,
724 "mem: %13uM total "
725 "%9uM used "
726 "%11uM active "
727 "%10uM inactive",
728 abs->mem_total / 1024,
729 (abs->mem_total - abs->mem_free) / 1024,
730 abs->mem_active / 1024,
731 abs->mem_inactive / 1024);
733 mvwprintw(screen, (*voff)++, 2,
734 "swap: %12uM total "
735 "%9uM used "
736 "%11uM cached",
737 abs->swap_total / 1024,
738 (abs->swap_total - abs->swap_free) / 1024,
739 abs->swap_cached / 1024);
742 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
743 int *voff, unsigned int idx, char *tag)
745 int max_padd = padding_from_num(get_number_cpus());
746 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
747 rel->cpu_idle[idx] + rel->cpu_iow[idx];
749 mvwprintw(screen, (*voff)++, 2,
750 "cpu%*d%s:%s %12.1lf%% usr/t "
751 "%9.1lf%% sys/t "
752 "%10.1lf%% idl/t "
753 "%11.1lf%% iow/t ", max_padd, idx,
754 tag, strlen(tag) == 0 ? " " : "",
755 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
756 100.0 * rel->cpu_sys[idx] / all,
757 100.0 * rel->cpu_idle[idx] / all,
758 100.0 * rel->cpu_iow[idx] / all);
761 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
762 int top_cpus, int *voff)
764 int i;
765 int cpus = get_number_cpus();
767 if (top_cpus == 0)
768 return;
770 /* Display top hitter */
771 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
773 /* Make sure we don't display the min. hitter twice */
774 if (top_cpus == cpus)
775 top_cpus--;
777 for (i = 1; i < top_cpus; ++i)
778 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "");
780 /* Display minimum hitter */
781 if (cpus != 1)
782 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
785 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
786 int *voff, unsigned int idx, char *tag)
788 int max_padd = padding_from_num(get_number_cpus());
790 mvwprintw(screen, (*voff)++, 2,
791 "cpu%*d%s:%s %13llu irqs/t "
792 "%15llu sirq rx/t "
793 "%15llu sirq tx/t ", max_padd, idx,
794 tag, strlen(tag) == 0 ? " " : "",
795 rel->irqs[idx],
796 rel->irqs_srx[idx],
797 rel->irqs_stx[idx]);
800 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
801 int top_cpus, int *voff)
803 int i;
804 int cpus = get_number_cpus();
806 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
808 if (top_cpus == cpus)
809 top_cpus--;
811 for (i = 1; i < top_cpus; ++i)
812 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "");
814 if (cpus != 1)
815 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
818 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
819 int *voff, unsigned int idx, char *tag)
821 int max_padd = padding_from_num(get_number_cpus());
823 mvwprintw(screen, (*voff)++, 2,
824 "cpu%*d%s:%s %13llu irqs", max_padd, idx,
825 tag, strlen(tag) == 0 ? " " : "",
826 abs->irqs[idx]);
829 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
830 int top_cpus, int *voff)
832 int i;
833 int cpus = get_number_cpus();
835 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
837 if (top_cpus == cpus)
838 top_cpus--;
840 for (i = 1; i < top_cpus; ++i)
841 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "");
843 if (cpus != 1)
844 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
847 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
848 const struct ifstat *abs, int *voff)
850 if (iswireless(abs)) {
851 mvwprintw(screen, (*voff)++, 2,
852 "linkqual: %7d/%d (%d/t) ",
853 abs->wifi.link_qual,
854 abs->wifi.link_qual_max,
855 rel->wifi.link_qual);
857 mvwprintw(screen, (*voff)++, 2,
858 "signal: %8d dBm (%d dBm/t) ",
859 abs->wifi.signal_level,
860 rel->wifi.signal_level);
864 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
865 const struct ifstat *abs, int *first, uint64_t ms_interval,
866 unsigned int top_cpus)
868 int cpus, top, voff = 1, cvoff = 2;
870 curs_set(0);
872 cpus = get_number_cpus();
873 top = min(cpus, top_cpus);
875 stats_top(rel, abs, cpus);
877 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
879 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
881 voff++;
882 screen_net_dev_rel(screen, rel, &voff);
884 voff++;
885 screen_net_dev_abs(screen, abs, &voff);
887 voff++;
888 screen_sys(screen, rel, abs, &voff);
890 voff++;
891 screen_mem_swap(screen, abs, &voff);
893 voff++;
894 screen_percpu_states(screen, rel, top, &voff);
896 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
898 voff++;
899 screen_percpu_irqs_rel(screen, rel, top, &voff);
901 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
903 voff++;
904 screen_percpu_irqs_abs(screen, abs, top, &voff);
906 voff++;
907 screen_wireless(screen, rel, abs, &voff);
909 if (*first) {
910 mvwprintw(screen, cvoff, 2, "Collecting data ...");
911 *first = 0;
912 } else {
913 mvwprintw(screen, cvoff, 2, " ");
916 wrefresh(screen);
917 refresh();
920 static int screen_main(const char *ifname, uint64_t ms_interval,
921 unsigned int top_cpus)
923 int first = 1, key;
925 stats_screen = screen_init(true);
927 while (!sigint) {
928 key = getch();
929 if (key == 'q' || key == 0x1b || key == KEY_F(10))
930 break;
932 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
933 &first, ms_interval, top_cpus);
935 stats_sample_generic(ifname, ms_interval);
938 screen_end();
940 return 0;
943 static void term_csv(const char *ifname, const struct ifstat *rel,
944 const struct ifstat *abs, uint64_t ms_interval)
946 int cpus, i;
948 printf("%ld ", time(0));
950 printf("%llu ", rel->rx_bytes);
951 printf("%llu ", rel->rx_packets);
952 printf("%llu ", rel->rx_drops);
953 printf("%llu ", rel->rx_errors);
955 printf("%llu ", abs->rx_bytes);
956 printf("%llu ", abs->rx_packets);
957 printf("%llu ", abs->rx_drops);
958 printf("%llu ", abs->rx_errors);
960 printf("%llu ", rel->tx_bytes);
961 printf("%llu ", rel->tx_packets);
962 printf("%llu ", rel->tx_drops);
963 printf("%llu ", rel->tx_errors);
965 printf("%llu ", abs->tx_bytes);
966 printf("%llu ", abs->tx_packets);
967 printf("%llu ", abs->tx_drops);
968 printf("%llu ", abs->tx_errors);
970 printf("%u ", rel->cswitch);
971 printf("%lu ", abs->mem_free);
972 printf("%lu ", abs->mem_total - abs->mem_free);
973 printf("%lu ", abs->mem_total);
974 printf("%lu ", abs->swap_free);
975 printf("%lu ", abs->swap_total - abs->swap_free);
976 printf("%lu ", abs->swap_total);
977 printf("%u ", abs->procs_total);
978 printf("%u ", abs->procs_run);
979 printf("%u ", abs->procs_iow);
981 cpus = get_number_cpus();
983 for (i = 0; i < cpus; ++i) {
984 printf("%lu ", rel->cpu_user[i]);
985 printf("%lu ", rel->cpu_nice[i]);
986 printf("%lu ", rel->cpu_sys[i]);
987 printf("%lu ", rel->cpu_idle[i]);
988 printf("%lu ", rel->cpu_iow[i]);
990 printf("%llu ", rel->irqs[i]);
991 printf("%llu ", abs->irqs[i]);
993 printf("%llu ", rel->irqs_srx[i]);
994 printf("%llu ", abs->irqs_srx[i]);
996 printf("%llu ", rel->irqs_stx[i]);
997 printf("%llu ", abs->irqs_stx[i]);
1000 if (iswireless(abs)) {
1001 printf("%u ", rel->wifi.link_qual);
1002 printf("%u ", abs->wifi.link_qual);
1003 printf("%u ", abs->wifi.link_qual_max);
1005 printf("%d ", rel->wifi.signal_level);
1006 printf("%d ", abs->wifi.signal_level);
1009 puts("");
1010 fflush(stdout);
1013 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1014 uint64_t ms_interval)
1016 int cpus, i, j = 1;
1018 printf("# gnuplot dump (#col:description)\n");
1019 printf("# networking interface: %s\n", ifname);
1020 printf("# sampling interval (t): %lu ms\n", ms_interval);
1021 printf("# %d:unixtime ", j++);
1023 printf("%d:rx-bytes-per-t ", j++);
1024 printf("%d:rx-pkts-per-t ", j++);
1025 printf("%d:rx-drops-per-t ", j++);
1026 printf("%d:rx-errors-per-t ", j++);
1028 printf("%d:rx-bytes ", j++);
1029 printf("%d:rx-pkts ", j++);
1030 printf("%d:rx-drops ", j++);
1031 printf("%d:rx-errors ", j++);
1033 printf("%d:tx-bytes-per-t ", j++);
1034 printf("%d:tx-pkts-per-t ", j++);
1035 printf("%d:tx-drops-per-t ", j++);
1036 printf("%d:tx-errors-per-t ", j++);
1038 printf("%d:tx-bytes ", j++);
1039 printf("%d:tx-pkts ", j++);
1040 printf("%d:tx-drops ", j++);
1041 printf("%d:tx-errors ", j++);
1043 printf("%d:context-switches-per-t ", j++);
1044 printf("%d:mem-free ", j++);
1045 printf("%d:mem-used ", j++);
1046 printf("%d:mem-total ", j++);
1047 printf("%d:swap-free ", j++);
1048 printf("%d:swap-used ", j++);
1049 printf("%d:swap-total ", j++);
1050 printf("%d:procs-total ", j++);
1051 printf("%d:procs-in-run ", j++);
1052 printf("%d:procs-in-iow ", j++);
1054 cpus = get_number_cpus();
1056 for (i = 0; i < cpus; ++i) {
1057 printf("%d:cpu%i-usr-per-t ", j++, i);
1058 printf("%d:cpu%i-nice-per-t ", j++, i);
1059 printf("%d:cpu%i-sys-per-t ", j++, i);
1060 printf("%d:cpu%i-idle-per-t ", j++, i);
1061 printf("%d:cpu%i-iow-per-t ", j++, i);
1063 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1064 printf("%d:cpu%i-net-irqs ", j++, i);
1066 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1067 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1068 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1069 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1072 if (iswireless(abs)) {
1073 printf("%d:wifi-link-qual-per-t ", j++);
1074 printf("%d:wifi-link-qual ", j++);
1075 printf("%d:wifi-link-qual-max ", j++);
1077 printf("%d:wifi-signal-dbm-per-t ", j++);
1078 printf("%d:wifi-signal-dbm ", j++);
1081 puts("");
1082 printf("# data:\n");
1083 fflush(stdout);
1086 static int term_main(const char *ifname, uint64_t ms_interval,
1087 unsigned int top_cpus __maybe_unused)
1089 int first = 1;
1091 do {
1092 stats_sample_generic(ifname, ms_interval);
1094 if (first) {
1095 first = 0;
1096 term_csv_header(ifname, &stats_new, ms_interval);
1099 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1100 } while (stats_loop && !sigint);
1102 return 0;
1105 int main(int argc, char **argv)
1107 short ifflags = 0;
1108 int c, opt_index, ret, cpus, promisc = 0;
1109 unsigned int top_cpus = 10;
1110 uint64_t interval = 1000;
1111 char *ifname = NULL;
1112 int (*func_main)(const char *ifname, uint64_t ms_interval,
1113 unsigned int top_cpus) = screen_main;
1115 setfsuid(getuid());
1116 setfsgid(getgid());
1118 while ((c = getopt_long(argc, argv, short_options, long_options,
1119 &opt_index)) != EOF) {
1120 switch (c) {
1121 case 'h':
1122 help();
1123 break;
1124 case 'v':
1125 version();
1126 break;
1127 case 'd':
1128 ifname = xstrndup(optarg, IFNAMSIZ);
1129 break;
1130 case 't':
1131 interval = strtoul(optarg, NULL, 10);
1132 break;
1133 case 'n':
1134 top_cpus = strtoul(optarg, NULL, 10);
1135 if (top_cpus < 1)
1136 panic("Number of top hitter CPUs must be greater than 0");
1137 break;
1138 case 'l':
1139 stats_loop = 1;
1140 break;
1141 case 'p':
1142 promisc = 1;
1143 break;
1144 case 'c':
1145 func_main = term_main;
1146 break;
1147 case '?':
1148 switch (optopt) {
1149 case 'd':
1150 case 't':
1151 panic("Option -%c requires an argument!\n",
1152 optopt);
1153 default:
1154 if (isprint(optopt))
1155 printf("Unknown option character `0x%X\'!\n", optopt);
1156 die();
1158 default:
1159 break;
1163 if (argc == 1)
1164 help();
1166 if (argc == 2)
1167 ifname = xstrndup(argv[1], IFNAMSIZ);
1168 if (ifname == NULL)
1169 panic("No networking device given!\n");
1171 if (!strncmp("lo", ifname, IFNAMSIZ))
1172 panic("lo is not supported!\n");
1173 if (device_mtu(ifname) == 0)
1174 panic("This is no networking device!\n");
1176 register_signal(SIGINT, signal_handler);
1177 register_signal(SIGHUP, signal_handler);
1179 cpus = get_number_cpus();
1180 top_cpus = min(top_cpus, cpus);
1182 stats_alloc(&stats_old, cpus);
1183 stats_alloc(&stats_new, cpus);
1184 stats_alloc(&stats_delta, cpus);
1186 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1188 if (promisc)
1189 ifflags = enter_promiscuous_mode(ifname);
1190 ret = func_main(ifname, interval, top_cpus);
1191 if (promisc)
1192 leave_promiscuous_mode(ifname, ifflags);
1194 xfree(ifname);
1195 return ret;