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 WINDOW
*stats_screen
= NULL
;
76 static struct utsname uts
;
78 static const char *short_options
= "d:t:n:vhclpW";
79 static const struct option long_options
[] = {
80 {"dev", required_argument
, NULL
, 'd'},
81 {"interval", required_argument
, NULL
, 't'},
82 {"num-cpus", required_argument
, NULL
, 'n'},
83 {"promisc", no_argument
, NULL
, 'p'},
84 {"csv", no_argument
, NULL
, 'c'},
85 {"loop", no_argument
, NULL
, 'l'},
86 {"no-warn", no_argument
, NULL
, 'W'},
87 {"version", no_argument
, NULL
, 'v'},
88 {"help", no_argument
, NULL
, 'h'},
92 static void signal_handler(int number
)
104 static inline int iswireless(const struct ifstat
*stats
)
106 return stats
->wifi
.bitrate
> 0;
109 static void __noreturn
help(void)
111 printf("\nifpps %s, top-like kernel networking and system statistics\n",
113 puts("http://www.netsniff-ng.org\n\n"
114 "Usage: ifpps [options] || ifpps <netdev>\n"
116 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
117 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
118 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
119 " in ncurses mode (default 5)\n"
120 " -p|--promisc Promiscuous mode\n"
121 " -c|--csv Output to terminal as Gnuplot-ready data\n"
122 " -l|--loop Continuous CSV output\n"
123 " -W|--no-warn Suppress warnings\n"
124 " -v|--version Print version and exit\n"
125 " -h|--help Print this help and exit\n\n"
129 " ifpps -lpcd wlan0 > plot.dat\n\n"
131 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
132 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
133 "Please report bugs to <bugs@netsniff-ng.org>\n"
134 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
135 "Swiss federal institute of technology (ETH Zurich)\n"
136 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
137 "License: GNU GPL version 2.0\n"
138 "This is free software: you are free to change and redistribute it.\n"
139 "There is NO WARRANTY, to the extent permitted by law.\n");
143 static void __noreturn
version(void)
145 printf("\nifpps %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
146 puts("top-like kernel networking and system statistics\n"
147 "http://www.netsniff-ng.org\n\n"
148 "Please report bugs to <bugs@netsniff-ng.org>\n"
149 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
150 "Swiss federal institute of technology (ETH Zurich)\n"
151 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
152 "License: GNU GPL version 2.0\n"
153 "This is free software: you are free to change and redistribute it.\n"
154 "There is NO WARRANTY, to the extent permitted by law.\n");
158 static inline int padding_from_num(int n
)
162 while ((n
/= 10) > 0);
166 #define STATS_ALLOC1(member) \
167 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
169 static void stats_alloc(struct ifstat
*stats
, int cpus
)
172 STATS_ALLOC1(irqs_srx
);
173 STATS_ALLOC1(irqs_stx
);
175 STATS_ALLOC1(cpu_user
);
176 STATS_ALLOC1(cpu_sys
);
177 STATS_ALLOC1(cpu_nice
);
178 STATS_ALLOC1(cpu_idle
);
179 STATS_ALLOC1(cpu_iow
);
182 #define STATS_ZERO1(member) \
183 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
185 static void stats_zero(struct ifstat
*stats
, int cpus
)
187 /* Only clear the non-pointer members */
188 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
191 STATS_ZERO1(irqs_srx
);
192 STATS_ZERO1(irqs_stx
);
194 STATS_ZERO1(cpu_user
);
195 STATS_ZERO1(cpu_sys
);
196 STATS_ZERO1(cpu_nice
);
197 STATS_ZERO1(cpu_idle
);
198 STATS_ZERO1(cpu_iow
);
201 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
207 fp
= fopen("/proc/net/dev", "r");
209 panic("Cannot open /proc/net/dev!\n");
211 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
212 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
214 memset(buff
, 0, sizeof(buff
));
216 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
217 buff
[sizeof(buff
) -1] = 0;
219 if (strstr(buff
, ifname
) == NULL
)
222 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
223 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
224 &stats
->rx_bytes
, &stats
->rx_packets
,
225 &stats
->rx_errors
, &stats
->rx_drops
,
226 &stats
->rx_fifo
, &stats
->rx_frame
,
227 &stats
->rx_multi
, &stats
->tx_bytes
,
228 &stats
->tx_packets
, &stats
->tx_errors
,
229 &stats
->tx_drops
, &stats
->tx_fifo
,
230 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
235 memset(buff
, 0, sizeof(buff
));
242 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
244 int ret
= -EINVAL
, i
, cpus
, try = 0;
248 struct ethtool_drvinfo drvinf
;
251 fp
= fopen("/proc/interrupts", "r");
253 panic("Cannot open /proc/interrupts!\n");
255 cpus
= get_number_cpus();
256 buff_len
= cpus
* 128;
257 buff
= xmalloc(buff_len
);
259 fseek(fp
, 0, SEEK_SET
);
260 memset(buff
, 0, buff_len
);
262 while (fgets(buff
, buff_len
, fp
) != NULL
) {
263 buff
[buff_len
- 1] = 0;
266 if (strstr(buff
, ifname
) == NULL
)
269 /* XXX: remove this one here */
270 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
271 bug_on(stats
->irq_nr
== 0);
275 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
277 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
279 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
286 memset(buff
, 0, buff_len
);
289 if (ret
== -EINVAL
&& try == 0) {
290 memset(&drvinf
, 0, sizeof(drvinf
));
291 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
294 ifname
= drvinf
.driver
;
305 static int stats_proc_softirqs(struct ifstat
*stats
)
316 fp
= fopen("/proc/softirqs", "r");
318 panic("Cannot open /proc/softirqs!\n");
320 cpus
= get_number_cpus();
321 buff_len
= cpus
* 128;
322 buff
= xmalloc(buff_len
);
324 memset(buff
, 0, buff_len
);
326 while (fgets(buff
, buff_len
, fp
) != NULL
) {
327 buff
[buff_len
- 1] = 0;
329 if ((ptr
= strstr(buff
, "NET_TX:")))
330 net_type
= softirqs_net_tx
;
331 else if ((ptr
= strstr(buff
, "NET_RX:")))
332 net_type
= softirqs_net_rx
;
336 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
338 case softirqs_net_tx
:
339 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
341 case softirqs_net_rx
:
342 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
347 memset(buff
, 0, buff_len
);
355 static int stats_proc_memory(struct ifstat
*stats
)
357 char *ptr
, buff
[256];
360 fp
= fopen("/proc/meminfo", "r");
362 panic("Cannot open /proc/meminfo!\n");
364 memset(buff
, 0, sizeof(buff
));
366 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
367 buff
[sizeof(buff
) - 1] = 0;
369 if ((ptr
= strstr(buff
, "MemTotal:"))) {
370 ptr
+= strlen("MemTotal:");
371 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
372 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
373 ptr
+= strlen("MemFree:");
374 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
375 } else if ((ptr
= strstr(buff
, "Active:"))) {
376 ptr
+= strlen("Active:");
377 stats
->mem_active
= strtoul(ptr
, &ptr
, 10);
378 } else if ((ptr
= strstr(buff
, "Inactive:"))) {
379 ptr
+= strlen("Inactive:");
380 stats
->mem_inactive
= strtoul(ptr
, &ptr
, 10);
381 } else if ((ptr
= strstr(buff
, "SwapTotal:"))) {
382 ptr
+= strlen("SwapTotal:");
383 stats
->swap_total
= strtoul(ptr
, &ptr
, 10);
384 } else if ((ptr
= strstr(buff
, "SwapFree:"))) {
385 ptr
+= strlen("SwapFree:");
386 stats
->swap_free
= strtoul(ptr
, &ptr
, 10);
387 } else if ((ptr
= strstr(buff
, "SwapCached:"))) {
388 ptr
+= strlen("SwapCached:");
389 stats
->swap_cached
= strtoul(ptr
, &ptr
, 10);
392 memset(buff
, 0, sizeof(buff
));
399 static int stats_proc_system(struct ifstat
*stats
)
402 char *ptr
, buff
[256];
405 fp
= fopen("/proc/stat", "r");
407 panic("Cannot open /proc/stat!\n");
409 cpus
= get_number_cpus();
411 memset(buff
, 0, sizeof(buff
));
413 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
414 buff
[sizeof(buff
) - 1] = 0;
416 if ((ptr
= strstr(buff
, "cpu"))) {
417 ptr
+= strlen("cpu");
421 cpu
= strtol(ptr
, &ptr
, 10);
424 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
425 &stats
->cpu_user
[cpu
],
426 &stats
->cpu_nice
[cpu
],
427 &stats
->cpu_sys
[cpu
],
428 &stats
->cpu_idle
[cpu
],
429 &stats
->cpu_iow
[cpu
]) != 5)
431 } else if ((ptr
= strstr(buff
, "ctxt"))) {
432 ptr
+= strlen("ctxt");
433 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
434 } else if ((ptr
= strstr(buff
, "procs_running"))) {
435 ptr
+= strlen("procs_running");
436 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
437 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
438 ptr
+= strlen("procs_blocked");
439 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
442 memset(buff
, 0, sizeof(buff
));
449 static int stats_proc_procs(struct ifstat
*stats
)
454 dir
= opendir("/proc");
456 panic("Cannot open /proc\n");
458 stats
->procs_total
= 0;
460 while ((e
= readdir(dir
)) != NULL
) {
461 const char *name
= e
->d_name
;
463 unsigned int pid
= strtoul(name
, &end
, 10);
466 if (pid
== 0 && end
== name
)
469 stats
->procs_total
++;
477 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
482 return dbm_val
- 0x100;
485 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
488 struct iw_statistics ws
;
490 ret
= wireless_sigqual(ifname
, &ws
);
492 stats
->wifi
.bitrate
= 0;
496 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
498 stats
->wifi
.signal_level
=
499 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
501 stats
->wifi
.link_qual
= ws
.qual
.qual
;
502 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
507 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
508 #define DIFF(member) do { \
509 if (sizeof(diff->member) != sizeof(new->member) || \
510 sizeof(diff->member) != sizeof(old->member)) \
512 bug_on((new->member - old->member) > (new->member)); \
516 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
537 DIFF1(wifi
.signal_level
);
538 DIFF1(wifi
.link_qual
);
542 cpus
= get_number_cpus();
544 for (i
= 0; i
< cpus
; ++i
) {
557 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
559 if (stats_proc_net_dev(ifname
, stats
) < 0)
560 panic("Cannot fetch device stats!\n");
561 if (stats_proc_softirqs(stats
) < 0)
562 panic("Cannot fetch software interrupts!\n");
563 if (stats_proc_memory(stats
) < 0)
564 panic("Cannot fetch memory stats!\n");
565 if (stats_proc_system(stats
) < 0)
566 panic("Cannot fetch system stats!\n");
567 if (stats_proc_procs(stats
) < 0)
568 panic("Cannot fetch process stats!\n");
570 stats_proc_interrupts((char *) ifname
, stats
);
572 stats_wireless(ifname
, stats
);
575 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
577 int cpus
= get_number_cpus();
579 stats_zero(&stats_old
, cpus
);
580 stats_zero(&stats_new
, cpus
);
581 stats_zero(&stats_delta
, cpus
);
583 stats_fetch(ifname
, &stats_old
);
584 usleep(ms_interval
* 1000);
585 stats_fetch(ifname
, &stats_new
);
587 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
590 static int cmp_hits(const void *p1
, const void *p2
)
592 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
595 * We want the hits sorted in descending order, thus reverse the return
598 if (h1
->hit
== h2
->hit
)
600 else if (h1
->hit
< h2
->hit
)
606 static int cmp_irqs_rel(const void *p1
, const void *p2
)
608 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
611 * We want the hits sorted in descending order, thus reverse the return
614 if (h1
->irqs_rel
== h2
->irqs_rel
)
616 else if (h1
->irqs_rel
< h2
->irqs_rel
)
622 static int cmp_irqs_abs(const void *p1
, const void *p2
)
624 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
627 * We want the hits sorted in descending order, thus reverse the return
630 if (h1
->irqs_abs
== h2
->irqs_abs
)
632 else if (h1
->irqs_abs
< h2
->irqs_abs
)
638 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
643 memset(&stats_avg
, 0, sizeof(stats_avg
));
645 for (i
= 0; i
< cpus
; ++i
) {
647 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
648 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
649 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
651 stats_avg
.cpu_user
+= rel
->cpu_user
[i
];
652 stats_avg
.cpu_sys
+= rel
->cpu_sys
[i
];
653 stats_avg
.cpu_nice
+= rel
->cpu_nice
[i
];
654 stats_avg
.cpu_idle
+= rel
->cpu_idle
[i
];
655 stats_avg
.cpu_iow
+= rel
->cpu_iow
[i
];
657 stats_avg
.irqs_abs
+= abs
->irqs
[i
];
658 stats_avg
.irqs_rel
+= rel
->irqs
[i
];
659 stats_avg
.irqs_srx_rel
+= rel
->irqs_srx
[i
];
660 stats_avg
.irqs_stx_rel
+= rel
->irqs_stx
[i
];
663 stats_avg
.cpu_user
/= cpus
;
664 stats_avg
.cpu_sys
/= cpus
;
665 stats_avg
.cpu_nice
/= cpus
;
666 stats_avg
.cpu_idle
/= cpus
;
667 stats_avg
.cpu_iow
/= cpus
;
668 stats_avg
.irqs_abs
/= cpus
;
669 stats_avg
.irqs_rel
/= cpus
;
670 stats_avg
.irqs_srx_rel
/= cpus
;
671 stats_avg
.irqs_stx_rel
/= cpus
;
674 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
675 uint64_t ms_interval
, unsigned int top_cpus
)
678 char buff
[64], machine
[64];
679 struct ethtool_drvinfo drvinf
;
680 u32 rate
= device_bitrate(ifname
);
681 int link
= ethtool_link(ifname
);
682 unsigned int cpus
= get_number_cpus();
684 memset(&drvinf
, 0, sizeof(drvinf
));
685 ethtool_drvinf(ifname
, &drvinf
);
687 memset(buff
, 0, sizeof(buff
));
688 memset(machine
, 0, sizeof(machine
));
691 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
693 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
694 link
== 0 ? "no" : "yes");
696 if (!strstr(uts
.release
, uts
.machine
))
697 slprintf(machine
, sizeof(machine
), " %s,", uts
.machine
);
699 mvwprintw(screen
, (*voff
)++, 2,
700 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
701 " ", uts
.release
, machine
,
702 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
703 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
706 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
711 mvwprintw(screen
, (*voff
)++, 0,
712 " rx: %16.3llf MiB/t "
716 ((long double) rel
->rx_bytes
) / (1LLU << 20),
717 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
719 mvwprintw(screen
, (*voff
)++, 0,
720 " tx: %16.3llf MiB/t "
724 ((long double) rel
->tx_bytes
) / (1LLU << 20),
725 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
730 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
733 mvwprintw(screen
, (*voff
)++, 2,
738 ((long double) abs
->rx_bytes
) / (1LLU << 20),
739 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
741 mvwprintw(screen
, (*voff
)++, 2,
746 ((long double) abs
->tx_bytes
) / (1LLU << 20),
747 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
750 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
751 const struct ifstat
*abs
, int *voff
)
753 mvwprintw(screen
, (*voff
)++, 2,
758 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
761 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
763 mvwprintw(screen
, (*voff
)++, 2,
768 abs
->mem_total
/ 1024,
769 (abs
->mem_total
- abs
->mem_free
) / 1024,
770 abs
->mem_active
/ 1024,
771 abs
->mem_inactive
/ 1024);
773 mvwprintw(screen
, (*voff
)++, 2,
777 abs
->swap_total
/ 1024,
778 (abs
->swap_total
- abs
->swap_free
) / 1024,
779 abs
->swap_cached
/ 1024);
782 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
783 int *voff
, unsigned int idx
, char *tag
)
785 int max_padd
= padding_from_num(get_number_cpus());
786 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
787 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
789 mvwprintw(screen
, (*voff
)++, 2,
790 "cpu%*d %s: %11.1lf%% usr/t "
795 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
796 100.0 * rel
->cpu_sys
[idx
] / all
,
797 100.0 * rel
->cpu_idle
[idx
] / all
,
798 100.0 * rel
->cpu_iow
[idx
] / all
);
801 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
802 const struct avg_stat
*avg
, int top_cpus
,
806 int cpus
= get_number_cpus();
807 int max_padd
= padding_from_num(cpus
);
813 /* Display top hitter */
814 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
816 /* Make sure we don't display the min. hitter twice */
817 if (top_cpus
== cpus
)
820 for (i
= 1; i
< top_cpus
; ++i
)
821 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
823 /* Display minimum hitter */
825 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
827 all
= avg
->cpu_user
+ avg
->cpu_sys
+ avg
->cpu_nice
+ avg
->cpu_idle
+ avg
->cpu_iow
;
828 mvwprintw(screen
, (*voff
)++, 2,
832 "%11.1lf%%", max_padd
, "",
833 100.0 * (avg
->cpu_user
+ avg
->cpu_nice
) / all
,
834 100.0 * avg
->cpu_sys
/ all
,
835 100.0 * avg
->cpu_idle
/all
,
836 100.0 * avg
->cpu_iow
/ all
);
839 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
840 int *voff
, unsigned int idx
, char *tag
)
842 int max_padd
= padding_from_num(get_number_cpus());
844 mvwprintw(screen
, (*voff
)++, 2,
845 "cpu%*d %s: %12llu irqs/t "
854 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
855 const struct avg_stat
*avg
, int top_cpus
,
859 int cpus
= get_number_cpus();
860 int max_padd
= padding_from_num(cpus
);
862 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
864 if (top_cpus
== cpus
)
867 for (i
= 1; i
< top_cpus
; ++i
)
868 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
871 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
873 mvwprintw(screen
, (*voff
)++, 2,
876 "%17.1Lf", max_padd
, "",
877 avg
->irqs_rel
, avg
->irqs_srx_rel
, avg
->irqs_stx_rel
);
880 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
881 int *voff
, unsigned int idx
, char *tag
)
883 int max_padd
= padding_from_num(get_number_cpus());
885 mvwprintw(screen
, (*voff
)++, 2,
886 "cpu%*d %s: %12llu irqs",
887 max_padd
, idx
, tag
, abs
->irqs
[idx
]);
890 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
891 const struct avg_stat
*avg
, int top_cpus
,
895 int cpus
= get_number_cpus();
896 int max_padd
= padding_from_num(cpus
);
898 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
900 if (top_cpus
== cpus
)
903 for (i
= 1; i
< top_cpus
; ++i
)
904 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "|");
907 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
909 mvwprintw(screen
, (*voff
)++, 2,
910 "avg:%*s%17.1Lf", max_padd
, "", avg
->irqs_abs
);
913 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
914 const struct ifstat
*abs
, int *voff
)
916 if (iswireless(abs
)) {
917 mvwprintw(screen
, (*voff
)++, 2,
918 "linkqual: %7d/%d (%d/t) ",
920 abs
->wifi
.link_qual_max
,
921 rel
->wifi
.link_qual
);
923 mvwprintw(screen
, (*voff
)++, 2,
924 "signal: %8d dBm (%d dBm/t) ",
925 abs
->wifi
.signal_level
,
926 rel
->wifi
.signal_level
);
930 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
931 const struct ifstat
*abs
, const struct avg_stat
*avg
,
932 int *first
, uint64_t ms_interval
, unsigned int top_cpus
,
935 int cpus
, top
, voff
= 1, cvoff
= 2;
939 cpus
= get_number_cpus();
940 top
= min(cpus
, top_cpus
);
942 stats_top(rel
, abs
, cpus
);
944 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
946 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
949 screen_net_dev_rel(screen
, rel
, &voff
);
952 screen_net_dev_abs(screen
, abs
, &voff
);
955 screen_sys(screen
, rel
, abs
, &voff
);
958 screen_mem_swap(screen
, abs
, &voff
);
961 screen_percpu_states(screen
, rel
, avg
, top
, &voff
);
963 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
966 screen_percpu_irqs_rel(screen
, rel
, avg
, top
, &voff
);
968 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
971 screen_percpu_irqs_abs(screen
, abs
, avg
, top
, &voff
);
974 screen_wireless(screen
, rel
, abs
, &voff
);
977 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
981 mvwprintw(screen
, cvoff
, 2, "(consider to increase "
982 "your sampling interval, e.g. -t 10000)");
984 mvwprintw(screen
, cvoff
, 2, " "
992 static int screen_main(const char *ifname
, uint64_t ms_interval
,
993 unsigned int top_cpus
, bool suppress_warnings
)
996 u32 rate
= device_bitrate(ifname
);
997 bool need_info
= false;
999 stats_screen
= screen_init(true);
1001 if (rate
> SPEED_1000
&& ms_interval
<= 1000 && !suppress_warnings
)
1006 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
1009 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
, &stats_avg
,
1010 &first
, ms_interval
, top_cpus
, need_info
);
1012 stats_sample_generic(ifname
, ms_interval
);
1020 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
1021 const struct ifstat
*abs
, uint64_t ms_interval
)
1025 printf("%ld ", time(0));
1027 printf("%llu ", rel
->rx_bytes
);
1028 printf("%llu ", rel
->rx_packets
);
1029 printf("%llu ", rel
->rx_drops
);
1030 printf("%llu ", rel
->rx_errors
);
1032 printf("%llu ", abs
->rx_bytes
);
1033 printf("%llu ", abs
->rx_packets
);
1034 printf("%llu ", abs
->rx_drops
);
1035 printf("%llu ", abs
->rx_errors
);
1037 printf("%llu ", rel
->tx_bytes
);
1038 printf("%llu ", rel
->tx_packets
);
1039 printf("%llu ", rel
->tx_drops
);
1040 printf("%llu ", rel
->tx_errors
);
1042 printf("%llu ", abs
->tx_bytes
);
1043 printf("%llu ", abs
->tx_packets
);
1044 printf("%llu ", abs
->tx_drops
);
1045 printf("%llu ", abs
->tx_errors
);
1047 printf("%u ", rel
->cswitch
);
1048 printf("%lu ", abs
->mem_free
);
1049 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
1050 printf("%lu ", abs
->mem_total
);
1051 printf("%lu ", abs
->swap_free
);
1052 printf("%lu ", abs
->swap_total
- abs
->swap_free
);
1053 printf("%lu ", abs
->swap_total
);
1054 printf("%u ", abs
->procs_total
);
1055 printf("%u ", abs
->procs_run
);
1056 printf("%u ", abs
->procs_iow
);
1058 cpus
= get_number_cpus();
1060 for (i
= 0; i
< cpus
; ++i
) {
1061 printf("%lu ", rel
->cpu_user
[i
]);
1062 printf("%lu ", rel
->cpu_nice
[i
]);
1063 printf("%lu ", rel
->cpu_sys
[i
]);
1064 printf("%lu ", rel
->cpu_idle
[i
]);
1065 printf("%lu ", rel
->cpu_iow
[i
]);
1067 printf("%llu ", rel
->irqs
[i
]);
1068 printf("%llu ", abs
->irqs
[i
]);
1070 printf("%llu ", rel
->irqs_srx
[i
]);
1071 printf("%llu ", abs
->irqs_srx
[i
]);
1073 printf("%llu ", rel
->irqs_stx
[i
]);
1074 printf("%llu ", abs
->irqs_stx
[i
]);
1077 if (iswireless(abs
)) {
1078 printf("%u ", rel
->wifi
.link_qual
);
1079 printf("%u ", abs
->wifi
.link_qual
);
1080 printf("%u ", abs
->wifi
.link_qual_max
);
1082 printf("%d ", rel
->wifi
.signal_level
);
1083 printf("%d ", abs
->wifi
.signal_level
);
1090 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1091 uint64_t ms_interval
)
1095 printf("# gnuplot dump (#col:description)\n");
1096 printf("# networking interface: %s\n", ifname
);
1097 printf("# sampling interval (t): %lu ms\n", ms_interval
);
1098 printf("# %d:unixtime ", j
++);
1100 printf("%d:rx-bytes-per-t ", j
++);
1101 printf("%d:rx-pkts-per-t ", j
++);
1102 printf("%d:rx-drops-per-t ", j
++);
1103 printf("%d:rx-errors-per-t ", j
++);
1105 printf("%d:rx-bytes ", j
++);
1106 printf("%d:rx-pkts ", j
++);
1107 printf("%d:rx-drops ", j
++);
1108 printf("%d:rx-errors ", j
++);
1110 printf("%d:tx-bytes-per-t ", j
++);
1111 printf("%d:tx-pkts-per-t ", j
++);
1112 printf("%d:tx-drops-per-t ", j
++);
1113 printf("%d:tx-errors-per-t ", j
++);
1115 printf("%d:tx-bytes ", j
++);
1116 printf("%d:tx-pkts ", j
++);
1117 printf("%d:tx-drops ", j
++);
1118 printf("%d:tx-errors ", j
++);
1120 printf("%d:context-switches-per-t ", j
++);
1121 printf("%d:mem-free ", j
++);
1122 printf("%d:mem-used ", j
++);
1123 printf("%d:mem-total ", j
++);
1124 printf("%d:swap-free ", j
++);
1125 printf("%d:swap-used ", j
++);
1126 printf("%d:swap-total ", j
++);
1127 printf("%d:procs-total ", j
++);
1128 printf("%d:procs-in-run ", j
++);
1129 printf("%d:procs-in-iow ", j
++);
1131 cpus
= get_number_cpus();
1133 for (i
= 0; i
< cpus
; ++i
) {
1134 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1135 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1136 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1137 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1138 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1140 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1141 printf("%d:cpu%i-net-irqs ", j
++, i
);
1143 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1144 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1145 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1146 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1149 if (iswireless(abs
)) {
1150 printf("%d:wifi-link-qual-per-t ", j
++);
1151 printf("%d:wifi-link-qual ", j
++);
1152 printf("%d:wifi-link-qual-max ", j
++);
1154 printf("%d:wifi-signal-dbm-per-t ", j
++);
1155 printf("%d:wifi-signal-dbm ", j
++);
1159 printf("# data:\n");
1163 static int term_main(const char *ifname
, uint64_t ms_interval
,
1164 unsigned int top_cpus __maybe_unused
,
1165 bool suppress_warnings __maybe_unused
)
1170 stats_sample_generic(ifname
, ms_interval
);
1174 term_csv_header(ifname
, &stats_new
, ms_interval
);
1177 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1178 } while (stats_loop
&& !sigint
);
1183 int main(int argc
, char **argv
)
1186 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1187 unsigned int top_cpus
= 5;
1188 uint64_t interval
= 1000;
1189 char *ifname
= NULL
;
1190 bool suppress_warnings
= false;
1191 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1192 unsigned int top_cpus
, bool suppress_warnings
);
1194 func_main
= screen_main
;
1199 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1200 &opt_index
)) != EOF
) {
1209 suppress_warnings
= true;
1212 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1215 interval
= strtoul(optarg
, NULL
, 10);
1218 top_cpus
= strtoul(optarg
, NULL
, 10);
1220 panic("Number of top hitter CPUs must be greater than 0");
1229 func_main
= term_main
;
1235 panic("Option -%c requires an argument!\n",
1238 if (isprint(optopt
))
1239 printf("Unknown option character `0x%X\'!\n", optopt
);
1251 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1253 panic("No networking device given!\n");
1255 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1256 panic("lo is not supported!\n");
1257 if (device_mtu(ifname
) == 0)
1258 panic("This is no networking device!\n");
1260 register_signal(SIGINT
, signal_handler
);
1261 register_signal(SIGHUP
, signal_handler
);
1263 cpus
= get_number_cpus();
1264 top_cpus
= min(top_cpus
, cpus
);
1265 if (uname(&uts
) < 0)
1266 panic("Cannot execute uname!\n");
1268 stats_alloc(&stats_old
, cpus
);
1269 stats_alloc(&stats_new
, cpus
);
1270 stats_alloc(&stats_delta
, cpus
);
1272 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1275 ifflags
= enter_promiscuous_mode(ifname
);
1276 ret
= func_main(ifname
, interval
, top_cpus
, suppress_warnings
);
1278 leave_promiscuous_mode(ifname
, ifflags
);