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>
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 irq_nr
, 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;
76 static WINDOW
*stats_screen
= NULL
;
77 static struct utsname uts
;
79 static const char *short_options
= "d:n:t:clmpWvh";
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 {"promisc", no_argument
, NULL
, 'p'},
88 {"no-warn", no_argument
, NULL
, 'W'},
89 {"version", no_argument
, NULL
, 'v'},
90 {"help", no_argument
, NULL
, 'h'},
94 static void signal_handler(int number
)
106 static inline int iswireless(const struct ifstat
*stats
)
108 return stats
->wifi
.bitrate
> 0;
111 static void __noreturn
help(void)
113 printf("\nifpps %s, top-like kernel networking and system statistics\n",
115 puts("http://www.netsniff-ng.org\n\n"
116 "Usage: ifpps [options] || ifpps <netdev>\n"
118 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
119 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
120 " in ncurses mode (default 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 " -p|--promisc Promiscuous mode\n"
126 " -W|--no-warn Suppress warnings\n"
127 " -v|--version Print version and exit\n"
128 " -h|--help Print this help and exit\n\n"
132 " ifpps -lpcd wlan0 > plot.dat\n\n"
134 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
135 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
136 "Please report bugs to <bugs@netsniff-ng.org>\n"
137 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
138 "Swiss federal institute of technology (ETH Zurich)\n"
139 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
140 "License: GNU GPL version 2.0\n"
141 "This is free software: you are free to change and redistribute it.\n"
142 "There is NO WARRANTY, to the extent permitted by law.\n");
146 static void __noreturn
version(void)
148 printf("\nifpps %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
149 puts("top-like kernel networking and system statistics\n"
150 "http://www.netsniff-ng.org\n\n"
151 "Please report bugs to <bugs@netsniff-ng.org>\n"
152 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
153 "Swiss federal institute of technology (ETH Zurich)\n"
154 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
155 "License: GNU GPL version 2.0\n"
156 "This is free software: you are free to change and redistribute it.\n"
157 "There is NO WARRANTY, to the extent permitted by law.\n");
161 static inline int padding_from_num(int n
)
165 while ((n
/= 10) > 0);
169 #define STATS_ALLOC1(member) \
170 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
172 static void stats_alloc(struct ifstat
*stats
, int cpus
)
175 STATS_ALLOC1(irqs_srx
);
176 STATS_ALLOC1(irqs_stx
);
178 STATS_ALLOC1(cpu_user
);
179 STATS_ALLOC1(cpu_sys
);
180 STATS_ALLOC1(cpu_nice
);
181 STATS_ALLOC1(cpu_idle
);
182 STATS_ALLOC1(cpu_iow
);
185 #define STATS_ZERO1(member) \
186 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
188 static void stats_zero(struct ifstat
*stats
, int cpus
)
190 /* Only clear the non-pointer members */
191 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
194 STATS_ZERO1(irqs_srx
);
195 STATS_ZERO1(irqs_stx
);
197 STATS_ZERO1(cpu_user
);
198 STATS_ZERO1(cpu_sys
);
199 STATS_ZERO1(cpu_nice
);
200 STATS_ZERO1(cpu_idle
);
201 STATS_ZERO1(cpu_iow
);
204 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
210 fp
= fopen("/proc/net/dev", "r");
212 panic("Cannot open /proc/net/dev!\n");
214 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
215 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
217 memset(buff
, 0, sizeof(buff
));
219 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
220 buff
[sizeof(buff
) -1] = 0;
222 if (strstr(buff
, ifname
) == NULL
)
225 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
226 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
227 &stats
->rx_bytes
, &stats
->rx_packets
,
228 &stats
->rx_errors
, &stats
->rx_drops
,
229 &stats
->rx_fifo
, &stats
->rx_frame
,
230 &stats
->rx_multi
, &stats
->tx_bytes
,
231 &stats
->tx_packets
, &stats
->tx_errors
,
232 &stats
->tx_drops
, &stats
->tx_fifo
,
233 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
238 memset(buff
, 0, sizeof(buff
));
245 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
247 int ret
= -EINVAL
, i
, cpus
, try = 0;
251 struct ethtool_drvinfo drvinf
;
254 fp
= fopen("/proc/interrupts", "r");
256 panic("Cannot open /proc/interrupts!\n");
258 cpus
= get_number_cpus();
259 buff_len
= cpus
* 128;
260 buff
= xmalloc(buff_len
);
262 fseek(fp
, 0, SEEK_SET
);
263 memset(buff
, 0, buff_len
);
265 while (fgets(buff
, buff_len
, fp
) != NULL
) {
266 buff
[buff_len
- 1] = 0;
269 if (strstr(buff
, ifname
) == NULL
)
272 /* XXX: remove this one here */
273 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
274 bug_on(stats
->irq_nr
== 0);
278 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
280 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
282 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
289 memset(buff
, 0, buff_len
);
292 if (ret
== -EINVAL
&& try == 0) {
293 memset(&drvinf
, 0, sizeof(drvinf
));
294 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
297 ifname
= drvinf
.driver
;
308 static int stats_proc_softirqs(struct ifstat
*stats
)
319 fp
= fopen("/proc/softirqs", "r");
321 panic("Cannot open /proc/softirqs!\n");
323 cpus
= get_number_cpus();
324 buff_len
= cpus
* 128;
325 buff
= xmalloc(buff_len
);
327 memset(buff
, 0, buff_len
);
329 while (fgets(buff
, buff_len
, fp
) != NULL
) {
330 buff
[buff_len
- 1] = 0;
332 if ((ptr
= strstr(buff
, "NET_TX:")))
333 net_type
= softirqs_net_tx
;
334 else if ((ptr
= strstr(buff
, "NET_RX:")))
335 net_type
= softirqs_net_rx
;
339 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
341 case softirqs_net_tx
:
342 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
344 case softirqs_net_rx
:
345 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
350 memset(buff
, 0, buff_len
);
358 static int stats_proc_memory(struct ifstat
*stats
)
360 char *ptr
, buff
[256];
363 fp
= fopen("/proc/meminfo", "r");
365 panic("Cannot open /proc/meminfo!\n");
367 memset(buff
, 0, sizeof(buff
));
369 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
370 buff
[sizeof(buff
) - 1] = 0;
372 if ((ptr
= strstr(buff
, "MemTotal:"))) {
373 ptr
+= strlen("MemTotal:");
374 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
375 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
376 ptr
+= strlen("MemFree:");
377 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
378 } else if ((ptr
= strstr(buff
, "Active:"))) {
379 ptr
+= strlen("Active:");
380 stats
->mem_active
= strtoul(ptr
, &ptr
, 10);
381 } else if ((ptr
= strstr(buff
, "Inactive:"))) {
382 ptr
+= strlen("Inactive:");
383 stats
->mem_inactive
= strtoul(ptr
, &ptr
, 10);
384 } else if ((ptr
= strstr(buff
, "SwapTotal:"))) {
385 ptr
+= strlen("SwapTotal:");
386 stats
->swap_total
= strtoul(ptr
, &ptr
, 10);
387 } else if ((ptr
= strstr(buff
, "SwapFree:"))) {
388 ptr
+= strlen("SwapFree:");
389 stats
->swap_free
= strtoul(ptr
, &ptr
, 10);
390 } else if ((ptr
= strstr(buff
, "SwapCached:"))) {
391 ptr
+= strlen("SwapCached:");
392 stats
->swap_cached
= strtoul(ptr
, &ptr
, 10);
395 memset(buff
, 0, sizeof(buff
));
402 static int stats_proc_system(struct ifstat
*stats
)
405 char *ptr
, buff
[256];
408 fp
= fopen("/proc/stat", "r");
410 panic("Cannot open /proc/stat!\n");
412 cpus
= get_number_cpus();
414 memset(buff
, 0, sizeof(buff
));
416 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
417 buff
[sizeof(buff
) - 1] = 0;
419 if ((ptr
= strstr(buff
, "cpu"))) {
420 ptr
+= strlen("cpu");
424 cpu
= strtol(ptr
, &ptr
, 10);
427 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
428 &stats
->cpu_user
[cpu
],
429 &stats
->cpu_nice
[cpu
],
430 &stats
->cpu_sys
[cpu
],
431 &stats
->cpu_idle
[cpu
],
432 &stats
->cpu_iow
[cpu
]) != 5)
434 } else if ((ptr
= strstr(buff
, "ctxt"))) {
435 ptr
+= strlen("ctxt");
436 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
437 } else if ((ptr
= strstr(buff
, "procs_running"))) {
438 ptr
+= strlen("procs_running");
439 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
440 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
441 ptr
+= strlen("procs_blocked");
442 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
445 memset(buff
, 0, sizeof(buff
));
452 static int stats_proc_procs(struct ifstat
*stats
)
457 dir
= opendir("/proc");
459 panic("Cannot open /proc\n");
461 stats
->procs_total
= 0;
463 while ((e
= readdir(dir
)) != NULL
) {
464 const char *name
= e
->d_name
;
466 unsigned int pid
= strtoul(name
, &end
, 10);
469 if (pid
== 0 && end
== name
)
472 stats
->procs_total
++;
480 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
485 return dbm_val
- 0x100;
488 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
491 struct iw_statistics ws
;
493 ret
= wireless_sigqual(ifname
, &ws
);
495 stats
->wifi
.bitrate
= 0;
499 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
501 stats
->wifi
.signal_level
=
502 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
504 stats
->wifi
.link_qual
= ws
.qual
.qual
;
505 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
510 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
511 #define DIFF(member) do { \
512 if (sizeof(diff->member) != sizeof(new->member) || \
513 sizeof(diff->member) != sizeof(old->member)) \
515 bug_on((new->member - old->member) > (new->member)); \
519 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
540 DIFF1(wifi
.signal_level
);
541 DIFF1(wifi
.link_qual
);
545 cpus
= get_number_cpus();
547 for (i
= 0; i
< cpus
; ++i
) {
560 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
562 if (stats_proc_net_dev(ifname
, stats
) < 0)
563 panic("Cannot fetch device stats!\n");
564 if (stats_proc_softirqs(stats
) < 0)
565 panic("Cannot fetch software interrupts!\n");
566 if (stats_proc_memory(stats
) < 0)
567 panic("Cannot fetch memory stats!\n");
568 if (stats_proc_system(stats
) < 0)
569 panic("Cannot fetch system stats!\n");
570 if (stats_proc_procs(stats
) < 0)
571 panic("Cannot fetch process stats!\n");
573 stats_proc_interrupts((char *) ifname
, stats
);
575 stats_wireless(ifname
, stats
);
578 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
580 int cpus
= get_number_cpus();
582 stats_zero(&stats_old
, cpus
);
583 stats_zero(&stats_new
, cpus
);
584 stats_zero(&stats_delta
, cpus
);
586 stats_fetch(ifname
, &stats_old
);
587 usleep(ms_interval
* 1000);
588 stats_fetch(ifname
, &stats_new
);
590 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
593 static int cmp_hits(const void *p1
, const void *p2
)
595 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
598 * We want the hits sorted in descending order, thus reverse the return
601 if (h1
->hit
== h2
->hit
)
603 else if (h1
->hit
< h2
->hit
)
609 static int cmp_irqs_rel(const void *p1
, const void *p2
)
611 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
614 * We want the hits sorted in descending order, thus reverse the return
617 if (h1
->irqs_rel
== h2
->irqs_rel
)
619 else if (h1
->irqs_rel
< h2
->irqs_rel
)
625 static int cmp_irqs_abs(const void *p1
, const void *p2
)
627 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
630 * We want the hits sorted in descending order, thus reverse the return
633 if (h1
->irqs_abs
== h2
->irqs_abs
)
635 else if (h1
->irqs_abs
< h2
->irqs_abs
)
641 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
646 memset(&stats_avg
, 0, sizeof(stats_avg
));
648 for (i
= 0; i
< cpus
; ++i
) {
650 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
651 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
652 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
654 stats_avg
.cpu_user
+= rel
->cpu_user
[i
];
655 stats_avg
.cpu_sys
+= rel
->cpu_sys
[i
];
656 stats_avg
.cpu_nice
+= rel
->cpu_nice
[i
];
657 stats_avg
.cpu_idle
+= rel
->cpu_idle
[i
];
658 stats_avg
.cpu_iow
+= rel
->cpu_iow
[i
];
660 stats_avg
.irqs_abs
+= abs
->irqs
[i
];
661 stats_avg
.irqs_rel
+= rel
->irqs
[i
];
662 stats_avg
.irqs_srx_rel
+= rel
->irqs_srx
[i
];
663 stats_avg
.irqs_stx_rel
+= rel
->irqs_stx
[i
];
666 stats_avg
.cpu_user
/= cpus
;
667 stats_avg
.cpu_sys
/= cpus
;
668 stats_avg
.cpu_nice
/= cpus
;
669 stats_avg
.cpu_idle
/= cpus
;
670 stats_avg
.cpu_iow
/= cpus
;
671 stats_avg
.irqs_abs
/= cpus
;
672 stats_avg
.irqs_rel
/= cpus
;
673 stats_avg
.irqs_srx_rel
/= cpus
;
674 stats_avg
.irqs_stx_rel
/= cpus
;
677 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
678 uint64_t ms_interval
, unsigned int top_cpus
)
681 char buff
[64], machine
[64];
682 struct ethtool_drvinfo drvinf
;
683 u32 rate
= device_bitrate(ifname
);
684 int link
= ethtool_link(ifname
);
685 unsigned int cpus
= get_number_cpus();
687 memset(&drvinf
, 0, sizeof(drvinf
));
688 ethtool_drvinf(ifname
, &drvinf
);
690 memset(buff
, 0, sizeof(buff
));
691 memset(machine
, 0, sizeof(machine
));
694 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
696 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
697 link
== 0 ? "no" : "yes");
699 if (!strstr(uts
.release
, uts
.machine
))
700 slprintf(machine
, sizeof(machine
), " %s,", uts
.machine
);
702 mvwprintw(screen
, (*voff
)++, 2,
703 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
704 " ", uts
.release
, machine
,
705 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
706 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
709 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
714 mvwprintw(screen
, (*voff
)++, 0,
715 " rx: %16.3llf MiB/t "
719 ((long double) rel
->rx_bytes
) / (1LLU << 20),
720 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
722 mvwprintw(screen
, (*voff
)++, 0,
723 " tx: %16.3llf MiB/t "
727 ((long double) rel
->tx_bytes
) / (1LLU << 20),
728 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
733 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
736 mvwprintw(screen
, (*voff
)++, 2,
741 ((long double) abs
->rx_bytes
) / (1LLU << 20),
742 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
744 mvwprintw(screen
, (*voff
)++, 2,
749 ((long double) abs
->tx_bytes
) / (1LLU << 20),
750 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
753 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
754 const struct ifstat
*abs
, int *voff
)
756 mvwprintw(screen
, (*voff
)++, 2,
761 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
764 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
766 mvwprintw(screen
, (*voff
)++, 2,
771 abs
->mem_total
/ 1024,
772 (abs
->mem_total
- abs
->mem_free
) / 1024,
773 abs
->mem_active
/ 1024,
774 abs
->mem_inactive
/ 1024);
776 mvwprintw(screen
, (*voff
)++, 2,
780 abs
->swap_total
/ 1024,
781 (abs
->swap_total
- abs
->swap_free
) / 1024,
782 abs
->swap_cached
/ 1024);
785 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
786 int *voff
, unsigned int idx
, char *tag
)
788 int max_padd
= padding_from_num(get_number_cpus());
789 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
790 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
792 mvwprintw(screen
, (*voff
)++, 2,
793 "cpu%*d %s: %11.1lf%% usr/t "
798 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
799 100.0 * rel
->cpu_sys
[idx
] / all
,
800 100.0 * rel
->cpu_idle
[idx
] / all
,
801 100.0 * rel
->cpu_iow
[idx
] / all
);
804 #define MEDIAN_EVEN(member) do { \
805 m_##member = (rel->member[i] + rel->member[j]) / 2; \
808 #define MEDIAN_ODD(member) do { \
809 m_##member = rel->member[i]; \
812 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
813 const struct avg_stat
*avg
, int top_cpus
,
817 int cpus
= get_number_cpus();
818 int max_padd
= padding_from_num(cpus
);
824 /* Display top hitter */
825 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
827 /* Make sure we don't display the min. hitter twice */
828 if (top_cpus
== cpus
)
831 for (i
= 1; i
< top_cpus
; ++i
)
832 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
834 /* Display minimum hitter */
836 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
838 all
= avg
->cpu_user
+ avg
->cpu_sys
+ avg
->cpu_nice
+ avg
->cpu_idle
+ avg
->cpu_iow
;
839 mvwprintw(screen
, (*voff
)++, 2,
843 "%11.1lf%%", max_padd
, "",
844 100.0 * (avg
->cpu_user
+ avg
->cpu_nice
) / all
,
845 100.0 * avg
->cpu_sys
/ all
,
846 100.0 * avg
->cpu_idle
/all
,
847 100.0 * avg
->cpu_iow
/ all
);
850 long double m_cpu_user
, m_cpu_nice
, m_cpu_sys
, m_cpu_idle
, m_cpu_iow
;
852 i
= cpu_hits
[cpus
/ 2].idx
;
854 /* take the mean of the 2 middle entries */
855 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
857 MEDIAN_EVEN(cpu_user
);
858 MEDIAN_EVEN(cpu_nice
);
859 MEDIAN_EVEN(cpu_sys
);
860 MEDIAN_EVEN(cpu_idle
);
861 MEDIAN_EVEN(cpu_iow
);
863 /* take the middle entry as is */
864 MEDIAN_ODD(cpu_user
);
865 MEDIAN_ODD(cpu_nice
);
867 MEDIAN_ODD(cpu_idle
);
871 all
= m_cpu_user
+ m_cpu_sys
+ m_cpu_nice
+ m_cpu_idle
+ m_cpu_iow
;
872 mvwprintw(screen
, (*voff
)++, 2,
876 "%11.1lf%%", max_padd
, "",
877 100.0 * (m_cpu_user
+ m_cpu_nice
) / all
,
878 100.0 * m_cpu_sys
/ all
,
879 100.0 * m_cpu_idle
/all
,
880 100.0 * m_cpu_iow
/ all
);
884 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
885 int *voff
, unsigned int idx
, char *tag
)
887 int max_padd
= padding_from_num(get_number_cpus());
889 mvwprintw(screen
, (*voff
)++, 2,
890 "cpu%*d %s: %12llu irqs/t "
899 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
900 const struct avg_stat
*avg
, int top_cpus
,
904 int cpus
= get_number_cpus();
905 int max_padd
= padding_from_num(cpus
);
907 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
909 if (top_cpus
== cpus
)
912 for (i
= 1; i
< top_cpus
; ++i
)
913 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
916 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
918 mvwprintw(screen
, (*voff
)++, 2,
921 "%17.1Lf", max_padd
, "",
922 avg
->irqs_rel
, avg
->irqs_srx_rel
, avg
->irqs_stx_rel
);
925 long double m_irqs
, m_irqs_srx
, m_irqs_stx
;
927 i
= cpu_hits
[cpus
/ 2].idx
;
929 /* take the mean of the 2 middle entries */
930 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
933 MEDIAN_EVEN(irqs_srx
);
934 MEDIAN_EVEN(irqs_stx
);
936 /* take the middle entry as is */
938 MEDIAN_ODD(irqs_srx
);
939 MEDIAN_ODD(irqs_stx
);
942 mvwprintw(screen
, (*voff
)++, 2,
945 "%17.1Lf", max_padd
, "",
946 m_irqs
, m_irqs_srx
, m_irqs_stx
);
950 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
951 int *voff
, unsigned int idx
, char *tag
)
953 int max_padd
= padding_from_num(get_number_cpus());
955 mvwprintw(screen
, (*voff
)++, 2,
956 "cpu%*d %s: %12llu irqs",
957 max_padd
, idx
, tag
, abs
->irqs
[idx
]);
960 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
961 const struct avg_stat
*avg
, int top_cpus
,
965 int cpus
= get_number_cpus();
966 int max_padd
= padding_from_num(cpus
);
968 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
970 if (top_cpus
== cpus
)
973 for (i
= 1; i
< top_cpus
; ++i
)
974 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "|");
977 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
979 mvwprintw(screen
, (*voff
)++, 2,
980 "avg:%*s%17.1Lf", max_padd
, "", avg
->irqs_abs
);
985 i
= cpu_hits
[cpus
/ 2].idx
;
987 /* take the mean of the 2 middle entries */
988 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
990 m_irqs
= (abs
->irqs
[i
] + abs
->irqs
[j
]) / 2;
992 /* take the middle entry as is */
993 m_irqs
= abs
->irqs
[i
];
996 mvwprintw(screen
, (*voff
)++, 2,
997 "med:%*s%17.1Lf", max_padd
, "", m_irqs
);
1001 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
1002 const struct ifstat
*abs
, int *voff
)
1004 if (iswireless(abs
)) {
1005 mvwprintw(screen
, (*voff
)++, 2,
1006 "linkqual: %7d/%d (%d/t) ",
1007 abs
->wifi
.link_qual
,
1008 abs
->wifi
.link_qual_max
,
1009 rel
->wifi
.link_qual
);
1011 mvwprintw(screen
, (*voff
)++, 2,
1012 "signal: %8d dBm (%d dBm/t) ",
1013 abs
->wifi
.signal_level
,
1014 rel
->wifi
.signal_level
);
1018 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
1019 const struct ifstat
*abs
, const struct avg_stat
*avg
,
1020 int *first
, uint64_t ms_interval
, unsigned int top_cpus
,
1023 int cpus
, top
, voff
= 1, cvoff
= 2;
1027 cpus
= get_number_cpus();
1028 top
= min(cpus
, top_cpus
);
1030 stats_top(rel
, abs
, cpus
);
1032 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
1034 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
1037 screen_net_dev_rel(screen
, rel
, &voff
);
1040 screen_net_dev_abs(screen
, abs
, &voff
);
1043 screen_sys(screen
, rel
, abs
, &voff
);
1046 screen_mem_swap(screen
, abs
, &voff
);
1049 screen_percpu_states(screen
, rel
, avg
, top
, &voff
);
1051 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
1054 screen_percpu_irqs_rel(screen
, rel
, avg
, top
, &voff
);
1056 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
1059 screen_percpu_irqs_abs(screen
, abs
, avg
, top
, &voff
);
1062 screen_wireless(screen
, rel
, abs
, &voff
);
1065 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
1069 mvwprintw(screen
, cvoff
, 2, "(consider to increase "
1070 "your sampling interval, e.g. -t 10000)");
1072 mvwprintw(screen
, cvoff
, 2, " "
1080 static int screen_main(const char *ifname
, uint64_t ms_interval
,
1081 unsigned int top_cpus
, bool suppress_warnings
)
1084 u32 rate
= device_bitrate(ifname
);
1085 bool need_info
= false;
1087 stats_screen
= screen_init(true);
1089 if (rate
> SPEED_1000
&& ms_interval
<= 1000 && !suppress_warnings
)
1094 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
1097 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
, &stats_avg
,
1098 &first
, ms_interval
, top_cpus
, need_info
);
1100 stats_sample_generic(ifname
, ms_interval
);
1108 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
1109 const struct ifstat
*abs
, uint64_t ms_interval
)
1113 printf("%ld ", time(0));
1115 printf("%llu ", rel
->rx_bytes
);
1116 printf("%llu ", rel
->rx_packets
);
1117 printf("%llu ", rel
->rx_drops
);
1118 printf("%llu ", rel
->rx_errors
);
1120 printf("%llu ", abs
->rx_bytes
);
1121 printf("%llu ", abs
->rx_packets
);
1122 printf("%llu ", abs
->rx_drops
);
1123 printf("%llu ", abs
->rx_errors
);
1125 printf("%llu ", rel
->tx_bytes
);
1126 printf("%llu ", rel
->tx_packets
);
1127 printf("%llu ", rel
->tx_drops
);
1128 printf("%llu ", rel
->tx_errors
);
1130 printf("%llu ", abs
->tx_bytes
);
1131 printf("%llu ", abs
->tx_packets
);
1132 printf("%llu ", abs
->tx_drops
);
1133 printf("%llu ", abs
->tx_errors
);
1135 printf("%u ", rel
->cswitch
);
1136 printf("%lu ", abs
->mem_free
);
1137 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
1138 printf("%lu ", abs
->mem_total
);
1139 printf("%lu ", abs
->swap_free
);
1140 printf("%lu ", abs
->swap_total
- abs
->swap_free
);
1141 printf("%lu ", abs
->swap_total
);
1142 printf("%u ", abs
->procs_total
);
1143 printf("%u ", abs
->procs_run
);
1144 printf("%u ", abs
->procs_iow
);
1146 cpus
= get_number_cpus();
1148 for (i
= 0; i
< cpus
; ++i
) {
1149 printf("%lu ", rel
->cpu_user
[i
]);
1150 printf("%lu ", rel
->cpu_nice
[i
]);
1151 printf("%lu ", rel
->cpu_sys
[i
]);
1152 printf("%lu ", rel
->cpu_idle
[i
]);
1153 printf("%lu ", rel
->cpu_iow
[i
]);
1155 printf("%llu ", rel
->irqs
[i
]);
1156 printf("%llu ", abs
->irqs
[i
]);
1158 printf("%llu ", rel
->irqs_srx
[i
]);
1159 printf("%llu ", abs
->irqs_srx
[i
]);
1161 printf("%llu ", rel
->irqs_stx
[i
]);
1162 printf("%llu ", abs
->irqs_stx
[i
]);
1165 if (iswireless(abs
)) {
1166 printf("%u ", rel
->wifi
.link_qual
);
1167 printf("%u ", abs
->wifi
.link_qual
);
1168 printf("%u ", abs
->wifi
.link_qual_max
);
1170 printf("%d ", rel
->wifi
.signal_level
);
1171 printf("%d ", abs
->wifi
.signal_level
);
1178 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1179 uint64_t ms_interval
)
1183 printf("# gnuplot dump (#col:description)\n");
1184 printf("# networking interface: %s\n", ifname
);
1185 printf("# sampling interval (t): %lu ms\n", ms_interval
);
1186 printf("# %d:unixtime ", j
++);
1188 printf("%d:rx-bytes-per-t ", j
++);
1189 printf("%d:rx-pkts-per-t ", j
++);
1190 printf("%d:rx-drops-per-t ", j
++);
1191 printf("%d:rx-errors-per-t ", j
++);
1193 printf("%d:rx-bytes ", j
++);
1194 printf("%d:rx-pkts ", j
++);
1195 printf("%d:rx-drops ", j
++);
1196 printf("%d:rx-errors ", j
++);
1198 printf("%d:tx-bytes-per-t ", j
++);
1199 printf("%d:tx-pkts-per-t ", j
++);
1200 printf("%d:tx-drops-per-t ", j
++);
1201 printf("%d:tx-errors-per-t ", j
++);
1203 printf("%d:tx-bytes ", j
++);
1204 printf("%d:tx-pkts ", j
++);
1205 printf("%d:tx-drops ", j
++);
1206 printf("%d:tx-errors ", j
++);
1208 printf("%d:context-switches-per-t ", j
++);
1209 printf("%d:mem-free ", j
++);
1210 printf("%d:mem-used ", j
++);
1211 printf("%d:mem-total ", j
++);
1212 printf("%d:swap-free ", j
++);
1213 printf("%d:swap-used ", j
++);
1214 printf("%d:swap-total ", j
++);
1215 printf("%d:procs-total ", j
++);
1216 printf("%d:procs-in-run ", j
++);
1217 printf("%d:procs-in-iow ", j
++);
1219 cpus
= get_number_cpus();
1221 for (i
= 0; i
< cpus
; ++i
) {
1222 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1223 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1224 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1225 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1226 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1228 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1229 printf("%d:cpu%i-net-irqs ", j
++, i
);
1231 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1232 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1233 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1234 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1237 if (iswireless(abs
)) {
1238 printf("%d:wifi-link-qual-per-t ", j
++);
1239 printf("%d:wifi-link-qual ", j
++);
1240 printf("%d:wifi-link-qual-max ", j
++);
1242 printf("%d:wifi-signal-dbm-per-t ", j
++);
1243 printf("%d:wifi-signal-dbm ", j
++);
1247 printf("# data:\n");
1251 static int term_main(const char *ifname
, uint64_t ms_interval
,
1252 unsigned int top_cpus __maybe_unused
,
1253 bool suppress_warnings __maybe_unused
)
1258 stats_sample_generic(ifname
, ms_interval
);
1262 term_csv_header(ifname
, &stats_new
, ms_interval
);
1265 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1266 } while (stats_loop
&& !sigint
);
1271 int main(int argc
, char **argv
)
1274 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1275 unsigned int top_cpus
= 5;
1276 uint64_t interval
= 1000;
1277 char *ifname
= NULL
;
1278 bool suppress_warnings
= false;
1279 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1280 unsigned int top_cpus
, bool suppress_warnings
);
1282 func_main
= screen_main
;
1287 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1288 &opt_index
)) != EOF
) {
1297 suppress_warnings
= true;
1300 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1303 interval
= strtoul(optarg
, NULL
, 10);
1306 top_cpus
= strtoul(optarg
, NULL
, 10);
1308 panic("Number of top hitter CPUs must be greater than 0");
1320 func_main
= term_main
;
1326 panic("Option -%c requires an argument!\n",
1329 if (isprint(optopt
))
1330 printf("Unknown option character `0x%X\'!\n", optopt
);
1342 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1344 panic("No networking device given!\n");
1346 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1347 panic("lo is not supported!\n");
1348 if (device_mtu(ifname
) == 0)
1349 panic("This is no networking device!\n");
1351 register_signal(SIGINT
, signal_handler
);
1352 register_signal(SIGHUP
, signal_handler
);
1354 cpus
= get_number_cpus();
1355 top_cpus
= min(top_cpus
, cpus
);
1356 if (uname(&uts
) < 0)
1357 panic("Cannot execute uname!\n");
1359 stats_alloc(&stats_old
, cpus
);
1360 stats_alloc(&stats_new
, cpus
);
1361 stats_alloc(&stats_delta
, cpus
);
1363 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1366 ifflags
= enter_promiscuous_mode(ifname
);
1367 ret
= func_main(ifname
, interval
, top_cpus
, suppress_warnings
);
1369 leave_promiscuous_mode(ifname
, ifflags
);