Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / samples / bpf / tracex2_user.c
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"
8
9 #define MAX_INDEX       64
10 #define MAX_STARS       38
11
12 static void stars(char *str, long val, long max, int width)
13 {
14         int i;
15
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';
21 }
22
23 static void print_hist(int fd)
24 {
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;
32
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;
40         }
41
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);
49         }
50 }
51 static void int_exit(int sig)
52 {
53         print_hist(map_fd[1]);
54         exit(0);
55 }
56
57 int main(int ac, char **argv)
58 {
59         char filename[256];
60         long key, next_key, value;
61         FILE *f;
62         int i;
63
64         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
65
66         signal(SIGINT, int_exit);
67
68         /* start 'ping' in the background to have some kfree_skb events */
69         f = popen("ping -c5 localhost", "r");
70         (void) f;
71
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;
75
76         if (load_bpf_file(filename)) {
77                 printf("%s", bpf_log_buf);
78                 return 1;
79         }
80
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;
87                 }
88                 if (key)
89                         printf("\n");
90                 sleep(1);
91         }
92         print_hist(map_fd[1]);
93
94         return 0;
95 }