trafgen: proto: Increment proto field at runtime
[netsniff-ng-new.git] / ifpps.c
blobdf6fe422982234e26db5939532e9d7eeb69d3d68
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 = "Please report bugs to <netsniff-ng@googlegroups.com>\n"
97 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
98 "Swiss federal institute of technology (ETH Zurich)\n"
99 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
100 "License: GNU GPL version 2.0\n"
101 "This is free software: you are free to change and redistribute it.\n"
102 "There is NO WARRANTY, to the extent permitted by law.";
104 static void signal_handler(int number)
106 switch (number) {
107 case SIGINT:
108 case SIGQUIT:
109 case SIGTERM:
110 sigint = 1;
111 break;
112 case SIGHUP:
113 default:
114 break;
118 static inline int iswireless(const struct ifstat *stats)
120 return stats->wifi.bitrate > 0;
123 static void __noreturn help(void)
125 printf("ifpps %s, top-like kernel networking and system statistics\n",
126 VERSION_STRING);
127 puts("http://www.netsniff-ng.org\n\n"
128 "Usage: ifpps [options] || ifpps <netdev>\n"
129 "Options:\n"
130 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
131 " -n|--num-cpus <num> Number of top hitter CPUs in ncurses mode (def: 5)\n"
132 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
133 " -c|--csv Output to terminal as Gnuplot-ready data\n"
134 " -l|--loop Continuous CSV output\n"
135 " -m|--median Display median values\n"
136 " -o|--omit-header Do not print the CSV header\n"
137 " -p|--promisc Promiscuous mode\n"
138 " -P|--percentage Show percentage of theoretical line rate\n"
139 " -W|--no-warn Suppress warnings\n"
140 " -v|--version Print version and exit\n"
141 " -h|--help Print this help and exit\n\n"
142 "Examples:\n"
143 " ifpps eth0\n"
144 " ifpps -pd eth0\n"
145 " ifpps -lpcd wlan0 > plot.dat\n\n"
146 "Note:\n"
147 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
148 " Thus, in those situations, it's good to use a -t of 10sec.\n");
149 puts(copyright);
150 die();
153 static void __noreturn version(void)
155 printf("ifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
156 puts("top-like kernel networking and system statistics\n"
157 "http://www.netsniff-ng.org\n");
158 puts(copyright);
159 die();
162 static inline int padding_from_num(int n)
164 int i = 0;
165 do {
166 i++;
167 } while ((n /= 10) > 0);
168 return i;
171 #define STATS_ALLOC1(member) \
172 do { stats->member = xcalloc(cpus, sizeof(*(stats->member))); } while (0)
174 static void stats_alloc(struct ifstat *stats, unsigned int cpus)
176 STATS_ALLOC1(irqs);
177 STATS_ALLOC1(irqs_srx);
178 STATS_ALLOC1(irqs_stx);
180 STATS_ALLOC1(cpu_user);
181 STATS_ALLOC1(cpu_sys);
182 STATS_ALLOC1(cpu_nice);
183 STATS_ALLOC1(cpu_idle);
184 STATS_ALLOC1(cpu_iow);
187 #define STATS_ZERO1(member) \
188 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
190 static void stats_zero(struct ifstat *stats, unsigned int cpus)
192 /* Only clear the non-pointer members */
193 memset(stats, 0, offsetof(struct ifstat, irqs));
195 STATS_ZERO1(irqs);
196 STATS_ZERO1(irqs_srx);
197 STATS_ZERO1(irqs_stx);
199 STATS_ZERO1(cpu_user);
200 STATS_ZERO1(cpu_sys);
201 STATS_ZERO1(cpu_nice);
202 STATS_ZERO1(cpu_idle);
203 STATS_ZERO1(cpu_iow);
206 #define STATS_RELEASE(member) \
207 do { xfree(stats->member); } while (0)
209 static void stats_release(struct ifstat *stats)
211 STATS_RELEASE(irqs);
212 STATS_RELEASE(irqs_srx);
213 STATS_RELEASE(irqs_stx);
215 STATS_RELEASE(cpu_user);
216 STATS_RELEASE(cpu_sys);
217 STATS_RELEASE(cpu_nice);
218 STATS_RELEASE(cpu_idle);
219 STATS_RELEASE(cpu_iow);
222 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
224 int ret = -EINVAL;
225 char buff[256];
226 char *ifname_colon;
227 FILE *fp;
229 fp = fopen("/proc/net/dev", "r");
230 if (!fp)
231 panic("Cannot open /proc/net/dev!\n");
233 ifname_colon = xstrndup(ifname, strlen(ifname) + 2);
234 ifname_colon[strlen(ifname)] = ':';
235 ifname_colon[strlen(ifname) + 1] = '\0';
237 if (fgets(buff, sizeof(buff), fp)) { ; }
238 if (fgets(buff, sizeof(buff), fp)) { ; }
240 memset(buff, 0, sizeof(buff));
242 while (fgets(buff, sizeof(buff), fp) != NULL) {
243 buff[sizeof(buff) - 1] = 0;
245 if (strstr(buff, ifname_colon) == NULL)
246 continue;
248 if (sscanf(buff, "%*[a-z0-9_ .-]:%llu%llu%llu%llu%llu%llu"
249 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
250 &stats->rx_bytes, &stats->rx_packets,
251 &stats->rx_errors, &stats->rx_drops,
252 &stats->rx_fifo, &stats->rx_frame,
253 &stats->rx_multi, &stats->tx_bytes,
254 &stats->tx_packets, &stats->tx_errors,
255 &stats->tx_drops, &stats->tx_fifo,
256 &stats->tx_colls, &stats->tx_carrier) == 14) {
257 ret = 0;
258 break;
261 memset(buff, 0, sizeof(buff));
264 xfree(ifname_colon);
265 fclose(fp);
266 return ret;
269 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
271 int ret = -EINVAL, try = 0;
272 unsigned int i, cpus;
273 char *ptr, *buff;
274 bool seen = false;
275 size_t buff_len;
276 struct ethtool_drvinfo drvinf;
277 FILE *fp;
279 cpus = get_number_cpus();
280 buff_len = cpus * 128;
281 buff = xmalloc(buff_len);
283 fp = fopen("/proc/interrupts", "r");
284 if (!fp)
285 panic("Cannot open /proc/interrupts!\n");
286 retry:
287 fseek(fp, 0, SEEK_SET);
288 memset(buff, 0, buff_len);
290 while (fgets(buff, buff_len, fp) != NULL) {
291 buff[buff_len - 1] = 0;
293 if (strstr(buff, ifname) == NULL)
294 continue;
296 ptr = strchr(buff, ':');
297 if (!ptr)
298 continue;
299 ptr++;
301 for (i = 0; i < cpus && ptr; ++i) {
302 if (seen)
303 stats->irqs[i] += strtol(ptr, &ptr, 10);
304 else
305 stats->irqs[i] = strtol(ptr, &ptr, 10);
306 if (i == cpus - 1) {
307 ret = 0;
308 seen = true;
312 memset(buff, 0, buff_len);
315 if (ret == -EINVAL && try == 0) {
316 if (ethtool_drvinf(ifname, &drvinf) < 0)
317 goto done;
319 ifname = drvinf.driver;
320 try++;
322 goto retry;
324 done:
325 xfree(buff);
326 fclose(fp);
327 return ret;
330 static int stats_proc_softirqs(struct ifstat *stats)
332 unsigned int i, cpus;
333 char *ptr, *buff;
334 size_t buff_len;
335 FILE *fp;
336 enum {
337 softirqs_net_rx,
338 softirqs_net_tx,
339 } net_type;
341 fp = fopen("/proc/softirqs", "r");
342 if (!fp)
343 panic("Cannot open /proc/softirqs!\n");
345 cpus = get_number_cpus();
346 buff_len = cpus * 128;
347 buff = xmalloc(buff_len);
349 memset(buff, 0, buff_len);
351 while (fgets(buff, buff_len, fp) != NULL) {
352 buff[buff_len - 1] = 0;
354 if ((ptr = strstr(buff, "NET_TX:")))
355 net_type = softirqs_net_tx;
356 else if ((ptr = strstr(buff, "NET_RX:")))
357 net_type = softirqs_net_rx;
358 else
359 continue;
361 ptr += strlen("NET_xX:");
363 for (i = 0; i < cpus; ++i) {
364 switch (net_type) {
365 case softirqs_net_tx:
366 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
367 break;
368 case softirqs_net_rx:
369 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
370 break;
374 memset(buff, 0, buff_len);
377 xfree(buff);
378 fclose(fp);
379 return 0;
382 static int stats_proc_memory(struct ifstat *stats)
384 char *ptr, buff[256];
385 FILE *fp;
387 fp = fopen("/proc/meminfo", "r");
388 if (!fp)
389 panic("Cannot open /proc/meminfo!\n");
391 memset(buff, 0, sizeof(buff));
393 while (fgets(buff, sizeof(buff), fp) != NULL) {
394 buff[sizeof(buff) - 1] = 0;
396 if ((ptr = strstr(buff, "MemTotal:"))) {
397 ptr += strlen("MemTotal:");
398 stats->mem_total = strtoul(ptr, &ptr, 10);
399 } else if ((ptr = strstr(buff, "MemFree:"))) {
400 ptr += strlen("MemFree:");
401 stats->mem_free = strtoul(ptr, &ptr, 10);
402 } else if ((ptr = strstr(buff, "Active:"))) {
403 ptr += strlen("Active:");
404 stats->mem_active = strtoul(ptr, &ptr, 10);
405 } else if ((ptr = strstr(buff, "Inactive:"))) {
406 ptr += strlen("Inactive:");
407 stats->mem_inactive = strtoul(ptr, &ptr, 10);
408 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
409 ptr += strlen("SwapTotal:");
410 stats->swap_total = strtoul(ptr, &ptr, 10);
411 } else if ((ptr = strstr(buff, "SwapFree:"))) {
412 ptr += strlen("SwapFree:");
413 stats->swap_free = strtoul(ptr, &ptr, 10);
414 } else if ((ptr = strstr(buff, "SwapCached:"))) {
415 ptr += strlen("SwapCached:");
416 stats->swap_cached = strtoul(ptr, &ptr, 10);
419 memset(buff, 0, sizeof(buff));
422 fclose(fp);
423 return 0;
426 static int stats_proc_system(struct ifstat *stats)
428 unsigned int cpu, cpus;
429 char *ptr, buff[256];
430 FILE *fp;
432 fp = fopen("/proc/stat", "r");
433 if (!fp)
434 panic("Cannot open /proc/stat!\n");
436 cpus = get_number_cpus();
438 memset(buff, 0, sizeof(buff));
440 while (fgets(buff, sizeof(buff), fp) != NULL) {
441 buff[sizeof(buff) - 1] = 0;
443 if ((ptr = strstr(buff, "cpu"))) {
444 ptr += strlen("cpu");
445 if (isblank(*ptr))
446 goto next;
448 cpu = strtol(ptr, &ptr, 10);
449 bug_on(cpu > cpus);
451 if (sscanf(ptr, "%"SCNu64"%"SCNu64"%"SCNu64"%"SCNu64"%"SCNu64,
452 &stats->cpu_user[cpu],
453 &stats->cpu_nice[cpu],
454 &stats->cpu_sys[cpu],
455 &stats->cpu_idle[cpu],
456 &stats->cpu_iow[cpu]) != 5)
457 goto next;
458 } else if ((ptr = strstr(buff, "ctxt"))) {
459 ptr += strlen("ctxt");
460 stats->cswitch = strtoul(ptr, &ptr, 10);
461 } else if ((ptr = strstr(buff, "procs_running"))) {
462 ptr += strlen("procs_running");
463 stats->procs_run = strtoul(ptr, &ptr, 10);
464 } else if ((ptr = strstr(buff, "procs_blocked"))) {
465 ptr += strlen("procs_blocked");
466 stats->procs_iow = strtoul(ptr, &ptr, 10);
468 next:
469 memset(buff, 0, sizeof(buff));
472 fclose(fp);
473 return 0;
476 static int stats_proc_procs(struct ifstat *stats)
478 DIR *dir;
479 struct dirent *e;
481 dir = opendir("/proc");
482 if (!dir)
483 panic("Cannot open /proc\n");
485 stats->procs_total = 0;
487 while ((e = readdir(dir)) != NULL) {
488 const char *name = e->d_name;
489 char *end;
490 unsigned int pid = strtoul(name, &end, 10);
492 /* not a number */
493 if (pid == 0 && end == name)
494 continue;
496 stats->procs_total++;
499 closedir(dir);
501 return 0;
504 static int adjust_dbm_level(int in_dbm, int dbm_val)
506 if (!in_dbm)
507 return dbm_val;
509 return dbm_val - 0x100;
512 static int stats_wireless(const char *ifname, struct ifstat *stats)
514 int ret;
515 struct iw_statistics ws;
517 ret = wireless_sigqual(ifname, &ws);
518 if (ret != 0) {
519 stats->wifi.bitrate = 0;
520 return -EINVAL;
523 stats->wifi.bitrate = wireless_bitrate(ifname);
525 stats->wifi.signal_level =
526 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
528 stats->wifi.link_qual = ws.qual.qual;
529 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
531 return ret;
534 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
535 #define DIFF(member) do { \
536 if (sizeof(diff->member) != sizeof(new->member) || \
537 sizeof(diff->member) != sizeof(old->member)) \
538 bug(); \
539 if ((new->member - old->member) > (new->member)) { \
540 diff->member = 0; \
541 } else { \
542 DIFF1(member); \
544 } while (0)
546 static void stats_diff(struct ifstat *old, struct ifstat *new,
547 struct ifstat *diff)
549 unsigned int cpus, i;
551 DIFF(rx_bytes);
552 DIFF(rx_packets);
553 DIFF(rx_drops);
554 DIFF(rx_errors);
555 DIFF(rx_fifo);
556 DIFF(rx_frame);
557 DIFF(rx_multi);
559 DIFF(tx_bytes);
560 DIFF(tx_packets);
561 DIFF(tx_drops);
562 DIFF(tx_errors);
563 DIFF(tx_fifo);
564 DIFF(tx_colls);
565 DIFF(tx_carrier);
567 DIFF1(wifi.signal_level);
568 DIFF1(wifi.link_qual);
570 DIFF1(cswitch);
572 cpus = get_number_cpus();
574 for (i = 0; i < cpus; ++i) {
575 DIFF(irqs[i]);
576 DIFF(irqs_srx[i]);
577 DIFF(irqs_stx[i]);
579 DIFF1(cpu_user[i]);
580 DIFF1(cpu_nice[i]);
581 DIFF1(cpu_sys[i]);
582 DIFF1(cpu_idle[i]);
583 DIFF1(cpu_iow[i]);
587 static void stats_fetch(const char *ifname, struct ifstat *stats)
589 if (stats_proc_net_dev(ifname, stats) < 0)
590 panic("Cannot fetch device stats!\n");
591 if (stats_proc_softirqs(stats) < 0)
592 panic("Cannot fetch software interrupts!\n");
593 if (stats_proc_memory(stats) < 0)
594 panic("Cannot fetch memory stats!\n");
595 if (stats_proc_system(stats) < 0)
596 panic("Cannot fetch system stats!\n");
597 if (stats_proc_procs(stats) < 0)
598 panic("Cannot fetch process stats!\n");
600 stats_proc_interrupts((char *) ifname, stats);
602 stats_wireless(ifname, stats);
605 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
607 unsigned int cpus = get_number_cpus();
609 stats_zero(&stats_old, cpus);
610 stats_zero(&stats_new, cpus);
611 stats_zero(&stats_delta, cpus);
613 stats_fetch(ifname, &stats_old);
614 usleep(ms_interval * 1000);
615 stats_fetch(ifname, &stats_new);
617 stats_diff(&stats_old, &stats_new, &stats_delta);
620 static int cmp_hits(const void *p1, const void *p2)
622 const struct cpu_hit *h1 = p1, *h2 = p2;
625 * We want the hits sorted in descending order, thus reverse the return
626 * values.
628 if (h1->hit == h2->hit)
629 return 0;
630 else if (h1->hit < h2->hit)
631 return 1;
632 else
633 return -1;
636 static int cmp_irqs_rel(const void *p1, const void *p2)
638 const struct cpu_hit *h1 = p1, *h2 = p2;
641 * We want the hits sorted in descending order, thus reverse the return
642 * values.
644 if (h1->irqs_rel == h2->irqs_rel)
645 return 0;
646 else if (h1->irqs_rel < h2->irqs_rel)
647 return 1;
648 else
649 return -1;
652 static int cmp_irqs_abs(const void *p1, const void *p2)
654 const struct cpu_hit *h1 = p1, *h2 = p2;
657 * We want the hits sorted in descending order, thus reverse the return
658 * values.
660 if (h1->irqs_abs == h2->irqs_abs)
661 return 0;
662 else if (h1->irqs_abs < h2->irqs_abs)
663 return 1;
664 else
665 return -1;
668 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
669 unsigned int cpus)
671 unsigned int i;
673 memset(&stats_avg, 0, sizeof(stats_avg));
675 for (i = 0; i < cpus; ++i) {
676 cpu_hits[i].idx = i;
677 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
678 cpu_hits[i].irqs_rel = rel->irqs[i];
679 cpu_hits[i].irqs_abs = abs->irqs[i];
681 stats_avg.cpu_user += rel->cpu_user[i];
682 stats_avg.cpu_sys += rel->cpu_sys[i];
683 stats_avg.cpu_nice += rel->cpu_nice[i];
684 stats_avg.cpu_idle += rel->cpu_idle[i];
685 stats_avg.cpu_iow += rel->cpu_iow[i];
687 stats_avg.irqs_abs += abs->irqs[i];
688 stats_avg.irqs_rel += rel->irqs[i];
689 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
690 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
693 stats_avg.cpu_user /= cpus;
694 stats_avg.cpu_sys /= cpus;
695 stats_avg.cpu_nice /= cpus;
696 stats_avg.cpu_idle /= cpus;
697 stats_avg.cpu_iow /= cpus;
698 stats_avg.irqs_abs /= cpus;
699 stats_avg.irqs_rel /= cpus;
700 stats_avg.irqs_srx_rel /= cpus;
701 stats_avg.irqs_stx_rel /= cpus;
704 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
705 u32 rate, uint64_t ms_interval, unsigned int top_cpus)
707 size_t len = 0;
708 char buff[64], machine[64];
709 struct ethtool_drvinfo drvinf;
710 int link = ethtool_link(ifname);
711 unsigned int cpus = get_number_cpus();
713 ethtool_drvinf(ifname, &drvinf);
715 memset(buff, 0, sizeof(buff));
716 memset(machine, 0, sizeof(machine));
718 if (rate)
719 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
720 if (link >= 0)
721 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
722 link == 0 ? "no" : "yes");
724 if (!strstr(uts.release, uts.machine))
725 slprintf(machine, sizeof(machine), " %s,", uts.machine);
727 mvwprintw(screen, (*voff)++, 2,
728 "%s,%s %s (%s%s), t=%"PRIu64"ms, cpus=%u%s/%u"
729 " ", uts.release, machine,
730 ifname, drvinf.driver, buff, ms_interval, top_cpus,
731 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
734 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
735 int *voff)
737 attron(A_REVERSE);
739 mvwprintw(screen, (*voff)++, 0,
740 " rx: %16.3llf MiB/t "
741 "%10llu pkts/t "
742 "%10llu drops/t "
743 "%10llu errors/t ",
744 ((long double) rel->rx_bytes) / (1LLU << 20),
745 rel->rx_packets, rel->rx_drops, rel->rx_errors);
747 mvwprintw(screen, (*voff)++, 0,
748 " tx: %16.3llf MiB/t "
749 "%10llu pkts/t "
750 "%10llu drops/t "
751 "%10llu errors/t ",
752 ((long double) rel->tx_bytes) / (1LLU << 20),
753 rel->tx_packets, rel->tx_drops, rel->tx_errors);
755 attroff(A_REVERSE);
758 static void screen_net_dev_percentage(WINDOW *screen, const struct ifstat *rel,
759 int *voff, u32 rate)
761 mvwprintw(screen, (*voff)++, 0,
762 " rx: %15.2llf%% of line rate "
763 " ",
764 rate ? ((((long double) rel->rx_bytes) / 125000) / rate) * 100.0 : 0.0);
766 mvwprintw(screen, (*voff)++, 0,
767 " tx: %15.2llf%% of line rate "
768 " ",
769 rate ? ((((long double) rel->tx_bytes) / 125000) / rate) * 100.0 : 0.0);
772 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
773 int *voff)
775 mvwprintw(screen, (*voff)++, 2,
776 "rx: %16.3llf MiB "
777 "%10llu pkts "
778 "%10llu drops "
779 "%10llu errors",
780 ((long double) abs->rx_bytes) / (1LLU << 20),
781 abs->rx_packets, abs->rx_drops, abs->rx_errors);
783 mvwprintw(screen, (*voff)++, 2,
784 "tx: %16.3llf MiB "
785 "%10llu pkts "
786 "%10llu drops "
787 "%10llu errors",
788 ((long double) abs->tx_bytes) / (1LLU << 20),
789 abs->tx_packets, abs->tx_drops, abs->tx_errors);
792 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
793 const struct ifstat *abs, int *voff)
795 mvwprintw(screen, (*voff)++, 2,
796 "sys: %14"PRIu32" cs/t "
797 "%11"PRIu32" procs "
798 "%11"PRIu32" running "
799 "%10"PRIu32" iowait",
800 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
803 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
805 mvwprintw(screen, (*voff)++, 2,
806 "mem: %13"PRIu64"M total "
807 "%9"PRIu64"M used "
808 "%11"PRIu64"M active "
809 "%10"PRIu64"M inactive",
810 abs->mem_total / 1024,
811 (abs->mem_total - abs->mem_free) / 1024,
812 abs->mem_active / 1024,
813 abs->mem_inactive / 1024);
815 mvwprintw(screen, (*voff)++, 2,
816 "swap: %12"PRIu64"M total "
817 "%9uM used "
818 "%11uM cached",
819 abs->swap_total / 1024,
820 (abs->swap_total - abs->swap_free) / 1024,
821 abs->swap_cached / 1024);
824 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
825 int *voff, unsigned int idx, char *tag)
827 int max_padd = padding_from_num(get_number_cpus());
828 double usr = 0.0, sys = 0.0, idl = 0.0, iow = 0.0;
829 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
830 rel->cpu_idle[idx] + rel->cpu_iow[idx];
832 if (all > 0) {
833 usr = 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all;
834 sys = 100.0 * rel->cpu_sys[idx] / all;
835 idl = 100.0 * rel->cpu_idle[idx] / all;
836 iow = 100.0 * rel->cpu_iow[idx] / all;
839 mvwprintw(screen, (*voff)++, 2,
840 "cpu%*d %s: %11.1lf%% usr/t "
841 "%9.1lf%% sys/t "
842 "%10.1lf%% idl/t "
843 "%11.1lf%% iow/t",
844 max_padd, idx, tag, usr, sys, idl, iow);
847 #define MEDIAN_EVEN(member) do { \
848 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
849 } while (0)
851 #define MEDIAN_ODD(member) do { \
852 m_##member = rel->member[i]; \
853 } while (0)
855 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
856 const struct avg_stat *avg,
857 unsigned int top_cpus, int *voff)
859 unsigned int i;
860 unsigned int cpus = get_number_cpus();
861 int max_padd = padding_from_num(cpus);
862 uint64_t all;
863 double usr = 0.0, sys = 0.0, idl = 0.0, iow = 0.0;
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 if (all > 0) {
884 usr = 100.0 * (avg->cpu_user + avg->cpu_nice) / all;
885 sys = 100.0 * avg->cpu_sys / all;
886 idl = 100.0 * avg->cpu_idle /all;
887 iow = 100.0 * avg->cpu_iow / all;
889 mvwprintw(screen, (*voff)++, 2,
890 "avg:%*s%14.1lf%% "
891 "%9.1lf%% "
892 "%10.1lf%% "
893 "%11.1lf%%", max_padd, "", usr, sys, idl, iow);
895 if (show_median) {
896 long double m_cpu_user, m_cpu_nice, m_cpu_sys, m_cpu_idle, m_cpu_iow;
897 long double m_all;
898 long double m_usr = 0.0, m_sys = 0.0, m_idl = 0.0, m_iow = 0.0;
900 i = cpu_hits[cpus / 2].idx;
901 if (cpus % 2 == 0) {
902 /* take the mean of the 2 middle entries */
903 int j = cpu_hits[(cpus / 2) - 1].idx;
905 MEDIAN_EVEN(cpu_user);
906 MEDIAN_EVEN(cpu_nice);
907 MEDIAN_EVEN(cpu_sys);
908 MEDIAN_EVEN(cpu_idle);
909 MEDIAN_EVEN(cpu_iow);
910 } else {
911 /* take the middle entry as is */
912 MEDIAN_ODD(cpu_user);
913 MEDIAN_ODD(cpu_nice);
914 MEDIAN_ODD(cpu_sys);
915 MEDIAN_ODD(cpu_idle);
916 MEDIAN_ODD(cpu_iow);
919 m_all = m_cpu_user + m_cpu_sys + m_cpu_nice + m_cpu_idle + m_cpu_iow;
920 if (m_all > 0.0) {
921 m_usr = 100.0 * (m_cpu_user + m_cpu_nice) / m_all;
922 m_sys = 100.0 * m_cpu_sys / m_all;
923 m_idl = 100.0 * m_cpu_idle /m_all;
924 m_iow = 100.0 * m_cpu_iow / m_all;
926 mvwprintw(screen, (*voff)++, 2,
927 "med:%*s%14.1Lf%% "
928 "%9.1Lf%% "
929 "%10.1Lf%% "
930 "%11.1Lf%%", max_padd, "", m_usr, m_sys, m_idl, m_iow);
934 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
935 int *voff, unsigned int idx, char *tag)
937 int max_padd = padding_from_num(get_number_cpus());
939 mvwprintw(screen, (*voff)++, 2,
940 "cpu%*d %s: %12llu irqs/t "
941 "%17llu sirq rx/t "
942 "%17llu sirq tx/t",
943 max_padd, idx, tag,
944 rel->irqs[idx],
945 rel->irqs_srx[idx],
946 rel->irqs_stx[idx]);
949 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
950 const struct avg_stat *avg,
951 unsigned int top_cpus, int *voff)
953 unsigned int i;
954 unsigned int cpus = get_number_cpus();
955 int max_padd = padding_from_num(cpus);
957 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
959 if (top_cpus == cpus)
960 top_cpus--;
962 for (i = 1; i < top_cpus; ++i)
963 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
965 if (cpus != 1)
966 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
968 mvwprintw(screen, (*voff)++, 2,
969 "avg:%*s%17.1Lf "
970 "%17.1Lf "
971 "%17.1Lf", max_padd, "",
972 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
974 if (show_median) {
975 long double m_irqs, m_irqs_srx, m_irqs_stx;
977 i = cpu_hits[cpus / 2].idx;
978 if (cpus % 2 == 0) {
979 /* take the mean of the 2 middle entries */
980 int j = cpu_hits[(cpus / 2) - 1].idx;
982 MEDIAN_EVEN(irqs);
983 MEDIAN_EVEN(irqs_srx);
984 MEDIAN_EVEN(irqs_stx);
985 } else {
986 /* take the middle entry as is */
987 MEDIAN_ODD(irqs);
988 MEDIAN_ODD(irqs_srx);
989 MEDIAN_ODD(irqs_stx);
992 mvwprintw(screen, (*voff)++, 2,
993 "med:%*s%17.1Lf "
994 "%17.1Lf "
995 "%17.1Lf", max_padd, "",
996 m_irqs, m_irqs_srx, m_irqs_stx);
1000 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
1001 int *voff, unsigned int idx, char *tag)
1003 int max_padd = padding_from_num(get_number_cpus());
1005 mvwprintw(screen, (*voff)++, 2,
1006 "cpu%*d %s: %12llu irqs",
1007 max_padd, idx, tag, abs->irqs[idx]);
1010 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
1011 const struct avg_stat *avg,
1012 unsigned int top_cpus, int *voff)
1014 unsigned int i;
1015 unsigned int cpus = get_number_cpus();
1016 int max_padd = padding_from_num(cpus);
1018 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
1020 if (top_cpus == cpus)
1021 top_cpus--;
1023 for (i = 1; i < top_cpus; ++i)
1024 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
1026 if (cpus != 1)
1027 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
1029 mvwprintw(screen, (*voff)++, 2,
1030 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
1032 if (show_median) {
1033 long double m_irqs;
1035 i = cpu_hits[cpus / 2].idx;
1036 if (cpus % 2 == 0) {
1037 /* take the mean of the 2 middle entries */
1038 int j = cpu_hits[(cpus / 2) - 1].idx;
1040 m_irqs = (abs->irqs[i] + abs->irqs[j]) / 2;
1041 } else {
1042 /* take the middle entry as is */
1043 m_irqs = abs->irqs[i];
1046 mvwprintw(screen, (*voff)++, 2,
1047 "med:%*s%17.1Lf", max_padd, "", m_irqs);
1051 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
1052 const struct ifstat *abs, int *voff)
1054 if (iswireless(abs)) {
1055 mvwprintw(screen, (*voff)++, 2,
1056 "linkqual: %7d/%d (%d/t) ",
1057 abs->wifi.link_qual,
1058 abs->wifi.link_qual_max,
1059 rel->wifi.link_qual);
1061 mvwprintw(screen, (*voff)++, 2,
1062 "signal: %8d dBm (%d dBm/t) ",
1063 abs->wifi.signal_level,
1064 rel->wifi.signal_level);
1068 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
1069 const struct ifstat *abs, const struct avg_stat *avg,
1070 int *first, uint64_t ms_interval, unsigned int top_cpus,
1071 bool need_info)
1073 unsigned int cpus, top;
1074 int voff = 1, cvoff = 2;
1075 u32 rate = device_bitrate(ifname);
1077 curs_set(0);
1079 cpus = get_number_cpus();
1080 top = min(cpus, top_cpus);
1082 stats_top(rel, abs, cpus);
1084 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
1086 screen_header(screen, ifname, &voff, rate, ms_interval, top_cpus);
1088 voff++;
1089 screen_net_dev_rel(screen, rel, &voff);
1091 if (show_percentage) {
1092 voff++;
1093 screen_net_dev_percentage(screen, rel, &voff, rate);
1096 voff++;
1097 screen_net_dev_abs(screen, abs, &voff);
1099 voff++;
1100 screen_sys(screen, rel, abs, &voff);
1102 voff++;
1103 screen_mem_swap(screen, abs, &voff);
1105 voff++;
1106 screen_percpu_states(screen, rel, avg, top, &voff);
1108 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
1110 voff++;
1111 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
1113 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
1115 voff++;
1116 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
1118 voff++;
1119 screen_wireless(screen, rel, abs, &voff);
1121 if (*first) {
1122 mvwprintw(screen, cvoff, 2, "Collecting data ...");
1123 *first = 0;
1124 } else {
1125 if (need_info)
1126 mvwprintw(screen, cvoff, 2, "(consider to increase "
1127 "your sampling interval, e.g. -t %d)",
1128 rate > SPEED_1000 ? 10000 : 1000);
1129 else
1130 mvwprintw(screen, cvoff, 2, " "
1131 " ");
1134 wrefresh(screen);
1135 refresh();
1138 static void on_panic_handler(void *arg __maybe_unused)
1140 screen_end();
1141 fprintf(stderr, "Please check <stderr> for error message\n");
1144 static int screen_main(const char *ifname, uint64_t ms_interval,
1145 unsigned int top_cpus, bool suppress_warnings,
1146 bool omit_header __maybe_unused)
1148 int first = 1, key;
1149 u32 rate = device_bitrate(ifname);
1150 bool need_info = false;
1152 stats_screen = screen_init(true);
1154 panic_handler_add(on_panic_handler, NULL);
1156 if (((rate > SPEED_1000 && ms_interval <= 1000) ||
1157 (rate = SPEED_1000 && ms_interval < 1000)) &&
1158 !suppress_warnings)
1159 need_info = true;
1161 while (!sigint) {
1162 key = getch();
1163 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1164 break;
1166 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1167 &first, ms_interval, top_cpus, need_info);
1169 stats_sample_generic(ifname, ms_interval);
1172 screen_end();
1174 return 0;
1177 static void term_csv(const struct ifstat *rel, const struct ifstat *abs)
1179 unsigned int cpus, i;
1181 printf("%ld ", time(NULL));
1183 printf("%llu ", rel->rx_bytes);
1184 printf("%llu ", rel->rx_packets);
1185 printf("%llu ", rel->rx_drops);
1186 printf("%llu ", rel->rx_errors);
1188 printf("%llu ", abs->rx_bytes);
1189 printf("%llu ", abs->rx_packets);
1190 printf("%llu ", abs->rx_drops);
1191 printf("%llu ", abs->rx_errors);
1193 printf("%llu ", rel->tx_bytes);
1194 printf("%llu ", rel->tx_packets);
1195 printf("%llu ", rel->tx_drops);
1196 printf("%llu ", rel->tx_errors);
1198 printf("%llu ", abs->tx_bytes);
1199 printf("%llu ", abs->tx_packets);
1200 printf("%llu ", abs->tx_drops);
1201 printf("%llu ", abs->tx_errors);
1203 printf("%"PRIu32" ", rel->cswitch);
1204 printf("%"PRIu64" ", abs->mem_free);
1205 printf("%"PRIu64" ", abs->mem_total - abs->mem_free);
1206 printf("%"PRIu64" ", abs->mem_total);
1207 printf("%"PRIu64" ", abs->swap_free);
1208 printf("%"PRIu64" ", abs->swap_total - abs->swap_free);
1209 printf("%"PRIu64" ", abs->swap_total);
1210 printf("%"PRIu32" ", abs->procs_total);
1211 printf("%"PRIu32" ", abs->procs_run);
1212 printf("%"PRIu32" ", abs->procs_iow);
1214 cpus = get_number_cpus();
1216 for (i = 0; i < cpus; ++i) {
1217 printf("%"PRIu64" ", rel->cpu_user[i]);
1218 printf("%"PRIu64" ", rel->cpu_nice[i]);
1219 printf("%"PRIu64" ", rel->cpu_sys[i]);
1220 printf("%"PRIu64" ", rel->cpu_idle[i]);
1221 printf("%"PRIu64" ", rel->cpu_iow[i]);
1223 printf("%llu ", rel->irqs[i]);
1224 printf("%llu ", abs->irqs[i]);
1226 printf("%llu ", rel->irqs_srx[i]);
1227 printf("%llu ", abs->irqs_srx[i]);
1229 printf("%llu ", rel->irqs_stx[i]);
1230 printf("%llu ", abs->irqs_stx[i]);
1233 if (iswireless(abs)) {
1234 printf("%"PRIu16" ", rel->wifi.link_qual);
1235 printf("%"PRIu16" ", abs->wifi.link_qual);
1236 printf("%"PRIu16" ", abs->wifi.link_qual_max);
1238 printf("%d ", rel->wifi.signal_level);
1239 printf("%d ", abs->wifi.signal_level);
1242 puts("");
1243 fflush(stdout);
1246 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1247 uint64_t ms_interval)
1249 unsigned int cpus, i, j = 1;
1251 printf("# gnuplot dump (#col:description)\n");
1252 printf("# networking interface: %s\n", ifname);
1253 printf("# sampling interval (t): %"PRIu64" ms\n", ms_interval);
1254 printf("# %d:unixtime ", j++);
1256 printf("%d:rx-bytes-per-t ", j++);
1257 printf("%d:rx-pkts-per-t ", j++);
1258 printf("%d:rx-drops-per-t ", j++);
1259 printf("%d:rx-errors-per-t ", j++);
1261 printf("%d:rx-bytes ", j++);
1262 printf("%d:rx-pkts ", j++);
1263 printf("%d:rx-drops ", j++);
1264 printf("%d:rx-errors ", j++);
1266 printf("%d:tx-bytes-per-t ", j++);
1267 printf("%d:tx-pkts-per-t ", j++);
1268 printf("%d:tx-drops-per-t ", j++);
1269 printf("%d:tx-errors-per-t ", j++);
1271 printf("%d:tx-bytes ", j++);
1272 printf("%d:tx-pkts ", j++);
1273 printf("%d:tx-drops ", j++);
1274 printf("%d:tx-errors ", j++);
1276 printf("%d:context-switches-per-t ", j++);
1277 printf("%d:mem-free ", j++);
1278 printf("%d:mem-used ", j++);
1279 printf("%d:mem-total ", j++);
1280 printf("%d:swap-free ", j++);
1281 printf("%d:swap-used ", j++);
1282 printf("%d:swap-total ", j++);
1283 printf("%d:procs-total ", j++);
1284 printf("%d:procs-in-run ", j++);
1285 printf("%d:procs-in-iow ", j++);
1287 cpus = get_number_cpus();
1289 for (i = 0; i < cpus; ++i) {
1290 printf("%d:cpu%i-usr-per-t ", j++, i);
1291 printf("%d:cpu%i-nice-per-t ", j++, i);
1292 printf("%d:cpu%i-sys-per-t ", j++, i);
1293 printf("%d:cpu%i-idle-per-t ", j++, i);
1294 printf("%d:cpu%i-iow-per-t ", j++, i);
1296 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1297 printf("%d:cpu%i-net-irqs ", j++, i);
1299 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1300 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1301 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1302 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1305 if (iswireless(abs)) {
1306 printf("%d:wifi-link-qual-per-t ", j++);
1307 printf("%d:wifi-link-qual ", j++);
1308 printf("%d:wifi-link-qual-max ", j++);
1310 printf("%d:wifi-signal-dbm-per-t ", j++);
1311 printf("%d:wifi-signal-dbm ", j++);
1314 puts("");
1315 printf("# data:\n");
1316 fflush(stdout);
1319 static int term_main(const char *ifname, uint64_t ms_interval,
1320 unsigned int top_cpus __maybe_unused,
1321 bool suppress_warnings __maybe_unused,
1322 bool omit_header)
1324 do {
1325 stats_sample_generic(ifname, ms_interval);
1327 if (!omit_header) {
1328 omit_header = true;
1329 term_csv_header(ifname, &stats_new, ms_interval);
1332 term_csv(&stats_delta, &stats_new);
1333 } while (stats_loop && !sigint);
1335 return 0;
1338 int main(int argc, char **argv)
1340 short ifflags = 0;
1341 int c, opt_index, ret, promisc = 0;
1342 unsigned int cpus, top_cpus = 5;
1343 uint64_t interval = 1000;
1344 char *ifname = NULL;
1345 bool suppress_warnings = false;
1346 bool omit_header = false;
1347 int (*func_main)(const char *ifname, uint64_t ms_interval,
1348 unsigned int top_cpus, bool suppress_warnings,
1349 bool omit_header);
1351 func_main = screen_main;
1353 setfsuid(getuid());
1354 setfsgid(getgid());
1356 while ((c = getopt_long(argc, argv, short_options, long_options,
1357 &opt_index)) != EOF) {
1358 switch (c) {
1359 case 'h':
1360 help();
1361 break;
1362 case 'v':
1363 version();
1364 break;
1365 case 'W':
1366 suppress_warnings = true;
1367 break;
1368 case 'd':
1369 ifname = xstrndup(optarg, IFNAMSIZ);
1370 break;
1371 case 't':
1372 interval = strtoul(optarg, NULL, 10);
1373 break;
1374 case 'n':
1375 top_cpus = strtoul(optarg, NULL, 10);
1376 if (top_cpus < 1)
1377 panic("Number of top hitter CPUs must be greater than 0");
1378 break;
1379 case 'l':
1380 stats_loop = 1;
1381 break;
1382 case 'p':
1383 promisc = 1;
1384 break;
1385 case 'P':
1386 show_percentage = 1;
1387 break;
1388 case 'm':
1389 show_median = 1;
1390 break;
1391 case 'c':
1392 func_main = term_main;
1393 break;
1394 case 'o':
1395 omit_header = true;
1396 break;
1397 case '?':
1398 switch (optopt) {
1399 case 'd':
1400 case 't':
1401 panic("Option -%c requires an argument!\n",
1402 optopt);
1403 default:
1404 if (isprint(optopt))
1405 printf("Unknown option character `0x%X\'!\n", optopt);
1406 die();
1408 default:
1409 break;
1413 if (argc == 1)
1414 help();
1416 if (argc == 2)
1417 ifname = xstrndup(argv[1], IFNAMSIZ);
1418 if (ifname == NULL)
1419 panic("No networking device given!\n");
1421 if (!strncmp("lo", ifname, IFNAMSIZ))
1422 panic("lo is not supported!\n");
1423 if (device_mtu(ifname) == 0)
1424 panic("This is no networking device!\n");
1426 register_signal(SIGINT, signal_handler);
1427 register_signal(SIGQUIT, signal_handler);
1428 register_signal(SIGSTOP, signal_handler);
1429 register_signal(SIGHUP, signal_handler);
1431 cpus = get_number_cpus();
1432 top_cpus = min(top_cpus, cpus);
1433 if (uname(&uts) < 0)
1434 panic("Cannot execute uname!\n");
1436 stats_alloc(&stats_old, cpus);
1437 stats_alloc(&stats_new, cpus);
1438 stats_alloc(&stats_delta, cpus);
1440 cpu_hits = xcalloc(cpus, sizeof(*cpu_hits));
1442 if (promisc)
1443 ifflags = device_enter_promiscuous_mode(ifname);
1444 ret = func_main(ifname, interval, top_cpus, suppress_warnings, omit_header);
1445 if (promisc)
1446 device_leave_promiscuous_mode(ifname, ifflags);
1448 stats_release(&stats_old);
1449 stats_release(&stats_new);
1450 stats_release(&stats_delta);
1452 xfree(ifname);
1453 return ret;