12 static void stars(char *str
, long val
, long max
, int width
)
16 for (i
= 0; i
< (width
* val
/ max
) - 1 && i
< width
- 1; i
++)
23 static void print_hist(int fd
)
27 long data
[MAX_INDEX
] = {};
28 char starstr
[MAX_STARS
];
33 for (key
= 0; key
< MAX_INDEX
; key
++) {
34 bpf_lookup_elem(fd
, &key
, &value
);
36 if (value
&& key
> max_ind
)
38 if (value
> max_value
)
42 printf(" syscall write() stats\n");
43 printf(" byte_size : count distribution\n");
44 for (i
= 1; i
<= max_ind
+ 1; i
++) {
45 stars(starstr
, data
[i
- 1], max_value
, MAX_STARS
);
46 printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
47 (1l << i
) >> 1, (1l << i
) - 1, data
[i
- 1],
51 static void int_exit(int sig
)
53 print_hist(map_fd
[1]);
57 int main(int ac
, char **argv
)
60 long key
, next_key
, value
;
64 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
66 signal(SIGINT
, int_exit
);
68 /* start 'ping' in the background to have some kfree_skb events */
69 f
= popen("ping -c5 localhost", "r");
72 /* start 'dd' in the background to have plenty of 'write' syscalls */
73 f
= popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
76 if (load_bpf_file(filename
)) {
77 printf("%s", bpf_log_buf
);
81 for (i
= 0; i
< 5; i
++) {
83 while (bpf_get_next_key(map_fd
[0], &key
, &next_key
) == 0) {
84 bpf_lookup_elem(map_fd
[0], &next_key
, &value
);
85 printf("location 0x%lx count %ld\n", next_key
, value
);
92 print_hist(map_fd
[1]);