1 // SPDX-License-Identifier: GPL-2.0
7 #include <sys/resource.h>
10 #include <bpf/libbpf.h>
16 /* my_map, my_hist_map */
19 static void stars(char *str, long val, long max, int width)
23 for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
41 #define SIZE sizeof(struct task)
43 static void print_hist_for_pid(int fd, void *task)
45 unsigned int nr_cpus = bpf_num_possible_cpus();
46 struct hist_key key = {}, next_key;
48 char starstr[MAX_STARS];
50 long data[MAX_INDEX] = {};
55 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
56 if (memcmp(&next_key, task, SIZE)) {
60 bpf_map_lookup_elem(fd, &next_key, values);
62 for (i = 0; i < nr_cpus; i++)
66 if (value && ind > max_ind)
68 if (value > max_value)
73 printf(" syscall write() stats\n");
74 printf(" byte_size : count distribution\n");
75 for (i = 1; i <= max_ind + 1; i++) {
76 stars(starstr, data[i - 1], max_value, MAX_STARS);
77 printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
78 (1l << i) >> 1, (1l << i) - 1, data[i - 1],
83 static void print_hist(int fd)
85 struct hist_key key = {}, next_key;
86 static struct task tasks[1024];
90 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
93 for (i = 0; i < task_cnt; i++)
94 if (memcmp(&tasks[i], &next_key, SIZE) == 0)
97 memcpy(&tasks[task_cnt++], &next_key, SIZE);
101 for (i = 0; i < task_cnt; i++) {
102 printf("\npid %d cmd %s uid %d\n",
103 (__u32) tasks[i].pid_tgid,
105 (__u32) tasks[i].uid_gid);
106 print_hist_for_pid(fd, &tasks[i]);
111 static void int_exit(int sig)
113 print_hist(map_fd[1]);
117 int main(int ac, char **argv)
119 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
120 long key, next_key, value;
121 struct bpf_link *links[2];
122 struct bpf_program *prog;
123 struct bpf_object *obj;
128 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
129 perror("setrlimit(RLIMIT_MEMLOCK)");
133 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
134 obj = bpf_object__open_file(filename, NULL);
135 if (libbpf_get_error(obj)) {
136 fprintf(stderr, "ERROR: opening BPF object file failed\n");
140 /* load BPF program */
141 if (bpf_object__load(obj)) {
142 fprintf(stderr, "ERROR: loading BPF object file failed\n");
146 map_fd[0] = bpf_object__find_map_fd_by_name(obj, "my_map");
147 map_fd[1] = bpf_object__find_map_fd_by_name(obj, "my_hist_map");
148 if (map_fd[0] < 0 || map_fd[1] < 0) {
149 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
153 signal(SIGINT, int_exit);
154 signal(SIGTERM, int_exit);
156 /* start 'ping' in the background to have some kfree_skb events */
157 f = popen("ping -4 -c5 localhost", "r");
160 /* start 'dd' in the background to have plenty of 'write' syscalls */
161 f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
164 bpf_object__for_each_program(prog, obj) {
165 links[j] = bpf_program__attach(prog);
166 if (libbpf_get_error(links[j])) {
167 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
174 for (i = 0; i < 5; i++) {
176 while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
177 bpf_map_lookup_elem(map_fd[0], &next_key, &value);
178 printf("location 0x%lx count %ld\n", next_key, value);
185 print_hist(map_fd[1]);
188 for (j--; j >= 0; j--)
189 bpf_link__destroy(links[j]);
191 bpf_object__close(obj);