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 #define STATS_RELEASE(member) \
205 do { xfree(stats->member); } while (0)
207 static void stats_release(struct ifstat
*stats
)
210 STATS_RELEASE(irqs_srx
);
211 STATS_RELEASE(irqs_stx
);
213 STATS_RELEASE(cpu_user
);
214 STATS_RELEASE(cpu_sys
);
215 STATS_RELEASE(cpu_nice
);
216 STATS_RELEASE(cpu_idle
);
217 STATS_RELEASE(cpu_iow
);
220 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
226 fp
= fopen("/proc/net/dev", "r");
228 panic("Cannot open /proc/net/dev!\n");
230 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
231 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
233 memset(buff
, 0, sizeof(buff
));
235 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
236 buff
[sizeof(buff
) -1] = 0;
238 if (strstr(buff
, ifname
) == NULL
)
241 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
242 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
243 &stats
->rx_bytes
, &stats
->rx_packets
,
244 &stats
->rx_errors
, &stats
->rx_drops
,
245 &stats
->rx_fifo
, &stats
->rx_frame
,
246 &stats
->rx_multi
, &stats
->tx_bytes
,
247 &stats
->tx_packets
, &stats
->tx_errors
,
248 &stats
->tx_drops
, &stats
->tx_fifo
,
249 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
254 memset(buff
, 0, sizeof(buff
));
261 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
263 int ret
= -EINVAL
, i
, cpus
, try = 0;
267 struct ethtool_drvinfo drvinf
;
270 fp
= fopen("/proc/interrupts", "r");
272 panic("Cannot open /proc/interrupts!\n");
274 cpus
= get_number_cpus();
275 buff_len
= cpus
* 128;
276 buff
= xmalloc(buff_len
);
278 fseek(fp
, 0, SEEK_SET
);
279 memset(buff
, 0, buff_len
);
281 while (fgets(buff
, buff_len
, fp
) != NULL
) {
282 buff
[buff_len
- 1] = 0;
285 if (strstr(buff
, ifname
) == NULL
)
288 /* XXX: remove this one here */
289 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
290 bug_on(stats
->irq_nr
== 0);
294 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
296 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
298 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
305 memset(buff
, 0, buff_len
);
308 if (ret
== -EINVAL
&& try == 0) {
309 memset(&drvinf
, 0, sizeof(drvinf
));
310 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
313 ifname
= drvinf
.driver
;
324 static int stats_proc_softirqs(struct ifstat
*stats
)
335 fp
= fopen("/proc/softirqs", "r");
337 panic("Cannot open /proc/softirqs!\n");
339 cpus
= get_number_cpus();
340 buff_len
= cpus
* 128;
341 buff
= xmalloc(buff_len
);
343 memset(buff
, 0, buff_len
);
345 while (fgets(buff
, buff_len
, fp
) != NULL
) {
346 buff
[buff_len
- 1] = 0;
348 if ((ptr
= strstr(buff
, "NET_TX:")))
349 net_type
= softirqs_net_tx
;
350 else if ((ptr
= strstr(buff
, "NET_RX:")))
351 net_type
= softirqs_net_rx
;
355 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
357 case softirqs_net_tx
:
358 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
360 case softirqs_net_rx
:
361 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
366 memset(buff
, 0, buff_len
);
374 static int stats_proc_memory(struct ifstat
*stats
)
376 char *ptr
, buff
[256];
379 fp
= fopen("/proc/meminfo", "r");
381 panic("Cannot open /proc/meminfo!\n");
383 memset(buff
, 0, sizeof(buff
));
385 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
386 buff
[sizeof(buff
) - 1] = 0;
388 if ((ptr
= strstr(buff
, "MemTotal:"))) {
389 ptr
+= strlen("MemTotal:");
390 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
391 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
392 ptr
+= strlen("MemFree:");
393 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
394 } else if ((ptr
= strstr(buff
, "Active:"))) {
395 ptr
+= strlen("Active:");
396 stats
->mem_active
= strtoul(ptr
, &ptr
, 10);
397 } else if ((ptr
= strstr(buff
, "Inactive:"))) {
398 ptr
+= strlen("Inactive:");
399 stats
->mem_inactive
= strtoul(ptr
, &ptr
, 10);
400 } else if ((ptr
= strstr(buff
, "SwapTotal:"))) {
401 ptr
+= strlen("SwapTotal:");
402 stats
->swap_total
= strtoul(ptr
, &ptr
, 10);
403 } else if ((ptr
= strstr(buff
, "SwapFree:"))) {
404 ptr
+= strlen("SwapFree:");
405 stats
->swap_free
= strtoul(ptr
, &ptr
, 10);
406 } else if ((ptr
= strstr(buff
, "SwapCached:"))) {
407 ptr
+= strlen("SwapCached:");
408 stats
->swap_cached
= strtoul(ptr
, &ptr
, 10);
411 memset(buff
, 0, sizeof(buff
));
418 static int stats_proc_system(struct ifstat
*stats
)
421 char *ptr
, buff
[256];
424 fp
= fopen("/proc/stat", "r");
426 panic("Cannot open /proc/stat!\n");
428 cpus
= get_number_cpus();
430 memset(buff
, 0, sizeof(buff
));
432 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
433 buff
[sizeof(buff
) - 1] = 0;
435 if ((ptr
= strstr(buff
, "cpu"))) {
436 ptr
+= strlen("cpu");
440 cpu
= strtol(ptr
, &ptr
, 10);
443 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
444 &stats
->cpu_user
[cpu
],
445 &stats
->cpu_nice
[cpu
],
446 &stats
->cpu_sys
[cpu
],
447 &stats
->cpu_idle
[cpu
],
448 &stats
->cpu_iow
[cpu
]) != 5)
450 } else if ((ptr
= strstr(buff
, "ctxt"))) {
451 ptr
+= strlen("ctxt");
452 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
453 } else if ((ptr
= strstr(buff
, "procs_running"))) {
454 ptr
+= strlen("procs_running");
455 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
456 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
457 ptr
+= strlen("procs_blocked");
458 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
461 memset(buff
, 0, sizeof(buff
));
468 static int stats_proc_procs(struct ifstat
*stats
)
473 dir
= opendir("/proc");
475 panic("Cannot open /proc\n");
477 stats
->procs_total
= 0;
479 while ((e
= readdir(dir
)) != NULL
) {
480 const char *name
= e
->d_name
;
482 unsigned int pid
= strtoul(name
, &end
, 10);
485 if (pid
== 0 && end
== name
)
488 stats
->procs_total
++;
496 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
501 return dbm_val
- 0x100;
504 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
507 struct iw_statistics ws
;
509 ret
= wireless_sigqual(ifname
, &ws
);
511 stats
->wifi
.bitrate
= 0;
515 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
517 stats
->wifi
.signal_level
=
518 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
520 stats
->wifi
.link_qual
= ws
.qual
.qual
;
521 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
526 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
527 #define DIFF(member) do { \
528 if (sizeof(diff->member) != sizeof(new->member) || \
529 sizeof(diff->member) != sizeof(old->member)) \
531 bug_on((new->member - old->member) > (new->member)); \
535 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
556 DIFF1(wifi
.signal_level
);
557 DIFF1(wifi
.link_qual
);
561 cpus
= get_number_cpus();
563 for (i
= 0; i
< cpus
; ++i
) {
576 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
578 if (stats_proc_net_dev(ifname
, stats
) < 0)
579 panic("Cannot fetch device stats!\n");
580 if (stats_proc_softirqs(stats
) < 0)
581 panic("Cannot fetch software interrupts!\n");
582 if (stats_proc_memory(stats
) < 0)
583 panic("Cannot fetch memory stats!\n");
584 if (stats_proc_system(stats
) < 0)
585 panic("Cannot fetch system stats!\n");
586 if (stats_proc_procs(stats
) < 0)
587 panic("Cannot fetch process stats!\n");
589 stats_proc_interrupts((char *) ifname
, stats
);
591 stats_wireless(ifname
, stats
);
594 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
596 int cpus
= get_number_cpus();
598 stats_zero(&stats_old
, cpus
);
599 stats_zero(&stats_new
, cpus
);
600 stats_zero(&stats_delta
, cpus
);
602 stats_fetch(ifname
, &stats_old
);
603 usleep(ms_interval
* 1000);
604 stats_fetch(ifname
, &stats_new
);
606 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
609 static int cmp_hits(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
->hit
== h2
->hit
)
619 else if (h1
->hit
< h2
->hit
)
625 static int cmp_irqs_rel(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_rel
== h2
->irqs_rel
)
635 else if (h1
->irqs_rel
< h2
->irqs_rel
)
641 static int cmp_irqs_abs(const void *p1
, const void *p2
)
643 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
646 * We want the hits sorted in descending order, thus reverse the return
649 if (h1
->irqs_abs
== h2
->irqs_abs
)
651 else if (h1
->irqs_abs
< h2
->irqs_abs
)
657 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
662 memset(&stats_avg
, 0, sizeof(stats_avg
));
664 for (i
= 0; i
< cpus
; ++i
) {
666 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
667 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
668 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
670 stats_avg
.cpu_user
+= rel
->cpu_user
[i
];
671 stats_avg
.cpu_sys
+= rel
->cpu_sys
[i
];
672 stats_avg
.cpu_nice
+= rel
->cpu_nice
[i
];
673 stats_avg
.cpu_idle
+= rel
->cpu_idle
[i
];
674 stats_avg
.cpu_iow
+= rel
->cpu_iow
[i
];
676 stats_avg
.irqs_abs
+= abs
->irqs
[i
];
677 stats_avg
.irqs_rel
+= rel
->irqs
[i
];
678 stats_avg
.irqs_srx_rel
+= rel
->irqs_srx
[i
];
679 stats_avg
.irqs_stx_rel
+= rel
->irqs_stx
[i
];
682 stats_avg
.cpu_user
/= cpus
;
683 stats_avg
.cpu_sys
/= cpus
;
684 stats_avg
.cpu_nice
/= cpus
;
685 stats_avg
.cpu_idle
/= cpus
;
686 stats_avg
.cpu_iow
/= cpus
;
687 stats_avg
.irqs_abs
/= cpus
;
688 stats_avg
.irqs_rel
/= cpus
;
689 stats_avg
.irqs_srx_rel
/= cpus
;
690 stats_avg
.irqs_stx_rel
/= cpus
;
693 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
694 uint64_t ms_interval
, unsigned int top_cpus
)
697 char buff
[64], machine
[64];
698 struct ethtool_drvinfo drvinf
;
699 u32 rate
= device_bitrate(ifname
);
700 int link
= ethtool_link(ifname
);
701 unsigned int cpus
= get_number_cpus();
703 memset(&drvinf
, 0, sizeof(drvinf
));
704 ethtool_drvinf(ifname
, &drvinf
);
706 memset(buff
, 0, sizeof(buff
));
707 memset(machine
, 0, sizeof(machine
));
710 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
712 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
713 link
== 0 ? "no" : "yes");
715 if (!strstr(uts
.release
, uts
.machine
))
716 slprintf(machine
, sizeof(machine
), " %s,", uts
.machine
);
718 mvwprintw(screen
, (*voff
)++, 2,
719 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
720 " ", uts
.release
, machine
,
721 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
722 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
725 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
730 mvwprintw(screen
, (*voff
)++, 0,
731 " rx: %16.3llf MiB/t "
735 ((long double) rel
->rx_bytes
) / (1LLU << 20),
736 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
738 mvwprintw(screen
, (*voff
)++, 0,
739 " tx: %16.3llf MiB/t "
743 ((long double) rel
->tx_bytes
) / (1LLU << 20),
744 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
749 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
752 mvwprintw(screen
, (*voff
)++, 2,
757 ((long double) abs
->rx_bytes
) / (1LLU << 20),
758 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
760 mvwprintw(screen
, (*voff
)++, 2,
765 ((long double) abs
->tx_bytes
) / (1LLU << 20),
766 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
769 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
770 const struct ifstat
*abs
, int *voff
)
772 mvwprintw(screen
, (*voff
)++, 2,
777 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
780 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
782 mvwprintw(screen
, (*voff
)++, 2,
787 abs
->mem_total
/ 1024,
788 (abs
->mem_total
- abs
->mem_free
) / 1024,
789 abs
->mem_active
/ 1024,
790 abs
->mem_inactive
/ 1024);
792 mvwprintw(screen
, (*voff
)++, 2,
796 abs
->swap_total
/ 1024,
797 (abs
->swap_total
- abs
->swap_free
) / 1024,
798 abs
->swap_cached
/ 1024);
801 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
802 int *voff
, unsigned int idx
, char *tag
)
804 int max_padd
= padding_from_num(get_number_cpus());
805 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
806 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
808 mvwprintw(screen
, (*voff
)++, 2,
809 "cpu%*d %s: %11.1lf%% usr/t "
814 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
815 100.0 * rel
->cpu_sys
[idx
] / all
,
816 100.0 * rel
->cpu_idle
[idx
] / all
,
817 100.0 * rel
->cpu_iow
[idx
] / all
);
820 #define MEDIAN_EVEN(member) do { \
821 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
824 #define MEDIAN_ODD(member) do { \
825 m_##member = rel->member[i]; \
828 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
829 const struct avg_stat
*avg
, int top_cpus
,
833 int cpus
= get_number_cpus();
834 int max_padd
= padding_from_num(cpus
);
840 /* Display top hitter */
841 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
843 /* Make sure we don't display the min. hitter twice */
844 if (top_cpus
== cpus
)
847 for (i
= 1; i
< top_cpus
; ++i
)
848 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
850 /* Display minimum hitter */
852 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
854 all
= avg
->cpu_user
+ avg
->cpu_sys
+ avg
->cpu_nice
+ avg
->cpu_idle
+ avg
->cpu_iow
;
855 mvwprintw(screen
, (*voff
)++, 2,
859 "%11.1lf%%", max_padd
, "",
860 100.0 * (avg
->cpu_user
+ avg
->cpu_nice
) / all
,
861 100.0 * avg
->cpu_sys
/ all
,
862 100.0 * avg
->cpu_idle
/all
,
863 100.0 * avg
->cpu_iow
/ all
);
866 long double m_cpu_user
, m_cpu_nice
, m_cpu_sys
, m_cpu_idle
, m_cpu_iow
;
869 i
= cpu_hits
[cpus
/ 2].idx
;
871 /* take the mean of the 2 middle entries */
872 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
874 MEDIAN_EVEN(cpu_user
);
875 MEDIAN_EVEN(cpu_nice
);
876 MEDIAN_EVEN(cpu_sys
);
877 MEDIAN_EVEN(cpu_idle
);
878 MEDIAN_EVEN(cpu_iow
);
880 /* take the middle entry as is */
881 MEDIAN_ODD(cpu_user
);
882 MEDIAN_ODD(cpu_nice
);
884 MEDIAN_ODD(cpu_idle
);
888 m_all
= m_cpu_user
+ m_cpu_sys
+ m_cpu_nice
+ m_cpu_idle
+ m_cpu_iow
;
889 mvwprintw(screen
, (*voff
)++, 2,
893 "%11.1Lf%%", max_padd
, "",
894 100.0 * (m_cpu_user
+ m_cpu_nice
) / m_all
,
895 100.0 * m_cpu_sys
/ m_all
,
896 100.0 * m_cpu_idle
/m_all
,
897 100.0 * m_cpu_iow
/ m_all
);
901 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
902 int *voff
, unsigned int idx
, char *tag
)
904 int max_padd
= padding_from_num(get_number_cpus());
906 mvwprintw(screen
, (*voff
)++, 2,
907 "cpu%*d %s: %12llu irqs/t "
916 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
917 const struct avg_stat
*avg
, int top_cpus
,
921 int cpus
= get_number_cpus();
922 int max_padd
= padding_from_num(cpus
);
924 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
926 if (top_cpus
== cpus
)
929 for (i
= 1; i
< top_cpus
; ++i
)
930 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
933 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
935 mvwprintw(screen
, (*voff
)++, 2,
938 "%17.1Lf", max_padd
, "",
939 avg
->irqs_rel
, avg
->irqs_srx_rel
, avg
->irqs_stx_rel
);
942 long double m_irqs
, m_irqs_srx
, m_irqs_stx
;
944 i
= cpu_hits
[cpus
/ 2].idx
;
946 /* take the mean of the 2 middle entries */
947 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
950 MEDIAN_EVEN(irqs_srx
);
951 MEDIAN_EVEN(irqs_stx
);
953 /* take the middle entry as is */
955 MEDIAN_ODD(irqs_srx
);
956 MEDIAN_ODD(irqs_stx
);
959 mvwprintw(screen
, (*voff
)++, 2,
962 "%17.1Lf", max_padd
, "",
963 m_irqs
, m_irqs_srx
, m_irqs_stx
);
967 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
968 int *voff
, unsigned int idx
, char *tag
)
970 int max_padd
= padding_from_num(get_number_cpus());
972 mvwprintw(screen
, (*voff
)++, 2,
973 "cpu%*d %s: %12llu irqs",
974 max_padd
, idx
, tag
, abs
->irqs
[idx
]);
977 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
978 const struct avg_stat
*avg
, int top_cpus
,
982 int cpus
= get_number_cpus();
983 int max_padd
= padding_from_num(cpus
);
985 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
987 if (top_cpus
== cpus
)
990 for (i
= 1; i
< top_cpus
; ++i
)
991 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "|");
994 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
996 mvwprintw(screen
, (*voff
)++, 2,
997 "avg:%*s%17.1Lf", max_padd
, "", avg
->irqs_abs
);
1002 i
= cpu_hits
[cpus
/ 2].idx
;
1003 if (cpus
% 2 == 0) {
1004 /* take the mean of the 2 middle entries */
1005 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
1007 m_irqs
= (abs
->irqs
[i
] + abs
->irqs
[j
]) / 2;
1009 /* take the middle entry as is */
1010 m_irqs
= abs
->irqs
[i
];
1013 mvwprintw(screen
, (*voff
)++, 2,
1014 "med:%*s%17.1Lf", max_padd
, "", m_irqs
);
1018 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
1019 const struct ifstat
*abs
, int *voff
)
1021 if (iswireless(abs
)) {
1022 mvwprintw(screen
, (*voff
)++, 2,
1023 "linkqual: %7d/%d (%d/t) ",
1024 abs
->wifi
.link_qual
,
1025 abs
->wifi
.link_qual_max
,
1026 rel
->wifi
.link_qual
);
1028 mvwprintw(screen
, (*voff
)++, 2,
1029 "signal: %8d dBm (%d dBm/t) ",
1030 abs
->wifi
.signal_level
,
1031 rel
->wifi
.signal_level
);
1035 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
1036 const struct ifstat
*abs
, const struct avg_stat
*avg
,
1037 int *first
, uint64_t ms_interval
, unsigned int top_cpus
,
1040 int cpus
, top
, voff
= 1, cvoff
= 2;
1041 u32 rate
= device_bitrate(ifname
);
1045 cpus
= get_number_cpus();
1046 top
= min(cpus
, top_cpus
);
1048 stats_top(rel
, abs
, cpus
);
1050 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
1052 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
1055 screen_net_dev_rel(screen
, rel
, &voff
);
1058 screen_net_dev_abs(screen
, abs
, &voff
);
1061 screen_sys(screen
, rel
, abs
, &voff
);
1064 screen_mem_swap(screen
, abs
, &voff
);
1067 screen_percpu_states(screen
, rel
, avg
, top
, &voff
);
1069 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
1072 screen_percpu_irqs_rel(screen
, rel
, avg
, top
, &voff
);
1074 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
1077 screen_percpu_irqs_abs(screen
, abs
, avg
, top
, &voff
);
1080 screen_wireless(screen
, rel
, abs
, &voff
);
1083 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
1087 mvwprintw(screen
, cvoff
, 2, "(consider to increase "
1088 "your sampling interval, e.g. -t %d)",
1089 rate
> SPEED_1000
? 10000 : 1000);
1091 mvwprintw(screen
, cvoff
, 2, " "
1099 static int screen_main(const char *ifname
, uint64_t ms_interval
,
1100 unsigned int top_cpus
, bool suppress_warnings
)
1103 u32 rate
= device_bitrate(ifname
);
1104 bool need_info
= false;
1106 stats_screen
= screen_init(true);
1108 if (((rate
> SPEED_1000
&& ms_interval
<= 1000) ||
1109 (rate
= SPEED_1000
&& ms_interval
< 1000)) &&
1115 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
1118 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
, &stats_avg
,
1119 &first
, ms_interval
, top_cpus
, need_info
);
1121 stats_sample_generic(ifname
, ms_interval
);
1129 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
1130 const struct ifstat
*abs
, uint64_t ms_interval
)
1134 printf("%ld ", time(0));
1136 printf("%llu ", rel
->rx_bytes
);
1137 printf("%llu ", rel
->rx_packets
);
1138 printf("%llu ", rel
->rx_drops
);
1139 printf("%llu ", rel
->rx_errors
);
1141 printf("%llu ", abs
->rx_bytes
);
1142 printf("%llu ", abs
->rx_packets
);
1143 printf("%llu ", abs
->rx_drops
);
1144 printf("%llu ", abs
->rx_errors
);
1146 printf("%llu ", rel
->tx_bytes
);
1147 printf("%llu ", rel
->tx_packets
);
1148 printf("%llu ", rel
->tx_drops
);
1149 printf("%llu ", rel
->tx_errors
);
1151 printf("%llu ", abs
->tx_bytes
);
1152 printf("%llu ", abs
->tx_packets
);
1153 printf("%llu ", abs
->tx_drops
);
1154 printf("%llu ", abs
->tx_errors
);
1156 printf("%u ", rel
->cswitch
);
1157 printf("%lu ", abs
->mem_free
);
1158 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
1159 printf("%lu ", abs
->mem_total
);
1160 printf("%lu ", abs
->swap_free
);
1161 printf("%lu ", abs
->swap_total
- abs
->swap_free
);
1162 printf("%lu ", abs
->swap_total
);
1163 printf("%u ", abs
->procs_total
);
1164 printf("%u ", abs
->procs_run
);
1165 printf("%u ", abs
->procs_iow
);
1167 cpus
= get_number_cpus();
1169 for (i
= 0; i
< cpus
; ++i
) {
1170 printf("%lu ", rel
->cpu_user
[i
]);
1171 printf("%lu ", rel
->cpu_nice
[i
]);
1172 printf("%lu ", rel
->cpu_sys
[i
]);
1173 printf("%lu ", rel
->cpu_idle
[i
]);
1174 printf("%lu ", rel
->cpu_iow
[i
]);
1176 printf("%llu ", rel
->irqs
[i
]);
1177 printf("%llu ", abs
->irqs
[i
]);
1179 printf("%llu ", rel
->irqs_srx
[i
]);
1180 printf("%llu ", abs
->irqs_srx
[i
]);
1182 printf("%llu ", rel
->irqs_stx
[i
]);
1183 printf("%llu ", abs
->irqs_stx
[i
]);
1186 if (iswireless(abs
)) {
1187 printf("%u ", rel
->wifi
.link_qual
);
1188 printf("%u ", abs
->wifi
.link_qual
);
1189 printf("%u ", abs
->wifi
.link_qual_max
);
1191 printf("%d ", rel
->wifi
.signal_level
);
1192 printf("%d ", abs
->wifi
.signal_level
);
1199 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1200 uint64_t ms_interval
)
1204 printf("# gnuplot dump (#col:description)\n");
1205 printf("# networking interface: %s\n", ifname
);
1206 printf("# sampling interval (t): %lu ms\n", ms_interval
);
1207 printf("# %d:unixtime ", j
++);
1209 printf("%d:rx-bytes-per-t ", j
++);
1210 printf("%d:rx-pkts-per-t ", j
++);
1211 printf("%d:rx-drops-per-t ", j
++);
1212 printf("%d:rx-errors-per-t ", j
++);
1214 printf("%d:rx-bytes ", j
++);
1215 printf("%d:rx-pkts ", j
++);
1216 printf("%d:rx-drops ", j
++);
1217 printf("%d:rx-errors ", j
++);
1219 printf("%d:tx-bytes-per-t ", j
++);
1220 printf("%d:tx-pkts-per-t ", j
++);
1221 printf("%d:tx-drops-per-t ", j
++);
1222 printf("%d:tx-errors-per-t ", j
++);
1224 printf("%d:tx-bytes ", j
++);
1225 printf("%d:tx-pkts ", j
++);
1226 printf("%d:tx-drops ", j
++);
1227 printf("%d:tx-errors ", j
++);
1229 printf("%d:context-switches-per-t ", j
++);
1230 printf("%d:mem-free ", j
++);
1231 printf("%d:mem-used ", j
++);
1232 printf("%d:mem-total ", j
++);
1233 printf("%d:swap-free ", j
++);
1234 printf("%d:swap-used ", j
++);
1235 printf("%d:swap-total ", j
++);
1236 printf("%d:procs-total ", j
++);
1237 printf("%d:procs-in-run ", j
++);
1238 printf("%d:procs-in-iow ", j
++);
1240 cpus
= get_number_cpus();
1242 for (i
= 0; i
< cpus
; ++i
) {
1243 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1244 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1245 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1246 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1247 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1249 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1250 printf("%d:cpu%i-net-irqs ", j
++, i
);
1252 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1253 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1254 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1255 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1258 if (iswireless(abs
)) {
1259 printf("%d:wifi-link-qual-per-t ", j
++);
1260 printf("%d:wifi-link-qual ", j
++);
1261 printf("%d:wifi-link-qual-max ", j
++);
1263 printf("%d:wifi-signal-dbm-per-t ", j
++);
1264 printf("%d:wifi-signal-dbm ", j
++);
1268 printf("# data:\n");
1272 static int term_main(const char *ifname
, uint64_t ms_interval
,
1273 unsigned int top_cpus __maybe_unused
,
1274 bool suppress_warnings __maybe_unused
)
1279 stats_sample_generic(ifname
, ms_interval
);
1283 term_csv_header(ifname
, &stats_new
, ms_interval
);
1286 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1287 } while (stats_loop
&& !sigint
);
1292 int main(int argc
, char **argv
)
1295 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1296 unsigned int top_cpus
= 5;
1297 uint64_t interval
= 1000;
1298 char *ifname
= NULL
;
1299 bool suppress_warnings
= false;
1300 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1301 unsigned int top_cpus
, bool suppress_warnings
);
1303 func_main
= screen_main
;
1308 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1309 &opt_index
)) != EOF
) {
1318 suppress_warnings
= true;
1321 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1324 interval
= strtoul(optarg
, NULL
, 10);
1327 top_cpus
= strtoul(optarg
, NULL
, 10);
1329 panic("Number of top hitter CPUs must be greater than 0");
1341 func_main
= term_main
;
1347 panic("Option -%c requires an argument!\n",
1350 if (isprint(optopt
))
1351 printf("Unknown option character `0x%X\'!\n", optopt
);
1363 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1365 panic("No networking device given!\n");
1367 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1368 panic("lo is not supported!\n");
1369 if (device_mtu(ifname
) == 0)
1370 panic("This is no networking device!\n");
1372 register_signal(SIGINT
, signal_handler
);
1373 register_signal(SIGHUP
, signal_handler
);
1375 cpus
= get_number_cpus();
1376 top_cpus
= min(top_cpus
, cpus
);
1377 if (uname(&uts
) < 0)
1378 panic("Cannot execute uname!\n");
1380 stats_alloc(&stats_old
, cpus
);
1381 stats_alloc(&stats_new
, cpus
);
1382 stats_alloc(&stats_delta
, cpus
);
1384 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1387 ifflags
= enter_promiscuous_mode(ifname
);
1388 ret
= func_main(ifname
, interval
, top_cpus
, suppress_warnings
);
1390 leave_promiscuous_mode(ifname
, ifflags
);
1392 stats_release(&stats_old
);
1393 stats_release(&stats_new
);
1394 stats_release(&stats_delta
);