blob: cea399424bca9ee020605429f814d59a9c2bf7b8 [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexei Starovoitov9811e352015-03-25 12:49:26 -07002/* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
Alexei Starovoitov9811e352015-03-25 12:49:26 -07003 */
4#include <stdio.h>
5#include <stdlib.h>
6#include <signal.h>
7#include <unistd.h>
8#include <stdbool.h>
9#include <string.h>
10#include <time.h>
Jesper Dangaard Brouer55de1702017-05-02 14:31:50 +020011#include <sys/resource.h>
12
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070013#include <bpf/bpf.h>
Daniel T. Lee63841bc2020-05-16 13:06:05 +090014#include <bpf/libbpf.h>
Alexei Starovoitov9811e352015-03-25 12:49:26 -070015
16struct pair {
17 long long val;
18 __u64 ip;
19};
20
21static __u64 time_get_ns(void)
22{
23 struct timespec ts;
24
25 clock_gettime(CLOCK_MONOTONIC, &ts);
26 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
27}
28
29static void print_old_objects(int fd)
30{
31 long long val = time_get_ns();
32 __u64 key, next_key;
33 struct pair v;
34
35 key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
36
37 key = -1;
Daniel T. Lee63841bc2020-05-16 13:06:05 +090038 while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
39 bpf_map_lookup_elem(fd, &next_key, &v);
Alexei Starovoitov9811e352015-03-25 12:49:26 -070040 key = next_key;
41 if (val - v.val < 1000000000ll)
42 /* object was allocated more then 1 sec ago */
43 continue;
44 printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
45 next_key, (val - v.val) / 1000000000ll, v.ip);
46 }
47}
48
49int main(int ac, char **argv)
50{
Daniel T. Lee63841bc2020-05-16 13:06:05 +090051 struct bpf_link *links[2];
52 struct bpf_program *prog;
53 struct bpf_object *obj;
Alexei Starovoitov9811e352015-03-25 12:49:26 -070054 char filename[256];
Daniel T. Lee63841bc2020-05-16 13:06:05 +090055 int map_fd, i, j = 0;
Alexei Starovoitov9811e352015-03-25 12:49:26 -070056
Daniel T. Lee63841bc2020-05-16 13:06:05 +090057 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
58 obj = bpf_object__open_file(filename, NULL);
59 if (libbpf_get_error(obj)) {
60 fprintf(stderr, "ERROR: opening BPF object file failed\n");
61 return 0;
62 }
63
64 /* load BPF program */
65 if (bpf_object__load(obj)) {
66 fprintf(stderr, "ERROR: loading BPF object file failed\n");
67 goto cleanup;
68 }
69
70 map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
71 if (map_fd < 0) {
72 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
73 goto cleanup;
74 }
75
76 bpf_object__for_each_program(prog, obj) {
77 links[j] = bpf_program__attach(prog);
78 if (libbpf_get_error(links[j])) {
79 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
80 links[j] = NULL;
81 goto cleanup;
82 }
83 j++;
Alexei Starovoitov9811e352015-03-25 12:49:26 -070084 }
85
86 for (i = 0; ; i++) {
Daniel T. Lee63841bc2020-05-16 13:06:05 +090087 print_old_objects(map_fd);
Alexei Starovoitov9811e352015-03-25 12:49:26 -070088 sleep(1);
89 }
90
Daniel T. Lee63841bc2020-05-16 13:06:05 +090091cleanup:
92 for (j--; j >= 0; j--)
93 bpf_link__destroy(links[j]);
94
95 bpf_object__close(obj);
Alexei Starovoitov9811e352015-03-25 12:49:26 -070096 return 0;
97}