debian: define SPEED_UNKNOWN if missing in linux/ethtool.h
[netsniff-ng.git] / ifpps.c
blobff9eaad808aa9dcd5c48bc239455b17afbcbba6c
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 sigint = 1;
100 break;
101 case SIGHUP:
102 default:
103 break;
107 static inline int iswireless(const struct ifstat *stats)
109 return stats->wifi.bitrate > 0;
112 static void __noreturn help(void)
114 printf("\nifpps %s, top-like kernel networking and system statistics\n",
115 VERSION_STRING);
116 puts("http://www.netsniff-ng.org\n\n"
117 "Usage: ifpps [options] || ifpps <netdev>\n"
118 "Options:\n"
119 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
120 " -n|--num-cpus <num> Number of top hitter CPUs in ncurses mode (def: 5)\n"
121 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
122 " -c|--csv Output to terminal as Gnuplot-ready data\n"
123 " -l|--loop Continuous CSV output\n"
124 " -m|--median Display median values\n"
125 " -o|--omit-header Do not print the CSV header\n"
126 " -p|--promisc Promiscuous mode\n"
127 " -P|--percentage Show percentage of theoretical line rate\n"
128 " -W|--no-warn Suppress warnings\n"
129 " -v|--version Print version and exit\n"
130 " -h|--help Print this help and exit\n\n"
131 "Examples:\n"
132 " ifpps eth0\n"
133 " ifpps -pd eth0\n"
134 " ifpps -lpcd wlan0 > plot.dat\n\n"
135 "Note:\n"
136 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
137 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
138 "Please report bugs to <bugs@netsniff-ng.org>\n"
139 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
140 "Swiss federal institute of technology (ETH Zurich)\n"
141 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
142 "License: GNU GPL version 2.0\n"
143 "This is free software: you are free to change and redistribute it.\n"
144 "There is NO WARRANTY, to the extent permitted by law.\n");
145 die();
148 static void __noreturn version(void)
150 printf("\nifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
151 puts("top-like kernel networking and system statistics\n"
152 "http://www.netsniff-ng.org\n\n"
153 "Please report bugs to <bugs@netsniff-ng.org>\n"
154 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
155 "Swiss federal institute of technology (ETH Zurich)\n"
156 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
157 "License: GNU GPL version 2.0\n"
158 "This is free software: you are free to change and redistribute it.\n"
159 "There is NO WARRANTY, to the extent permitted by law.\n");
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 = xzmalloc(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 FILE *fp;
229 fp = fopen("/proc/net/dev", "r");
230 if (!fp)
231 panic("Cannot open /proc/net/dev!\n");
233 if (fgets(buff, sizeof(buff), fp)) { ; }
234 if (fgets(buff, sizeof(buff), fp)) { ; }
236 memset(buff, 0, sizeof(buff));
238 while (fgets(buff, sizeof(buff), fp) != NULL) {
239 buff[sizeof(buff) -1] = 0;
241 if (strstr(buff, ifname) == NULL)
242 continue;
244 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
245 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
246 &stats->rx_bytes, &stats->rx_packets,
247 &stats->rx_errors, &stats->rx_drops,
248 &stats->rx_fifo, &stats->rx_frame,
249 &stats->rx_multi, &stats->tx_bytes,
250 &stats->tx_packets, &stats->tx_errors,
251 &stats->tx_drops, &stats->tx_fifo,
252 &stats->tx_colls, &stats->tx_carrier) == 14) {
253 ret = 0;
254 break;
257 memset(buff, 0, sizeof(buff));
260 fclose(fp);
261 return ret;
264 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
266 int ret = -EINVAL, try = 0;
267 unsigned int i, cpus;
268 char *ptr, *buff;
269 bool seen = false;
270 size_t buff_len;
271 struct ethtool_drvinfo drvinf;
272 FILE *fp;
274 fp = fopen("/proc/interrupts", "r");
275 if (!fp)
276 panic("Cannot open /proc/interrupts!\n");
278 cpus = get_number_cpus();
279 buff_len = cpus * 128;
280 buff = xmalloc(buff_len);
281 retry:
282 fseek(fp, 0, SEEK_SET);
283 memset(buff, 0, buff_len);
285 while (fgets(buff, buff_len, fp) != NULL) {
286 buff[buff_len - 1] = 0;
288 if (strstr(buff, ifname) == NULL)
289 continue;
291 ptr = strchr(buff, ':');
292 if (!ptr)
293 continue;
294 ptr++;
296 for (i = 0; i < cpus && ptr; ++i) {
297 if (seen)
298 stats->irqs[i] += strtol(ptr, &ptr, 10);
299 else
300 stats->irqs[i] = strtol(ptr, &ptr, 10);
301 if (i == cpus - 1) {
302 ret = 0;
303 seen = true;
307 memset(buff, 0, buff_len);
310 if (ret == -EINVAL && try == 0) {
311 memset(&drvinf, 0, sizeof(drvinf));
312 if (ethtool_drvinf(ifname, &drvinf) < 0)
313 goto done;
315 ifname = drvinf.driver;
316 try++;
318 goto retry;
320 done:
321 xfree(buff);
322 fclose(fp);
323 return ret;
326 static int stats_proc_softirqs(struct ifstat *stats)
328 unsigned int i, cpus;
329 char *ptr, *buff;
330 size_t buff_len;
331 FILE *fp;
332 enum {
333 softirqs_net_rx,
334 softirqs_net_tx,
335 } net_type;
337 fp = fopen("/proc/softirqs", "r");
338 if (!fp)
339 panic("Cannot open /proc/softirqs!\n");
341 cpus = get_number_cpus();
342 buff_len = cpus * 128;
343 buff = xmalloc(buff_len);
345 memset(buff, 0, buff_len);
347 while (fgets(buff, buff_len, fp) != NULL) {
348 buff[buff_len - 1] = 0;
350 if ((ptr = strstr(buff, "NET_TX:")))
351 net_type = softirqs_net_tx;
352 else if ((ptr = strstr(buff, "NET_RX:")))
353 net_type = softirqs_net_rx;
354 else
355 continue;
357 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
358 switch (net_type) {
359 case softirqs_net_tx:
360 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
361 break;
362 case softirqs_net_rx:
363 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
364 break;
368 memset(buff, 0, buff_len);
371 xfree(buff);
372 fclose(fp);
373 return 0;
376 static int stats_proc_memory(struct ifstat *stats)
378 char *ptr, buff[256];
379 FILE *fp;
381 fp = fopen("/proc/meminfo", "r");
382 if (!fp)
383 panic("Cannot open /proc/meminfo!\n");
385 memset(buff, 0, sizeof(buff));
387 while (fgets(buff, sizeof(buff), fp) != NULL) {
388 buff[sizeof(buff) - 1] = 0;
390 if ((ptr = strstr(buff, "MemTotal:"))) {
391 ptr += strlen("MemTotal:");
392 stats->mem_total = strtoul(ptr, &ptr, 10);
393 } else if ((ptr = strstr(buff, "MemFree:"))) {
394 ptr += strlen("MemFree:");
395 stats->mem_free = strtoul(ptr, &ptr, 10);
396 } else if ((ptr = strstr(buff, "Active:"))) {
397 ptr += strlen("Active:");
398 stats->mem_active = strtoul(ptr, &ptr, 10);
399 } else if ((ptr = strstr(buff, "Inactive:"))) {
400 ptr += strlen("Inactive:");
401 stats->mem_inactive = strtoul(ptr, &ptr, 10);
402 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
403 ptr += strlen("SwapTotal:");
404 stats->swap_total = strtoul(ptr, &ptr, 10);
405 } else if ((ptr = strstr(buff, "SwapFree:"))) {
406 ptr += strlen("SwapFree:");
407 stats->swap_free = strtoul(ptr, &ptr, 10);
408 } else if ((ptr = strstr(buff, "SwapCached:"))) {
409 ptr += strlen("SwapCached:");
410 stats->swap_cached = strtoul(ptr, &ptr, 10);
413 memset(buff, 0, sizeof(buff));
416 fclose(fp);
417 return 0;
420 static int stats_proc_system(struct ifstat *stats)
422 unsigned int cpu, cpus;
423 char *ptr, buff[256];
424 FILE *fp;
426 fp = fopen("/proc/stat", "r");
427 if (!fp)
428 panic("Cannot open /proc/stat!\n");
430 cpus = get_number_cpus();
432 memset(buff, 0, sizeof(buff));
434 while (fgets(buff, sizeof(buff), fp) != NULL) {
435 buff[sizeof(buff) - 1] = 0;
437 if ((ptr = strstr(buff, "cpu"))) {
438 ptr += strlen("cpu");
439 if (isblank(*ptr))
440 goto next;
442 cpu = strtol(ptr, &ptr, 10);
443 bug_on(cpu > cpus);
445 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
446 &stats->cpu_user[cpu],
447 &stats->cpu_nice[cpu],
448 &stats->cpu_sys[cpu],
449 &stats->cpu_idle[cpu],
450 &stats->cpu_iow[cpu]) != 5)
451 goto next;
452 } else if ((ptr = strstr(buff, "ctxt"))) {
453 ptr += strlen("ctxt");
454 stats->cswitch = strtoul(ptr, &ptr, 10);
455 } else if ((ptr = strstr(buff, "procs_running"))) {
456 ptr += strlen("procs_running");
457 stats->procs_run = strtoul(ptr, &ptr, 10);
458 } else if ((ptr = strstr(buff, "procs_blocked"))) {
459 ptr += strlen("procs_blocked");
460 stats->procs_iow = strtoul(ptr, &ptr, 10);
462 next:
463 memset(buff, 0, sizeof(buff));
466 fclose(fp);
467 return 0;
470 static int stats_proc_procs(struct ifstat *stats)
472 DIR *dir;
473 struct dirent *e;
475 dir = opendir("/proc");
476 if (!dir)
477 panic("Cannot open /proc\n");
479 stats->procs_total = 0;
481 while ((e = readdir(dir)) != NULL) {
482 const char *name = e->d_name;
483 char *end;
484 unsigned int pid = strtoul(name, &end, 10);
486 /* not a number */
487 if (pid == 0 && end == name)
488 continue;
490 stats->procs_total++;
493 closedir(dir);
495 return 0;
498 static int adjust_dbm_level(int in_dbm, int dbm_val)
500 if (!in_dbm)
501 return dbm_val;
503 return dbm_val - 0x100;
506 static int stats_wireless(const char *ifname, struct ifstat *stats)
508 int ret;
509 struct iw_statistics ws;
511 ret = wireless_sigqual(ifname, &ws);
512 if (ret != 0) {
513 stats->wifi.bitrate = 0;
514 return -EINVAL;
517 stats->wifi.bitrate = wireless_bitrate(ifname);
519 stats->wifi.signal_level =
520 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
522 stats->wifi.link_qual = ws.qual.qual;
523 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
525 return ret;
528 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
529 #define DIFF(member) do { \
530 if (sizeof(diff->member) != sizeof(new->member) || \
531 sizeof(diff->member) != sizeof(old->member)) \
532 bug(); \
533 if ((new->member - old->member) > (new->member)) { \
534 diff->member = 0; \
535 } else { \
536 DIFF1(member); \
538 } while (0)
540 static void stats_diff(struct ifstat *old, struct ifstat *new,
541 struct ifstat *diff)
543 unsigned int cpus, i;
545 DIFF(rx_bytes);
546 DIFF(rx_packets);
547 DIFF(rx_drops);
548 DIFF(rx_errors);
549 DIFF(rx_fifo);
550 DIFF(rx_frame);
551 DIFF(rx_multi);
553 DIFF(tx_bytes);
554 DIFF(tx_packets);
555 DIFF(tx_drops);
556 DIFF(tx_errors);
557 DIFF(tx_fifo);
558 DIFF(tx_colls);
559 DIFF(tx_carrier);
561 DIFF1(wifi.signal_level);
562 DIFF1(wifi.link_qual);
564 DIFF1(cswitch);
566 cpus = get_number_cpus();
568 for (i = 0; i < cpus; ++i) {
569 DIFF(irqs[i]);
570 DIFF(irqs_srx[i]);
571 DIFF(irqs_stx[i]);
573 DIFF1(cpu_user[i]);
574 DIFF1(cpu_nice[i]);
575 DIFF1(cpu_sys[i]);
576 DIFF1(cpu_idle[i]);
577 DIFF1(cpu_iow[i]);
581 static void stats_fetch(const char *ifname, struct ifstat *stats)
583 if (stats_proc_net_dev(ifname, stats) < 0)
584 panic("Cannot fetch device stats!\n");
585 if (stats_proc_softirqs(stats) < 0)
586 panic("Cannot fetch software interrupts!\n");
587 if (stats_proc_memory(stats) < 0)
588 panic("Cannot fetch memory stats!\n");
589 if (stats_proc_system(stats) < 0)
590 panic("Cannot fetch system stats!\n");
591 if (stats_proc_procs(stats) < 0)
592 panic("Cannot fetch process stats!\n");
594 stats_proc_interrupts((char *) ifname, stats);
596 stats_wireless(ifname, stats);
599 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
601 unsigned int cpus = get_number_cpus();
603 stats_zero(&stats_old, cpus);
604 stats_zero(&stats_new, cpus);
605 stats_zero(&stats_delta, cpus);
607 stats_fetch(ifname, &stats_old);
608 usleep(ms_interval * 1000);
609 stats_fetch(ifname, &stats_new);
611 stats_diff(&stats_old, &stats_new, &stats_delta);
614 static int cmp_hits(const void *p1, const void *p2)
616 const struct cpu_hit *h1 = p1, *h2 = p2;
619 * We want the hits sorted in descending order, thus reverse the return
620 * values.
622 if (h1->hit == h2->hit)
623 return 0;
624 else if (h1->hit < h2->hit)
625 return 1;
626 else
627 return -1;
630 static int cmp_irqs_rel(const void *p1, const void *p2)
632 const struct cpu_hit *h1 = p1, *h2 = p2;
635 * We want the hits sorted in descending order, thus reverse the return
636 * values.
638 if (h1->irqs_rel == h2->irqs_rel)
639 return 0;
640 else if (h1->irqs_rel < h2->irqs_rel)
641 return 1;
642 else
643 return -1;
646 static int cmp_irqs_abs(const void *p1, const void *p2)
648 const struct cpu_hit *h1 = p1, *h2 = p2;
651 * We want the hits sorted in descending order, thus reverse the return
652 * values.
654 if (h1->irqs_abs == h2->irqs_abs)
655 return 0;
656 else if (h1->irqs_abs < h2->irqs_abs)
657 return 1;
658 else
659 return -1;
662 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
663 unsigned int cpus)
665 unsigned int i;
667 memset(&stats_avg, 0, sizeof(stats_avg));
669 for (i = 0; i < cpus; ++i) {
670 cpu_hits[i].idx = i;
671 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
672 cpu_hits[i].irqs_rel = rel->irqs[i];
673 cpu_hits[i].irqs_abs = abs->irqs[i];
675 stats_avg.cpu_user += rel->cpu_user[i];
676 stats_avg.cpu_sys += rel->cpu_sys[i];
677 stats_avg.cpu_nice += rel->cpu_nice[i];
678 stats_avg.cpu_idle += rel->cpu_idle[i];
679 stats_avg.cpu_iow += rel->cpu_iow[i];
681 stats_avg.irqs_abs += abs->irqs[i];
682 stats_avg.irqs_rel += rel->irqs[i];
683 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
684 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
687 stats_avg.cpu_user /= cpus;
688 stats_avg.cpu_sys /= cpus;
689 stats_avg.cpu_nice /= cpus;
690 stats_avg.cpu_idle /= cpus;
691 stats_avg.cpu_iow /= cpus;
692 stats_avg.irqs_abs /= cpus;
693 stats_avg.irqs_rel /= cpus;
694 stats_avg.irqs_srx_rel /= cpus;
695 stats_avg.irqs_stx_rel /= cpus;
698 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
699 u32 rate, uint64_t ms_interval, unsigned int top_cpus)
701 size_t len = 0;
702 char buff[64], machine[64];
703 struct ethtool_drvinfo drvinf;
704 int link = ethtool_link(ifname);
705 unsigned int cpus = get_number_cpus();
707 memset(&drvinf, 0, sizeof(drvinf));
708 ethtool_drvinf(ifname, &drvinf);
710 memset(buff, 0, sizeof(buff));
711 memset(machine, 0, sizeof(machine));
713 if (rate)
714 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
715 if (link >= 0)
716 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
717 link == 0 ? "no" : "yes");
719 if (!strstr(uts.release, uts.machine))
720 slprintf(machine, sizeof(machine), " %s,", uts.machine);
722 mvwprintw(screen, (*voff)++, 2,
723 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
724 " ", uts.release, machine,
725 ifname, drvinf.driver, buff, ms_interval, top_cpus,
726 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
729 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
730 int *voff)
732 attron(A_REVERSE);
734 mvwprintw(screen, (*voff)++, 0,
735 " rx: %16.3llf MiB/t "
736 "%10llu pkts/t "
737 "%10llu drops/t "
738 "%10llu errors/t ",
739 ((long double) rel->rx_bytes) / (1LLU << 20),
740 rel->rx_packets, rel->rx_drops, rel->rx_errors);
742 mvwprintw(screen, (*voff)++, 0,
743 " tx: %16.3llf MiB/t "
744 "%10llu pkts/t "
745 "%10llu drops/t "
746 "%10llu errors/t ",
747 ((long double) rel->tx_bytes) / (1LLU << 20),
748 rel->tx_packets, rel->tx_drops, rel->tx_errors);
750 attroff(A_REVERSE);
753 static void screen_net_dev_percentage(WINDOW *screen, const struct ifstat *rel,
754 int *voff, u32 rate)
756 mvwprintw(screen, (*voff)++, 0,
757 " rx: %15.2llf%% of line rate "
758 " ",
759 rate ? ((((long double) rel->rx_bytes) / 125000) / rate) * 100.0 : 0.0);
761 mvwprintw(screen, (*voff)++, 0,
762 " tx: %15.2llf%% of line rate "
763 " ",
764 rate ? ((((long double) rel->tx_bytes) / 125000) / rate) * 100.0 : 0.0);
767 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
768 int *voff)
770 mvwprintw(screen, (*voff)++, 2,
771 "rx: %16.3llf MiB "
772 "%10llu pkts "
773 "%10llu drops "
774 "%10llu errors",
775 ((long double) abs->rx_bytes) / (1LLU << 20),
776 abs->rx_packets, abs->rx_drops, abs->rx_errors);
778 mvwprintw(screen, (*voff)++, 2,
779 "tx: %16.3llf MiB "
780 "%10llu pkts "
781 "%10llu drops "
782 "%10llu errors",
783 ((long double) abs->tx_bytes) / (1LLU << 20),
784 abs->tx_packets, abs->tx_drops, abs->tx_errors);
787 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
788 const struct ifstat *abs, int *voff)
790 mvwprintw(screen, (*voff)++, 2,
791 "sys: %14u cs/t "
792 "%11u procs "
793 "%11u running "
794 "%10u iowait",
795 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
798 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
800 mvwprintw(screen, (*voff)++, 2,
801 "mem: %13uM total "
802 "%9uM used "
803 "%11uM active "
804 "%10uM inactive",
805 abs->mem_total / 1024,
806 (abs->mem_total - abs->mem_free) / 1024,
807 abs->mem_active / 1024,
808 abs->mem_inactive / 1024);
810 mvwprintw(screen, (*voff)++, 2,
811 "swap: %12uM total "
812 "%9uM used "
813 "%11uM cached",
814 abs->swap_total / 1024,
815 (abs->swap_total - abs->swap_free) / 1024,
816 abs->swap_cached / 1024);
819 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
820 int *voff, unsigned int idx, char *tag)
822 int max_padd = padding_from_num(get_number_cpus());
823 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
824 rel->cpu_idle[idx] + rel->cpu_iow[idx];
826 mvwprintw(screen, (*voff)++, 2,
827 "cpu%*d %s: %11.1lf%% usr/t "
828 "%9.1lf%% sys/t "
829 "%10.1lf%% idl/t "
830 "%11.1lf%% iow/t",
831 max_padd, idx, tag,
832 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
833 100.0 * rel->cpu_sys[idx] / all,
834 100.0 * rel->cpu_idle[idx] / all,
835 100.0 * rel->cpu_iow[idx] / all);
838 #define MEDIAN_EVEN(member) do { \
839 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
840 } while (0)
842 #define MEDIAN_ODD(member) do { \
843 m_##member = rel->member[i]; \
844 } while (0)
846 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
847 const struct avg_stat *avg,
848 unsigned int top_cpus, int *voff)
850 unsigned int i;
851 unsigned int cpus = get_number_cpus();
852 int max_padd = padding_from_num(cpus);
853 uint64_t all;
855 if (top_cpus == 0)
856 return;
858 /* Display top hitter */
859 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
861 /* Make sure we don't display the min. hitter twice */
862 if (top_cpus == cpus)
863 top_cpus--;
865 for (i = 1; i < top_cpus; ++i)
866 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "|");
868 /* Display minimum hitter */
869 if (cpus != 1)
870 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
872 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
873 mvwprintw(screen, (*voff)++, 2,
874 "avg:%*s%14.1lf%% "
875 "%9.1lf%% "
876 "%10.1lf%% "
877 "%11.1lf%%", max_padd, "",
878 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
879 100.0 * avg->cpu_sys / all,
880 100.0 * avg->cpu_idle /all,
881 100.0 * avg->cpu_iow / all);
883 if (show_median) {
884 long double m_cpu_user, m_cpu_nice, m_cpu_sys, m_cpu_idle, m_cpu_iow;
885 long double m_all;
887 i = cpu_hits[cpus / 2].idx;
888 if (cpus % 2 == 0) {
889 /* take the mean of the 2 middle entries */
890 int j = cpu_hits[(cpus / 2) - 1].idx;
892 MEDIAN_EVEN(cpu_user);
893 MEDIAN_EVEN(cpu_nice);
894 MEDIAN_EVEN(cpu_sys);
895 MEDIAN_EVEN(cpu_idle);
896 MEDIAN_EVEN(cpu_iow);
897 } else {
898 /* take the middle entry as is */
899 MEDIAN_ODD(cpu_user);
900 MEDIAN_ODD(cpu_nice);
901 MEDIAN_ODD(cpu_sys);
902 MEDIAN_ODD(cpu_idle);
903 MEDIAN_ODD(cpu_iow);
906 m_all = m_cpu_user + m_cpu_sys + m_cpu_nice + m_cpu_idle + m_cpu_iow;
907 mvwprintw(screen, (*voff)++, 2,
908 "med:%*s%14.1Lf%% "
909 "%9.1Lf%% "
910 "%10.1Lf%% "
911 "%11.1Lf%%", max_padd, "",
912 100.0 * (m_cpu_user + m_cpu_nice) / m_all,
913 100.0 * m_cpu_sys / m_all,
914 100.0 * m_cpu_idle /m_all,
915 100.0 * m_cpu_iow / m_all);
919 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
920 int *voff, unsigned int idx, char *tag)
922 int max_padd = padding_from_num(get_number_cpus());
924 mvwprintw(screen, (*voff)++, 2,
925 "cpu%*d %s: %12llu irqs/t "
926 "%17llu sirq rx/t "
927 "%17llu sirq tx/t",
928 max_padd, idx, tag,
929 rel->irqs[idx],
930 rel->irqs_srx[idx],
931 rel->irqs_stx[idx]);
934 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
935 const struct avg_stat *avg,
936 unsigned int top_cpus, int *voff)
938 unsigned int i;
939 unsigned int cpus = get_number_cpus();
940 int max_padd = padding_from_num(cpus);
942 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
944 if (top_cpus == cpus)
945 top_cpus--;
947 for (i = 1; i < top_cpus; ++i)
948 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
950 if (cpus != 1)
951 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
953 mvwprintw(screen, (*voff)++, 2,
954 "avg:%*s%17.1Lf "
955 "%17.1Lf "
956 "%17.1Lf", max_padd, "",
957 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
959 if (show_median) {
960 long double m_irqs, m_irqs_srx, m_irqs_stx;
962 i = cpu_hits[cpus / 2].idx;
963 if (cpus % 2 == 0) {
964 /* take the mean of the 2 middle entries */
965 int j = cpu_hits[(cpus / 2) - 1].idx;
967 MEDIAN_EVEN(irqs);
968 MEDIAN_EVEN(irqs_srx);
969 MEDIAN_EVEN(irqs_stx);
970 } else {
971 /* take the middle entry as is */
972 MEDIAN_ODD(irqs);
973 MEDIAN_ODD(irqs_srx);
974 MEDIAN_ODD(irqs_stx);
977 mvwprintw(screen, (*voff)++, 2,
978 "med:%*s%17.1Lf "
979 "%17.1Lf "
980 "%17.1Lf", max_padd, "",
981 m_irqs, m_irqs_srx, m_irqs_stx);
985 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
986 int *voff, unsigned int idx, char *tag)
988 int max_padd = padding_from_num(get_number_cpus());
990 mvwprintw(screen, (*voff)++, 2,
991 "cpu%*d %s: %12llu irqs",
992 max_padd, idx, tag, abs->irqs[idx]);
995 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
996 const struct avg_stat *avg,
997 unsigned int top_cpus, int *voff)
999 unsigned int i;
1000 unsigned int cpus = get_number_cpus();
1001 int max_padd = padding_from_num(cpus);
1003 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
1005 if (top_cpus == cpus)
1006 top_cpus--;
1008 for (i = 1; i < top_cpus; ++i)
1009 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
1011 if (cpus != 1)
1012 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
1014 mvwprintw(screen, (*voff)++, 2,
1015 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
1017 if (show_median) {
1018 long double m_irqs;
1020 i = cpu_hits[cpus / 2].idx;
1021 if (cpus % 2 == 0) {
1022 /* take the mean of the 2 middle entries */
1023 int j = cpu_hits[(cpus / 2) - 1].idx;
1025 m_irqs = (abs->irqs[i] + abs->irqs[j]) / 2;
1026 } else {
1027 /* take the middle entry as is */
1028 m_irqs = abs->irqs[i];
1031 mvwprintw(screen, (*voff)++, 2,
1032 "med:%*s%17.1Lf", max_padd, "", m_irqs);
1036 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
1037 const struct ifstat *abs, int *voff)
1039 if (iswireless(abs)) {
1040 mvwprintw(screen, (*voff)++, 2,
1041 "linkqual: %7d/%d (%d/t) ",
1042 abs->wifi.link_qual,
1043 abs->wifi.link_qual_max,
1044 rel->wifi.link_qual);
1046 mvwprintw(screen, (*voff)++, 2,
1047 "signal: %8d dBm (%d dBm/t) ",
1048 abs->wifi.signal_level,
1049 rel->wifi.signal_level);
1053 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
1054 const struct ifstat *abs, const struct avg_stat *avg,
1055 int *first, uint64_t ms_interval, unsigned int top_cpus,
1056 bool need_info)
1058 unsigned int cpus, top;
1059 int voff = 1, cvoff = 2;
1060 u32 rate = device_bitrate(ifname);
1062 curs_set(0);
1064 cpus = get_number_cpus();
1065 top = min(cpus, top_cpus);
1067 stats_top(rel, abs, cpus);
1069 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
1071 screen_header(screen, ifname, &voff, rate, ms_interval, top_cpus);
1073 voff++;
1074 screen_net_dev_rel(screen, rel, &voff);
1076 if (show_percentage) {
1077 voff++;
1078 screen_net_dev_percentage(screen, rel, &voff, rate);
1081 voff++;
1082 screen_net_dev_abs(screen, abs, &voff);
1084 voff++;
1085 screen_sys(screen, rel, abs, &voff);
1087 voff++;
1088 screen_mem_swap(screen, abs, &voff);
1090 voff++;
1091 screen_percpu_states(screen, rel, avg, top, &voff);
1093 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
1095 voff++;
1096 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
1098 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
1100 voff++;
1101 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
1103 voff++;
1104 screen_wireless(screen, rel, abs, &voff);
1106 if (*first) {
1107 mvwprintw(screen, cvoff, 2, "Collecting data ...");
1108 *first = 0;
1109 } else {
1110 if (need_info)
1111 mvwprintw(screen, cvoff, 2, "(consider to increase "
1112 "your sampling interval, e.g. -t %d)",
1113 rate > SPEED_1000 ? 10000 : 1000);
1114 else
1115 mvwprintw(screen, cvoff, 2, " "
1116 " ");
1119 wrefresh(screen);
1120 refresh();
1123 static int screen_main(const char *ifname, uint64_t ms_interval,
1124 unsigned int top_cpus, bool suppress_warnings,
1125 bool omit_header __maybe_unused)
1127 int first = 1, key;
1128 u32 rate = device_bitrate(ifname);
1129 bool need_info = false;
1131 stats_screen = screen_init(true);
1133 if (((rate > SPEED_1000 && ms_interval <= 1000) ||
1134 (rate = SPEED_1000 && ms_interval < 1000)) &&
1135 !suppress_warnings)
1136 need_info = true;
1138 while (!sigint) {
1139 key = getch();
1140 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1141 break;
1143 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1144 &first, ms_interval, top_cpus, need_info);
1146 stats_sample_generic(ifname, ms_interval);
1149 screen_end();
1151 return 0;
1154 static void term_csv(const struct ifstat *rel, const struct ifstat *abs)
1156 unsigned int cpus, i;
1158 printf("%ld ", time(NULL));
1160 printf("%llu ", rel->rx_bytes);
1161 printf("%llu ", rel->rx_packets);
1162 printf("%llu ", rel->rx_drops);
1163 printf("%llu ", rel->rx_errors);
1165 printf("%llu ", abs->rx_bytes);
1166 printf("%llu ", abs->rx_packets);
1167 printf("%llu ", abs->rx_drops);
1168 printf("%llu ", abs->rx_errors);
1170 printf("%llu ", rel->tx_bytes);
1171 printf("%llu ", rel->tx_packets);
1172 printf("%llu ", rel->tx_drops);
1173 printf("%llu ", rel->tx_errors);
1175 printf("%llu ", abs->tx_bytes);
1176 printf("%llu ", abs->tx_packets);
1177 printf("%llu ", abs->tx_drops);
1178 printf("%llu ", abs->tx_errors);
1180 printf("%u ", rel->cswitch);
1181 printf("%lu ", abs->mem_free);
1182 printf("%lu ", abs->mem_total - abs->mem_free);
1183 printf("%lu ", abs->mem_total);
1184 printf("%lu ", abs->swap_free);
1185 printf("%lu ", abs->swap_total - abs->swap_free);
1186 printf("%lu ", abs->swap_total);
1187 printf("%u ", abs->procs_total);
1188 printf("%u ", abs->procs_run);
1189 printf("%u ", abs->procs_iow);
1191 cpus = get_number_cpus();
1193 for (i = 0; i < cpus; ++i) {
1194 printf("%lu ", rel->cpu_user[i]);
1195 printf("%lu ", rel->cpu_nice[i]);
1196 printf("%lu ", rel->cpu_sys[i]);
1197 printf("%lu ", rel->cpu_idle[i]);
1198 printf("%lu ", rel->cpu_iow[i]);
1200 printf("%llu ", rel->irqs[i]);
1201 printf("%llu ", abs->irqs[i]);
1203 printf("%llu ", rel->irqs_srx[i]);
1204 printf("%llu ", abs->irqs_srx[i]);
1206 printf("%llu ", rel->irqs_stx[i]);
1207 printf("%llu ", abs->irqs_stx[i]);
1210 if (iswireless(abs)) {
1211 printf("%u ", rel->wifi.link_qual);
1212 printf("%u ", abs->wifi.link_qual);
1213 printf("%u ", abs->wifi.link_qual_max);
1215 printf("%d ", rel->wifi.signal_level);
1216 printf("%d ", abs->wifi.signal_level);
1219 puts("");
1220 fflush(stdout);
1223 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1224 uint64_t ms_interval)
1226 unsigned int cpus, i, j = 1;
1228 printf("# gnuplot dump (#col:description)\n");
1229 printf("# networking interface: %s\n", ifname);
1230 printf("# sampling interval (t): %lu ms\n", ms_interval);
1231 printf("# %d:unixtime ", j++);
1233 printf("%d:rx-bytes-per-t ", j++);
1234 printf("%d:rx-pkts-per-t ", j++);
1235 printf("%d:rx-drops-per-t ", j++);
1236 printf("%d:rx-errors-per-t ", j++);
1238 printf("%d:rx-bytes ", j++);
1239 printf("%d:rx-pkts ", j++);
1240 printf("%d:rx-drops ", j++);
1241 printf("%d:rx-errors ", j++);
1243 printf("%d:tx-bytes-per-t ", j++);
1244 printf("%d:tx-pkts-per-t ", j++);
1245 printf("%d:tx-drops-per-t ", j++);
1246 printf("%d:tx-errors-per-t ", j++);
1248 printf("%d:tx-bytes ", j++);
1249 printf("%d:tx-pkts ", j++);
1250 printf("%d:tx-drops ", j++);
1251 printf("%d:tx-errors ", j++);
1253 printf("%d:context-switches-per-t ", j++);
1254 printf("%d:mem-free ", j++);
1255 printf("%d:mem-used ", j++);
1256 printf("%d:mem-total ", j++);
1257 printf("%d:swap-free ", j++);
1258 printf("%d:swap-used ", j++);
1259 printf("%d:swap-total ", j++);
1260 printf("%d:procs-total ", j++);
1261 printf("%d:procs-in-run ", j++);
1262 printf("%d:procs-in-iow ", j++);
1264 cpus = get_number_cpus();
1266 for (i = 0; i < cpus; ++i) {
1267 printf("%d:cpu%i-usr-per-t ", j++, i);
1268 printf("%d:cpu%i-nice-per-t ", j++, i);
1269 printf("%d:cpu%i-sys-per-t ", j++, i);
1270 printf("%d:cpu%i-idle-per-t ", j++, i);
1271 printf("%d:cpu%i-iow-per-t ", j++, i);
1273 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1274 printf("%d:cpu%i-net-irqs ", j++, i);
1276 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1277 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1278 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1279 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1282 if (iswireless(abs)) {
1283 printf("%d:wifi-link-qual-per-t ", j++);
1284 printf("%d:wifi-link-qual ", j++);
1285 printf("%d:wifi-link-qual-max ", j++);
1287 printf("%d:wifi-signal-dbm-per-t ", j++);
1288 printf("%d:wifi-signal-dbm ", j++);
1291 puts("");
1292 printf("# data:\n");
1293 fflush(stdout);
1296 static int term_main(const char *ifname, uint64_t ms_interval,
1297 unsigned int top_cpus __maybe_unused,
1298 bool suppress_warnings __maybe_unused,
1299 bool omit_header)
1301 do {
1302 stats_sample_generic(ifname, ms_interval);
1304 if (!omit_header) {
1305 omit_header = true;
1306 term_csv_header(ifname, &stats_new, ms_interval);
1309 term_csv(&stats_delta, &stats_new);
1310 } while (stats_loop && !sigint);
1312 return 0;
1315 int main(int argc, char **argv)
1317 short ifflags = 0;
1318 int c, opt_index, ret, promisc = 0;
1319 unsigned int cpus, top_cpus = 5;
1320 uint64_t interval = 1000;
1321 char *ifname = NULL;
1322 bool suppress_warnings = false;
1323 bool omit_header = false;
1324 int (*func_main)(const char *ifname, uint64_t ms_interval,
1325 unsigned int top_cpus, bool suppress_warnings,
1326 bool omit_header);
1328 func_main = screen_main;
1330 setfsuid(getuid());
1331 setfsgid(getgid());
1333 while ((c = getopt_long(argc, argv, short_options, long_options,
1334 &opt_index)) != EOF) {
1335 switch (c) {
1336 case 'h':
1337 help();
1338 break;
1339 case 'v':
1340 version();
1341 break;
1342 case 'W':
1343 suppress_warnings = true;
1344 break;
1345 case 'd':
1346 ifname = xstrndup(optarg, IFNAMSIZ);
1347 break;
1348 case 't':
1349 interval = strtoul(optarg, NULL, 10);
1350 break;
1351 case 'n':
1352 top_cpus = strtoul(optarg, NULL, 10);
1353 if (top_cpus < 1)
1354 panic("Number of top hitter CPUs must be greater than 0");
1355 break;
1356 case 'l':
1357 stats_loop = 1;
1358 break;
1359 case 'p':
1360 promisc = 1;
1361 break;
1362 case 'P':
1363 show_percentage = 1;
1364 break;
1365 case 'm':
1366 show_median = 1;
1367 break;
1368 case 'c':
1369 func_main = term_main;
1370 break;
1371 case 'o':
1372 omit_header = true;
1373 break;
1374 case '?':
1375 switch (optopt) {
1376 case 'd':
1377 case 't':
1378 panic("Option -%c requires an argument!\n",
1379 optopt);
1380 default:
1381 if (isprint(optopt))
1382 printf("Unknown option character `0x%X\'!\n", optopt);
1383 die();
1385 default:
1386 break;
1390 if (argc == 1)
1391 help();
1393 if (argc == 2)
1394 ifname = xstrndup(argv[1], IFNAMSIZ);
1395 if (ifname == NULL)
1396 panic("No networking device given!\n");
1398 if (!strncmp("lo", ifname, IFNAMSIZ))
1399 panic("lo is not supported!\n");
1400 if (device_mtu(ifname) == 0)
1401 panic("This is no networking device!\n");
1403 register_signal(SIGINT, signal_handler);
1404 register_signal(SIGHUP, signal_handler);
1406 cpus = get_number_cpus();
1407 top_cpus = min(top_cpus, cpus);
1408 if (uname(&uts) < 0)
1409 panic("Cannot execute uname!\n");
1411 stats_alloc(&stats_old, cpus);
1412 stats_alloc(&stats_new, cpus);
1413 stats_alloc(&stats_delta, cpus);
1415 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1417 if (promisc)
1418 ifflags = device_enter_promiscuous_mode(ifname);
1419 ret = func_main(ifname, interval, top_cpus, suppress_warnings, omit_header);
1420 if (promisc)
1421 device_leave_promiscuous_mode(ifname, ifflags);
1423 stats_release(&stats_old);
1424 stats_release(&stats_new);
1425 stats_release(&stats_delta);
1427 xfree(ifname);
1428 return ret;