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 volatile sig_atomic_t sigint
= 0;
57 static struct ifstat stats_old
, stats_new
, stats_delta
;
59 static struct cpu_hit
*cpu_hits
;
61 static int stats_loop
= 0;
63 static WINDOW
*stats_screen
= NULL
;
65 static const char *short_options
= "d:t:n:vhclp";
66 static const struct option long_options
[] = {
67 {"dev", required_argument
, NULL
, 'd'},
68 {"interval", required_argument
, NULL
, 't'},
69 {"num-cpus", required_argument
, NULL
, 'n'},
70 {"promisc", no_argument
, NULL
, 'p'},
71 {"csv", no_argument
, NULL
, 'c'},
72 {"loop", no_argument
, NULL
, 'l'},
73 {"version", no_argument
, NULL
, 'v'},
74 {"help", no_argument
, NULL
, 'h'},
78 static void signal_handler(int number
)
90 static inline int iswireless(const struct ifstat
*stats
)
92 return stats
->wifi
.bitrate
> 0;
95 static void __noreturn
help(void)
97 printf("\nifpps %s, top-like kernel networking and system statistics\n",
99 puts("http://www.netsniff-ng.org\n\n"
100 "Usage: ifpps [options] || ifpps <netdev>\n"
102 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
103 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
104 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
105 " in ncurses mode (default 10)\n"
106 " -p|--promisc Promiscuous mode\n"
107 " -c|--csv Output to terminal as Gnuplot-ready data\n"
108 " -l|--loop Continuous CSV output\n"
109 " -v|--version Print version and exit\n"
110 " -h|--help Print this help and exit\n\n"
114 " ifpps -lpcd wlan0 > plot.dat\n\n"
116 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
117 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
118 "Please report bugs to <bugs@netsniff-ng.org>\n"
119 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
120 "Swiss federal institute of technology (ETH Zurich)\n"
121 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
122 "License: GNU GPL version 2.0\n"
123 "This is free software: you are free to change and redistribute it.\n"
124 "There is NO WARRANTY, to the extent permitted by law.\n");
128 static void __noreturn
version(void)
130 printf("\nifpps %s, top-like kernel networking and system statistics\n",
132 puts("http://www.netsniff-ng.org\n\n"
133 "Please report bugs to <bugs@netsniff-ng.org>\n"
134 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
135 "Swiss federal institute of technology (ETH Zurich)\n"
136 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
137 "License: GNU GPL version 2.0\n"
138 "This is free software: you are free to change and redistribute it.\n"
139 "There is NO WARRANTY, to the extent permitted by law.\n");
143 static inline int padding_from_num(int n
)
147 while ((n
/= 10) > 0);
151 #define STATS_ALLOC1(member) \
152 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
154 static void stats_alloc(struct ifstat
*stats
, int cpus
)
157 STATS_ALLOC1(irqs_srx
);
158 STATS_ALLOC1(irqs_stx
);
160 STATS_ALLOC1(cpu_user
);
161 STATS_ALLOC1(cpu_sys
);
162 STATS_ALLOC1(cpu_nice
);
163 STATS_ALLOC1(cpu_idle
);
164 STATS_ALLOC1(cpu_iow
);
167 #define STATS_ZERO1(member) \
168 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
170 static void stats_zero(struct ifstat
*stats
, int cpus
)
172 /* Only clear the non-pointer members */
173 memset(stats
, 0, offsetof(struct ifstat
, irqs
));
176 STATS_ZERO1(irqs_srx
);
177 STATS_ZERO1(irqs_stx
);
179 STATS_ZERO1(cpu_user
);
180 STATS_ZERO1(cpu_sys
);
181 STATS_ZERO1(cpu_nice
);
182 STATS_ZERO1(cpu_idle
);
183 STATS_ZERO1(cpu_iow
);
186 static int stats_proc_net_dev(const char *ifname
, struct ifstat
*stats
)
192 fp
= fopen("/proc/net/dev", "r");
194 panic("Cannot open /proc/net/dev!\n");
196 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
197 if (fgets(buff
, sizeof(buff
), fp
)) { ; }
199 memset(buff
, 0, sizeof(buff
));
201 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
202 buff
[sizeof(buff
) -1] = 0;
204 if (strstr(buff
, ifname
) == NULL
)
207 if (sscanf(buff
, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
208 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
209 &stats
->rx_bytes
, &stats
->rx_packets
,
210 &stats
->rx_errors
, &stats
->rx_drops
,
211 &stats
->rx_fifo
, &stats
->rx_frame
,
212 &stats
->rx_multi
, &stats
->tx_bytes
,
213 &stats
->tx_packets
, &stats
->tx_errors
,
214 &stats
->tx_drops
, &stats
->tx_fifo
,
215 &stats
->tx_colls
, &stats
->tx_carrier
) == 14) {
220 memset(buff
, 0, sizeof(buff
));
227 static int stats_proc_interrupts(char *ifname
, struct ifstat
*stats
)
229 int ret
= -EINVAL
, i
, cpus
, try = 0;
233 struct ethtool_drvinfo drvinf
;
236 fp
= fopen("/proc/interrupts", "r");
238 panic("Cannot open /proc/interrupts!\n");
240 cpus
= get_number_cpus();
241 buff_len
= cpus
* 128;
242 buff
= xmalloc(buff_len
);
244 fseek(fp
, 0, SEEK_SET
);
245 memset(buff
, 0, buff_len
);
247 while (fgets(buff
, buff_len
, fp
) != NULL
) {
248 buff
[buff_len
- 1] = 0;
251 if (strstr(buff
, ifname
) == NULL
)
254 /* XXX: remove this one here */
255 stats
->irq_nr
= strtol(ptr
, &ptr
, 10);
256 bug_on(stats
->irq_nr
== 0);
260 for (i
= 0; i
< cpus
&& ptr
; ++i
) {
262 stats
->irqs
[i
] += strtol(ptr
, &ptr
, 10);
264 stats
->irqs
[i
] = strtol(ptr
, &ptr
, 10);
271 memset(buff
, 0, buff_len
);
274 if (ret
== -EINVAL
&& try == 0) {
275 memset(&drvinf
, 0, sizeof(drvinf
));
276 if (ethtool_drvinf(ifname
, &drvinf
) < 0)
279 ifname
= drvinf
.driver
;
290 static int stats_proc_softirqs(struct ifstat
*stats
)
301 fp
= fopen("/proc/softirqs", "r");
303 panic("Cannot open /proc/softirqs!\n");
305 cpus
= get_number_cpus();
306 buff_len
= cpus
* 128;
307 buff
= xmalloc(buff_len
);
309 memset(buff
, 0, buff_len
);
311 while (fgets(buff
, buff_len
, fp
) != NULL
) {
312 buff
[buff_len
- 1] = 0;
314 if ((ptr
= strstr(buff
, "NET_TX:")))
315 net_type
= softirqs_net_tx
;
316 else if ((ptr
= strstr(buff
, "NET_RX:")))
317 net_type
= softirqs_net_rx
;
321 for (ptr
+= strlen("NET_xX:"), i
= 0; i
< cpus
; ++i
) {
323 case softirqs_net_tx
:
324 stats
->irqs_stx
[i
] = strtol(ptr
, &ptr
, 10);
326 case softirqs_net_rx
:
327 stats
->irqs_srx
[i
] = strtol(ptr
, &ptr
, 10);
332 memset(buff
, 0, buff_len
);
340 static int stats_proc_memory(struct ifstat
*stats
)
342 char *ptr
, buff
[256];
345 fp
= fopen("/proc/meminfo", "r");
347 panic("Cannot open /proc/meminfo!\n");
349 memset(buff
, 0, sizeof(buff
));
351 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
352 buff
[sizeof(buff
) - 1] = 0;
354 if ((ptr
= strstr(buff
, "MemTotal:"))) {
355 ptr
+= strlen("MemTotal:");
356 stats
->mem_total
= strtoul(ptr
, &ptr
, 10);
357 } else if ((ptr
= strstr(buff
, "MemFree:"))) {
358 ptr
+= strlen("MemFree:");
359 stats
->mem_free
= strtoul(ptr
, &ptr
, 10);
362 memset(buff
, 0, sizeof(buff
));
369 static int stats_proc_system(struct ifstat
*stats
)
372 char *ptr
, buff
[256];
375 fp
= fopen("/proc/stat", "r");
377 panic("Cannot open /proc/stat!\n");
379 cpus
= get_number_cpus();
381 memset(buff
, 0, sizeof(buff
));
383 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
384 buff
[sizeof(buff
) - 1] = 0;
386 if ((ptr
= strstr(buff
, "cpu"))) {
387 ptr
+= strlen("cpu");
391 cpu
= strtol(ptr
, &ptr
, 10);
394 if (sscanf(ptr
, "%lu%lu%lu%lu%lu",
395 &stats
->cpu_user
[cpu
],
396 &stats
->cpu_nice
[cpu
],
397 &stats
->cpu_sys
[cpu
],
398 &stats
->cpu_idle
[cpu
],
399 &stats
->cpu_iow
[cpu
]) != 5)
401 } else if ((ptr
= strstr(buff
, "ctxt"))) {
402 ptr
+= strlen("ctxt");
403 stats
->cswitch
= strtoul(ptr
, &ptr
, 10);
404 } else if ((ptr
= strstr(buff
, "processes"))) {
405 ptr
+= strlen("processes");
406 stats
->forks
= strtoul(ptr
, &ptr
, 10);
407 } else if ((ptr
= strstr(buff
, "procs_running"))) {
408 ptr
+= strlen("procs_running");
409 stats
->procs_run
= strtoul(ptr
, &ptr
, 10);
410 } else if ((ptr
= strstr(buff
, "procs_blocked"))) {
411 ptr
+= strlen("procs_blocked");
412 stats
->procs_iow
= strtoul(ptr
, &ptr
, 10);
415 memset(buff
, 0, sizeof(buff
));
422 static int adjust_dbm_level(int in_dbm
, int dbm_val
)
427 return dbm_val
- 0x100;
430 static int stats_wireless(const char *ifname
, struct ifstat
*stats
)
433 struct iw_statistics ws
;
435 ret
= wireless_sigqual(ifname
, &ws
);
437 stats
->wifi
.bitrate
= 0;
441 stats
->wifi
.bitrate
= wireless_bitrate(ifname
);
443 stats
->wifi
.signal_level
=
444 adjust_dbm_level(ws
.qual
.updated
& IW_QUAL_DBM
, ws
.qual
.level
);
446 stats
->wifi
.link_qual
= ws
.qual
.qual
;
447 stats
->wifi
.link_qual_max
= wireless_rangemax_sigqual(ifname
);
452 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
453 #define DIFF(member) do { \
454 if (sizeof(diff->member) != sizeof(new->member) || \
455 sizeof(diff->member) != sizeof(old->member)) \
457 bug_on((new->member - old->member) > (new->member)); \
461 static void stats_diff(struct ifstat
*old
, struct ifstat
*new,
486 DIFF1(wifi
.signal_level
);
487 DIFF1(wifi
.link_qual
);
492 cpus
= get_number_cpus();
494 for (i
= 0; i
< cpus
; ++i
) {
507 static void stats_fetch(const char *ifname
, struct ifstat
*stats
)
509 if (stats_proc_net_dev(ifname
, stats
) < 0)
510 panic("Cannot fetch device stats!\n");
511 if (stats_proc_softirqs(stats
) < 0)
512 panic("Cannot fetch software interrupts!\n");
513 if (stats_proc_memory(stats
) < 0)
514 panic("Cannot fetch memory stats!\n");
515 if (stats_proc_system(stats
) < 0)
516 panic("Cannot fetch system stats!\n");
518 stats_proc_interrupts((char *) ifname
, stats
);
520 stats_wireless(ifname
, stats
);
523 static void stats_sample_generic(const char *ifname
, uint64_t ms_interval
)
525 int cpus
= get_number_cpus();
527 stats_zero(&stats_old
, cpus
);
528 stats_zero(&stats_new
, cpus
);
529 stats_zero(&stats_delta
, cpus
);
531 stats_fetch(ifname
, &stats_old
);
532 usleep(ms_interval
* 1000);
533 stats_fetch(ifname
, &stats_new
);
535 stats_diff(&stats_old
, &stats_new
, &stats_delta
);
538 static int cmp_hits(const void *p1
, const void *p2
)
540 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
543 * We want the hits sorted in descending order, thus reverse the return
546 if (h1
->hit
== h2
->hit
)
548 else if (h1
->hit
< h2
->hit
)
554 static int cmp_irqs_rel(const void *p1
, const void *p2
)
556 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
559 * We want the hits sorted in descending order, thus reverse the return
562 if (h1
->irqs_rel
== h2
->irqs_rel
)
564 else if (h1
->irqs_rel
< h2
->irqs_rel
)
570 static int cmp_irqs_abs(const void *p1
, const void *p2
)
572 const struct cpu_hit
*h1
= p1
, *h2
= p2
;
575 * We want the hits sorted in descending order, thus reverse the return
578 if (h1
->irqs_abs
== h2
->irqs_abs
)
580 else if (h1
->irqs_abs
< h2
->irqs_abs
)
586 static void stats_top(const struct ifstat
*rel
, const struct ifstat
*abs
,
591 for (i
= 0; i
< top_cpus
; ++i
) {
593 cpu_hits
[i
].hit
= rel
->cpu_user
[i
] + rel
->cpu_nice
[i
] + rel
->cpu_sys
[i
];
594 cpu_hits
[i
].irqs_rel
= rel
->irqs
[i
];
595 cpu_hits
[i
].irqs_abs
= abs
->irqs
[i
];
599 static void screen_init(WINDOW
**screen
)
601 (*screen
) = initscr();
606 nodelay((*screen
), TRUE
);
608 keypad(stdscr
, TRUE
);
614 static void screen_header(WINDOW
*screen
, const char *ifname
, int *voff
,
615 uint64_t ms_interval
, unsigned int top_cpus
)
619 struct ethtool_drvinfo drvinf
;
620 u32 rate
= device_bitrate(ifname
);
621 int link
= ethtool_link(ifname
);
623 memset(&drvinf
, 0, sizeof(drvinf
));
624 ethtool_drvinf(ifname
, &drvinf
);
626 memset(buff
, 0, sizeof(buff
));
628 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " %uMbit/s", rate
);
630 len
+= snprintf(buff
+ len
, sizeof(buff
) - len
, " link:%s",
631 link
== 0 ? "no" : "yes");
633 mvwprintw(screen
, (*voff
)++, 2,
634 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u/%u"
636 ifname
, drvinf
.driver
, buff
, ms_interval
, top_cpus
, get_number_cpus());
639 static void screen_net_dev_rel(WINDOW
*screen
, const struct ifstat
*rel
,
644 mvwprintw(screen
, (*voff
)++, 0,
645 " rx: %16.3llf MiB/t "
649 ((long double) rel
->rx_bytes
) / (1LLU << 20),
650 rel
->rx_packets
, rel
->rx_drops
, rel
->rx_errors
);
652 mvwprintw(screen
, (*voff
)++, 0,
653 " tx: %16.3llf MiB/t "
657 ((long double) rel
->tx_bytes
) / (1LLU << 20),
658 rel
->tx_packets
, rel
->tx_drops
, rel
->tx_errors
);
663 static void screen_net_dev_abs(WINDOW
*screen
, const struct ifstat
*abs
,
666 mvwprintw(screen
, (*voff
)++, 2,
671 ((long double) abs
->rx_bytes
) / (1LLU << 20),
672 abs
->rx_packets
, abs
->rx_drops
, abs
->rx_errors
);
674 mvwprintw(screen
, (*voff
)++, 2,
679 ((long double) abs
->tx_bytes
) / (1LLU << 20),
680 abs
->tx_packets
, abs
->tx_drops
, abs
->tx_errors
);
683 static void screen_sys_mem(WINDOW
*screen
, const struct ifstat
*rel
,
684 const struct ifstat
*abs
, int *voff
)
686 mvwprintw(screen
, (*voff
)++, 2,
692 (100.0 * (abs
->mem_total
- abs
->mem_free
)) / abs
->mem_total
,
693 abs
->procs_run
, abs
->procs_iow
);
696 static void screen_percpu_states(WINDOW
*screen
, const struct ifstat
*rel
,
697 int top_cpus
, int *voff
)
701 int max_padd
= padding_from_num(get_number_cpus());
703 for (i
= 0; i
< top_cpus
; ++i
) {
704 unsigned int idx
= cpu_hits
[i
].idx
;
706 all
= rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
] + rel
->cpu_sys
[idx
] +
707 rel
->cpu_idle
[idx
] + rel
->cpu_iow
[idx
];
709 mvwprintw(screen
, (*voff
)++, 2,
710 "cpu%*d: %13.1lf%% usr/t "
713 "%11.1lf%% iow/t ", max_padd
, idx
,
714 100.0 * (rel
->cpu_user
[idx
] + rel
->cpu_nice
[idx
]) / all
,
715 100.0 * rel
->cpu_sys
[idx
] / all
,
716 100.0 * rel
->cpu_idle
[idx
] / all
,
717 100.0 * rel
->cpu_iow
[idx
] / all
);
721 static void screen_percpu_irqs_rel(WINDOW
*screen
, const struct ifstat
*rel
,
722 int top_cpus
, int *voff
)
725 int max_padd
= padding_from_num(get_number_cpus());
727 for (i
= 0; i
< top_cpus
; ++i
) {
728 unsigned int idx
= cpu_hits
[i
].idx
;
730 mvwprintw(screen
, (*voff
)++, 2,
731 "cpu%*d: %14llu irqs/t "
733 "%15llu sirq tx/t ", max_padd
, idx
,
740 static void screen_percpu_irqs_abs(WINDOW
*screen
, const struct ifstat
*abs
,
741 int top_cpus
, int *voff
)
744 int max_padd
= padding_from_num(get_number_cpus());
746 for (i
= 0; i
< top_cpus
; ++i
) {
747 unsigned int idx
= cpu_hits
[i
].idx
;
749 mvwprintw(screen
, (*voff
)++, 2,
750 "cpu%*d: %14llu irqs", max_padd
, idx
,
755 static void screen_wireless(WINDOW
*screen
, const struct ifstat
*rel
,
756 const struct ifstat
*abs
, int *voff
)
758 if (iswireless(abs
)) {
759 mvwprintw(screen
, (*voff
)++, 2,
760 "linkqual: %7d/%d (%d/t) ",
762 abs
->wifi
.link_qual_max
,
763 rel
->wifi
.link_qual
);
765 mvwprintw(screen
, (*voff
)++, 2,
766 "signal: %8d dBm (%d dBm/t) ",
767 abs
->wifi
.signal_level
,
768 rel
->wifi
.signal_level
);
772 static void screen_update(WINDOW
*screen
, const char *ifname
, const struct ifstat
*rel
,
773 const struct ifstat
*abs
, int *first
, uint64_t ms_interval
,
774 unsigned int top_cpus
)
776 int cpus
, top
, voff
= 1, cvoff
= 2;
780 cpus
= get_number_cpus();
781 top
= min(cpus
, top_cpus
);
783 stats_top(rel
, abs
, cpus
);
785 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_hits
);
787 screen_header(screen
, ifname
, &voff
, ms_interval
, top_cpus
);
790 screen_net_dev_rel(screen
, rel
, &voff
);
793 screen_net_dev_abs(screen
, abs
, &voff
);
796 screen_sys_mem(screen
, rel
, abs
, &voff
);
799 screen_percpu_states(screen
, rel
, top
, &voff
);
801 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_rel
);
804 screen_percpu_irqs_rel(screen
, rel
, top
, &voff
);
806 qsort(cpu_hits
, cpus
, sizeof(*cpu_hits
), cmp_irqs_abs
);
809 screen_percpu_irqs_abs(screen
, abs
, top
, &voff
);
812 screen_wireless(screen
, rel
, abs
, &voff
);
815 mvwprintw(screen
, cvoff
, 2, "Collecting data ...");
818 mvwprintw(screen
, cvoff
, 2, " ");
825 static void screen_end(void)
830 static int screen_main(const char *ifname
, uint64_t ms_interval
,
831 unsigned int top_cpus
)
835 screen_init(&stats_screen
);
839 if (key
== 'q' || key
== 0x1b || key
== KEY_F(10))
842 screen_update(stats_screen
, ifname
, &stats_delta
, &stats_new
,
843 &first
, ms_interval
, top_cpus
);
845 stats_sample_generic(ifname
, ms_interval
);
853 static void term_csv(const char *ifname
, const struct ifstat
*rel
,
854 const struct ifstat
*abs
, uint64_t ms_interval
)
858 printf("%ld ", time(0));
860 printf("%llu ", rel
->rx_bytes
);
861 printf("%llu ", rel
->rx_packets
);
862 printf("%llu ", rel
->rx_drops
);
863 printf("%llu ", rel
->rx_errors
);
865 printf("%llu ", abs
->rx_bytes
);
866 printf("%llu ", abs
->rx_packets
);
867 printf("%llu ", abs
->rx_drops
);
868 printf("%llu ", abs
->rx_errors
);
870 printf("%llu ", rel
->tx_bytes
);
871 printf("%llu ", rel
->tx_packets
);
872 printf("%llu ", rel
->tx_drops
);
873 printf("%llu ", rel
->tx_errors
);
875 printf("%llu ", abs
->tx_bytes
);
876 printf("%llu ", abs
->tx_packets
);
877 printf("%llu ", abs
->tx_drops
);
878 printf("%llu ", abs
->tx_errors
);
880 printf("%u ", rel
->cswitch
);
881 printf("%lu ", abs
->mem_free
);
882 printf("%lu ", abs
->mem_total
- abs
->mem_free
);
883 printf("%lu ", abs
->mem_total
);
884 printf("%u ", abs
->procs_run
);
885 printf("%u ", abs
->procs_iow
);
887 cpus
= get_number_cpus();
889 for (i
= 0; i
< cpus
; ++i
) {
890 printf("%lu ", rel
->cpu_user
[i
]);
891 printf("%lu ", rel
->cpu_nice
[i
]);
892 printf("%lu ", rel
->cpu_sys
[i
]);
893 printf("%lu ", rel
->cpu_idle
[i
]);
894 printf("%lu ", rel
->cpu_iow
[i
]);
896 printf("%llu ", rel
->irqs
[i
]);
897 printf("%llu ", abs
->irqs
[i
]);
899 printf("%llu ", rel
->irqs_srx
[i
]);
900 printf("%llu ", abs
->irqs_srx
[i
]);
902 printf("%llu ", rel
->irqs_stx
[i
]);
903 printf("%llu ", abs
->irqs_stx
[i
]);
906 if (iswireless(abs
)) {
907 printf("%u ", rel
->wifi
.link_qual
);
908 printf("%u ", abs
->wifi
.link_qual
);
909 printf("%u ", abs
->wifi
.link_qual_max
);
911 printf("%d ", rel
->wifi
.signal_level
);
912 printf("%d ", abs
->wifi
.signal_level
);
919 static void term_csv_header(const char *ifname
, const struct ifstat
*abs
,
920 uint64_t ms_interval
)
924 printf("# gnuplot dump (#col:description)\n");
925 printf("# networking interface: %s\n", ifname
);
926 printf("# sampling interval (t): %lu ms\n", ms_interval
);
927 printf("# %d:unixtime ", j
++);
929 printf("%d:rx-bytes-per-t ", j
++);
930 printf("%d:rx-pkts-per-t ", j
++);
931 printf("%d:rx-drops-per-t ", j
++);
932 printf("%d:rx-errors-per-t ", j
++);
934 printf("%d:rx-bytes ", j
++);
935 printf("%d:rx-pkts ", j
++);
936 printf("%d:rx-drops ", j
++);
937 printf("%d:rx-errors ", j
++);
939 printf("%d:tx-bytes-per-t ", j
++);
940 printf("%d:tx-pkts-per-t ", j
++);
941 printf("%d:tx-drops-per-t ", j
++);
942 printf("%d:tx-errors-per-t ", j
++);
944 printf("%d:tx-bytes ", j
++);
945 printf("%d:tx-pkts ", j
++);
946 printf("%d:tx-drops ", j
++);
947 printf("%d:tx-errors ", j
++);
949 printf("%d:context-switches-per-t ", j
++);
950 printf("%d:mem-free ", j
++);
951 printf("%d:mem-used ", j
++);
952 printf("%d:mem-total ", j
++);
953 printf("%d:procs-in-run ", j
++);
954 printf("%d:procs-in-iow ", j
++);
956 cpus
= get_number_cpus();
958 for (i
= 0, j
= 22; i
< cpus
; ++i
) {
959 printf("%d:cpu%i-usr-per-t ", j
++, i
);
960 printf("%d:cpu%i-nice-per-t ", j
++, i
);
961 printf("%d:cpu%i-sys-per-t ", j
++, i
);
962 printf("%d:cpu%i-idle-per-t ", j
++, i
);
963 printf("%d:cpu%i-iow-per-t ", j
++, i
);
965 printf("%d:cpu%i-net-irqs-per-t ", j
++, i
);
966 printf("%d:cpu%i-net-irqs ", j
++, i
);
968 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j
++, i
);
969 printf("%d:cpu%i-net-rx-soft-irqs ", j
++, i
);
970 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j
++, i
);
971 printf("%d:cpu%i-net-tx-soft-irqs ", j
++, i
);
974 if (iswireless(abs
)) {
975 printf("%d:wifi-link-qual-per-t ", j
++);
976 printf("%d:wifi-link-qual ", j
++);
977 printf("%d:wifi-link-qual-max ", j
++);
979 printf("%d:wifi-signal-dbm-per-t ", j
++);
980 printf("%d:wifi-signal-dbm ", j
++);
988 static int term_main(const char *ifname
, uint64_t ms_interval
,
989 unsigned int top_cpus __maybe_unused
)
994 stats_sample_generic(ifname
, ms_interval
);
998 term_csv_header(ifname
, &stats_new
, ms_interval
);
1001 term_csv(ifname
, &stats_delta
, &stats_new
, ms_interval
);
1002 } while (stats_loop
&& !sigint
);
1007 int main(int argc
, char **argv
)
1010 int c
, opt_index
, ret
, cpus
, promisc
= 0;
1011 unsigned int top_cpus
= 10;
1012 uint64_t interval
= 1000;
1013 char *ifname
= NULL
;
1014 int (*func_main
)(const char *ifname
, uint64_t ms_interval
,
1015 unsigned int top_cpus
) = screen_main
;
1020 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
1021 &opt_index
)) != EOF
) {
1030 ifname
= xstrndup(optarg
, IFNAMSIZ
);
1033 interval
= strtoul(optarg
, NULL
, 10);
1036 top_cpus
= strtoul(optarg
, NULL
, 10);
1045 func_main
= term_main
;
1051 panic("Option -%c requires an argument!\n",
1054 if (isprint(optopt
))
1055 printf("Unknown option character `0x%X\'!\n", optopt
);
1067 ifname
= xstrndup(argv
[1], IFNAMSIZ
);
1069 panic("No networking device given!\n");
1071 if (!strncmp("lo", ifname
, IFNAMSIZ
))
1072 panic("lo is not supported!\n");
1073 if (device_mtu(ifname
) == 0)
1074 panic("This is no networking device!\n");
1076 register_signal(SIGINT
, signal_handler
);
1077 register_signal(SIGHUP
, signal_handler
);
1079 cpus
= get_number_cpus();
1080 top_cpus
= min(top_cpus
, cpus
);
1082 stats_alloc(&stats_old
, cpus
);
1083 stats_alloc(&stats_new
, cpus
);
1084 stats_alloc(&stats_delta
, cpus
);
1086 cpu_hits
= xzmalloc(cpus
* sizeof(*cpu_hits
));
1089 ifflags
= enter_promiscuous_mode(ifname
);
1090 ret
= func_main(ifname
, interval
, top_cpus
);
1092 leave_promiscuous_mode(ifname
, ifflags
);