netsniff-ng: Use while() instead of empty for
[netsniff-ng.git] / ifpps.c
blob0d13b089faaaff4538245cf292c18bc648587e3a
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009 - 2013 Daniel Borkmann.
4 * Copyright 2013 Tobias Klauser
5 * Subject to the GPL, version 2.
6 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <curses.h>
11 #include <getopt.h>
12 #include <ctype.h>
13 #include <sys/socket.h>
14 #include <sys/fsuid.h>
15 #include <sys/types.h>
16 #include <sys/utsname.h>
17 #include <signal.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <time.h>
21 #include <dirent.h>
23 #include "die.h"
24 #include "dev.h"
25 #include "sig.h"
26 #include "str.h"
27 #include "link.h"
28 #include "xmalloc.h"
29 #include "ioops.h"
30 #include "cpus.h"
31 #include "config.h"
32 #include "built_in.h"
33 #include "screen.h"
35 struct wifi_stat {
36 uint32_t bitrate;
37 int16_t link_qual, link_qual_max;
38 int signal_level /*, noise_level*/;
41 struct ifstat {
42 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
43 long long unsigned int rx_fifo, rx_frame, rx_multi;
44 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
45 long long unsigned int tx_fifo, tx_colls, tx_carrier;
46 uint64_t mem_free, mem_total, mem_active, mem_inactive;
47 uint64_t swap_total, swap_free, swap_cached;
48 uint32_t procs_total, procs_run, procs_iow, cswitch;
49 struct wifi_stat wifi;
51 * Pointer members need to be last in order for stats_zero() to work
52 * properly.
54 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
55 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
58 struct cpu_hit {
59 unsigned int idx;
60 uint64_t hit;
61 long long unsigned int irqs_rel, irqs_abs;
64 struct avg_stat {
65 uint64_t cpu_user, cpu_sys, cpu_nice, cpu_idle, cpu_iow;
66 long double irqs_abs, irqs_rel, irqs_srx_rel, irqs_stx_rel;
69 static volatile sig_atomic_t sigint = 0;
70 static struct ifstat stats_old, stats_new, stats_delta;
71 static struct cpu_hit *cpu_hits;
72 static struct avg_stat stats_avg;
73 static int stats_loop = 0;
74 static int show_median = 0, show_percentage = 0;
75 static WINDOW *stats_screen = NULL;
76 static struct utsname uts;
78 static const char *short_options = "d:n:t:clmopPWvh";
79 static const struct option long_options[] = {
80 {"dev", required_argument, NULL, 'd'},
81 {"num-cpus", required_argument, NULL, 'n'},
82 {"interval", required_argument, NULL, 't'},
83 {"csv", no_argument, NULL, 'c'},
84 {"loop", no_argument, NULL, 'l'},
85 {"median", no_argument, NULL, 'm'},
86 {"omit-header", no_argument, NULL, 'o'},
87 {"promisc", no_argument, NULL, 'p'},
88 {"percentage", no_argument, NULL, 'P'},
89 {"no-warn", no_argument, NULL, 'W'},
90 {"version", no_argument, NULL, 'v'},
91 {"help", no_argument, NULL, 'h'},
92 {NULL, 0, NULL, 0}
95 static void signal_handler(int number)
97 switch (number) {
98 case SIGINT:
99 case SIGQUIT:
100 case SIGTERM:
101 sigint = 1;
102 break;
103 case SIGHUP:
104 default:
105 break;
109 static inline int iswireless(const struct ifstat *stats)
111 return stats->wifi.bitrate > 0;
114 static void __noreturn help(void)
116 printf("\nifpps %s, top-like kernel networking and system statistics\n",
117 VERSION_STRING);
118 puts("http://www.netsniff-ng.org\n\n"
119 "Usage: ifpps [options] || ifpps <netdev>\n"
120 "Options:\n"
121 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
122 " -n|--num-cpus <num> Number of top hitter CPUs in ncurses mode (def: 5)\n"
123 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
124 " -c|--csv Output to terminal as Gnuplot-ready data\n"
125 " -l|--loop Continuous CSV output\n"
126 " -m|--median Display median values\n"
127 " -o|--omit-header Do not print the CSV header\n"
128 " -p|--promisc Promiscuous mode\n"
129 " -P|--percentage Show percentage of theoretical line rate\n"
130 " -W|--no-warn Suppress warnings\n"
131 " -v|--version Print version and exit\n"
132 " -h|--help Print this help and exit\n\n"
133 "Examples:\n"
134 " ifpps eth0\n"
135 " ifpps -pd eth0\n"
136 " ifpps -lpcd wlan0 > plot.dat\n\n"
137 "Note:\n"
138 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
139 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
140 "Please report bugs to <bugs@netsniff-ng.org>\n"
141 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
142 "Swiss federal institute of technology (ETH Zurich)\n"
143 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
144 "License: GNU GPL version 2.0\n"
145 "This is free software: you are free to change and redistribute it.\n"
146 "There is NO WARRANTY, to the extent permitted by law.\n");
147 die();
150 static void __noreturn version(void)
152 printf("\nifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
153 puts("top-like kernel networking and system statistics\n"
154 "http://www.netsniff-ng.org\n\n"
155 "Please report bugs to <bugs@netsniff-ng.org>\n"
156 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
157 "Swiss federal institute of technology (ETH Zurich)\n"
158 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
159 "License: GNU GPL version 2.0\n"
160 "This is free software: you are free to change and redistribute it.\n"
161 "There is NO WARRANTY, to the extent permitted by law.\n");
162 die();
165 static inline int padding_from_num(int n)
167 int i = 0;
168 do {
169 i++;
170 } while ((n /= 10) > 0);
171 return i;
174 #define STATS_ALLOC1(member) \
175 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
177 static void stats_alloc(struct ifstat *stats, unsigned int cpus)
179 STATS_ALLOC1(irqs);
180 STATS_ALLOC1(irqs_srx);
181 STATS_ALLOC1(irqs_stx);
183 STATS_ALLOC1(cpu_user);
184 STATS_ALLOC1(cpu_sys);
185 STATS_ALLOC1(cpu_nice);
186 STATS_ALLOC1(cpu_idle);
187 STATS_ALLOC1(cpu_iow);
190 #define STATS_ZERO1(member) \
191 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
193 static void stats_zero(struct ifstat *stats, unsigned int cpus)
195 /* Only clear the non-pointer members */
196 memset(stats, 0, offsetof(struct ifstat, irqs));
198 STATS_ZERO1(irqs);
199 STATS_ZERO1(irqs_srx);
200 STATS_ZERO1(irqs_stx);
202 STATS_ZERO1(cpu_user);
203 STATS_ZERO1(cpu_sys);
204 STATS_ZERO1(cpu_nice);
205 STATS_ZERO1(cpu_idle);
206 STATS_ZERO1(cpu_iow);
209 #define STATS_RELEASE(member) \
210 do { xfree(stats->member); } while (0)
212 static void stats_release(struct ifstat *stats)
214 STATS_RELEASE(irqs);
215 STATS_RELEASE(irqs_srx);
216 STATS_RELEASE(irqs_stx);
218 STATS_RELEASE(cpu_user);
219 STATS_RELEASE(cpu_sys);
220 STATS_RELEASE(cpu_nice);
221 STATS_RELEASE(cpu_idle);
222 STATS_RELEASE(cpu_iow);
225 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
227 int ret = -EINVAL;
228 char buff[256];
229 char *ifname_colon;
230 FILE *fp;
232 fp = fopen("/proc/net/dev", "r");
233 if (!fp)
234 panic("Cannot open /proc/net/dev!\n");
236 ifname_colon = xstrndup(ifname, strlen(ifname) + 2);
237 ifname_colon[strlen(ifname)] = ':';
238 ifname_colon[strlen(ifname) + 1] = '\0';
240 if (fgets(buff, sizeof(buff), fp)) { ; }
241 if (fgets(buff, sizeof(buff), fp)) { ; }
243 memset(buff, 0, sizeof(buff));
245 while (fgets(buff, sizeof(buff), fp) != NULL) {
246 buff[sizeof(buff) - 1] = 0;
248 if (strstr(buff, ifname_colon) == NULL)
249 continue;
251 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
252 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
253 &stats->rx_bytes, &stats->rx_packets,
254 &stats->rx_errors, &stats->rx_drops,
255 &stats->rx_fifo, &stats->rx_frame,
256 &stats->rx_multi, &stats->tx_bytes,
257 &stats->tx_packets, &stats->tx_errors,
258 &stats->tx_drops, &stats->tx_fifo,
259 &stats->tx_colls, &stats->tx_carrier) == 14) {
260 ret = 0;
261 break;
264 memset(buff, 0, sizeof(buff));
267 xfree(ifname_colon);
268 fclose(fp);
269 return ret;
272 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
274 int ret = -EINVAL, try = 0;
275 unsigned int i, cpus;
276 char *ptr, *buff;
277 bool seen = false;
278 size_t buff_len;
279 struct ethtool_drvinfo drvinf;
280 FILE *fp;
282 cpus = get_number_cpus();
283 buff_len = cpus * 128;
284 buff = xmalloc(buff_len);
286 fp = fopen("/proc/interrupts", "r");
287 if (!fp)
288 panic("Cannot open /proc/interrupts!\n");
289 retry:
290 fseek(fp, 0, SEEK_SET);
291 memset(buff, 0, buff_len);
293 while (fgets(buff, buff_len, fp) != NULL) {
294 buff[buff_len - 1] = 0;
296 if (strstr(buff, ifname) == NULL)
297 continue;
299 ptr = strchr(buff, ':');
300 if (!ptr)
301 continue;
302 ptr++;
304 for (i = 0; i < cpus && ptr; ++i) {
305 if (seen)
306 stats->irqs[i] += strtol(ptr, &ptr, 10);
307 else
308 stats->irqs[i] = strtol(ptr, &ptr, 10);
309 if (i == cpus - 1) {
310 ret = 0;
311 seen = true;
315 memset(buff, 0, buff_len);
318 if (ret == -EINVAL && try == 0) {
319 memset(&drvinf, 0, sizeof(drvinf));
320 if (ethtool_drvinf(ifname, &drvinf) < 0)
321 goto done;
323 ifname = drvinf.driver;
324 try++;
326 goto retry;
328 done:
329 xfree(buff);
330 fclose(fp);
331 return ret;
334 static int stats_proc_softirqs(struct ifstat *stats)
336 unsigned int i, cpus;
337 char *ptr, *buff;
338 size_t buff_len;
339 FILE *fp;
340 enum {
341 softirqs_net_rx,
342 softirqs_net_tx,
343 } net_type;
345 fp = fopen("/proc/softirqs", "r");
346 if (!fp)
347 panic("Cannot open /proc/softirqs!\n");
349 cpus = get_number_cpus();
350 buff_len = cpus * 128;
351 buff = xmalloc(buff_len);
353 memset(buff, 0, buff_len);
355 while (fgets(buff, buff_len, fp) != NULL) {
356 buff[buff_len - 1] = 0;
358 if ((ptr = strstr(buff, "NET_TX:")))
359 net_type = softirqs_net_tx;
360 else if ((ptr = strstr(buff, "NET_RX:")))
361 net_type = softirqs_net_rx;
362 else
363 continue;
365 ptr += strlen("NET_xX:");
367 for (i = 0; i < cpus; ++i) {
368 switch (net_type) {
369 case softirqs_net_tx:
370 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
371 break;
372 case softirqs_net_rx:
373 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
374 break;
378 memset(buff, 0, buff_len);
381 xfree(buff);
382 fclose(fp);
383 return 0;
386 static int stats_proc_memory(struct ifstat *stats)
388 char *ptr, buff[256];
389 FILE *fp;
391 fp = fopen("/proc/meminfo", "r");
392 if (!fp)
393 panic("Cannot open /proc/meminfo!\n");
395 memset(buff, 0, sizeof(buff));
397 while (fgets(buff, sizeof(buff), fp) != NULL) {
398 buff[sizeof(buff) - 1] = 0;
400 if ((ptr = strstr(buff, "MemTotal:"))) {
401 ptr += strlen("MemTotal:");
402 stats->mem_total = strtoul(ptr, &ptr, 10);
403 } else if ((ptr = strstr(buff, "MemFree:"))) {
404 ptr += strlen("MemFree:");
405 stats->mem_free = strtoul(ptr, &ptr, 10);
406 } else if ((ptr = strstr(buff, "Active:"))) {
407 ptr += strlen("Active:");
408 stats->mem_active = strtoul(ptr, &ptr, 10);
409 } else if ((ptr = strstr(buff, "Inactive:"))) {
410 ptr += strlen("Inactive:");
411 stats->mem_inactive = strtoul(ptr, &ptr, 10);
412 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
413 ptr += strlen("SwapTotal:");
414 stats->swap_total = strtoul(ptr, &ptr, 10);
415 } else if ((ptr = strstr(buff, "SwapFree:"))) {
416 ptr += strlen("SwapFree:");
417 stats->swap_free = strtoul(ptr, &ptr, 10);
418 } else if ((ptr = strstr(buff, "SwapCached:"))) {
419 ptr += strlen("SwapCached:");
420 stats->swap_cached = strtoul(ptr, &ptr, 10);
423 memset(buff, 0, sizeof(buff));
426 fclose(fp);
427 return 0;
430 static int stats_proc_system(struct ifstat *stats)
432 unsigned int cpu, cpus;
433 char *ptr, buff[256];
434 FILE *fp;
436 fp = fopen("/proc/stat", "r");
437 if (!fp)
438 panic("Cannot open /proc/stat!\n");
440 cpus = get_number_cpus();
442 memset(buff, 0, sizeof(buff));
444 while (fgets(buff, sizeof(buff), fp) != NULL) {
445 buff[sizeof(buff) - 1] = 0;
447 if ((ptr = strstr(buff, "cpu"))) {
448 ptr += strlen("cpu");
449 if (isblank(*ptr))
450 goto next;
452 cpu = strtol(ptr, &ptr, 10);
453 bug_on(cpu > cpus);
455 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
456 &stats->cpu_user[cpu],
457 &stats->cpu_nice[cpu],
458 &stats->cpu_sys[cpu],
459 &stats->cpu_idle[cpu],
460 &stats->cpu_iow[cpu]) != 5)
461 goto next;
462 } else if ((ptr = strstr(buff, "ctxt"))) {
463 ptr += strlen("ctxt");
464 stats->cswitch = strtoul(ptr, &ptr, 10);
465 } else if ((ptr = strstr(buff, "procs_running"))) {
466 ptr += strlen("procs_running");
467 stats->procs_run = strtoul(ptr, &ptr, 10);
468 } else if ((ptr = strstr(buff, "procs_blocked"))) {
469 ptr += strlen("procs_blocked");
470 stats->procs_iow = strtoul(ptr, &ptr, 10);
472 next:
473 memset(buff, 0, sizeof(buff));
476 fclose(fp);
477 return 0;
480 static int stats_proc_procs(struct ifstat *stats)
482 DIR *dir;
483 struct dirent *e;
485 dir = opendir("/proc");
486 if (!dir)
487 panic("Cannot open /proc\n");
489 stats->procs_total = 0;
491 while ((e = readdir(dir)) != NULL) {
492 const char *name = e->d_name;
493 char *end;
494 unsigned int pid = strtoul(name, &end, 10);
496 /* not a number */
497 if (pid == 0 && end == name)
498 continue;
500 stats->procs_total++;
503 closedir(dir);
505 return 0;
508 static int adjust_dbm_level(int in_dbm, int dbm_val)
510 if (!in_dbm)
511 return dbm_val;
513 return dbm_val - 0x100;
516 static int stats_wireless(const char *ifname, struct ifstat *stats)
518 int ret;
519 struct iw_statistics ws;
521 ret = wireless_sigqual(ifname, &ws);
522 if (ret != 0) {
523 stats->wifi.bitrate = 0;
524 return -EINVAL;
527 stats->wifi.bitrate = wireless_bitrate(ifname);
529 stats->wifi.signal_level =
530 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
532 stats->wifi.link_qual = ws.qual.qual;
533 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
535 return ret;
538 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
539 #define DIFF(member) do { \
540 if (sizeof(diff->member) != sizeof(new->member) || \
541 sizeof(diff->member) != sizeof(old->member)) \
542 bug(); \
543 if ((new->member - old->member) > (new->member)) { \
544 diff->member = 0; \
545 } else { \
546 DIFF1(member); \
548 } while (0)
550 static void stats_diff(struct ifstat *old, struct ifstat *new,
551 struct ifstat *diff)
553 unsigned int cpus, i;
555 DIFF(rx_bytes);
556 DIFF(rx_packets);
557 DIFF(rx_drops);
558 DIFF(rx_errors);
559 DIFF(rx_fifo);
560 DIFF(rx_frame);
561 DIFF(rx_multi);
563 DIFF(tx_bytes);
564 DIFF(tx_packets);
565 DIFF(tx_drops);
566 DIFF(tx_errors);
567 DIFF(tx_fifo);
568 DIFF(tx_colls);
569 DIFF(tx_carrier);
571 DIFF1(wifi.signal_level);
572 DIFF1(wifi.link_qual);
574 DIFF1(cswitch);
576 cpus = get_number_cpus();
578 for (i = 0; i < cpus; ++i) {
579 DIFF(irqs[i]);
580 DIFF(irqs_srx[i]);
581 DIFF(irqs_stx[i]);
583 DIFF1(cpu_user[i]);
584 DIFF1(cpu_nice[i]);
585 DIFF1(cpu_sys[i]);
586 DIFF1(cpu_idle[i]);
587 DIFF1(cpu_iow[i]);
591 static void stats_fetch(const char *ifname, struct ifstat *stats)
593 if (stats_proc_net_dev(ifname, stats) < 0)
594 panic("Cannot fetch device stats!\n");
595 if (stats_proc_softirqs(stats) < 0)
596 panic("Cannot fetch software interrupts!\n");
597 if (stats_proc_memory(stats) < 0)
598 panic("Cannot fetch memory stats!\n");
599 if (stats_proc_system(stats) < 0)
600 panic("Cannot fetch system stats!\n");
601 if (stats_proc_procs(stats) < 0)
602 panic("Cannot fetch process stats!\n");
604 stats_proc_interrupts((char *) ifname, stats);
606 stats_wireless(ifname, stats);
609 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
611 unsigned int cpus = get_number_cpus();
613 stats_zero(&stats_old, cpus);
614 stats_zero(&stats_new, cpus);
615 stats_zero(&stats_delta, cpus);
617 stats_fetch(ifname, &stats_old);
618 usleep(ms_interval * 1000);
619 stats_fetch(ifname, &stats_new);
621 stats_diff(&stats_old, &stats_new, &stats_delta);
624 static int cmp_hits(const void *p1, const void *p2)
626 const struct cpu_hit *h1 = p1, *h2 = p2;
629 * We want the hits sorted in descending order, thus reverse the return
630 * values.
632 if (h1->hit == h2->hit)
633 return 0;
634 else if (h1->hit < h2->hit)
635 return 1;
636 else
637 return -1;
640 static int cmp_irqs_rel(const void *p1, const void *p2)
642 const struct cpu_hit *h1 = p1, *h2 = p2;
645 * We want the hits sorted in descending order, thus reverse the return
646 * values.
648 if (h1->irqs_rel == h2->irqs_rel)
649 return 0;
650 else if (h1->irqs_rel < h2->irqs_rel)
651 return 1;
652 else
653 return -1;
656 static int cmp_irqs_abs(const void *p1, const void *p2)
658 const struct cpu_hit *h1 = p1, *h2 = p2;
661 * We want the hits sorted in descending order, thus reverse the return
662 * values.
664 if (h1->irqs_abs == h2->irqs_abs)
665 return 0;
666 else if (h1->irqs_abs < h2->irqs_abs)
667 return 1;
668 else
669 return -1;
672 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
673 unsigned int cpus)
675 unsigned int i;
677 memset(&stats_avg, 0, sizeof(stats_avg));
679 for (i = 0; i < cpus; ++i) {
680 cpu_hits[i].idx = i;
681 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
682 cpu_hits[i].irqs_rel = rel->irqs[i];
683 cpu_hits[i].irqs_abs = abs->irqs[i];
685 stats_avg.cpu_user += rel->cpu_user[i];
686 stats_avg.cpu_sys += rel->cpu_sys[i];
687 stats_avg.cpu_nice += rel->cpu_nice[i];
688 stats_avg.cpu_idle += rel->cpu_idle[i];
689 stats_avg.cpu_iow += rel->cpu_iow[i];
691 stats_avg.irqs_abs += abs->irqs[i];
692 stats_avg.irqs_rel += rel->irqs[i];
693 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
694 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
697 stats_avg.cpu_user /= cpus;
698 stats_avg.cpu_sys /= cpus;
699 stats_avg.cpu_nice /= cpus;
700 stats_avg.cpu_idle /= cpus;
701 stats_avg.cpu_iow /= cpus;
702 stats_avg.irqs_abs /= cpus;
703 stats_avg.irqs_rel /= cpus;
704 stats_avg.irqs_srx_rel /= cpus;
705 stats_avg.irqs_stx_rel /= cpus;
708 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
709 u32 rate, uint64_t ms_interval, unsigned int top_cpus)
711 size_t len = 0;
712 char buff[64], machine[64];
713 struct ethtool_drvinfo drvinf;
714 int link = ethtool_link(ifname);
715 unsigned int cpus = get_number_cpus();
717 memset(&drvinf, 0, sizeof(drvinf));
718 ethtool_drvinf(ifname, &drvinf);
720 memset(buff, 0, sizeof(buff));
721 memset(machine, 0, sizeof(machine));
723 if (rate)
724 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
725 if (link >= 0)
726 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
727 link == 0 ? "no" : "yes");
729 if (!strstr(uts.release, uts.machine))
730 slprintf(machine, sizeof(machine), " %s,", uts.machine);
732 mvwprintw(screen, (*voff)++, 2,
733 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
734 " ", uts.release, machine,
735 ifname, drvinf.driver, buff, ms_interval, top_cpus,
736 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
739 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
740 int *voff)
742 attron(A_REVERSE);
744 mvwprintw(screen, (*voff)++, 0,
745 " rx: %16.3llf MiB/t "
746 "%10llu pkts/t "
747 "%10llu drops/t "
748 "%10llu errors/t ",
749 ((long double) rel->rx_bytes) / (1LLU << 20),
750 rel->rx_packets, rel->rx_drops, rel->rx_errors);
752 mvwprintw(screen, (*voff)++, 0,
753 " tx: %16.3llf MiB/t "
754 "%10llu pkts/t "
755 "%10llu drops/t "
756 "%10llu errors/t ",
757 ((long double) rel->tx_bytes) / (1LLU << 20),
758 rel->tx_packets, rel->tx_drops, rel->tx_errors);
760 attroff(A_REVERSE);
763 static void screen_net_dev_percentage(WINDOW *screen, const struct ifstat *rel,
764 int *voff, u32 rate)
766 mvwprintw(screen, (*voff)++, 0,
767 " rx: %15.2llf%% of line rate "
768 " ",
769 rate ? ((((long double) rel->rx_bytes) / 125000) / rate) * 100.0 : 0.0);
771 mvwprintw(screen, (*voff)++, 0,
772 " tx: %15.2llf%% of line rate "
773 " ",
774 rate ? ((((long double) rel->tx_bytes) / 125000) / rate) * 100.0 : 0.0);
777 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
778 int *voff)
780 mvwprintw(screen, (*voff)++, 2,
781 "rx: %16.3llf MiB "
782 "%10llu pkts "
783 "%10llu drops "
784 "%10llu errors",
785 ((long double) abs->rx_bytes) / (1LLU << 20),
786 abs->rx_packets, abs->rx_drops, abs->rx_errors);
788 mvwprintw(screen, (*voff)++, 2,
789 "tx: %16.3llf MiB "
790 "%10llu pkts "
791 "%10llu drops "
792 "%10llu errors",
793 ((long double) abs->tx_bytes) / (1LLU << 20),
794 abs->tx_packets, abs->tx_drops, abs->tx_errors);
797 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
798 const struct ifstat *abs, int *voff)
800 mvwprintw(screen, (*voff)++, 2,
801 "sys: %14u cs/t "
802 "%11u procs "
803 "%11u running "
804 "%10u iowait",
805 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
808 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
810 mvwprintw(screen, (*voff)++, 2,
811 "mem: %13uM total "
812 "%9uM used "
813 "%11uM active "
814 "%10uM inactive",
815 abs->mem_total / 1024,
816 (abs->mem_total - abs->mem_free) / 1024,
817 abs->mem_active / 1024,
818 abs->mem_inactive / 1024);
820 mvwprintw(screen, (*voff)++, 2,
821 "swap: %12uM total "
822 "%9uM used "
823 "%11uM cached",
824 abs->swap_total / 1024,
825 (abs->swap_total - abs->swap_free) / 1024,
826 abs->swap_cached / 1024);
829 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
830 int *voff, unsigned int idx, char *tag)
832 int max_padd = padding_from_num(get_number_cpus());
833 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
834 rel->cpu_idle[idx] + rel->cpu_iow[idx];
836 mvwprintw(screen, (*voff)++, 2,
837 "cpu%*d %s: %11.1lf%% usr/t "
838 "%9.1lf%% sys/t "
839 "%10.1lf%% idl/t "
840 "%11.1lf%% iow/t",
841 max_padd, idx, tag,
842 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
843 100.0 * rel->cpu_sys[idx] / all,
844 100.0 * rel->cpu_idle[idx] / all,
845 100.0 * rel->cpu_iow[idx] / all);
848 #define MEDIAN_EVEN(member) do { \
849 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
850 } while (0)
852 #define MEDIAN_ODD(member) do { \
853 m_##member = rel->member[i]; \
854 } while (0)
856 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
857 const struct avg_stat *avg,
858 unsigned int top_cpus, int *voff)
860 unsigned int i;
861 unsigned int cpus = get_number_cpus();
862 int max_padd = padding_from_num(cpus);
863 uint64_t all;
865 if (top_cpus == 0)
866 return;
868 /* Display top hitter */
869 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
871 /* Make sure we don't display the min. hitter twice */
872 if (top_cpus == cpus)
873 top_cpus--;
875 for (i = 1; i < top_cpus; ++i)
876 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "|");
878 /* Display minimum hitter */
879 if (cpus != 1)
880 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
882 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
883 mvwprintw(screen, (*voff)++, 2,
884 "avg:%*s%14.1lf%% "
885 "%9.1lf%% "
886 "%10.1lf%% "
887 "%11.1lf%%", max_padd, "",
888 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
889 100.0 * avg->cpu_sys / all,
890 100.0 * avg->cpu_idle /all,
891 100.0 * avg->cpu_iow / all);
893 if (show_median) {
894 long double m_cpu_user, m_cpu_nice, m_cpu_sys, m_cpu_idle, m_cpu_iow;
895 long double m_all;
897 i = cpu_hits[cpus / 2].idx;
898 if (cpus % 2 == 0) {
899 /* take the mean of the 2 middle entries */
900 int j = cpu_hits[(cpus / 2) - 1].idx;
902 MEDIAN_EVEN(cpu_user);
903 MEDIAN_EVEN(cpu_nice);
904 MEDIAN_EVEN(cpu_sys);
905 MEDIAN_EVEN(cpu_idle);
906 MEDIAN_EVEN(cpu_iow);
907 } else {
908 /* take the middle entry as is */
909 MEDIAN_ODD(cpu_user);
910 MEDIAN_ODD(cpu_nice);
911 MEDIAN_ODD(cpu_sys);
912 MEDIAN_ODD(cpu_idle);
913 MEDIAN_ODD(cpu_iow);
916 m_all = m_cpu_user + m_cpu_sys + m_cpu_nice + m_cpu_idle + m_cpu_iow;
917 mvwprintw(screen, (*voff)++, 2,
918 "med:%*s%14.1Lf%% "
919 "%9.1Lf%% "
920 "%10.1Lf%% "
921 "%11.1Lf%%", max_padd, "",
922 100.0 * (m_cpu_user + m_cpu_nice) / m_all,
923 100.0 * m_cpu_sys / m_all,
924 100.0 * m_cpu_idle /m_all,
925 100.0 * m_cpu_iow / m_all);
929 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
930 int *voff, unsigned int idx, char *tag)
932 int max_padd = padding_from_num(get_number_cpus());
934 mvwprintw(screen, (*voff)++, 2,
935 "cpu%*d %s: %12llu irqs/t "
936 "%17llu sirq rx/t "
937 "%17llu sirq tx/t",
938 max_padd, idx, tag,
939 rel->irqs[idx],
940 rel->irqs_srx[idx],
941 rel->irqs_stx[idx]);
944 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
945 const struct avg_stat *avg,
946 unsigned int top_cpus, int *voff)
948 unsigned int i;
949 unsigned int cpus = get_number_cpus();
950 int max_padd = padding_from_num(cpus);
952 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
954 if (top_cpus == cpus)
955 top_cpus--;
957 for (i = 1; i < top_cpus; ++i)
958 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
960 if (cpus != 1)
961 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
963 mvwprintw(screen, (*voff)++, 2,
964 "avg:%*s%17.1Lf "
965 "%17.1Lf "
966 "%17.1Lf", max_padd, "",
967 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
969 if (show_median) {
970 long double m_irqs, m_irqs_srx, m_irqs_stx;
972 i = cpu_hits[cpus / 2].idx;
973 if (cpus % 2 == 0) {
974 /* take the mean of the 2 middle entries */
975 int j = cpu_hits[(cpus / 2) - 1].idx;
977 MEDIAN_EVEN(irqs);
978 MEDIAN_EVEN(irqs_srx);
979 MEDIAN_EVEN(irqs_stx);
980 } else {
981 /* take the middle entry as is */
982 MEDIAN_ODD(irqs);
983 MEDIAN_ODD(irqs_srx);
984 MEDIAN_ODD(irqs_stx);
987 mvwprintw(screen, (*voff)++, 2,
988 "med:%*s%17.1Lf "
989 "%17.1Lf "
990 "%17.1Lf", max_padd, "",
991 m_irqs, m_irqs_srx, m_irqs_stx);
995 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
996 int *voff, unsigned int idx, char *tag)
998 int max_padd = padding_from_num(get_number_cpus());
1000 mvwprintw(screen, (*voff)++, 2,
1001 "cpu%*d %s: %12llu irqs",
1002 max_padd, idx, tag, abs->irqs[idx]);
1005 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
1006 const struct avg_stat *avg,
1007 unsigned int top_cpus, int *voff)
1009 unsigned int i;
1010 unsigned int cpus = get_number_cpus();
1011 int max_padd = padding_from_num(cpus);
1013 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
1015 if (top_cpus == cpus)
1016 top_cpus--;
1018 for (i = 1; i < top_cpus; ++i)
1019 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
1021 if (cpus != 1)
1022 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
1024 mvwprintw(screen, (*voff)++, 2,
1025 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
1027 if (show_median) {
1028 long double m_irqs;
1030 i = cpu_hits[cpus / 2].idx;
1031 if (cpus % 2 == 0) {
1032 /* take the mean of the 2 middle entries */
1033 int j = cpu_hits[(cpus / 2) - 1].idx;
1035 m_irqs = (abs->irqs[i] + abs->irqs[j]) / 2;
1036 } else {
1037 /* take the middle entry as is */
1038 m_irqs = abs->irqs[i];
1041 mvwprintw(screen, (*voff)++, 2,
1042 "med:%*s%17.1Lf", max_padd, "", m_irqs);
1046 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
1047 const struct ifstat *abs, int *voff)
1049 if (iswireless(abs)) {
1050 mvwprintw(screen, (*voff)++, 2,
1051 "linkqual: %7d/%d (%d/t) ",
1052 abs->wifi.link_qual,
1053 abs->wifi.link_qual_max,
1054 rel->wifi.link_qual);
1056 mvwprintw(screen, (*voff)++, 2,
1057 "signal: %8d dBm (%d dBm/t) ",
1058 abs->wifi.signal_level,
1059 rel->wifi.signal_level);
1063 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
1064 const struct ifstat *abs, const struct avg_stat *avg,
1065 int *first, uint64_t ms_interval, unsigned int top_cpus,
1066 bool need_info)
1068 unsigned int cpus, top;
1069 int voff = 1, cvoff = 2;
1070 u32 rate = device_bitrate(ifname);
1072 curs_set(0);
1074 cpus = get_number_cpus();
1075 top = min(cpus, top_cpus);
1077 stats_top(rel, abs, cpus);
1079 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
1081 screen_header(screen, ifname, &voff, rate, ms_interval, top_cpus);
1083 voff++;
1084 screen_net_dev_rel(screen, rel, &voff);
1086 if (show_percentage) {
1087 voff++;
1088 screen_net_dev_percentage(screen, rel, &voff, rate);
1091 voff++;
1092 screen_net_dev_abs(screen, abs, &voff);
1094 voff++;
1095 screen_sys(screen, rel, abs, &voff);
1097 voff++;
1098 screen_mem_swap(screen, abs, &voff);
1100 voff++;
1101 screen_percpu_states(screen, rel, avg, top, &voff);
1103 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
1105 voff++;
1106 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
1108 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
1110 voff++;
1111 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
1113 voff++;
1114 screen_wireless(screen, rel, abs, &voff);
1116 if (*first) {
1117 mvwprintw(screen, cvoff, 2, "Collecting data ...");
1118 *first = 0;
1119 } else {
1120 if (need_info)
1121 mvwprintw(screen, cvoff, 2, "(consider to increase "
1122 "your sampling interval, e.g. -t %d)",
1123 rate > SPEED_1000 ? 10000 : 1000);
1124 else
1125 mvwprintw(screen, cvoff, 2, " "
1126 " ");
1129 wrefresh(screen);
1130 refresh();
1133 static int screen_main(const char *ifname, uint64_t ms_interval,
1134 unsigned int top_cpus, bool suppress_warnings,
1135 bool omit_header __maybe_unused)
1137 int first = 1, key;
1138 u32 rate = device_bitrate(ifname);
1139 bool need_info = false;
1141 stats_screen = screen_init(true);
1143 if (((rate > SPEED_1000 && ms_interval <= 1000) ||
1144 (rate = SPEED_1000 && ms_interval < 1000)) &&
1145 !suppress_warnings)
1146 need_info = true;
1148 while (!sigint) {
1149 key = getch();
1150 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1151 break;
1153 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1154 &first, ms_interval, top_cpus, need_info);
1156 stats_sample_generic(ifname, ms_interval);
1159 screen_end();
1161 return 0;
1164 static void term_csv(const struct ifstat *rel, const struct ifstat *abs)
1166 unsigned int cpus, i;
1168 printf("%ld ", time(NULL));
1170 printf("%llu ", rel->rx_bytes);
1171 printf("%llu ", rel->rx_packets);
1172 printf("%llu ", rel->rx_drops);
1173 printf("%llu ", rel->rx_errors);
1175 printf("%llu ", abs->rx_bytes);
1176 printf("%llu ", abs->rx_packets);
1177 printf("%llu ", abs->rx_drops);
1178 printf("%llu ", abs->rx_errors);
1180 printf("%llu ", rel->tx_bytes);
1181 printf("%llu ", rel->tx_packets);
1182 printf("%llu ", rel->tx_drops);
1183 printf("%llu ", rel->tx_errors);
1185 printf("%llu ", abs->tx_bytes);
1186 printf("%llu ", abs->tx_packets);
1187 printf("%llu ", abs->tx_drops);
1188 printf("%llu ", abs->tx_errors);
1190 printf("%u ", rel->cswitch);
1191 printf("%lu ", abs->mem_free);
1192 printf("%lu ", abs->mem_total - abs->mem_free);
1193 printf("%lu ", abs->mem_total);
1194 printf("%lu ", abs->swap_free);
1195 printf("%lu ", abs->swap_total - abs->swap_free);
1196 printf("%lu ", abs->swap_total);
1197 printf("%u ", abs->procs_total);
1198 printf("%u ", abs->procs_run);
1199 printf("%u ", abs->procs_iow);
1201 cpus = get_number_cpus();
1203 for (i = 0; i < cpus; ++i) {
1204 printf("%lu ", rel->cpu_user[i]);
1205 printf("%lu ", rel->cpu_nice[i]);
1206 printf("%lu ", rel->cpu_sys[i]);
1207 printf("%lu ", rel->cpu_idle[i]);
1208 printf("%lu ", rel->cpu_iow[i]);
1210 printf("%llu ", rel->irqs[i]);
1211 printf("%llu ", abs->irqs[i]);
1213 printf("%llu ", rel->irqs_srx[i]);
1214 printf("%llu ", abs->irqs_srx[i]);
1216 printf("%llu ", rel->irqs_stx[i]);
1217 printf("%llu ", abs->irqs_stx[i]);
1220 if (iswireless(abs)) {
1221 printf("%u ", rel->wifi.link_qual);
1222 printf("%u ", abs->wifi.link_qual);
1223 printf("%u ", abs->wifi.link_qual_max);
1225 printf("%d ", rel->wifi.signal_level);
1226 printf("%d ", abs->wifi.signal_level);
1229 puts("");
1230 fflush(stdout);
1233 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1234 uint64_t ms_interval)
1236 unsigned int cpus, i, j = 1;
1238 printf("# gnuplot dump (#col:description)\n");
1239 printf("# networking interface: %s\n", ifname);
1240 printf("# sampling interval (t): %lu ms\n", ms_interval);
1241 printf("# %d:unixtime ", j++);
1243 printf("%d:rx-bytes-per-t ", j++);
1244 printf("%d:rx-pkts-per-t ", j++);
1245 printf("%d:rx-drops-per-t ", j++);
1246 printf("%d:rx-errors-per-t ", j++);
1248 printf("%d:rx-bytes ", j++);
1249 printf("%d:rx-pkts ", j++);
1250 printf("%d:rx-drops ", j++);
1251 printf("%d:rx-errors ", j++);
1253 printf("%d:tx-bytes-per-t ", j++);
1254 printf("%d:tx-pkts-per-t ", j++);
1255 printf("%d:tx-drops-per-t ", j++);
1256 printf("%d:tx-errors-per-t ", j++);
1258 printf("%d:tx-bytes ", j++);
1259 printf("%d:tx-pkts ", j++);
1260 printf("%d:tx-drops ", j++);
1261 printf("%d:tx-errors ", j++);
1263 printf("%d:context-switches-per-t ", j++);
1264 printf("%d:mem-free ", j++);
1265 printf("%d:mem-used ", j++);
1266 printf("%d:mem-total ", j++);
1267 printf("%d:swap-free ", j++);
1268 printf("%d:swap-used ", j++);
1269 printf("%d:swap-total ", j++);
1270 printf("%d:procs-total ", j++);
1271 printf("%d:procs-in-run ", j++);
1272 printf("%d:procs-in-iow ", j++);
1274 cpus = get_number_cpus();
1276 for (i = 0; i < cpus; ++i) {
1277 printf("%d:cpu%i-usr-per-t ", j++, i);
1278 printf("%d:cpu%i-nice-per-t ", j++, i);
1279 printf("%d:cpu%i-sys-per-t ", j++, i);
1280 printf("%d:cpu%i-idle-per-t ", j++, i);
1281 printf("%d:cpu%i-iow-per-t ", j++, i);
1283 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1284 printf("%d:cpu%i-net-irqs ", j++, i);
1286 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1287 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1288 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1289 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1292 if (iswireless(abs)) {
1293 printf("%d:wifi-link-qual-per-t ", j++);
1294 printf("%d:wifi-link-qual ", j++);
1295 printf("%d:wifi-link-qual-max ", j++);
1297 printf("%d:wifi-signal-dbm-per-t ", j++);
1298 printf("%d:wifi-signal-dbm ", j++);
1301 puts("");
1302 printf("# data:\n");
1303 fflush(stdout);
1306 static int term_main(const char *ifname, uint64_t ms_interval,
1307 unsigned int top_cpus __maybe_unused,
1308 bool suppress_warnings __maybe_unused,
1309 bool omit_header)
1311 do {
1312 stats_sample_generic(ifname, ms_interval);
1314 if (!omit_header) {
1315 omit_header = true;
1316 term_csv_header(ifname, &stats_new, ms_interval);
1319 term_csv(&stats_delta, &stats_new);
1320 } while (stats_loop && !sigint);
1322 return 0;
1325 int main(int argc, char **argv)
1327 short ifflags = 0;
1328 int c, opt_index, ret, promisc = 0;
1329 unsigned int cpus, top_cpus = 5;
1330 uint64_t interval = 1000;
1331 char *ifname = NULL;
1332 bool suppress_warnings = false;
1333 bool omit_header = false;
1334 int (*func_main)(const char *ifname, uint64_t ms_interval,
1335 unsigned int top_cpus, bool suppress_warnings,
1336 bool omit_header);
1338 func_main = screen_main;
1340 setfsuid(getuid());
1341 setfsgid(getgid());
1343 while ((c = getopt_long(argc, argv, short_options, long_options,
1344 &opt_index)) != EOF) {
1345 switch (c) {
1346 case 'h':
1347 help();
1348 break;
1349 case 'v':
1350 version();
1351 break;
1352 case 'W':
1353 suppress_warnings = true;
1354 break;
1355 case 'd':
1356 ifname = xstrndup(optarg, IFNAMSIZ);
1357 break;
1358 case 't':
1359 interval = strtoul(optarg, NULL, 10);
1360 break;
1361 case 'n':
1362 top_cpus = strtoul(optarg, NULL, 10);
1363 if (top_cpus < 1)
1364 panic("Number of top hitter CPUs must be greater than 0");
1365 break;
1366 case 'l':
1367 stats_loop = 1;
1368 break;
1369 case 'p':
1370 promisc = 1;
1371 break;
1372 case 'P':
1373 show_percentage = 1;
1374 break;
1375 case 'm':
1376 show_median = 1;
1377 break;
1378 case 'c':
1379 func_main = term_main;
1380 break;
1381 case 'o':
1382 omit_header = true;
1383 break;
1384 case '?':
1385 switch (optopt) {
1386 case 'd':
1387 case 't':
1388 panic("Option -%c requires an argument!\n",
1389 optopt);
1390 default:
1391 if (isprint(optopt))
1392 printf("Unknown option character `0x%X\'!\n", optopt);
1393 die();
1395 default:
1396 break;
1400 if (argc == 1)
1401 help();
1403 if (argc == 2)
1404 ifname = xstrndup(argv[1], IFNAMSIZ);
1405 if (ifname == NULL)
1406 panic("No networking device given!\n");
1408 if (!strncmp("lo", ifname, IFNAMSIZ))
1409 panic("lo is not supported!\n");
1410 if (device_mtu(ifname) == 0)
1411 panic("This is no networking device!\n");
1413 register_signal(SIGINT, signal_handler);
1414 register_signal(SIGQUIT, signal_handler);
1415 register_signal(SIGSTOP, signal_handler);
1416 register_signal(SIGHUP, signal_handler);
1418 cpus = get_number_cpus();
1419 top_cpus = min(top_cpus, cpus);
1420 if (uname(&uts) < 0)
1421 panic("Cannot execute uname!\n");
1423 stats_alloc(&stats_old, cpus);
1424 stats_alloc(&stats_new, cpus);
1425 stats_alloc(&stats_delta, cpus);
1427 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1429 if (promisc)
1430 ifflags = device_enter_promiscuous_mode(ifname);
1431 ret = func_main(ifname, interval, top_cpus, suppress_warnings, omit_header);
1432 if (promisc)
1433 device_leave_promiscuous_mode(ifname, ifflags);
1435 stats_release(&stats_old);
1436 stats_release(&stats_new);
1437 stats_release(&stats_delta);
1439 xfree(ifname);
1440 return ret;