^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <sys/resource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "bpf_util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define MAX_INDEX 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define MAX_STARS 38
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /* my_map, my_hist_map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static int map_fd[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static void stars(char *str, long val, long max, int width)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) str[i] = '*';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (val > max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) str[i - 1] = '+';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) str[i] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct task {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) char comm[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) __u64 pid_tgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) __u64 uid_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct hist_key {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct task t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) __u32 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define SIZE sizeof(struct task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void print_hist_for_pid(int fd, void *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned int nr_cpus = bpf_num_possible_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct hist_key key = {}, next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) long values[nr_cpus];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) char starstr[MAX_STARS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) long data[MAX_INDEX] = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int max_ind = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) long max_value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int i, ind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (memcmp(&next_key, task, SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) key = next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) bpf_map_lookup_elem(fd, &next_key, values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) for (i = 0; i < nr_cpus; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) value += values[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ind = next_key.index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) data[ind] = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (value && ind > max_ind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) max_ind = ind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (value > max_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) max_value = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) key = next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) printf(" syscall write() stats\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) printf(" byte_size : count distribution\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) for (i = 1; i <= max_ind + 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) stars(starstr, data[i - 1], max_value, MAX_STARS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) (1l << i) >> 1, (1l << i) - 1, data[i - 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) MAX_STARS, starstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static void print_hist(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct hist_key key = {}, next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static struct task tasks[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int task_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) for (i = 0; i < task_cnt; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (memcmp(&tasks[i], &next_key, SIZE) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) memcpy(&tasks[task_cnt++], &next_key, SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) key = next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) for (i = 0; i < task_cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) printf("\npid %d cmd %s uid %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) (__u32) tasks[i].pid_tgid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) tasks[i].comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) (__u32) tasks[i].uid_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) print_hist_for_pid(fd, &tasks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static void int_exit(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) print_hist(map_fd[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int main(int ac, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) long key, next_key, value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct bpf_link *links[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct bpf_program *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) char filename[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int i, j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (setrlimit(RLIMIT_MEMLOCK, &r)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) perror("setrlimit(RLIMIT_MEMLOCK)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) obj = bpf_object__open_file(filename, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (libbpf_get_error(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) fprintf(stderr, "ERROR: opening BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* load BPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (bpf_object__load(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) fprintf(stderr, "ERROR: loading BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) map_fd[0] = bpf_object__find_map_fd_by_name(obj, "my_map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) map_fd[1] = bpf_object__find_map_fd_by_name(obj, "my_hist_map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (map_fd[0] < 0 || map_fd[1] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) fprintf(stderr, "ERROR: finding a map in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) signal(SIGINT, int_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) signal(SIGTERM, int_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* start 'ping' in the background to have some kfree_skb events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) f = popen("ping -4 -c5 localhost", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) (void) f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* start 'dd' in the background to have plenty of 'write' syscalls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) (void) f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) bpf_object__for_each_program(prog, obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) links[j] = bpf_program__attach(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (libbpf_get_error(links[j])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) fprintf(stderr, "ERROR: bpf_program__attach failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) links[j] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) j++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) for (i = 0; i < 5; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) bpf_map_lookup_elem(map_fd[0], &next_key, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) printf("location 0x%lx count %ld\n", next_key, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) key = next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) sleep(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) print_hist(map_fd[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) for (j--; j >= 0; j--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) bpf_link__destroy(links[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) bpf_object__close(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }