1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
11 #include <sys/resource.h>
14 #include <bpf/libbpf.h>
21 static __u64 time_get_ns(void)
25 clock_gettime(CLOCK_MONOTONIC, &ts);
26 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
29 static void print_old_objects(int fd)
31 long long val = time_get_ns();
35 key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
38 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
39 bpf_map_lookup_elem(fd, &next_key, &v);
41 if (val - v.val < 1000000000ll)
42 /* object was allocated more then 1 sec ago */
44 printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
45 next_key, (val - v.val) / 1000000000ll, v.ip);
49 int main(int ac, char **argv)
51 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
52 struct bpf_link *links[2];
53 struct bpf_program *prog;
54 struct bpf_object *obj;
58 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
59 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
63 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
64 obj = bpf_object__open_file(filename, NULL);
65 if (libbpf_get_error(obj)) {
66 fprintf(stderr, "ERROR: opening BPF object file failed\n");
70 /* load BPF program */
71 if (bpf_object__load(obj)) {
72 fprintf(stderr, "ERROR: loading BPF object file failed\n");
76 map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
78 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
82 bpf_object__for_each_program(prog, obj) {
83 links[j] = bpf_program__attach(prog);
84 if (libbpf_get_error(links[j])) {
85 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
93 print_old_objects(map_fd);
98 for (j--; j >= 0; j--)
99 bpf_link__destroy(links[j]);
101 bpf_object__close(obj);