Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 2 | /* |
| 3 | * sampleip: sample instruction pointer and frequency count in a BPF map. |
| 4 | * |
| 5 | * Copyright 2016 Netflix, Inc. |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 9 | #include <unistd.h> |
| 10 | #include <errno.h> |
| 11 | #include <signal.h> |
| 12 | #include <string.h> |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 13 | #include <linux/perf_event.h> |
| 14 | #include <linux/ptrace.h> |
| 15 | #include <linux/bpf.h> |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 16 | #include <bpf/bpf.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 17 | #include <bpf/libbpf.h> |
Joe Stringer | 205c8ad | 2016-12-08 18:46:19 -0800 | [diff] [blame] | 18 | #include "perf-sys.h" |
Yonghong Song | 28dbf86 | 2018-04-28 22:28:13 -0700 | [diff] [blame] | 19 | #include "trace_helpers.h" |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 20 | |
| 21 | #define DEFAULT_FREQ 99 |
| 22 | #define DEFAULT_SECS 5 |
| 23 | #define MAX_IPS 8192 |
| 24 | #define PAGE_OFFSET 0xffff880000000000 |
| 25 | |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 26 | static int map_fd; |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 27 | static int nr_cpus; |
| 28 | |
| 29 | static void usage(void) |
| 30 | { |
| 31 | printf("USAGE: sampleip [-F freq] [duration]\n"); |
| 32 | printf(" -F freq # sample frequency (Hertz), default 99\n"); |
| 33 | printf(" duration # sampling duration (seconds), default 5\n"); |
| 34 | } |
| 35 | |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 36 | static int sampling_start(int freq, struct bpf_program *prog, |
| 37 | struct bpf_link *links[]) |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 38 | { |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 39 | int i, pmu_fd; |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 40 | |
| 41 | struct perf_event_attr pe_sample_attr = { |
| 42 | .type = PERF_TYPE_SOFTWARE, |
| 43 | .freq = 1, |
| 44 | .sample_period = freq, |
| 45 | .config = PERF_COUNT_SW_CPU_CLOCK, |
| 46 | .inherit = 1, |
| 47 | }; |
| 48 | |
| 49 | for (i = 0; i < nr_cpus; i++) { |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 50 | pmu_fd = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i, |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 51 | -1 /* group_fd */, 0 /* flags */); |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 52 | if (pmu_fd < 0) { |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 53 | fprintf(stderr, "ERROR: Initializing perf sampling\n"); |
| 54 | return 1; |
| 55 | } |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 56 | links[i] = bpf_program__attach_perf_event(prog, pmu_fd); |
Daniel T. Lee | 0efdcef | 2020-05-16 13:06:04 +0900 | [diff] [blame] | 57 | if (libbpf_get_error(links[i])) { |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 58 | fprintf(stderr, "ERROR: Attach perf event\n"); |
| 59 | links[i] = NULL; |
| 60 | close(pmu_fd); |
| 61 | return 1; |
| 62 | } |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 68 | static void sampling_end(struct bpf_link *links[]) |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 69 | { |
| 70 | int i; |
| 71 | |
| 72 | for (i = 0; i < nr_cpus; i++) |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 73 | bpf_link__destroy(links[i]); |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | struct ipcount { |
| 77 | __u64 ip; |
| 78 | __u32 count; |
| 79 | }; |
| 80 | |
| 81 | /* used for sorting */ |
| 82 | struct ipcount counts[MAX_IPS]; |
| 83 | |
| 84 | static int count_cmp(const void *p1, const void *p2) |
| 85 | { |
| 86 | return ((struct ipcount *)p1)->count - ((struct ipcount *)p2)->count; |
| 87 | } |
| 88 | |
| 89 | static void print_ip_map(int fd) |
| 90 | { |
| 91 | struct ksym *sym; |
| 92 | __u64 key, next_key; |
| 93 | __u32 value; |
| 94 | int i, max; |
| 95 | |
| 96 | printf("%-19s %-32s %s\n", "ADDR", "KSYM", "COUNT"); |
| 97 | |
| 98 | /* fetch IPs and counts */ |
| 99 | key = 0, i = 0; |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 100 | while (bpf_map_get_next_key(fd, &key, &next_key) == 0) { |
| 101 | bpf_map_lookup_elem(fd, &next_key, &value); |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 102 | counts[i].ip = next_key; |
| 103 | counts[i++].count = value; |
| 104 | key = next_key; |
| 105 | } |
| 106 | max = i; |
| 107 | |
| 108 | /* sort and print */ |
| 109 | qsort(counts, max, sizeof(struct ipcount), count_cmp); |
| 110 | for (i = 0; i < max; i++) { |
| 111 | if (counts[i].ip > PAGE_OFFSET) { |
| 112 | sym = ksym_search(counts[i].ip); |
Daniel T. Lee | e67b2c7 | 2019-04-04 07:17:56 +0900 | [diff] [blame] | 113 | if (!sym) { |
| 114 | printf("ksym not found. Is kallsyms loaded?\n"); |
| 115 | continue; |
| 116 | } |
| 117 | |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 118 | printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name, |
| 119 | counts[i].count); |
| 120 | } else { |
| 121 | printf("0x%-17llx %-32s %u\n", counts[i].ip, "(user)", |
| 122 | counts[i].count); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (max == MAX_IPS) { |
| 127 | printf("WARNING: IP hash was full (max %d entries); ", max); |
| 128 | printf("may have dropped samples\n"); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static void int_exit(int sig) |
| 133 | { |
| 134 | printf("\n"); |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 135 | print_ip_map(map_fd); |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 136 | exit(0); |
| 137 | } |
| 138 | |
| 139 | int main(int argc, char **argv) |
| 140 | { |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 141 | int opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS, error = 1; |
| 142 | struct bpf_object *obj = NULL; |
| 143 | struct bpf_program *prog; |
| 144 | struct bpf_link **links; |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 145 | char filename[256]; |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 146 | |
| 147 | /* process arguments */ |
| 148 | while ((opt = getopt(argc, argv, "F:h")) != -1) { |
| 149 | switch (opt) { |
| 150 | case 'F': |
| 151 | freq = atoi(optarg); |
| 152 | break; |
| 153 | case 'h': |
| 154 | default: |
| 155 | usage(); |
| 156 | return 0; |
| 157 | } |
| 158 | } |
| 159 | if (argc - optind == 1) |
| 160 | secs = atoi(argv[optind]); |
| 161 | if (freq == 0 || secs == 0) { |
| 162 | usage(); |
| 163 | return 1; |
| 164 | } |
| 165 | |
| 166 | /* initialize kernel symbol translation */ |
| 167 | if (load_kallsyms()) { |
| 168 | fprintf(stderr, "ERROR: loading /proc/kallsyms\n"); |
| 169 | return 2; |
| 170 | } |
| 171 | |
| 172 | /* create perf FDs for each CPU */ |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 173 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 174 | links = calloc(nr_cpus, sizeof(struct bpf_link *)); |
| 175 | if (!links) { |
| 176 | fprintf(stderr, "ERROR: malloc of links\n"); |
| 177 | goto cleanup; |
| 178 | } |
| 179 | |
| 180 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
| 181 | obj = bpf_object__open_file(filename, NULL); |
Daniel T. Lee | 0efdcef | 2020-05-16 13:06:04 +0900 | [diff] [blame] | 182 | if (libbpf_get_error(obj)) { |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 183 | fprintf(stderr, "ERROR: opening BPF object file failed\n"); |
| 184 | obj = NULL; |
| 185 | goto cleanup; |
| 186 | } |
| 187 | |
| 188 | prog = bpf_object__find_program_by_name(obj, "do_sample"); |
| 189 | if (!prog) { |
| 190 | fprintf(stderr, "ERROR: finding a prog in obj file failed\n"); |
| 191 | goto cleanup; |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* load BPF program */ |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 195 | if (bpf_object__load(obj)) { |
| 196 | fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
| 197 | goto cleanup; |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 198 | } |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 199 | |
| 200 | map_fd = bpf_object__find_map_fd_by_name(obj, "ip_map"); |
| 201 | if (map_fd < 0) { |
| 202 | fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
| 203 | goto cleanup; |
| 204 | } |
| 205 | |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 206 | signal(SIGINT, int_exit); |
Andy Gospodarek | ad990db | 2017-05-11 15:52:30 -0400 | [diff] [blame] | 207 | signal(SIGTERM, int_exit); |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 208 | |
| 209 | /* do sampling */ |
| 210 | printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n", |
| 211 | freq, secs); |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 212 | if (sampling_start(freq, prog, links) != 0) |
| 213 | goto cleanup; |
| 214 | |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 215 | sleep(secs); |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 216 | error = 0; |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 217 | |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 218 | cleanup: |
| 219 | sampling_end(links); |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 220 | /* output sample counts */ |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 221 | if (!error) |
| 222 | print_ip_map(map_fd); |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 223 | |
Daniel T. Lee | aa5e2af | 2020-03-21 19:04:24 +0900 | [diff] [blame] | 224 | free(links); |
| 225 | bpf_object__close(obj); |
| 226 | return error; |
Brendan Gregg | 7287441 | 2016-09-01 18:37:26 -0700 | [diff] [blame] | 227 | } |