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 int show_median
= 0;
76 static WINDOW
*stats_screen
= NULL
;
77 static struct utsname uts
;
79 static const char *short_options
= "d:t:n:vhclpmW";
80 static const struct option long_options
[] = {
81 {"dev", required_argument
, NULL
, 'd'},
82 {"interval", required_argument
, NULL
, 't'},
83 {"num-cpus", required_argument
, NULL
, 'n'},
84 {"promisc", no_argument
, NULL
, 'p'},
85 {"csv", no_argument
, NULL
, 'c'},
86 {"loop", no_argument
, NULL
, 'l'},
87 {"median", no_argument
, NULL
, 'm'},
88 {"no-warn", no_argument
, NULL
, 'W'},
89 {"version", no_argument
, NULL
, 'v'},
90 {"help", no_argument
, NULL
, 'h'},
94 static void signal_handler(int number
)
106 static inline int iswireless(const struct ifstat
*stats
)
108 return stats
->wifi
.bitrate
> 0;
111 static void __noreturn
help(void)
113 printf("\nifpps %s, top-like kernel networking and system statistics\n",
115 puts("http://www.netsniff-ng.org\n\n"
116 "Usage: ifpps [options] || ifpps <netdev>\n"
118 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
119 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
120 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
121 " in ncurses mode (default 5)\n"
122 " -p|--promisc Promiscuous mode\n"
123 " -c|--csv Output to terminal as Gnuplot-ready data\n"
124 " -l|--loop Continuous CSV output\n"
125 " -W|--no-warn Suppress warnings\n"
126 " -v|--version Print version and exit\n"
127 " -h|--help Print this help and exit\n\n"
131 " ifpps -lpcd wlan0 > plot.dat\n\n"
133 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
134 " Thus, in those situations, it's good to use a -t of 10sec.\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 void __noreturn
version(void)
147 printf("\nifpps %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
148 puts("top-like kernel networking and system statistics\n"
149 "http://www.netsniff-ng.org\n\n"
150 "Please report bugs to <bugs@netsniff-ng.org>\n"
151 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
152 "Swiss federal institute of technology (ETH Zurich)\n"
153 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
154 "License: GNU GPL version 2.0\n"
155 "This is free software: you are free to change and redistribute it.\n"
156 "There is NO WARRANTY, to the extent permitted by law.\n");
160 static inline int padding_from_num(int n
)
164 while ((n
/= 10) > 0);
168 #define STATS_ALLOC1(member) \
169 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
171 static void stats_alloc(struct ifstat
*stats
, int cpus
)
174 STATS_ALLOC1(irqs_srx
);
175 STATS_ALLOC1(irqs_stx
);
177 STATS_ALLOC1(cpu_user
);
178 STATS_ALLOC1(cpu_sys
);
179 STATS_ALLOC1(cpu_nice
);
180 STATS_ALLOC1(cpu_idle
);
181 STATS_ALLOC1(cpu_iow
);
184 #define STATS_ZERO1(member) \
185 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
187 static void stats_zero(struct ifstat
*stats
, int cpus
)
189 /* Only clear the non-pointer members */
190 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
193 STATS_ZERO1(irqs_srx
);
194 STATS_ZERO1(irqs_stx
);
196 STATS_ZERO1(cpu_user
);
197 STATS_ZERO1(cpu_sys
);
198 STATS_ZERO1(cpu_nice
);
199 STATS_ZERO1(cpu_idle
);
200 STATS_ZERO1(cpu_iow
);
203 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
209 fp
= fopen("/proc/net/dev", "r");
211 panic("Cannot open /proc/net/dev!\n");
213 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
214 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
216 memset(buff
, 0, sizeof(buff
));
218 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
219 buff
[sizeof(buff
) -1] = 0;
221 if (strstr(buff
, ifname
) == NULL
)
224 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
225 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
226 &stats
->rx_bytes
, &stats
->rx_packets
,
227 &stats
->rx_errors
, &stats
->rx_drops
,
228 &stats
->rx_fifo
, &stats
->rx_frame
,
229 &stats
->rx_multi
, &stats
->tx_bytes
,
230 &stats
->tx_packets
, &stats
->tx_errors
,
231 &stats
->tx_drops
, &stats
->tx_fifo
,
232 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
237 memset(buff
, 0, sizeof(buff
));
244 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
246 int ret
= -EINVAL
, i
, cpus
, try = 0;
250 struct ethtool_drvinfo drvinf
;
253 fp
= fopen("/proc/interrupts", "r");
255 panic("Cannot open /proc/interrupts!\n");
257 cpus
= get_number_cpus();
258 buff_len
= cpus
* 128;
259 buff
= xmalloc(buff_len
);
261 fseek(fp
, 0, SEEK_SET
);
262 memset(buff
, 0, buff_len
);
264 while (fgets(buff
, buff_len
, fp
) != NULL
) {
265 buff
[buff_len
- 1] = 0;
268 if (strstr(buff
, ifname
) == NULL
)
271 /* XXX: remove this one here */
272 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
273 bug_on(stats
->irq_nr
== 0);
277 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
279 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
281 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
288 memset(buff
, 0, buff_len
);
291 if (ret
== -EINVAL
&& try == 0) {
292 memset(&drvinf
, 0, sizeof(drvinf
));
293 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
296 ifname
= drvinf
.driver
;
307 static int stats_proc_softirqs(struct ifstat
*stats
)
318 fp
= fopen("/proc/softirqs", "r");
320 panic("Cannot open /proc/softirqs!\n");
322 cpus
= get_number_cpus();
323 buff_len
= cpus
* 128;
324 buff
= xmalloc(buff_len
);
326 memset(buff
, 0, buff_len
);
328 while (fgets(buff
, buff_len
, fp
) != NULL
) {
329 buff
[buff_len
- 1] = 0;
331 if ((ptr
= strstr(buff
, "NET_TX:")))
332 net_type
= softirqs_net_tx
;
333 else if ((ptr
= strstr(buff
, "NET_RX:")))
334 net_type
= softirqs_net_rx
;
338 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
340 case softirqs_net_tx
:
341 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
343 case softirqs_net_rx
:
344 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
349 memset(buff
, 0, buff_len
);
357 static int stats_proc_memory(struct ifstat
*stats
)
359 char *ptr
, buff
[256];
362 fp
= fopen("/proc/meminfo", "r");
364 panic("Cannot open /proc/meminfo!\n");
366 memset(buff
, 0, sizeof(buff
));
368 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
369 buff
[sizeof(buff
) - 1] = 0;
371 if ((ptr
= strstr(buff
, "MemTotal:"))) {
372 ptr
+= strlen("MemTotal:");
373 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
374 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
375 ptr
+= strlen("MemFree:");
376 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
377 } else if ((ptr
= strstr(buff
, "Active:"))) {
378 ptr
+= strlen("Active:");
379 stats
->mem_active
= strtoul(ptr
, &ptr
, 10);
380 } else if ((ptr
= strstr(buff
, "Inactive:"))) {
381 ptr
+= strlen("Inactive:");
382 stats
->mem_inactive
= strtoul(ptr
, &ptr
, 10);
383 } else if ((ptr
= strstr(buff
, "SwapTotal:"))) {
384 ptr
+= strlen("SwapTotal:");
385 stats
->swap_total
= strtoul(ptr
, &ptr
, 10);
386 } else if ((ptr
= strstr(buff
, "SwapFree:"))) {
387 ptr
+= strlen("SwapFree:");
388 stats
->swap_free
= strtoul(ptr
, &ptr
, 10);
389 } else if ((ptr
= strstr(buff
, "SwapCached:"))) {
390 ptr
+= strlen("SwapCached:");
391 stats
->swap_cached
= strtoul(ptr
, &ptr
, 10);
394 memset(buff
, 0, sizeof(buff
));
401 static int stats_proc_system(struct ifstat
*stats
)
404 char *ptr
, buff
[256];
407 fp
= fopen("/proc/stat", "r");
409 panic("Cannot open /proc/stat!\n");
411 cpus
= get_number_cpus();
413 memset(buff
, 0, sizeof(buff
));
415 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
416 buff
[sizeof(buff
) - 1] = 0;
418 if ((ptr
= strstr(buff
, "cpu"))) {
419 ptr
+= strlen("cpu");
423 cpu
= strtol(ptr
, &ptr
, 10);
426 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
427 &stats
->cpu_user
[cpu
],
428 &stats
->cpu_nice
[cpu
],
429 &stats
->cpu_sys
[cpu
],
430 &stats
->cpu_idle
[cpu
],
431 &stats
->cpu_iow
[cpu
]) != 5)
433 } else if ((ptr
= strstr(buff
, "ctxt"))) {
434 ptr
+= strlen("ctxt");
435 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
436 } else if ((ptr
= strstr(buff
, "procs_running"))) {
437 ptr
+= strlen("procs_running");
438 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
439 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
440 ptr
+= strlen("procs_blocked");
441 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
444 memset(buff
, 0, sizeof(buff
));
451 static int stats_proc_procs(struct ifstat
*stats
)
456 dir
= opendir("/proc");
458 panic("Cannot open /proc\n");
460 stats
->procs_total
= 0;
462 while ((e
= readdir(dir
)) != NULL
) {
463 const char *name
= e
->d_name
;
465 unsigned int pid
= strtoul(name
, &end
, 10);
468 if (pid
== 0 && end
== name
)
471 stats
->procs_total
++;
479 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
484 return dbm_val
- 0x100;
487 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
490 struct iw_statistics ws
;
492 ret
= wireless_sigqual(ifname
, &ws
);
494 stats
->wifi
.bitrate
= 0;
498 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
500 stats
->wifi
.signal_level
=
501 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
503 stats
->wifi
.link_qual
= ws
.qual
.qual
;
504 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
509 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
510 #define DIFF(member) do { \
511 if (sizeof(diff->member) != sizeof(new->member) || \
512 sizeof(diff->member) != sizeof(old->member)) \
514 bug_on((new->member - old->member) > (new->member)); \
518 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
539 DIFF1(wifi
.signal_level
);
540 DIFF1(wifi
.link_qual
);
544 cpus
= get_number_cpus();
546 for (i
= 0; i
< cpus
; ++i
) {
559 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
561 if (stats_proc_net_dev(ifname
, stats
) < 0)
562 panic("Cannot fetch device stats!\n");
563 if (stats_proc_softirqs(stats
) < 0)
564 panic("Cannot fetch software interrupts!\n");
565 if (stats_proc_memory(stats
) < 0)
566 panic("Cannot fetch memory stats!\n");
567 if (stats_proc_system(stats
) < 0)
568 panic("Cannot fetch system stats!\n");
569 if (stats_proc_procs(stats
) < 0)
570 panic("Cannot fetch process stats!\n");
572 stats_proc_interrupts((char *) ifname
, stats
);
574 stats_wireless(ifname
, stats
);
577 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
579 int cpus
= get_number_cpus();
581 stats_zero(&stats_old
, cpus
);
582 stats_zero(&stats_new
, cpus
);
583 stats_zero(&stats_delta
, cpus
);
585 stats_fetch(ifname
, &stats_old
);
586 usleep(ms_interval
* 1000);
587 stats_fetch(ifname
, &stats_new
);
589 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
592 static int cmp_hits(const void *p1
, const void *p2
)
594 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
597 * We want the hits sorted in descending order, thus reverse the return
600 if (h1
->hit
== h2
->hit
)
602 else if (h1
->hit
< h2
->hit
)
608 static int cmp_irqs_rel(const void *p1
, const void *p2
)
610 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
613 * We want the hits sorted in descending order, thus reverse the return
616 if (h1
->irqs_rel
== h2
->irqs_rel
)
618 else if (h1
->irqs_rel
< h2
->irqs_rel
)
624 static int cmp_irqs_abs(const void *p1
, const void *p2
)
626 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
629 * We want the hits sorted in descending order, thus reverse the return
632 if (h1
->irqs_abs
== h2
->irqs_abs
)
634 else if (h1
->irqs_abs
< h2
->irqs_abs
)
640 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
645 memset(&stats_avg
, 0, sizeof(stats_avg
));
647 for (i
= 0; i
< cpus
; ++i
) {
649 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
650 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
651 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
653 stats_avg
.cpu_user
+= rel
->cpu_user
[i
];
654 stats_avg
.cpu_sys
+= rel
->cpu_sys
[i
];
655 stats_avg
.cpu_nice
+= rel
->cpu_nice
[i
];
656 stats_avg
.cpu_idle
+= rel
->cpu_idle
[i
];
657 stats_avg
.cpu_iow
+= rel
->cpu_iow
[i
];
659 stats_avg
.irqs_abs
+= abs
->irqs
[i
];
660 stats_avg
.irqs_rel
+= rel
->irqs
[i
];
661 stats_avg
.irqs_srx_rel
+= rel
->irqs_srx
[i
];
662 stats_avg
.irqs_stx_rel
+= rel
->irqs_stx
[i
];
665 stats_avg
.cpu_user
/= cpus
;
666 stats_avg
.cpu_sys
/= cpus
;
667 stats_avg
.cpu_nice
/= cpus
;
668 stats_avg
.cpu_idle
/= cpus
;
669 stats_avg
.cpu_iow
/= cpus
;
670 stats_avg
.irqs_abs
/= cpus
;
671 stats_avg
.irqs_rel
/= cpus
;
672 stats_avg
.irqs_srx_rel
/= cpus
;
673 stats_avg
.irqs_stx_rel
/= cpus
;
676 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
677 uint64_t ms_interval
, unsigned int top_cpus
)
680 char buff
[64], machine
[64];
681 struct ethtool_drvinfo drvinf
;
682 u32 rate
= device_bitrate(ifname
);
683 int link
= ethtool_link(ifname
);
684 unsigned int cpus
= get_number_cpus();
686 memset(&drvinf
, 0, sizeof(drvinf
));
687 ethtool_drvinf(ifname
, &drvinf
);
689 memset(buff
, 0, sizeof(buff
));
690 memset(machine
, 0, sizeof(machine
));
693 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
695 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
696 link
== 0 ? "no" : "yes");
698 if (!strstr(uts
.release
, uts
.machine
))
699 slprintf(machine
, sizeof(machine
), " %s,", uts
.machine
);
701 mvwprintw(screen
, (*voff
)++, 2,
702 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
703 " ", uts
.release
, machine
,
704 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
705 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
708 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
713 mvwprintw(screen
, (*voff
)++, 0,
714 " rx: %16.3llf MiB/t "
718 ((long double) rel
->rx_bytes
) / (1LLU << 20),
719 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
721 mvwprintw(screen
, (*voff
)++, 0,
722 " tx: %16.3llf MiB/t "
726 ((long double) rel
->tx_bytes
) / (1LLU << 20),
727 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
732 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
735 mvwprintw(screen
, (*voff
)++, 2,
740 ((long double) abs
->rx_bytes
) / (1LLU << 20),
741 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
743 mvwprintw(screen
, (*voff
)++, 2,
748 ((long double) abs
->tx_bytes
) / (1LLU << 20),
749 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
752 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
753 const struct ifstat
*abs
, int *voff
)
755 mvwprintw(screen
, (*voff
)++, 2,
760 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
763 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
765 mvwprintw(screen
, (*voff
)++, 2,
770 abs
->mem_total
/ 1024,
771 (abs
->mem_total
- abs
->mem_free
) / 1024,
772 abs
->mem_active
/ 1024,
773 abs
->mem_inactive
/ 1024);
775 mvwprintw(screen
, (*voff
)++, 2,
779 abs
->swap_total
/ 1024,
780 (abs
->swap_total
- abs
->swap_free
) / 1024,
781 abs
->swap_cached
/ 1024);
784 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
785 int *voff
, unsigned int idx
, char *tag
)
787 int max_padd
= padding_from_num(get_number_cpus());
788 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
789 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
791 mvwprintw(screen
, (*voff
)++, 2,
792 "cpu%*d %s: %11.1lf%% usr/t "
797 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
798 100.0 * rel
->cpu_sys
[idx
] / all
,
799 100.0 * rel
->cpu_idle
[idx
] / all
,
800 100.0 * rel
->cpu_iow
[idx
] / all
);
803 #define MEDIAN_EVEN(member) do { \
804 m_##member = (rel->member[i] + rel->member[j]) / 2; \
807 #define MEDIAN_ODD(member) do { \
808 m_##member = rel->member[i]; \
811 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
812 const struct avg_stat
*avg
, int top_cpus
,
816 int cpus
= get_number_cpus();
817 int max_padd
= padding_from_num(cpus
);
823 /* Display top hitter */
824 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
826 /* Make sure we don't display the min. hitter twice */
827 if (top_cpus
== cpus
)
830 for (i
= 1; i
< top_cpus
; ++i
)
831 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
833 /* Display minimum hitter */
835 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
837 all
= avg
->cpu_user
+ avg
->cpu_sys
+ avg
->cpu_nice
+ avg
->cpu_idle
+ avg
->cpu_iow
;
838 mvwprintw(screen
, (*voff
)++, 2,
842 "%11.1lf%%", max_padd
, "",
843 100.0 * (avg
->cpu_user
+ avg
->cpu_nice
) / all
,
844 100.0 * avg
->cpu_sys
/ all
,
845 100.0 * avg
->cpu_idle
/all
,
846 100.0 * avg
->cpu_iow
/ all
);
849 long double m_cpu_user
, m_cpu_nice
, m_cpu_sys
, m_cpu_idle
, m_cpu_iow
;
851 i
= cpu_hits
[cpus
/ 2].idx
;
853 /* take the mean of the 2 middle entries */
854 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
856 MEDIAN_EVEN(cpu_user
);
857 MEDIAN_EVEN(cpu_nice
);
858 MEDIAN_EVEN(cpu_sys
);
859 MEDIAN_EVEN(cpu_idle
);
860 MEDIAN_EVEN(cpu_iow
);
862 /* take the middle entry as is */
863 MEDIAN_ODD(cpu_user
);
864 MEDIAN_ODD(cpu_nice
);
866 MEDIAN_ODD(cpu_idle
);
870 all
= m_cpu_user
+ m_cpu_sys
+ m_cpu_nice
+ m_cpu_idle
+ m_cpu_iow
;
871 mvwprintw(screen
, (*voff
)++, 2,
876 100.0 * (m_cpu_user
+ m_cpu_nice
) / all
,
877 100.0 * m_cpu_sys
/ all
,
878 100.0 * m_cpu_idle
/all
,
879 100.0 * m_cpu_iow
/ all
);
883 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
884 int *voff
, unsigned int idx
, char *tag
)
886 int max_padd
= padding_from_num(get_number_cpus());
888 mvwprintw(screen
, (*voff
)++, 2,
889 "cpu%*d %s: %12llu irqs/t "
898 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
899 const struct avg_stat
*avg
, int top_cpus
,
903 int cpus
= get_number_cpus();
904 int max_padd
= padding_from_num(cpus
);
906 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
908 if (top_cpus
== cpus
)
911 for (i
= 1; i
< top_cpus
; ++i
)
912 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
915 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
917 mvwprintw(screen
, (*voff
)++, 2,
920 "%17.1Lf", max_padd
, "",
921 avg
->irqs_rel
, avg
->irqs_srx_rel
, avg
->irqs_stx_rel
);
924 long double m_irqs
, m_irqs_srx
, m_irqs_stx
;
926 i
= cpu_hits
[cpus
/ 2].idx
;
928 /* take the mean of the 2 middle entries */
929 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
932 MEDIAN_EVEN(irqs_srx
);
933 MEDIAN_EVEN(irqs_stx
);
935 /* take the middle entry as is */
937 MEDIAN_ODD(irqs_srx
);
938 MEDIAN_ODD(irqs_stx
);
941 mvwprintw(screen
, (*voff
)++, 2,
944 "%17.1Lf", max_padd
, "",
945 m_irqs
, m_irqs_srx
, m_irqs_stx
);
949 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
950 int *voff
, unsigned int idx
, char *tag
)
952 int max_padd
= padding_from_num(get_number_cpus());
954 mvwprintw(screen
, (*voff
)++, 2,
955 "cpu%*d %s: %12llu irqs",
956 max_padd
, idx
, tag
, abs
->irqs
[idx
]);
959 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
960 const struct avg_stat
*avg
, int top_cpus
,
964 int cpus
= get_number_cpus();
965 int max_padd
= padding_from_num(cpus
);
967 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
969 if (top_cpus
== cpus
)
972 for (i
= 1; i
< top_cpus
; ++i
)
973 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "|");
976 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
978 mvwprintw(screen
, (*voff
)++, 2,
979 "avg:%*s%17.1Lf", max_padd
, "", avg
->irqs_abs
);
984 i
= cpu_hits
[cpus
/ 2].idx
;
986 /* take the mean of the 2 middle entries */
987 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
989 m_irqs
= (abs
->irqs
[i
] + abs
->irqs
[j
]) / 2;
991 /* take the middle entry as is */
992 m_irqs
= abs
->irqs
[i
];
995 mvwprintw(screen
, (*voff
)++, 2,
996 "med:%*s%17.1Lf", max_padd
, "", m_irqs
);
1000 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
1001 const struct ifstat
*abs
, int *voff
)
1003 if (iswireless(abs
)) {
1004 mvwprintw(screen
, (*voff
)++, 2,
1005 "linkqual: %7d/%d (%d/t) ",
1006 abs
->wifi
.link_qual
,
1007 abs
->wifi
.link_qual_max
,
1008 rel
->wifi
.link_qual
);
1010 mvwprintw(screen
, (*voff
)++, 2,
1011 "signal: %8d dBm (%d dBm/t) ",
1012 abs
->wifi
.signal_level
,
1013 rel
->wifi
.signal_level
);
1017 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
1018 const struct ifstat
*abs
, const struct avg_stat
*avg
,
1019 int *first
, uint64_t ms_interval
, unsigned int top_cpus
,
1022 int cpus
, top
, voff
= 1, cvoff
= 2;
1026 cpus
= get_number_cpus();
1027 top
= min(cpus
, top_cpus
);
1029 stats_top(rel
, abs
, cpus
);
1031 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
1033 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
1036 screen_net_dev_rel(screen
, rel
, &voff
);
1039 screen_net_dev_abs(screen
, abs
, &voff
);
1042 screen_sys(screen
, rel
, abs
, &voff
);
1045 screen_mem_swap(screen
, abs
, &voff
);
1048 screen_percpu_states(screen
, rel
, avg
, top
, &voff
);
1050 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
1053 screen_percpu_irqs_rel(screen
, rel
, avg
, top
, &voff
);
1055 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
1058 screen_percpu_irqs_abs(screen
, abs
, avg
, top
, &voff
);
1061 screen_wireless(screen
, rel
, abs
, &voff
);
1064 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
1068 mvwprintw(screen
, cvoff
, 2, "(consider to increase "
1069 "your sampling interval, e.g. -t 10000)");
1071 mvwprintw(screen
, cvoff
, 2, " "
1079 static int screen_main(const char *ifname
, uint64_t ms_interval
,
1080 unsigned int top_cpus
, bool suppress_warnings
)
1083 u32 rate
= device_bitrate(ifname
);
1084 bool need_info
= false;
1086 stats_screen
= screen_init(true);
1088 if (rate
> SPEED_1000
&& ms_interval
<= 1000 && !suppress_warnings
)
1093 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
1096 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
, &stats_avg
,
1097 &first
, ms_interval
, top_cpus
, need_info
);
1099 stats_sample_generic(ifname
, ms_interval
);
1107 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
1108 const struct ifstat
*abs
, uint64_t ms_interval
)
1112 printf("%ld ", time(0));
1114 printf("%llu ", rel
->rx_bytes
);
1115 printf("%llu ", rel
->rx_packets
);
1116 printf("%llu ", rel
->rx_drops
);
1117 printf("%llu ", rel
->rx_errors
);
1119 printf("%llu ", abs
->rx_bytes
);
1120 printf("%llu ", abs
->rx_packets
);
1121 printf("%llu ", abs
->rx_drops
);
1122 printf("%llu ", abs
->rx_errors
);
1124 printf("%llu ", rel
->tx_bytes
);
1125 printf("%llu ", rel
->tx_packets
);
1126 printf("%llu ", rel
->tx_drops
);
1127 printf("%llu ", rel
->tx_errors
);
1129 printf("%llu ", abs
->tx_bytes
);
1130 printf("%llu ", abs
->tx_packets
);
1131 printf("%llu ", abs
->tx_drops
);
1132 printf("%llu ", abs
->tx_errors
);
1134 printf("%u ", rel
->cswitch
);
1135 printf("%lu ", abs
->mem_free
);
1136 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
1137 printf("%lu ", abs
->mem_total
);
1138 printf("%lu ", abs
->swap_free
);
1139 printf("%lu ", abs
->swap_total
- abs
->swap_free
);
1140 printf("%lu ", abs
->swap_total
);
1141 printf("%u ", abs
->procs_total
);
1142 printf("%u ", abs
->procs_run
);
1143 printf("%u ", abs
->procs_iow
);
1145 cpus
= get_number_cpus();
1147 for (i
= 0; i
< cpus
; ++i
) {
1148 printf("%lu ", rel
->cpu_user
[i
]);
1149 printf("%lu ", rel
->cpu_nice
[i
]);
1150 printf("%lu ", rel
->cpu_sys
[i
]);
1151 printf("%lu ", rel
->cpu_idle
[i
]);
1152 printf("%lu ", rel
->cpu_iow
[i
]);
1154 printf("%llu ", rel
->irqs
[i
]);
1155 printf("%llu ", abs
->irqs
[i
]);
1157 printf("%llu ", rel
->irqs_srx
[i
]);
1158 printf("%llu ", abs
->irqs_srx
[i
]);
1160 printf("%llu ", rel
->irqs_stx
[i
]);
1161 printf("%llu ", abs
->irqs_stx
[i
]);
1164 if (iswireless(abs
)) {
1165 printf("%u ", rel
->wifi
.link_qual
);
1166 printf("%u ", abs
->wifi
.link_qual
);
1167 printf("%u ", abs
->wifi
.link_qual_max
);
1169 printf("%d ", rel
->wifi
.signal_level
);
1170 printf("%d ", abs
->wifi
.signal_level
);
1177 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1178 uint64_t ms_interval
)
1182 printf("# gnuplot dump (#col:description)\n");
1183 printf("# networking interface: %s\n", ifname
);
1184 printf("# sampling interval (t): %lu ms\n", ms_interval
);
1185 printf("# %d:unixtime ", j
++);
1187 printf("%d:rx-bytes-per-t ", j
++);
1188 printf("%d:rx-pkts-per-t ", j
++);
1189 printf("%d:rx-drops-per-t ", j
++);
1190 printf("%d:rx-errors-per-t ", j
++);
1192 printf("%d:rx-bytes ", j
++);
1193 printf("%d:rx-pkts ", j
++);
1194 printf("%d:rx-drops ", j
++);
1195 printf("%d:rx-errors ", j
++);
1197 printf("%d:tx-bytes-per-t ", j
++);
1198 printf("%d:tx-pkts-per-t ", j
++);
1199 printf("%d:tx-drops-per-t ", j
++);
1200 printf("%d:tx-errors-per-t ", j
++);
1202 printf("%d:tx-bytes ", j
++);
1203 printf("%d:tx-pkts ", j
++);
1204 printf("%d:tx-drops ", j
++);
1205 printf("%d:tx-errors ", j
++);
1207 printf("%d:context-switches-per-t ", j
++);
1208 printf("%d:mem-free ", j
++);
1209 printf("%d:mem-used ", j
++);
1210 printf("%d:mem-total ", j
++);
1211 printf("%d:swap-free ", j
++);
1212 printf("%d:swap-used ", j
++);
1213 printf("%d:swap-total ", j
++);
1214 printf("%d:procs-total ", j
++);
1215 printf("%d:procs-in-run ", j
++);
1216 printf("%d:procs-in-iow ", j
++);
1218 cpus
= get_number_cpus();
1220 for (i
= 0; i
< cpus
; ++i
) {
1221 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1222 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1223 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1224 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1225 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1227 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1228 printf("%d:cpu%i-net-irqs ", j
++, i
);
1230 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1231 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1232 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1233 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1236 if (iswireless(abs
)) {
1237 printf("%d:wifi-link-qual-per-t ", j
++);
1238 printf("%d:wifi-link-qual ", j
++);
1239 printf("%d:wifi-link-qual-max ", j
++);
1241 printf("%d:wifi-signal-dbm-per-t ", j
++);
1242 printf("%d:wifi-signal-dbm ", j
++);
1246 printf("# data:\n");
1250 static int term_main(const char *ifname
, uint64_t ms_interval
,
1251 unsigned int top_cpus __maybe_unused
,
1252 bool suppress_warnings __maybe_unused
)
1257 stats_sample_generic(ifname
, ms_interval
);
1261 term_csv_header(ifname
, &stats_new
, ms_interval
);
1264 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1265 } while (stats_loop
&& !sigint
);
1270 int main(int argc
, char **argv
)
1273 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1274 unsigned int top_cpus
= 5;
1275 uint64_t interval
= 1000;
1276 char *ifname
= NULL
;
1277 bool suppress_warnings
= false;
1278 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1279 unsigned int top_cpus
, bool suppress_warnings
);
1281 func_main
= screen_main
;
1286 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1287 &opt_index
)) != EOF
) {
1296 suppress_warnings
= true;
1299 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1302 interval
= strtoul(optarg
, NULL
, 10);
1305 top_cpus
= strtoul(optarg
, NULL
, 10);
1307 panic("Number of top hitter CPUs must be greater than 0");
1319 func_main
= term_main
;
1325 panic("Option -%c requires an argument!\n",
1328 if (isprint(optopt
))
1329 printf("Unknown option character `0x%X\'!\n", optopt
);
1341 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1343 panic("No networking device given!\n");
1345 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1346 panic("lo is not supported!\n");
1347 if (device_mtu(ifname
) == 0)
1348 panic("This is no networking device!\n");
1350 register_signal(SIGINT
, signal_handler
);
1351 register_signal(SIGHUP
, signal_handler
);
1353 cpus
= get_number_cpus();
1354 top_cpus
= min(top_cpus
, cpus
);
1355 if (uname(&uts
) < 0)
1356 panic("Cannot execute uname!\n");
1358 stats_alloc(&stats_old
, cpus
);
1359 stats_alloc(&stats_new
, cpus
);
1360 stats_alloc(&stats_delta
, cpus
);
1362 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1365 ifflags
= enter_promiscuous_mode(ifname
);
1366 ret
= func_main(ifname
, interval
, top_cpus
, suppress_warnings
);
1368 leave_promiscuous_mode(ifname
, ifflags
);