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.
13 #include <sys/socket.h>
14 #include <sys/fsuid.h>
15 #include <sys/types.h>
16 #include <sys/utsname.h>
37 int16_t link_qual
, link_qual_max
;
38 int signal_level
/*, noise_level*/;
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
54 long long unsigned int *irqs
, *irqs_srx
, *irqs_stx
;
55 uint64_t *cpu_user
, *cpu_sys
, *cpu_nice
, *cpu_idle
, *cpu_iow
;
61 long long unsigned int irqs_rel
, irqs_abs
;
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'},
95 static void signal_handler(int number
)
109 static inline int iswireless(const struct ifstat
*stats
)
111 return stats
->wifi
.bitrate
> 0;
114 static void __noreturn
help(void)
116 printf("\nifpps %s, top-like kernel networking and system statistics\n",
118 puts("http://www.netsniff-ng.org\n\n"
119 "Usage: ifpps [options] || ifpps <netdev>\n"
121 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
122 " -n|--num-cpus <num> Number of top hitter CPUs in ncurses mode (def: 5)\n"
123 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
124 " -c|--csv Output to terminal as Gnuplot-ready data\n"
125 " -l|--loop Continuous CSV output\n"
126 " -m|--median Display median values\n"
127 " -o|--omit-header Do not print the CSV header\n"
128 " -p|--promisc Promiscuous mode\n"
129 " -P|--percentage Show percentage of theoretical line rate\n"
130 " -W|--no-warn Suppress warnings\n"
131 " -v|--version Print version and exit\n"
132 " -h|--help Print this help and exit\n\n"
136 " ifpps -lpcd wlan0 > plot.dat\n\n"
138 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
139 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
140 "Please report bugs to <bugs@netsniff-ng.org>\n"
141 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
142 "Swiss federal institute of technology (ETH Zurich)\n"
143 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
144 "License: GNU GPL version 2.0\n"
145 "This is free software: you are free to change and redistribute it.\n"
146 "There is NO WARRANTY, to the extent permitted by law.\n");
150 static void __noreturn
version(void)
152 printf("\nifpps %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
153 puts("top-like kernel networking and system statistics\n"
154 "http://www.netsniff-ng.org\n\n"
155 "Please report bugs to <bugs@netsniff-ng.org>\n"
156 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
157 "Swiss federal institute of technology (ETH Zurich)\n"
158 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
159 "License: GNU GPL version 2.0\n"
160 "This is free software: you are free to change and redistribute it.\n"
161 "There is NO WARRANTY, to the extent permitted by law.\n");
165 static inline int padding_from_num(int n
)
170 } while ((n
/= 10) > 0);
174 #define STATS_ALLOC1(member) \
175 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
177 static void stats_alloc(struct ifstat
*stats
, unsigned int cpus
)
180 STATS_ALLOC1(irqs_srx
);
181 STATS_ALLOC1(irqs_stx
);
183 STATS_ALLOC1(cpu_user
);
184 STATS_ALLOC1(cpu_sys
);
185 STATS_ALLOC1(cpu_nice
);
186 STATS_ALLOC1(cpu_idle
);
187 STATS_ALLOC1(cpu_iow
);
190 #define STATS_ZERO1(member) \
191 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
193 static void stats_zero(struct ifstat
*stats
, unsigned int cpus
)
195 /* Only clear the non-pointer members */
196 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
199 STATS_ZERO1(irqs_srx
);
200 STATS_ZERO1(irqs_stx
);
202 STATS_ZERO1(cpu_user
);
203 STATS_ZERO1(cpu_sys
);
204 STATS_ZERO1(cpu_nice
);
205 STATS_ZERO1(cpu_idle
);
206 STATS_ZERO1(cpu_iow
);
209 #define STATS_RELEASE(member) \
210 do { xfree(stats->member); } while (0)
212 static void stats_release(struct ifstat
*stats
)
215 STATS_RELEASE(irqs_srx
);
216 STATS_RELEASE(irqs_stx
);
218 STATS_RELEASE(cpu_user
);
219 STATS_RELEASE(cpu_sys
);
220 STATS_RELEASE(cpu_nice
);
221 STATS_RELEASE(cpu_idle
);
222 STATS_RELEASE(cpu_iow
);
225 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
231 fp
= fopen("/proc/net/dev", "r");
233 panic("Cannot open /proc/net/dev!\n");
235 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
236 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
238 memset(buff
, 0, sizeof(buff
));
240 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
241 buff
[sizeof(buff
) -1] = 0;
243 if (strstr(buff
, ifname
) == NULL
)
246 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
247 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
248 &stats
->rx_bytes
, &stats
->rx_packets
,
249 &stats
->rx_errors
, &stats
->rx_drops
,
250 &stats
->rx_fifo
, &stats
->rx_frame
,
251 &stats
->rx_multi
, &stats
->tx_bytes
,
252 &stats
->tx_packets
, &stats
->tx_errors
,
253 &stats
->tx_drops
, &stats
->tx_fifo
,
254 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
259 memset(buff
, 0, sizeof(buff
));
266 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
268 int ret
= -EINVAL
, try = 0;
269 unsigned int i
, cpus
;
273 struct ethtool_drvinfo drvinf
;
276 cpus
= get_number_cpus();
277 buff_len
= cpus
* 128;
278 buff
= xmalloc(buff_len
);
280 fp
= fopen("/proc/interrupts", "r");
282 panic("Cannot open /proc/interrupts!\n");
284 fseek(fp
, 0, SEEK_SET
);
285 memset(buff
, 0, buff_len
);
287 while (fgets(buff
, buff_len
, fp
) != NULL
) {
288 buff
[buff_len
- 1] = 0;
290 if (strstr(buff
, ifname
) == NULL
)
293 ptr
= strchr(buff
, ':');
298 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
300 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
302 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
309 memset(buff
, 0, buff_len
);
312 if (ret
== -EINVAL
&& try == 0) {
313 memset(&drvinf
, 0, sizeof(drvinf
));
314 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
317 ifname
= drvinf
.driver
;
328 static int stats_proc_softirqs(struct ifstat
*stats
)
330 unsigned int i
, cpus
;
339 fp
= fopen("/proc/softirqs", "r");
341 panic("Cannot open /proc/softirqs!\n");
343 cpus
= get_number_cpus();
344 buff_len
= cpus
* 128;
345 buff
= xmalloc(buff_len
);
347 memset(buff
, 0, buff_len
);
349 while (fgets(buff
, buff_len
, fp
) != NULL
) {
350 buff
[buff_len
- 1] = 0;
352 if ((ptr
= strstr(buff
, "NET_TX:")))
353 net_type
= softirqs_net_tx
;
354 else if ((ptr
= strstr(buff
, "NET_RX:")))
355 net_type
= softirqs_net_rx
;
359 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
361 case softirqs_net_tx
:
362 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
364 case softirqs_net_rx
:
365 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
370 memset(buff
, 0, buff_len
);
378 static int stats_proc_memory(struct ifstat
*stats
)
380 char *ptr
, buff
[256];
383 fp
= fopen("/proc/meminfo", "r");
385 panic("Cannot open /proc/meminfo!\n");
387 memset(buff
, 0, sizeof(buff
));
389 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
390 buff
[sizeof(buff
) - 1] = 0;
392 if ((ptr
= strstr(buff
, "MemTotal:"))) {
393 ptr
+= strlen("MemTotal:");
394 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
395 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
396 ptr
+= strlen("MemFree:");
397 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
398 } else if ((ptr
= strstr(buff
, "Active:"))) {
399 ptr
+= strlen("Active:");
400 stats
->mem_active
= strtoul(ptr
, &ptr
, 10);
401 } else if ((ptr
= strstr(buff
, "Inactive:"))) {
402 ptr
+= strlen("Inactive:");
403 stats
->mem_inactive
= strtoul(ptr
, &ptr
, 10);
404 } else if ((ptr
= strstr(buff
, "SwapTotal:"))) {
405 ptr
+= strlen("SwapTotal:");
406 stats
->swap_total
= strtoul(ptr
, &ptr
, 10);
407 } else if ((ptr
= strstr(buff
, "SwapFree:"))) {
408 ptr
+= strlen("SwapFree:");
409 stats
->swap_free
= strtoul(ptr
, &ptr
, 10);
410 } else if ((ptr
= strstr(buff
, "SwapCached:"))) {
411 ptr
+= strlen("SwapCached:");
412 stats
->swap_cached
= strtoul(ptr
, &ptr
, 10);
415 memset(buff
, 0, sizeof(buff
));
422 static int stats_proc_system(struct ifstat
*stats
)
424 unsigned int cpu
, cpus
;
425 char *ptr
, buff
[256];
428 fp
= fopen("/proc/stat", "r");
430 panic("Cannot open /proc/stat!\n");
432 cpus
= get_number_cpus();
434 memset(buff
, 0, sizeof(buff
));
436 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
437 buff
[sizeof(buff
) - 1] = 0;
439 if ((ptr
= strstr(buff
, "cpu"))) {
440 ptr
+= strlen("cpu");
444 cpu
= strtol(ptr
, &ptr
, 10);
447 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
448 &stats
->cpu_user
[cpu
],
449 &stats
->cpu_nice
[cpu
],
450 &stats
->cpu_sys
[cpu
],
451 &stats
->cpu_idle
[cpu
],
452 &stats
->cpu_iow
[cpu
]) != 5)
454 } else if ((ptr
= strstr(buff
, "ctxt"))) {
455 ptr
+= strlen("ctxt");
456 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
457 } else if ((ptr
= strstr(buff
, "procs_running"))) {
458 ptr
+= strlen("procs_running");
459 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
460 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
461 ptr
+= strlen("procs_blocked");
462 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
465 memset(buff
, 0, sizeof(buff
));
472 static int stats_proc_procs(struct ifstat
*stats
)
477 dir
= opendir("/proc");
479 panic("Cannot open /proc\n");
481 stats
->procs_total
= 0;
483 while ((e
= readdir(dir
)) != NULL
) {
484 const char *name
= e
->d_name
;
486 unsigned int pid
= strtoul(name
, &end
, 10);
489 if (pid
== 0 && end
== name
)
492 stats
->procs_total
++;
500 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
505 return dbm_val
- 0x100;
508 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
511 struct iw_statistics ws
;
513 ret
= wireless_sigqual(ifname
, &ws
);
515 stats
->wifi
.bitrate
= 0;
519 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
521 stats
->wifi
.signal_level
=
522 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
524 stats
->wifi
.link_qual
= ws
.qual
.qual
;
525 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
530 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
531 #define DIFF(member) do { \
532 if (sizeof(diff->member) != sizeof(new->member) || \
533 sizeof(diff->member) != sizeof(old->member)) \
535 if ((new->member - old->member) > (new->member)) { \
542 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
545 unsigned int cpus
, i
;
563 DIFF1(wifi
.signal_level
);
564 DIFF1(wifi
.link_qual
);
568 cpus
= get_number_cpus();
570 for (i
= 0; i
< cpus
; ++i
) {
583 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
585 if (stats_proc_net_dev(ifname
, stats
) < 0)
586 panic("Cannot fetch device stats!\n");
587 if (stats_proc_softirqs(stats
) < 0)
588 panic("Cannot fetch software interrupts!\n");
589 if (stats_proc_memory(stats
) < 0)
590 panic("Cannot fetch memory stats!\n");
591 if (stats_proc_system(stats
) < 0)
592 panic("Cannot fetch system stats!\n");
593 if (stats_proc_procs(stats
) < 0)
594 panic("Cannot fetch process stats!\n");
596 stats_proc_interrupts((char *) ifname
, stats
);
598 stats_wireless(ifname
, stats
);
601 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
603 unsigned int cpus
= get_number_cpus();
605 stats_zero(&stats_old
, cpus
);
606 stats_zero(&stats_new
, cpus
);
607 stats_zero(&stats_delta
, cpus
);
609 stats_fetch(ifname
, &stats_old
);
610 usleep(ms_interval
* 1000);
611 stats_fetch(ifname
, &stats_new
);
613 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
616 static int cmp_hits(const void *p1
, const void *p2
)
618 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
621 * We want the hits sorted in descending order, thus reverse the return
624 if (h1
->hit
== h2
->hit
)
626 else if (h1
->hit
< h2
->hit
)
632 static int cmp_irqs_rel(const void *p1
, const void *p2
)
634 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
637 * We want the hits sorted in descending order, thus reverse the return
640 if (h1
->irqs_rel
== h2
->irqs_rel
)
642 else if (h1
->irqs_rel
< h2
->irqs_rel
)
648 static int cmp_irqs_abs(const void *p1
, const void *p2
)
650 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
653 * We want the hits sorted in descending order, thus reverse the return
656 if (h1
->irqs_abs
== h2
->irqs_abs
)
658 else if (h1
->irqs_abs
< h2
->irqs_abs
)
664 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
669 memset(&stats_avg
, 0, sizeof(stats_avg
));
671 for (i
= 0; i
< cpus
; ++i
) {
673 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
674 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
675 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
677 stats_avg
.cpu_user
+= rel
->cpu_user
[i
];
678 stats_avg
.cpu_sys
+= rel
->cpu_sys
[i
];
679 stats_avg
.cpu_nice
+= rel
->cpu_nice
[i
];
680 stats_avg
.cpu_idle
+= rel
->cpu_idle
[i
];
681 stats_avg
.cpu_iow
+= rel
->cpu_iow
[i
];
683 stats_avg
.irqs_abs
+= abs
->irqs
[i
];
684 stats_avg
.irqs_rel
+= rel
->irqs
[i
];
685 stats_avg
.irqs_srx_rel
+= rel
->irqs_srx
[i
];
686 stats_avg
.irqs_stx_rel
+= rel
->irqs_stx
[i
];
689 stats_avg
.cpu_user
/= cpus
;
690 stats_avg
.cpu_sys
/= cpus
;
691 stats_avg
.cpu_nice
/= cpus
;
692 stats_avg
.cpu_idle
/= cpus
;
693 stats_avg
.cpu_iow
/= cpus
;
694 stats_avg
.irqs_abs
/= cpus
;
695 stats_avg
.irqs_rel
/= cpus
;
696 stats_avg
.irqs_srx_rel
/= cpus
;
697 stats_avg
.irqs_stx_rel
/= cpus
;
700 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
701 u32 rate
, uint64_t ms_interval
, unsigned int top_cpus
)
704 char buff
[64], machine
[64];
705 struct ethtool_drvinfo drvinf
;
706 int link
= ethtool_link(ifname
);
707 unsigned int cpus
= get_number_cpus();
709 memset(&drvinf
, 0, sizeof(drvinf
));
710 ethtool_drvinf(ifname
, &drvinf
);
712 memset(buff
, 0, sizeof(buff
));
713 memset(machine
, 0, sizeof(machine
));
716 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
718 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
719 link
== 0 ? "no" : "yes");
721 if (!strstr(uts
.release
, uts
.machine
))
722 slprintf(machine
, sizeof(machine
), " %s,", uts
.machine
);
724 mvwprintw(screen
, (*voff
)++, 2,
725 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
726 " ", uts
.release
, machine
,
727 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
728 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
731 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
736 mvwprintw(screen
, (*voff
)++, 0,
737 " rx: %16.3llf MiB/t "
741 ((long double) rel
->rx_bytes
) / (1LLU << 20),
742 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
744 mvwprintw(screen
, (*voff
)++, 0,
745 " tx: %16.3llf MiB/t "
749 ((long double) rel
->tx_bytes
) / (1LLU << 20),
750 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
755 static void screen_net_dev_percentage(WINDOW
*screen
, const struct ifstat
*rel
,
758 mvwprintw(screen
, (*voff
)++, 0,
759 " rx: %15.2llf%% of line rate "
761 rate
? ((((long double) rel
->rx_bytes
) / 125000) / rate
) * 100.0 : 0.0);
763 mvwprintw(screen
, (*voff
)++, 0,
764 " tx: %15.2llf%% of line rate "
766 rate
? ((((long double) rel
->tx_bytes
) / 125000) / rate
) * 100.0 : 0.0);
769 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
772 mvwprintw(screen
, (*voff
)++, 2,
777 ((long double) abs
->rx_bytes
) / (1LLU << 20),
778 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
780 mvwprintw(screen
, (*voff
)++, 2,
785 ((long double) abs
->tx_bytes
) / (1LLU << 20),
786 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
789 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
790 const struct ifstat
*abs
, int *voff
)
792 mvwprintw(screen
, (*voff
)++, 2,
797 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
800 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
802 mvwprintw(screen
, (*voff
)++, 2,
807 abs
->mem_total
/ 1024,
808 (abs
->mem_total
- abs
->mem_free
) / 1024,
809 abs
->mem_active
/ 1024,
810 abs
->mem_inactive
/ 1024);
812 mvwprintw(screen
, (*voff
)++, 2,
816 abs
->swap_total
/ 1024,
817 (abs
->swap_total
- abs
->swap_free
) / 1024,
818 abs
->swap_cached
/ 1024);
821 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
822 int *voff
, unsigned int idx
, char *tag
)
824 int max_padd
= padding_from_num(get_number_cpus());
825 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
826 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
828 mvwprintw(screen
, (*voff
)++, 2,
829 "cpu%*d %s: %11.1lf%% usr/t "
834 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
835 100.0 * rel
->cpu_sys
[idx
] / all
,
836 100.0 * rel
->cpu_idle
[idx
] / all
,
837 100.0 * rel
->cpu_iow
[idx
] / all
);
840 #define MEDIAN_EVEN(member) do { \
841 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
844 #define MEDIAN_ODD(member) do { \
845 m_##member = rel->member[i]; \
848 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
849 const struct avg_stat
*avg
,
850 unsigned int top_cpus
, int *voff
)
853 unsigned int cpus
= get_number_cpus();
854 int max_padd
= padding_from_num(cpus
);
860 /* Display top hitter */
861 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
863 /* Make sure we don't display the min. hitter twice */
864 if (top_cpus
== cpus
)
867 for (i
= 1; i
< top_cpus
; ++i
)
868 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
870 /* Display minimum hitter */
872 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
874 all
= avg
->cpu_user
+ avg
->cpu_sys
+ avg
->cpu_nice
+ avg
->cpu_idle
+ avg
->cpu_iow
;
875 mvwprintw(screen
, (*voff
)++, 2,
879 "%11.1lf%%", max_padd
, "",
880 100.0 * (avg
->cpu_user
+ avg
->cpu_nice
) / all
,
881 100.0 * avg
->cpu_sys
/ all
,
882 100.0 * avg
->cpu_idle
/all
,
883 100.0 * avg
->cpu_iow
/ all
);
886 long double m_cpu_user
, m_cpu_nice
, m_cpu_sys
, m_cpu_idle
, m_cpu_iow
;
889 i
= cpu_hits
[cpus
/ 2].idx
;
891 /* take the mean of the 2 middle entries */
892 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
894 MEDIAN_EVEN(cpu_user
);
895 MEDIAN_EVEN(cpu_nice
);
896 MEDIAN_EVEN(cpu_sys
);
897 MEDIAN_EVEN(cpu_idle
);
898 MEDIAN_EVEN(cpu_iow
);
900 /* take the middle entry as is */
901 MEDIAN_ODD(cpu_user
);
902 MEDIAN_ODD(cpu_nice
);
904 MEDIAN_ODD(cpu_idle
);
908 m_all
= m_cpu_user
+ m_cpu_sys
+ m_cpu_nice
+ m_cpu_idle
+ m_cpu_iow
;
909 mvwprintw(screen
, (*voff
)++, 2,
913 "%11.1Lf%%", max_padd
, "",
914 100.0 * (m_cpu_user
+ m_cpu_nice
) / m_all
,
915 100.0 * m_cpu_sys
/ m_all
,
916 100.0 * m_cpu_idle
/m_all
,
917 100.0 * m_cpu_iow
/ m_all
);
921 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
922 int *voff
, unsigned int idx
, char *tag
)
924 int max_padd
= padding_from_num(get_number_cpus());
926 mvwprintw(screen
, (*voff
)++, 2,
927 "cpu%*d %s: %12llu irqs/t "
936 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
937 const struct avg_stat
*avg
,
938 unsigned int top_cpus
, int *voff
)
941 unsigned int cpus
= get_number_cpus();
942 int max_padd
= padding_from_num(cpus
);
944 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
946 if (top_cpus
== cpus
)
949 for (i
= 1; i
< top_cpus
; ++i
)
950 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
953 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
955 mvwprintw(screen
, (*voff
)++, 2,
958 "%17.1Lf", max_padd
, "",
959 avg
->irqs_rel
, avg
->irqs_srx_rel
, avg
->irqs_stx_rel
);
962 long double m_irqs
, m_irqs_srx
, m_irqs_stx
;
964 i
= cpu_hits
[cpus
/ 2].idx
;
966 /* take the mean of the 2 middle entries */
967 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
970 MEDIAN_EVEN(irqs_srx
);
971 MEDIAN_EVEN(irqs_stx
);
973 /* take the middle entry as is */
975 MEDIAN_ODD(irqs_srx
);
976 MEDIAN_ODD(irqs_stx
);
979 mvwprintw(screen
, (*voff
)++, 2,
982 "%17.1Lf", max_padd
, "",
983 m_irqs
, m_irqs_srx
, m_irqs_stx
);
987 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
988 int *voff
, unsigned int idx
, char *tag
)
990 int max_padd
= padding_from_num(get_number_cpus());
992 mvwprintw(screen
, (*voff
)++, 2,
993 "cpu%*d %s: %12llu irqs",
994 max_padd
, idx
, tag
, abs
->irqs
[idx
]);
997 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
998 const struct avg_stat
*avg
,
999 unsigned int top_cpus
, int *voff
)
1002 unsigned int cpus
= get_number_cpus();
1003 int max_padd
= padding_from_num(cpus
);
1005 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
1007 if (top_cpus
== cpus
)
1010 for (i
= 1; i
< top_cpus
; ++i
)
1011 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "|");
1014 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
1016 mvwprintw(screen
, (*voff
)++, 2,
1017 "avg:%*s%17.1Lf", max_padd
, "", avg
->irqs_abs
);
1022 i
= cpu_hits
[cpus
/ 2].idx
;
1023 if (cpus
% 2 == 0) {
1024 /* take the mean of the 2 middle entries */
1025 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
1027 m_irqs
= (abs
->irqs
[i
] + abs
->irqs
[j
]) / 2;
1029 /* take the middle entry as is */
1030 m_irqs
= abs
->irqs
[i
];
1033 mvwprintw(screen
, (*voff
)++, 2,
1034 "med:%*s%17.1Lf", max_padd
, "", m_irqs
);
1038 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
1039 const struct ifstat
*abs
, int *voff
)
1041 if (iswireless(abs
)) {
1042 mvwprintw(screen
, (*voff
)++, 2,
1043 "linkqual: %7d/%d (%d/t) ",
1044 abs
->wifi
.link_qual
,
1045 abs
->wifi
.link_qual_max
,
1046 rel
->wifi
.link_qual
);
1048 mvwprintw(screen
, (*voff
)++, 2,
1049 "signal: %8d dBm (%d dBm/t) ",
1050 abs
->wifi
.signal_level
,
1051 rel
->wifi
.signal_level
);
1055 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
1056 const struct ifstat
*abs
, const struct avg_stat
*avg
,
1057 int *first
, uint64_t ms_interval
, unsigned int top_cpus
,
1060 unsigned int cpus
, top
;
1061 int voff
= 1, cvoff
= 2;
1062 u32 rate
= device_bitrate(ifname
);
1066 cpus
= get_number_cpus();
1067 top
= min(cpus
, top_cpus
);
1069 stats_top(rel
, abs
, cpus
);
1071 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
1073 screen_header(screen
, ifname
, &voff
, rate
, ms_interval
, top_cpus
);
1076 screen_net_dev_rel(screen
, rel
, &voff
);
1078 if (show_percentage
) {
1080 screen_net_dev_percentage(screen
, rel
, &voff
, rate
);
1084 screen_net_dev_abs(screen
, abs
, &voff
);
1087 screen_sys(screen
, rel
, abs
, &voff
);
1090 screen_mem_swap(screen
, abs
, &voff
);
1093 screen_percpu_states(screen
, rel
, avg
, top
, &voff
);
1095 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
1098 screen_percpu_irqs_rel(screen
, rel
, avg
, top
, &voff
);
1100 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
1103 screen_percpu_irqs_abs(screen
, abs
, avg
, top
, &voff
);
1106 screen_wireless(screen
, rel
, abs
, &voff
);
1109 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
1113 mvwprintw(screen
, cvoff
, 2, "(consider to increase "
1114 "your sampling interval, e.g. -t %d)",
1115 rate
> SPEED_1000
? 10000 : 1000);
1117 mvwprintw(screen
, cvoff
, 2, " "
1125 static int screen_main(const char *ifname
, uint64_t ms_interval
,
1126 unsigned int top_cpus
, bool suppress_warnings
,
1127 bool omit_header __maybe_unused
)
1130 u32 rate
= device_bitrate(ifname
);
1131 bool need_info
= false;
1133 stats_screen
= screen_init(true);
1135 if (((rate
> SPEED_1000
&& ms_interval
<= 1000) ||
1136 (rate
= SPEED_1000
&& ms_interval
< 1000)) &&
1142 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
1145 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
, &stats_avg
,
1146 &first
, ms_interval
, top_cpus
, need_info
);
1148 stats_sample_generic(ifname
, ms_interval
);
1156 static void term_csv(const struct ifstat
*rel
, const struct ifstat
*abs
)
1158 unsigned int cpus
, i
;
1160 printf("%ld ", time(NULL
));
1162 printf("%llu ", rel
->rx_bytes
);
1163 printf("%llu ", rel
->rx_packets
);
1164 printf("%llu ", rel
->rx_drops
);
1165 printf("%llu ", rel
->rx_errors
);
1167 printf("%llu ", abs
->rx_bytes
);
1168 printf("%llu ", abs
->rx_packets
);
1169 printf("%llu ", abs
->rx_drops
);
1170 printf("%llu ", abs
->rx_errors
);
1172 printf("%llu ", rel
->tx_bytes
);
1173 printf("%llu ", rel
->tx_packets
);
1174 printf("%llu ", rel
->tx_drops
);
1175 printf("%llu ", rel
->tx_errors
);
1177 printf("%llu ", abs
->tx_bytes
);
1178 printf("%llu ", abs
->tx_packets
);
1179 printf("%llu ", abs
->tx_drops
);
1180 printf("%llu ", abs
->tx_errors
);
1182 printf("%u ", rel
->cswitch
);
1183 printf("%lu ", abs
->mem_free
);
1184 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
1185 printf("%lu ", abs
->mem_total
);
1186 printf("%lu ", abs
->swap_free
);
1187 printf("%lu ", abs
->swap_total
- abs
->swap_free
);
1188 printf("%lu ", abs
->swap_total
);
1189 printf("%u ", abs
->procs_total
);
1190 printf("%u ", abs
->procs_run
);
1191 printf("%u ", abs
->procs_iow
);
1193 cpus
= get_number_cpus();
1195 for (i
= 0; i
< cpus
; ++i
) {
1196 printf("%lu ", rel
->cpu_user
[i
]);
1197 printf("%lu ", rel
->cpu_nice
[i
]);
1198 printf("%lu ", rel
->cpu_sys
[i
]);
1199 printf("%lu ", rel
->cpu_idle
[i
]);
1200 printf("%lu ", rel
->cpu_iow
[i
]);
1202 printf("%llu ", rel
->irqs
[i
]);
1203 printf("%llu ", abs
->irqs
[i
]);
1205 printf("%llu ", rel
->irqs_srx
[i
]);
1206 printf("%llu ", abs
->irqs_srx
[i
]);
1208 printf("%llu ", rel
->irqs_stx
[i
]);
1209 printf("%llu ", abs
->irqs_stx
[i
]);
1212 if (iswireless(abs
)) {
1213 printf("%u ", rel
->wifi
.link_qual
);
1214 printf("%u ", abs
->wifi
.link_qual
);
1215 printf("%u ", abs
->wifi
.link_qual_max
);
1217 printf("%d ", rel
->wifi
.signal_level
);
1218 printf("%d ", abs
->wifi
.signal_level
);
1225 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1226 uint64_t ms_interval
)
1228 unsigned int cpus
, i
, j
= 1;
1230 printf("# gnuplot dump (#col:description)\n");
1231 printf("# networking interface: %s\n", ifname
);
1232 printf("# sampling interval (t): %lu ms\n", ms_interval
);
1233 printf("# %d:unixtime ", j
++);
1235 printf("%d:rx-bytes-per-t ", j
++);
1236 printf("%d:rx-pkts-per-t ", j
++);
1237 printf("%d:rx-drops-per-t ", j
++);
1238 printf("%d:rx-errors-per-t ", j
++);
1240 printf("%d:rx-bytes ", j
++);
1241 printf("%d:rx-pkts ", j
++);
1242 printf("%d:rx-drops ", j
++);
1243 printf("%d:rx-errors ", j
++);
1245 printf("%d:tx-bytes-per-t ", j
++);
1246 printf("%d:tx-pkts-per-t ", j
++);
1247 printf("%d:tx-drops-per-t ", j
++);
1248 printf("%d:tx-errors-per-t ", j
++);
1250 printf("%d:tx-bytes ", j
++);
1251 printf("%d:tx-pkts ", j
++);
1252 printf("%d:tx-drops ", j
++);
1253 printf("%d:tx-errors ", j
++);
1255 printf("%d:context-switches-per-t ", j
++);
1256 printf("%d:mem-free ", j
++);
1257 printf("%d:mem-used ", j
++);
1258 printf("%d:mem-total ", j
++);
1259 printf("%d:swap-free ", j
++);
1260 printf("%d:swap-used ", j
++);
1261 printf("%d:swap-total ", j
++);
1262 printf("%d:procs-total ", j
++);
1263 printf("%d:procs-in-run ", j
++);
1264 printf("%d:procs-in-iow ", j
++);
1266 cpus
= get_number_cpus();
1268 for (i
= 0; i
< cpus
; ++i
) {
1269 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1270 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1271 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1272 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1273 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1275 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1276 printf("%d:cpu%i-net-irqs ", j
++, i
);
1278 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1279 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1280 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1281 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1284 if (iswireless(abs
)) {
1285 printf("%d:wifi-link-qual-per-t ", j
++);
1286 printf("%d:wifi-link-qual ", j
++);
1287 printf("%d:wifi-link-qual-max ", j
++);
1289 printf("%d:wifi-signal-dbm-per-t ", j
++);
1290 printf("%d:wifi-signal-dbm ", j
++);
1294 printf("# data:\n");
1298 static int term_main(const char *ifname
, uint64_t ms_interval
,
1299 unsigned int top_cpus __maybe_unused
,
1300 bool suppress_warnings __maybe_unused
,
1304 stats_sample_generic(ifname
, ms_interval
);
1308 term_csv_header(ifname
, &stats_new
, ms_interval
);
1311 term_csv(&stats_delta
, &stats_new
);
1312 } while (stats_loop
&& !sigint
);
1317 int main(int argc
, char **argv
)
1320 int c
, opt_index
, ret
, promisc
= 0;
1321 unsigned int cpus
, top_cpus
= 5;
1322 uint64_t interval
= 1000;
1323 char *ifname
= NULL
;
1324 bool suppress_warnings
= false;
1325 bool omit_header
= false;
1326 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1327 unsigned int top_cpus
, bool suppress_warnings
,
1330 func_main
= screen_main
;
1335 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1336 &opt_index
)) != EOF
) {
1345 suppress_warnings
= true;
1348 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1351 interval
= strtoul(optarg
, NULL
, 10);
1354 top_cpus
= strtoul(optarg
, NULL
, 10);
1356 panic("Number of top hitter CPUs must be greater than 0");
1365 show_percentage
= 1;
1371 func_main
= term_main
;
1380 panic("Option -%c requires an argument!\n",
1383 if (isprint(optopt
))
1384 printf("Unknown option character `0x%X\'!\n", optopt
);
1396 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1398 panic("No networking device given!\n");
1400 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1401 panic("lo is not supported!\n");
1402 if (device_mtu(ifname
) == 0)
1403 panic("This is no networking device!\n");
1405 register_signal(SIGINT
, signal_handler
);
1406 register_signal(SIGQUIT
, signal_handler
);
1407 register_signal(SIGSTOP
, signal_handler
);
1408 register_signal(SIGHUP
, signal_handler
);
1410 cpus
= get_number_cpus();
1411 top_cpus
= min(top_cpus
, cpus
);
1412 if (uname(&uts
) < 0)
1413 panic("Cannot execute uname!\n");
1415 stats_alloc(&stats_old
, cpus
);
1416 stats_alloc(&stats_new
, cpus
);
1417 stats_alloc(&stats_delta
, cpus
);
1419 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1422 ifflags
= device_enter_promiscuous_mode(ifname
);
1423 ret
= func_main(ifname
, interval
, top_cpus
, suppress_warnings
, omit_header
);
1425 device_leave_promiscuous_mode(ifname
, ifflags
);
1427 stats_release(&stats_old
);
1428 stats_release(&stats_new
);
1429 stats_release(&stats_delta
);