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
, forks
;
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
, "processes"))) {
422 ptr
+= strlen("processes");
423 stats
->forks
= strtoul(ptr
, &ptr
, 10);
424 } else if ((ptr
= strstr(buff
, "procs_running"))) {
425 ptr
+= strlen("procs_running");
426 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
427 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
428 ptr
+= strlen("procs_blocked");
429 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
432 memset(buff
, 0, sizeof(buff
));
439 static int stats_proc_procs(struct ifstat
*stats
)
444 dir
= opendir("/proc");
446 panic("Cannot open /proc\n");
448 stats
->procs_total
= 0;
450 while ((e
= readdir(dir
)) != NULL
) {
451 const char *name
= e
->d_name
;
453 unsigned int pid
= strtoul(name
, &end
, 10);
456 if (pid
== 0 && end
== name
)
459 stats
->procs_total
++;
467 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
472 return dbm_val
- 0x100;
475 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
478 struct iw_statistics ws
;
480 ret
= wireless_sigqual(ifname
, &ws
);
482 stats
->wifi
.bitrate
= 0;
486 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
488 stats
->wifi
.signal_level
=
489 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
491 stats
->wifi
.link_qual
= ws
.qual
.qual
;
492 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
497 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
498 #define DIFF(member) do { \
499 if (sizeof(diff->member) != sizeof(new->member) || \
500 sizeof(diff->member) != sizeof(old->member)) \
502 bug_on((new->member - old->member) > (new->member)); \
506 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
527 DIFF1(wifi
.signal_level
);
528 DIFF1(wifi
.link_qual
);
532 cpus
= get_number_cpus();
534 for (i
= 0; i
< cpus
; ++i
) {
547 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
549 if (stats_proc_net_dev(ifname
, stats
) < 0)
550 panic("Cannot fetch device stats!\n");
551 if (stats_proc_softirqs(stats
) < 0)
552 panic("Cannot fetch software interrupts!\n");
553 if (stats_proc_memory(stats
) < 0)
554 panic("Cannot fetch memory stats!\n");
555 if (stats_proc_system(stats
) < 0)
556 panic("Cannot fetch system stats!\n");
557 if (stats_proc_procs(stats
) < 0)
558 panic("Cannot fetch process stats!\n");
560 stats_proc_interrupts((char *) ifname
, stats
);
562 stats_wireless(ifname
, stats
);
565 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
567 int cpus
= get_number_cpus();
569 stats_zero(&stats_old
, cpus
);
570 stats_zero(&stats_new
, cpus
);
571 stats_zero(&stats_delta
, cpus
);
573 stats_fetch(ifname
, &stats_old
);
574 usleep(ms_interval
* 1000);
575 stats_fetch(ifname
, &stats_new
);
577 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
580 static int cmp_hits(const void *p1
, const void *p2
)
582 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
585 * We want the hits sorted in descending order, thus reverse the return
588 if (h1
->hit
== h2
->hit
)
590 else if (h1
->hit
< h2
->hit
)
596 static int cmp_irqs_rel(const void *p1
, const void *p2
)
598 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
601 * We want the hits sorted in descending order, thus reverse the return
604 if (h1
->irqs_rel
== h2
->irqs_rel
)
606 else if (h1
->irqs_rel
< h2
->irqs_rel
)
612 static int cmp_irqs_abs(const void *p1
, const void *p2
)
614 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
617 * We want the hits sorted in descending order, thus reverse the return
620 if (h1
->irqs_abs
== h2
->irqs_abs
)
622 else if (h1
->irqs_abs
< h2
->irqs_abs
)
628 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
633 for (i
= 0; i
< cpus
; ++i
) {
635 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
636 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
637 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
641 static void screen_init(WINDOW
**screen
)
643 (*screen
) = initscr();
648 nodelay((*screen
), TRUE
);
650 keypad(stdscr
, TRUE
);
656 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
657 uint64_t ms_interval
, unsigned int top_cpus
)
661 struct ethtool_drvinfo drvinf
;
662 u32 rate
= device_bitrate(ifname
);
663 int link
= ethtool_link(ifname
);
664 unsigned int cpus
= get_number_cpus();
666 memset(&drvinf
, 0, sizeof(drvinf
));
667 ethtool_drvinf(ifname
, &drvinf
);
669 memset(buff
, 0, sizeof(buff
));
671 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
673 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
674 link
== 0 ? "no" : "yes");
676 mvwprintw(screen
, (*voff
)++, 2,
677 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u%s/%u"
679 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
680 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
683 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
688 mvwprintw(screen
, (*voff
)++, 0,
689 " rx: %16.3llf MiB/t "
693 ((long double) rel
->rx_bytes
) / (1LLU << 20),
694 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
696 mvwprintw(screen
, (*voff
)++, 0,
697 " tx: %16.3llf MiB/t "
701 ((long double) rel
->tx_bytes
) / (1LLU << 20),
702 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
707 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
710 mvwprintw(screen
, (*voff
)++, 2,
715 ((long double) abs
->rx_bytes
) / (1LLU << 20),
716 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
718 mvwprintw(screen
, (*voff
)++, 2,
723 ((long double) abs
->tx_bytes
) / (1LLU << 20),
724 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
727 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
728 const struct ifstat
*abs
, int *voff
)
730 mvwprintw(screen
, (*voff
)++, 2,
735 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
738 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
740 mvwprintw(screen
, (*voff
)++, 2,
745 abs
->mem_total
/ 1024,
746 (abs
->mem_total
- abs
->mem_free
) / 1024,
747 abs
->mem_active
/ 1024,
748 abs
->mem_inactive
/ 1024);
750 mvwprintw(screen
, (*voff
)++, 2,
754 abs
->swap_total
/ 1024,
755 (abs
->swap_total
- abs
->swap_free
) / 1024,
756 abs
->swap_cached
/ 1024);
759 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
760 int *voff
, unsigned int idx
, char *tag
)
762 int max_padd
= padding_from_num(get_number_cpus());
763 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
764 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
766 mvwprintw(screen
, (*voff
)++, 2,
767 "cpu%*d%s:%s %13.1lf%% usr/t "
770 "%11.1lf%% iow/t ", max_padd
, idx
,
771 tag
, strlen(tag
) == 0 ? " " : "",
772 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
773 100.0 * rel
->cpu_sys
[idx
] / all
,
774 100.0 * rel
->cpu_idle
[idx
] / all
,
775 100.0 * rel
->cpu_iow
[idx
] / all
);
778 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
779 int top_cpus
, int *voff
)
782 int cpus
= get_number_cpus();
787 /* Display top hitter */
788 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
790 /* Make sure we don't display the min. hitter twice */
791 if (top_cpus
== cpus
)
794 for (i
= 1; i
< top_cpus
; ++i
)
795 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "");
797 /* Display minimum hitter */
799 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
802 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
803 int *voff
, unsigned int idx
, char *tag
)
805 int max_padd
= padding_from_num(get_number_cpus());
807 mvwprintw(screen
, (*voff
)++, 2,
808 "cpu%*d%s:%s %14llu irqs/t "
810 "%15llu sirq tx/t ", max_padd
, idx
,
811 tag
, strlen(tag
) == 0 ? " " : "",
817 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
818 int top_cpus
, int *voff
)
821 int cpus
= get_number_cpus();
823 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
825 if (top_cpus
== cpus
)
828 for (i
= 1; i
< top_cpus
; ++i
)
829 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "");
832 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
835 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
836 int *voff
, unsigned int idx
, char *tag
)
838 int max_padd
= padding_from_num(get_number_cpus());
840 mvwprintw(screen
, (*voff
)++, 2,
841 "cpu%*d%s:%s %14llu irqs", max_padd
, idx
,
842 tag
, strlen(tag
) == 0 ? " " : "",
846 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
847 int top_cpus
, int *voff
)
850 int cpus
= get_number_cpus();
852 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
854 if (top_cpus
== cpus
)
857 for (i
= 1; i
< top_cpus
; ++i
)
858 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "");
861 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
864 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
865 const struct ifstat
*abs
, int *voff
)
867 if (iswireless(abs
)) {
868 mvwprintw(screen
, (*voff
)++, 2,
869 "linkqual: %7d/%d (%d/t) ",
871 abs
->wifi
.link_qual_max
,
872 rel
->wifi
.link_qual
);
874 mvwprintw(screen
, (*voff
)++, 2,
875 "signal: %8d dBm (%d dBm/t) ",
876 abs
->wifi
.signal_level
,
877 rel
->wifi
.signal_level
);
881 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
882 const struct ifstat
*abs
, int *first
, uint64_t ms_interval
,
883 unsigned int top_cpus
)
885 int cpus
, top
, voff
= 1, cvoff
= 2;
889 cpus
= get_number_cpus();
890 top
= min(cpus
, top_cpus
);
892 stats_top(rel
, abs
, cpus
);
894 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
896 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
899 screen_net_dev_rel(screen
, rel
, &voff
);
902 screen_net_dev_abs(screen
, abs
, &voff
);
905 screen_sys(screen
, rel
, abs
, &voff
);
908 screen_mem_swap(screen
, abs
, &voff
);
911 screen_percpu_states(screen
, rel
, top
, &voff
);
913 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
916 screen_percpu_irqs_rel(screen
, rel
, top
, &voff
);
918 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
921 screen_percpu_irqs_abs(screen
, abs
, top
, &voff
);
924 screen_wireless(screen
, rel
, abs
, &voff
);
927 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
930 mvwprintw(screen
, cvoff
, 2, " ");
937 static void screen_end(void)
942 static int screen_main(const char *ifname
, uint64_t ms_interval
,
943 unsigned int top_cpus
)
947 screen_init(&stats_screen
);
951 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
954 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
,
955 &first
, ms_interval
, top_cpus
);
957 stats_sample_generic(ifname
, ms_interval
);
965 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
966 const struct ifstat
*abs
, uint64_t ms_interval
)
970 printf("%ld ", time(0));
972 printf("%llu ", rel
->rx_bytes
);
973 printf("%llu ", rel
->rx_packets
);
974 printf("%llu ", rel
->rx_drops
);
975 printf("%llu ", rel
->rx_errors
);
977 printf("%llu ", abs
->rx_bytes
);
978 printf("%llu ", abs
->rx_packets
);
979 printf("%llu ", abs
->rx_drops
);
980 printf("%llu ", abs
->rx_errors
);
982 printf("%llu ", rel
->tx_bytes
);
983 printf("%llu ", rel
->tx_packets
);
984 printf("%llu ", rel
->tx_drops
);
985 printf("%llu ", rel
->tx_errors
);
987 printf("%llu ", abs
->tx_bytes
);
988 printf("%llu ", abs
->tx_packets
);
989 printf("%llu ", abs
->tx_drops
);
990 printf("%llu ", abs
->tx_errors
);
992 printf("%u ", rel
->cswitch
);
993 printf("%lu ", abs
->mem_free
);
994 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
995 printf("%lu ", abs
->mem_total
);
996 printf("%lu ", abs
->swap_free
);
997 printf("%lu ", abs
->swap_total
- abs
->swap_free
);
998 printf("%lu ", abs
->swap_total
);
999 printf("%u ", abs
->procs_total
);
1000 printf("%u ", abs
->procs_run
);
1001 printf("%u ", abs
->procs_iow
);
1003 cpus
= get_number_cpus();
1005 for (i
= 0; i
< cpus
; ++i
) {
1006 printf("%lu ", rel
->cpu_user
[i
]);
1007 printf("%lu ", rel
->cpu_nice
[i
]);
1008 printf("%lu ", rel
->cpu_sys
[i
]);
1009 printf("%lu ", rel
->cpu_idle
[i
]);
1010 printf("%lu ", rel
->cpu_iow
[i
]);
1012 printf("%llu ", rel
->irqs
[i
]);
1013 printf("%llu ", abs
->irqs
[i
]);
1015 printf("%llu ", rel
->irqs_srx
[i
]);
1016 printf("%llu ", abs
->irqs_srx
[i
]);
1018 printf("%llu ", rel
->irqs_stx
[i
]);
1019 printf("%llu ", abs
->irqs_stx
[i
]);
1022 if (iswireless(abs
)) {
1023 printf("%u ", rel
->wifi
.link_qual
);
1024 printf("%u ", abs
->wifi
.link_qual
);
1025 printf("%u ", abs
->wifi
.link_qual_max
);
1027 printf("%d ", rel
->wifi
.signal_level
);
1028 printf("%d ", abs
->wifi
.signal_level
);
1035 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1036 uint64_t ms_interval
)
1040 printf("# gnuplot dump (#col:description)\n");
1041 printf("# networking interface: %s\n", ifname
);
1042 printf("# sampling interval (t): %lu ms\n", ms_interval
);
1043 printf("# %d:unixtime ", j
++);
1045 printf("%d:rx-bytes-per-t ", j
++);
1046 printf("%d:rx-pkts-per-t ", j
++);
1047 printf("%d:rx-drops-per-t ", j
++);
1048 printf("%d:rx-errors-per-t ", j
++);
1050 printf("%d:rx-bytes ", j
++);
1051 printf("%d:rx-pkts ", j
++);
1052 printf("%d:rx-drops ", j
++);
1053 printf("%d:rx-errors ", j
++);
1055 printf("%d:tx-bytes-per-t ", j
++);
1056 printf("%d:tx-pkts-per-t ", j
++);
1057 printf("%d:tx-drops-per-t ", j
++);
1058 printf("%d:tx-errors-per-t ", j
++);
1060 printf("%d:tx-bytes ", j
++);
1061 printf("%d:tx-pkts ", j
++);
1062 printf("%d:tx-drops ", j
++);
1063 printf("%d:tx-errors ", j
++);
1065 printf("%d:context-switches-per-t ", j
++);
1066 printf("%d:mem-free ", j
++);
1067 printf("%d:mem-used ", j
++);
1068 printf("%d:mem-total ", j
++);
1069 printf("%d:swap-free ", j
++);
1070 printf("%d:swap-used ", j
++);
1071 printf("%d:swap-total ", j
++);
1072 printf("%d:procs-total ", j
++);
1073 printf("%d:procs-in-run ", j
++);
1074 printf("%d:procs-in-iow ", j
++);
1076 cpus
= get_number_cpus();
1078 for (i
= 0; i
< cpus
; ++i
) {
1079 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1080 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1081 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1082 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1083 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1085 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1086 printf("%d:cpu%i-net-irqs ", j
++, i
);
1088 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1089 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1090 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1091 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1094 if (iswireless(abs
)) {
1095 printf("%d:wifi-link-qual-per-t ", j
++);
1096 printf("%d:wifi-link-qual ", j
++);
1097 printf("%d:wifi-link-qual-max ", j
++);
1099 printf("%d:wifi-signal-dbm-per-t ", j
++);
1100 printf("%d:wifi-signal-dbm ", j
++);
1104 printf("# data:\n");
1108 static int term_main(const char *ifname
, uint64_t ms_interval
,
1109 unsigned int top_cpus __maybe_unused
)
1114 stats_sample_generic(ifname
, ms_interval
);
1118 term_csv_header(ifname
, &stats_new
, ms_interval
);
1121 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1122 } while (stats_loop
&& !sigint
);
1127 int main(int argc
, char **argv
)
1130 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1131 unsigned int top_cpus
= 10;
1132 uint64_t interval
= 1000;
1133 char *ifname
= NULL
;
1134 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1135 unsigned int top_cpus
) = screen_main
;
1140 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1141 &opt_index
)) != EOF
) {
1150 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1153 interval
= strtoul(optarg
, NULL
, 10);
1156 top_cpus
= strtoul(optarg
, NULL
, 10);
1158 panic("Number of top hitter CPUs must be greater than 0");
1167 func_main
= term_main
;
1173 panic("Option -%c requires an argument!\n",
1176 if (isprint(optopt
))
1177 printf("Unknown option character `0x%X\'!\n", optopt
);
1189 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1191 panic("No networking device given!\n");
1193 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1194 panic("lo is not supported!\n");
1195 if (device_mtu(ifname
) == 0)
1196 panic("This is no networking device!\n");
1198 register_signal(SIGINT
, signal_handler
);
1199 register_signal(SIGHUP
, signal_handler
);
1201 cpus
= get_number_cpus();
1202 top_cpus
= min(top_cpus
, cpus
);
1204 stats_alloc(&stats_old
, cpus
);
1205 stats_alloc(&stats_new
, cpus
);
1206 stats_alloc(&stats_delta
, cpus
);
1208 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1211 ifflags
= enter_promiscuous_mode(ifname
);
1212 ret
= func_main(ifname
, interval
, top_cpus
);
1214 leave_promiscuous_mode(ifname
, ifflags
);