Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 9811e35 | 2015-03-25 12:49:26 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 9811e35 | 2015-03-25 12:49:26 -0700 | [diff] [blame] | 3 | */ |
| 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 Brouer | 55de170 | 2017-05-02 14:31:50 +0200 | [diff] [blame] | 11 | #include <sys/resource.h> |
| 12 | |
Jakub Kicinski | 2bf3e2e | 2018-05-14 22:35:02 -0700 | [diff] [blame] | 13 | #include <bpf/bpf.h> |
Daniel T. Lee | 63841bc | 2020-05-16 13:06:05 +0900 | [diff] [blame] | 14 | #include <bpf/libbpf.h> |
Alexei Starovoitov | 9811e35 | 2015-03-25 12:49:26 -0700 | [diff] [blame] | 15 | |
| 16 | struct pair { |
| 17 | long long val; |
| 18 | __u64 ip; |
| 19 | }; |
| 20 | |
| 21 | static __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 | |
| 29 | static 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. Lee | 63841bc | 2020-05-16 13:06:05 +0900 | [diff] [blame] | 38 | while (bpf_map_get_next_key(fd, &key, &next_key) == 0) { |
| 39 | bpf_map_lookup_elem(fd, &next_key, &v); |
Alexei Starovoitov | 9811e35 | 2015-03-25 12:49:26 -0700 | [diff] [blame] | 40 | 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 | |
| 49 | int main(int ac, char **argv) |
| 50 | { |
Jesper Dangaard Brouer | 55de170 | 2017-05-02 14:31:50 +0200 | [diff] [blame] | 51 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
Daniel T. Lee | 63841bc | 2020-05-16 13:06:05 +0900 | [diff] [blame] | 52 | struct bpf_link *links[2]; |
| 53 | struct bpf_program *prog; |
| 54 | struct bpf_object *obj; |
Alexei Starovoitov | 9811e35 | 2015-03-25 12:49:26 -0700 | [diff] [blame] | 55 | char filename[256]; |
Daniel T. Lee | 63841bc | 2020-05-16 13:06:05 +0900 | [diff] [blame] | 56 | int map_fd, i, j = 0; |
Alexei Starovoitov | 9811e35 | 2015-03-25 12:49:26 -0700 | [diff] [blame] | 57 | |
Jesper Dangaard Brouer | 55de170 | 2017-05-02 14:31:50 +0200 | [diff] [blame] | 58 | if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
| 59 | perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)"); |
| 60 | return 1; |
| 61 | } |
| 62 | |
Daniel T. Lee | 63841bc | 2020-05-16 13:06:05 +0900 | [diff] [blame] | 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"); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | /* load BPF program */ |
| 71 | if (bpf_object__load(obj)) { |
| 72 | fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
| 73 | goto cleanup; |
| 74 | } |
| 75 | |
| 76 | map_fd = bpf_object__find_map_fd_by_name(obj, "my_map"); |
| 77 | if (map_fd < 0) { |
| 78 | fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
| 79 | goto cleanup; |
| 80 | } |
| 81 | |
| 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"); |
| 86 | links[j] = NULL; |
| 87 | goto cleanup; |
| 88 | } |
| 89 | j++; |
Alexei Starovoitov | 9811e35 | 2015-03-25 12:49:26 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | for (i = 0; ; i++) { |
Daniel T. Lee | 63841bc | 2020-05-16 13:06:05 +0900 | [diff] [blame] | 93 | print_old_objects(map_fd); |
Alexei Starovoitov | 9811e35 | 2015-03-25 12:49:26 -0700 | [diff] [blame] | 94 | sleep(1); |
| 95 | } |
| 96 | |
Daniel T. Lee | 63841bc | 2020-05-16 13:06:05 +0900 | [diff] [blame] | 97 | cleanup: |
| 98 | for (j--; j >= 0; j--) |
| 99 | bpf_link__destroy(links[j]); |
| 100 | |
| 101 | bpf_object__close(obj); |
Alexei Starovoitov | 9811e35 | 2015-03-25 12:49:26 -0700 | [diff] [blame] | 102 | return 0; |
| 103 | } |