Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2016 Facebook |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <unistd.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <signal.h> |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 8 | #include <linux/perf_event.h> |
| 9 | #include <errno.h> |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 10 | #include <stdbool.h> |
| 11 | #include <sys/resource.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 12 | #include <bpf/libbpf.h> |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 13 | #include <bpf/bpf.h> |
Yonghong Song | 28dbf86 | 2018-04-28 22:28:13 -0700 | [diff] [blame] | 14 | #include "trace_helpers.h" |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 15 | |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 16 | #define PRINT_RAW_ADDR 0 |
| 17 | |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 18 | /* counts, stackmap */ |
| 19 | static int map_fd[2]; |
| 20 | |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 21 | static void print_ksym(__u64 addr) |
| 22 | { |
| 23 | struct ksym *sym; |
| 24 | |
| 25 | if (!addr) |
| 26 | return; |
Alexei Starovoitov | 3622e7e | 2016-03-07 21:57:19 -0800 | [diff] [blame] | 27 | sym = ksym_search(addr); |
Daniel T. Lee | e67b2c7 | 2019-04-04 07:17:56 +0900 | [diff] [blame] | 28 | if (!sym) { |
| 29 | printf("ksym not found. Is kallsyms loaded?\n"); |
| 30 | return; |
| 31 | } |
| 32 | |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 33 | if (PRINT_RAW_ADDR) |
| 34 | printf("%s/%llx;", sym->name, addr); |
| 35 | else |
| 36 | printf("%s;", sym->name); |
| 37 | } |
| 38 | |
| 39 | #define TASK_COMM_LEN 16 |
| 40 | |
| 41 | struct key_t { |
| 42 | char waker[TASK_COMM_LEN]; |
| 43 | char target[TASK_COMM_LEN]; |
| 44 | __u32 wret; |
| 45 | __u32 tret; |
| 46 | }; |
| 47 | |
| 48 | static void print_stack(struct key_t *key, __u64 count) |
| 49 | { |
| 50 | __u64 ip[PERF_MAX_STACK_DEPTH] = {}; |
| 51 | static bool warned; |
| 52 | int i; |
| 53 | |
| 54 | printf("%s;", key->target); |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 55 | if (bpf_map_lookup_elem(map_fd[1], &key->tret, ip) != 0) { |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 56 | printf("---;"); |
| 57 | } else { |
| 58 | for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--) |
| 59 | print_ksym(ip[i]); |
| 60 | } |
| 61 | printf("-;"); |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 62 | if (bpf_map_lookup_elem(map_fd[1], &key->wret, ip) != 0) { |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 63 | printf("---;"); |
| 64 | } else { |
| 65 | for (i = 0; i < PERF_MAX_STACK_DEPTH; i++) |
| 66 | print_ksym(ip[i]); |
| 67 | } |
| 68 | printf(";%s %lld\n", key->waker, count); |
| 69 | |
| 70 | if ((key->tret == -EEXIST || key->wret == -EEXIST) && !warned) { |
| 71 | printf("stackmap collisions seen. Consider increasing size\n"); |
| 72 | warned = true; |
| 73 | } else if (((int)(key->tret) < 0 || (int)(key->wret) < 0)) { |
| 74 | printf("err stackid %d %d\n", key->tret, key->wret); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static void print_stacks(int fd) |
| 79 | { |
| 80 | struct key_t key = {}, next_key; |
| 81 | __u64 value; |
| 82 | |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 83 | while (bpf_map_get_next_key(fd, &key, &next_key) == 0) { |
| 84 | bpf_map_lookup_elem(fd, &next_key, &value); |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 85 | print_stack(&next_key, value); |
| 86 | key = next_key; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void int_exit(int sig) |
| 91 | { |
| 92 | print_stacks(map_fd[0]); |
| 93 | exit(0); |
| 94 | } |
| 95 | |
| 96 | int main(int argc, char **argv) |
| 97 | { |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 98 | struct bpf_object *obj = NULL; |
| 99 | struct bpf_link *links[2]; |
| 100 | struct bpf_program *prog; |
| 101 | int delay = 1, i = 0; |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 102 | char filename[256]; |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 103 | |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 104 | if (load_kallsyms()) { |
| 105 | printf("failed to process /proc/kallsyms\n"); |
| 106 | return 2; |
| 107 | } |
| 108 | |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 109 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
| 110 | obj = bpf_object__open_file(filename, NULL); |
| 111 | if (libbpf_get_error(obj)) { |
| 112 | fprintf(stderr, "ERROR: opening BPF object file failed\n"); |
| 113 | obj = NULL; |
| 114 | goto cleanup; |
| 115 | } |
| 116 | |
| 117 | /* load BPF program */ |
| 118 | if (bpf_object__load(obj)) { |
| 119 | fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
| 120 | goto cleanup; |
| 121 | } |
| 122 | |
| 123 | map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counts"); |
| 124 | map_fd[1] = bpf_object__find_map_fd_by_name(obj, "stackmap"); |
| 125 | if (map_fd[0] < 0 || map_fd[1] < 0) { |
| 126 | fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
| 127 | goto cleanup; |
| 128 | } |
| 129 | |
| 130 | signal(SIGINT, int_exit); |
| 131 | signal(SIGTERM, int_exit); |
| 132 | |
| 133 | bpf_object__for_each_program(prog, obj) { |
| 134 | links[i] = bpf_program__attach(prog); |
| 135 | if (libbpf_get_error(links[i])) { |
| 136 | fprintf(stderr, "ERROR: bpf_program__attach failed\n"); |
| 137 | links[i] = NULL; |
| 138 | goto cleanup; |
| 139 | } |
| 140 | i++; |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (argc > 1) |
| 144 | delay = atoi(argv[1]); |
| 145 | sleep(delay); |
| 146 | print_stacks(map_fd[0]); |
| 147 | |
Daniel T. Lee | f0c328f | 2020-08-23 17:53:34 +0900 | [diff] [blame] | 148 | cleanup: |
| 149 | for (i--; i >= 0; i--) |
| 150 | bpf_link__destroy(links[i]); |
| 151 | |
| 152 | bpf_object__close(obj); |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 153 | return 0; |
| 154 | } |