1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
13 #include <bpf/libbpf.h>
20 static __u64
time_get_ns(void)
24 clock_gettime(CLOCK_MONOTONIC
, &ts
);
25 return ts
.tv_sec
* 1000000000ull + ts
.tv_nsec
;
28 static void print_old_objects(int fd
)
30 long long val
= time_get_ns();
34 key
= write(1, "\e[1;1H\e[2J", 11); /* clear screen */
37 while (bpf_map_get_next_key(fd
, &key
, &next_key
) == 0) {
38 bpf_map_lookup_elem(fd
, &next_key
, &v
);
40 if (val
- v
.val
< 1000000000ll)
41 /* object was allocated more then 1 sec ago */
43 printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
44 next_key
, (val
- v
.val
) / 1000000000ll, v
.ip
);
48 int main(int ac
, char **argv
)
50 struct bpf_link
*links
[2];
51 struct bpf_program
*prog
;
52 struct bpf_object
*obj
;
56 snprintf(filename
, sizeof(filename
), "%s.bpf.o", argv
[0]);
57 obj
= bpf_object__open_file(filename
, NULL
);
58 if (libbpf_get_error(obj
)) {
59 fprintf(stderr
, "ERROR: opening BPF object file failed\n");
63 /* load BPF program */
64 if (bpf_object__load(obj
)) {
65 fprintf(stderr
, "ERROR: loading BPF object file failed\n");
69 map_fd
= bpf_object__find_map_fd_by_name(obj
, "my_map");
71 fprintf(stderr
, "ERROR: finding a map in obj file failed\n");
75 bpf_object__for_each_program(prog
, obj
) {
76 links
[j
] = bpf_program__attach(prog
);
77 if (libbpf_get_error(links
[j
])) {
78 fprintf(stderr
, "ERROR: bpf_program__attach failed\n");
86 print_old_objects(map_fd
);
91 for (j
--; j
>= 0; j
--)
92 bpf_link__destroy(links
[j
]);
94 bpf_object__close(obj
);