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