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>
29 int16_t link_qual
, link_qual_max
;
30 int signal_level
/*, noise_level*/;
34 long long unsigned int rx_bytes
, rx_packets
, rx_drops
, rx_errors
;
35 long long unsigned int rx_fifo
, rx_frame
, rx_multi
;
36 long long unsigned int tx_bytes
, tx_packets
, tx_drops
, tx_errors
;
37 long long unsigned int tx_fifo
, tx_colls
, tx_carrier
;
38 uint64_t mem_free
, mem_total
;
39 uint32_t irq_nr
, procs_run
, procs_iow
, cswitch
, forks
;
40 struct wifi_stat wifi
;
42 * Pointer members need to be last in order for stats_zero() to work
45 long long unsigned int *irqs
, *irqs_srx
, *irqs_stx
;
46 uint64_t *cpu_user
, *cpu_sys
, *cpu_nice
, *cpu_idle
, *cpu_iow
;
52 long long unsigned int irqs_rel
, irqs_abs
;
55 static volatile sig_atomic_t sigint
= 0;
56 static struct ifstat stats_old
, stats_new
, stats_delta
;
57 static struct cpu_hit
*cpu_hits
;
58 static int stats_loop
= 0;
59 static WINDOW
*stats_screen
= NULL
;
61 static const char *short_options
= "d:t:n:vhclp";
62 static const struct option long_options
[] = {
63 {"dev", required_argument
, NULL
, 'd'},
64 {"interval", required_argument
, NULL
, 't'},
65 {"num-cpus", required_argument
, NULL
, 'n'},
66 {"promisc", no_argument
, NULL
, 'p'},
67 {"csv", no_argument
, NULL
, 'c'},
68 {"loop", no_argument
, NULL
, 'l'},
69 {"version", no_argument
, NULL
, 'v'},
70 {"help", no_argument
, NULL
, 'h'},
74 static void signal_handler(int number
)
86 static inline int iswireless(const struct ifstat
*stats
)
88 return stats
->wifi
.bitrate
> 0;
91 static void __noreturn
help(void)
93 printf("\nifpps %s, top-like kernel networking and system statistics\n",
95 puts("http://www.netsniff-ng.org\n\n"
96 "Usage: ifpps [options] || ifpps <netdev>\n"
98 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
99 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
100 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
101 " in ncurses mode (default 10)\n"
102 " -p|--promisc Promiscuous mode\n"
103 " -c|--csv Output to terminal as Gnuplot-ready data\n"
104 " -l|--loop Continuous CSV output\n"
105 " -v|--version Print version and exit\n"
106 " -h|--help Print this help and exit\n\n"
110 " ifpps -lpcd wlan0 > plot.dat\n\n"
112 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
113 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
114 "Please report bugs to <bugs@netsniff-ng.org>\n"
115 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
116 "Swiss federal institute of technology (ETH Zurich)\n"
117 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
118 "License: GNU GPL version 2.0\n"
119 "This is free software: you are free to change and redistribute it.\n"
120 "There is NO WARRANTY, to the extent permitted by law.\n");
124 static void __noreturn
version(void)
126 printf("\nifpps %s, top-like kernel networking and system statistics\n",
128 puts("http://www.netsniff-ng.org\n\n"
129 "Please report bugs to <bugs@netsniff-ng.org>\n"
130 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
131 "Swiss federal institute of technology (ETH Zurich)\n"
132 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
133 "License: GNU GPL version 2.0\n"
134 "This is free software: you are free to change and redistribute it.\n"
135 "There is NO WARRANTY, to the extent permitted by law.\n");
139 static inline int padding_from_num(int n
)
143 while ((n
/= 10) > 0);
147 #define STATS_ALLOC1(member) \
148 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
150 static void stats_alloc(struct ifstat
*stats
, int cpus
)
153 STATS_ALLOC1(irqs_srx
);
154 STATS_ALLOC1(irqs_stx
);
156 STATS_ALLOC1(cpu_user
);
157 STATS_ALLOC1(cpu_sys
);
158 STATS_ALLOC1(cpu_nice
);
159 STATS_ALLOC1(cpu_idle
);
160 STATS_ALLOC1(cpu_iow
);
163 #define STATS_ZERO1(member) \
164 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
166 static void stats_zero(struct ifstat
*stats
, int cpus
)
168 /* Only clear the non-pointer members */
169 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
172 STATS_ZERO1(irqs_srx
);
173 STATS_ZERO1(irqs_stx
);
175 STATS_ZERO1(cpu_user
);
176 STATS_ZERO1(cpu_sys
);
177 STATS_ZERO1(cpu_nice
);
178 STATS_ZERO1(cpu_idle
);
179 STATS_ZERO1(cpu_iow
);
182 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
188 fp
= fopen("/proc/net/dev", "r");
190 panic("Cannot open /proc/net/dev!\n");
192 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
193 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
195 memset(buff
, 0, sizeof(buff
));
197 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
198 buff
[sizeof(buff
) -1] = 0;
200 if (strstr(buff
, ifname
) == NULL
)
203 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
204 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
205 &stats
->rx_bytes
, &stats
->rx_packets
,
206 &stats
->rx_errors
, &stats
->rx_drops
,
207 &stats
->rx_fifo
, &stats
->rx_frame
,
208 &stats
->rx_multi
, &stats
->tx_bytes
,
209 &stats
->tx_packets
, &stats
->tx_errors
,
210 &stats
->tx_drops
, &stats
->tx_fifo
,
211 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
216 memset(buff
, 0, sizeof(buff
));
223 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
225 int ret
= -EINVAL
, i
, cpus
, try = 0;
229 struct ethtool_drvinfo drvinf
;
232 fp
= fopen("/proc/interrupts", "r");
234 panic("Cannot open /proc/interrupts!\n");
236 cpus
= get_number_cpus();
237 buff_len
= cpus
* 128;
238 buff
= xmalloc(buff_len
);
240 fseek(fp
, 0, SEEK_SET
);
241 memset(buff
, 0, buff_len
);
243 while (fgets(buff
, buff_len
, fp
) != NULL
) {
244 buff
[buff_len
- 1] = 0;
247 if (strstr(buff
, ifname
) == NULL
)
250 /* XXX: remove this one here */
251 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
252 bug_on(stats
->irq_nr
== 0);
256 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
258 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
260 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
267 memset(buff
, 0, buff_len
);
270 if (ret
== -EINVAL
&& try == 0) {
271 memset(&drvinf
, 0, sizeof(drvinf
));
272 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
275 ifname
= drvinf
.driver
;
286 static int stats_proc_softirqs(struct ifstat
*stats
)
297 fp
= fopen("/proc/softirqs", "r");
299 panic("Cannot open /proc/softirqs!\n");
301 cpus
= get_number_cpus();
302 buff_len
= cpus
* 128;
303 buff
= xmalloc(buff_len
);
305 memset(buff
, 0, buff_len
);
307 while (fgets(buff
, buff_len
, fp
) != NULL
) {
308 buff
[buff_len
- 1] = 0;
310 if ((ptr
= strstr(buff
, "NET_TX:")))
311 net_type
= softirqs_net_tx
;
312 else if ((ptr
= strstr(buff
, "NET_RX:")))
313 net_type
= softirqs_net_rx
;
317 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
319 case softirqs_net_tx
:
320 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
322 case softirqs_net_rx
:
323 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
328 memset(buff
, 0, buff_len
);
336 static int stats_proc_memory(struct ifstat
*stats
)
338 char *ptr
, buff
[256];
341 fp
= fopen("/proc/meminfo", "r");
343 panic("Cannot open /proc/meminfo!\n");
345 memset(buff
, 0, sizeof(buff
));
347 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
348 buff
[sizeof(buff
) - 1] = 0;
350 if ((ptr
= strstr(buff
, "MemTotal:"))) {
351 ptr
+= strlen("MemTotal:");
352 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
353 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
354 ptr
+= strlen("MemFree:");
355 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
358 memset(buff
, 0, sizeof(buff
));
365 static int stats_proc_system(struct ifstat
*stats
)
368 char *ptr
, buff
[256];
371 fp
= fopen("/proc/stat", "r");
373 panic("Cannot open /proc/stat!\n");
375 cpus
= get_number_cpus();
377 memset(buff
, 0, sizeof(buff
));
379 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
380 buff
[sizeof(buff
) - 1] = 0;
382 if ((ptr
= strstr(buff
, "cpu"))) {
383 ptr
+= strlen("cpu");
387 cpu
= strtol(ptr
, &ptr
, 10);
390 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
391 &stats
->cpu_user
[cpu
],
392 &stats
->cpu_nice
[cpu
],
393 &stats
->cpu_sys
[cpu
],
394 &stats
->cpu_idle
[cpu
],
395 &stats
->cpu_iow
[cpu
]) != 5)
397 } else if ((ptr
= strstr(buff
, "ctxt"))) {
398 ptr
+= strlen("ctxt");
399 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
400 } else if ((ptr
= strstr(buff
, "processes"))) {
401 ptr
+= strlen("processes");
402 stats
->forks
= strtoul(ptr
, &ptr
, 10);
403 } else if ((ptr
= strstr(buff
, "procs_running"))) {
404 ptr
+= strlen("procs_running");
405 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
406 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
407 ptr
+= strlen("procs_blocked");
408 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
411 memset(buff
, 0, sizeof(buff
));
418 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
423 return dbm_val
- 0x100;
426 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
429 struct iw_statistics ws
;
431 ret
= wireless_sigqual(ifname
, &ws
);
433 stats
->wifi
.bitrate
= 0;
437 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
439 stats
->wifi
.signal_level
=
440 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
442 stats
->wifi
.link_qual
= ws
.qual
.qual
;
443 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
448 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
449 #define DIFF(member) do { \
450 if (sizeof(diff->member) != sizeof(new->member) || \
451 sizeof(diff->member) != sizeof(old->member)) \
453 bug_on((new->member - old->member) > (new->member)); \
457 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
482 DIFF1(wifi
.signal_level
);
483 DIFF1(wifi
.link_qual
);
488 cpus
= get_number_cpus();
490 for (i
= 0; i
< cpus
; ++i
) {
503 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
505 if (stats_proc_net_dev(ifname
, stats
) < 0)
506 panic("Cannot fetch device stats!\n");
507 if (stats_proc_softirqs(stats
) < 0)
508 panic("Cannot fetch software interrupts!\n");
509 if (stats_proc_memory(stats
) < 0)
510 panic("Cannot fetch memory stats!\n");
511 if (stats_proc_system(stats
) < 0)
512 panic("Cannot fetch system stats!\n");
514 stats_proc_interrupts((char *) ifname
, stats
);
516 stats_wireless(ifname
, stats
);
519 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
521 int cpus
= get_number_cpus();
523 stats_zero(&stats_old
, cpus
);
524 stats_zero(&stats_new
, cpus
);
525 stats_zero(&stats_delta
, cpus
);
527 stats_fetch(ifname
, &stats_old
);
528 usleep(ms_interval
* 1000);
529 stats_fetch(ifname
, &stats_new
);
531 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
534 static int cmp_hits(const void *p1
, const void *p2
)
536 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
539 * We want the hits sorted in descending order, thus reverse the return
542 if (h1
->hit
== h2
->hit
)
544 else if (h1
->hit
< h2
->hit
)
550 static int cmp_irqs_rel(const void *p1
, const void *p2
)
552 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
555 * We want the hits sorted in descending order, thus reverse the return
558 if (h1
->irqs_rel
== h2
->irqs_rel
)
560 else if (h1
->irqs_rel
< h2
->irqs_rel
)
566 static int cmp_irqs_abs(const void *p1
, const void *p2
)
568 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
571 * We want the hits sorted in descending order, thus reverse the return
574 if (h1
->irqs_abs
== h2
->irqs_abs
)
576 else if (h1
->irqs_abs
< h2
->irqs_abs
)
582 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
587 for (i
= 0; i
< top_cpus
; ++i
) {
589 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
590 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
591 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
595 static void screen_init(WINDOW
**screen
)
597 (*screen
) = initscr();
602 nodelay((*screen
), TRUE
);
604 keypad(stdscr
, TRUE
);
610 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
611 uint64_t ms_interval
, unsigned int top_cpus
)
615 struct ethtool_drvinfo drvinf
;
616 u32 rate
= device_bitrate(ifname
);
617 int link
= ethtool_link(ifname
);
619 memset(&drvinf
, 0, sizeof(drvinf
));
620 ethtool_drvinf(ifname
, &drvinf
);
622 memset(buff
, 0, sizeof(buff
));
624 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
626 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
627 link
== 0 ? "no" : "yes");
629 mvwprintw(screen
, (*voff
)++, 2,
630 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u/%u"
632 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
, get_number_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(WINDOW
*screen
, const struct ifstat
*rel
,
693 int top_cpus
, int *voff
)
697 int max_padd
= padding_from_num(get_number_cpus());
699 for (i
= 0; i
< top_cpus
; ++i
) {
700 unsigned int idx
= cpu_hits
[i
].idx
;
702 all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
703 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
705 mvwprintw(screen
, (*voff
)++, 2,
706 "cpu%*d: %13.1lf%% usr/t "
709 "%11.1lf%% iow/t ", max_padd
, idx
,
710 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
711 100.0 * rel
->cpu_sys
[idx
] / all
,
712 100.0 * rel
->cpu_idle
[idx
] / all
,
713 100.0 * rel
->cpu_iow
[idx
] / all
);
717 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
718 int top_cpus
, int *voff
)
721 int max_padd
= padding_from_num(get_number_cpus());
723 for (i
= 0; i
< top_cpus
; ++i
) {
724 unsigned int idx
= cpu_hits
[i
].idx
;
726 mvwprintw(screen
, (*voff
)++, 2,
727 "cpu%*d: %14llu irqs/t "
729 "%15llu sirq tx/t ", max_padd
, idx
,
736 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
737 int top_cpus
, int *voff
)
740 int max_padd
= padding_from_num(get_number_cpus());
742 for (i
= 0; i
< top_cpus
; ++i
) {
743 unsigned int idx
= cpu_hits
[i
].idx
;
745 mvwprintw(screen
, (*voff
)++, 2,
746 "cpu%*d: %14llu irqs", max_padd
, idx
,
751 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
752 const struct ifstat
*abs
, int *voff
)
754 if (iswireless(abs
)) {
755 mvwprintw(screen
, (*voff
)++, 2,
756 "linkqual: %7d/%d (%d/t) ",
758 abs
->wifi
.link_qual_max
,
759 rel
->wifi
.link_qual
);
761 mvwprintw(screen
, (*voff
)++, 2,
762 "signal: %8d dBm (%d dBm/t) ",
763 abs
->wifi
.signal_level
,
764 rel
->wifi
.signal_level
);
768 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
769 const struct ifstat
*abs
, int *first
, uint64_t ms_interval
,
770 unsigned int top_cpus
)
772 int cpus
, top
, voff
= 1, cvoff
= 2;
776 cpus
= get_number_cpus();
777 top
= min(cpus
, top_cpus
);
779 stats_top(rel
, abs
, cpus
);
781 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
783 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
786 screen_net_dev_rel(screen
, rel
, &voff
);
789 screen_net_dev_abs(screen
, abs
, &voff
);
792 screen_sys_mem(screen
, rel
, abs
, &voff
);
795 screen_percpu_states(screen
, rel
, top
, &voff
);
797 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
800 screen_percpu_irqs_rel(screen
, rel
, top
, &voff
);
802 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
805 screen_percpu_irqs_abs(screen
, abs
, top
, &voff
);
808 screen_wireless(screen
, rel
, abs
, &voff
);
811 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
814 mvwprintw(screen
, cvoff
, 2, " ");
821 static void screen_end(void)
826 static int screen_main(const char *ifname
, uint64_t ms_interval
,
827 unsigned int top_cpus
)
831 screen_init(&stats_screen
);
835 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
838 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
,
839 &first
, ms_interval
, top_cpus
);
841 stats_sample_generic(ifname
, ms_interval
);
849 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
850 const struct ifstat
*abs
, uint64_t ms_interval
)
854 printf("%ld ", time(0));
856 printf("%llu ", rel
->rx_bytes
);
857 printf("%llu ", rel
->rx_packets
);
858 printf("%llu ", rel
->rx_drops
);
859 printf("%llu ", rel
->rx_errors
);
861 printf("%llu ", abs
->rx_bytes
);
862 printf("%llu ", abs
->rx_packets
);
863 printf("%llu ", abs
->rx_drops
);
864 printf("%llu ", abs
->rx_errors
);
866 printf("%llu ", rel
->tx_bytes
);
867 printf("%llu ", rel
->tx_packets
);
868 printf("%llu ", rel
->tx_drops
);
869 printf("%llu ", rel
->tx_errors
);
871 printf("%llu ", abs
->tx_bytes
);
872 printf("%llu ", abs
->tx_packets
);
873 printf("%llu ", abs
->tx_drops
);
874 printf("%llu ", abs
->tx_errors
);
876 printf("%u ", rel
->cswitch
);
877 printf("%lu ", abs
->mem_free
);
878 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
879 printf("%lu ", abs
->mem_total
);
880 printf("%u ", abs
->procs_run
);
881 printf("%u ", abs
->procs_iow
);
883 cpus
= get_number_cpus();
885 for (i
= 0; i
< cpus
; ++i
) {
886 printf("%lu ", rel
->cpu_user
[i
]);
887 printf("%lu ", rel
->cpu_nice
[i
]);
888 printf("%lu ", rel
->cpu_sys
[i
]);
889 printf("%lu ", rel
->cpu_idle
[i
]);
890 printf("%lu ", rel
->cpu_iow
[i
]);
892 printf("%llu ", rel
->irqs
[i
]);
893 printf("%llu ", abs
->irqs
[i
]);
895 printf("%llu ", rel
->irqs_srx
[i
]);
896 printf("%llu ", abs
->irqs_srx
[i
]);
898 printf("%llu ", rel
->irqs_stx
[i
]);
899 printf("%llu ", abs
->irqs_stx
[i
]);
902 if (iswireless(abs
)) {
903 printf("%u ", rel
->wifi
.link_qual
);
904 printf("%u ", abs
->wifi
.link_qual
);
905 printf("%u ", abs
->wifi
.link_qual_max
);
907 printf("%d ", rel
->wifi
.signal_level
);
908 printf("%d ", abs
->wifi
.signal_level
);
915 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
916 uint64_t ms_interval
)
920 printf("# gnuplot dump (#col:description)\n");
921 printf("# networking interface: %s\n", ifname
);
922 printf("# sampling interval (t): %lu ms\n", ms_interval
);
923 printf("# %d:unixtime ", j
++);
925 printf("%d:rx-bytes-per-t ", j
++);
926 printf("%d:rx-pkts-per-t ", j
++);
927 printf("%d:rx-drops-per-t ", j
++);
928 printf("%d:rx-errors-per-t ", j
++);
930 printf("%d:rx-bytes ", j
++);
931 printf("%d:rx-pkts ", j
++);
932 printf("%d:rx-drops ", j
++);
933 printf("%d:rx-errors ", j
++);
935 printf("%d:tx-bytes-per-t ", j
++);
936 printf("%d:tx-pkts-per-t ", j
++);
937 printf("%d:tx-drops-per-t ", j
++);
938 printf("%d:tx-errors-per-t ", j
++);
940 printf("%d:tx-bytes ", j
++);
941 printf("%d:tx-pkts ", j
++);
942 printf("%d:tx-drops ", j
++);
943 printf("%d:tx-errors ", j
++);
945 printf("%d:context-switches-per-t ", j
++);
946 printf("%d:mem-free ", j
++);
947 printf("%d:mem-used ", j
++);
948 printf("%d:mem-total ", j
++);
949 printf("%d:procs-in-run ", j
++);
950 printf("%d:procs-in-iow ", j
++);
952 cpus
= get_number_cpus();
954 for (i
= 0, j
= 22; i
< cpus
; ++i
) {
955 printf("%d:cpu%i-usr-per-t ", j
++, i
);
956 printf("%d:cpu%i-nice-per-t ", j
++, i
);
957 printf("%d:cpu%i-sys-per-t ", j
++, i
);
958 printf("%d:cpu%i-idle-per-t ", j
++, i
);
959 printf("%d:cpu%i-iow-per-t ", j
++, i
);
961 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
962 printf("%d:cpu%i-net-irqs ", j
++, i
);
964 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
965 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
966 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
967 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
970 if (iswireless(abs
)) {
971 printf("%d:wifi-link-qual-per-t ", j
++);
972 printf("%d:wifi-link-qual ", j
++);
973 printf("%d:wifi-link-qual-max ", j
++);
975 printf("%d:wifi-signal-dbm-per-t ", j
++);
976 printf("%d:wifi-signal-dbm ", j
++);
984 static int term_main(const char *ifname
, uint64_t ms_interval
,
985 unsigned int top_cpus __maybe_unused
)
990 stats_sample_generic(ifname
, ms_interval
);
994 term_csv_header(ifname
, &stats_new
, ms_interval
);
997 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
998 } while (stats_loop
&& !sigint
);
1003 int main(int argc
, char **argv
)
1006 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1007 unsigned int top_cpus
= 10;
1008 uint64_t interval
= 1000;
1009 char *ifname
= NULL
;
1010 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1011 unsigned int top_cpus
) = screen_main
;
1016 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1017 &opt_index
)) != EOF
) {
1026 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1029 interval
= strtoul(optarg
, NULL
, 10);
1032 top_cpus
= strtoul(optarg
, NULL
, 10);
1041 func_main
= term_main
;
1047 panic("Option -%c requires an argument!\n",
1050 if (isprint(optopt
))
1051 printf("Unknown option character `0x%X\'!\n", optopt
);
1063 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1065 panic("No networking device given!\n");
1067 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1068 panic("lo is not supported!\n");
1069 if (device_mtu(ifname
) == 0)
1070 panic("This is no networking device!\n");
1072 register_signal(SIGINT
, signal_handler
);
1073 register_signal(SIGHUP
, signal_handler
);
1075 cpus
= get_number_cpus();
1076 top_cpus
= min(top_cpus
, cpus
);
1078 stats_alloc(&stats_old
, cpus
);
1079 stats_alloc(&stats_new
, cpus
);
1080 stats_alloc(&stats_delta
, cpus
);
1082 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1085 ifflags
= enter_promiscuous_mode(ifname
);
1086 ret
= func_main(ifname
, interval
, top_cpus
);
1088 leave_promiscuous_mode(ifname
, ifflags
);