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
)
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",
116 puts("http://www.netsniff-ng.org\n\n"
117 "Usage: ifpps [options] || ifpps <netdev>\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"
134 " ifpps -lpcd wlan0 > plot.dat\n\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");
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");
163 static inline int padding_from_num(int n
)
168 } while ((n
/= 10) > 0);
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
)
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
));
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
)
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
)
229 fp
= fopen("/proc/net/dev", "r");
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
)
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) {
257 memset(buff
, 0, sizeof(buff
));
264 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
266 int ret
= -EINVAL
, try = 0;
267 unsigned int i
, cpus
;
271 struct ethtool_drvinfo drvinf
;
274 fp
= fopen("/proc/interrupts", "r");
276 panic("Cannot open /proc/interrupts!\n");
278 cpus
= get_number_cpus();
279 buff_len
= cpus
* 128;
280 buff
= xmalloc(buff_len
);
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
)
291 ptr
= strchr(buff
, ':');
296 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
298 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
300 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
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)
315 ifname
= drvinf
.driver
;
326 static int stats_proc_softirqs(struct ifstat
*stats
)
328 unsigned int i
, cpus
;
337 fp
= fopen("/proc/softirqs", "r");
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
;
357 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
359 case softirqs_net_tx
:
360 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
362 case softirqs_net_rx
:
363 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
368 memset(buff
, 0, buff_len
);
376 static int stats_proc_memory(struct ifstat
*stats
)
378 char *ptr
, buff
[256];
381 fp
= fopen("/proc/meminfo", "r");
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
));
420 static int stats_proc_system(struct ifstat
*stats
)
422 unsigned int cpu
, cpus
;
423 char *ptr
, buff
[256];
426 fp
= fopen("/proc/stat", "r");
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");
442 cpu
= strtol(ptr
, &ptr
, 10);
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)
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);
463 memset(buff
, 0, sizeof(buff
));
470 static int stats_proc_procs(struct ifstat
*stats
)
475 dir
= opendir("/proc");
477 panic("Cannot open /proc\n");
479 stats
->procs_total
= 0;
481 while ((e
= readdir(dir
)) != NULL
) {
482 const char *name
= e
->d_name
;
484 unsigned int pid
= strtoul(name
, &end
, 10);
487 if (pid
== 0 && end
== name
)
490 stats
->procs_total
++;
498 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
503 return dbm_val
- 0x100;
506 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
509 struct iw_statistics ws
;
511 ret
= wireless_sigqual(ifname
, &ws
);
513 stats
->wifi
.bitrate
= 0;
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
);
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)) \
533 if ((new->member - old->member) > (new->member)) { \
540 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
543 unsigned int cpus
, i
;
561 DIFF1(wifi
.signal_level
);
562 DIFF1(wifi
.link_qual
);
566 cpus
= get_number_cpus();
568 for (i
= 0; i
< cpus
; ++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
622 if (h1
->hit
== h2
->hit
)
624 else if (h1
->hit
< h2
->hit
)
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
638 if (h1
->irqs_rel
== h2
->irqs_rel
)
640 else if (h1
->irqs_rel
< h2
->irqs_rel
)
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
654 if (h1
->irqs_abs
== h2
->irqs_abs
)
656 else if (h1
->irqs_abs
< h2
->irqs_abs
)
662 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
667 memset(&stats_avg
, 0, sizeof(stats_avg
));
669 for (i
= 0; i
< cpus
; ++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
)
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
));
714 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
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
,
734 mvwprintw(screen
, (*voff
)++, 0,
735 " rx: %16.3llf MiB/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 "
747 ((long double) rel
->tx_bytes
) / (1LLU << 20),
748 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
753 static void screen_net_dev_percentage(WINDOW
*screen
, const struct ifstat
*rel
,
756 mvwprintw(screen
, (*voff
)++, 0,
757 " rx: %15.2llf%% of line rate "
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 "
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
,
770 mvwprintw(screen
, (*voff
)++, 2,
775 ((long double) abs
->rx_bytes
) / (1LLU << 20),
776 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
778 mvwprintw(screen
, (*voff
)++, 2,
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,
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,
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,
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 "
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; \
842 #define MEDIAN_ODD(member) do { \
843 m_##member = rel->member[i]; \
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
)
851 unsigned int cpus
= get_number_cpus();
852 int max_padd
= padding_from_num(cpus
);
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
)
865 for (i
= 1; i
< top_cpus
; ++i
)
866 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
868 /* Display minimum hitter */
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,
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
);
884 long double m_cpu_user
, m_cpu_nice
, m_cpu_sys
, m_cpu_idle
, m_cpu_iow
;
887 i
= cpu_hits
[cpus
/ 2].idx
;
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
);
898 /* take the middle entry as is */
899 MEDIAN_ODD(cpu_user
);
900 MEDIAN_ODD(cpu_nice
);
902 MEDIAN_ODD(cpu_idle
);
906 m_all
= m_cpu_user
+ m_cpu_sys
+ m_cpu_nice
+ m_cpu_idle
+ m_cpu_iow
;
907 mvwprintw(screen
, (*voff
)++, 2,
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 "
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
)
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
)
947 for (i
= 1; i
< top_cpus
; ++i
)
948 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
951 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
953 mvwprintw(screen
, (*voff
)++, 2,
956 "%17.1Lf", max_padd
, "",
957 avg
->irqs_rel
, avg
->irqs_srx_rel
, avg
->irqs_stx_rel
);
960 long double m_irqs
, m_irqs_srx
, m_irqs_stx
;
962 i
= cpu_hits
[cpus
/ 2].idx
;
964 /* take the mean of the 2 middle entries */
965 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
968 MEDIAN_EVEN(irqs_srx
);
969 MEDIAN_EVEN(irqs_stx
);
971 /* take the middle entry as is */
973 MEDIAN_ODD(irqs_srx
);
974 MEDIAN_ODD(irqs_stx
);
977 mvwprintw(screen
, (*voff
)++, 2,
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
)
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
)
1008 for (i
= 1; i
< top_cpus
; ++i
)
1009 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "|");
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
);
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;
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
,
1058 unsigned int cpus
, top
;
1059 int voff
= 1, cvoff
= 2;
1060 u32 rate
= device_bitrate(ifname
);
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
);
1074 screen_net_dev_rel(screen
, rel
, &voff
);
1076 if (show_percentage
) {
1078 screen_net_dev_percentage(screen
, rel
, &voff
, rate
);
1082 screen_net_dev_abs(screen
, abs
, &voff
);
1085 screen_sys(screen
, rel
, abs
, &voff
);
1088 screen_mem_swap(screen
, abs
, &voff
);
1091 screen_percpu_states(screen
, rel
, avg
, top
, &voff
);
1093 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
1096 screen_percpu_irqs_rel(screen
, rel
, avg
, top
, &voff
);
1098 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
1101 screen_percpu_irqs_abs(screen
, abs
, avg
, top
, &voff
);
1104 screen_wireless(screen
, rel
, abs
, &voff
);
1107 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
1111 mvwprintw(screen
, cvoff
, 2, "(consider to increase "
1112 "your sampling interval, e.g. -t %d)",
1113 rate
> SPEED_1000
? 10000 : 1000);
1115 mvwprintw(screen
, cvoff
, 2, " "
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
)
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)) &&
1140 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
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
);
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
);
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
++);
1292 printf("# data:\n");
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
,
1302 stats_sample_generic(ifname
, ms_interval
);
1306 term_csv_header(ifname
, &stats_new
, ms_interval
);
1309 term_csv(&stats_delta
, &stats_new
);
1310 } while (stats_loop
&& !sigint
);
1315 int main(int argc
, char **argv
)
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
,
1328 func_main
= screen_main
;
1333 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1334 &opt_index
)) != EOF
) {
1343 suppress_warnings
= true;
1346 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1349 interval
= strtoul(optarg
, NULL
, 10);
1352 top_cpus
= strtoul(optarg
, NULL
, 10);
1354 panic("Number of top hitter CPUs must be greater than 0");
1363 show_percentage
= 1;
1369 func_main
= term_main
;
1378 panic("Option -%c requires an argument!\n",
1381 if (isprint(optopt
))
1382 printf("Unknown option character `0x%X\'!\n", optopt
);
1394 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
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
));
1418 ifflags
= device_enter_promiscuous_mode(ifname
);
1419 ret
= func_main(ifname
, interval
, top_cpus
, suppress_warnings
, omit_header
);
1421 device_leave_promiscuous_mode(ifname
, ifflags
);
1423 stats_release(&stats_old
);
1424 stats_release(&stats_new
);
1425 stats_release(&stats_delta
);