]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - samples/bpf/tracex2_user.c
Merge remote-tracking branch 'regulator/fix/max77802' into regulator-linus
[mirror_ubuntu-artful-kernel.git] / 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 <string.h>
7 #include <sys/resource.h>
8
9 #include "libbpf.h"
10 #include "bpf_load.h"
11 #include "bpf_util.h"
12
13 #define MAX_INDEX 64
14 #define MAX_STARS 38
15
16 static void stars(char *str, long val, long max, int width)
17 {
18 int i;
19
20 for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
21 str[i] = '*';
22 if (val > max)
23 str[i - 1] = '+';
24 str[i] = '\0';
25 }
26
27 struct task {
28 char comm[16];
29 __u64 pid_tgid;
30 __u64 uid_gid;
31 };
32
33 struct hist_key {
34 struct task t;
35 __u32 index;
36 };
37
38 #define SIZE sizeof(struct task)
39
40 static void print_hist_for_pid(int fd, void *task)
41 {
42 unsigned int nr_cpus = bpf_num_possible_cpus();
43 struct hist_key key = {}, next_key;
44 long values[nr_cpus];
45 char starstr[MAX_STARS];
46 long value;
47 long data[MAX_INDEX] = {};
48 int max_ind = -1;
49 long max_value = 0;
50 int i, ind;
51
52 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
53 if (memcmp(&next_key, task, SIZE)) {
54 key = next_key;
55 continue;
56 }
57 bpf_map_lookup_elem(fd, &next_key, values);
58 value = 0;
59 for (i = 0; i < nr_cpus; i++)
60 value += values[i];
61 ind = next_key.index;
62 data[ind] = value;
63 if (value && ind > max_ind)
64 max_ind = ind;
65 if (value > max_value)
66 max_value = value;
67 key = next_key;
68 }
69
70 printf(" syscall write() stats\n");
71 printf(" byte_size : count distribution\n");
72 for (i = 1; i <= max_ind + 1; i++) {
73 stars(starstr, data[i - 1], max_value, MAX_STARS);
74 printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
75 (1l << i) >> 1, (1l << i) - 1, data[i - 1],
76 MAX_STARS, starstr);
77 }
78 }
79
80 static void print_hist(int fd)
81 {
82 struct hist_key key = {}, next_key;
83 static struct task tasks[1024];
84 int task_cnt = 0;
85 int i;
86
87 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
88 int found = 0;
89
90 for (i = 0; i < task_cnt; i++)
91 if (memcmp(&tasks[i], &next_key, SIZE) == 0)
92 found = 1;
93 if (!found)
94 memcpy(&tasks[task_cnt++], &next_key, SIZE);
95 key = next_key;
96 }
97
98 for (i = 0; i < task_cnt; i++) {
99 printf("\npid %d cmd %s uid %d\n",
100 (__u32) tasks[i].pid_tgid,
101 tasks[i].comm,
102 (__u32) tasks[i].uid_gid);
103 print_hist_for_pid(fd, &tasks[i]);
104 }
105
106 }
107
108 static void int_exit(int sig)
109 {
110 print_hist(map_fd[1]);
111 exit(0);
112 }
113
114 int main(int ac, char **argv)
115 {
116 struct rlimit r = {1024*1024, RLIM_INFINITY};
117 char filename[256];
118 long key, next_key, value;
119 FILE *f;
120 int i;
121
122 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
123
124 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
125 perror("setrlimit(RLIMIT_MEMLOCK)");
126 return 1;
127 }
128
129 signal(SIGINT, int_exit);
130 signal(SIGTERM, int_exit);
131
132 /* start 'ping' in the background to have some kfree_skb events */
133 f = popen("ping -c5 localhost", "r");
134 (void) f;
135
136 /* start 'dd' in the background to have plenty of 'write' syscalls */
137 f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
138 (void) f;
139
140 if (load_bpf_file(filename)) {
141 printf("%s", bpf_log_buf);
142 return 1;
143 }
144
145 for (i = 0; i < 5; i++) {
146 key = 0;
147 while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
148 bpf_map_lookup_elem(map_fd[0], &next_key, &value);
149 printf("location 0x%lx count %ld\n", next_key, value);
150 key = next_key;
151 }
152 if (key)
153 printf("\n");
154 sleep(1);
155 }
156 print_hist(map_fd[1]);
157
158 return 0;
159 }