build: flowtop: Only build ioops with GeoIP support enabled
[netsniff-ng.git] / ifpps.c
blob77c63a6e2483f89c471265b2994651c877c667c3
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 <inttypes.h>
14 #include <sys/socket.h>
15 #include <sys/fsuid.h>
16 #include <sys/types.h>
17 #include <sys/utsname.h>
18 #include <signal.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <time.h>
22 #include <dirent.h>
24 #include "die.h"
25 #include "dev.h"
26 #include "sig.h"
27 #include "str.h"
28 #include "link.h"
29 #include "xmalloc.h"
30 #include "ioops.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 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 int show_median = 0, show_percentage = 0;
76 static WINDOW *stats_screen = NULL;
77 static struct utsname uts;
79 static const char *short_options = "d:n:t:clmopPWvh";
80 static const struct option long_options[] = {
81 {"dev", required_argument, NULL, 'd'},
82 {"num-cpus", required_argument, NULL, 'n'},
83 {"interval", required_argument, NULL, 't'},
84 {"csv", no_argument, NULL, 'c'},
85 {"loop", no_argument, NULL, 'l'},
86 {"median", no_argument, NULL, 'm'},
87 {"omit-header", no_argument, NULL, 'o'},
88 {"promisc", no_argument, NULL, 'p'},
89 {"percentage", no_argument, NULL, 'P'},
90 {"no-warn", no_argument, NULL, 'W'},
91 {"version", no_argument, NULL, 'v'},
92 {"help", no_argument, NULL, 'h'},
93 {NULL, 0, NULL, 0}
96 static void signal_handler(int number)
98 switch (number) {
99 case SIGINT:
100 case SIGQUIT:
101 case SIGTERM:
102 sigint = 1;
103 break;
104 case SIGHUP:
105 default:
106 break;
110 static inline int iswireless(const struct ifstat *stats)
112 return stats->wifi.bitrate > 0;
115 static void __noreturn help(void)
117 printf("\nifpps %s, top-like kernel networking and system statistics\n",
118 VERSION_STRING);
119 puts("http://www.netsniff-ng.org\n\n"
120 "Usage: ifpps [options] || ifpps <netdev>\n"
121 "Options:\n"
122 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
123 " -n|--num-cpus <num> Number of top hitter CPUs in ncurses mode (def: 5)\n"
124 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
125 " -c|--csv Output to terminal as Gnuplot-ready data\n"
126 " -l|--loop Continuous CSV output\n"
127 " -m|--median Display median values\n"
128 " -o|--omit-header Do not print the CSV header\n"
129 " -p|--promisc Promiscuous mode\n"
130 " -P|--percentage Show percentage of theoretical line rate\n"
131 " -W|--no-warn Suppress warnings\n"
132 " -v|--version Print version and exit\n"
133 " -h|--help Print this help and exit\n\n"
134 "Examples:\n"
135 " ifpps eth0\n"
136 " ifpps -pd eth0\n"
137 " ifpps -lpcd wlan0 > plot.dat\n\n"
138 "Note:\n"
139 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
140 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
141 "Please report bugs to <bugs@netsniff-ng.org>\n"
142 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
143 "Swiss federal institute of technology (ETH Zurich)\n"
144 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
145 "License: GNU GPL version 2.0\n"
146 "This is free software: you are free to change and redistribute it.\n"
147 "There is NO WARRANTY, to the extent permitted by law.\n");
148 die();
151 static void __noreturn version(void)
153 printf("\nifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
154 puts("top-like kernel networking and system statistics\n"
155 "http://www.netsniff-ng.org\n\n"
156 "Please report bugs to <bugs@netsniff-ng.org>\n"
157 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
158 "Swiss federal institute of technology (ETH Zurich)\n"
159 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
160 "License: GNU GPL version 2.0\n"
161 "This is free software: you are free to change and redistribute it.\n"
162 "There is NO WARRANTY, to the extent permitted by law.\n");
163 die();
166 static inline int padding_from_num(int n)
168 int i = 0;
169 do {
170 i++;
171 } while ((n /= 10) > 0);
172 return i;
175 #define STATS_ALLOC1(member) \
176 do { stats->member = xcalloc(cpus, sizeof(*(stats->member))); } while (0)
178 static void stats_alloc(struct ifstat *stats, unsigned int cpus)
180 STATS_ALLOC1(irqs);
181 STATS_ALLOC1(irqs_srx);
182 STATS_ALLOC1(irqs_stx);
184 STATS_ALLOC1(cpu_user);
185 STATS_ALLOC1(cpu_sys);
186 STATS_ALLOC1(cpu_nice);
187 STATS_ALLOC1(cpu_idle);
188 STATS_ALLOC1(cpu_iow);
191 #define STATS_ZERO1(member) \
192 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
194 static void stats_zero(struct ifstat *stats, unsigned int cpus)
196 /* Only clear the non-pointer members */
197 memset(stats, 0, offsetof(struct ifstat, irqs));
199 STATS_ZERO1(irqs);
200 STATS_ZERO1(irqs_srx);
201 STATS_ZERO1(irqs_stx);
203 STATS_ZERO1(cpu_user);
204 STATS_ZERO1(cpu_sys);
205 STATS_ZERO1(cpu_nice);
206 STATS_ZERO1(cpu_idle);
207 STATS_ZERO1(cpu_iow);
210 #define STATS_RELEASE(member) \
211 do { xfree(stats->member); } while (0)
213 static void stats_release(struct ifstat *stats)
215 STATS_RELEASE(irqs);
216 STATS_RELEASE(irqs_srx);
217 STATS_RELEASE(irqs_stx);
219 STATS_RELEASE(cpu_user);
220 STATS_RELEASE(cpu_sys);
221 STATS_RELEASE(cpu_nice);
222 STATS_RELEASE(cpu_idle);
223 STATS_RELEASE(cpu_iow);
226 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
228 int ret = -EINVAL;
229 char buff[256];
230 char *ifname_colon;
231 FILE *fp;
233 fp = fopen("/proc/net/dev", "r");
234 if (!fp)
235 panic("Cannot open /proc/net/dev!\n");
237 ifname_colon = xstrndup(ifname, strlen(ifname) + 2);
238 ifname_colon[strlen(ifname)] = ':';
239 ifname_colon[strlen(ifname) + 1] = '\0';
241 if (fgets(buff, sizeof(buff), fp)) { ; }
242 if (fgets(buff, sizeof(buff), fp)) { ; }
244 memset(buff, 0, sizeof(buff));
246 while (fgets(buff, sizeof(buff), fp) != NULL) {
247 buff[sizeof(buff) - 1] = 0;
249 if (strstr(buff, ifname_colon) == NULL)
250 continue;
252 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
253 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
254 &stats->rx_bytes, &stats->rx_packets,
255 &stats->rx_errors, &stats->rx_drops,
256 &stats->rx_fifo, &stats->rx_frame,
257 &stats->rx_multi, &stats->tx_bytes,
258 &stats->tx_packets, &stats->tx_errors,
259 &stats->tx_drops, &stats->tx_fifo,
260 &stats->tx_colls, &stats->tx_carrier) == 14) {
261 ret = 0;
262 break;
265 memset(buff, 0, sizeof(buff));
268 xfree(ifname_colon);
269 fclose(fp);
270 return ret;
273 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
275 int ret = -EINVAL, try = 0;
276 unsigned int i, cpus;
277 char *ptr, *buff;
278 bool seen = false;
279 size_t buff_len;
280 struct ethtool_drvinfo drvinf;
281 FILE *fp;
283 cpus = get_number_cpus();
284 buff_len = cpus * 128;
285 buff = xmalloc(buff_len);
287 fp = fopen("/proc/interrupts", "r");
288 if (!fp)
289 panic("Cannot open /proc/interrupts!\n");
290 retry:
291 fseek(fp, 0, SEEK_SET);
292 memset(buff, 0, buff_len);
294 while (fgets(buff, buff_len, fp) != NULL) {
295 buff[buff_len - 1] = 0;
297 if (strstr(buff, ifname) == NULL)
298 continue;
300 ptr = strchr(buff, ':');
301 if (!ptr)
302 continue;
303 ptr++;
305 for (i = 0; i < cpus && ptr; ++i) {
306 if (seen)
307 stats->irqs[i] += strtol(ptr, &ptr, 10);
308 else
309 stats->irqs[i] = strtol(ptr, &ptr, 10);
310 if (i == cpus - 1) {
311 ret = 0;
312 seen = true;
316 memset(buff, 0, buff_len);
319 if (ret == -EINVAL && try == 0) {
320 memset(&drvinf, 0, sizeof(drvinf));
321 if (ethtool_drvinf(ifname, &drvinf) < 0)
322 goto done;
324 ifname = drvinf.driver;
325 try++;
327 goto retry;
329 done:
330 xfree(buff);
331 fclose(fp);
332 return ret;
335 static int stats_proc_softirqs(struct ifstat *stats)
337 unsigned int i, cpus;
338 char *ptr, *buff;
339 size_t buff_len;
340 FILE *fp;
341 enum {
342 softirqs_net_rx,
343 softirqs_net_tx,
344 } net_type;
346 fp = fopen("/proc/softirqs", "r");
347 if (!fp)
348 panic("Cannot open /proc/softirqs!\n");
350 cpus = get_number_cpus();
351 buff_len = cpus * 128;
352 buff = xmalloc(buff_len);
354 memset(buff, 0, buff_len);
356 while (fgets(buff, buff_len, fp) != NULL) {
357 buff[buff_len - 1] = 0;
359 if ((ptr = strstr(buff, "NET_TX:")))
360 net_type = softirqs_net_tx;
361 else if ((ptr = strstr(buff, "NET_RX:")))
362 net_type = softirqs_net_rx;
363 else
364 continue;
366 ptr += strlen("NET_xX:");
368 for (i = 0; i < cpus; ++i) {
369 switch (net_type) {
370 case softirqs_net_tx:
371 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
372 break;
373 case softirqs_net_rx:
374 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
375 break;
379 memset(buff, 0, buff_len);
382 xfree(buff);
383 fclose(fp);
384 return 0;
387 static int stats_proc_memory(struct ifstat *stats)
389 char *ptr, buff[256];
390 FILE *fp;
392 fp = fopen("/proc/meminfo", "r");
393 if (!fp)
394 panic("Cannot open /proc/meminfo!\n");
396 memset(buff, 0, sizeof(buff));
398 while (fgets(buff, sizeof(buff), fp) != NULL) {
399 buff[sizeof(buff) - 1] = 0;
401 if ((ptr = strstr(buff, "MemTotal:"))) {
402 ptr += strlen("MemTotal:");
403 stats->mem_total = strtoul(ptr, &ptr, 10);
404 } else if ((ptr = strstr(buff, "MemFree:"))) {
405 ptr += strlen("MemFree:");
406 stats->mem_free = strtoul(ptr, &ptr, 10);
407 } else if ((ptr = strstr(buff, "Active:"))) {
408 ptr += strlen("Active:");
409 stats->mem_active = strtoul(ptr, &ptr, 10);
410 } else if ((ptr = strstr(buff, "Inactive:"))) {
411 ptr += strlen("Inactive:");
412 stats->mem_inactive = strtoul(ptr, &ptr, 10);
413 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
414 ptr += strlen("SwapTotal:");
415 stats->swap_total = strtoul(ptr, &ptr, 10);
416 } else if ((ptr = strstr(buff, "SwapFree:"))) {
417 ptr += strlen("SwapFree:");
418 stats->swap_free = strtoul(ptr, &ptr, 10);
419 } else if ((ptr = strstr(buff, "SwapCached:"))) {
420 ptr += strlen("SwapCached:");
421 stats->swap_cached = strtoul(ptr, &ptr, 10);
424 memset(buff, 0, sizeof(buff));
427 fclose(fp);
428 return 0;
431 static int stats_proc_system(struct ifstat *stats)
433 unsigned int cpu, cpus;
434 char *ptr, buff[256];
435 FILE *fp;
437 fp = fopen("/proc/stat", "r");
438 if (!fp)
439 panic("Cannot open /proc/stat!\n");
441 cpus = get_number_cpus();
443 memset(buff, 0, sizeof(buff));
445 while (fgets(buff, sizeof(buff), fp) != NULL) {
446 buff[sizeof(buff) - 1] = 0;
448 if ((ptr = strstr(buff, "cpu"))) {
449 ptr += strlen("cpu");
450 if (isblank(*ptr))
451 goto next;
453 cpu = strtol(ptr, &ptr, 10);
454 bug_on(cpu > cpus);
456 if (sscanf(ptr, "%"SCNu64"%"SCNu64"%"SCNu64"%"SCNu64"%"SCNu64,
457 &stats->cpu_user[cpu],
458 &stats->cpu_nice[cpu],
459 &stats->cpu_sys[cpu],
460 &stats->cpu_idle[cpu],
461 &stats->cpu_iow[cpu]) != 5)
462 goto next;
463 } else if ((ptr = strstr(buff, "ctxt"))) {
464 ptr += strlen("ctxt");
465 stats->cswitch = strtoul(ptr, &ptr, 10);
466 } else if ((ptr = strstr(buff, "procs_running"))) {
467 ptr += strlen("procs_running");
468 stats->procs_run = strtoul(ptr, &ptr, 10);
469 } else if ((ptr = strstr(buff, "procs_blocked"))) {
470 ptr += strlen("procs_blocked");
471 stats->procs_iow = strtoul(ptr, &ptr, 10);
473 next:
474 memset(buff, 0, sizeof(buff));
477 fclose(fp);
478 return 0;
481 static int stats_proc_procs(struct ifstat *stats)
483 DIR *dir;
484 struct dirent *e;
486 dir = opendir("/proc");
487 if (!dir)
488 panic("Cannot open /proc\n");
490 stats->procs_total = 0;
492 while ((e = readdir(dir)) != NULL) {
493 const char *name = e->d_name;
494 char *end;
495 unsigned int pid = strtoul(name, &end, 10);
497 /* not a number */
498 if (pid == 0 && end == name)
499 continue;
501 stats->procs_total++;
504 closedir(dir);
506 return 0;
509 static int adjust_dbm_level(int in_dbm, int dbm_val)
511 if (!in_dbm)
512 return dbm_val;
514 return dbm_val - 0x100;
517 static int stats_wireless(const char *ifname, struct ifstat *stats)
519 int ret;
520 struct iw_statistics ws;
522 ret = wireless_sigqual(ifname, &ws);
523 if (ret != 0) {
524 stats->wifi.bitrate = 0;
525 return -EINVAL;
528 stats->wifi.bitrate = wireless_bitrate(ifname);
530 stats->wifi.signal_level =
531 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
533 stats->wifi.link_qual = ws.qual.qual;
534 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
536 return ret;
539 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
540 #define DIFF(member) do { \
541 if (sizeof(diff->member) != sizeof(new->member) || \
542 sizeof(diff->member) != sizeof(old->member)) \
543 bug(); \
544 if ((new->member - old->member) > (new->member)) { \
545 diff->member = 0; \
546 } else { \
547 DIFF1(member); \
549 } while (0)
551 static void stats_diff(struct ifstat *old, struct ifstat *new,
552 struct ifstat *diff)
554 unsigned int cpus, i;
556 DIFF(rx_bytes);
557 DIFF(rx_packets);
558 DIFF(rx_drops);
559 DIFF(rx_errors);
560 DIFF(rx_fifo);
561 DIFF(rx_frame);
562 DIFF(rx_multi);
564 DIFF(tx_bytes);
565 DIFF(tx_packets);
566 DIFF(tx_drops);
567 DIFF(tx_errors);
568 DIFF(tx_fifo);
569 DIFF(tx_colls);
570 DIFF(tx_carrier);
572 DIFF1(wifi.signal_level);
573 DIFF1(wifi.link_qual);
575 DIFF1(cswitch);
577 cpus = get_number_cpus();
579 for (i = 0; i < cpus; ++i) {
580 DIFF(irqs[i]);
581 DIFF(irqs_srx[i]);
582 DIFF(irqs_stx[i]);
584 DIFF1(cpu_user[i]);
585 DIFF1(cpu_nice[i]);
586 DIFF1(cpu_sys[i]);
587 DIFF1(cpu_idle[i]);
588 DIFF1(cpu_iow[i]);
592 static void stats_fetch(const char *ifname, struct ifstat *stats)
594 if (stats_proc_net_dev(ifname, stats) < 0)
595 panic("Cannot fetch device stats!\n");
596 if (stats_proc_softirqs(stats) < 0)
597 panic("Cannot fetch software interrupts!\n");
598 if (stats_proc_memory(stats) < 0)
599 panic("Cannot fetch memory stats!\n");
600 if (stats_proc_system(stats) < 0)
601 panic("Cannot fetch system stats!\n");
602 if (stats_proc_procs(stats) < 0)
603 panic("Cannot fetch process stats!\n");
605 stats_proc_interrupts((char *) ifname, stats);
607 stats_wireless(ifname, stats);
610 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
612 unsigned int cpus = get_number_cpus();
614 stats_zero(&stats_old, cpus);
615 stats_zero(&stats_new, cpus);
616 stats_zero(&stats_delta, cpus);
618 stats_fetch(ifname, &stats_old);
619 usleep(ms_interval * 1000);
620 stats_fetch(ifname, &stats_new);
622 stats_diff(&stats_old, &stats_new, &stats_delta);
625 static int cmp_hits(const void *p1, const void *p2)
627 const struct cpu_hit *h1 = p1, *h2 = p2;
630 * We want the hits sorted in descending order, thus reverse the return
631 * values.
633 if (h1->hit == h2->hit)
634 return 0;
635 else if (h1->hit < h2->hit)
636 return 1;
637 else
638 return -1;
641 static int cmp_irqs_rel(const void *p1, const void *p2)
643 const struct cpu_hit *h1 = p1, *h2 = p2;
646 * We want the hits sorted in descending order, thus reverse the return
647 * values.
649 if (h1->irqs_rel == h2->irqs_rel)
650 return 0;
651 else if (h1->irqs_rel < h2->irqs_rel)
652 return 1;
653 else
654 return -1;
657 static int cmp_irqs_abs(const void *p1, const void *p2)
659 const struct cpu_hit *h1 = p1, *h2 = p2;
662 * We want the hits sorted in descending order, thus reverse the return
663 * values.
665 if (h1->irqs_abs == h2->irqs_abs)
666 return 0;
667 else if (h1->irqs_abs < h2->irqs_abs)
668 return 1;
669 else
670 return -1;
673 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
674 unsigned int cpus)
676 unsigned int i;
678 memset(&stats_avg, 0, sizeof(stats_avg));
680 for (i = 0; i < cpus; ++i) {
681 cpu_hits[i].idx = i;
682 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
683 cpu_hits[i].irqs_rel = rel->irqs[i];
684 cpu_hits[i].irqs_abs = abs->irqs[i];
686 stats_avg.cpu_user += rel->cpu_user[i];
687 stats_avg.cpu_sys += rel->cpu_sys[i];
688 stats_avg.cpu_nice += rel->cpu_nice[i];
689 stats_avg.cpu_idle += rel->cpu_idle[i];
690 stats_avg.cpu_iow += rel->cpu_iow[i];
692 stats_avg.irqs_abs += abs->irqs[i];
693 stats_avg.irqs_rel += rel->irqs[i];
694 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
695 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
698 stats_avg.cpu_user /= cpus;
699 stats_avg.cpu_sys /= cpus;
700 stats_avg.cpu_nice /= cpus;
701 stats_avg.cpu_idle /= cpus;
702 stats_avg.cpu_iow /= cpus;
703 stats_avg.irqs_abs /= cpus;
704 stats_avg.irqs_rel /= cpus;
705 stats_avg.irqs_srx_rel /= cpus;
706 stats_avg.irqs_stx_rel /= cpus;
709 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
710 u32 rate, uint64_t ms_interval, unsigned int top_cpus)
712 size_t len = 0;
713 char buff[64], machine[64];
714 struct ethtool_drvinfo drvinf;
715 int link = ethtool_link(ifname);
716 unsigned int cpus = get_number_cpus();
718 memset(&drvinf, 0, sizeof(drvinf));
719 ethtool_drvinf(ifname, &drvinf);
721 memset(buff, 0, sizeof(buff));
722 memset(machine, 0, sizeof(machine));
724 if (rate)
725 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
726 if (link >= 0)
727 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
728 link == 0 ? "no" : "yes");
730 if (!strstr(uts.release, uts.machine))
731 slprintf(machine, sizeof(machine), " %s,", uts.machine);
733 mvwprintw(screen, (*voff)++, 2,
734 "%s,%s %s (%s%s), t=%"PRIu64"ms, cpus=%u%s/%u"
735 " ", uts.release, machine,
736 ifname, drvinf.driver, buff, ms_interval, top_cpus,
737 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
740 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
741 int *voff)
743 attron(A_REVERSE);
745 mvwprintw(screen, (*voff)++, 0,
746 " rx: %16.3llf MiB/t "
747 "%10llu pkts/t "
748 "%10llu drops/t "
749 "%10llu errors/t ",
750 ((long double) rel->rx_bytes) / (1LLU << 20),
751 rel->rx_packets, rel->rx_drops, rel->rx_errors);
753 mvwprintw(screen, (*voff)++, 0,
754 " tx: %16.3llf MiB/t "
755 "%10llu pkts/t "
756 "%10llu drops/t "
757 "%10llu errors/t ",
758 ((long double) rel->tx_bytes) / (1LLU << 20),
759 rel->tx_packets, rel->tx_drops, rel->tx_errors);
761 attroff(A_REVERSE);
764 static void screen_net_dev_percentage(WINDOW *screen, const struct ifstat *rel,
765 int *voff, u32 rate)
767 mvwprintw(screen, (*voff)++, 0,
768 " rx: %15.2llf%% of line rate "
769 " ",
770 rate ? ((((long double) rel->rx_bytes) / 125000) / rate) * 100.0 : 0.0);
772 mvwprintw(screen, (*voff)++, 0,
773 " tx: %15.2llf%% of line rate "
774 " ",
775 rate ? ((((long double) rel->tx_bytes) / 125000) / rate) * 100.0 : 0.0);
778 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
779 int *voff)
781 mvwprintw(screen, (*voff)++, 2,
782 "rx: %16.3llf MiB "
783 "%10llu pkts "
784 "%10llu drops "
785 "%10llu errors",
786 ((long double) abs->rx_bytes) / (1LLU << 20),
787 abs->rx_packets, abs->rx_drops, abs->rx_errors);
789 mvwprintw(screen, (*voff)++, 2,
790 "tx: %16.3llf MiB "
791 "%10llu pkts "
792 "%10llu drops "
793 "%10llu errors",
794 ((long double) abs->tx_bytes) / (1LLU << 20),
795 abs->tx_packets, abs->tx_drops, abs->tx_errors);
798 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
799 const struct ifstat *abs, int *voff)
801 mvwprintw(screen, (*voff)++, 2,
802 "sys: %14"PRIu32" cs/t "
803 "%11"PRIu32" procs "
804 "%11"PRIu32" running "
805 "%10"PRIu32" iowait",
806 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
809 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
811 mvwprintw(screen, (*voff)++, 2,
812 "mem: %13"PRIu64"M total "
813 "%9"PRIu64"M used "
814 "%11"PRIu64"M active "
815 "%10"PRIu64"M inactive",
816 abs->mem_total / 1024,
817 (abs->mem_total - abs->mem_free) / 1024,
818 abs->mem_active / 1024,
819 abs->mem_inactive / 1024);
821 mvwprintw(screen, (*voff)++, 2,
822 "swap: %12"PRIu64"M total "
823 "%9uM used "
824 "%11uM cached",
825 abs->swap_total / 1024,
826 (abs->swap_total - abs->swap_free) / 1024,
827 abs->swap_cached / 1024);
830 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
831 int *voff, unsigned int idx, char *tag)
833 int max_padd = padding_from_num(get_number_cpus());
834 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
835 rel->cpu_idle[idx] + rel->cpu_iow[idx];
837 mvwprintw(screen, (*voff)++, 2,
838 "cpu%*d %s: %11.1lf%% usr/t "
839 "%9.1lf%% sys/t "
840 "%10.1lf%% idl/t "
841 "%11.1lf%% iow/t",
842 max_padd, idx, tag,
843 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
844 100.0 * rel->cpu_sys[idx] / all,
845 100.0 * rel->cpu_idle[idx] / all,
846 100.0 * rel->cpu_iow[idx] / all);
849 #define MEDIAN_EVEN(member) do { \
850 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
851 } while (0)
853 #define MEDIAN_ODD(member) do { \
854 m_##member = rel->member[i]; \
855 } while (0)
857 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
858 const struct avg_stat *avg,
859 unsigned int top_cpus, int *voff)
861 unsigned int i;
862 unsigned int cpus = get_number_cpus();
863 int max_padd = padding_from_num(cpus);
864 uint64_t all;
866 if (top_cpus == 0)
867 return;
869 /* Display top hitter */
870 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
872 /* Make sure we don't display the min. hitter twice */
873 if (top_cpus == cpus)
874 top_cpus--;
876 for (i = 1; i < top_cpus; ++i)
877 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "|");
879 /* Display minimum hitter */
880 if (cpus != 1)
881 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
883 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
884 mvwprintw(screen, (*voff)++, 2,
885 "avg:%*s%14.1lf%% "
886 "%9.1lf%% "
887 "%10.1lf%% "
888 "%11.1lf%%", max_padd, "",
889 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
890 100.0 * avg->cpu_sys / all,
891 100.0 * avg->cpu_idle /all,
892 100.0 * avg->cpu_iow / all);
894 if (show_median) {
895 long double m_cpu_user, m_cpu_nice, m_cpu_sys, m_cpu_idle, m_cpu_iow;
896 long double m_all;
898 i = cpu_hits[cpus / 2].idx;
899 if (cpus % 2 == 0) {
900 /* take the mean of the 2 middle entries */
901 int j = cpu_hits[(cpus / 2) - 1].idx;
903 MEDIAN_EVEN(cpu_user);
904 MEDIAN_EVEN(cpu_nice);
905 MEDIAN_EVEN(cpu_sys);
906 MEDIAN_EVEN(cpu_idle);
907 MEDIAN_EVEN(cpu_iow);
908 } else {
909 /* take the middle entry as is */
910 MEDIAN_ODD(cpu_user);
911 MEDIAN_ODD(cpu_nice);
912 MEDIAN_ODD(cpu_sys);
913 MEDIAN_ODD(cpu_idle);
914 MEDIAN_ODD(cpu_iow);
917 m_all = m_cpu_user + m_cpu_sys + m_cpu_nice + m_cpu_idle + m_cpu_iow;
918 mvwprintw(screen, (*voff)++, 2,
919 "med:%*s%14.1Lf%% "
920 "%9.1Lf%% "
921 "%10.1Lf%% "
922 "%11.1Lf%%", max_padd, "",
923 100.0 * (m_cpu_user + m_cpu_nice) / m_all,
924 100.0 * m_cpu_sys / m_all,
925 100.0 * m_cpu_idle /m_all,
926 100.0 * m_cpu_iow / m_all);
930 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
931 int *voff, unsigned int idx, char *tag)
933 int max_padd = padding_from_num(get_number_cpus());
935 mvwprintw(screen, (*voff)++, 2,
936 "cpu%*d %s: %12llu irqs/t "
937 "%17llu sirq rx/t "
938 "%17llu sirq tx/t",
939 max_padd, idx, tag,
940 rel->irqs[idx],
941 rel->irqs_srx[idx],
942 rel->irqs_stx[idx]);
945 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
946 const struct avg_stat *avg,
947 unsigned int top_cpus, int *voff)
949 unsigned int i;
950 unsigned int cpus = get_number_cpus();
951 int max_padd = padding_from_num(cpus);
953 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
955 if (top_cpus == cpus)
956 top_cpus--;
958 for (i = 1; i < top_cpus; ++i)
959 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
961 if (cpus != 1)
962 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
964 mvwprintw(screen, (*voff)++, 2,
965 "avg:%*s%17.1Lf "
966 "%17.1Lf "
967 "%17.1Lf", max_padd, "",
968 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
970 if (show_median) {
971 long double m_irqs, m_irqs_srx, m_irqs_stx;
973 i = cpu_hits[cpus / 2].idx;
974 if (cpus % 2 == 0) {
975 /* take the mean of the 2 middle entries */
976 int j = cpu_hits[(cpus / 2) - 1].idx;
978 MEDIAN_EVEN(irqs);
979 MEDIAN_EVEN(irqs_srx);
980 MEDIAN_EVEN(irqs_stx);
981 } else {
982 /* take the middle entry as is */
983 MEDIAN_ODD(irqs);
984 MEDIAN_ODD(irqs_srx);
985 MEDIAN_ODD(irqs_stx);
988 mvwprintw(screen, (*voff)++, 2,
989 "med:%*s%17.1Lf "
990 "%17.1Lf "
991 "%17.1Lf", max_padd, "",
992 m_irqs, m_irqs_srx, m_irqs_stx);
996 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
997 int *voff, unsigned int idx, char *tag)
999 int max_padd = padding_from_num(get_number_cpus());
1001 mvwprintw(screen, (*voff)++, 2,
1002 "cpu%*d %s: %12llu irqs",
1003 max_padd, idx, tag, abs->irqs[idx]);
1006 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
1007 const struct avg_stat *avg,
1008 unsigned int top_cpus, int *voff)
1010 unsigned int i;
1011 unsigned int cpus = get_number_cpus();
1012 int max_padd = padding_from_num(cpus);
1014 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
1016 if (top_cpus == cpus)
1017 top_cpus--;
1019 for (i = 1; i < top_cpus; ++i)
1020 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
1022 if (cpus != 1)
1023 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
1025 mvwprintw(screen, (*voff)++, 2,
1026 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
1028 if (show_median) {
1029 long double m_irqs;
1031 i = cpu_hits[cpus / 2].idx;
1032 if (cpus % 2 == 0) {
1033 /* take the mean of the 2 middle entries */
1034 int j = cpu_hits[(cpus / 2) - 1].idx;
1036 m_irqs = (abs->irqs[i] + abs->irqs[j]) / 2;
1037 } else {
1038 /* take the middle entry as is */
1039 m_irqs = abs->irqs[i];
1042 mvwprintw(screen, (*voff)++, 2,
1043 "med:%*s%17.1Lf", max_padd, "", m_irqs);
1047 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
1048 const struct ifstat *abs, int *voff)
1050 if (iswireless(abs)) {
1051 mvwprintw(screen, (*voff)++, 2,
1052 "linkqual: %7d/%d (%d/t) ",
1053 abs->wifi.link_qual,
1054 abs->wifi.link_qual_max,
1055 rel->wifi.link_qual);
1057 mvwprintw(screen, (*voff)++, 2,
1058 "signal: %8d dBm (%d dBm/t) ",
1059 abs->wifi.signal_level,
1060 rel->wifi.signal_level);
1064 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
1065 const struct ifstat *abs, const struct avg_stat *avg,
1066 int *first, uint64_t ms_interval, unsigned int top_cpus,
1067 bool need_info)
1069 unsigned int cpus, top;
1070 int voff = 1, cvoff = 2;
1071 u32 rate = device_bitrate(ifname);
1073 curs_set(0);
1075 cpus = get_number_cpus();
1076 top = min(cpus, top_cpus);
1078 stats_top(rel, abs, cpus);
1080 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
1082 screen_header(screen, ifname, &voff, rate, ms_interval, top_cpus);
1084 voff++;
1085 screen_net_dev_rel(screen, rel, &voff);
1087 if (show_percentage) {
1088 voff++;
1089 screen_net_dev_percentage(screen, rel, &voff, rate);
1092 voff++;
1093 screen_net_dev_abs(screen, abs, &voff);
1095 voff++;
1096 screen_sys(screen, rel, abs, &voff);
1098 voff++;
1099 screen_mem_swap(screen, abs, &voff);
1101 voff++;
1102 screen_percpu_states(screen, rel, avg, top, &voff);
1104 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
1106 voff++;
1107 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
1109 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
1111 voff++;
1112 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
1114 voff++;
1115 screen_wireless(screen, rel, abs, &voff);
1117 if (*first) {
1118 mvwprintw(screen, cvoff, 2, "Collecting data ...");
1119 *first = 0;
1120 } else {
1121 if (need_info)
1122 mvwprintw(screen, cvoff, 2, "(consider to increase "
1123 "your sampling interval, e.g. -t %d)",
1124 rate > SPEED_1000 ? 10000 : 1000);
1125 else
1126 mvwprintw(screen, cvoff, 2, " "
1127 " ");
1130 wrefresh(screen);
1131 refresh();
1134 static int screen_main(const char *ifname, uint64_t ms_interval,
1135 unsigned int top_cpus, bool suppress_warnings,
1136 bool omit_header __maybe_unused)
1138 int first = 1, key;
1139 u32 rate = device_bitrate(ifname);
1140 bool need_info = false;
1142 stats_screen = screen_init(true);
1144 if (((rate > SPEED_1000 && ms_interval <= 1000) ||
1145 (rate = SPEED_1000 && ms_interval < 1000)) &&
1146 !suppress_warnings)
1147 need_info = true;
1149 while (!sigint) {
1150 key = getch();
1151 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1152 break;
1154 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1155 &first, ms_interval, top_cpus, need_info);
1157 stats_sample_generic(ifname, ms_interval);
1160 screen_end();
1162 return 0;
1165 static void term_csv(const struct ifstat *rel, const struct ifstat *abs)
1167 unsigned int cpus, i;
1169 printf("%ld ", time(NULL));
1171 printf("%llu ", rel->rx_bytes);
1172 printf("%llu ", rel->rx_packets);
1173 printf("%llu ", rel->rx_drops);
1174 printf("%llu ", rel->rx_errors);
1176 printf("%llu ", abs->rx_bytes);
1177 printf("%llu ", abs->rx_packets);
1178 printf("%llu ", abs->rx_drops);
1179 printf("%llu ", abs->rx_errors);
1181 printf("%llu ", rel->tx_bytes);
1182 printf("%llu ", rel->tx_packets);
1183 printf("%llu ", rel->tx_drops);
1184 printf("%llu ", rel->tx_errors);
1186 printf("%llu ", abs->tx_bytes);
1187 printf("%llu ", abs->tx_packets);
1188 printf("%llu ", abs->tx_drops);
1189 printf("%llu ", abs->tx_errors);
1191 printf("%"PRIu32" ", rel->cswitch);
1192 printf("%"PRIu64" ", abs->mem_free);
1193 printf("%"PRIu64" ", abs->mem_total - abs->mem_free);
1194 printf("%"PRIu64" ", abs->mem_total);
1195 printf("%"PRIu64" ", abs->swap_free);
1196 printf("%"PRIu64" ", abs->swap_total - abs->swap_free);
1197 printf("%"PRIu64" ", abs->swap_total);
1198 printf("%"PRIu32" ", abs->procs_total);
1199 printf("%"PRIu32" ", abs->procs_run);
1200 printf("%"PRIu32" ", abs->procs_iow);
1202 cpus = get_number_cpus();
1204 for (i = 0; i < cpus; ++i) {
1205 printf("%"PRIu64" ", rel->cpu_user[i]);
1206 printf("%"PRIu64" ", rel->cpu_nice[i]);
1207 printf("%"PRIu64" ", rel->cpu_sys[i]);
1208 printf("%"PRIu64" ", rel->cpu_idle[i]);
1209 printf("%"PRIu64" ", rel->cpu_iow[i]);
1211 printf("%llu ", rel->irqs[i]);
1212 printf("%llu ", abs->irqs[i]);
1214 printf("%llu ", rel->irqs_srx[i]);
1215 printf("%llu ", abs->irqs_srx[i]);
1217 printf("%llu ", rel->irqs_stx[i]);
1218 printf("%llu ", abs->irqs_stx[i]);
1221 if (iswireless(abs)) {
1222 printf("%"PRIu16" ", rel->wifi.link_qual);
1223 printf("%"PRIu16" ", abs->wifi.link_qual);
1224 printf("%"PRIu16" ", abs->wifi.link_qual_max);
1226 printf("%d ", rel->wifi.signal_level);
1227 printf("%d ", abs->wifi.signal_level);
1230 puts("");
1231 fflush(stdout);
1234 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1235 uint64_t ms_interval)
1237 unsigned int cpus, i, j = 1;
1239 printf("# gnuplot dump (#col:description)\n");
1240 printf("# networking interface: %s\n", ifname);
1241 printf("# sampling interval (t): %"PRIu64" ms\n", ms_interval);
1242 printf("# %d:unixtime ", j++);
1244 printf("%d:rx-bytes-per-t ", j++);
1245 printf("%d:rx-pkts-per-t ", j++);
1246 printf("%d:rx-drops-per-t ", j++);
1247 printf("%d:rx-errors-per-t ", j++);
1249 printf("%d:rx-bytes ", j++);
1250 printf("%d:rx-pkts ", j++);
1251 printf("%d:rx-drops ", j++);
1252 printf("%d:rx-errors ", j++);
1254 printf("%d:tx-bytes-per-t ", j++);
1255 printf("%d:tx-pkts-per-t ", j++);
1256 printf("%d:tx-drops-per-t ", j++);
1257 printf("%d:tx-errors-per-t ", j++);
1259 printf("%d:tx-bytes ", j++);
1260 printf("%d:tx-pkts ", j++);
1261 printf("%d:tx-drops ", j++);
1262 printf("%d:tx-errors ", j++);
1264 printf("%d:context-switches-per-t ", j++);
1265 printf("%d:mem-free ", j++);
1266 printf("%d:mem-used ", j++);
1267 printf("%d:mem-total ", j++);
1268 printf("%d:swap-free ", j++);
1269 printf("%d:swap-used ", j++);
1270 printf("%d:swap-total ", j++);
1271 printf("%d:procs-total ", j++);
1272 printf("%d:procs-in-run ", j++);
1273 printf("%d:procs-in-iow ", j++);
1275 cpus = get_number_cpus();
1277 for (i = 0; i < cpus; ++i) {
1278 printf("%d:cpu%i-usr-per-t ", j++, i);
1279 printf("%d:cpu%i-nice-per-t ", j++, i);
1280 printf("%d:cpu%i-sys-per-t ", j++, i);
1281 printf("%d:cpu%i-idle-per-t ", j++, i);
1282 printf("%d:cpu%i-iow-per-t ", j++, i);
1284 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1285 printf("%d:cpu%i-net-irqs ", j++, i);
1287 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1288 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1289 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1290 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1293 if (iswireless(abs)) {
1294 printf("%d:wifi-link-qual-per-t ", j++);
1295 printf("%d:wifi-link-qual ", j++);
1296 printf("%d:wifi-link-qual-max ", j++);
1298 printf("%d:wifi-signal-dbm-per-t ", j++);
1299 printf("%d:wifi-signal-dbm ", j++);
1302 puts("");
1303 printf("# data:\n");
1304 fflush(stdout);
1307 static int term_main(const char *ifname, uint64_t ms_interval,
1308 unsigned int top_cpus __maybe_unused,
1309 bool suppress_warnings __maybe_unused,
1310 bool omit_header)
1312 do {
1313 stats_sample_generic(ifname, ms_interval);
1315 if (!omit_header) {
1316 omit_header = true;
1317 term_csv_header(ifname, &stats_new, ms_interval);
1320 term_csv(&stats_delta, &stats_new);
1321 } while (stats_loop && !sigint);
1323 return 0;
1326 int main(int argc, char **argv)
1328 short ifflags = 0;
1329 int c, opt_index, ret, promisc = 0;
1330 unsigned int cpus, top_cpus = 5;
1331 uint64_t interval = 1000;
1332 char *ifname = NULL;
1333 bool suppress_warnings = false;
1334 bool omit_header = false;
1335 int (*func_main)(const char *ifname, uint64_t ms_interval,
1336 unsigned int top_cpus, bool suppress_warnings,
1337 bool omit_header);
1339 func_main = screen_main;
1341 setfsuid(getuid());
1342 setfsgid(getgid());
1344 while ((c = getopt_long(argc, argv, short_options, long_options,
1345 &opt_index)) != EOF) {
1346 switch (c) {
1347 case 'h':
1348 help();
1349 break;
1350 case 'v':
1351 version();
1352 break;
1353 case 'W':
1354 suppress_warnings = true;
1355 break;
1356 case 'd':
1357 ifname = xstrndup(optarg, IFNAMSIZ);
1358 break;
1359 case 't':
1360 interval = strtoul(optarg, NULL, 10);
1361 break;
1362 case 'n':
1363 top_cpus = strtoul(optarg, NULL, 10);
1364 if (top_cpus < 1)
1365 panic("Number of top hitter CPUs must be greater than 0");
1366 break;
1367 case 'l':
1368 stats_loop = 1;
1369 break;
1370 case 'p':
1371 promisc = 1;
1372 break;
1373 case 'P':
1374 show_percentage = 1;
1375 break;
1376 case 'm':
1377 show_median = 1;
1378 break;
1379 case 'c':
1380 func_main = term_main;
1381 break;
1382 case 'o':
1383 omit_header = true;
1384 break;
1385 case '?':
1386 switch (optopt) {
1387 case 'd':
1388 case 't':
1389 panic("Option -%c requires an argument!\n",
1390 optopt);
1391 default:
1392 if (isprint(optopt))
1393 printf("Unknown option character `0x%X\'!\n", optopt);
1394 die();
1396 default:
1397 break;
1401 if (argc == 1)
1402 help();
1404 if (argc == 2)
1405 ifname = xstrndup(argv[1], IFNAMSIZ);
1406 if (ifname == NULL)
1407 panic("No networking device given!\n");
1409 if (!strncmp("lo", ifname, IFNAMSIZ))
1410 panic("lo is not supported!\n");
1411 if (device_mtu(ifname) == 0)
1412 panic("This is no networking device!\n");
1414 register_signal(SIGINT, signal_handler);
1415 register_signal(SIGQUIT, signal_handler);
1416 register_signal(SIGSTOP, signal_handler);
1417 register_signal(SIGHUP, signal_handler);
1419 cpus = get_number_cpus();
1420 top_cpus = min(top_cpus, cpus);
1421 if (uname(&uts) < 0)
1422 panic("Cannot execute uname!\n");
1424 stats_alloc(&stats_old, cpus);
1425 stats_alloc(&stats_new, cpus);
1426 stats_alloc(&stats_delta, cpus);
1428 cpu_hits = xcalloc(cpus, sizeof(*cpu_hits));
1430 if (promisc)
1431 ifflags = device_enter_promiscuous_mode(ifname);
1432 ret = func_main(ifname, interval, top_cpus, suppress_warnings, omit_header);
1433 if (promisc)
1434 device_leave_promiscuous_mode(ifname, ifflags);
1436 stats_release(&stats_old);
1437 stats_release(&stats_new);
1438 stats_release(&stats_delta);
1440 xfree(ifname);
1441 return ret;