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>
31 int16_t link_qual
, link_qual_max
;
32 int signal_level
/*, noise_level*/;
36 long long unsigned int rx_bytes
, rx_packets
, rx_drops
, rx_errors
;
37 long long unsigned int rx_fifo
, rx_frame
, rx_multi
;
38 long long unsigned int tx_bytes
, tx_packets
, tx_drops
, tx_errors
;
39 long long unsigned int tx_fifo
, tx_colls
, tx_carrier
;
40 uint64_t mem_free
, mem_total
;
41 uint32_t irq_nr
, procs_run
, procs_iow
, cswitch
, forks
;
42 struct wifi_stat wifi
;
44 * Pointer members need to be last in order for stats_zero() to work
47 long long unsigned int *irqs
, *irqs_srx
, *irqs_stx
;
48 uint64_t *cpu_user
, *cpu_sys
, *cpu_nice
, *cpu_idle
, *cpu_iow
;
54 long long unsigned int irqs_rel
, irqs_abs
;
57 static volatile sig_atomic_t sigint
= 0;
58 static struct ifstat stats_old
, stats_new
, stats_delta
;
59 static struct cpu_hit
*cpu_hits
;
60 static int stats_loop
= 0;
61 static WINDOW
*stats_screen
= NULL
;
63 static const char *short_options
= "d:t:n:vhclp";
64 static const struct option long_options
[] = {
65 {"dev", required_argument
, NULL
, 'd'},
66 {"interval", required_argument
, NULL
, 't'},
67 {"num-cpus", required_argument
, NULL
, 'n'},
68 {"promisc", no_argument
, NULL
, 'p'},
69 {"csv", no_argument
, NULL
, 'c'},
70 {"loop", no_argument
, NULL
, 'l'},
71 {"version", no_argument
, NULL
, 'v'},
72 {"help", no_argument
, NULL
, 'h'},
76 static void signal_handler(int number
)
88 static inline int iswireless(const struct ifstat
*stats
)
90 return stats
->wifi
.bitrate
> 0;
93 static void __noreturn
help(void)
95 printf("\nifpps %s, top-like kernel networking and system statistics\n",
97 puts("http://www.netsniff-ng.org\n\n"
98 "Usage: ifpps [options] || ifpps <netdev>\n"
100 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
101 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
102 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
103 " in ncurses mode (default 10)\n"
104 " -p|--promisc Promiscuous mode\n"
105 " -c|--csv Output to terminal as Gnuplot-ready data\n"
106 " -l|--loop Continuous CSV output\n"
107 " -v|--version Print version and exit\n"
108 " -h|--help Print this help and exit\n\n"
112 " ifpps -lpcd wlan0 > plot.dat\n\n"
114 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
115 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
116 "Please report bugs to <bugs@netsniff-ng.org>\n"
117 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
118 "Swiss federal institute of technology (ETH Zurich)\n"
119 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
120 "License: GNU GPL version 2.0\n"
121 "This is free software: you are free to change and redistribute it.\n"
122 "There is NO WARRANTY, to the extent permitted by law.\n");
126 static void __noreturn
version(void)
128 printf("\nifpps %s, top-like kernel networking and system statistics\n",
130 puts("http://www.netsniff-ng.org\n\n"
131 "Please report bugs to <bugs@netsniff-ng.org>\n"
132 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
133 "Swiss federal institute of technology (ETH Zurich)\n"
134 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
135 "License: GNU GPL version 2.0\n"
136 "This is free software: you are free to change and redistribute it.\n"
137 "There is NO WARRANTY, to the extent permitted by law.\n");
141 static inline int padding_from_num(int n
)
145 while ((n
/= 10) > 0);
149 #define STATS_ALLOC1(member) \
150 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
152 static void stats_alloc(struct ifstat
*stats
, int cpus
)
155 STATS_ALLOC1(irqs_srx
);
156 STATS_ALLOC1(irqs_stx
);
158 STATS_ALLOC1(cpu_user
);
159 STATS_ALLOC1(cpu_sys
);
160 STATS_ALLOC1(cpu_nice
);
161 STATS_ALLOC1(cpu_idle
);
162 STATS_ALLOC1(cpu_iow
);
165 #define STATS_ZERO1(member) \
166 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
168 static void stats_zero(struct ifstat
*stats
, int cpus
)
170 /* Only clear the non-pointer members */
171 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
174 STATS_ZERO1(irqs_srx
);
175 STATS_ZERO1(irqs_stx
);
177 STATS_ZERO1(cpu_user
);
178 STATS_ZERO1(cpu_sys
);
179 STATS_ZERO1(cpu_nice
);
180 STATS_ZERO1(cpu_idle
);
181 STATS_ZERO1(cpu_iow
);
184 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
190 fp
= fopen("/proc/net/dev", "r");
192 panic("Cannot open /proc/net/dev!\n");
194 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
195 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
197 memset(buff
, 0, sizeof(buff
));
199 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
200 buff
[sizeof(buff
) -1] = 0;
202 if (strstr(buff
, ifname
) == NULL
)
205 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
206 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
207 &stats
->rx_bytes
, &stats
->rx_packets
,
208 &stats
->rx_errors
, &stats
->rx_drops
,
209 &stats
->rx_fifo
, &stats
->rx_frame
,
210 &stats
->rx_multi
, &stats
->tx_bytes
,
211 &stats
->tx_packets
, &stats
->tx_errors
,
212 &stats
->tx_drops
, &stats
->tx_fifo
,
213 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
218 memset(buff
, 0, sizeof(buff
));
225 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
227 int ret
= -EINVAL
, i
, cpus
, try = 0;
231 struct ethtool_drvinfo drvinf
;
234 fp
= fopen("/proc/interrupts", "r");
236 panic("Cannot open /proc/interrupts!\n");
238 cpus
= get_number_cpus();
239 buff_len
= cpus
* 128;
240 buff
= xmalloc(buff_len
);
242 fseek(fp
, 0, SEEK_SET
);
243 memset(buff
, 0, buff_len
);
245 while (fgets(buff
, buff_len
, fp
) != NULL
) {
246 buff
[buff_len
- 1] = 0;
249 if (strstr(buff
, ifname
) == NULL
)
252 /* XXX: remove this one here */
253 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
254 bug_on(stats
->irq_nr
== 0);
258 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
260 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
262 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
269 memset(buff
, 0, buff_len
);
272 if (ret
== -EINVAL
&& try == 0) {
273 memset(&drvinf
, 0, sizeof(drvinf
));
274 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
277 ifname
= drvinf
.driver
;
288 static int stats_proc_softirqs(struct ifstat
*stats
)
299 fp
= fopen("/proc/softirqs", "r");
301 panic("Cannot open /proc/softirqs!\n");
303 cpus
= get_number_cpus();
304 buff_len
= cpus
* 128;
305 buff
= xmalloc(buff_len
);
307 memset(buff
, 0, buff_len
);
309 while (fgets(buff
, buff_len
, fp
) != NULL
) {
310 buff
[buff_len
- 1] = 0;
312 if ((ptr
= strstr(buff
, "NET_TX:")))
313 net_type
= softirqs_net_tx
;
314 else if ((ptr
= strstr(buff
, "NET_RX:")))
315 net_type
= softirqs_net_rx
;
319 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
321 case softirqs_net_tx
:
322 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
324 case softirqs_net_rx
:
325 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
330 memset(buff
, 0, buff_len
);
338 static int stats_proc_memory(struct ifstat
*stats
)
340 char *ptr
, buff
[256];
343 fp
= fopen("/proc/meminfo", "r");
345 panic("Cannot open /proc/meminfo!\n");
347 memset(buff
, 0, sizeof(buff
));
349 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
350 buff
[sizeof(buff
) - 1] = 0;
352 if ((ptr
= strstr(buff
, "MemTotal:"))) {
353 ptr
+= strlen("MemTotal:");
354 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
355 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
356 ptr
+= strlen("MemFree:");
357 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
360 memset(buff
, 0, sizeof(buff
));
367 static int stats_proc_system(struct ifstat
*stats
)
370 char *ptr
, buff
[256];
373 fp
= fopen("/proc/stat", "r");
375 panic("Cannot open /proc/stat!\n");
377 cpus
= get_number_cpus();
379 memset(buff
, 0, sizeof(buff
));
381 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
382 buff
[sizeof(buff
) - 1] = 0;
384 if ((ptr
= strstr(buff
, "cpu"))) {
385 ptr
+= strlen("cpu");
389 cpu
= strtol(ptr
, &ptr
, 10);
392 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
393 &stats
->cpu_user
[cpu
],
394 &stats
->cpu_nice
[cpu
],
395 &stats
->cpu_sys
[cpu
],
396 &stats
->cpu_idle
[cpu
],
397 &stats
->cpu_iow
[cpu
]) != 5)
399 } else if ((ptr
= strstr(buff
, "ctxt"))) {
400 ptr
+= strlen("ctxt");
401 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
402 } else if ((ptr
= strstr(buff
, "processes"))) {
403 ptr
+= strlen("processes");
404 stats
->forks
= strtoul(ptr
, &ptr
, 10);
405 } else if ((ptr
= strstr(buff
, "procs_running"))) {
406 ptr
+= strlen("procs_running");
407 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
408 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
409 ptr
+= strlen("procs_blocked");
410 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
413 memset(buff
, 0, sizeof(buff
));
420 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
425 return dbm_val
- 0x100;
428 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
431 struct iw_statistics ws
;
433 ret
= wireless_sigqual(ifname
, &ws
);
435 stats
->wifi
.bitrate
= 0;
439 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
441 stats
->wifi
.signal_level
=
442 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
444 stats
->wifi
.link_qual
= ws
.qual
.qual
;
445 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
450 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
451 #define DIFF(member) do { \
452 if (sizeof(diff->member) != sizeof(new->member) || \
453 sizeof(diff->member) != sizeof(old->member)) \
455 bug_on((new->member - old->member) > (new->member)); \
459 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
484 DIFF1(wifi
.signal_level
);
485 DIFF1(wifi
.link_qual
);
490 cpus
= get_number_cpus();
492 for (i
= 0; i
< cpus
; ++i
) {
505 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
507 if (stats_proc_net_dev(ifname
, stats
) < 0)
508 panic("Cannot fetch device stats!\n");
509 if (stats_proc_softirqs(stats
) < 0)
510 panic("Cannot fetch software interrupts!\n");
511 if (stats_proc_memory(stats
) < 0)
512 panic("Cannot fetch memory stats!\n");
513 if (stats_proc_system(stats
) < 0)
514 panic("Cannot fetch system stats!\n");
516 stats_proc_interrupts((char *) ifname
, stats
);
518 stats_wireless(ifname
, stats
);
521 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
523 int cpus
= get_number_cpus();
525 stats_zero(&stats_old
, cpus
);
526 stats_zero(&stats_new
, cpus
);
527 stats_zero(&stats_delta
, cpus
);
529 stats_fetch(ifname
, &stats_old
);
530 usleep(ms_interval
* 1000);
531 stats_fetch(ifname
, &stats_new
);
533 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
536 static int cmp_hits(const void *p1
, const void *p2
)
538 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
541 * We want the hits sorted in descending order, thus reverse the return
544 if (h1
->hit
== h2
->hit
)
546 else if (h1
->hit
< h2
->hit
)
552 static int cmp_irqs_rel(const void *p1
, const void *p2
)
554 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
557 * We want the hits sorted in descending order, thus reverse the return
560 if (h1
->irqs_rel
== h2
->irqs_rel
)
562 else if (h1
->irqs_rel
< h2
->irqs_rel
)
568 static int cmp_irqs_abs(const void *p1
, const void *p2
)
570 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
573 * We want the hits sorted in descending order, thus reverse the return
576 if (h1
->irqs_abs
== h2
->irqs_abs
)
578 else if (h1
->irqs_abs
< h2
->irqs_abs
)
584 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
589 for (i
= 0; i
< top_cpus
; ++i
) {
591 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
592 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
593 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
597 static void screen_init(WINDOW
**screen
)
599 (*screen
) = initscr();
604 nodelay((*screen
), TRUE
);
606 keypad(stdscr
, TRUE
);
612 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
613 uint64_t ms_interval
, unsigned int top_cpus
)
617 struct ethtool_drvinfo drvinf
;
618 u32 rate
= device_bitrate(ifname
);
619 int link
= ethtool_link(ifname
);
621 memset(&drvinf
, 0, sizeof(drvinf
));
622 ethtool_drvinf(ifname
, &drvinf
);
624 memset(buff
, 0, sizeof(buff
));
626 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
628 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
629 link
== 0 ? "no" : "yes");
631 mvwprintw(screen
, (*voff
)++, 2,
632 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u/%u"
634 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
, get_number_cpus());
637 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
642 mvwprintw(screen
, (*voff
)++, 0,
643 " rx: %16.3llf MiB/t "
647 ((long double) rel
->rx_bytes
) / (1LLU << 20),
648 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
650 mvwprintw(screen
, (*voff
)++, 0,
651 " tx: %16.3llf MiB/t "
655 ((long double) rel
->tx_bytes
) / (1LLU << 20),
656 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
661 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
664 mvwprintw(screen
, (*voff
)++, 2,
669 ((long double) abs
->rx_bytes
) / (1LLU << 20),
670 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
672 mvwprintw(screen
, (*voff
)++, 2,
677 ((long double) abs
->tx_bytes
) / (1LLU << 20),
678 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
681 static void screen_sys_mem(WINDOW
*screen
, const struct ifstat
*rel
,
682 const struct ifstat
*abs
, int *voff
)
684 mvwprintw(screen
, (*voff
)++, 2,
690 (100.0 * (abs
->mem_total
- abs
->mem_free
)) / abs
->mem_total
,
691 abs
->procs_run
, abs
->procs_iow
);
694 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
695 int top_cpus
, int *voff
)
699 int max_padd
= padding_from_num(get_number_cpus());
701 for (i
= 0; i
< top_cpus
; ++i
) {
702 unsigned int idx
= cpu_hits
[i
].idx
;
704 all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
705 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
707 mvwprintw(screen
, (*voff
)++, 2,
708 "cpu%*d: %13.1lf%% usr/t "
711 "%11.1lf%% iow/t ", max_padd
, idx
,
712 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
713 100.0 * rel
->cpu_sys
[idx
] / all
,
714 100.0 * rel
->cpu_idle
[idx
] / all
,
715 100.0 * rel
->cpu_iow
[idx
] / all
);
719 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
720 int top_cpus
, int *voff
)
723 int max_padd
= padding_from_num(get_number_cpus());
725 for (i
= 0; i
< top_cpus
; ++i
) {
726 unsigned int idx
= cpu_hits
[i
].idx
;
728 mvwprintw(screen
, (*voff
)++, 2,
729 "cpu%*d: %14llu irqs/t "
731 "%15llu sirq tx/t ", max_padd
, idx
,
738 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
739 int top_cpus
, int *voff
)
742 int max_padd
= padding_from_num(get_number_cpus());
744 for (i
= 0; i
< top_cpus
; ++i
) {
745 unsigned int idx
= cpu_hits
[i
].idx
;
747 mvwprintw(screen
, (*voff
)++, 2,
748 "cpu%*d: %14llu irqs", max_padd
, idx
,
753 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
754 const struct ifstat
*abs
, int *voff
)
756 if (iswireless(abs
)) {
757 mvwprintw(screen
, (*voff
)++, 2,
758 "linkqual: %7d/%d (%d/t) ",
760 abs
->wifi
.link_qual_max
,
761 rel
->wifi
.link_qual
);
763 mvwprintw(screen
, (*voff
)++, 2,
764 "signal: %8d dBm (%d dBm/t) ",
765 abs
->wifi
.signal_level
,
766 rel
->wifi
.signal_level
);
770 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
771 const struct ifstat
*abs
, int *first
, uint64_t ms_interval
,
772 unsigned int top_cpus
)
774 int cpus
, top
, voff
= 1, cvoff
= 2;
778 cpus
= get_number_cpus();
779 top
= min(cpus
, top_cpus
);
781 stats_top(rel
, abs
, cpus
);
783 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
785 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
788 screen_net_dev_rel(screen
, rel
, &voff
);
791 screen_net_dev_abs(screen
, abs
, &voff
);
794 screen_sys_mem(screen
, rel
, abs
, &voff
);
797 screen_percpu_states(screen
, rel
, top
, &voff
);
799 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
802 screen_percpu_irqs_rel(screen
, rel
, top
, &voff
);
804 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
807 screen_percpu_irqs_abs(screen
, abs
, top
, &voff
);
810 screen_wireless(screen
, rel
, abs
, &voff
);
813 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
816 mvwprintw(screen
, cvoff
, 2, " ");
823 static void screen_end(void)
828 static int screen_main(const char *ifname
, uint64_t ms_interval
,
829 unsigned int top_cpus
)
833 screen_init(&stats_screen
);
837 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
840 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
,
841 &first
, ms_interval
, top_cpus
);
843 stats_sample_generic(ifname
, ms_interval
);
851 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
852 const struct ifstat
*abs
, uint64_t ms_interval
)
856 printf("%ld ", time(0));
858 printf("%llu ", rel
->rx_bytes
);
859 printf("%llu ", rel
->rx_packets
);
860 printf("%llu ", rel
->rx_drops
);
861 printf("%llu ", rel
->rx_errors
);
863 printf("%llu ", abs
->rx_bytes
);
864 printf("%llu ", abs
->rx_packets
);
865 printf("%llu ", abs
->rx_drops
);
866 printf("%llu ", abs
->rx_errors
);
868 printf("%llu ", rel
->tx_bytes
);
869 printf("%llu ", rel
->tx_packets
);
870 printf("%llu ", rel
->tx_drops
);
871 printf("%llu ", rel
->tx_errors
);
873 printf("%llu ", abs
->tx_bytes
);
874 printf("%llu ", abs
->tx_packets
);
875 printf("%llu ", abs
->tx_drops
);
876 printf("%llu ", abs
->tx_errors
);
878 printf("%u ", rel
->cswitch
);
879 printf("%lu ", abs
->mem_free
);
880 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
881 printf("%lu ", abs
->mem_total
);
882 printf("%u ", abs
->procs_run
);
883 printf("%u ", abs
->procs_iow
);
885 cpus
= get_number_cpus();
887 for (i
= 0; i
< cpus
; ++i
) {
888 printf("%lu ", rel
->cpu_user
[i
]);
889 printf("%lu ", rel
->cpu_nice
[i
]);
890 printf("%lu ", rel
->cpu_sys
[i
]);
891 printf("%lu ", rel
->cpu_idle
[i
]);
892 printf("%lu ", rel
->cpu_iow
[i
]);
894 printf("%llu ", rel
->irqs
[i
]);
895 printf("%llu ", abs
->irqs
[i
]);
897 printf("%llu ", rel
->irqs_srx
[i
]);
898 printf("%llu ", abs
->irqs_srx
[i
]);
900 printf("%llu ", rel
->irqs_stx
[i
]);
901 printf("%llu ", abs
->irqs_stx
[i
]);
904 if (iswireless(abs
)) {
905 printf("%u ", rel
->wifi
.link_qual
);
906 printf("%u ", abs
->wifi
.link_qual
);
907 printf("%u ", abs
->wifi
.link_qual_max
);
909 printf("%d ", rel
->wifi
.signal_level
);
910 printf("%d ", abs
->wifi
.signal_level
);
917 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
918 uint64_t ms_interval
)
922 printf("# gnuplot dump (#col:description)\n");
923 printf("# networking interface: %s\n", ifname
);
924 printf("# sampling interval (t): %lu ms\n", ms_interval
);
925 printf("# %d:unixtime ", j
++);
927 printf("%d:rx-bytes-per-t ", j
++);
928 printf("%d:rx-pkts-per-t ", j
++);
929 printf("%d:rx-drops-per-t ", j
++);
930 printf("%d:rx-errors-per-t ", j
++);
932 printf("%d:rx-bytes ", j
++);
933 printf("%d:rx-pkts ", j
++);
934 printf("%d:rx-drops ", j
++);
935 printf("%d:rx-errors ", j
++);
937 printf("%d:tx-bytes-per-t ", j
++);
938 printf("%d:tx-pkts-per-t ", j
++);
939 printf("%d:tx-drops-per-t ", j
++);
940 printf("%d:tx-errors-per-t ", j
++);
942 printf("%d:tx-bytes ", j
++);
943 printf("%d:tx-pkts ", j
++);
944 printf("%d:tx-drops ", j
++);
945 printf("%d:tx-errors ", j
++);
947 printf("%d:context-switches-per-t ", j
++);
948 printf("%d:mem-free ", j
++);
949 printf("%d:mem-used ", j
++);
950 printf("%d:mem-total ", j
++);
951 printf("%d:procs-in-run ", j
++);
952 printf("%d:procs-in-iow ", j
++);
954 cpus
= get_number_cpus();
956 for (i
= 0, j
= 22; i
< cpus
; ++i
) {
957 printf("%d:cpu%i-usr-per-t ", j
++, i
);
958 printf("%d:cpu%i-nice-per-t ", j
++, i
);
959 printf("%d:cpu%i-sys-per-t ", j
++, i
);
960 printf("%d:cpu%i-idle-per-t ", j
++, i
);
961 printf("%d:cpu%i-iow-per-t ", j
++, i
);
963 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
964 printf("%d:cpu%i-net-irqs ", j
++, i
);
966 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
967 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
968 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
969 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
972 if (iswireless(abs
)) {
973 printf("%d:wifi-link-qual-per-t ", j
++);
974 printf("%d:wifi-link-qual ", j
++);
975 printf("%d:wifi-link-qual-max ", j
++);
977 printf("%d:wifi-signal-dbm-per-t ", j
++);
978 printf("%d:wifi-signal-dbm ", j
++);
986 static int term_main(const char *ifname
, uint64_t ms_interval
,
987 unsigned int top_cpus __maybe_unused
)
992 stats_sample_generic(ifname
, ms_interval
);
996 term_csv_header(ifname
, &stats_new
, ms_interval
);
999 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1000 } while (stats_loop
&& !sigint
);
1005 int main(int argc
, char **argv
)
1008 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1009 unsigned int top_cpus
= 10;
1010 uint64_t interval
= 1000;
1011 char *ifname
= NULL
;
1012 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1013 unsigned int top_cpus
) = screen_main
;
1018 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1019 &opt_index
)) != EOF
) {
1028 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1031 interval
= strtoul(optarg
, NULL
, 10);
1034 top_cpus
= strtoul(optarg
, NULL
, 10);
1043 func_main
= term_main
;
1049 panic("Option -%c requires an argument!\n",
1052 if (isprint(optopt
))
1053 printf("Unknown option character `0x%X\'!\n", optopt
);
1065 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1067 panic("No networking device given!\n");
1069 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1070 panic("lo is not supported!\n");
1071 if (device_mtu(ifname
) == 0)
1072 panic("This is no networking device!\n");
1074 register_signal(SIGINT
, signal_handler
);
1075 register_signal(SIGHUP
, signal_handler
);
1077 cpus
= get_number_cpus();
1078 top_cpus
= min(top_cpus
, cpus
);
1080 stats_alloc(&stats_old
, cpus
);
1081 stats_alloc(&stats_new
, cpus
);
1082 stats_alloc(&stats_delta
, cpus
);
1084 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1087 ifflags
= enter_promiscuous_mode(ifname
);
1088 ret
= func_main(ifname
, interval
, top_cpus
);
1090 leave_promiscuous_mode(ifname
, ifflags
);