1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
11 #include <linux/bpf.h>
12 #include <sys/resource.h>
22 static __u64 time_get_ns(void)
26 clock_gettime(CLOCK_MONOTONIC, &ts);
27 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
30 static void print_old_objects(int fd)
32 long long val = time_get_ns();
36 key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
39 while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
40 bpf_map_lookup_elem(map_fd[0], &next_key, &v);
42 if (val - v.val < 1000000000ll)
43 /* object was allocated more then 1 sec ago */
45 printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
46 next_key, (val - v.val) / 1000000000ll, v.ip);
50 int main(int ac, char **argv)
52 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
56 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
58 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
59 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
63 if (load_bpf_file(filename)) {
64 printf("%s", bpf_log_buf);
69 print_old_objects(map_fd[1]);