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>
34 int16_t link_qual
, link_qual_max
;
35 int signal_level
/*, noise_level*/;
39 long long unsigned int rx_bytes
, rx_packets
, rx_drops
, rx_errors
;
40 long long unsigned int rx_fifo
, rx_frame
, rx_multi
;
41 long long unsigned int tx_bytes
, tx_packets
, tx_drops
, tx_errors
;
42 long long unsigned int tx_fifo
, tx_colls
, tx_carrier
;
43 uint64_t mem_free
, mem_total
, mem_active
, mem_inactive
;
44 uint64_t swap_total
, swap_free
, swap_cached
;
45 uint32_t irq_nr
, procs_total
, procs_run
, procs_iow
, cswitch
;
46 struct wifi_stat wifi
;
48 * Pointer members need to be last in order for stats_zero() to work
51 long long unsigned int *irqs
, *irqs_srx
, *irqs_stx
;
52 uint64_t *cpu_user
, *cpu_sys
, *cpu_nice
, *cpu_idle
, *cpu_iow
;
58 long long unsigned int irqs_rel
, irqs_abs
;
61 static volatile sig_atomic_t sigint
= 0;
62 static struct ifstat stats_old
, stats_new
, stats_delta
;
63 static struct cpu_hit
*cpu_hits
;
64 static int stats_loop
= 0;
65 static WINDOW
*stats_screen
= NULL
;
67 static const char *short_options
= "d:t:n:vhclp";
68 static const struct option long_options
[] = {
69 {"dev", required_argument
, NULL
, 'd'},
70 {"interval", required_argument
, NULL
, 't'},
71 {"num-cpus", required_argument
, NULL
, 'n'},
72 {"promisc", no_argument
, NULL
, 'p'},
73 {"csv", no_argument
, NULL
, 'c'},
74 {"loop", no_argument
, NULL
, 'l'},
75 {"version", no_argument
, NULL
, 'v'},
76 {"help", no_argument
, NULL
, 'h'},
80 static void signal_handler(int number
)
92 static inline int iswireless(const struct ifstat
*stats
)
94 return stats
->wifi
.bitrate
> 0;
97 static void __noreturn
help(void)
99 printf("\nifpps %s, top-like kernel networking and system statistics\n",
101 puts("http://www.netsniff-ng.org\n\n"
102 "Usage: ifpps [options] || ifpps <netdev>\n"
104 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
105 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
106 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
107 " in ncurses mode (default 10)\n"
108 " -p|--promisc Promiscuous mode\n"
109 " -c|--csv Output to terminal as Gnuplot-ready data\n"
110 " -l|--loop Continuous CSV output\n"
111 " -v|--version Print version and exit\n"
112 " -h|--help Print this help and exit\n\n"
116 " ifpps -lpcd wlan0 > plot.dat\n\n"
118 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
119 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
120 "Please report bugs to <bugs@netsniff-ng.org>\n"
121 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
122 "Swiss federal institute of technology (ETH Zurich)\n"
123 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
124 "License: GNU GPL version 2.0\n"
125 "This is free software: you are free to change and redistribute it.\n"
126 "There is NO WARRANTY, to the extent permitted by law.\n");
130 static void __noreturn
version(void)
132 printf("\nifpps %s, top-like kernel networking and system statistics\n",
134 puts("http://www.netsniff-ng.org\n\n"
135 "Please report bugs to <bugs@netsniff-ng.org>\n"
136 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
137 "Swiss federal institute of technology (ETH Zurich)\n"
138 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
139 "License: GNU GPL version 2.0\n"
140 "This is free software: you are free to change and redistribute it.\n"
141 "There is NO WARRANTY, to the extent permitted by law.\n");
145 static inline int padding_from_num(int n
)
149 while ((n
/= 10) > 0);
153 #define STATS_ALLOC1(member) \
154 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
156 static void stats_alloc(struct ifstat
*stats
, int cpus
)
159 STATS_ALLOC1(irqs_srx
);
160 STATS_ALLOC1(irqs_stx
);
162 STATS_ALLOC1(cpu_user
);
163 STATS_ALLOC1(cpu_sys
);
164 STATS_ALLOC1(cpu_nice
);
165 STATS_ALLOC1(cpu_idle
);
166 STATS_ALLOC1(cpu_iow
);
169 #define STATS_ZERO1(member) \
170 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
172 static void stats_zero(struct ifstat
*stats
, int cpus
)
174 /* Only clear the non-pointer members */
175 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
178 STATS_ZERO1(irqs_srx
);
179 STATS_ZERO1(irqs_stx
);
181 STATS_ZERO1(cpu_user
);
182 STATS_ZERO1(cpu_sys
);
183 STATS_ZERO1(cpu_nice
);
184 STATS_ZERO1(cpu_idle
);
185 STATS_ZERO1(cpu_iow
);
188 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
194 fp
= fopen("/proc/net/dev", "r");
196 panic("Cannot open /proc/net/dev!\n");
198 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
199 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
201 memset(buff
, 0, sizeof(buff
));
203 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
204 buff
[sizeof(buff
) -1] = 0;
206 if (strstr(buff
, ifname
) == NULL
)
209 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
210 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
211 &stats
->rx_bytes
, &stats
->rx_packets
,
212 &stats
->rx_errors
, &stats
->rx_drops
,
213 &stats
->rx_fifo
, &stats
->rx_frame
,
214 &stats
->rx_multi
, &stats
->tx_bytes
,
215 &stats
->tx_packets
, &stats
->tx_errors
,
216 &stats
->tx_drops
, &stats
->tx_fifo
,
217 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
222 memset(buff
, 0, sizeof(buff
));
229 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
231 int ret
= -EINVAL
, i
, cpus
, try = 0;
235 struct ethtool_drvinfo drvinf
;
238 fp
= fopen("/proc/interrupts", "r");
240 panic("Cannot open /proc/interrupts!\n");
242 cpus
= get_number_cpus();
243 buff_len
= cpus
* 128;
244 buff
= xmalloc(buff_len
);
246 fseek(fp
, 0, SEEK_SET
);
247 memset(buff
, 0, buff_len
);
249 while (fgets(buff
, buff_len
, fp
) != NULL
) {
250 buff
[buff_len
- 1] = 0;
253 if (strstr(buff
, ifname
) == NULL
)
256 /* XXX: remove this one here */
257 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
258 bug_on(stats
->irq_nr
== 0);
262 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
264 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
266 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
273 memset(buff
, 0, buff_len
);
276 if (ret
== -EINVAL
&& try == 0) {
277 memset(&drvinf
, 0, sizeof(drvinf
));
278 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
281 ifname
= drvinf
.driver
;
292 static int stats_proc_softirqs(struct ifstat
*stats
)
303 fp
= fopen("/proc/softirqs", "r");
305 panic("Cannot open /proc/softirqs!\n");
307 cpus
= get_number_cpus();
308 buff_len
= cpus
* 128;
309 buff
= xmalloc(buff_len
);
311 memset(buff
, 0, buff_len
);
313 while (fgets(buff
, buff_len
, fp
) != NULL
) {
314 buff
[buff_len
- 1] = 0;
316 if ((ptr
= strstr(buff
, "NET_TX:")))
317 net_type
= softirqs_net_tx
;
318 else if ((ptr
= strstr(buff
, "NET_RX:")))
319 net_type
= softirqs_net_rx
;
323 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
325 case softirqs_net_tx
:
326 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
328 case softirqs_net_rx
:
329 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
334 memset(buff
, 0, buff_len
);
342 static int stats_proc_memory(struct ifstat
*stats
)
344 char *ptr
, buff
[256];
347 fp
= fopen("/proc/meminfo", "r");
349 panic("Cannot open /proc/meminfo!\n");
351 memset(buff
, 0, sizeof(buff
));
353 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
354 buff
[sizeof(buff
) - 1] = 0;
356 if ((ptr
= strstr(buff
, "MemTotal:"))) {
357 ptr
+= strlen("MemTotal:");
358 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
359 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
360 ptr
+= strlen("MemFree:");
361 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
362 } else if ((ptr
= strstr(buff
, "Active:"))) {
363 ptr
+= strlen("Active:");
364 stats
->mem_active
= strtoul(ptr
, &ptr
, 10);
365 } else if ((ptr
= strstr(buff
, "Inactive:"))) {
366 ptr
+= strlen("Inactive:");
367 stats
->mem_inactive
= strtoul(ptr
, &ptr
, 10);
368 } else if ((ptr
= strstr(buff
, "SwapTotal:"))) {
369 ptr
+= strlen("SwapTotal:");
370 stats
->swap_total
= strtoul(ptr
, &ptr
, 10);
371 } else if ((ptr
= strstr(buff
, "SwapFree:"))) {
372 ptr
+= strlen("SwapFree:");
373 stats
->swap_free
= strtoul(ptr
, &ptr
, 10);
374 } else if ((ptr
= strstr(buff
, "SwapCached:"))) {
375 ptr
+= strlen("SwapCached:");
376 stats
->swap_cached
= strtoul(ptr
, &ptr
, 10);
379 memset(buff
, 0, sizeof(buff
));
386 static int stats_proc_system(struct ifstat
*stats
)
389 char *ptr
, buff
[256];
392 fp
= fopen("/proc/stat", "r");
394 panic("Cannot open /proc/stat!\n");
396 cpus
= get_number_cpus();
398 memset(buff
, 0, sizeof(buff
));
400 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
401 buff
[sizeof(buff
) - 1] = 0;
403 if ((ptr
= strstr(buff
, "cpu"))) {
404 ptr
+= strlen("cpu");
408 cpu
= strtol(ptr
, &ptr
, 10);
411 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
412 &stats
->cpu_user
[cpu
],
413 &stats
->cpu_nice
[cpu
],
414 &stats
->cpu_sys
[cpu
],
415 &stats
->cpu_idle
[cpu
],
416 &stats
->cpu_iow
[cpu
]) != 5)
418 } else if ((ptr
= strstr(buff
, "ctxt"))) {
419 ptr
+= strlen("ctxt");
420 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
421 } else if ((ptr
= strstr(buff
, "procs_running"))) {
422 ptr
+= strlen("procs_running");
423 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
424 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
425 ptr
+= strlen("procs_blocked");
426 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
429 memset(buff
, 0, sizeof(buff
));
436 static int stats_proc_procs(struct ifstat
*stats
)
441 dir
= opendir("/proc");
443 panic("Cannot open /proc\n");
445 stats
->procs_total
= 0;
447 while ((e
= readdir(dir
)) != NULL
) {
448 const char *name
= e
->d_name
;
450 unsigned int pid
= strtoul(name
, &end
, 10);
453 if (pid
== 0 && end
== name
)
456 stats
->procs_total
++;
464 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
469 return dbm_val
- 0x100;
472 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
475 struct iw_statistics ws
;
477 ret
= wireless_sigqual(ifname
, &ws
);
479 stats
->wifi
.bitrate
= 0;
483 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
485 stats
->wifi
.signal_level
=
486 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
488 stats
->wifi
.link_qual
= ws
.qual
.qual
;
489 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
494 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
495 #define DIFF(member) do { \
496 if (sizeof(diff->member) != sizeof(new->member) || \
497 sizeof(diff->member) != sizeof(old->member)) \
499 bug_on((new->member - old->member) > (new->member)); \
503 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
524 DIFF1(wifi
.signal_level
);
525 DIFF1(wifi
.link_qual
);
529 cpus
= get_number_cpus();
531 for (i
= 0; i
< cpus
; ++i
) {
544 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
546 if (stats_proc_net_dev(ifname
, stats
) < 0)
547 panic("Cannot fetch device stats!\n");
548 if (stats_proc_softirqs(stats
) < 0)
549 panic("Cannot fetch software interrupts!\n");
550 if (stats_proc_memory(stats
) < 0)
551 panic("Cannot fetch memory stats!\n");
552 if (stats_proc_system(stats
) < 0)
553 panic("Cannot fetch system stats!\n");
554 if (stats_proc_procs(stats
) < 0)
555 panic("Cannot fetch process stats!\n");
557 stats_proc_interrupts((char *) ifname
, stats
);
559 stats_wireless(ifname
, stats
);
562 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
564 int cpus
= get_number_cpus();
566 stats_zero(&stats_old
, cpus
);
567 stats_zero(&stats_new
, cpus
);
568 stats_zero(&stats_delta
, cpus
);
570 stats_fetch(ifname
, &stats_old
);
571 usleep(ms_interval
* 1000);
572 stats_fetch(ifname
, &stats_new
);
574 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
577 static int cmp_hits(const void *p1
, const void *p2
)
579 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
582 * We want the hits sorted in descending order, thus reverse the return
585 if (h1
->hit
== h2
->hit
)
587 else if (h1
->hit
< h2
->hit
)
593 static int cmp_irqs_rel(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
->irqs_rel
== h2
->irqs_rel
)
603 else if (h1
->irqs_rel
< h2
->irqs_rel
)
609 static int cmp_irqs_abs(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_abs
== h2
->irqs_abs
)
619 else if (h1
->irqs_abs
< h2
->irqs_abs
)
625 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
630 for (i
= 0; i
< cpus
; ++i
) {
632 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
633 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
634 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
638 static void screen_init(WINDOW
**screen
)
640 (*screen
) = initscr();
645 nodelay((*screen
), TRUE
);
647 keypad(stdscr
, TRUE
);
653 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
654 uint64_t ms_interval
, unsigned int top_cpus
)
658 struct ethtool_drvinfo drvinf
;
659 u32 rate
= device_bitrate(ifname
);
660 int link
= ethtool_link(ifname
);
661 unsigned int cpus
= get_number_cpus();
663 memset(&drvinf
, 0, sizeof(drvinf
));
664 ethtool_drvinf(ifname
, &drvinf
);
666 memset(buff
, 0, sizeof(buff
));
668 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
670 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
671 link
== 0 ? "no" : "yes");
673 mvwprintw(screen
, (*voff
)++, 2,
674 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u%s/%u"
676 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
677 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
680 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
685 mvwprintw(screen
, (*voff
)++, 0,
686 " rx: %16.3llf MiB/t "
690 ((long double) rel
->rx_bytes
) / (1LLU << 20),
691 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
693 mvwprintw(screen
, (*voff
)++, 0,
694 " tx: %16.3llf MiB/t "
698 ((long double) rel
->tx_bytes
) / (1LLU << 20),
699 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
704 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
707 mvwprintw(screen
, (*voff
)++, 2,
712 ((long double) abs
->rx_bytes
) / (1LLU << 20),
713 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
715 mvwprintw(screen
, (*voff
)++, 2,
720 ((long double) abs
->tx_bytes
) / (1LLU << 20),
721 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
724 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
725 const struct ifstat
*abs
, int *voff
)
727 mvwprintw(screen
, (*voff
)++, 2,
732 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
735 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
737 mvwprintw(screen
, (*voff
)++, 2,
742 abs
->mem_total
/ 1024,
743 (abs
->mem_total
- abs
->mem_free
) / 1024,
744 abs
->mem_active
/ 1024,
745 abs
->mem_inactive
/ 1024);
747 mvwprintw(screen
, (*voff
)++, 2,
751 abs
->swap_total
/ 1024,
752 (abs
->swap_total
- abs
->swap_free
) / 1024,
753 abs
->swap_cached
/ 1024);
756 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
757 int *voff
, unsigned int idx
, char *tag
)
759 int max_padd
= padding_from_num(get_number_cpus());
760 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
761 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
763 mvwprintw(screen
, (*voff
)++, 2,
764 "cpu%*d%s:%s %12.1lf%% usr/t "
767 "%11.1lf%% iow/t ", max_padd
, idx
,
768 tag
, strlen(tag
) == 0 ? " " : "",
769 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
770 100.0 * rel
->cpu_sys
[idx
] / all
,
771 100.0 * rel
->cpu_idle
[idx
] / all
,
772 100.0 * rel
->cpu_iow
[idx
] / all
);
775 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
776 int top_cpus
, int *voff
)
779 int cpus
= get_number_cpus();
784 /* Display top hitter */
785 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
787 /* Make sure we don't display the min. hitter twice */
788 if (top_cpus
== cpus
)
791 for (i
= 1; i
< top_cpus
; ++i
)
792 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "");
794 /* Display minimum hitter */
796 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
799 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
800 int *voff
, unsigned int idx
, char *tag
)
802 int max_padd
= padding_from_num(get_number_cpus());
804 mvwprintw(screen
, (*voff
)++, 2,
805 "cpu%*d%s:%s %13llu irqs/t "
807 "%15llu sirq tx/t ", max_padd
, idx
,
808 tag
, strlen(tag
) == 0 ? " " : "",
814 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
815 int top_cpus
, int *voff
)
818 int cpus
= get_number_cpus();
820 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
822 if (top_cpus
== cpus
)
825 for (i
= 1; i
< top_cpus
; ++i
)
826 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "");
829 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
832 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
833 int *voff
, unsigned int idx
, char *tag
)
835 int max_padd
= padding_from_num(get_number_cpus());
837 mvwprintw(screen
, (*voff
)++, 2,
838 "cpu%*d%s:%s %13llu irqs", max_padd
, idx
,
839 tag
, strlen(tag
) == 0 ? " " : "",
843 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
844 int top_cpus
, int *voff
)
847 int cpus
= get_number_cpus();
849 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
851 if (top_cpus
== cpus
)
854 for (i
= 1; i
< top_cpus
; ++i
)
855 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "");
858 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
861 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
862 const struct ifstat
*abs
, int *voff
)
864 if (iswireless(abs
)) {
865 mvwprintw(screen
, (*voff
)++, 2,
866 "linkqual: %7d/%d (%d/t) ",
868 abs
->wifi
.link_qual_max
,
869 rel
->wifi
.link_qual
);
871 mvwprintw(screen
, (*voff
)++, 2,
872 "signal: %8d dBm (%d dBm/t) ",
873 abs
->wifi
.signal_level
,
874 rel
->wifi
.signal_level
);
878 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
879 const struct ifstat
*abs
, int *first
, uint64_t ms_interval
,
880 unsigned int top_cpus
)
882 int cpus
, top
, voff
= 1, cvoff
= 2;
886 cpus
= get_number_cpus();
887 top
= min(cpus
, top_cpus
);
889 stats_top(rel
, abs
, cpus
);
891 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
893 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
896 screen_net_dev_rel(screen
, rel
, &voff
);
899 screen_net_dev_abs(screen
, abs
, &voff
);
902 screen_sys(screen
, rel
, abs
, &voff
);
905 screen_mem_swap(screen
, abs
, &voff
);
908 screen_percpu_states(screen
, rel
, top
, &voff
);
910 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
913 screen_percpu_irqs_rel(screen
, rel
, top
, &voff
);
915 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
918 screen_percpu_irqs_abs(screen
, abs
, top
, &voff
);
921 screen_wireless(screen
, rel
, abs
, &voff
);
924 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
927 mvwprintw(screen
, cvoff
, 2, " ");
934 static void screen_end(void)
939 static int screen_main(const char *ifname
, uint64_t ms_interval
,
940 unsigned int top_cpus
)
944 screen_init(&stats_screen
);
948 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
951 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
,
952 &first
, ms_interval
, top_cpus
);
954 stats_sample_generic(ifname
, ms_interval
);
962 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
963 const struct ifstat
*abs
, uint64_t ms_interval
)
967 printf("%ld ", time(0));
969 printf("%llu ", rel
->rx_bytes
);
970 printf("%llu ", rel
->rx_packets
);
971 printf("%llu ", rel
->rx_drops
);
972 printf("%llu ", rel
->rx_errors
);
974 printf("%llu ", abs
->rx_bytes
);
975 printf("%llu ", abs
->rx_packets
);
976 printf("%llu ", abs
->rx_drops
);
977 printf("%llu ", abs
->rx_errors
);
979 printf("%llu ", rel
->tx_bytes
);
980 printf("%llu ", rel
->tx_packets
);
981 printf("%llu ", rel
->tx_drops
);
982 printf("%llu ", rel
->tx_errors
);
984 printf("%llu ", abs
->tx_bytes
);
985 printf("%llu ", abs
->tx_packets
);
986 printf("%llu ", abs
->tx_drops
);
987 printf("%llu ", abs
->tx_errors
);
989 printf("%u ", rel
->cswitch
);
990 printf("%lu ", abs
->mem_free
);
991 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
992 printf("%lu ", abs
->mem_total
);
993 printf("%lu ", abs
->swap_free
);
994 printf("%lu ", abs
->swap_total
- abs
->swap_free
);
995 printf("%lu ", abs
->swap_total
);
996 printf("%u ", abs
->procs_total
);
997 printf("%u ", abs
->procs_run
);
998 printf("%u ", abs
->procs_iow
);
1000 cpus
= get_number_cpus();
1002 for (i
= 0; i
< cpus
; ++i
) {
1003 printf("%lu ", rel
->cpu_user
[i
]);
1004 printf("%lu ", rel
->cpu_nice
[i
]);
1005 printf("%lu ", rel
->cpu_sys
[i
]);
1006 printf("%lu ", rel
->cpu_idle
[i
]);
1007 printf("%lu ", rel
->cpu_iow
[i
]);
1009 printf("%llu ", rel
->irqs
[i
]);
1010 printf("%llu ", abs
->irqs
[i
]);
1012 printf("%llu ", rel
->irqs_srx
[i
]);
1013 printf("%llu ", abs
->irqs_srx
[i
]);
1015 printf("%llu ", rel
->irqs_stx
[i
]);
1016 printf("%llu ", abs
->irqs_stx
[i
]);
1019 if (iswireless(abs
)) {
1020 printf("%u ", rel
->wifi
.link_qual
);
1021 printf("%u ", abs
->wifi
.link_qual
);
1022 printf("%u ", abs
->wifi
.link_qual_max
);
1024 printf("%d ", rel
->wifi
.signal_level
);
1025 printf("%d ", abs
->wifi
.signal_level
);
1032 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1033 uint64_t ms_interval
)
1037 printf("# gnuplot dump (#col:description)\n");
1038 printf("# networking interface: %s\n", ifname
);
1039 printf("# sampling interval (t): %lu ms\n", ms_interval
);
1040 printf("# %d:unixtime ", j
++);
1042 printf("%d:rx-bytes-per-t ", j
++);
1043 printf("%d:rx-pkts-per-t ", j
++);
1044 printf("%d:rx-drops-per-t ", j
++);
1045 printf("%d:rx-errors-per-t ", j
++);
1047 printf("%d:rx-bytes ", j
++);
1048 printf("%d:rx-pkts ", j
++);
1049 printf("%d:rx-drops ", j
++);
1050 printf("%d:rx-errors ", j
++);
1052 printf("%d:tx-bytes-per-t ", j
++);
1053 printf("%d:tx-pkts-per-t ", j
++);
1054 printf("%d:tx-drops-per-t ", j
++);
1055 printf("%d:tx-errors-per-t ", j
++);
1057 printf("%d:tx-bytes ", j
++);
1058 printf("%d:tx-pkts ", j
++);
1059 printf("%d:tx-drops ", j
++);
1060 printf("%d:tx-errors ", j
++);
1062 printf("%d:context-switches-per-t ", j
++);
1063 printf("%d:mem-free ", j
++);
1064 printf("%d:mem-used ", j
++);
1065 printf("%d:mem-total ", j
++);
1066 printf("%d:swap-free ", j
++);
1067 printf("%d:swap-used ", j
++);
1068 printf("%d:swap-total ", j
++);
1069 printf("%d:procs-total ", j
++);
1070 printf("%d:procs-in-run ", j
++);
1071 printf("%d:procs-in-iow ", j
++);
1073 cpus
= get_number_cpus();
1075 for (i
= 0; i
< cpus
; ++i
) {
1076 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1077 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1078 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1079 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1080 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1082 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1083 printf("%d:cpu%i-net-irqs ", j
++, i
);
1085 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1086 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1087 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1088 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1091 if (iswireless(abs
)) {
1092 printf("%d:wifi-link-qual-per-t ", j
++);
1093 printf("%d:wifi-link-qual ", j
++);
1094 printf("%d:wifi-link-qual-max ", j
++);
1096 printf("%d:wifi-signal-dbm-per-t ", j
++);
1097 printf("%d:wifi-signal-dbm ", j
++);
1101 printf("# data:\n");
1105 static int term_main(const char *ifname
, uint64_t ms_interval
,
1106 unsigned int top_cpus __maybe_unused
)
1111 stats_sample_generic(ifname
, ms_interval
);
1115 term_csv_header(ifname
, &stats_new
, ms_interval
);
1118 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1119 } while (stats_loop
&& !sigint
);
1124 int main(int argc
, char **argv
)
1127 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1128 unsigned int top_cpus
= 10;
1129 uint64_t interval
= 1000;
1130 char *ifname
= NULL
;
1131 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1132 unsigned int top_cpus
) = screen_main
;
1137 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1138 &opt_index
)) != EOF
) {
1147 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1150 interval
= strtoul(optarg
, NULL
, 10);
1153 top_cpus
= strtoul(optarg
, NULL
, 10);
1155 panic("Number of top hitter CPUs must be greater than 0");
1164 func_main
= term_main
;
1170 panic("Option -%c requires an argument!\n",
1173 if (isprint(optopt
))
1174 printf("Unknown option character `0x%X\'!\n", optopt
);
1186 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1188 panic("No networking device given!\n");
1190 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1191 panic("lo is not supported!\n");
1192 if (device_mtu(ifname
) == 0)
1193 panic("This is no networking device!\n");
1195 register_signal(SIGINT
, signal_handler
);
1196 register_signal(SIGHUP
, signal_handler
);
1198 cpus
= get_number_cpus();
1199 top_cpus
= min(top_cpus
, cpus
);
1201 stats_alloc(&stats_old
, cpus
);
1202 stats_alloc(&stats_new
, cpus
);
1203 stats_alloc(&stats_delta
, cpus
);
1205 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1208 ifflags
= enter_promiscuous_mode(ifname
);
1209 ret
= func_main(ifname
, interval
, top_cpus
);
1211 leave_promiscuous_mode(ifname
, ifflags
);