1 // SPDX-License-Identifier: GPL-2.0 2 #define _GNU_SOURCE 3 #include <pthread.h> 4 #include <sched.h> 5 #include <sys/socket.h> 6 #include <test_progs.h> 7 #include "test_perf_buffer.skel.h" 8 #include "bpf/libbpf_internal.h" 9 10 /* AddressSanitizer sometimes crashes due to data dereference below, due to 11 * this being mmap()'ed memory. Disable instrumentation with 12 * no_sanitize_address attribute 13 */ 14 __attribute__((no_sanitize_address)) 15 static void on_sample(void *ctx, int cpu, void *data, __u32 size) 16 { 17 int cpu_data = *(int *)data, duration = 0; 18 cpu_set_t *cpu_seen = ctx; 19 20 if (cpu_data != cpu) 21 CHECK(cpu_data != cpu, "check_cpu_data", 22 "cpu_data %d != cpu %d\n", cpu_data, cpu); 23 24 CPU_SET(cpu, cpu_seen); 25 } 26 27 void test_perf_buffer(void) 28 { 29 int err, on_len, nr_on_cpus = 0, nr_cpus, i, duration = 0; 30 struct perf_buffer_opts pb_opts = {}; 31 struct test_perf_buffer *skel; 32 cpu_set_t cpu_set, cpu_seen; 33 struct perf_buffer *pb; 34 bool *online; 35 36 nr_cpus = libbpf_num_possible_cpus(); 37 if (CHECK(nr_cpus < 0, "nr_cpus", "err %d\n", nr_cpus)) 38 return; 39 40 err = parse_cpu_mask_file("/sys/devices/system/cpu/online", 41 &online, &on_len); 42 if (CHECK(err, "nr_on_cpus", "err %d\n", err)) 43 return; 44 45 for (i = 0; i < on_len; i++) 46 if (online[i]) 47 nr_on_cpus++; 48 49 /* load program */ 50 skel = test_perf_buffer__open_and_load(); 51 if (CHECK(!skel, "skel_load", "skeleton open/load failed\n")) 52 goto out_close; 53 54 /* attach probe */ 55 err = test_perf_buffer__attach(skel); 56 if (CHECK(err, "attach_kprobe", "err %d\n", err)) 57 goto out_close; 58 59 /* set up perf buffer */ 60 pb_opts.sample_cb = on_sample; 61 pb_opts.ctx = &cpu_seen; 62 pb = perf_buffer__new(bpf_map__fd(skel->maps.perf_buf_map), 1, &pb_opts); 63 if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb))) 64 goto out_close; 65 66 /* trigger kprobe on every CPU */ 67 CPU_ZERO(&cpu_seen); 68 for (i = 0; i < nr_cpus; i++) { 69 if (i >= on_len || !online[i]) { 70 printf("skipping offline CPU #%d\n", i); 71 continue; 72 } 73 74 CPU_ZERO(&cpu_set); 75 CPU_SET(i, &cpu_set); 76 77 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), 78 &cpu_set); 79 if (err && CHECK(err, "set_affinity", "cpu #%d, err %d\n", 80 i, err)) 81 goto out_close; 82 83 usleep(1); 84 } 85 86 /* read perf buffer */ 87 err = perf_buffer__poll(pb, 100); 88 if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err)) 89 goto out_free_pb; 90 91 if (CHECK(CPU_COUNT(&cpu_seen) != nr_on_cpus, "seen_cpu_cnt", 92 "expect %d, seen %d\n", nr_on_cpus, CPU_COUNT(&cpu_seen))) 93 goto out_free_pb; 94 95 out_free_pb: 96 perf_buffer__free(pb); 97 out_close: 98 test_perf_buffer__destroy(skel); 99 free(online); 100 } 101