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>
37 int16_t link_qual
, link_qual_max
;
38 int signal_level
/*, noise_level*/;
42 long long unsigned int rx_bytes
, rx_packets
, rx_drops
, rx_errors
;
43 long long unsigned int rx_fifo
, rx_frame
, rx_multi
;
44 long long unsigned int tx_bytes
, tx_packets
, tx_drops
, tx_errors
;
45 long long unsigned int tx_fifo
, tx_colls
, tx_carrier
;
46 uint64_t mem_free
, mem_total
, mem_active
, mem_inactive
;
47 uint64_t swap_total
, swap_free
, swap_cached
;
48 uint32_t irq_nr
, procs_total
, procs_run
, procs_iow
, cswitch
;
49 struct wifi_stat wifi
;
51 * Pointer members need to be last in order for stats_zero() to work
54 long long unsigned int *irqs
, *irqs_srx
, *irqs_stx
;
55 uint64_t *cpu_user
, *cpu_sys
, *cpu_nice
, *cpu_idle
, *cpu_iow
;
61 long long unsigned int irqs_rel
, irqs_abs
;
65 uint64_t cpu_user
, cpu_sys
, cpu_nice
, cpu_idle
, cpu_iow
;
66 long double irqs_abs
, irqs_rel
, irqs_srx_rel
, irqs_stx_rel
;
69 static volatile sig_atomic_t sigint
= 0;
70 static struct ifstat stats_old
, stats_new
, stats_delta
;
71 static struct cpu_hit
*cpu_hits
;
72 static struct avg_stat stats_avg
;
73 static int stats_loop
= 0;
74 static int show_median
= 0;
75 static WINDOW
*stats_screen
= NULL
;
76 static struct utsname uts
;
78 static const char *short_options
= "d:n:t:clmpWvh";
79 static const struct option long_options
[] = {
80 {"dev", required_argument
, NULL
, 'd'},
81 {"num-cpus", required_argument
, NULL
, 'n'},
82 {"interval", required_argument
, NULL
, 't'},
83 {"csv", no_argument
, NULL
, 'c'},
84 {"loop", no_argument
, NULL
, 'l'},
85 {"median", no_argument
, NULL
, 'm'},
86 {"promisc", no_argument
, NULL
, 'p'},
87 {"no-warn", no_argument
, NULL
, 'W'},
88 {"version", no_argument
, NULL
, 'v'},
89 {"help", no_argument
, NULL
, 'h'},
93 static void signal_handler(int number
)
105 static inline int iswireless(const struct ifstat
*stats
)
107 return stats
->wifi
.bitrate
> 0;
110 static void __noreturn
help(void)
112 printf("\nifpps %s, top-like kernel networking and system statistics\n",
114 puts("http://www.netsniff-ng.org\n\n"
115 "Usage: ifpps [options] || ifpps <netdev>\n"
117 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
118 " -n|--num-cpus <num> Number of top hitter CPUs in ncurses mode (def: 5)\n"
119 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
120 " -c|--csv Output to terminal as Gnuplot-ready data\n"
121 " -l|--loop Continuous CSV output\n"
122 " -m|--median Display median values\n"
123 " -p|--promisc Promiscuous mode\n"
124 " -W|--no-warn Suppress warnings\n"
125 " -v|--version Print version and exit\n"
126 " -h|--help Print this help and exit\n\n"
130 " ifpps -lpcd wlan0 > plot.dat\n\n"
132 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
133 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
134 "Please report bugs to <bugs@netsniff-ng.org>\n"
135 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
136 "Swiss federal institute of technology (ETH Zurich)\n"
137 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
138 "License: GNU GPL version 2.0\n"
139 "This is free software: you are free to change and redistribute it.\n"
140 "There is NO WARRANTY, to the extent permitted by law.\n");
144 static void __noreturn
version(void)
146 printf("\nifpps %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
147 puts("top-like kernel networking and system statistics\n"
148 "http://www.netsniff-ng.org\n\n"
149 "Please report bugs to <bugs@netsniff-ng.org>\n"
150 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
151 "Swiss federal institute of technology (ETH Zurich)\n"
152 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
153 "License: GNU GPL version 2.0\n"
154 "This is free software: you are free to change and redistribute it.\n"
155 "There is NO WARRANTY, to the extent permitted by law.\n");
159 static inline int padding_from_num(int n
)
163 while ((n
/= 10) > 0);
167 #define STATS_ALLOC1(member) \
168 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
170 static void stats_alloc(struct ifstat
*stats
, int cpus
)
173 STATS_ALLOC1(irqs_srx
);
174 STATS_ALLOC1(irqs_stx
);
176 STATS_ALLOC1(cpu_user
);
177 STATS_ALLOC1(cpu_sys
);
178 STATS_ALLOC1(cpu_nice
);
179 STATS_ALLOC1(cpu_idle
);
180 STATS_ALLOC1(cpu_iow
);
183 #define STATS_ZERO1(member) \
184 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
186 static void stats_zero(struct ifstat
*stats
, int cpus
)
188 /* Only clear the non-pointer members */
189 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
192 STATS_ZERO1(irqs_srx
);
193 STATS_ZERO1(irqs_stx
);
195 STATS_ZERO1(cpu_user
);
196 STATS_ZERO1(cpu_sys
);
197 STATS_ZERO1(cpu_nice
);
198 STATS_ZERO1(cpu_idle
);
199 STATS_ZERO1(cpu_iow
);
202 #define STATS_RELEASE(member) \
203 do { xfree(stats->member); } while (0)
205 static void stats_release(struct ifstat
*stats
)
208 STATS_RELEASE(irqs_srx
);
209 STATS_RELEASE(irqs_stx
);
211 STATS_RELEASE(cpu_user
);
212 STATS_RELEASE(cpu_sys
);
213 STATS_RELEASE(cpu_nice
);
214 STATS_RELEASE(cpu_idle
);
215 STATS_RELEASE(cpu_iow
);
218 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
224 fp
= fopen("/proc/net/dev", "r");
226 panic("Cannot open /proc/net/dev!\n");
228 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
229 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
231 memset(buff
, 0, sizeof(buff
));
233 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
234 buff
[sizeof(buff
) -1] = 0;
236 if (strstr(buff
, ifname
) == NULL
)
239 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
240 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
241 &stats
->rx_bytes
, &stats
->rx_packets
,
242 &stats
->rx_errors
, &stats
->rx_drops
,
243 &stats
->rx_fifo
, &stats
->rx_frame
,
244 &stats
->rx_multi
, &stats
->tx_bytes
,
245 &stats
->tx_packets
, &stats
->tx_errors
,
246 &stats
->tx_drops
, &stats
->tx_fifo
,
247 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
252 memset(buff
, 0, sizeof(buff
));
259 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
261 int ret
= -EINVAL
, i
, cpus
, try = 0;
265 struct ethtool_drvinfo drvinf
;
268 fp
= fopen("/proc/interrupts", "r");
270 panic("Cannot open /proc/interrupts!\n");
272 cpus
= get_number_cpus();
273 buff_len
= cpus
* 128;
274 buff
= xmalloc(buff_len
);
276 fseek(fp
, 0, SEEK_SET
);
277 memset(buff
, 0, buff_len
);
279 while (fgets(buff
, buff_len
, fp
) != NULL
) {
280 buff
[buff_len
- 1] = 0;
283 if (strstr(buff
, ifname
) == NULL
)
286 /* XXX: remove this one here */
287 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
288 bug_on(stats
->irq_nr
== 0);
292 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
294 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
296 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
303 memset(buff
, 0, buff_len
);
306 if (ret
== -EINVAL
&& try == 0) {
307 memset(&drvinf
, 0, sizeof(drvinf
));
308 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
311 ifname
= drvinf
.driver
;
322 static int stats_proc_softirqs(struct ifstat
*stats
)
333 fp
= fopen("/proc/softirqs", "r");
335 panic("Cannot open /proc/softirqs!\n");
337 cpus
= get_number_cpus();
338 buff_len
= cpus
* 128;
339 buff
= xmalloc(buff_len
);
341 memset(buff
, 0, buff_len
);
343 while (fgets(buff
, buff_len
, fp
) != NULL
) {
344 buff
[buff_len
- 1] = 0;
346 if ((ptr
= strstr(buff
, "NET_TX:")))
347 net_type
= softirqs_net_tx
;
348 else if ((ptr
= strstr(buff
, "NET_RX:")))
349 net_type
= softirqs_net_rx
;
353 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
355 case softirqs_net_tx
:
356 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
358 case softirqs_net_rx
:
359 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
364 memset(buff
, 0, buff_len
);
372 static int stats_proc_memory(struct ifstat
*stats
)
374 char *ptr
, buff
[256];
377 fp
= fopen("/proc/meminfo", "r");
379 panic("Cannot open /proc/meminfo!\n");
381 memset(buff
, 0, sizeof(buff
));
383 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
384 buff
[sizeof(buff
) - 1] = 0;
386 if ((ptr
= strstr(buff
, "MemTotal:"))) {
387 ptr
+= strlen("MemTotal:");
388 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
389 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
390 ptr
+= strlen("MemFree:");
391 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
392 } else if ((ptr
= strstr(buff
, "Active:"))) {
393 ptr
+= strlen("Active:");
394 stats
->mem_active
= strtoul(ptr
, &ptr
, 10);
395 } else if ((ptr
= strstr(buff
, "Inactive:"))) {
396 ptr
+= strlen("Inactive:");
397 stats
->mem_inactive
= strtoul(ptr
, &ptr
, 10);
398 } else if ((ptr
= strstr(buff
, "SwapTotal:"))) {
399 ptr
+= strlen("SwapTotal:");
400 stats
->swap_total
= strtoul(ptr
, &ptr
, 10);
401 } else if ((ptr
= strstr(buff
, "SwapFree:"))) {
402 ptr
+= strlen("SwapFree:");
403 stats
->swap_free
= strtoul(ptr
, &ptr
, 10);
404 } else if ((ptr
= strstr(buff
, "SwapCached:"))) {
405 ptr
+= strlen("SwapCached:");
406 stats
->swap_cached
= strtoul(ptr
, &ptr
, 10);
409 memset(buff
, 0, sizeof(buff
));
416 static int stats_proc_system(struct ifstat
*stats
)
419 char *ptr
, buff
[256];
422 fp
= fopen("/proc/stat", "r");
424 panic("Cannot open /proc/stat!\n");
426 cpus
= get_number_cpus();
428 memset(buff
, 0, sizeof(buff
));
430 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
431 buff
[sizeof(buff
) - 1] = 0;
433 if ((ptr
= strstr(buff
, "cpu"))) {
434 ptr
+= strlen("cpu");
438 cpu
= strtol(ptr
, &ptr
, 10);
441 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
442 &stats
->cpu_user
[cpu
],
443 &stats
->cpu_nice
[cpu
],
444 &stats
->cpu_sys
[cpu
],
445 &stats
->cpu_idle
[cpu
],
446 &stats
->cpu_iow
[cpu
]) != 5)
448 } else if ((ptr
= strstr(buff
, "ctxt"))) {
449 ptr
+= strlen("ctxt");
450 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
451 } else if ((ptr
= strstr(buff
, "procs_running"))) {
452 ptr
+= strlen("procs_running");
453 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
454 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
455 ptr
+= strlen("procs_blocked");
456 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
459 memset(buff
, 0, sizeof(buff
));
466 static int stats_proc_procs(struct ifstat
*stats
)
471 dir
= opendir("/proc");
473 panic("Cannot open /proc\n");
475 stats
->procs_total
= 0;
477 while ((e
= readdir(dir
)) != NULL
) {
478 const char *name
= e
->d_name
;
480 unsigned int pid
= strtoul(name
, &end
, 10);
483 if (pid
== 0 && end
== name
)
486 stats
->procs_total
++;
494 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
499 return dbm_val
- 0x100;
502 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
505 struct iw_statistics ws
;
507 ret
= wireless_sigqual(ifname
, &ws
);
509 stats
->wifi
.bitrate
= 0;
513 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
515 stats
->wifi
.signal_level
=
516 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
518 stats
->wifi
.link_qual
= ws
.qual
.qual
;
519 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
524 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
525 #define DIFF(member) do { \
526 if (sizeof(diff->member) != sizeof(new->member) || \
527 sizeof(diff->member) != sizeof(old->member)) \
529 bug_on((new->member - old->member) > (new->member)); \
533 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
554 DIFF1(wifi
.signal_level
);
555 DIFF1(wifi
.link_qual
);
559 cpus
= get_number_cpus();
561 for (i
= 0; i
< cpus
; ++i
) {
574 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
576 if (stats_proc_net_dev(ifname
, stats
) < 0)
577 panic("Cannot fetch device stats!\n");
578 if (stats_proc_softirqs(stats
) < 0)
579 panic("Cannot fetch software interrupts!\n");
580 if (stats_proc_memory(stats
) < 0)
581 panic("Cannot fetch memory stats!\n");
582 if (stats_proc_system(stats
) < 0)
583 panic("Cannot fetch system stats!\n");
584 if (stats_proc_procs(stats
) < 0)
585 panic("Cannot fetch process stats!\n");
587 stats_proc_interrupts((char *) ifname
, stats
);
589 stats_wireless(ifname
, stats
);
592 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
594 int cpus
= get_number_cpus();
596 stats_zero(&stats_old
, cpus
);
597 stats_zero(&stats_new
, cpus
);
598 stats_zero(&stats_delta
, cpus
);
600 stats_fetch(ifname
, &stats_old
);
601 usleep(ms_interval
* 1000);
602 stats_fetch(ifname
, &stats_new
);
604 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
607 static int cmp_hits(const void *p1
, const void *p2
)
609 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
612 * We want the hits sorted in descending order, thus reverse the return
615 if (h1
->hit
== h2
->hit
)
617 else if (h1
->hit
< h2
->hit
)
623 static int cmp_irqs_rel(const void *p1
, const void *p2
)
625 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
628 * We want the hits sorted in descending order, thus reverse the return
631 if (h1
->irqs_rel
== h2
->irqs_rel
)
633 else if (h1
->irqs_rel
< h2
->irqs_rel
)
639 static int cmp_irqs_abs(const void *p1
, const void *p2
)
641 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
644 * We want the hits sorted in descending order, thus reverse the return
647 if (h1
->irqs_abs
== h2
->irqs_abs
)
649 else if (h1
->irqs_abs
< h2
->irqs_abs
)
655 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
660 memset(&stats_avg
, 0, sizeof(stats_avg
));
662 for (i
= 0; i
< cpus
; ++i
) {
664 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
665 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
666 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
668 stats_avg
.cpu_user
+= rel
->cpu_user
[i
];
669 stats_avg
.cpu_sys
+= rel
->cpu_sys
[i
];
670 stats_avg
.cpu_nice
+= rel
->cpu_nice
[i
];
671 stats_avg
.cpu_idle
+= rel
->cpu_idle
[i
];
672 stats_avg
.cpu_iow
+= rel
->cpu_iow
[i
];
674 stats_avg
.irqs_abs
+= abs
->irqs
[i
];
675 stats_avg
.irqs_rel
+= rel
->irqs
[i
];
676 stats_avg
.irqs_srx_rel
+= rel
->irqs_srx
[i
];
677 stats_avg
.irqs_stx_rel
+= rel
->irqs_stx
[i
];
680 stats_avg
.cpu_user
/= cpus
;
681 stats_avg
.cpu_sys
/= cpus
;
682 stats_avg
.cpu_nice
/= cpus
;
683 stats_avg
.cpu_idle
/= cpus
;
684 stats_avg
.cpu_iow
/= cpus
;
685 stats_avg
.irqs_abs
/= cpus
;
686 stats_avg
.irqs_rel
/= cpus
;
687 stats_avg
.irqs_srx_rel
/= cpus
;
688 stats_avg
.irqs_stx_rel
/= cpus
;
691 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
692 uint64_t ms_interval
, unsigned int top_cpus
)
695 char buff
[64], machine
[64];
696 struct ethtool_drvinfo drvinf
;
697 u32 rate
= device_bitrate(ifname
);
698 int link
= ethtool_link(ifname
);
699 unsigned int cpus
= get_number_cpus();
701 memset(&drvinf
, 0, sizeof(drvinf
));
702 ethtool_drvinf(ifname
, &drvinf
);
704 memset(buff
, 0, sizeof(buff
));
705 memset(machine
, 0, sizeof(machine
));
708 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
710 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
711 link
== 0 ? "no" : "yes");
713 if (!strstr(uts
.release
, uts
.machine
))
714 slprintf(machine
, sizeof(machine
), " %s,", uts
.machine
);
716 mvwprintw(screen
, (*voff
)++, 2,
717 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
718 " ", uts
.release
, machine
,
719 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
720 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
723 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
728 mvwprintw(screen
, (*voff
)++, 0,
729 " rx: %16.3llf MiB/t "
733 ((long double) rel
->rx_bytes
) / (1LLU << 20),
734 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
736 mvwprintw(screen
, (*voff
)++, 0,
737 " tx: %16.3llf MiB/t "
741 ((long double) rel
->tx_bytes
) / (1LLU << 20),
742 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
747 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
750 mvwprintw(screen
, (*voff
)++, 2,
755 ((long double) abs
->rx_bytes
) / (1LLU << 20),
756 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
758 mvwprintw(screen
, (*voff
)++, 2,
763 ((long double) abs
->tx_bytes
) / (1LLU << 20),
764 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
767 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
768 const struct ifstat
*abs
, int *voff
)
770 mvwprintw(screen
, (*voff
)++, 2,
775 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
778 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
780 mvwprintw(screen
, (*voff
)++, 2,
785 abs
->mem_total
/ 1024,
786 (abs
->mem_total
- abs
->mem_free
) / 1024,
787 abs
->mem_active
/ 1024,
788 abs
->mem_inactive
/ 1024);
790 mvwprintw(screen
, (*voff
)++, 2,
794 abs
->swap_total
/ 1024,
795 (abs
->swap_total
- abs
->swap_free
) / 1024,
796 abs
->swap_cached
/ 1024);
799 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
800 int *voff
, unsigned int idx
, char *tag
)
802 int max_padd
= padding_from_num(get_number_cpus());
803 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
804 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
806 mvwprintw(screen
, (*voff
)++, 2,
807 "cpu%*d %s: %11.1lf%% usr/t "
812 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
813 100.0 * rel
->cpu_sys
[idx
] / all
,
814 100.0 * rel
->cpu_idle
[idx
] / all
,
815 100.0 * rel
->cpu_iow
[idx
] / all
);
818 #define MEDIAN_EVEN(member) do { \
819 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
822 #define MEDIAN_ODD(member) do { \
823 m_##member = rel->member[i]; \
826 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
827 const struct avg_stat
*avg
, int top_cpus
,
831 int cpus
= get_number_cpus();
832 int max_padd
= padding_from_num(cpus
);
838 /* Display top hitter */
839 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
841 /* Make sure we don't display the min. hitter twice */
842 if (top_cpus
== cpus
)
845 for (i
= 1; i
< top_cpus
; ++i
)
846 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
848 /* Display minimum hitter */
850 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
852 all
= avg
->cpu_user
+ avg
->cpu_sys
+ avg
->cpu_nice
+ avg
->cpu_idle
+ avg
->cpu_iow
;
853 mvwprintw(screen
, (*voff
)++, 2,
857 "%11.1lf%%", max_padd
, "",
858 100.0 * (avg
->cpu_user
+ avg
->cpu_nice
) / all
,
859 100.0 * avg
->cpu_sys
/ all
,
860 100.0 * avg
->cpu_idle
/all
,
861 100.0 * avg
->cpu_iow
/ all
);
864 long double m_cpu_user
, m_cpu_nice
, m_cpu_sys
, m_cpu_idle
, m_cpu_iow
;
867 i
= cpu_hits
[cpus
/ 2].idx
;
869 /* take the mean of the 2 middle entries */
870 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
872 MEDIAN_EVEN(cpu_user
);
873 MEDIAN_EVEN(cpu_nice
);
874 MEDIAN_EVEN(cpu_sys
);
875 MEDIAN_EVEN(cpu_idle
);
876 MEDIAN_EVEN(cpu_iow
);
878 /* take the middle entry as is */
879 MEDIAN_ODD(cpu_user
);
880 MEDIAN_ODD(cpu_nice
);
882 MEDIAN_ODD(cpu_idle
);
886 m_all
= m_cpu_user
+ m_cpu_sys
+ m_cpu_nice
+ m_cpu_idle
+ m_cpu_iow
;
887 mvwprintw(screen
, (*voff
)++, 2,
891 "%11.1Lf%%", max_padd
, "",
892 100.0 * (m_cpu_user
+ m_cpu_nice
) / m_all
,
893 100.0 * m_cpu_sys
/ m_all
,
894 100.0 * m_cpu_idle
/m_all
,
895 100.0 * m_cpu_iow
/ m_all
);
899 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
900 int *voff
, unsigned int idx
, char *tag
)
902 int max_padd
= padding_from_num(get_number_cpus());
904 mvwprintw(screen
, (*voff
)++, 2,
905 "cpu%*d %s: %12llu irqs/t "
914 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
915 const struct avg_stat
*avg
, int top_cpus
,
919 int cpus
= get_number_cpus();
920 int max_padd
= padding_from_num(cpus
);
922 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
924 if (top_cpus
== cpus
)
927 for (i
= 1; i
< top_cpus
; ++i
)
928 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "|");
931 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
933 mvwprintw(screen
, (*voff
)++, 2,
936 "%17.1Lf", max_padd
, "",
937 avg
->irqs_rel
, avg
->irqs_srx_rel
, avg
->irqs_stx_rel
);
940 long double m_irqs
, m_irqs_srx
, m_irqs_stx
;
942 i
= cpu_hits
[cpus
/ 2].idx
;
944 /* take the mean of the 2 middle entries */
945 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
948 MEDIAN_EVEN(irqs_srx
);
949 MEDIAN_EVEN(irqs_stx
);
951 /* take the middle entry as is */
953 MEDIAN_ODD(irqs_srx
);
954 MEDIAN_ODD(irqs_stx
);
957 mvwprintw(screen
, (*voff
)++, 2,
960 "%17.1Lf", max_padd
, "",
961 m_irqs
, m_irqs_srx
, m_irqs_stx
);
965 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
966 int *voff
, unsigned int idx
, char *tag
)
968 int max_padd
= padding_from_num(get_number_cpus());
970 mvwprintw(screen
, (*voff
)++, 2,
971 "cpu%*d %s: %12llu irqs",
972 max_padd
, idx
, tag
, abs
->irqs
[idx
]);
975 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
976 const struct avg_stat
*avg
, int top_cpus
,
980 int cpus
= get_number_cpus();
981 int max_padd
= padding_from_num(cpus
);
983 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
985 if (top_cpus
== cpus
)
988 for (i
= 1; i
< top_cpus
; ++i
)
989 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "|");
992 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
994 mvwprintw(screen
, (*voff
)++, 2,
995 "avg:%*s%17.1Lf", max_padd
, "", avg
->irqs_abs
);
1000 i
= cpu_hits
[cpus
/ 2].idx
;
1001 if (cpus
% 2 == 0) {
1002 /* take the mean of the 2 middle entries */
1003 int j
= cpu_hits
[(cpus
/ 2) - 1].idx
;
1005 m_irqs
= (abs
->irqs
[i
] + abs
->irqs
[j
]) / 2;
1007 /* take the middle entry as is */
1008 m_irqs
= abs
->irqs
[i
];
1011 mvwprintw(screen
, (*voff
)++, 2,
1012 "med:%*s%17.1Lf", max_padd
, "", m_irqs
);
1016 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
1017 const struct ifstat
*abs
, int *voff
)
1019 if (iswireless(abs
)) {
1020 mvwprintw(screen
, (*voff
)++, 2,
1021 "linkqual: %7d/%d (%d/t) ",
1022 abs
->wifi
.link_qual
,
1023 abs
->wifi
.link_qual_max
,
1024 rel
->wifi
.link_qual
);
1026 mvwprintw(screen
, (*voff
)++, 2,
1027 "signal: %8d dBm (%d dBm/t) ",
1028 abs
->wifi
.signal_level
,
1029 rel
->wifi
.signal_level
);
1033 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
1034 const struct ifstat
*abs
, const struct avg_stat
*avg
,
1035 int *first
, uint64_t ms_interval
, unsigned int top_cpus
,
1038 unsigned int cpus
, top
;
1039 int voff
= 1, cvoff
= 2;
1040 u32 rate
= device_bitrate(ifname
);
1044 cpus
= get_number_cpus();
1045 top
= min(cpus
, top_cpus
);
1047 stats_top(rel
, abs
, cpus
);
1049 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
1051 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
1054 screen_net_dev_rel(screen
, rel
, &voff
);
1057 screen_net_dev_abs(screen
, abs
, &voff
);
1060 screen_sys(screen
, rel
, abs
, &voff
);
1063 screen_mem_swap(screen
, abs
, &voff
);
1066 screen_percpu_states(screen
, rel
, avg
, top
, &voff
);
1068 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
1071 screen_percpu_irqs_rel(screen
, rel
, avg
, top
, &voff
);
1073 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
1076 screen_percpu_irqs_abs(screen
, abs
, avg
, top
, &voff
);
1079 screen_wireless(screen
, rel
, abs
, &voff
);
1082 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
1086 mvwprintw(screen
, cvoff
, 2, "(consider to increase "
1087 "your sampling interval, e.g. -t %d)",
1088 rate
> SPEED_1000
? 10000 : 1000);
1090 mvwprintw(screen
, cvoff
, 2, " "
1098 static int screen_main(const char *ifname
, uint64_t ms_interval
,
1099 unsigned int top_cpus
, bool suppress_warnings
)
1102 u32 rate
= device_bitrate(ifname
);
1103 bool need_info
= false;
1105 stats_screen
= screen_init(true);
1107 if (((rate
> SPEED_1000
&& ms_interval
<= 1000) ||
1108 (rate
= SPEED_1000
&& ms_interval
< 1000)) &&
1114 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
1117 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
, &stats_avg
,
1118 &first
, ms_interval
, top_cpus
, need_info
);
1120 stats_sample_generic(ifname
, ms_interval
);
1128 static void term_csv(const struct ifstat
*rel
, const struct ifstat
*abs
)
1132 printf("%ld ", time(NULL
));
1134 printf("%llu ", rel
->rx_bytes
);
1135 printf("%llu ", rel
->rx_packets
);
1136 printf("%llu ", rel
->rx_drops
);
1137 printf("%llu ", rel
->rx_errors
);
1139 printf("%llu ", abs
->rx_bytes
);
1140 printf("%llu ", abs
->rx_packets
);
1141 printf("%llu ", abs
->rx_drops
);
1142 printf("%llu ", abs
->rx_errors
);
1144 printf("%llu ", rel
->tx_bytes
);
1145 printf("%llu ", rel
->tx_packets
);
1146 printf("%llu ", rel
->tx_drops
);
1147 printf("%llu ", rel
->tx_errors
);
1149 printf("%llu ", abs
->tx_bytes
);
1150 printf("%llu ", abs
->tx_packets
);
1151 printf("%llu ", abs
->tx_drops
);
1152 printf("%llu ", abs
->tx_errors
);
1154 printf("%u ", rel
->cswitch
);
1155 printf("%lu ", abs
->mem_free
);
1156 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
1157 printf("%lu ", abs
->mem_total
);
1158 printf("%lu ", abs
->swap_free
);
1159 printf("%lu ", abs
->swap_total
- abs
->swap_free
);
1160 printf("%lu ", abs
->swap_total
);
1161 printf("%u ", abs
->procs_total
);
1162 printf("%u ", abs
->procs_run
);
1163 printf("%u ", abs
->procs_iow
);
1165 cpus
= get_number_cpus();
1167 for (i
= 0; i
< cpus
; ++i
) {
1168 printf("%lu ", rel
->cpu_user
[i
]);
1169 printf("%lu ", rel
->cpu_nice
[i
]);
1170 printf("%lu ", rel
->cpu_sys
[i
]);
1171 printf("%lu ", rel
->cpu_idle
[i
]);
1172 printf("%lu ", rel
->cpu_iow
[i
]);
1174 printf("%llu ", rel
->irqs
[i
]);
1175 printf("%llu ", abs
->irqs
[i
]);
1177 printf("%llu ", rel
->irqs_srx
[i
]);
1178 printf("%llu ", abs
->irqs_srx
[i
]);
1180 printf("%llu ", rel
->irqs_stx
[i
]);
1181 printf("%llu ", abs
->irqs_stx
[i
]);
1184 if (iswireless(abs
)) {
1185 printf("%u ", rel
->wifi
.link_qual
);
1186 printf("%u ", abs
->wifi
.link_qual
);
1187 printf("%u ", abs
->wifi
.link_qual_max
);
1189 printf("%d ", rel
->wifi
.signal_level
);
1190 printf("%d ", abs
->wifi
.signal_level
);
1197 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1198 uint64_t ms_interval
)
1202 printf("# gnuplot dump (#col:description)\n");
1203 printf("# networking interface: %s\n", ifname
);
1204 printf("# sampling interval (t): %lu ms\n", ms_interval
);
1205 printf("# %d:unixtime ", j
++);
1207 printf("%d:rx-bytes-per-t ", j
++);
1208 printf("%d:rx-pkts-per-t ", j
++);
1209 printf("%d:rx-drops-per-t ", j
++);
1210 printf("%d:rx-errors-per-t ", j
++);
1212 printf("%d:rx-bytes ", j
++);
1213 printf("%d:rx-pkts ", j
++);
1214 printf("%d:rx-drops ", j
++);
1215 printf("%d:rx-errors ", j
++);
1217 printf("%d:tx-bytes-per-t ", j
++);
1218 printf("%d:tx-pkts-per-t ", j
++);
1219 printf("%d:tx-drops-per-t ", j
++);
1220 printf("%d:tx-errors-per-t ", j
++);
1222 printf("%d:tx-bytes ", j
++);
1223 printf("%d:tx-pkts ", j
++);
1224 printf("%d:tx-drops ", j
++);
1225 printf("%d:tx-errors ", j
++);
1227 printf("%d:context-switches-per-t ", j
++);
1228 printf("%d:mem-free ", j
++);
1229 printf("%d:mem-used ", j
++);
1230 printf("%d:mem-total ", j
++);
1231 printf("%d:swap-free ", j
++);
1232 printf("%d:swap-used ", j
++);
1233 printf("%d:swap-total ", j
++);
1234 printf("%d:procs-total ", j
++);
1235 printf("%d:procs-in-run ", j
++);
1236 printf("%d:procs-in-iow ", j
++);
1238 cpus
= get_number_cpus();
1240 for (i
= 0; i
< cpus
; ++i
) {
1241 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1242 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1243 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1244 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1245 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1247 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1248 printf("%d:cpu%i-net-irqs ", j
++, i
);
1250 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1251 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1252 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1253 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1256 if (iswireless(abs
)) {
1257 printf("%d:wifi-link-qual-per-t ", j
++);
1258 printf("%d:wifi-link-qual ", j
++);
1259 printf("%d:wifi-link-qual-max ", j
++);
1261 printf("%d:wifi-signal-dbm-per-t ", j
++);
1262 printf("%d:wifi-signal-dbm ", j
++);
1266 printf("# data:\n");
1270 static int term_main(const char *ifname
, uint64_t ms_interval
,
1271 unsigned int top_cpus __maybe_unused
,
1272 bool suppress_warnings __maybe_unused
)
1277 stats_sample_generic(ifname
, ms_interval
);
1281 term_csv_header(ifname
, &stats_new
, ms_interval
);
1284 term_csv(&stats_delta
, &stats_new
);
1285 } while (stats_loop
&& !sigint
);
1290 int main(int argc
, char **argv
)
1293 int c
, opt_index
, ret
, promisc
= 0;
1294 unsigned int cpus
, top_cpus
= 5;
1295 uint64_t interval
= 1000;
1296 char *ifname
= NULL
;
1297 bool suppress_warnings
= false;
1298 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1299 unsigned int top_cpus
, bool suppress_warnings
);
1301 func_main
= screen_main
;
1306 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1307 &opt_index
)) != EOF
) {
1316 suppress_warnings
= true;
1319 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1322 interval
= strtoul(optarg
, NULL
, 10);
1325 top_cpus
= strtoul(optarg
, NULL
, 10);
1327 panic("Number of top hitter CPUs must be greater than 0");
1339 func_main
= term_main
;
1345 panic("Option -%c requires an argument!\n",
1348 if (isprint(optopt
))
1349 printf("Unknown option character `0x%X\'!\n", optopt
);
1361 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1363 panic("No networking device given!\n");
1365 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1366 panic("lo is not supported!\n");
1367 if (device_mtu(ifname
) == 0)
1368 panic("This is no networking device!\n");
1370 register_signal(SIGINT
, signal_handler
);
1371 register_signal(SIGHUP
, signal_handler
);
1373 cpus
= get_number_cpus();
1374 top_cpus
= min(top_cpus
, cpus
);
1375 if (uname(&uts
) < 0)
1376 panic("Cannot execute uname!\n");
1378 stats_alloc(&stats_old
, cpus
);
1379 stats_alloc(&stats_new
, cpus
);
1380 stats_alloc(&stats_delta
, cpus
);
1382 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1385 ifflags
= device_enter_promiscuous_mode(ifname
);
1386 ret
= func_main(ifname
, interval
, top_cpus
, suppress_warnings
);
1388 device_leave_promiscuous_mode(ifname
, ifflags
);
1390 stats_release(&stats_old
);
1391 stats_release(&stats_new
);
1392 stats_release(&stats_delta
);