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>
36 int16_t link_qual
, link_qual_max
;
37 int signal_level
/*, noise_level*/;
41 long long unsigned int rx_bytes
, rx_packets
, rx_drops
, rx_errors
;
42 long long unsigned int rx_fifo
, rx_frame
, rx_multi
;
43 long long unsigned int tx_bytes
, tx_packets
, tx_drops
, tx_errors
;
44 long long unsigned int tx_fifo
, tx_colls
, tx_carrier
;
45 uint64_t mem_free
, mem_total
, mem_active
, mem_inactive
;
46 uint64_t swap_total
, swap_free
, swap_cached
;
47 uint32_t irq_nr
, procs_total
, procs_run
, procs_iow
, cswitch
;
48 struct wifi_stat wifi
;
50 * Pointer members need to be last in order for stats_zero() to work
53 long long unsigned int *irqs
, *irqs_srx
, *irqs_stx
;
54 uint64_t *cpu_user
, *cpu_sys
, *cpu_nice
, *cpu_idle
, *cpu_iow
;
60 long long unsigned int irqs_rel
, irqs_abs
;
64 uint64_t cpu_user
, cpu_sys
, cpu_nice
, cpu_idle
, cpu_iow
;
65 long double irqs_abs
, irqs_rel
, irqs_srx_rel
, irqs_stx_rel
;
68 static volatile sig_atomic_t sigint
= 0;
69 static struct ifstat stats_old
, stats_new
, stats_delta
;
70 static struct cpu_hit
*cpu_hits
;
71 static struct avg_stat stats_avg
;
72 static int stats_loop
= 0;
73 static WINDOW
*stats_screen
= NULL
;
75 static const char *short_options
= "d:t:n:vhclp";
76 static const struct option long_options
[] = {
77 {"dev", required_argument
, NULL
, 'd'},
78 {"interval", required_argument
, NULL
, 't'},
79 {"num-cpus", required_argument
, NULL
, 'n'},
80 {"promisc", no_argument
, NULL
, 'p'},
81 {"csv", no_argument
, NULL
, 'c'},
82 {"loop", no_argument
, NULL
, 'l'},
83 {"version", no_argument
, NULL
, 'v'},
84 {"help", no_argument
, NULL
, 'h'},
88 static void signal_handler(int number
)
100 static inline int iswireless(const struct ifstat
*stats
)
102 return stats
->wifi
.bitrate
> 0;
105 static void __noreturn
help(void)
107 printf("\nifpps %s, top-like kernel networking and system statistics\n",
109 puts("http://www.netsniff-ng.org\n\n"
110 "Usage: ifpps [options] || ifpps <netdev>\n"
112 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
113 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
114 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
115 " in ncurses mode (default 5)\n"
116 " -p|--promisc Promiscuous mode\n"
117 " -c|--csv Output to terminal as Gnuplot-ready data\n"
118 " -l|--loop Continuous CSV output\n"
119 " -v|--version Print version and exit\n"
120 " -h|--help Print this help and exit\n\n"
124 " ifpps -lpcd wlan0 > plot.dat\n\n"
126 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
127 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
128 "Please report bugs to <bugs@netsniff-ng.org>\n"
129 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
130 "Swiss federal institute of technology (ETH Zurich)\n"
131 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
132 "License: GNU GPL version 2.0\n"
133 "This is free software: you are free to change and redistribute it.\n"
134 "There is NO WARRANTY, to the extent permitted by law.\n");
138 static void __noreturn
version(void)
140 printf("\nifpps %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
141 puts("top-like kernel networking and system statistics\n"
142 "http://www.netsniff-ng.org\n\n"
143 "Please report bugs to <bugs@netsniff-ng.org>\n"
144 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
145 "Swiss federal institute of technology (ETH Zurich)\n"
146 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
147 "License: GNU GPL version 2.0\n"
148 "This is free software: you are free to change and redistribute it.\n"
149 "There is NO WARRANTY, to the extent permitted by law.\n");
153 static inline int padding_from_num(int n
)
157 while ((n
/= 10) > 0);
161 #define STATS_ALLOC1(member) \
162 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
164 static void stats_alloc(struct ifstat
*stats
, int cpus
)
167 STATS_ALLOC1(irqs_srx
);
168 STATS_ALLOC1(irqs_stx
);
170 STATS_ALLOC1(cpu_user
);
171 STATS_ALLOC1(cpu_sys
);
172 STATS_ALLOC1(cpu_nice
);
173 STATS_ALLOC1(cpu_idle
);
174 STATS_ALLOC1(cpu_iow
);
177 #define STATS_ZERO1(member) \
178 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
180 static void stats_zero(struct ifstat
*stats
, int cpus
)
182 /* Only clear the non-pointer members */
183 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
186 STATS_ZERO1(irqs_srx
);
187 STATS_ZERO1(irqs_stx
);
189 STATS_ZERO1(cpu_user
);
190 STATS_ZERO1(cpu_sys
);
191 STATS_ZERO1(cpu_nice
);
192 STATS_ZERO1(cpu_idle
);
193 STATS_ZERO1(cpu_iow
);
196 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
202 fp
= fopen("/proc/net/dev", "r");
204 panic("Cannot open /proc/net/dev!\n");
206 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
207 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
209 memset(buff
, 0, sizeof(buff
));
211 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
212 buff
[sizeof(buff
) -1] = 0;
214 if (strstr(buff
, ifname
) == NULL
)
217 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
218 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
219 &stats
->rx_bytes
, &stats
->rx_packets
,
220 &stats
->rx_errors
, &stats
->rx_drops
,
221 &stats
->rx_fifo
, &stats
->rx_frame
,
222 &stats
->rx_multi
, &stats
->tx_bytes
,
223 &stats
->tx_packets
, &stats
->tx_errors
,
224 &stats
->tx_drops
, &stats
->tx_fifo
,
225 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
230 memset(buff
, 0, sizeof(buff
));
237 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
239 int ret
= -EINVAL
, i
, cpus
, try = 0;
243 struct ethtool_drvinfo drvinf
;
246 fp
= fopen("/proc/interrupts", "r");
248 panic("Cannot open /proc/interrupts!\n");
250 cpus
= get_number_cpus();
251 buff_len
= cpus
* 128;
252 buff
= xmalloc(buff_len
);
254 fseek(fp
, 0, SEEK_SET
);
255 memset(buff
, 0, buff_len
);
257 while (fgets(buff
, buff_len
, fp
) != NULL
) {
258 buff
[buff_len
- 1] = 0;
261 if (strstr(buff
, ifname
) == NULL
)
264 /* XXX: remove this one here */
265 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
266 bug_on(stats
->irq_nr
== 0);
270 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
272 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
274 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
281 memset(buff
, 0, buff_len
);
284 if (ret
== -EINVAL
&& try == 0) {
285 memset(&drvinf
, 0, sizeof(drvinf
));
286 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
289 ifname
= drvinf
.driver
;
300 static int stats_proc_softirqs(struct ifstat
*stats
)
311 fp
= fopen("/proc/softirqs", "r");
313 panic("Cannot open /proc/softirqs!\n");
315 cpus
= get_number_cpus();
316 buff_len
= cpus
* 128;
317 buff
= xmalloc(buff_len
);
319 memset(buff
, 0, buff_len
);
321 while (fgets(buff
, buff_len
, fp
) != NULL
) {
322 buff
[buff_len
- 1] = 0;
324 if ((ptr
= strstr(buff
, "NET_TX:")))
325 net_type
= softirqs_net_tx
;
326 else if ((ptr
= strstr(buff
, "NET_RX:")))
327 net_type
= softirqs_net_rx
;
331 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
333 case softirqs_net_tx
:
334 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
336 case softirqs_net_rx
:
337 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
342 memset(buff
, 0, buff_len
);
350 static int stats_proc_memory(struct ifstat
*stats
)
352 char *ptr
, buff
[256];
355 fp
= fopen("/proc/meminfo", "r");
357 panic("Cannot open /proc/meminfo!\n");
359 memset(buff
, 0, sizeof(buff
));
361 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
362 buff
[sizeof(buff
) - 1] = 0;
364 if ((ptr
= strstr(buff
, "MemTotal:"))) {
365 ptr
+= strlen("MemTotal:");
366 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
367 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
368 ptr
+= strlen("MemFree:");
369 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
370 } else if ((ptr
= strstr(buff
, "Active:"))) {
371 ptr
+= strlen("Active:");
372 stats
->mem_active
= strtoul(ptr
, &ptr
, 10);
373 } else if ((ptr
= strstr(buff
, "Inactive:"))) {
374 ptr
+= strlen("Inactive:");
375 stats
->mem_inactive
= strtoul(ptr
, &ptr
, 10);
376 } else if ((ptr
= strstr(buff
, "SwapTotal:"))) {
377 ptr
+= strlen("SwapTotal:");
378 stats
->swap_total
= strtoul(ptr
, &ptr
, 10);
379 } else if ((ptr
= strstr(buff
, "SwapFree:"))) {
380 ptr
+= strlen("SwapFree:");
381 stats
->swap_free
= strtoul(ptr
, &ptr
, 10);
382 } else if ((ptr
= strstr(buff
, "SwapCached:"))) {
383 ptr
+= strlen("SwapCached:");
384 stats
->swap_cached
= strtoul(ptr
, &ptr
, 10);
387 memset(buff
, 0, sizeof(buff
));
394 static int stats_proc_system(struct ifstat
*stats
)
397 char *ptr
, buff
[256];
400 fp
= fopen("/proc/stat", "r");
402 panic("Cannot open /proc/stat!\n");
404 cpus
= get_number_cpus();
406 memset(buff
, 0, sizeof(buff
));
408 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
409 buff
[sizeof(buff
) - 1] = 0;
411 if ((ptr
= strstr(buff
, "cpu"))) {
412 ptr
+= strlen("cpu");
416 cpu
= strtol(ptr
, &ptr
, 10);
419 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
420 &stats
->cpu_user
[cpu
],
421 &stats
->cpu_nice
[cpu
],
422 &stats
->cpu_sys
[cpu
],
423 &stats
->cpu_idle
[cpu
],
424 &stats
->cpu_iow
[cpu
]) != 5)
426 } else if ((ptr
= strstr(buff
, "ctxt"))) {
427 ptr
+= strlen("ctxt");
428 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
429 } else if ((ptr
= strstr(buff
, "procs_running"))) {
430 ptr
+= strlen("procs_running");
431 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
432 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
433 ptr
+= strlen("procs_blocked");
434 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
437 memset(buff
, 0, sizeof(buff
));
444 static int stats_proc_procs(struct ifstat
*stats
)
449 dir
= opendir("/proc");
451 panic("Cannot open /proc\n");
453 stats
->procs_total
= 0;
455 while ((e
= readdir(dir
)) != NULL
) {
456 const char *name
= e
->d_name
;
458 unsigned int pid
= strtoul(name
, &end
, 10);
461 if (pid
== 0 && end
== name
)
464 stats
->procs_total
++;
472 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
477 return dbm_val
- 0x100;
480 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
483 struct iw_statistics ws
;
485 ret
= wireless_sigqual(ifname
, &ws
);
487 stats
->wifi
.bitrate
= 0;
491 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
493 stats
->wifi
.signal_level
=
494 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
496 stats
->wifi
.link_qual
= ws
.qual
.qual
;
497 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
502 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
503 #define DIFF(member) do { \
504 if (sizeof(diff->member) != sizeof(new->member) || \
505 sizeof(diff->member) != sizeof(old->member)) \
507 bug_on((new->member - old->member) > (new->member)); \
511 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
532 DIFF1(wifi
.signal_level
);
533 DIFF1(wifi
.link_qual
);
537 cpus
= get_number_cpus();
539 for (i
= 0; i
< cpus
; ++i
) {
552 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
554 if (stats_proc_net_dev(ifname
, stats
) < 0)
555 panic("Cannot fetch device stats!\n");
556 if (stats_proc_softirqs(stats
) < 0)
557 panic("Cannot fetch software interrupts!\n");
558 if (stats_proc_memory(stats
) < 0)
559 panic("Cannot fetch memory stats!\n");
560 if (stats_proc_system(stats
) < 0)
561 panic("Cannot fetch system stats!\n");
562 if (stats_proc_procs(stats
) < 0)
563 panic("Cannot fetch process stats!\n");
565 stats_proc_interrupts((char *) ifname
, stats
);
567 stats_wireless(ifname
, stats
);
570 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
572 int cpus
= get_number_cpus();
574 stats_zero(&stats_old
, cpus
);
575 stats_zero(&stats_new
, cpus
);
576 stats_zero(&stats_delta
, cpus
);
578 stats_fetch(ifname
, &stats_old
);
579 usleep(ms_interval
* 1000);
580 stats_fetch(ifname
, &stats_new
);
582 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
585 static int cmp_hits(const void *p1
, const void *p2
)
587 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
590 * We want the hits sorted in descending order, thus reverse the return
593 if (h1
->hit
== h2
->hit
)
595 else if (h1
->hit
< h2
->hit
)
601 static int cmp_irqs_rel(const void *p1
, const void *p2
)
603 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
606 * We want the hits sorted in descending order, thus reverse the return
609 if (h1
->irqs_rel
== h2
->irqs_rel
)
611 else if (h1
->irqs_rel
< h2
->irqs_rel
)
617 static int cmp_irqs_abs(const void *p1
, const void *p2
)
619 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
622 * We want the hits sorted in descending order, thus reverse the return
625 if (h1
->irqs_abs
== h2
->irqs_abs
)
627 else if (h1
->irqs_abs
< h2
->irqs_abs
)
633 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
638 memset(&stats_avg
, 0, sizeof(stats_avg
));
640 for (i
= 0; i
< cpus
; ++i
) {
642 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
643 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
644 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
646 stats_avg
.cpu_user
+= rel
->cpu_user
[i
];
647 stats_avg
.cpu_sys
+= rel
->cpu_sys
[i
];
648 stats_avg
.cpu_nice
+= rel
->cpu_nice
[i
];
649 stats_avg
.cpu_idle
+= rel
->cpu_idle
[i
];
650 stats_avg
.cpu_iow
+= rel
->cpu_iow
[i
];
652 stats_avg
.irqs_abs
+= abs
->irqs
[i
];
653 stats_avg
.irqs_rel
+= rel
->irqs
[i
];
654 stats_avg
.irqs_srx_rel
+= rel
->irqs_srx
[i
];
655 stats_avg
.irqs_stx_rel
+= rel
->irqs_stx
[i
];
658 stats_avg
.cpu_user
/= cpus
;
659 stats_avg
.cpu_sys
/= cpus
;
660 stats_avg
.cpu_nice
/= cpus
;
661 stats_avg
.cpu_idle
/= cpus
;
662 stats_avg
.cpu_iow
/= cpus
;
663 stats_avg
.irqs_abs
/= cpus
;
664 stats_avg
.irqs_rel
/= cpus
;
665 stats_avg
.irqs_srx_rel
/= cpus
;
666 stats_avg
.irqs_stx_rel
/= cpus
;
669 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
670 uint64_t ms_interval
, unsigned int top_cpus
)
674 struct ethtool_drvinfo drvinf
;
675 u32 rate
= device_bitrate(ifname
);
676 int link
= ethtool_link(ifname
);
677 unsigned int cpus
= get_number_cpus();
679 memset(&drvinf
, 0, sizeof(drvinf
));
680 ethtool_drvinf(ifname
, &drvinf
);
682 memset(buff
, 0, sizeof(buff
));
684 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
686 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
687 link
== 0 ? "no" : "yes");
689 mvwprintw(screen
, (*voff
)++, 2,
690 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u%s/%u"
692 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
693 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
696 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
701 mvwprintw(screen
, (*voff
)++, 0,
702 " rx: %16.3llf MiB/t "
706 ((long double) rel
->rx_bytes
) / (1LLU << 20),
707 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
709 mvwprintw(screen
, (*voff
)++, 0,
710 " tx: %16.3llf MiB/t "
714 ((long double) rel
->tx_bytes
) / (1LLU << 20),
715 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
720 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
723 mvwprintw(screen
, (*voff
)++, 2,
728 ((long double) abs
->rx_bytes
) / (1LLU << 20),
729 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
731 mvwprintw(screen
, (*voff
)++, 2,
736 ((long double) abs
->tx_bytes
) / (1LLU << 20),
737 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
740 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
741 const struct ifstat
*abs
, int *voff
)
743 mvwprintw(screen
, (*voff
)++, 2,
748 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
751 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
753 mvwprintw(screen
, (*voff
)++, 2,
758 abs
->mem_total
/ 1024,
759 (abs
->mem_total
- abs
->mem_free
) / 1024,
760 abs
->mem_active
/ 1024,
761 abs
->mem_inactive
/ 1024);
763 mvwprintw(screen
, (*voff
)++, 2,
767 abs
->swap_total
/ 1024,
768 (abs
->swap_total
- abs
->swap_free
) / 1024,
769 abs
->swap_cached
/ 1024);
772 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
773 int *voff
, unsigned int idx
, char *tag
)
775 int max_padd
= padding_from_num(get_number_cpus());
776 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
777 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
779 mvwprintw(screen
, (*voff
)++, 2,
780 "cpu%*d%s:%s %12.1lf%% usr/t "
783 "%11.1lf%% iow/t", max_padd
, idx
,
784 tag
, strlen(tag
) == 0 ? " " : "",
785 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
786 100.0 * rel
->cpu_sys
[idx
] / all
,
787 100.0 * rel
->cpu_idle
[idx
] / all
,
788 100.0 * rel
->cpu_iow
[idx
] / all
);
791 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
792 const struct avg_stat
*avg
, int top_cpus
,
796 int cpus
= get_number_cpus();
802 /* Display top hitter */
803 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
805 /* Make sure we don't display the min. hitter twice */
806 if (top_cpus
== cpus
)
809 for (i
= 1; i
< top_cpus
; ++i
)
810 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "");
812 /* Display minimum hitter */
814 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
816 all
= avg
->cpu_user
+ avg
->cpu_sys
+ avg
->cpu_nice
+ avg
->cpu_idle
+ avg
->cpu_iow
;
817 mvwprintw(screen
, (*voff
)++, 2,
818 "avg: %14.1lf%% usr/t "
822 100.0 * (avg
->cpu_user
+ avg
->cpu_nice
) / all
,
823 100.0 * avg
->cpu_sys
/ all
,
824 100.0 * avg
->cpu_idle
/all
,
825 100.0 * avg
->cpu_iow
/ all
);
828 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
829 int *voff
, unsigned int idx
, char *tag
)
831 int max_padd
= padding_from_num(get_number_cpus());
833 mvwprintw(screen
, (*voff
)++, 2,
834 "cpu%*d%s:%s %13llu irqs/t "
836 "%17llu sirq tx/t", max_padd
, idx
,
837 tag
, strlen(tag
) == 0 ? " " : "",
843 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
844 const struct avg_stat
*avg
, int top_cpus
,
848 int cpus
= get_number_cpus();
850 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
852 if (top_cpus
== cpus
)
855 for (i
= 1; i
< top_cpus
; ++i
)
856 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "");
859 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
861 mvwprintw(screen
, (*voff
)++, 2,
862 "avg: %15.2Lf irqs/t "
865 avg
->irqs_rel
, avg
->irqs_srx_rel
, avg
->irqs_stx_rel
);
868 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
869 int *voff
, unsigned int idx
, char *tag
)
871 int max_padd
= padding_from_num(get_number_cpus());
873 mvwprintw(screen
, (*voff
)++, 2,
874 "cpu%*d%s:%s %13llu irqs", max_padd
, idx
,
875 tag
, strlen(tag
) == 0 ? " " : "",
879 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
880 const struct avg_stat
*avg
, int top_cpus
,
884 int cpus
= get_number_cpus();
886 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
888 if (top_cpus
== cpus
)
891 for (i
= 1; i
< top_cpus
; ++i
)
892 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "");
895 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
897 mvwprintw(screen
, (*voff
)++, 2,
898 "avg: %15.2Lf irqs", avg
->irqs_abs
);
901 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
902 const struct ifstat
*abs
, int *voff
)
904 if (iswireless(abs
)) {
905 mvwprintw(screen
, (*voff
)++, 2,
906 "linkqual: %7d/%d (%d/t) ",
908 abs
->wifi
.link_qual_max
,
909 rel
->wifi
.link_qual
);
911 mvwprintw(screen
, (*voff
)++, 2,
912 "signal: %8d dBm (%d dBm/t) ",
913 abs
->wifi
.signal_level
,
914 rel
->wifi
.signal_level
);
918 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
919 const struct ifstat
*abs
, const struct avg_stat
*avg
,
920 int *first
, uint64_t ms_interval
, unsigned int top_cpus
)
922 int cpus
, top
, voff
= 1, cvoff
= 2;
926 cpus
= get_number_cpus();
927 top
= min(cpus
, top_cpus
);
929 stats_top(rel
, abs
, cpus
);
931 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
933 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
936 screen_net_dev_rel(screen
, rel
, &voff
);
939 screen_net_dev_abs(screen
, abs
, &voff
);
942 screen_sys(screen
, rel
, abs
, &voff
);
945 screen_mem_swap(screen
, abs
, &voff
);
948 screen_percpu_states(screen
, rel
, avg
, top
, &voff
);
950 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
953 screen_percpu_irqs_rel(screen
, rel
, avg
, top
, &voff
);
955 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
958 screen_percpu_irqs_abs(screen
, abs
, avg
, top
, &voff
);
961 screen_wireless(screen
, rel
, abs
, &voff
);
964 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
967 mvwprintw(screen
, cvoff
, 2, " ");
974 static int screen_main(const char *ifname
, uint64_t ms_interval
,
975 unsigned int top_cpus
)
979 stats_screen
= screen_init(true);
983 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
986 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
, &stats_avg
,
987 &first
, ms_interval
, top_cpus
);
989 stats_sample_generic(ifname
, ms_interval
);
997 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
998 const struct ifstat
*abs
, uint64_t ms_interval
)
1002 printf("%ld ", time(0));
1004 printf("%llu ", rel
->rx_bytes
);
1005 printf("%llu ", rel
->rx_packets
);
1006 printf("%llu ", rel
->rx_drops
);
1007 printf("%llu ", rel
->rx_errors
);
1009 printf("%llu ", abs
->rx_bytes
);
1010 printf("%llu ", abs
->rx_packets
);
1011 printf("%llu ", abs
->rx_drops
);
1012 printf("%llu ", abs
->rx_errors
);
1014 printf("%llu ", rel
->tx_bytes
);
1015 printf("%llu ", rel
->tx_packets
);
1016 printf("%llu ", rel
->tx_drops
);
1017 printf("%llu ", rel
->tx_errors
);
1019 printf("%llu ", abs
->tx_bytes
);
1020 printf("%llu ", abs
->tx_packets
);
1021 printf("%llu ", abs
->tx_drops
);
1022 printf("%llu ", abs
->tx_errors
);
1024 printf("%u ", rel
->cswitch
);
1025 printf("%lu ", abs
->mem_free
);
1026 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
1027 printf("%lu ", abs
->mem_total
);
1028 printf("%lu ", abs
->swap_free
);
1029 printf("%lu ", abs
->swap_total
- abs
->swap_free
);
1030 printf("%lu ", abs
->swap_total
);
1031 printf("%u ", abs
->procs_total
);
1032 printf("%u ", abs
->procs_run
);
1033 printf("%u ", abs
->procs_iow
);
1035 cpus
= get_number_cpus();
1037 for (i
= 0; i
< cpus
; ++i
) {
1038 printf("%lu ", rel
->cpu_user
[i
]);
1039 printf("%lu ", rel
->cpu_nice
[i
]);
1040 printf("%lu ", rel
->cpu_sys
[i
]);
1041 printf("%lu ", rel
->cpu_idle
[i
]);
1042 printf("%lu ", rel
->cpu_iow
[i
]);
1044 printf("%llu ", rel
->irqs
[i
]);
1045 printf("%llu ", abs
->irqs
[i
]);
1047 printf("%llu ", rel
->irqs_srx
[i
]);
1048 printf("%llu ", abs
->irqs_srx
[i
]);
1050 printf("%llu ", rel
->irqs_stx
[i
]);
1051 printf("%llu ", abs
->irqs_stx
[i
]);
1054 if (iswireless(abs
)) {
1055 printf("%u ", rel
->wifi
.link_qual
);
1056 printf("%u ", abs
->wifi
.link_qual
);
1057 printf("%u ", abs
->wifi
.link_qual_max
);
1059 printf("%d ", rel
->wifi
.signal_level
);
1060 printf("%d ", abs
->wifi
.signal_level
);
1067 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1068 uint64_t ms_interval
)
1072 printf("# gnuplot dump (#col:description)\n");
1073 printf("# networking interface: %s\n", ifname
);
1074 printf("# sampling interval (t): %lu ms\n", ms_interval
);
1075 printf("# %d:unixtime ", j
++);
1077 printf("%d:rx-bytes-per-t ", j
++);
1078 printf("%d:rx-pkts-per-t ", j
++);
1079 printf("%d:rx-drops-per-t ", j
++);
1080 printf("%d:rx-errors-per-t ", j
++);
1082 printf("%d:rx-bytes ", j
++);
1083 printf("%d:rx-pkts ", j
++);
1084 printf("%d:rx-drops ", j
++);
1085 printf("%d:rx-errors ", j
++);
1087 printf("%d:tx-bytes-per-t ", j
++);
1088 printf("%d:tx-pkts-per-t ", j
++);
1089 printf("%d:tx-drops-per-t ", j
++);
1090 printf("%d:tx-errors-per-t ", j
++);
1092 printf("%d:tx-bytes ", j
++);
1093 printf("%d:tx-pkts ", j
++);
1094 printf("%d:tx-drops ", j
++);
1095 printf("%d:tx-errors ", j
++);
1097 printf("%d:context-switches-per-t ", j
++);
1098 printf("%d:mem-free ", j
++);
1099 printf("%d:mem-used ", j
++);
1100 printf("%d:mem-total ", j
++);
1101 printf("%d:swap-free ", j
++);
1102 printf("%d:swap-used ", j
++);
1103 printf("%d:swap-total ", j
++);
1104 printf("%d:procs-total ", j
++);
1105 printf("%d:procs-in-run ", j
++);
1106 printf("%d:procs-in-iow ", j
++);
1108 cpus
= get_number_cpus();
1110 for (i
= 0; i
< cpus
; ++i
) {
1111 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1112 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1113 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1114 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1115 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1117 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1118 printf("%d:cpu%i-net-irqs ", j
++, i
);
1120 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1121 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1122 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1123 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1126 if (iswireless(abs
)) {
1127 printf("%d:wifi-link-qual-per-t ", j
++);
1128 printf("%d:wifi-link-qual ", j
++);
1129 printf("%d:wifi-link-qual-max ", j
++);
1131 printf("%d:wifi-signal-dbm-per-t ", j
++);
1132 printf("%d:wifi-signal-dbm ", j
++);
1136 printf("# data:\n");
1140 static int term_main(const char *ifname
, uint64_t ms_interval
,
1141 unsigned int top_cpus __maybe_unused
)
1146 stats_sample_generic(ifname
, ms_interval
);
1150 term_csv_header(ifname
, &stats_new
, ms_interval
);
1153 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1154 } while (stats_loop
&& !sigint
);
1159 int main(int argc
, char **argv
)
1162 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1163 unsigned int top_cpus
= 5;
1164 uint64_t interval
= 1000;
1165 char *ifname
= NULL
;
1166 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1167 unsigned int top_cpus
) = screen_main
;
1172 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1173 &opt_index
)) != EOF
) {
1182 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1185 interval
= strtoul(optarg
, NULL
, 10);
1188 top_cpus
= strtoul(optarg
, NULL
, 10);
1190 panic("Number of top hitter CPUs must be greater than 0");
1199 func_main
= term_main
;
1205 panic("Option -%c requires an argument!\n",
1208 if (isprint(optopt
))
1209 printf("Unknown option character `0x%X\'!\n", optopt
);
1221 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1223 panic("No networking device given!\n");
1225 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1226 panic("lo is not supported!\n");
1227 if (device_mtu(ifname
) == 0)
1228 panic("This is no networking device!\n");
1230 register_signal(SIGINT
, signal_handler
);
1231 register_signal(SIGHUP
, signal_handler
);
1233 cpus
= get_number_cpus();
1234 top_cpus
= min(top_cpus
, cpus
);
1236 stats_alloc(&stats_old
, cpus
);
1237 stats_alloc(&stats_new
, cpus
);
1238 stats_alloc(&stats_delta
, cpus
);
1240 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1241 memset(&stats_avg
, 0, sizeof(stats_avg
));
1244 ifflags
= enter_promiscuous_mode(ifname
);
1245 ret
= func_main(ifname
, interval
, top_cpus
);
1247 leave_promiscuous_mode(ifname
, ifflags
);