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>
32 int16_t link_qual
, link_qual_max
;
33 int signal_level
/*, noise_level*/;
37 long long unsigned int rx_bytes
, rx_packets
, rx_drops
, rx_errors
;
38 long long unsigned int rx_fifo
, rx_frame
, rx_multi
;
39 long long unsigned int tx_bytes
, tx_packets
, tx_drops
, tx_errors
;
40 long long unsigned int tx_fifo
, tx_colls
, tx_carrier
;
41 uint64_t mem_free
, mem_total
;
42 uint32_t irq_nr
, procs_run
, procs_iow
, cswitch
, forks
;
43 struct wifi_stat wifi
;
45 * Pointer members need to be last in order for stats_zero() to work
48 long long unsigned int *irqs
, *irqs_srx
, *irqs_stx
;
49 uint64_t *cpu_user
, *cpu_sys
, *cpu_nice
, *cpu_idle
, *cpu_iow
;
55 long long unsigned int irqs_rel
, irqs_abs
;
58 static volatile sig_atomic_t sigint
= 0;
59 static struct ifstat stats_old
, stats_new
, stats_delta
;
60 static struct cpu_hit
*cpu_hits
;
61 static int stats_loop
= 0;
62 static WINDOW
*stats_screen
= NULL
;
64 static const char *short_options
= "d:t:n:vhclp";
65 static const struct option long_options
[] = {
66 {"dev", required_argument
, NULL
, 'd'},
67 {"interval", required_argument
, NULL
, 't'},
68 {"num-cpus", required_argument
, NULL
, 'n'},
69 {"promisc", no_argument
, NULL
, 'p'},
70 {"csv", no_argument
, NULL
, 'c'},
71 {"loop", no_argument
, NULL
, 'l'},
72 {"version", no_argument
, NULL
, 'v'},
73 {"help", no_argument
, NULL
, 'h'},
77 static void signal_handler(int number
)
89 static inline int iswireless(const struct ifstat
*stats
)
91 return stats
->wifi
.bitrate
> 0;
94 static void __noreturn
help(void)
96 printf("\nifpps %s, top-like kernel networking and system statistics\n",
98 puts("http://www.netsniff-ng.org\n\n"
99 "Usage: ifpps [options] || ifpps <netdev>\n"
101 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
102 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
103 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
104 " in ncurses mode (default 10)\n"
105 " -p|--promisc Promiscuous mode\n"
106 " -c|--csv Output to terminal as Gnuplot-ready data\n"
107 " -l|--loop Continuous CSV output\n"
108 " -v|--version Print version and exit\n"
109 " -h|--help Print this help and exit\n\n"
113 " ifpps -lpcd wlan0 > plot.dat\n\n"
115 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
116 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
117 "Please report bugs to <bugs@netsniff-ng.org>\n"
118 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
119 "Swiss federal institute of technology (ETH Zurich)\n"
120 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
121 "License: GNU GPL version 2.0\n"
122 "This is free software: you are free to change and redistribute it.\n"
123 "There is NO WARRANTY, to the extent permitted by law.\n");
127 static void __noreturn
version(void)
129 printf("\nifpps %s, top-like kernel networking and system statistics\n",
131 puts("http://www.netsniff-ng.org\n\n"
132 "Please report bugs to <bugs@netsniff-ng.org>\n"
133 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
134 "Swiss federal institute of technology (ETH Zurich)\n"
135 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
136 "License: GNU GPL version 2.0\n"
137 "This is free software: you are free to change and redistribute it.\n"
138 "There is NO WARRANTY, to the extent permitted by law.\n");
142 static inline int padding_from_num(int n
)
146 while ((n
/= 10) > 0);
150 #define STATS_ALLOC1(member) \
151 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
153 static void stats_alloc(struct ifstat
*stats
, int cpus
)
156 STATS_ALLOC1(irqs_srx
);
157 STATS_ALLOC1(irqs_stx
);
159 STATS_ALLOC1(cpu_user
);
160 STATS_ALLOC1(cpu_sys
);
161 STATS_ALLOC1(cpu_nice
);
162 STATS_ALLOC1(cpu_idle
);
163 STATS_ALLOC1(cpu_iow
);
166 #define STATS_ZERO1(member) \
167 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
169 static void stats_zero(struct ifstat
*stats
, int cpus
)
171 /* Only clear the non-pointer members */
172 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
175 STATS_ZERO1(irqs_srx
);
176 STATS_ZERO1(irqs_stx
);
178 STATS_ZERO1(cpu_user
);
179 STATS_ZERO1(cpu_sys
);
180 STATS_ZERO1(cpu_nice
);
181 STATS_ZERO1(cpu_idle
);
182 STATS_ZERO1(cpu_iow
);
185 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
191 fp
= fopen("/proc/net/dev", "r");
193 panic("Cannot open /proc/net/dev!\n");
195 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
196 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
198 memset(buff
, 0, sizeof(buff
));
200 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
201 buff
[sizeof(buff
) -1] = 0;
203 if (strstr(buff
, ifname
) == NULL
)
206 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
207 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
208 &stats
->rx_bytes
, &stats
->rx_packets
,
209 &stats
->rx_errors
, &stats
->rx_drops
,
210 &stats
->rx_fifo
, &stats
->rx_frame
,
211 &stats
->rx_multi
, &stats
->tx_bytes
,
212 &stats
->tx_packets
, &stats
->tx_errors
,
213 &stats
->tx_drops
, &stats
->tx_fifo
,
214 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
219 memset(buff
, 0, sizeof(buff
));
226 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
228 int ret
= -EINVAL
, i
, cpus
, try = 0;
232 struct ethtool_drvinfo drvinf
;
235 fp
= fopen("/proc/interrupts", "r");
237 panic("Cannot open /proc/interrupts!\n");
239 cpus
= get_number_cpus();
240 buff_len
= cpus
* 128;
241 buff
= xmalloc(buff_len
);
243 fseek(fp
, 0, SEEK_SET
);
244 memset(buff
, 0, buff_len
);
246 while (fgets(buff
, buff_len
, fp
) != NULL
) {
247 buff
[buff_len
- 1] = 0;
250 if (strstr(buff
, ifname
) == NULL
)
253 /* XXX: remove this one here */
254 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
255 bug_on(stats
->irq_nr
== 0);
259 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
261 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
263 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
270 memset(buff
, 0, buff_len
);
273 if (ret
== -EINVAL
&& try == 0) {
274 memset(&drvinf
, 0, sizeof(drvinf
));
275 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
278 ifname
= drvinf
.driver
;
289 static int stats_proc_softirqs(struct ifstat
*stats
)
300 fp
= fopen("/proc/softirqs", "r");
302 panic("Cannot open /proc/softirqs!\n");
304 cpus
= get_number_cpus();
305 buff_len
= cpus
* 128;
306 buff
= xmalloc(buff_len
);
308 memset(buff
, 0, buff_len
);
310 while (fgets(buff
, buff_len
, fp
) != NULL
) {
311 buff
[buff_len
- 1] = 0;
313 if ((ptr
= strstr(buff
, "NET_TX:")))
314 net_type
= softirqs_net_tx
;
315 else if ((ptr
= strstr(buff
, "NET_RX:")))
316 net_type
= softirqs_net_rx
;
320 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
322 case softirqs_net_tx
:
323 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
325 case softirqs_net_rx
:
326 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
331 memset(buff
, 0, buff_len
);
339 static int stats_proc_memory(struct ifstat
*stats
)
341 char *ptr
, buff
[256];
344 fp
= fopen("/proc/meminfo", "r");
346 panic("Cannot open /proc/meminfo!\n");
348 memset(buff
, 0, sizeof(buff
));
350 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
351 buff
[sizeof(buff
) - 1] = 0;
353 if ((ptr
= strstr(buff
, "MemTotal:"))) {
354 ptr
+= strlen("MemTotal:");
355 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
356 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
357 ptr
+= strlen("MemFree:");
358 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
361 memset(buff
, 0, sizeof(buff
));
368 static int stats_proc_system(struct ifstat
*stats
)
371 char *ptr
, buff
[256];
374 fp
= fopen("/proc/stat", "r");
376 panic("Cannot open /proc/stat!\n");
378 cpus
= get_number_cpus();
380 memset(buff
, 0, sizeof(buff
));
382 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
383 buff
[sizeof(buff
) - 1] = 0;
385 if ((ptr
= strstr(buff
, "cpu"))) {
386 ptr
+= strlen("cpu");
390 cpu
= strtol(ptr
, &ptr
, 10);
393 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
394 &stats
->cpu_user
[cpu
],
395 &stats
->cpu_nice
[cpu
],
396 &stats
->cpu_sys
[cpu
],
397 &stats
->cpu_idle
[cpu
],
398 &stats
->cpu_iow
[cpu
]) != 5)
400 } else if ((ptr
= strstr(buff
, "ctxt"))) {
401 ptr
+= strlen("ctxt");
402 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
403 } else if ((ptr
= strstr(buff
, "processes"))) {
404 ptr
+= strlen("processes");
405 stats
->forks
= strtoul(ptr
, &ptr
, 10);
406 } else if ((ptr
= strstr(buff
, "procs_running"))) {
407 ptr
+= strlen("procs_running");
408 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
409 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
410 ptr
+= strlen("procs_blocked");
411 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
414 memset(buff
, 0, sizeof(buff
));
421 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
426 return dbm_val
- 0x100;
429 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
432 struct iw_statistics ws
;
434 ret
= wireless_sigqual(ifname
, &ws
);
436 stats
->wifi
.bitrate
= 0;
440 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
442 stats
->wifi
.signal_level
=
443 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
445 stats
->wifi
.link_qual
= ws
.qual
.qual
;
446 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
451 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
452 #define DIFF(member) do { \
453 if (sizeof(diff->member) != sizeof(new->member) || \
454 sizeof(diff->member) != sizeof(old->member)) \
456 bug_on((new->member - old->member) > (new->member)); \
460 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
481 DIFF1(wifi
.signal_level
);
482 DIFF1(wifi
.link_qual
);
486 cpus
= get_number_cpus();
488 for (i
= 0; i
< cpus
; ++i
) {
501 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
503 if (stats_proc_net_dev(ifname
, stats
) < 0)
504 panic("Cannot fetch device stats!\n");
505 if (stats_proc_softirqs(stats
) < 0)
506 panic("Cannot fetch software interrupts!\n");
507 if (stats_proc_memory(stats
) < 0)
508 panic("Cannot fetch memory stats!\n");
509 if (stats_proc_system(stats
) < 0)
510 panic("Cannot fetch system stats!\n");
512 stats_proc_interrupts((char *) ifname
, stats
);
514 stats_wireless(ifname
, stats
);
517 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
519 int cpus
= get_number_cpus();
521 stats_zero(&stats_old
, cpus
);
522 stats_zero(&stats_new
, cpus
);
523 stats_zero(&stats_delta
, cpus
);
525 stats_fetch(ifname
, &stats_old
);
526 usleep(ms_interval
* 1000);
527 stats_fetch(ifname
, &stats_new
);
529 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
532 static int cmp_hits(const void *p1
, const void *p2
)
534 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
537 * We want the hits sorted in descending order, thus reverse the return
540 if (h1
->hit
== h2
->hit
)
542 else if (h1
->hit
< h2
->hit
)
548 static int cmp_irqs_rel(const void *p1
, const void *p2
)
550 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
553 * We want the hits sorted in descending order, thus reverse the return
556 if (h1
->irqs_rel
== h2
->irqs_rel
)
558 else if (h1
->irqs_rel
< h2
->irqs_rel
)
564 static int cmp_irqs_abs(const void *p1
, const void *p2
)
566 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
569 * We want the hits sorted in descending order, thus reverse the return
572 if (h1
->irqs_abs
== h2
->irqs_abs
)
574 else if (h1
->irqs_abs
< h2
->irqs_abs
)
580 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
585 for (i
= 0; i
< top_cpus
; ++i
) {
587 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
588 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
589 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
593 static void screen_init(WINDOW
**screen
)
595 (*screen
) = initscr();
600 nodelay((*screen
), TRUE
);
602 keypad(stdscr
, TRUE
);
608 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
609 uint64_t ms_interval
, unsigned int top_cpus
)
613 struct ethtool_drvinfo drvinf
;
614 u32 rate
= device_bitrate(ifname
);
615 int link
= ethtool_link(ifname
);
616 unsigned int cpus
= get_number_cpus();
618 memset(&drvinf
, 0, sizeof(drvinf
));
619 ethtool_drvinf(ifname
, &drvinf
);
621 memset(buff
, 0, sizeof(buff
));
623 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
625 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
626 link
== 0 ? "no" : "yes");
628 mvwprintw(screen
, (*voff
)++, 2,
629 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u%s/%u"
631 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
,
632 top_cpus
> 0 && top_cpus
< cpus
? "+1" : "", cpus
);
635 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
640 mvwprintw(screen
, (*voff
)++, 0,
641 " rx: %16.3llf MiB/t "
645 ((long double) rel
->rx_bytes
) / (1LLU << 20),
646 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
648 mvwprintw(screen
, (*voff
)++, 0,
649 " tx: %16.3llf MiB/t "
653 ((long double) rel
->tx_bytes
) / (1LLU << 20),
654 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
659 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
662 mvwprintw(screen
, (*voff
)++, 2,
667 ((long double) abs
->rx_bytes
) / (1LLU << 20),
668 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
670 mvwprintw(screen
, (*voff
)++, 2,
675 ((long double) abs
->tx_bytes
) / (1LLU << 20),
676 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
679 static void screen_sys_mem(WINDOW
*screen
, const struct ifstat
*rel
,
680 const struct ifstat
*abs
, int *voff
)
682 mvwprintw(screen
, (*voff
)++, 2,
688 (100.0 * (abs
->mem_total
- abs
->mem_free
)) / abs
->mem_total
,
689 abs
->procs_run
, abs
->procs_iow
);
692 static void screen_percpu_states_one(WINDOW
*screen
, const struct ifstat
*rel
,
693 int *voff
, unsigned int idx
, char *tag
)
695 int max_padd
= padding_from_num(get_number_cpus());
696 uint64_t all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
697 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
699 mvwprintw(screen
, (*voff
)++, 2,
700 "cpu%*d%s:%s %13.1lf%% usr/t "
703 "%11.1lf%% iow/t ", max_padd
, idx
,
704 tag
, strlen(tag
) == 0 ? " " : "",
705 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
706 100.0 * rel
->cpu_sys
[idx
] / all
,
707 100.0 * rel
->cpu_idle
[idx
] / all
,
708 100.0 * rel
->cpu_iow
[idx
] / all
);
711 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
712 int top_cpus
, int *voff
)
715 int cpus
= get_number_cpus();
720 /* Display top hitter */
721 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
723 /* Make sure we don't display the min. hitter twice */
724 if (top_cpus
== cpus
)
727 for (i
= 1; i
< top_cpus
; ++i
)
728 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "");
730 /* Display minimum hitter */
732 screen_percpu_states_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
735 static void screen_percpu_irqs_rel_one(WINDOW
*screen
, const struct ifstat
*rel
,
736 int *voff
, unsigned int idx
, char *tag
)
738 int max_padd
= padding_from_num(get_number_cpus());
740 mvwprintw(screen
, (*voff
)++, 2,
741 "cpu%*d%s:%s %14llu irqs/t "
743 "%15llu sirq tx/t ", max_padd
, idx
,
744 tag
, strlen(tag
) == 0 ? " " : "",
750 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
751 int top_cpus
, int *voff
)
754 int cpus
= get_number_cpus();
756 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[0].idx
, "+");
758 if (top_cpus
== cpus
)
761 for (i
= 1; i
< top_cpus
; ++i
)
762 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[i
].idx
, "");
765 screen_percpu_irqs_rel_one(screen
, rel
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
768 static void screen_percpu_irqs_abs_one(WINDOW
*screen
, const struct ifstat
*abs
,
769 int *voff
, unsigned int idx
, char *tag
)
771 int max_padd
= padding_from_num(get_number_cpus());
773 mvwprintw(screen
, (*voff
)++, 2,
774 "cpu%*d%s:%s %14llu irqs", max_padd
, idx
,
775 tag
, strlen(tag
) == 0 ? " " : "",
779 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
780 int top_cpus
, int *voff
)
783 int cpus
= get_number_cpus();
785 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[0].idx
, "+");
787 if (top_cpus
== cpus
)
790 for (i
= 1; i
< top_cpus
; ++i
)
791 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[i
].idx
, "");
794 screen_percpu_irqs_abs_one(screen
, abs
, voff
, cpu_hits
[cpus
- 1].idx
, "-");
797 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
798 const struct ifstat
*abs
, int *voff
)
800 if (iswireless(abs
)) {
801 mvwprintw(screen
, (*voff
)++, 2,
802 "linkqual: %7d/%d (%d/t) ",
804 abs
->wifi
.link_qual_max
,
805 rel
->wifi
.link_qual
);
807 mvwprintw(screen
, (*voff
)++, 2,
808 "signal: %8d dBm (%d dBm/t) ",
809 abs
->wifi
.signal_level
,
810 rel
->wifi
.signal_level
);
814 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
815 const struct ifstat
*abs
, int *first
, uint64_t ms_interval
,
816 unsigned int top_cpus
)
818 int cpus
, top
, voff
= 1, cvoff
= 2;
822 cpus
= get_number_cpus();
823 top
= min(cpus
, top_cpus
);
825 stats_top(rel
, abs
, cpus
);
827 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
829 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
832 screen_net_dev_rel(screen
, rel
, &voff
);
835 screen_net_dev_abs(screen
, abs
, &voff
);
838 screen_sys_mem(screen
, rel
, abs
, &voff
);
841 screen_percpu_states(screen
, rel
, top
, &voff
);
843 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
846 screen_percpu_irqs_rel(screen
, rel
, top
, &voff
);
848 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
851 screen_percpu_irqs_abs(screen
, abs
, top
, &voff
);
854 screen_wireless(screen
, rel
, abs
, &voff
);
857 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
860 mvwprintw(screen
, cvoff
, 2, " ");
867 static void screen_end(void)
872 static int screen_main(const char *ifname
, uint64_t ms_interval
,
873 unsigned int top_cpus
)
877 screen_init(&stats_screen
);
881 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
884 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
,
885 &first
, ms_interval
, top_cpus
);
887 stats_sample_generic(ifname
, ms_interval
);
895 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
896 const struct ifstat
*abs
, uint64_t ms_interval
)
900 printf("%ld ", time(0));
902 printf("%llu ", rel
->rx_bytes
);
903 printf("%llu ", rel
->rx_packets
);
904 printf("%llu ", rel
->rx_drops
);
905 printf("%llu ", rel
->rx_errors
);
907 printf("%llu ", abs
->rx_bytes
);
908 printf("%llu ", abs
->rx_packets
);
909 printf("%llu ", abs
->rx_drops
);
910 printf("%llu ", abs
->rx_errors
);
912 printf("%llu ", rel
->tx_bytes
);
913 printf("%llu ", rel
->tx_packets
);
914 printf("%llu ", rel
->tx_drops
);
915 printf("%llu ", rel
->tx_errors
);
917 printf("%llu ", abs
->tx_bytes
);
918 printf("%llu ", abs
->tx_packets
);
919 printf("%llu ", abs
->tx_drops
);
920 printf("%llu ", abs
->tx_errors
);
922 printf("%u ", rel
->cswitch
);
923 printf("%lu ", abs
->mem_free
);
924 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
925 printf("%lu ", abs
->mem_total
);
926 printf("%u ", abs
->procs_run
);
927 printf("%u ", abs
->procs_iow
);
929 cpus
= get_number_cpus();
931 for (i
= 0; i
< cpus
; ++i
) {
932 printf("%lu ", rel
->cpu_user
[i
]);
933 printf("%lu ", rel
->cpu_nice
[i
]);
934 printf("%lu ", rel
->cpu_sys
[i
]);
935 printf("%lu ", rel
->cpu_idle
[i
]);
936 printf("%lu ", rel
->cpu_iow
[i
]);
938 printf("%llu ", rel
->irqs
[i
]);
939 printf("%llu ", abs
->irqs
[i
]);
941 printf("%llu ", rel
->irqs_srx
[i
]);
942 printf("%llu ", abs
->irqs_srx
[i
]);
944 printf("%llu ", rel
->irqs_stx
[i
]);
945 printf("%llu ", abs
->irqs_stx
[i
]);
948 if (iswireless(abs
)) {
949 printf("%u ", rel
->wifi
.link_qual
);
950 printf("%u ", abs
->wifi
.link_qual
);
951 printf("%u ", abs
->wifi
.link_qual_max
);
953 printf("%d ", rel
->wifi
.signal_level
);
954 printf("%d ", abs
->wifi
.signal_level
);
961 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
962 uint64_t ms_interval
)
966 printf("# gnuplot dump (#col:description)\n");
967 printf("# networking interface: %s\n", ifname
);
968 printf("# sampling interval (t): %lu ms\n", ms_interval
);
969 printf("# %d:unixtime ", j
++);
971 printf("%d:rx-bytes-per-t ", j
++);
972 printf("%d:rx-pkts-per-t ", j
++);
973 printf("%d:rx-drops-per-t ", j
++);
974 printf("%d:rx-errors-per-t ", j
++);
976 printf("%d:rx-bytes ", j
++);
977 printf("%d:rx-pkts ", j
++);
978 printf("%d:rx-drops ", j
++);
979 printf("%d:rx-errors ", j
++);
981 printf("%d:tx-bytes-per-t ", j
++);
982 printf("%d:tx-pkts-per-t ", j
++);
983 printf("%d:tx-drops-per-t ", j
++);
984 printf("%d:tx-errors-per-t ", j
++);
986 printf("%d:tx-bytes ", j
++);
987 printf("%d:tx-pkts ", j
++);
988 printf("%d:tx-drops ", j
++);
989 printf("%d:tx-errors ", j
++);
991 printf("%d:context-switches-per-t ", j
++);
992 printf("%d:mem-free ", j
++);
993 printf("%d:mem-used ", j
++);
994 printf("%d:mem-total ", j
++);
995 printf("%d:procs-in-run ", j
++);
996 printf("%d:procs-in-iow ", j
++);
998 cpus
= get_number_cpus();
1000 for (i
= 0; i
< cpus
; ++i
) {
1001 printf("%d:cpu%i-usr-per-t ", j
++, i
);
1002 printf("%d:cpu%i-nice-per-t ", j
++, i
);
1003 printf("%d:cpu%i-sys-per-t ", j
++, i
);
1004 printf("%d:cpu%i-idle-per-t ", j
++, i
);
1005 printf("%d:cpu%i-iow-per-t ", j
++, i
);
1007 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
1008 printf("%d:cpu%i-net-irqs ", j
++, i
);
1010 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
1011 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
1012 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
1013 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
1016 if (iswireless(abs
)) {
1017 printf("%d:wifi-link-qual-per-t ", j
++);
1018 printf("%d:wifi-link-qual ", j
++);
1019 printf("%d:wifi-link-qual-max ", j
++);
1021 printf("%d:wifi-signal-dbm-per-t ", j
++);
1022 printf("%d:wifi-signal-dbm ", j
++);
1026 printf("# data:\n");
1030 static int term_main(const char *ifname
, uint64_t ms_interval
,
1031 unsigned int top_cpus __maybe_unused
)
1036 stats_sample_generic(ifname
, ms_interval
);
1040 term_csv_header(ifname
, &stats_new
, ms_interval
);
1043 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1044 } while (stats_loop
&& !sigint
);
1049 int main(int argc
, char **argv
)
1052 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1053 unsigned int top_cpus
= 10;
1054 uint64_t interval
= 1000;
1055 char *ifname
= NULL
;
1056 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1057 unsigned int top_cpus
) = screen_main
;
1062 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1063 &opt_index
)) != EOF
) {
1072 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1075 interval
= strtoul(optarg
, NULL
, 10);
1078 top_cpus
= strtoul(optarg
, NULL
, 10);
1080 panic("Number of top hitter CPUs must be greater than 0");
1089 func_main
= term_main
;
1095 panic("Option -%c requires an argument!\n",
1098 if (isprint(optopt
))
1099 printf("Unknown option character `0x%X\'!\n", optopt
);
1111 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1113 panic("No networking device given!\n");
1115 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1116 panic("lo is not supported!\n");
1117 if (device_mtu(ifname
) == 0)
1118 panic("This is no networking device!\n");
1120 register_signal(SIGINT
, signal_handler
);
1121 register_signal(SIGHUP
, signal_handler
);
1123 cpus
= get_number_cpus();
1124 top_cpus
= min(top_cpus
, cpus
);
1126 stats_alloc(&stats_old
, cpus
);
1127 stats_alloc(&stats_new
, cpus
);
1128 stats_alloc(&stats_delta
, cpus
);
1130 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1133 ifflags
= enter_promiscuous_mode(ifname
);
1134 ret
= func_main(ifname
, interval
, top_cpus
);
1136 leave_promiscuous_mode(ifname
, ifflags
);