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.
14 #include <sys/socket.h>
15 #include <sys/fsuid.h>
16 #include <sys/types.h>
17 #include <sys/utsname.h>
38 int16_t link_qual
, link_qual_max
;
39 int signal_level
/*, noise_level*/;
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
55 long long unsigned int *irqs
, *irqs_srx
, *irqs_stx
;
56 uint64_t *cpu_user
, *cpu_sys
, *cpu_nice
, *cpu_idle
, *cpu_iow
;
62 long long unsigned int irqs_rel
, irqs_abs
;
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'},
96 static const char *copyright
= "Please report bugs to <bugs@netsniff-ng.org>\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
)
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",
127 puts("http://www.netsniff-ng.org\n\n"
128 "Usage: ifpps [options] || ifpps <netdev>\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"
145 " ifpps -lpcd wlan0 > plot.dat\n\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");
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");
162 static inline int padding_from_num(int n
)
167 } while ((n
/= 10) > 0);
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
)
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
));
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
)
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
)
229 fp
= fopen("/proc/net/dev", "r");
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
)
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) {
261 memset(buff
, 0, sizeof(buff
));
269 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
271 int ret
= -EINVAL
, try = 0;
272 unsigned int i
, cpus
;
276 struct ethtool_drvinfo drvinf
;
279 cpus
= get_number_cpus();
280 buff_len
= cpus
* 128;
281 buff
= xmalloc(buff_len
);
283 fp
= fopen("/proc/interrupts", "r");
285 panic("Cannot open /proc/interrupts!\n");
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
)
296 ptr
= strchr(buff
, ':');
301 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
303 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
305 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
312 memset(buff
, 0, buff_len
);
315 if (ret
== -EINVAL
&& try == 0) {
316 memset(&drvinf
, 0, sizeof(drvinf
));
317 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
320 ifname
= drvinf
.driver
;
331 static int stats_proc_softirqs(struct ifstat
*stats
)
333 unsigned int i
, cpus
;
342 fp
= fopen("/proc/softirqs", "r");
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
;
362 ptr
+= strlen("NET_xX:");
364 for (i
= 0; i
< cpus
; ++i
) {
366 case softirqs_net_tx
:
367 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
369 case softirqs_net_rx
:
370 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
375 memset(buff
, 0, buff_len
);
383 static int stats_proc_memory(struct ifstat
*stats
)
385 char *ptr
, buff
[256];
388 fp
= fopen("/proc/meminfo", "r");
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
));
427 static int stats_proc_system(struct ifstat
*stats
)
429 unsigned int cpu
, cpus
;
430 char *ptr
, buff
[256];
433 fp
= fopen("/proc/stat", "r");
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");
449 cpu
= strtol(ptr
, &ptr
, 10);
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)
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);
470 memset(buff
, 0, sizeof(buff
));
477 static int stats_proc_procs(struct ifstat
*stats
)
482 dir
= opendir("/proc");
484 panic("Cannot open /proc\n");
486 stats
->procs_total
= 0;
488 while ((e
= readdir(dir
)) != NULL
) {
489 const char *name
= e
->d_name
;
491 unsigned int pid
= strtoul(name
, &end
, 10);
494 if (pid
== 0 && end
== name
)
497 stats
->procs_total
++;
505 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
510 return dbm_val
- 0x100;
513 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
516 struct iw_statistics ws
;
518 ret
= wireless_sigqual(ifname
, &ws
);
520 stats
->wifi
.bitrate
= 0;
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
);
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)) \
540 if ((new->member - old->member) > (new->member)) { \
547 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
550 unsigned int cpus
, i
;
568 DIFF1(wifi
.signal_level
);
569 DIFF1(wifi
.link_qual
);
573 cpus
= get_number_cpus();
575 for (i
= 0; i
< cpus
; ++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
629 if (h1
->hit
== h2
->hit
)
631 else if (h1
->hit
< h2
->hit
)
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
645 if (h1
->irqs_rel
== h2
->irqs_rel
)
647 else if (h1
->irqs_rel
< h2
->irqs_rel
)
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
661 if (h1
->irqs_abs
== h2
->irqs_abs
)
663 else if (h1
->irqs_abs
< h2
->irqs_abs
)
669 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
674 memset(&stats_avg
, 0, sizeof(stats_avg
));
676 for (i
= 0; i
< cpus
; ++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 u32 rate
, uint64_t ms_interval
, unsigned int top_cpus
)
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 memset(&drvinf
, 0, sizeof(drvinf
));
715 ethtool_drvinf(ifname
, &drvinf
);
717 memset(buff
, 0, sizeof(buff
));
718 memset(machine
, 0, sizeof(machine
));
721 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
723 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
724 link
== 0 ? "no" : "yes");
726 if (!strstr(uts
.release
, uts
.machine
))
727 slprintf(machine
, sizeof(machine
), " %s,", uts
.machine
);
729 mvwprintw(screen
, (*voff
)++, 2,
730 "%s,%s %s (%s%s), t=%"PRIu64
"ms, cpus=%u%s/%u"
731 " ", uts
.release
, machine
,
732 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
733 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
736 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
741 mvwprintw(screen
, (*voff
)++, 0,
742 " rx: %16.3llf MiB/t "
746 ((long double) rel
->rx_bytes
) / (1LLU << 20),
747 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
749 mvwprintw(screen
, (*voff
)++, 0,
750 " tx: %16.3llf MiB/t "
754 ((long double) rel
->tx_bytes
) / (1LLU << 20),
755 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
760 static void screen_net_dev_percentage(WINDOW
*screen
, const struct ifstat
*rel
,
763 mvwprintw(screen
, (*voff
)++, 0,
764 " rx: %15.2llf%% of line rate "
766 rate
? ((((long double) rel
->rx_bytes
) / 125000) / rate
) * 100.0 : 0.0);
768 mvwprintw(screen
, (*voff
)++, 0,
769 " tx: %15.2llf%% of line rate "
771 rate
? ((((long double) rel
->tx_bytes
) / 125000) / rate
) * 100.0 : 0.0);
774 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
777 mvwprintw(screen
, (*voff
)++, 2,
782 ((long double) abs
->rx_bytes
) / (1LLU << 20),
783 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
785 mvwprintw(screen
, (*voff
)++, 2,
790 ((long double) abs
->tx_bytes
) / (1LLU << 20),
791 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
794 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
795 const struct ifstat
*abs
, int *voff
)
797 mvwprintw(screen
, (*voff
)++, 2,
798 "sys: %14"PRIu32
" cs/t "
800 "%11"PRIu32
" running "
801 "%10"PRIu32
" iowait",
802 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
805 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
807 mvwprintw(screen
, (*voff
)++, 2,
808 "mem: %13"PRIu64
"M total "
810 "%11"PRIu64
"M active "
811 "%10"PRIu64
"M inactive",
812 abs
->mem_total
/ 1024,
813 (abs
->mem_total
- abs
->mem_free
) / 1024,
814 abs
->mem_active
/ 1024,
815 abs
->mem_inactive
/ 1024);
817 mvwprintw(screen
, (*voff
)++, 2,
818 "swap: %12"PRIu64
"M total "
821 abs
->swap_total
/ 1024,
822 (abs
->swap_total
- abs
->swap_free
) / 1024,
823 abs
->swap_cached
/ 1024);
826 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
827 int *voff
, unsigned int idx
, char *tag
)
829 int max_padd
= padding_from_num(get_number_cpus());
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 mvwprintw(screen
, (*voff
)++, 2,
834 "cpu%*d %s: %11.1lf%% usr/t "
839 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
840 100.0 * rel
->cpu_sys
[idx
] / all
,
841 100.0 * rel
->cpu_idle
[idx
] / all
,
842 100.0 * rel
->cpu_iow
[idx
] / all
);
845 #define MEDIAN_EVEN(member) do { \
846 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
849 #define MEDIAN_ODD(member) do { \
850 m_##member = rel->member[i]; \
853 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
854 const struct avg_stat
*avg
,
855 unsigned int top_cpus
, int *voff
)
858 unsigned int cpus
= get_number_cpus();
859 int max_padd
= padding_from_num(cpus
);
865 /* Display top hitter */
866 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
868 /* Make sure we don't display the min. hitter twice */
869 if (top_cpus
== cpus
)
872 for (i
= 1; i
< top_cpus
; ++i
)
873 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
875 /* Display minimum hitter */
877 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
879 all
= avg
->cpu_user
+ avg
->cpu_sys
+ avg
->cpu_nice
+ avg
->cpu_idle
+ avg
->cpu_iow
;
880 mvwprintw(screen
, (*voff
)++, 2,
884 "%11.1lf%%", max_padd
, "",
885 100.0 * (avg
->cpu_user
+ avg
->cpu_nice
) / all
,
886 100.0 * avg
->cpu_sys
/ all
,
887 100.0 * avg
->cpu_idle
/all
,
888 100.0 * avg
->cpu_iow
/ all
);
891 long double m_cpu_user
, m_cpu_nice
, m_cpu_sys
, m_cpu_idle
, m_cpu_iow
;
894 i
= cpu_hits
[cpus
/ 2].idx
;
896 /* take the mean of the 2 middle entries */
897 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
899 MEDIAN_EVEN(cpu_user
);
900 MEDIAN_EVEN(cpu_nice
);
901 MEDIAN_EVEN(cpu_sys
);
902 MEDIAN_EVEN(cpu_idle
);
903 MEDIAN_EVEN(cpu_iow
);
905 /* take the middle entry as is */
906 MEDIAN_ODD(cpu_user
);
907 MEDIAN_ODD(cpu_nice
);
909 MEDIAN_ODD(cpu_idle
);
913 m_all
= m_cpu_user
+ m_cpu_sys
+ m_cpu_nice
+ m_cpu_idle
+ m_cpu_iow
;
914 mvwprintw(screen
, (*voff
)++, 2,
918 "%11.1Lf%%", max_padd
, "",
919 100.0 * (m_cpu_user
+ m_cpu_nice
) / m_all
,
920 100.0 * m_cpu_sys
/ m_all
,
921 100.0 * m_cpu_idle
/m_all
,
922 100.0 * m_cpu_iow
/ m_all
);
926 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
927 int *voff
, unsigned int idx
, char *tag
)
929 int max_padd
= padding_from_num(get_number_cpus());
931 mvwprintw(screen
, (*voff
)++, 2,
932 "cpu%*d %s: %12llu irqs/t "
941 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
942 const struct avg_stat
*avg
,
943 unsigned int top_cpus
, int *voff
)
946 unsigned int cpus
= get_number_cpus();
947 int max_padd
= padding_from_num(cpus
);
949 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
951 if (top_cpus
== cpus
)
954 for (i
= 1; i
< top_cpus
; ++i
)
955 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
958 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
960 mvwprintw(screen
, (*voff
)++, 2,
963 "%17.1Lf", max_padd
, "",
964 avg
->irqs_rel
, avg
->irqs_srx_rel
, avg
->irqs_stx_rel
);
967 long double m_irqs
, m_irqs_srx
, m_irqs_stx
;
969 i
= cpu_hits
[cpus
/ 2].idx
;
971 /* take the mean of the 2 middle entries */
972 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
975 MEDIAN_EVEN(irqs_srx
);
976 MEDIAN_EVEN(irqs_stx
);
978 /* take the middle entry as is */
980 MEDIAN_ODD(irqs_srx
);
981 MEDIAN_ODD(irqs_stx
);
984 mvwprintw(screen
, (*voff
)++, 2,
987 "%17.1Lf", max_padd
, "",
988 m_irqs
, m_irqs_srx
, m_irqs_stx
);
992 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
993 int *voff
, unsigned int idx
, char *tag
)
995 int max_padd
= padding_from_num(get_number_cpus());
997 mvwprintw(screen
, (*voff
)++, 2,
998 "cpu%*d %s: %12llu irqs",
999 max_padd
, idx
, tag
, abs
->irqs
[idx
]);
1002 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
1003 const struct avg_stat
*avg
,
1004 unsigned int top_cpus
, int *voff
)
1007 unsigned int cpus
= get_number_cpus();
1008 int max_padd
= padding_from_num(cpus
);
1010 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
1012 if (top_cpus
== cpus
)
1015 for (i
= 1; i
< top_cpus
; ++i
)
1016 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "|");
1019 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
1021 mvwprintw(screen
, (*voff
)++, 2,
1022 "avg:%*s%17.1Lf", max_padd
, "", avg
->irqs_abs
);
1027 i
= cpu_hits
[cpus
/ 2].idx
;
1028 if (cpus
% 2 == 0) {
1029 /* take the mean of the 2 middle entries */
1030 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
1032 m_irqs
= (abs
->irqs
[i
] + abs
->irqs
[j
]) / 2;
1034 /* take the middle entry as is */
1035 m_irqs
= abs
->irqs
[i
];
1038 mvwprintw(screen
, (*voff
)++, 2,
1039 "med:%*s%17.1Lf", max_padd
, "", m_irqs
);
1043 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
1044 const struct ifstat
*abs
, int *voff
)
1046 if (iswireless(abs
)) {
1047 mvwprintw(screen
, (*voff
)++, 2,
1048 "linkqual: %7d/%d (%d/t) ",
1049 abs
->wifi
.link_qual
,
1050 abs
->wifi
.link_qual_max
,
1051 rel
->wifi
.link_qual
);
1053 mvwprintw(screen
, (*voff
)++, 2,
1054 "signal: %8d dBm (%d dBm/t) ",
1055 abs
->wifi
.signal_level
,
1056 rel
->wifi
.signal_level
);
1060 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
1061 const struct ifstat
*abs
, const struct avg_stat
*avg
,
1062 int *first
, uint64_t ms_interval
, unsigned int top_cpus
,
1065 unsigned int cpus
, top
;
1066 int voff
= 1, cvoff
= 2;
1067 u32 rate
= device_bitrate(ifname
);
1071 cpus
= get_number_cpus();
1072 top
= min(cpus
, top_cpus
);
1074 stats_top(rel
, abs
, cpus
);
1076 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
1078 screen_header(screen
, ifname
, &voff
, rate
, ms_interval
, top_cpus
);
1081 screen_net_dev_rel(screen
, rel
, &voff
);
1083 if (show_percentage
) {
1085 screen_net_dev_percentage(screen
, rel
, &voff
, rate
);
1089 screen_net_dev_abs(screen
, abs
, &voff
);
1092 screen_sys(screen
, rel
, abs
, &voff
);
1095 screen_mem_swap(screen
, abs
, &voff
);
1098 screen_percpu_states(screen
, rel
, avg
, top
, &voff
);
1100 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
1103 screen_percpu_irqs_rel(screen
, rel
, avg
, top
, &voff
);
1105 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
1108 screen_percpu_irqs_abs(screen
, abs
, avg
, top
, &voff
);
1111 screen_wireless(screen
, rel
, abs
, &voff
);
1114 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
1118 mvwprintw(screen
, cvoff
, 2, "(consider to increase "
1119 "your sampling interval, e.g. -t %d)",
1120 rate
> SPEED_1000
? 10000 : 1000);
1122 mvwprintw(screen
, cvoff
, 2, " "
1130 static void on_panic_handler(void *arg
)
1133 fprintf(stderr
, "Please check <stderr> for error message\n");
1136 static int screen_main(const char *ifname
, uint64_t ms_interval
,
1137 unsigned int top_cpus
, bool suppress_warnings
,
1138 bool omit_header __maybe_unused
)
1141 u32 rate
= device_bitrate(ifname
);
1142 bool need_info
= false;
1144 stats_screen
= screen_init(true);
1146 panic_handler_add(on_panic_handler
, NULL
);
1148 if (((rate
> SPEED_1000
&& ms_interval
<= 1000) ||
1149 (rate
= SPEED_1000
&& ms_interval
< 1000)) &&
1155 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
1158 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
, &stats_avg
,
1159 &first
, ms_interval
, top_cpus
, need_info
);
1161 stats_sample_generic(ifname
, ms_interval
);
1169 static void term_csv(const struct ifstat
*rel
, const struct ifstat
*abs
)
1171 unsigned int cpus
, i
;
1173 printf("%ld ", time(NULL
));
1175 printf("%llu ", rel
->rx_bytes
);
1176 printf("%llu ", rel
->rx_packets
);
1177 printf("%llu ", rel
->rx_drops
);
1178 printf("%llu ", rel
->rx_errors
);
1180 printf("%llu ", abs
->rx_bytes
);
1181 printf("%llu ", abs
->rx_packets
);
1182 printf("%llu ", abs
->rx_drops
);
1183 printf("%llu ", abs
->rx_errors
);
1185 printf("%llu ", rel
->tx_bytes
);
1186 printf("%llu ", rel
->tx_packets
);
1187 printf("%llu ", rel
->tx_drops
);
1188 printf("%llu ", rel
->tx_errors
);
1190 printf("%llu ", abs
->tx_bytes
);
1191 printf("%llu ", abs
->tx_packets
);
1192 printf("%llu ", abs
->tx_drops
);
1193 printf("%llu ", abs
->tx_errors
);
1195 printf("%"PRIu32
" ", rel
->cswitch
);
1196 printf("%"PRIu64
" ", abs
->mem_free
);
1197 printf("%"PRIu64
" ", abs
->mem_total
- abs
->mem_free
);
1198 printf("%"PRIu64
" ", abs
->mem_total
);
1199 printf("%"PRIu64
" ", abs
->swap_free
);
1200 printf("%"PRIu64
" ", abs
->swap_total
- abs
->swap_free
);
1201 printf("%"PRIu64
" ", abs
->swap_total
);
1202 printf("%"PRIu32
" ", abs
->procs_total
);
1203 printf("%"PRIu32
" ", abs
->procs_run
);
1204 printf("%"PRIu32
" ", abs
->procs_iow
);
1206 cpus
= get_number_cpus();
1208 for (i
= 0; i
< cpus
; ++i
) {
1209 printf("%"PRIu64
" ", rel
->cpu_user
[i
]);
1210 printf("%"PRIu64
" ", rel
->cpu_nice
[i
]);
1211 printf("%"PRIu64
" ", rel
->cpu_sys
[i
]);
1212 printf("%"PRIu64
" ", rel
->cpu_idle
[i
]);
1213 printf("%"PRIu64
" ", rel
->cpu_iow
[i
]);
1215 printf("%llu ", rel
->irqs
[i
]);
1216 printf("%llu ", abs
->irqs
[i
]);
1218 printf("%llu ", rel
->irqs_srx
[i
]);
1219 printf("%llu ", abs
->irqs_srx
[i
]);
1221 printf("%llu ", rel
->irqs_stx
[i
]);
1222 printf("%llu ", abs
->irqs_stx
[i
]);
1225 if (iswireless(abs
)) {
1226 printf("%"PRIu16
" ", rel
->wifi
.link_qual
);
1227 printf("%"PRIu16
" ", abs
->wifi
.link_qual
);
1228 printf("%"PRIu16
" ", abs
->wifi
.link_qual_max
);
1230 printf("%d ", rel
->wifi
.signal_level
);
1231 printf("%d ", abs
->wifi
.signal_level
);
1238 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1239 uint64_t ms_interval
)
1241 unsigned int cpus
, i
, j
= 1;
1243 printf("# gnuplot dump (#col:description)\n");
1244 printf("# networking interface: %s\n", ifname
);
1245 printf("# sampling interval (t): %"PRIu64
" ms\n", ms_interval
);
1246 printf("# %d:unixtime ", j
++);
1248 printf("%d:rx-bytes-per-t ", j
++);
1249 printf("%d:rx-pkts-per-t ", j
++);
1250 printf("%d:rx-drops-per-t ", j
++);
1251 printf("%d:rx-errors-per-t ", j
++);
1253 printf("%d:rx-bytes ", j
++);
1254 printf("%d:rx-pkts ", j
++);
1255 printf("%d:rx-drops ", j
++);
1256 printf("%d:rx-errors ", j
++);
1258 printf("%d:tx-bytes-per-t ", j
++);
1259 printf("%d:tx-pkts-per-t ", j
++);
1260 printf("%d:tx-drops-per-t ", j
++);
1261 printf("%d:tx-errors-per-t ", j
++);
1263 printf("%d:tx-bytes ", j
++);
1264 printf("%d:tx-pkts ", j
++);
1265 printf("%d:tx-drops ", j
++);
1266 printf("%d:tx-errors ", j
++);
1268 printf("%d:context-switches-per-t ", j
++);
1269 printf("%d:mem-free ", j
++);
1270 printf("%d:mem-used ", j
++);
1271 printf("%d:mem-total ", j
++);
1272 printf("%d:swap-free ", j
++);
1273 printf("%d:swap-used ", j
++);
1274 printf("%d:swap-total ", j
++);
1275 printf("%d:procs-total ", j
++);
1276 printf("%d:procs-in-run ", j
++);
1277 printf("%d:procs-in-iow ", j
++);
1279 cpus
= get_number_cpus();
1281 for (i
= 0; i
< cpus
; ++i
) {
1282 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1283 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1284 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1285 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1286 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1288 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1289 printf("%d:cpu%i-net-irqs ", j
++, i
);
1291 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1292 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1293 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1294 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1297 if (iswireless(abs
)) {
1298 printf("%d:wifi-link-qual-per-t ", j
++);
1299 printf("%d:wifi-link-qual ", j
++);
1300 printf("%d:wifi-link-qual-max ", j
++);
1302 printf("%d:wifi-signal-dbm-per-t ", j
++);
1303 printf("%d:wifi-signal-dbm ", j
++);
1307 printf("# data:\n");
1311 static int term_main(const char *ifname
, uint64_t ms_interval
,
1312 unsigned int top_cpus __maybe_unused
,
1313 bool suppress_warnings __maybe_unused
,
1317 stats_sample_generic(ifname
, ms_interval
);
1321 term_csv_header(ifname
, &stats_new
, ms_interval
);
1324 term_csv(&stats_delta
, &stats_new
);
1325 } while (stats_loop
&& !sigint
);
1330 int main(int argc
, char **argv
)
1333 int c
, opt_index
, ret
, promisc
= 0;
1334 unsigned int cpus
, top_cpus
= 5;
1335 uint64_t interval
= 1000;
1336 char *ifname
= NULL
;
1337 bool suppress_warnings
= false;
1338 bool omit_header
= false;
1339 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1340 unsigned int top_cpus
, bool suppress_warnings
,
1343 func_main
= screen_main
;
1348 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1349 &opt_index
)) != EOF
) {
1358 suppress_warnings
= true;
1361 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1364 interval
= strtoul(optarg
, NULL
, 10);
1367 top_cpus
= strtoul(optarg
, NULL
, 10);
1369 panic("Number of top hitter CPUs must be greater than 0");
1378 show_percentage
= 1;
1384 func_main
= term_main
;
1393 panic("Option -%c requires an argument!\n",
1396 if (isprint(optopt
))
1397 printf("Unknown option character `0x%X\'!\n", optopt
);
1409 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1411 panic("No networking device given!\n");
1413 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1414 panic("lo is not supported!\n");
1415 if (device_mtu(ifname
) == 0)
1416 panic("This is no networking device!\n");
1418 register_signal(SIGINT
, signal_handler
);
1419 register_signal(SIGQUIT
, signal_handler
);
1420 register_signal(SIGSTOP
, signal_handler
);
1421 register_signal(SIGHUP
, signal_handler
);
1423 cpus
= get_number_cpus();
1424 top_cpus
= min(top_cpus
, cpus
);
1425 if (uname(&uts
) < 0)
1426 panic("Cannot execute uname!\n");
1428 stats_alloc(&stats_old
, cpus
);
1429 stats_alloc(&stats_new
, cpus
);
1430 stats_alloc(&stats_delta
, cpus
);
1432 cpu_hits
= xcalloc(cpus
, sizeof(*cpu_hits
));
1435 ifflags
= device_enter_promiscuous_mode(ifname
);
1436 ret
= func_main(ifname
, interval
, top_cpus
, suppress_warnings
, omit_header
);
1438 device_leave_promiscuous_mode(ifname
, ifflags
);
1440 stats_release(&stats_old
);
1441 stats_release(&stats_new
);
1442 stats_release(&stats_delta
);