mausezahn: use getopt_long instead of getopt
[netsniff-ng.git] / ifpps.c
blobd1123742b63d975eb1d32b10e98d9d75512ea74a
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 const char *copyright =
97 "Please report bugs at https://github.com/netsniff-ng/netsniff-ng/issues\n"
98 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
99 "Swiss federal institute of technology (ETH Zurich)\n"
100 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
101 "License: GNU GPL version 2.0\n"
102 "This is free software: you are free to change and redistribute it.\n"
103 "There is NO WARRANTY, to the extent permitted by law.";
105 static void signal_handler(int number)
107 switch (number) {
108 case SIGINT:
109 case SIGQUIT:
110 case SIGTERM:
111 sigint = 1;
112 break;
113 case SIGHUP:
114 default:
115 break;
119 static inline int iswireless(const struct ifstat *stats)
121 return stats->wifi.bitrate > 0;
124 static void __noreturn help(void)
126 printf("ifpps %s, top-like kernel networking and system statistics\n",
127 VERSION_STRING);
128 puts("http://www.netsniff-ng.org\n\n"
129 "Usage: ifpps [options] || ifpps <netdev>\n"
130 "Options:\n"
131 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
132 " -n|--num-cpus <num> Number of top hitter CPUs in ncurses mode (def: 5)\n"
133 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
134 " -c|--csv Output to terminal as Gnuplot-ready data\n"
135 " -l|--loop Continuous CSV output\n"
136 " -m|--median Display median values\n"
137 " -o|--omit-header Do not print the CSV header\n"
138 " -p|--promisc Promiscuous mode\n"
139 " -P|--percentage Show percentage of theoretical line rate\n"
140 " -W|--no-warn Suppress warnings\n"
141 " -v|--version Print version and exit\n"
142 " -h|--help Print this help and exit\n\n"
143 "Examples:\n"
144 " ifpps eth0\n"
145 " ifpps -pd eth0\n"
146 " ifpps -lpcd wlan0 > plot.dat\n\n"
147 "Note:\n"
148 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
149 " Thus, in those situations, it's good to use a -t of 10sec.\n");
150 puts(copyright);
151 die();
154 static void __noreturn version(void)
156 printf("ifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
157 puts("top-like kernel networking and system statistics\n"
158 "http://www.netsniff-ng.org\n");
159 puts(copyright);
160 die();
163 static inline int padding_from_num(int n)
165 int i = 0;
166 do {
167 i++;
168 } while ((n /= 10) > 0);
169 return i;
172 #define STATS_ALLOC1(member) \
173 do { stats->member = xcalloc(cpus, sizeof(*(stats->member))); } while (0)
175 static void stats_alloc(struct ifstat *stats, unsigned int cpus)
177 STATS_ALLOC1(irqs);
178 STATS_ALLOC1(irqs_srx);
179 STATS_ALLOC1(irqs_stx);
181 STATS_ALLOC1(cpu_user);
182 STATS_ALLOC1(cpu_sys);
183 STATS_ALLOC1(cpu_nice);
184 STATS_ALLOC1(cpu_idle);
185 STATS_ALLOC1(cpu_iow);
188 #define STATS_ZERO1(member) \
189 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
191 static void stats_zero(struct ifstat *stats, unsigned int cpus)
193 /* Only clear the non-pointer members */
194 memset(stats, 0, offsetof(struct ifstat, irqs));
196 STATS_ZERO1(irqs);
197 STATS_ZERO1(irqs_srx);
198 STATS_ZERO1(irqs_stx);
200 STATS_ZERO1(cpu_user);
201 STATS_ZERO1(cpu_sys);
202 STATS_ZERO1(cpu_nice);
203 STATS_ZERO1(cpu_idle);
204 STATS_ZERO1(cpu_iow);
207 #define STATS_RELEASE(member) \
208 do { xfree(stats->member); } while (0)
210 static void stats_release(struct ifstat *stats)
212 STATS_RELEASE(irqs);
213 STATS_RELEASE(irqs_srx);
214 STATS_RELEASE(irqs_stx);
216 STATS_RELEASE(cpu_user);
217 STATS_RELEASE(cpu_sys);
218 STATS_RELEASE(cpu_nice);
219 STATS_RELEASE(cpu_idle);
220 STATS_RELEASE(cpu_iow);
223 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
225 int ret = -EINVAL;
226 char buff[256];
227 char *ifname_colon;
228 FILE *fp;
230 fp = fopen("/proc/net/dev", "r");
231 if (!fp)
232 panic("Cannot open /proc/net/dev!\n");
234 ifname_colon = xstrndup(ifname, strlen(ifname) + 2);
235 ifname_colon[strlen(ifname)] = ':';
236 ifname_colon[strlen(ifname) + 1] = '\0';
238 if (fgets(buff, sizeof(buff), fp)) { ; }
239 if (fgets(buff, sizeof(buff), fp)) { ; }
241 memset(buff, 0, sizeof(buff));
243 while (fgets(buff, sizeof(buff), fp) != NULL) {
244 buff[sizeof(buff) - 1] = 0;
246 if (strstr(buff, ifname_colon) == NULL)
247 continue;
249 if (sscanf(buff, "%*[a-zA-Z0-9_ .-]:%llu%llu%llu%llu%llu%llu"
250 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
251 &stats->rx_bytes, &stats->rx_packets,
252 &stats->rx_errors, &stats->rx_drops,
253 &stats->rx_fifo, &stats->rx_frame,
254 &stats->rx_multi, &stats->tx_bytes,
255 &stats->tx_packets, &stats->tx_errors,
256 &stats->tx_drops, &stats->tx_fifo,
257 &stats->tx_colls, &stats->tx_carrier) == 14) {
258 ret = 0;
259 break;
262 memset(buff, 0, sizeof(buff));
265 xfree(ifname_colon);
266 fclose(fp);
267 return ret;
270 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
272 int ret = -EINVAL, try = 0;
273 unsigned int i, cpus;
274 char *ptr, *buff;
275 bool seen = false;
276 size_t buff_len;
277 struct ethtool_drvinfo drvinf;
278 FILE *fp;
280 cpus = get_number_cpus();
281 buff_len = cpus * 128;
282 buff = xmalloc(buff_len);
284 fp = fopen("/proc/interrupts", "r");
285 if (!fp)
286 panic("Cannot open /proc/interrupts!\n");
287 retry:
288 fseek(fp, 0, SEEK_SET);
289 memset(buff, 0, buff_len);
291 while (fgets(buff, buff_len, fp) != NULL) {
292 buff[buff_len - 1] = 0;
294 if (strstr(buff, ifname) == NULL)
295 continue;
297 ptr = strchr(buff, ':');
298 if (!ptr)
299 continue;
300 ptr++;
302 for (i = 0; i < cpus && ptr; ++i) {
303 if (seen)
304 stats->irqs[i] += strtol(ptr, &ptr, 10);
305 else
306 stats->irqs[i] = strtol(ptr, &ptr, 10);
307 if (i == cpus - 1) {
308 ret = 0;
309 seen = true;
313 memset(buff, 0, buff_len);
316 if (ret == -EINVAL && try == 0) {
317 if (ethtool_drvinf(ifname, &drvinf) < 0)
318 goto done;
320 ifname = drvinf.driver;
321 try++;
323 goto retry;
325 done:
326 xfree(buff);
327 fclose(fp);
328 return ret;
331 static int stats_proc_softirqs(struct ifstat *stats)
333 unsigned int i, cpus;
334 char *ptr, *buff;
335 size_t buff_len;
336 FILE *fp;
337 enum {
338 softirqs_net_rx,
339 softirqs_net_tx,
340 } net_type;
342 fp = fopen("/proc/softirqs", "r");
343 if (!fp)
344 panic("Cannot open /proc/softirqs!\n");
346 cpus = get_number_cpus();
347 buff_len = cpus * 128;
348 buff = xmalloc(buff_len);
350 memset(buff, 0, buff_len);
352 while (fgets(buff, buff_len, fp) != NULL) {
353 buff[buff_len - 1] = 0;
355 if ((ptr = strstr(buff, "NET_TX:")))
356 net_type = softirqs_net_tx;
357 else if ((ptr = strstr(buff, "NET_RX:")))
358 net_type = softirqs_net_rx;
359 else
360 continue;
362 ptr += strlen("NET_xX:");
364 for (i = 0; i < cpus; ++i) {
365 switch (net_type) {
366 case softirqs_net_tx:
367 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
368 break;
369 case softirqs_net_rx:
370 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
371 break;
375 memset(buff, 0, buff_len);
378 xfree(buff);
379 fclose(fp);
380 return 0;
383 static int stats_proc_memory(struct ifstat *stats)
385 char *ptr, buff[256];
386 FILE *fp;
388 fp = fopen("/proc/meminfo", "r");
389 if (!fp)
390 panic("Cannot open /proc/meminfo!\n");
392 memset(buff, 0, sizeof(buff));
394 while (fgets(buff, sizeof(buff), fp) != NULL) {
395 buff[sizeof(buff) - 1] = 0;
397 if ((ptr = strstr(buff, "MemTotal:"))) {
398 ptr += strlen("MemTotal:");
399 stats->mem_total = strtoul(ptr, &ptr, 10);
400 } else if ((ptr = strstr(buff, "MemFree:"))) {
401 ptr += strlen("MemFree:");
402 stats->mem_free = strtoul(ptr, &ptr, 10);
403 } else if ((ptr = strstr(buff, "Active:"))) {
404 ptr += strlen("Active:");
405 stats->mem_active = strtoul(ptr, &ptr, 10);
406 } else if ((ptr = strstr(buff, "Inactive:"))) {
407 ptr += strlen("Inactive:");
408 stats->mem_inactive = strtoul(ptr, &ptr, 10);
409 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
410 ptr += strlen("SwapTotal:");
411 stats->swap_total = strtoul(ptr, &ptr, 10);
412 } else if ((ptr = strstr(buff, "SwapFree:"))) {
413 ptr += strlen("SwapFree:");
414 stats->swap_free = strtoul(ptr, &ptr, 10);
415 } else if ((ptr = strstr(buff, "SwapCached:"))) {
416 ptr += strlen("SwapCached:");
417 stats->swap_cached = strtoul(ptr, &ptr, 10);
420 memset(buff, 0, sizeof(buff));
423 fclose(fp);
424 return 0;
427 static int stats_proc_system(struct ifstat *stats)
429 unsigned int cpu, cpus;
430 char *ptr, buff[256];
431 FILE *fp;
433 fp = fopen("/proc/stat", "r");
434 if (!fp)
435 panic("Cannot open /proc/stat!\n");
437 cpus = get_number_cpus();
439 memset(buff, 0, sizeof(buff));
441 while (fgets(buff, sizeof(buff), fp) != NULL) {
442 buff[sizeof(buff) - 1] = 0;
444 if ((ptr = strstr(buff, "cpu"))) {
445 ptr += strlen("cpu");
446 if (isblank(*ptr))
447 goto next;
449 cpu = strtol(ptr, &ptr, 10);
450 bug_on(cpu > cpus);
452 if (sscanf(ptr, "%"SCNu64"%"SCNu64"%"SCNu64"%"SCNu64"%"SCNu64,
453 &stats->cpu_user[cpu],
454 &stats->cpu_nice[cpu],
455 &stats->cpu_sys[cpu],
456 &stats->cpu_idle[cpu],
457 &stats->cpu_iow[cpu]) != 5)
458 goto next;
459 } else if ((ptr = strstr(buff, "ctxt"))) {
460 ptr += strlen("ctxt");
461 stats->cswitch = strtoul(ptr, &ptr, 10);
462 } else if ((ptr = strstr(buff, "procs_running"))) {
463 ptr += strlen("procs_running");
464 stats->procs_run = strtoul(ptr, &ptr, 10);
465 } else if ((ptr = strstr(buff, "procs_blocked"))) {
466 ptr += strlen("procs_blocked");
467 stats->procs_iow = strtoul(ptr, &ptr, 10);
469 next:
470 memset(buff, 0, sizeof(buff));
473 fclose(fp);
474 return 0;
477 static int stats_proc_procs(struct ifstat *stats)
479 DIR *dir;
480 struct dirent *e;
482 dir = opendir("/proc");
483 if (!dir)
484 panic("Cannot open /proc\n");
486 stats->procs_total = 0;
488 while ((e = readdir(dir)) != NULL) {
489 const char *name = e->d_name;
490 char *end;
491 unsigned int pid = strtoul(name, &end, 10);
493 /* not a number */
494 if (pid == 0 && end == name)
495 continue;
497 stats->procs_total++;
500 closedir(dir);
502 return 0;
505 static int adjust_dbm_level(int in_dbm, int dbm_val)
507 if (!in_dbm)
508 return dbm_val;
510 return dbm_val - 0x100;
513 static int stats_wireless(const char *ifname, struct ifstat *stats)
515 int ret;
516 struct iw_statistics ws;
518 ret = wireless_sigqual(ifname, &ws);
519 if (ret != 0) {
520 stats->wifi.bitrate = 0;
521 return -EINVAL;
524 stats->wifi.bitrate = wireless_bitrate(ifname);
526 stats->wifi.signal_level =
527 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
529 stats->wifi.link_qual = ws.qual.qual;
530 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
532 return ret;
535 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
536 #define DIFF(member) do { \
537 if (sizeof(diff->member) != sizeof(new->member) || \
538 sizeof(diff->member) != sizeof(old->member)) \
539 bug(); \
540 if ((new->member - old->member) > (new->member)) { \
541 diff->member = 0; \
542 } else { \
543 DIFF1(member); \
545 } while (0)
547 static void stats_diff(struct ifstat *old, struct ifstat *new,
548 struct ifstat *diff)
550 unsigned int cpus, i;
552 DIFF(rx_bytes);
553 DIFF(rx_packets);
554 DIFF(rx_drops);
555 DIFF(rx_errors);
556 DIFF(rx_fifo);
557 DIFF(rx_frame);
558 DIFF(rx_multi);
560 DIFF(tx_bytes);
561 DIFF(tx_packets);
562 DIFF(tx_drops);
563 DIFF(tx_errors);
564 DIFF(tx_fifo);
565 DIFF(tx_colls);
566 DIFF(tx_carrier);
568 DIFF1(wifi.signal_level);
569 DIFF1(wifi.link_qual);
571 DIFF1(cswitch);
573 cpus = get_number_cpus();
575 for (i = 0; i < cpus; ++i) {
576 DIFF(irqs[i]);
577 DIFF(irqs_srx[i]);
578 DIFF(irqs_stx[i]);
580 DIFF1(cpu_user[i]);
581 DIFF1(cpu_nice[i]);
582 DIFF1(cpu_sys[i]);
583 DIFF1(cpu_idle[i]);
584 DIFF1(cpu_iow[i]);
588 static void stats_fetch(const char *ifname, struct ifstat *stats)
590 if (stats_proc_net_dev(ifname, stats) < 0)
591 panic("Cannot fetch device stats!\n");
592 if (stats_proc_softirqs(stats) < 0)
593 panic("Cannot fetch software interrupts!\n");
594 if (stats_proc_memory(stats) < 0)
595 panic("Cannot fetch memory stats!\n");
596 if (stats_proc_system(stats) < 0)
597 panic("Cannot fetch system stats!\n");
598 if (stats_proc_procs(stats) < 0)
599 panic("Cannot fetch process stats!\n");
601 stats_proc_interrupts((char *) ifname, stats);
603 stats_wireless(ifname, stats);
606 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
608 unsigned int cpus = get_number_cpus();
610 stats_zero(&stats_old, cpus);
611 stats_zero(&stats_new, cpus);
612 stats_zero(&stats_delta, cpus);
614 stats_fetch(ifname, &stats_old);
615 usleep(ms_interval * 1000);
616 stats_fetch(ifname, &stats_new);
618 stats_diff(&stats_old, &stats_new, &stats_delta);
621 static int cmp_hits(const void *p1, const void *p2)
623 const struct cpu_hit *h1 = p1, *h2 = p2;
626 * We want the hits sorted in descending order, thus reverse the return
627 * values.
629 if (h1->hit == h2->hit)
630 return 0;
631 else if (h1->hit < h2->hit)
632 return 1;
633 else
634 return -1;
637 static int cmp_irqs_rel(const void *p1, const void *p2)
639 const struct cpu_hit *h1 = p1, *h2 = p2;
642 * We want the hits sorted in descending order, thus reverse the return
643 * values.
645 if (h1->irqs_rel == h2->irqs_rel)
646 return 0;
647 else if (h1->irqs_rel < h2->irqs_rel)
648 return 1;
649 else
650 return -1;
653 static int cmp_irqs_abs(const void *p1, const void *p2)
655 const struct cpu_hit *h1 = p1, *h2 = p2;
658 * We want the hits sorted in descending order, thus reverse the return
659 * values.
661 if (h1->irqs_abs == h2->irqs_abs)
662 return 0;
663 else if (h1->irqs_abs < h2->irqs_abs)
664 return 1;
665 else
666 return -1;
669 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
670 unsigned int cpus)
672 unsigned int i;
674 memset(&stats_avg, 0, sizeof(stats_avg));
676 for (i = 0; i < cpus; ++i) {
677 cpu_hits[i].idx = i;
678 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
679 cpu_hits[i].irqs_rel = rel->irqs[i];
680 cpu_hits[i].irqs_abs = abs->irqs[i];
682 stats_avg.cpu_user += rel->cpu_user[i];
683 stats_avg.cpu_sys += rel->cpu_sys[i];
684 stats_avg.cpu_nice += rel->cpu_nice[i];
685 stats_avg.cpu_idle += rel->cpu_idle[i];
686 stats_avg.cpu_iow += rel->cpu_iow[i];
688 stats_avg.irqs_abs += abs->irqs[i];
689 stats_avg.irqs_rel += rel->irqs[i];
690 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
691 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
694 stats_avg.cpu_user /= cpus;
695 stats_avg.cpu_sys /= cpus;
696 stats_avg.cpu_nice /= cpus;
697 stats_avg.cpu_idle /= cpus;
698 stats_avg.cpu_iow /= cpus;
699 stats_avg.irqs_abs /= cpus;
700 stats_avg.irqs_rel /= cpus;
701 stats_avg.irqs_srx_rel /= cpus;
702 stats_avg.irqs_stx_rel /= cpus;
705 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
706 uint32_t rate, uint64_t ms_interval, unsigned int top_cpus)
708 size_t len = 0;
709 char buff[64], machine[64];
710 struct ethtool_drvinfo drvinf;
711 int link = ethtool_link(ifname);
712 unsigned int cpus = get_number_cpus();
714 ethtool_drvinf(ifname, &drvinf);
716 memset(buff, 0, sizeof(buff));
717 memset(machine, 0, sizeof(machine));
719 if (rate)
720 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
721 if (link >= 0)
722 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
723 link == 0 ? "no" : "yes");
725 if (!strstr(uts.release, uts.machine))
726 slprintf(machine, sizeof(machine), " %s,", uts.machine);
728 mvwprintw(screen, (*voff)++, 2,
729 "%s,%s %s (%s%s), t=%"PRIu64"ms, cpus=%u%s/%u"
730 " ", uts.release, machine,
731 ifname, drvinf.driver, buff, ms_interval, top_cpus,
732 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
735 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
736 int *voff)
738 attron(A_REVERSE);
740 mvwprintw(screen, (*voff)++, 0,
741 " rx: %16.3llf MiB/t "
742 "%10llu pkts/t "
743 "%10llu drops/t "
744 "%10llu errors/t ",
745 ((long double) rel->rx_bytes) / (1LLU << 20),
746 rel->rx_packets, rel->rx_drops, rel->rx_errors);
748 mvwprintw(screen, (*voff)++, 0,
749 " tx: %16.3llf MiB/t "
750 "%10llu pkts/t "
751 "%10llu drops/t "
752 "%10llu errors/t ",
753 ((long double) rel->tx_bytes) / (1LLU << 20),
754 rel->tx_packets, rel->tx_drops, rel->tx_errors);
756 attroff(A_REVERSE);
759 static void screen_net_dev_percentage(WINDOW *screen, const struct ifstat *rel,
760 int *voff, uint32_t rate)
762 mvwprintw(screen, (*voff)++, 0,
763 " rx: %15.2llf%% of line rate "
764 " ",
765 rate ? ((((long double) rel->rx_bytes) / 125000) / rate) * 100.0 : 0.0);
767 mvwprintw(screen, (*voff)++, 0,
768 " tx: %15.2llf%% of line rate "
769 " ",
770 rate ? ((((long double) rel->tx_bytes) / 125000) / rate) * 100.0 : 0.0);
773 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
774 int *voff)
776 mvwprintw(screen, (*voff)++, 2,
777 "rx: %16.3llf MiB "
778 "%10llu pkts "
779 "%10llu drops "
780 "%10llu errors",
781 ((long double) abs->rx_bytes) / (1LLU << 20),
782 abs->rx_packets, abs->rx_drops, abs->rx_errors);
784 mvwprintw(screen, (*voff)++, 2,
785 "tx: %16.3llf MiB "
786 "%10llu pkts "
787 "%10llu drops "
788 "%10llu errors",
789 ((long double) abs->tx_bytes) / (1LLU << 20),
790 abs->tx_packets, abs->tx_drops, abs->tx_errors);
793 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
794 const struct ifstat *abs, int *voff)
796 mvwprintw(screen, (*voff)++, 2,
797 "sys: %14"PRIu32" cs/t "
798 "%11"PRIu32" procs "
799 "%11"PRIu32" running "
800 "%10"PRIu32" iowait",
801 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
804 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
806 mvwprintw(screen, (*voff)++, 2,
807 "mem: %13"PRIu64"M total "
808 "%9"PRIu64"M used "
809 "%11"PRIu64"M active "
810 "%10"PRIu64"M inactive",
811 abs->mem_total / 1024,
812 (abs->mem_total - abs->mem_free) / 1024,
813 abs->mem_active / 1024,
814 abs->mem_inactive / 1024);
816 mvwprintw(screen, (*voff)++, 2,
817 "swap: %12"PRIu64"M total "
818 "%9uM used "
819 "%11uM cached",
820 abs->swap_total / 1024,
821 (abs->swap_total - abs->swap_free) / 1024,
822 abs->swap_cached / 1024);
825 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
826 int *voff, unsigned int idx, char *tag)
828 int max_padd = padding_from_num(get_number_cpus());
829 double usr = 0.0, sys = 0.0, idl = 0.0, iow = 0.0;
830 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
831 rel->cpu_idle[idx] + rel->cpu_iow[idx];
833 if (all > 0) {
834 usr = 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all;
835 sys = 100.0 * rel->cpu_sys[idx] / all;
836 idl = 100.0 * rel->cpu_idle[idx] / all;
837 iow = 100.0 * rel->cpu_iow[idx] / all;
840 mvwprintw(screen, (*voff)++, 2,
841 "cpu%*d %s: %11.1lf%% usr/t "
842 "%9.1lf%% sys/t "
843 "%10.1lf%% idl/t "
844 "%11.1lf%% iow/t",
845 max_padd, idx, tag, usr, sys, idl, iow);
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;
864 double usr = 0.0, sys = 0.0, idl = 0.0, iow = 0.0;
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 if (all > 0) {
885 usr = 100.0 * (avg->cpu_user + avg->cpu_nice) / all;
886 sys = 100.0 * avg->cpu_sys / all;
887 idl = 100.0 * avg->cpu_idle /all;
888 iow = 100.0 * avg->cpu_iow / all;
890 mvwprintw(screen, (*voff)++, 2,
891 "avg:%*s%14.1lf%% "
892 "%9.1lf%% "
893 "%10.1lf%% "
894 "%11.1lf%%", max_padd, "", usr, sys, idl, iow);
896 if (show_median) {
897 long double m_cpu_user, m_cpu_nice, m_cpu_sys, m_cpu_idle, m_cpu_iow;
898 long double m_all;
899 long double m_usr = 0.0, m_sys = 0.0, m_idl = 0.0, m_iow = 0.0;
901 i = cpu_hits[cpus / 2].idx;
902 if (cpus % 2 == 0) {
903 /* take the mean of the 2 middle entries */
904 int j = cpu_hits[(cpus / 2) - 1].idx;
906 MEDIAN_EVEN(cpu_user);
907 MEDIAN_EVEN(cpu_nice);
908 MEDIAN_EVEN(cpu_sys);
909 MEDIAN_EVEN(cpu_idle);
910 MEDIAN_EVEN(cpu_iow);
911 } else {
912 /* take the middle entry as is */
913 MEDIAN_ODD(cpu_user);
914 MEDIAN_ODD(cpu_nice);
915 MEDIAN_ODD(cpu_sys);
916 MEDIAN_ODD(cpu_idle);
917 MEDIAN_ODD(cpu_iow);
920 m_all = m_cpu_user + m_cpu_sys + m_cpu_nice + m_cpu_idle + m_cpu_iow;
921 if (m_all > 0.0) {
922 m_usr = 100.0 * (m_cpu_user + m_cpu_nice) / m_all;
923 m_sys = 100.0 * m_cpu_sys / m_all;
924 m_idl = 100.0 * m_cpu_idle /m_all;
925 m_iow = 100.0 * m_cpu_iow / m_all;
927 mvwprintw(screen, (*voff)++, 2,
928 "med:%*s%14.1Lf%% "
929 "%9.1Lf%% "
930 "%10.1Lf%% "
931 "%11.1Lf%%", max_padd, "", m_usr, m_sys, m_idl, m_iow);
935 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
936 int *voff, unsigned int idx, char *tag)
938 int max_padd = padding_from_num(get_number_cpus());
940 mvwprintw(screen, (*voff)++, 2,
941 "cpu%*d %s: %12llu irqs/t "
942 "%17llu sirq rx/t "
943 "%17llu sirq tx/t",
944 max_padd, idx, tag,
945 rel->irqs[idx],
946 rel->irqs_srx[idx],
947 rel->irqs_stx[idx]);
950 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
951 const struct avg_stat *avg,
952 unsigned int top_cpus, int *voff)
954 unsigned int i;
955 unsigned int cpus = get_number_cpus();
956 int max_padd = padding_from_num(cpus);
958 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
960 if (top_cpus == cpus)
961 top_cpus--;
963 for (i = 1; i < top_cpus; ++i)
964 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
966 if (cpus != 1)
967 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
969 mvwprintw(screen, (*voff)++, 2,
970 "avg:%*s%17.1Lf "
971 "%17.1Lf "
972 "%17.1Lf", max_padd, "",
973 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
975 if (show_median) {
976 long double m_irqs, m_irqs_srx, m_irqs_stx;
978 i = cpu_hits[cpus / 2].idx;
979 if (cpus % 2 == 0) {
980 /* take the mean of the 2 middle entries */
981 int j = cpu_hits[(cpus / 2) - 1].idx;
983 MEDIAN_EVEN(irqs);
984 MEDIAN_EVEN(irqs_srx);
985 MEDIAN_EVEN(irqs_stx);
986 } else {
987 /* take the middle entry as is */
988 MEDIAN_ODD(irqs);
989 MEDIAN_ODD(irqs_srx);
990 MEDIAN_ODD(irqs_stx);
993 mvwprintw(screen, (*voff)++, 2,
994 "med:%*s%17.1Lf "
995 "%17.1Lf "
996 "%17.1Lf", max_padd, "",
997 m_irqs, m_irqs_srx, m_irqs_stx);
1001 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
1002 int *voff, unsigned int idx, char *tag)
1004 int max_padd = padding_from_num(get_number_cpus());
1006 mvwprintw(screen, (*voff)++, 2,
1007 "cpu%*d %s: %12llu irqs",
1008 max_padd, idx, tag, abs->irqs[idx]);
1011 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
1012 const struct avg_stat *avg,
1013 unsigned int top_cpus, int *voff)
1015 unsigned int i;
1016 unsigned int cpus = get_number_cpus();
1017 int max_padd = padding_from_num(cpus);
1019 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
1021 if (top_cpus == cpus)
1022 top_cpus--;
1024 for (i = 1; i < top_cpus; ++i)
1025 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
1027 if (cpus != 1)
1028 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
1030 mvwprintw(screen, (*voff)++, 2,
1031 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
1033 if (show_median) {
1034 long double m_irqs;
1036 i = cpu_hits[cpus / 2].idx;
1037 if (cpus % 2 == 0) {
1038 /* take the mean of the 2 middle entries */
1039 int j = cpu_hits[(cpus / 2) - 1].idx;
1041 m_irqs = (abs->irqs[i] + abs->irqs[j]) / 2;
1042 } else {
1043 /* take the middle entry as is */
1044 m_irqs = abs->irqs[i];
1047 mvwprintw(screen, (*voff)++, 2,
1048 "med:%*s%17.1Lf", max_padd, "", m_irqs);
1052 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
1053 const struct ifstat *abs, int *voff)
1055 if (iswireless(abs)) {
1056 mvwprintw(screen, (*voff)++, 2,
1057 "linkqual: %7d/%d (%d/t) ",
1058 abs->wifi.link_qual,
1059 abs->wifi.link_qual_max,
1060 rel->wifi.link_qual);
1062 mvwprintw(screen, (*voff)++, 2,
1063 "signal: %8d dBm (%d dBm/t) ",
1064 abs->wifi.signal_level,
1065 rel->wifi.signal_level);
1069 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
1070 const struct ifstat *abs, const struct avg_stat *avg,
1071 int *first, uint64_t ms_interval, unsigned int top_cpus,
1072 bool need_info)
1074 unsigned int cpus, top;
1075 int voff = 1, cvoff = 2;
1076 uint32_t rate = device_bitrate(ifname);
1078 curs_set(0);
1080 cpus = get_number_cpus();
1081 top = min(cpus, top_cpus);
1083 stats_top(rel, abs, cpus);
1085 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
1087 screen_header(screen, ifname, &voff, rate, ms_interval, top_cpus);
1089 voff++;
1090 screen_net_dev_rel(screen, rel, &voff);
1092 if (show_percentage) {
1093 voff++;
1094 screen_net_dev_percentage(screen, rel, &voff, rate);
1097 voff++;
1098 screen_net_dev_abs(screen, abs, &voff);
1100 voff++;
1101 screen_sys(screen, rel, abs, &voff);
1103 voff++;
1104 screen_mem_swap(screen, abs, &voff);
1106 voff++;
1107 screen_percpu_states(screen, rel, avg, top, &voff);
1109 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
1111 voff++;
1112 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
1114 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
1116 voff++;
1117 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
1119 voff++;
1120 screen_wireless(screen, rel, abs, &voff);
1122 if (*first) {
1123 mvwprintw(screen, cvoff, 2, "Collecting data ...");
1124 *first = 0;
1125 } else {
1126 if (need_info)
1127 mvwprintw(screen, cvoff, 2, "(consider to increase "
1128 "your sampling interval, e.g. -t %d)",
1129 rate > SPEED_1000 ? 10000 : 1000);
1130 else
1131 mvwprintw(screen, cvoff, 2, " "
1132 " ");
1135 wrefresh(screen);
1136 refresh();
1139 static void on_panic_handler(void *arg __maybe_unused)
1141 screen_end();
1142 fprintf(stderr, "Please check <stderr> for error message\n");
1145 static int screen_main(const char *ifname, uint64_t ms_interval,
1146 unsigned int top_cpus, bool suppress_warnings,
1147 bool omit_header __maybe_unused)
1149 int first = 1, key;
1150 uint32_t rate = device_bitrate(ifname);
1151 bool need_info = false;
1153 stats_screen = screen_init(true);
1155 panic_handler_add(on_panic_handler, NULL);
1157 if (((rate > SPEED_1000 && ms_interval <= 10000) ||
1158 (rate == SPEED_1000 && ms_interval < 1000)) &&
1159 !suppress_warnings)
1160 need_info = true;
1162 while (!sigint) {
1163 key = getch();
1164 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1165 break;
1167 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1168 &first, ms_interval, top_cpus, need_info);
1170 stats_sample_generic(ifname, ms_interval);
1173 screen_end();
1175 return 0;
1178 static void term_csv(const struct ifstat *rel, const struct ifstat *abs)
1180 unsigned int cpus, i;
1182 printf("%ld ", time(NULL));
1184 printf("%llu ", rel->rx_bytes);
1185 printf("%llu ", rel->rx_packets);
1186 printf("%llu ", rel->rx_drops);
1187 printf("%llu ", rel->rx_errors);
1189 printf("%llu ", abs->rx_bytes);
1190 printf("%llu ", abs->rx_packets);
1191 printf("%llu ", abs->rx_drops);
1192 printf("%llu ", abs->rx_errors);
1194 printf("%llu ", rel->tx_bytes);
1195 printf("%llu ", rel->tx_packets);
1196 printf("%llu ", rel->tx_drops);
1197 printf("%llu ", rel->tx_errors);
1199 printf("%llu ", abs->tx_bytes);
1200 printf("%llu ", abs->tx_packets);
1201 printf("%llu ", abs->tx_drops);
1202 printf("%llu ", abs->tx_errors);
1204 printf("%"PRIu32" ", rel->cswitch);
1205 printf("%"PRIu64" ", abs->mem_free);
1206 printf("%"PRIu64" ", abs->mem_total - abs->mem_free);
1207 printf("%"PRIu64" ", abs->mem_total);
1208 printf("%"PRIu64" ", abs->swap_free);
1209 printf("%"PRIu64" ", abs->swap_total - abs->swap_free);
1210 printf("%"PRIu64" ", abs->swap_total);
1211 printf("%"PRIu32" ", abs->procs_total);
1212 printf("%"PRIu32" ", abs->procs_run);
1213 printf("%"PRIu32" ", abs->procs_iow);
1215 cpus = get_number_cpus();
1217 for (i = 0; i < cpus; ++i) {
1218 printf("%"PRIu64" ", rel->cpu_user[i]);
1219 printf("%"PRIu64" ", rel->cpu_nice[i]);
1220 printf("%"PRIu64" ", rel->cpu_sys[i]);
1221 printf("%"PRIu64" ", rel->cpu_idle[i]);
1222 printf("%"PRIu64" ", rel->cpu_iow[i]);
1224 printf("%llu ", rel->irqs[i]);
1225 printf("%llu ", abs->irqs[i]);
1227 printf("%llu ", rel->irqs_srx[i]);
1228 printf("%llu ", abs->irqs_srx[i]);
1230 printf("%llu ", rel->irqs_stx[i]);
1231 printf("%llu ", abs->irqs_stx[i]);
1234 if (iswireless(abs)) {
1235 printf("%"PRIu16" ", rel->wifi.link_qual);
1236 printf("%"PRIu16" ", abs->wifi.link_qual);
1237 printf("%"PRIu16" ", abs->wifi.link_qual_max);
1239 printf("%d ", rel->wifi.signal_level);
1240 printf("%d ", abs->wifi.signal_level);
1243 puts("");
1244 fflush(stdout);
1247 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1248 uint64_t ms_interval)
1250 unsigned int cpus, i, j = 1;
1252 printf("# gnuplot dump (#col:description)\n");
1253 printf("# networking interface: %s\n", ifname);
1254 printf("# sampling interval (t): %"PRIu64" ms\n", ms_interval);
1255 printf("# %d:unixtime ", j++);
1257 printf("%d:rx-bytes-per-t ", j++);
1258 printf("%d:rx-pkts-per-t ", j++);
1259 printf("%d:rx-drops-per-t ", j++);
1260 printf("%d:rx-errors-per-t ", j++);
1262 printf("%d:rx-bytes ", j++);
1263 printf("%d:rx-pkts ", j++);
1264 printf("%d:rx-drops ", j++);
1265 printf("%d:rx-errors ", j++);
1267 printf("%d:tx-bytes-per-t ", j++);
1268 printf("%d:tx-pkts-per-t ", j++);
1269 printf("%d:tx-drops-per-t ", j++);
1270 printf("%d:tx-errors-per-t ", j++);
1272 printf("%d:tx-bytes ", j++);
1273 printf("%d:tx-pkts ", j++);
1274 printf("%d:tx-drops ", j++);
1275 printf("%d:tx-errors ", j++);
1277 printf("%d:context-switches-per-t ", j++);
1278 printf("%d:mem-free ", j++);
1279 printf("%d:mem-used ", j++);
1280 printf("%d:mem-total ", j++);
1281 printf("%d:swap-free ", j++);
1282 printf("%d:swap-used ", j++);
1283 printf("%d:swap-total ", j++);
1284 printf("%d:procs-total ", j++);
1285 printf("%d:procs-in-run ", j++);
1286 printf("%d:procs-in-iow ", j++);
1288 cpus = get_number_cpus();
1290 for (i = 0; i < cpus; ++i) {
1291 printf("%d:cpu%i-usr-per-t ", j++, i);
1292 printf("%d:cpu%i-nice-per-t ", j++, i);
1293 printf("%d:cpu%i-sys-per-t ", j++, i);
1294 printf("%d:cpu%i-idle-per-t ", j++, i);
1295 printf("%d:cpu%i-iow-per-t ", j++, i);
1297 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1298 printf("%d:cpu%i-net-irqs ", j++, i);
1300 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1301 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1302 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1303 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1306 if (iswireless(abs)) {
1307 printf("%d:wifi-link-qual-per-t ", j++);
1308 printf("%d:wifi-link-qual ", j++);
1309 printf("%d:wifi-link-qual-max ", j++);
1311 printf("%d:wifi-signal-dbm-per-t ", j++);
1312 printf("%d:wifi-signal-dbm ", j++);
1315 puts("");
1316 printf("# data:\n");
1317 fflush(stdout);
1320 static int term_main(const char *ifname, uint64_t ms_interval,
1321 unsigned int top_cpus __maybe_unused,
1322 bool suppress_warnings __maybe_unused,
1323 bool omit_header)
1325 do {
1326 stats_sample_generic(ifname, ms_interval);
1328 if (!omit_header) {
1329 omit_header = true;
1330 term_csv_header(ifname, &stats_new, ms_interval);
1333 term_csv(&stats_delta, &stats_new);
1334 } while (stats_loop && !sigint);
1336 return 0;
1339 int main(int argc, char **argv)
1341 short ifflags = 0;
1342 int c, ret, promisc = 0;
1343 unsigned int cpus, top_cpus = 5;
1344 uint64_t interval = 1000;
1345 char *ifname = NULL;
1346 bool suppress_warnings = false;
1347 bool omit_header = false;
1348 int (*func_main)(const char *ifname, uint64_t ms_interval,
1349 unsigned int top_cpus, bool suppress_warnings,
1350 bool omit_header);
1352 func_main = screen_main;
1354 setfsuid(getuid());
1355 setfsgid(getgid());
1357 while ((c = getopt_long(argc, argv, short_options, long_options,
1358 NULL)) != EOF) {
1359 switch (c) {
1360 case 'h':
1361 help();
1362 break;
1363 case 'v':
1364 version();
1365 break;
1366 case 'W':
1367 suppress_warnings = true;
1368 break;
1369 case 'd':
1370 ifname = xstrndup(optarg, IFNAMSIZ);
1371 break;
1372 case 't':
1373 interval = strtoul(optarg, NULL, 10);
1374 break;
1375 case 'n':
1376 top_cpus = strtoul(optarg, NULL, 10);
1377 if (top_cpus < 1)
1378 panic("Number of top hitter CPUs must be greater than 0");
1379 break;
1380 case 'l':
1381 stats_loop = 1;
1382 break;
1383 case 'p':
1384 promisc = 1;
1385 break;
1386 case 'P':
1387 show_percentage = 1;
1388 break;
1389 case 'm':
1390 show_median = 1;
1391 break;
1392 case 'c':
1393 func_main = term_main;
1394 break;
1395 case 'o':
1396 omit_header = true;
1397 break;
1398 case '?':
1399 switch (optopt) {
1400 case 'd':
1401 case 't':
1402 panic("Option -%c requires an argument!\n",
1403 optopt);
1404 default:
1405 if (isprint(optopt))
1406 printf("Unknown option character `0x%X\'!\n", optopt);
1407 die();
1409 default:
1410 break;
1414 if (argc == 1)
1415 help();
1417 if (argc == 2)
1418 ifname = xstrndup(argv[1], IFNAMSIZ);
1419 if (ifname == NULL)
1420 panic("No networking device given!\n");
1422 if (!strncmp("lo", ifname, IFNAMSIZ))
1423 panic("lo is not supported!\n");
1424 if (device_mtu(ifname) == 0)
1425 panic("This is no networking device!\n");
1427 register_signal(SIGINT, signal_handler);
1428 register_signal(SIGQUIT, signal_handler);
1429 register_signal(SIGSTOP, signal_handler);
1430 register_signal(SIGHUP, signal_handler);
1432 cpus = get_number_cpus();
1433 top_cpus = min(top_cpus, cpus);
1434 if (uname(&uts) < 0)
1435 panic("Cannot execute uname!\n");
1437 stats_alloc(&stats_old, cpus);
1438 stats_alloc(&stats_new, cpus);
1439 stats_alloc(&stats_delta, cpus);
1441 cpu_hits = xcalloc(cpus, sizeof(*cpu_hits));
1443 if (promisc)
1444 ifflags = device_enter_promiscuous_mode(ifname);
1445 ret = func_main(ifname, interval, top_cpus, suppress_warnings, omit_header);
1446 if (promisc)
1447 device_leave_promiscuous_mode(ifname, ifflags);
1449 stats_release(&stats_old);
1450 stats_release(&stats_new);
1451 stats_release(&stats_delta);
1453 xfree(ifname);
1454 return ret;