Linux 4.1.18
[linux/fpc-iii.git] / samples / bpf / tracex2_user.c
blob91b8d0896fbb706487951cea8d2ab4fbece0fb44
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <signal.h>
5 #include <linux/bpf.h>
6 #include "libbpf.h"
7 #include "bpf_load.h"
9 #define MAX_INDEX 64
10 #define MAX_STARS 38
12 static void stars(char *str, long val, long max, int width)
14 int i;
16 for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
17 str[i] = '*';
18 if (val > max)
19 str[i - 1] = '+';
20 str[i] = '\0';
23 static void print_hist(int fd)
25 int key;
26 long value;
27 long data[MAX_INDEX] = {};
28 char starstr[MAX_STARS];
29 int i;
30 int max_ind = -1;
31 long max_value = 0;
33 for (key = 0; key < MAX_INDEX; key++) {
34 bpf_lookup_elem(fd, &key, &value);
35 data[key] = value;
36 if (value && key > max_ind)
37 max_ind = key;
38 if (value > max_value)
39 max_value = 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],
48 MAX_STARS, starstr);
51 static void int_exit(int sig)
53 print_hist(map_fd[1]);
54 exit(0);
57 int main(int ac, char **argv)
59 char filename[256];
60 long key, next_key, value;
61 FILE *f;
62 int i;
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");
70 (void) f;
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");
74 (void) f;
76 if (load_bpf_file(filename)) {
77 printf("%s", bpf_log_buf);
78 return 1;
81 for (i = 0; i < 5; i++) {
82 key = 0;
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);
86 key = next_key;
88 if (key)
89 printf("\n");
90 sleep(1);
92 print_hist(map_fd[1]);
94 return 0;