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>
35 int16_t link_qual
, link_qual_max
;
36 int signal_level
/*, noise_level*/;
40 long long unsigned int rx_bytes
, rx_packets
, rx_drops
, rx_errors
;
41 long long unsigned int rx_fifo
, rx_frame
, rx_multi
;
42 long long unsigned int tx_bytes
, tx_packets
, tx_drops
, tx_errors
;
43 long long unsigned int tx_fifo
, tx_colls
, tx_carrier
;
44 uint64_t mem_free
, mem_total
, mem_active
, mem_inactive
;
45 uint64_t swap_total
, swap_free
, swap_cached
;
46 uint32_t irq_nr
, procs_total
, procs_run
, procs_iow
, cswitch
;
47 struct wifi_stat wifi
;
49 * Pointer members need to be last in order for stats_zero() to work
52 long long unsigned int *irqs
, *irqs_srx
, *irqs_stx
;
53 uint64_t *cpu_user
, *cpu_sys
, *cpu_nice
, *cpu_idle
, *cpu_iow
;
59 long long unsigned int irqs_rel
, irqs_abs
;
62 static volatile sig_atomic_t sigint
= 0;
63 static struct ifstat stats_old
, stats_new
, stats_delta
;
64 static struct cpu_hit
*cpu_hits
;
65 static int stats_loop
= 0;
66 static WINDOW
*stats_screen
= NULL
;
68 static const char *short_options
= "d:t:n:vhclp";
69 static const struct option long_options
[] = {
70 {"dev", required_argument
, NULL
, 'd'},
71 {"interval", required_argument
, NULL
, 't'},
72 {"num-cpus", required_argument
, NULL
, 'n'},
73 {"promisc", no_argument
, NULL
, 'p'},
74 {"csv", no_argument
, NULL
, 'c'},
75 {"loop", no_argument
, NULL
, 'l'},
76 {"version", no_argument
, NULL
, 'v'},
77 {"help", no_argument
, NULL
, 'h'},
81 static void signal_handler(int number
)
93 static inline int iswireless(const struct ifstat
*stats
)
95 return stats
->wifi
.bitrate
> 0;
98 static void __noreturn
help(void)
100 printf("\nifpps %s, top-like kernel networking and system statistics\n",
102 puts("http://www.netsniff-ng.org\n\n"
103 "Usage: ifpps [options] || ifpps <netdev>\n"
105 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
106 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
107 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
108 " in ncurses mode (default 10)\n"
109 " -p|--promisc Promiscuous mode\n"
110 " -c|--csv Output to terminal as Gnuplot-ready data\n"
111 " -l|--loop Continuous CSV output\n"
112 " -v|--version Print version and exit\n"
113 " -h|--help Print this help and exit\n\n"
117 " ifpps -lpcd wlan0 > plot.dat\n\n"
119 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
120 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
121 "Please report bugs to <bugs@netsniff-ng.org>\n"
122 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
123 "Swiss federal institute of technology (ETH Zurich)\n"
124 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
125 "License: GNU GPL version 2.0\n"
126 "This is free software: you are free to change and redistribute it.\n"
127 "There is NO WARRANTY, to the extent permitted by law.\n");
131 static void __noreturn
version(void)
133 printf("\nifpps %s, top-like kernel networking and system statistics\n",
135 puts("http://www.netsniff-ng.org\n\n"
136 "Please report bugs to <bugs@netsniff-ng.org>\n"
137 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
138 "Swiss federal institute of technology (ETH Zurich)\n"
139 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
140 "License: GNU GPL version 2.0\n"
141 "This is free software: you are free to change and redistribute it.\n"
142 "There is NO WARRANTY, to the extent permitted by law.\n");
146 static inline int padding_from_num(int n
)
150 while ((n
/= 10) > 0);
154 #define STATS_ALLOC1(member) \
155 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
157 static void stats_alloc(struct ifstat
*stats
, int cpus
)
160 STATS_ALLOC1(irqs_srx
);
161 STATS_ALLOC1(irqs_stx
);
163 STATS_ALLOC1(cpu_user
);
164 STATS_ALLOC1(cpu_sys
);
165 STATS_ALLOC1(cpu_nice
);
166 STATS_ALLOC1(cpu_idle
);
167 STATS_ALLOC1(cpu_iow
);
170 #define STATS_ZERO1(member) \
171 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
173 static void stats_zero(struct ifstat
*stats
, int cpus
)
175 /* Only clear the non-pointer members */
176 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
179 STATS_ZERO1(irqs_srx
);
180 STATS_ZERO1(irqs_stx
);
182 STATS_ZERO1(cpu_user
);
183 STATS_ZERO1(cpu_sys
);
184 STATS_ZERO1(cpu_nice
);
185 STATS_ZERO1(cpu_idle
);
186 STATS_ZERO1(cpu_iow
);
189 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
195 fp
= fopen("/proc/net/dev", "r");
197 panic("Cannot open /proc/net/dev!\n");
199 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
200 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
202 memset(buff
, 0, sizeof(buff
));
204 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
205 buff
[sizeof(buff
) -1] = 0;
207 if (strstr(buff
, ifname
) == NULL
)
210 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
211 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
212 &stats
->rx_bytes
, &stats
->rx_packets
,
213 &stats
->rx_errors
, &stats
->rx_drops
,
214 &stats
->rx_fifo
, &stats
->rx_frame
,
215 &stats
->rx_multi
, &stats
->tx_bytes
,
216 &stats
->tx_packets
, &stats
->tx_errors
,
217 &stats
->tx_drops
, &stats
->tx_fifo
,
218 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
223 memset(buff
, 0, sizeof(buff
));
230 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
232 int ret
= -EINVAL
, i
, cpus
, try = 0;
236 struct ethtool_drvinfo drvinf
;
239 fp
= fopen("/proc/interrupts", "r");
241 panic("Cannot open /proc/interrupts!\n");
243 cpus
= get_number_cpus();
244 buff_len
= cpus
* 128;
245 buff
= xmalloc(buff_len
);
247 fseek(fp
, 0, SEEK_SET
);
248 memset(buff
, 0, buff_len
);
250 while (fgets(buff
, buff_len
, fp
) != NULL
) {
251 buff
[buff_len
- 1] = 0;
254 if (strstr(buff
, ifname
) == NULL
)
257 /* XXX: remove this one here */
258 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
259 bug_on(stats
->irq_nr
== 0);
263 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
265 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
267 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
274 memset(buff
, 0, buff_len
);
277 if (ret
== -EINVAL
&& try == 0) {
278 memset(&drvinf
, 0, sizeof(drvinf
));
279 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
282 ifname
= drvinf
.driver
;
293 static int stats_proc_softirqs(struct ifstat
*stats
)
304 fp
= fopen("/proc/softirqs", "r");
306 panic("Cannot open /proc/softirqs!\n");
308 cpus
= get_number_cpus();
309 buff_len
= cpus
* 128;
310 buff
= xmalloc(buff_len
);
312 memset(buff
, 0, buff_len
);
314 while (fgets(buff
, buff_len
, fp
) != NULL
) {
315 buff
[buff_len
- 1] = 0;
317 if ((ptr
= strstr(buff
, "NET_TX:")))
318 net_type
= softirqs_net_tx
;
319 else if ((ptr
= strstr(buff
, "NET_RX:")))
320 net_type
= softirqs_net_rx
;
324 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
326 case softirqs_net_tx
:
327 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
329 case softirqs_net_rx
:
330 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
335 memset(buff
, 0, buff_len
);
343 static int stats_proc_memory(struct ifstat
*stats
)
345 char *ptr
, buff
[256];
348 fp
= fopen("/proc/meminfo", "r");
350 panic("Cannot open /proc/meminfo!\n");
352 memset(buff
, 0, sizeof(buff
));
354 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
355 buff
[sizeof(buff
) - 1] = 0;
357 if ((ptr
= strstr(buff
, "MemTotal:"))) {
358 ptr
+= strlen("MemTotal:");
359 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
360 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
361 ptr
+= strlen("MemFree:");
362 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
363 } else if ((ptr
= strstr(buff
, "Active:"))) {
364 ptr
+= strlen("Active:");
365 stats
->mem_active
= strtoul(ptr
, &ptr
, 10);
366 } else if ((ptr
= strstr(buff
, "Inactive:"))) {
367 ptr
+= strlen("Inactive:");
368 stats
->mem_inactive
= strtoul(ptr
, &ptr
, 10);
369 } else if ((ptr
= strstr(buff
, "SwapTotal:"))) {
370 ptr
+= strlen("SwapTotal:");
371 stats
->swap_total
= strtoul(ptr
, &ptr
, 10);
372 } else if ((ptr
= strstr(buff
, "SwapFree:"))) {
373 ptr
+= strlen("SwapFree:");
374 stats
->swap_free
= strtoul(ptr
, &ptr
, 10);
375 } else if ((ptr
= strstr(buff
, "SwapCached:"))) {
376 ptr
+= strlen("SwapCached:");
377 stats
->swap_cached
= strtoul(ptr
, &ptr
, 10);
380 memset(buff
, 0, sizeof(buff
));
387 static int stats_proc_system(struct ifstat
*stats
)
390 char *ptr
, buff
[256];
393 fp
= fopen("/proc/stat", "r");
395 panic("Cannot open /proc/stat!\n");
397 cpus
= get_number_cpus();
399 memset(buff
, 0, sizeof(buff
));
401 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
402 buff
[sizeof(buff
) - 1] = 0;
404 if ((ptr
= strstr(buff
, "cpu"))) {
405 ptr
+= strlen("cpu");
409 cpu
= strtol(ptr
, &ptr
, 10);
412 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
413 &stats
->cpu_user
[cpu
],
414 &stats
->cpu_nice
[cpu
],
415 &stats
->cpu_sys
[cpu
],
416 &stats
->cpu_idle
[cpu
],
417 &stats
->cpu_iow
[cpu
]) != 5)
419 } else if ((ptr
= strstr(buff
, "ctxt"))) {
420 ptr
+= strlen("ctxt");
421 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
422 } else if ((ptr
= strstr(buff
, "procs_running"))) {
423 ptr
+= strlen("procs_running");
424 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
425 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
426 ptr
+= strlen("procs_blocked");
427 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
430 memset(buff
, 0, sizeof(buff
));
437 static int stats_proc_procs(struct ifstat
*stats
)
442 dir
= opendir("/proc");
444 panic("Cannot open /proc\n");
446 stats
->procs_total
= 0;
448 while ((e
= readdir(dir
)) != NULL
) {
449 const char *name
= e
->d_name
;
451 unsigned int pid
= strtoul(name
, &end
, 10);
454 if (pid
== 0 && end
== name
)
457 stats
->procs_total
++;
465 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
470 return dbm_val
- 0x100;
473 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
476 struct iw_statistics ws
;
478 ret
= wireless_sigqual(ifname
, &ws
);
480 stats
->wifi
.bitrate
= 0;
484 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
486 stats
->wifi
.signal_level
=
487 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
489 stats
->wifi
.link_qual
= ws
.qual
.qual
;
490 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
495 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
496 #define DIFF(member) do { \
497 if (sizeof(diff->member) != sizeof(new->member) || \
498 sizeof(diff->member) != sizeof(old->member)) \
500 bug_on((new->member - old->member) > (new->member)); \
504 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
525 DIFF1(wifi
.signal_level
);
526 DIFF1(wifi
.link_qual
);
530 cpus
= get_number_cpus();
532 for (i
= 0; i
< cpus
; ++i
) {
545 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
547 if (stats_proc_net_dev(ifname
, stats
) < 0)
548 panic("Cannot fetch device stats!\n");
549 if (stats_proc_softirqs(stats
) < 0)
550 panic("Cannot fetch software interrupts!\n");
551 if (stats_proc_memory(stats
) < 0)
552 panic("Cannot fetch memory stats!\n");
553 if (stats_proc_system(stats
) < 0)
554 panic("Cannot fetch system stats!\n");
555 if (stats_proc_procs(stats
) < 0)
556 panic("Cannot fetch process stats!\n");
558 stats_proc_interrupts((char *) ifname
, stats
);
560 stats_wireless(ifname
, stats
);
563 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
565 int cpus
= get_number_cpus();
567 stats_zero(&stats_old
, cpus
);
568 stats_zero(&stats_new
, cpus
);
569 stats_zero(&stats_delta
, cpus
);
571 stats_fetch(ifname
, &stats_old
);
572 usleep(ms_interval
* 1000);
573 stats_fetch(ifname
, &stats_new
);
575 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
578 static int cmp_hits(const void *p1
, const void *p2
)
580 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
583 * We want the hits sorted in descending order, thus reverse the return
586 if (h1
->hit
== h2
->hit
)
588 else if (h1
->hit
< h2
->hit
)
594 static int cmp_irqs_rel(const void *p1
, const void *p2
)
596 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
599 * We want the hits sorted in descending order, thus reverse the return
602 if (h1
->irqs_rel
== h2
->irqs_rel
)
604 else if (h1
->irqs_rel
< h2
->irqs_rel
)
610 static int cmp_irqs_abs(const void *p1
, const void *p2
)
612 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
615 * We want the hits sorted in descending order, thus reverse the return
618 if (h1
->irqs_abs
== h2
->irqs_abs
)
620 else if (h1
->irqs_abs
< h2
->irqs_abs
)
626 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
631 for (i
= 0; i
< cpus
; ++i
) {
633 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
634 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
635 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
639 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
640 uint64_t ms_interval
, unsigned int top_cpus
)
644 struct ethtool_drvinfo drvinf
;
645 u32 rate
= device_bitrate(ifname
);
646 int link
= ethtool_link(ifname
);
647 unsigned int cpus
= get_number_cpus();
649 memset(&drvinf
, 0, sizeof(drvinf
));
650 ethtool_drvinf(ifname
, &drvinf
);
652 memset(buff
, 0, sizeof(buff
));
654 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
656 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
657 link
== 0 ? "no" : "yes");
659 mvwprintw(screen
, (*voff
)++, 2,
660 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u%s/%u"
662 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
663 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
666 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
671 mvwprintw(screen
, (*voff
)++, 0,
672 " rx: %16.3llf MiB/t "
676 ((long double) rel
->rx_bytes
) / (1LLU << 20),
677 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
679 mvwprintw(screen
, (*voff
)++, 0,
680 " tx: %16.3llf MiB/t "
684 ((long double) rel
->tx_bytes
) / (1LLU << 20),
685 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
690 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
693 mvwprintw(screen
, (*voff
)++, 2,
698 ((long double) abs
->rx_bytes
) / (1LLU << 20),
699 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
701 mvwprintw(screen
, (*voff
)++, 2,
706 ((long double) abs
->tx_bytes
) / (1LLU << 20),
707 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
710 static void screen_sys(WINDOW
*screen
, const struct ifstat
*rel
,
711 const struct ifstat
*abs
, int *voff
)
713 mvwprintw(screen
, (*voff
)++, 2,
718 rel
->cswitch
, abs
->procs_total
, abs
->procs_run
, abs
->procs_iow
);
721 static void screen_mem_swap(WINDOW
*screen
, const struct ifstat
*abs
, int *voff
)
723 mvwprintw(screen
, (*voff
)++, 2,
728 abs
->mem_total
/ 1024,
729 (abs
->mem_total
- abs
->mem_free
) / 1024,
730 abs
->mem_active
/ 1024,
731 abs
->mem_inactive
/ 1024);
733 mvwprintw(screen
, (*voff
)++, 2,
737 abs
->swap_total
/ 1024,
738 (abs
->swap_total
- abs
->swap_free
) / 1024,
739 abs
->swap_cached
/ 1024);
742 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
743 int *voff
, unsigned int idx
, char *tag
)
745 int max_padd
= padding_from_num(get_number_cpus());
746 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
747 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
749 mvwprintw(screen
, (*voff
)++, 2,
750 "cpu%*d%s:%s %12.1lf%% usr/t "
753 "%11.1lf%% iow/t ", max_padd
, idx
,
754 tag
, strlen(tag
) == 0 ? " " : "",
755 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
756 100.0 * rel
->cpu_sys
[idx
] / all
,
757 100.0 * rel
->cpu_idle
[idx
] / all
,
758 100.0 * rel
->cpu_iow
[idx
] / all
);
761 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
762 int top_cpus
, int *voff
)
765 int cpus
= get_number_cpus();
770 /* Display top hitter */
771 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
773 /* Make sure we don't display the min. hitter twice */
774 if (top_cpus
== cpus
)
777 for (i
= 1; i
< top_cpus
; ++i
)
778 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "");
780 /* Display minimum hitter */
782 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
785 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
786 int *voff
, unsigned int idx
, char *tag
)
788 int max_padd
= padding_from_num(get_number_cpus());
790 mvwprintw(screen
, (*voff
)++, 2,
791 "cpu%*d%s:%s %13llu irqs/t "
793 "%15llu sirq tx/t ", max_padd
, idx
,
794 tag
, strlen(tag
) == 0 ? " " : "",
800 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
801 int top_cpus
, int *voff
)
804 int cpus
= get_number_cpus();
806 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
808 if (top_cpus
== cpus
)
811 for (i
= 1; i
< top_cpus
; ++i
)
812 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "");
815 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
818 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
819 int *voff
, unsigned int idx
, char *tag
)
821 int max_padd
= padding_from_num(get_number_cpus());
823 mvwprintw(screen
, (*voff
)++, 2,
824 "cpu%*d%s:%s %13llu irqs", max_padd
, idx
,
825 tag
, strlen(tag
) == 0 ? " " : "",
829 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
830 int top_cpus
, int *voff
)
833 int cpus
= get_number_cpus();
835 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
837 if (top_cpus
== cpus
)
840 for (i
= 1; i
< top_cpus
; ++i
)
841 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "");
844 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
847 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
848 const struct ifstat
*abs
, int *voff
)
850 if (iswireless(abs
)) {
851 mvwprintw(screen
, (*voff
)++, 2,
852 "linkqual: %7d/%d (%d/t) ",
854 abs
->wifi
.link_qual_max
,
855 rel
->wifi
.link_qual
);
857 mvwprintw(screen
, (*voff
)++, 2,
858 "signal: %8d dBm (%d dBm/t) ",
859 abs
->wifi
.signal_level
,
860 rel
->wifi
.signal_level
);
864 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
865 const struct ifstat
*abs
, int *first
, uint64_t ms_interval
,
866 unsigned int top_cpus
)
868 int cpus
, top
, voff
= 1, cvoff
= 2;
872 cpus
= get_number_cpus();
873 top
= min(cpus
, top_cpus
);
875 stats_top(rel
, abs
, cpus
);
877 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
879 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
882 screen_net_dev_rel(screen
, rel
, &voff
);
885 screen_net_dev_abs(screen
, abs
, &voff
);
888 screen_sys(screen
, rel
, abs
, &voff
);
891 screen_mem_swap(screen
, abs
, &voff
);
894 screen_percpu_states(screen
, rel
, top
, &voff
);
896 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
899 screen_percpu_irqs_rel(screen
, rel
, top
, &voff
);
901 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
904 screen_percpu_irqs_abs(screen
, abs
, top
, &voff
);
907 screen_wireless(screen
, rel
, abs
, &voff
);
910 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
913 mvwprintw(screen
, cvoff
, 2, " ");
920 static int screen_main(const char *ifname
, uint64_t ms_interval
,
921 unsigned int top_cpus
)
925 stats_screen
= screen_init(true);
929 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
932 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
,
933 &first
, ms_interval
, top_cpus
);
935 stats_sample_generic(ifname
, ms_interval
);
943 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
944 const struct ifstat
*abs
, uint64_t ms_interval
)
948 printf("%ld ", time(0));
950 printf("%llu ", rel
->rx_bytes
);
951 printf("%llu ", rel
->rx_packets
);
952 printf("%llu ", rel
->rx_drops
);
953 printf("%llu ", rel
->rx_errors
);
955 printf("%llu ", abs
->rx_bytes
);
956 printf("%llu ", abs
->rx_packets
);
957 printf("%llu ", abs
->rx_drops
);
958 printf("%llu ", abs
->rx_errors
);
960 printf("%llu ", rel
->tx_bytes
);
961 printf("%llu ", rel
->tx_packets
);
962 printf("%llu ", rel
->tx_drops
);
963 printf("%llu ", rel
->tx_errors
);
965 printf("%llu ", abs
->tx_bytes
);
966 printf("%llu ", abs
->tx_packets
);
967 printf("%llu ", abs
->tx_drops
);
968 printf("%llu ", abs
->tx_errors
);
970 printf("%u ", rel
->cswitch
);
971 printf("%lu ", abs
->mem_free
);
972 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
973 printf("%lu ", abs
->mem_total
);
974 printf("%lu ", abs
->swap_free
);
975 printf("%lu ", abs
->swap_total
- abs
->swap_free
);
976 printf("%lu ", abs
->swap_total
);
977 printf("%u ", abs
->procs_total
);
978 printf("%u ", abs
->procs_run
);
979 printf("%u ", abs
->procs_iow
);
981 cpus
= get_number_cpus();
983 for (i
= 0; i
< cpus
; ++i
) {
984 printf("%lu ", rel
->cpu_user
[i
]);
985 printf("%lu ", rel
->cpu_nice
[i
]);
986 printf("%lu ", rel
->cpu_sys
[i
]);
987 printf("%lu ", rel
->cpu_idle
[i
]);
988 printf("%lu ", rel
->cpu_iow
[i
]);
990 printf("%llu ", rel
->irqs
[i
]);
991 printf("%llu ", abs
->irqs
[i
]);
993 printf("%llu ", rel
->irqs_srx
[i
]);
994 printf("%llu ", abs
->irqs_srx
[i
]);
996 printf("%llu ", rel
->irqs_stx
[i
]);
997 printf("%llu ", abs
->irqs_stx
[i
]);
1000 if (iswireless(abs
)) {
1001 printf("%u ", rel
->wifi
.link_qual
);
1002 printf("%u ", abs
->wifi
.link_qual
);
1003 printf("%u ", abs
->wifi
.link_qual_max
);
1005 printf("%d ", rel
->wifi
.signal_level
);
1006 printf("%d ", abs
->wifi
.signal_level
);
1013 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
1014 uint64_t ms_interval
)
1018 printf("# gnuplot dump (#col:description)\n");
1019 printf("# networking interface: %s\n", ifname
);
1020 printf("# sampling interval (t): %lu ms\n", ms_interval
);
1021 printf("# %d:unixtime ", j
++);
1023 printf("%d:rx-bytes-per-t ", j
++);
1024 printf("%d:rx-pkts-per-t ", j
++);
1025 printf("%d:rx-drops-per-t ", j
++);
1026 printf("%d:rx-errors-per-t ", j
++);
1028 printf("%d:rx-bytes ", j
++);
1029 printf("%d:rx-pkts ", j
++);
1030 printf("%d:rx-drops ", j
++);
1031 printf("%d:rx-errors ", j
++);
1033 printf("%d:tx-bytes-per-t ", j
++);
1034 printf("%d:tx-pkts-per-t ", j
++);
1035 printf("%d:tx-drops-per-t ", j
++);
1036 printf("%d:tx-errors-per-t ", j
++);
1038 printf("%d:tx-bytes ", j
++);
1039 printf("%d:tx-pkts ", j
++);
1040 printf("%d:tx-drops ", j
++);
1041 printf("%d:tx-errors ", j
++);
1043 printf("%d:context-switches-per-t ", j
++);
1044 printf("%d:mem-free ", j
++);
1045 printf("%d:mem-used ", j
++);
1046 printf("%d:mem-total ", j
++);
1047 printf("%d:swap-free ", j
++);
1048 printf("%d:swap-used ", j
++);
1049 printf("%d:swap-total ", j
++);
1050 printf("%d:procs-total ", j
++);
1051 printf("%d:procs-in-run ", j
++);
1052 printf("%d:procs-in-iow ", j
++);
1054 cpus
= get_number_cpus();
1056 for (i
= 0; i
< cpus
; ++i
) {
1057 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1058 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1059 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1060 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1061 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1063 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1064 printf("%d:cpu%i-net-irqs ", j
++, i
);
1066 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1067 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1068 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1069 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1072 if (iswireless(abs
)) {
1073 printf("%d:wifi-link-qual-per-t ", j
++);
1074 printf("%d:wifi-link-qual ", j
++);
1075 printf("%d:wifi-link-qual-max ", j
++);
1077 printf("%d:wifi-signal-dbm-per-t ", j
++);
1078 printf("%d:wifi-signal-dbm ", j
++);
1082 printf("# data:\n");
1086 static int term_main(const char *ifname
, uint64_t ms_interval
,
1087 unsigned int top_cpus __maybe_unused
)
1092 stats_sample_generic(ifname
, ms_interval
);
1096 term_csv_header(ifname
, &stats_new
, ms_interval
);
1099 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1100 } while (stats_loop
&& !sigint
);
1105 int main(int argc
, char **argv
)
1108 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1109 unsigned int top_cpus
= 10;
1110 uint64_t interval
= 1000;
1111 char *ifname
= NULL
;
1112 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1113 unsigned int top_cpus
) = screen_main
;
1118 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1119 &opt_index
)) != EOF
) {
1128 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1131 interval
= strtoul(optarg
, NULL
, 10);
1134 top_cpus
= strtoul(optarg
, NULL
, 10);
1136 panic("Number of top hitter CPUs must be greater than 0");
1145 func_main
= term_main
;
1151 panic("Option -%c requires an argument!\n",
1154 if (isprint(optopt
))
1155 printf("Unknown option character `0x%X\'!\n", optopt
);
1167 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1169 panic("No networking device given!\n");
1171 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1172 panic("lo is not supported!\n");
1173 if (device_mtu(ifname
) == 0)
1174 panic("This is no networking device!\n");
1176 register_signal(SIGINT
, signal_handler
);
1177 register_signal(SIGHUP
, signal_handler
);
1179 cpus
= get_number_cpus();
1180 top_cpus
= min(top_cpus
, cpus
);
1182 stats_alloc(&stats_old
, cpus
);
1183 stats_alloc(&stats_new
, cpus
);
1184 stats_alloc(&stats_delta
, cpus
);
1186 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1189 ifflags
= enter_promiscuous_mode(ifname
);
1190 ret
= func_main(ifname
, interval
, top_cpus
);
1192 leave_promiscuous_mode(ifname
, ifflags
);