1 // SPDX-License-Identifier: GPL-2.0
9 #include <bpf/libbpf.h>
15 /* my_map, my_hist_map */
18 static void stars(char *str, long val, long max, int width)
22 for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
40 #define SIZE sizeof(struct task)
42 static void print_hist_for_pid(int fd, void *task)
44 unsigned int nr_cpus = bpf_num_possible_cpus();
45 struct hist_key key = {}, next_key;
47 char starstr[MAX_STARS];
49 long data[MAX_INDEX] = {};
54 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
55 if (memcmp(&next_key, task, SIZE)) {
59 bpf_map_lookup_elem(fd, &next_key, values);
61 for (i = 0; i < nr_cpus; i++)
65 if (value && ind > max_ind)
67 if (value > max_value)
72 printf(" syscall write() stats\n");
73 printf(" byte_size : count distribution\n");
74 for (i = 1; i <= max_ind + 1; i++) {
75 stars(starstr, data[i - 1], max_value, MAX_STARS);
76 printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
77 (1l << i) >> 1, (1l << i) - 1, data[i - 1],
82 static void print_hist(int fd)
84 struct hist_key key = {}, next_key;
85 static struct task tasks[1024];
89 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
92 for (i = 0; i < task_cnt; i++)
93 if (memcmp(&tasks[i], &next_key, SIZE) == 0)
96 memcpy(&tasks[task_cnt++], &next_key, SIZE);
100 for (i = 0; i < task_cnt; i++) {
101 printf("\npid %d cmd %s uid %d\n",
102 (__u32) tasks[i].pid_tgid,
104 (__u32) tasks[i].uid_gid);
105 print_hist_for_pid(fd, &tasks[i]);
110 static void int_exit(int sig)
112 print_hist(map_fd[1]);
116 int main(int ac, char **argv)
118 long key, next_key, value;
119 struct bpf_link *links[2];
120 struct bpf_program *prog;
121 struct bpf_object *obj;
126 snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
127 obj = bpf_object__open_file(filename, NULL);
128 if (libbpf_get_error(obj)) {
129 fprintf(stderr, "ERROR: opening BPF object file failed\n");
133 /* load BPF program */
134 if (bpf_object__load(obj)) {
135 fprintf(stderr, "ERROR: loading BPF object file failed\n");
139 map_fd[0] = bpf_object__find_map_fd_by_name(obj, "my_map");
140 map_fd[1] = bpf_object__find_map_fd_by_name(obj, "my_hist_map");
141 if (map_fd[0] < 0 || map_fd[1] < 0) {
142 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
146 signal(SIGINT, int_exit);
147 signal(SIGTERM, int_exit);
149 /* start 'ping' in the background to have some kfree_skb_reason
151 f = popen("ping -4 -c5 localhost", "r");
154 /* start 'dd' in the background to have plenty of 'write' syscalls */
155 f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
158 bpf_object__for_each_program(prog, obj) {
159 links[j] = bpf_program__attach(prog);
160 if (libbpf_get_error(links[j])) {
161 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
168 for (i = 0; i < 5; i++) {
170 while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
171 bpf_map_lookup_elem(map_fd[0], &next_key, &value);
172 printf("location 0x%lx count %ld\n", next_key, value);
179 print_hist(map_fd[1]);
182 for (j--; j >= 0; j--)
183 bpf_link__destroy(links[j]);
185 bpf_object__close(obj);