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:vhclp";
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 {"version", no_argument
, NULL
, 'v'},
87 {"help", no_argument
, NULL
, 'h'},
91 static void signal_handler(int number
)
103 static inline int iswireless(const struct ifstat
*stats
)
105 return stats
->wifi
.bitrate
> 0;
108 static void __noreturn
help(void)
110 printf("\nifpps %s, top-like kernel networking and system statistics\n",
112 puts("http://www.netsniff-ng.org\n\n"
113 "Usage: ifpps [options] || ifpps <netdev>\n"
115 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
116 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
117 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
118 " in ncurses mode (default 5)\n"
119 " -p|--promisc Promiscuous mode\n"
120 " -c|--csv Output to terminal as Gnuplot-ready data\n"
121 " -l|--loop Continuous CSV output\n"
122 " -v|--version Print version and exit\n"
123 " -h|--help Print this help and exit\n\n"
127 " ifpps -lpcd wlan0 > plot.dat\n\n"
129 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
130 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
131 "Please report bugs to <bugs@netsniff-ng.org>\n"
132 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
133 "Swiss federal institute of technology (ETH Zurich)\n"
134 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
135 "License: GNU GPL version 2.0\n"
136 "This is free software: you are free to change and redistribute it.\n"
137 "There is NO WARRANTY, to the extent permitted by law.\n");
141 static void __noreturn
version(void)
143 printf("\nifpps %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
144 puts("top-like kernel networking and system statistics\n"
145 "http://www.netsniff-ng.org\n\n"
146 "Please report bugs to <bugs@netsniff-ng.org>\n"
147 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
148 "Swiss federal institute of technology (ETH Zurich)\n"
149 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
150 "License: GNU GPL version 2.0\n"
151 "This is free software: you are free to change and redistribute it.\n"
152 "There is NO WARRANTY, to the extent permitted by law.\n");
156 static inline int padding_from_num(int n
)
160 while ((n
/= 10) > 0);
164 #define STATS_ALLOC1(member) \
165 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
167 static void stats_alloc(struct ifstat
*stats
, int cpus
)
170 STATS_ALLOC1(irqs_srx
);
171 STATS_ALLOC1(irqs_stx
);
173 STATS_ALLOC1(cpu_user
);
174 STATS_ALLOC1(cpu_sys
);
175 STATS_ALLOC1(cpu_nice
);
176 STATS_ALLOC1(cpu_idle
);
177 STATS_ALLOC1(cpu_iow
);
180 #define STATS_ZERO1(member) \
181 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
183 static void stats_zero(struct ifstat
*stats
, int cpus
)
185 /* Only clear the non-pointer members */
186 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
189 STATS_ZERO1(irqs_srx
);
190 STATS_ZERO1(irqs_stx
);
192 STATS_ZERO1(cpu_user
);
193 STATS_ZERO1(cpu_sys
);
194 STATS_ZERO1(cpu_nice
);
195 STATS_ZERO1(cpu_idle
);
196 STATS_ZERO1(cpu_iow
);
199 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
205 fp
= fopen("/proc/net/dev", "r");
207 panic("Cannot open /proc/net/dev!\n");
209 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
210 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
212 memset(buff
, 0, sizeof(buff
));
214 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
215 buff
[sizeof(buff
) -1] = 0;
217 if (strstr(buff
, ifname
) == NULL
)
220 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
221 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
222 &stats
->rx_bytes
, &stats
->rx_packets
,
223 &stats
->rx_errors
, &stats
->rx_drops
,
224 &stats
->rx_fifo
, &stats
->rx_frame
,
225 &stats
->rx_multi
, &stats
->tx_bytes
,
226 &stats
->tx_packets
, &stats
->tx_errors
,
227 &stats
->tx_drops
, &stats
->tx_fifo
,
228 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
233 memset(buff
, 0, sizeof(buff
));
240 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
242 int ret
= -EINVAL
, i
, cpus
, try = 0;
246 struct ethtool_drvinfo drvinf
;
249 fp
= fopen("/proc/interrupts", "r");
251 panic("Cannot open /proc/interrupts!\n");
253 cpus
= get_number_cpus();
254 buff_len
= cpus
* 128;
255 buff
= xmalloc(buff_len
);
257 fseek(fp
, 0, SEEK_SET
);
258 memset(buff
, 0, buff_len
);
260 while (fgets(buff
, buff_len
, fp
) != NULL
) {
261 buff
[buff_len
- 1] = 0;
264 if (strstr(buff
, ifname
) == NULL
)
267 /* XXX: remove this one here */
268 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
269 bug_on(stats
->irq_nr
== 0);
273 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
275 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
277 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
284 memset(buff
, 0, buff_len
);
287 if (ret
== -EINVAL
&& try == 0) {
288 memset(&drvinf
, 0, sizeof(drvinf
));
289 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
292 ifname
= drvinf
.driver
;
303 static int stats_proc_softirqs(struct ifstat
*stats
)
314 fp
= fopen("/proc/softirqs", "r");
316 panic("Cannot open /proc/softirqs!\n");
318 cpus
= get_number_cpus();
319 buff_len
= cpus
* 128;
320 buff
= xmalloc(buff_len
);
322 memset(buff
, 0, buff_len
);
324 while (fgets(buff
, buff_len
, fp
) != NULL
) {
325 buff
[buff_len
- 1] = 0;
327 if ((ptr
= strstr(buff
, "NET_TX:")))
328 net_type
= softirqs_net_tx
;
329 else if ((ptr
= strstr(buff
, "NET_RX:")))
330 net_type
= softirqs_net_rx
;
334 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
336 case softirqs_net_tx
:
337 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
339 case softirqs_net_rx
:
340 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
345 memset(buff
, 0, buff_len
);
353 static int stats_proc_memory(struct ifstat
*stats
)
355 char *ptr
, buff
[256];
358 fp
= fopen("/proc/meminfo", "r");
360 panic("Cannot open /proc/meminfo!\n");
362 memset(buff
, 0, sizeof(buff
));
364 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
365 buff
[sizeof(buff
) - 1] = 0;
367 if ((ptr
= strstr(buff
, "MemTotal:"))) {
368 ptr
+= strlen("MemTotal:");
369 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
370 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
371 ptr
+= strlen("MemFree:");
372 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
373 } else if ((ptr
= strstr(buff
, "Active:"))) {
374 ptr
+= strlen("Active:");
375 stats
->mem_active
= strtoul(ptr
, &ptr
, 10);
376 } else if ((ptr
= strstr(buff
, "Inactive:"))) {
377 ptr
+= strlen("Inactive:");
378 stats
->mem_inactive
= strtoul(ptr
, &ptr
, 10);
379 } else if ((ptr
= strstr(buff
, "SwapTotal:"))) {
380 ptr
+= strlen("SwapTotal:");
381 stats
->swap_total
= strtoul(ptr
, &ptr
, 10);
382 } else if ((ptr
= strstr(buff
, "SwapFree:"))) {
383 ptr
+= strlen("SwapFree:");
384 stats
->swap_free
= strtoul(ptr
, &ptr
, 10);
385 } else if ((ptr
= strstr(buff
, "SwapCached:"))) {
386 ptr
+= strlen("SwapCached:");
387 stats
->swap_cached
= strtoul(ptr
, &ptr
, 10);
390 memset(buff
, 0, sizeof(buff
));
397 static int stats_proc_system(struct ifstat
*stats
)
400 char *ptr
, buff
[256];
403 fp
= fopen("/proc/stat", "r");
405 panic("Cannot open /proc/stat!\n");
407 cpus
= get_number_cpus();
409 memset(buff
, 0, sizeof(buff
));
411 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
412 buff
[sizeof(buff
) - 1] = 0;
414 if ((ptr
= strstr(buff
, "cpu"))) {
415 ptr
+= strlen("cpu");
419 cpu
= strtol(ptr
, &ptr
, 10);
422 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
423 &stats
->cpu_user
[cpu
],
424 &stats
->cpu_nice
[cpu
],
425 &stats
->cpu_sys
[cpu
],
426 &stats
->cpu_idle
[cpu
],
427 &stats
->cpu_iow
[cpu
]) != 5)
429 } else if ((ptr
= strstr(buff
, "ctxt"))) {
430 ptr
+= strlen("ctxt");
431 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
432 } else if ((ptr
= strstr(buff
, "procs_running"))) {
433 ptr
+= strlen("procs_running");
434 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
435 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
436 ptr
+= strlen("procs_blocked");
437 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
440 memset(buff
, 0, sizeof(buff
));
447 static int stats_proc_procs(struct ifstat
*stats
)
452 dir
= opendir("/proc");
454 panic("Cannot open /proc\n");
456 stats
->procs_total
= 0;
458 while ((e
= readdir(dir
)) != NULL
) {
459 const char *name
= e
->d_name
;
461 unsigned int pid
= strtoul(name
, &end
, 10);
464 if (pid
== 0 && end
== name
)
467 stats
->procs_total
++;
475 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
480 return dbm_val
- 0x100;
483 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
486 struct iw_statistics ws
;
488 ret
= wireless_sigqual(ifname
, &ws
);
490 stats
->wifi
.bitrate
= 0;
494 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
496 stats
->wifi
.signal_level
=
497 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
499 stats
->wifi
.link_qual
= ws
.qual
.qual
;
500 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
505 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
506 #define DIFF(member) do { \
507 if (sizeof(diff->member) != sizeof(new->member) || \
508 sizeof(diff->member) != sizeof(old->member)) \
510 bug_on((new->member - old->member) > (new->member)); \
514 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
535 DIFF1(wifi
.signal_level
);
536 DIFF1(wifi
.link_qual
);
540 cpus
= get_number_cpus();
542 for (i
= 0; i
< cpus
; ++i
) {
555 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
557 if (stats_proc_net_dev(ifname
, stats
) < 0)
558 panic("Cannot fetch device stats!\n");
559 if (stats_proc_softirqs(stats
) < 0)
560 panic("Cannot fetch software interrupts!\n");
561 if (stats_proc_memory(stats
) < 0)
562 panic("Cannot fetch memory stats!\n");
563 if (stats_proc_system(stats
) < 0)
564 panic("Cannot fetch system stats!\n");
565 if (stats_proc_procs(stats
) < 0)
566 panic("Cannot fetch process stats!\n");
568 stats_proc_interrupts((char *) ifname
, stats
);
570 stats_wireless(ifname
, stats
);
573 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
575 int cpus
= get_number_cpus();
577 stats_zero(&stats_old
, cpus
);
578 stats_zero(&stats_new
, cpus
);
579 stats_zero(&stats_delta
, cpus
);
581 stats_fetch(ifname
, &stats_old
);
582 usleep(ms_interval
* 1000);
583 stats_fetch(ifname
, &stats_new
);
585 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
588 static int cmp_hits(const void *p1
, const void *p2
)
590 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
593 * We want the hits sorted in descending order, thus reverse the return
596 if (h1
->hit
== h2
->hit
)
598 else if (h1
->hit
< h2
->hit
)
604 static int cmp_irqs_rel(const void *p1
, const void *p2
)
606 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
609 * We want the hits sorted in descending order, thus reverse the return
612 if (h1
->irqs_rel
== h2
->irqs_rel
)
614 else if (h1
->irqs_rel
< h2
->irqs_rel
)
620 static int cmp_irqs_abs(const void *p1
, const void *p2
)
622 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
625 * We want the hits sorted in descending order, thus reverse the return
628 if (h1
->irqs_abs
== h2
->irqs_abs
)
630 else if (h1
->irqs_abs
< h2
->irqs_abs
)
636 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
641 memset(&stats_avg
, 0, sizeof(stats_avg
));
643 for (i
= 0; i
< cpus
; ++i
) {
645 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
646 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
647 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
649 stats_avg
.cpu_user
+= rel
->cpu_user
[i
];
650 stats_avg
.cpu_sys
+= rel
->cpu_sys
[i
];
651 stats_avg
.cpu_nice
+= rel
->cpu_nice
[i
];
652 stats_avg
.cpu_idle
+= rel
->cpu_idle
[i
];
653 stats_avg
.cpu_iow
+= rel
->cpu_iow
[i
];
655 stats_avg
.irqs_abs
+= abs
->irqs
[i
];
656 stats_avg
.irqs_rel
+= rel
->irqs
[i
];
657 stats_avg
.irqs_srx_rel
+= rel
->irqs_srx
[i
];
658 stats_avg
.irqs_stx_rel
+= rel
->irqs_stx
[i
];
661 stats_avg
.cpu_user
/= cpus
;
662 stats_avg
.cpu_sys
/= cpus
;
663 stats_avg
.cpu_nice
/= cpus
;
664 stats_avg
.cpu_idle
/= cpus
;
665 stats_avg
.cpu_iow
/= cpus
;
666 stats_avg
.irqs_abs
/= cpus
;
667 stats_avg
.irqs_rel
/= cpus
;
668 stats_avg
.irqs_srx_rel
/= cpus
;
669 stats_avg
.irqs_stx_rel
/= cpus
;
672 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
673 uint64_t ms_interval
, unsigned int top_cpus
)
676 char buff
[64], machine
[64];
677 struct ethtool_drvinfo drvinf
;
678 u32 rate
= device_bitrate(ifname
);
679 int link
= ethtool_link(ifname
);
680 unsigned int cpus
= get_number_cpus();
682 memset(&drvinf
, 0, sizeof(drvinf
));
683 ethtool_drvinf(ifname
, &drvinf
);
685 memset(buff
, 0, sizeof(buff
));
686 memset(machine
, 0, sizeof(machine
));
689 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
691 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
692 link
== 0 ? "no" : "yes");
694 if (!strstr(uts
.release
, uts
.machine
))
695 slprintf(machine
, sizeof(machine
), " %s,", uts
.machine
);
697 mvwprintw(screen
, (*voff
)++, 2,
698 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
699 " ", uts
.release
, machine
,
700 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
701 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
704 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
709 mvwprintw(screen
, (*voff
)++, 0,
710 " rx: %16.3llf MiB/t "
714 ((long double) rel
->rx_bytes
) / (1LLU << 20),
715 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
717 mvwprintw(screen
, (*voff
)++, 0,
718 " tx: %16.3llf MiB/t "
722 ((long double) rel
->tx_bytes
) / (1LLU << 20),
723 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
728 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
731 mvwprintw(screen
, (*voff
)++, 2,
736 ((long double) abs
->rx_bytes
) / (1LLU << 20),
737 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
739 mvwprintw(screen
, (*voff
)++, 2,
744 ((long double) abs
->tx_bytes
) / (1LLU << 20),
745 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
748 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
749 const struct ifstat
*abs
, int *voff
)
751 mvwprintw(screen
, (*voff
)++, 2,
756 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
759 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
761 mvwprintw(screen
, (*voff
)++, 2,
766 abs
->mem_total
/ 1024,
767 (abs
->mem_total
- abs
->mem_free
) / 1024,
768 abs
->mem_active
/ 1024,
769 abs
->mem_inactive
/ 1024);
771 mvwprintw(screen
, (*voff
)++, 2,
775 abs
->swap_total
/ 1024,
776 (abs
->swap_total
- abs
->swap_free
) / 1024,
777 abs
->swap_cached
/ 1024);
780 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
781 int *voff
, unsigned int idx
, char *tag
)
783 int max_padd
= padding_from_num(get_number_cpus());
784 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
785 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
787 mvwprintw(screen
, (*voff
)++, 2,
788 "cpu%*d %s: %11.1lf%% usr/t "
793 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
794 100.0 * rel
->cpu_sys
[idx
] / all
,
795 100.0 * rel
->cpu_idle
[idx
] / all
,
796 100.0 * rel
->cpu_iow
[idx
] / all
);
799 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
800 const struct avg_stat
*avg
, int top_cpus
,
804 int cpus
= get_number_cpus();
810 /* Display top hitter */
811 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
813 /* Make sure we don't display the min. hitter twice */
814 if (top_cpus
== cpus
)
817 for (i
= 1; i
< top_cpus
; ++i
)
818 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
820 /* Display minimum hitter */
822 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
824 all
= avg
->cpu_user
+ avg
->cpu_sys
+ avg
->cpu_nice
+ avg
->cpu_idle
+ avg
->cpu_iow
;
825 mvwprintw(screen
, (*voff
)++, 2,
830 100.0 * (avg
->cpu_user
+ avg
->cpu_nice
) / all
,
831 100.0 * avg
->cpu_sys
/ all
,
832 100.0 * avg
->cpu_idle
/all
,
833 100.0 * avg
->cpu_iow
/ all
);
836 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
837 int *voff
, unsigned int idx
, char *tag
)
839 int max_padd
= padding_from_num(get_number_cpus());
841 mvwprintw(screen
, (*voff
)++, 2,
842 "cpu%*d %s: %12llu irqs/t "
851 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
852 const struct avg_stat
*avg
, int top_cpus
,
856 int cpus
= get_number_cpus();
858 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
860 if (top_cpus
== cpus
)
863 for (i
= 1; i
< top_cpus
; ++i
)
864 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
867 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
869 mvwprintw(screen
, (*voff
)++, 2,
873 avg
->irqs_rel
, avg
->irqs_srx_rel
, avg
->irqs_stx_rel
);
876 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
877 int *voff
, unsigned int idx
, char *tag
)
879 int max_padd
= padding_from_num(get_number_cpus());
881 mvwprintw(screen
, (*voff
)++, 2,
882 "cpu%*d %s: %12llu irqs",
883 max_padd
, idx
, tag
, abs
->irqs
[idx
]);
886 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
887 const struct avg_stat
*avg
, int top_cpus
,
891 int cpus
= get_number_cpus();
893 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
895 if (top_cpus
== cpus
)
898 for (i
= 1; i
< top_cpus
; ++i
)
899 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "|");
902 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
904 mvwprintw(screen
, (*voff
)++, 2,
905 "avg: %17.1Lf", avg
->irqs_abs
);
908 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
909 const struct ifstat
*abs
, int *voff
)
911 if (iswireless(abs
)) {
912 mvwprintw(screen
, (*voff
)++, 2,
913 "linkqual: %7d/%d (%d/t) ",
915 abs
->wifi
.link_qual_max
,
916 rel
->wifi
.link_qual
);
918 mvwprintw(screen
, (*voff
)++, 2,
919 "signal: %8d dBm (%d dBm/t) ",
920 abs
->wifi
.signal_level
,
921 rel
->wifi
.signal_level
);
925 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
926 const struct ifstat
*abs
, const struct avg_stat
*avg
,
927 int *first
, uint64_t ms_interval
, unsigned int top_cpus
)
929 int cpus
, top
, voff
= 1, cvoff
= 2;
933 cpus
= get_number_cpus();
934 top
= min(cpus
, top_cpus
);
936 stats_top(rel
, abs
, cpus
);
938 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
940 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
943 screen_net_dev_rel(screen
, rel
, &voff
);
946 screen_net_dev_abs(screen
, abs
, &voff
);
949 screen_sys(screen
, rel
, abs
, &voff
);
952 screen_mem_swap(screen
, abs
, &voff
);
955 screen_percpu_states(screen
, rel
, avg
, top
, &voff
);
957 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
960 screen_percpu_irqs_rel(screen
, rel
, avg
, top
, &voff
);
962 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
965 screen_percpu_irqs_abs(screen
, abs
, avg
, top
, &voff
);
968 screen_wireless(screen
, rel
, abs
, &voff
);
971 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
974 mvwprintw(screen
, cvoff
, 2, " ");
981 static int screen_main(const char *ifname
, uint64_t ms_interval
,
982 unsigned int top_cpus
)
986 stats_screen
= screen_init(true);
990 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
993 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
, &stats_avg
,
994 &first
, ms_interval
, top_cpus
);
996 stats_sample_generic(ifname
, ms_interval
);
1004 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
1005 const struct ifstat
*abs
, uint64_t ms_interval
)
1009 printf("%ld ", time(0));
1011 printf("%llu ", rel
->rx_bytes
);
1012 printf("%llu ", rel
->rx_packets
);
1013 printf("%llu ", rel
->rx_drops
);
1014 printf("%llu ", rel
->rx_errors
);
1016 printf("%llu ", abs
->rx_bytes
);
1017 printf("%llu ", abs
->rx_packets
);
1018 printf("%llu ", abs
->rx_drops
);
1019 printf("%llu ", abs
->rx_errors
);
1021 printf("%llu ", rel
->tx_bytes
);
1022 printf("%llu ", rel
->tx_packets
);
1023 printf("%llu ", rel
->tx_drops
);
1024 printf("%llu ", rel
->tx_errors
);
1026 printf("%llu ", abs
->tx_bytes
);
1027 printf("%llu ", abs
->tx_packets
);
1028 printf("%llu ", abs
->tx_drops
);
1029 printf("%llu ", abs
->tx_errors
);
1031 printf("%u ", rel
->cswitch
);
1032 printf("%lu ", abs
->mem_free
);
1033 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
1034 printf("%lu ", abs
->mem_total
);
1035 printf("%lu ", abs
->swap_free
);
1036 printf("%lu ", abs
->swap_total
- abs
->swap_free
);
1037 printf("%lu ", abs
->swap_total
);
1038 printf("%u ", abs
->procs_total
);
1039 printf("%u ", abs
->procs_run
);
1040 printf("%u ", abs
->procs_iow
);
1042 cpus
= get_number_cpus();
1044 for (i
= 0; i
< cpus
; ++i
) {
1045 printf("%lu ", rel
->cpu_user
[i
]);
1046 printf("%lu ", rel
->cpu_nice
[i
]);
1047 printf("%lu ", rel
->cpu_sys
[i
]);
1048 printf("%lu ", rel
->cpu_idle
[i
]);
1049 printf("%lu ", rel
->cpu_iow
[i
]);
1051 printf("%llu ", rel
->irqs
[i
]);
1052 printf("%llu ", abs
->irqs
[i
]);
1054 printf("%llu ", rel
->irqs_srx
[i
]);
1055 printf("%llu ", abs
->irqs_srx
[i
]);
1057 printf("%llu ", rel
->irqs_stx
[i
]);
1058 printf("%llu ", abs
->irqs_stx
[i
]);
1061 if (iswireless(abs
)) {
1062 printf("%u ", rel
->wifi
.link_qual
);
1063 printf("%u ", abs
->wifi
.link_qual
);
1064 printf("%u ", abs
->wifi
.link_qual_max
);
1066 printf("%d ", rel
->wifi
.signal_level
);
1067 printf("%d ", abs
->wifi
.signal_level
);
1074 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1075 uint64_t ms_interval
)
1079 printf("# gnuplot dump (#col:description)\n");
1080 printf("# networking interface: %s\n", ifname
);
1081 printf("# sampling interval (t): %lu ms\n", ms_interval
);
1082 printf("# %d:unixtime ", j
++);
1084 printf("%d:rx-bytes-per-t ", j
++);
1085 printf("%d:rx-pkts-per-t ", j
++);
1086 printf("%d:rx-drops-per-t ", j
++);
1087 printf("%d:rx-errors-per-t ", j
++);
1089 printf("%d:rx-bytes ", j
++);
1090 printf("%d:rx-pkts ", j
++);
1091 printf("%d:rx-drops ", j
++);
1092 printf("%d:rx-errors ", j
++);
1094 printf("%d:tx-bytes-per-t ", j
++);
1095 printf("%d:tx-pkts-per-t ", j
++);
1096 printf("%d:tx-drops-per-t ", j
++);
1097 printf("%d:tx-errors-per-t ", j
++);
1099 printf("%d:tx-bytes ", j
++);
1100 printf("%d:tx-pkts ", j
++);
1101 printf("%d:tx-drops ", j
++);
1102 printf("%d:tx-errors ", j
++);
1104 printf("%d:context-switches-per-t ", j
++);
1105 printf("%d:mem-free ", j
++);
1106 printf("%d:mem-used ", j
++);
1107 printf("%d:mem-total ", j
++);
1108 printf("%d:swap-free ", j
++);
1109 printf("%d:swap-used ", j
++);
1110 printf("%d:swap-total ", j
++);
1111 printf("%d:procs-total ", j
++);
1112 printf("%d:procs-in-run ", j
++);
1113 printf("%d:procs-in-iow ", j
++);
1115 cpus
= get_number_cpus();
1117 for (i
= 0; i
< cpus
; ++i
) {
1118 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1119 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1120 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1121 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1122 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1124 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1125 printf("%d:cpu%i-net-irqs ", j
++, i
);
1127 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1128 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1129 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1130 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1133 if (iswireless(abs
)) {
1134 printf("%d:wifi-link-qual-per-t ", j
++);
1135 printf("%d:wifi-link-qual ", j
++);
1136 printf("%d:wifi-link-qual-max ", j
++);
1138 printf("%d:wifi-signal-dbm-per-t ", j
++);
1139 printf("%d:wifi-signal-dbm ", j
++);
1143 printf("# data:\n");
1147 static int term_main(const char *ifname
, uint64_t ms_interval
,
1148 unsigned int top_cpus __maybe_unused
)
1153 stats_sample_generic(ifname
, ms_interval
);
1157 term_csv_header(ifname
, &stats_new
, ms_interval
);
1160 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1161 } while (stats_loop
&& !sigint
);
1166 int main(int argc
, char **argv
)
1169 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1170 unsigned int top_cpus
= 5;
1171 uint64_t interval
= 1000;
1172 char *ifname
= NULL
;
1173 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1174 unsigned int top_cpus
) = screen_main
;
1179 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1180 &opt_index
)) != EOF
) {
1189 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1192 interval
= strtoul(optarg
, NULL
, 10);
1195 top_cpus
= strtoul(optarg
, NULL
, 10);
1197 panic("Number of top hitter CPUs must be greater than 0");
1206 func_main
= term_main
;
1212 panic("Option -%c requires an argument!\n",
1215 if (isprint(optopt
))
1216 printf("Unknown option character `0x%X\'!\n", optopt
);
1228 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1230 panic("No networking device given!\n");
1232 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1233 panic("lo is not supported!\n");
1234 if (device_mtu(ifname
) == 0)
1235 panic("This is no networking device!\n");
1237 register_signal(SIGINT
, signal_handler
);
1238 register_signal(SIGHUP
, signal_handler
);
1240 cpus
= get_number_cpus();
1241 top_cpus
= min(top_cpus
, cpus
);
1242 if (uname(&uts
) < 0)
1243 panic("Cannot execute uname!\n");
1245 stats_alloc(&stats_old
, cpus
);
1246 stats_alloc(&stats_new
, cpus
);
1247 stats_alloc(&stats_delta
, cpus
);
1249 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1252 ifflags
= enter_promiscuous_mode(ifname
);
1253 ret
= func_main(ifname
, interval
, top_cpus
);
1255 leave_promiscuous_mode(ifname
, ifflags
);