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 static int duration;
11
12 /* AddressSanitizer sometimes crashes due to data dereference below, due to
13 * this being mmap()'ed memory. Disable instrumentation with
14 * no_sanitize_address attribute
15 */
16 __attribute__((no_sanitize_address))
on_sample(void * ctx,int cpu,void * data,__u32 size)17 static void on_sample(void *ctx, int cpu, void *data, __u32 size)
18 {
19 int cpu_data = *(int *)data, duration = 0;
20 cpu_set_t *cpu_seen = ctx;
21
22 if (cpu_data != cpu)
23 CHECK(cpu_data != cpu, "check_cpu_data",
24 "cpu_data %d != cpu %d\n", cpu_data, cpu);
25
26 CPU_SET(cpu, cpu_seen);
27 }
28
trigger_on_cpu(int cpu)29 int trigger_on_cpu(int cpu)
30 {
31 cpu_set_t cpu_set;
32 int err;
33
34 CPU_ZERO(&cpu_set);
35 CPU_SET(cpu, &cpu_set);
36
37 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set);
38 if (err && CHECK(err, "set_affinity", "cpu #%d, err %d\n", cpu, err))
39 return err;
40
41 usleep(1);
42
43 return 0;
44 }
45
serial_test_perf_buffer(void)46 void serial_test_perf_buffer(void)
47 {
48 int err, on_len, nr_on_cpus = 0, nr_cpus, i, j;
49 int zero = 0, my_pid = getpid();
50 struct perf_buffer_opts pb_opts = {};
51 struct test_perf_buffer *skel;
52 cpu_set_t cpu_seen;
53 struct perf_buffer *pb;
54 int last_fd = -1, fd;
55 bool *online;
56
57 nr_cpus = libbpf_num_possible_cpus();
58 if (CHECK(nr_cpus < 0, "nr_cpus", "err %d\n", nr_cpus))
59 return;
60
61 err = parse_cpu_mask_file("/sys/devices/system/cpu/online",
62 &online, &on_len);
63 if (CHECK(err, "nr_on_cpus", "err %d\n", err))
64 return;
65
66 for (i = 0; i < on_len; i++)
67 if (online[i])
68 nr_on_cpus++;
69
70 /* load program */
71 skel = test_perf_buffer__open_and_load();
72 if (CHECK(!skel, "skel_load", "skeleton open/load failed\n"))
73 goto out_close;
74
75 err = bpf_map_update_elem(bpf_map__fd(skel->maps.my_pid_map), &zero, &my_pid, 0);
76 if (!ASSERT_OK(err, "my_pid_update"))
77 goto out_close;
78
79 /* attach probe */
80 err = test_perf_buffer__attach(skel);
81 if (CHECK(err, "attach_kprobe", "err %d\n", err))
82 goto out_close;
83
84 /* set up perf buffer */
85 pb_opts.sample_cb = on_sample;
86 pb_opts.ctx = &cpu_seen;
87 pb = perf_buffer__new(bpf_map__fd(skel->maps.perf_buf_map), 1, &pb_opts);
88 if (!ASSERT_OK_PTR(pb, "perf_buf__new"))
89 goto out_close;
90
91 CHECK(perf_buffer__epoll_fd(pb) < 0, "epoll_fd",
92 "bad fd: %d\n", perf_buffer__epoll_fd(pb));
93
94 /* trigger kprobe on every CPU */
95 CPU_ZERO(&cpu_seen);
96 for (i = 0; i < nr_cpus; i++) {
97 if (i >= on_len || !online[i]) {
98 printf("skipping offline CPU #%d\n", i);
99 continue;
100 }
101
102 if (trigger_on_cpu(i))
103 goto out_close;
104 }
105
106 /* read perf buffer */
107 err = perf_buffer__poll(pb, 100);
108 if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
109 goto out_free_pb;
110
111 if (CHECK(CPU_COUNT(&cpu_seen) != nr_on_cpus, "seen_cpu_cnt",
112 "expect %d, seen %d\n", nr_on_cpus, CPU_COUNT(&cpu_seen)))
113 goto out_free_pb;
114
115 if (CHECK(perf_buffer__buffer_cnt(pb) != nr_on_cpus, "buf_cnt",
116 "got %zu, expected %d\n", perf_buffer__buffer_cnt(pb), nr_on_cpus))
117 goto out_close;
118
119 for (i = 0, j = 0; i < nr_cpus; i++) {
120 if (i >= on_len || !online[i])
121 continue;
122
123 fd = perf_buffer__buffer_fd(pb, j);
124 CHECK(fd < 0 || last_fd == fd, "fd_check", "last fd %d == fd %d\n", last_fd, fd);
125 last_fd = fd;
126
127 err = perf_buffer__consume_buffer(pb, j);
128 if (CHECK(err, "drain_buf", "cpu %d, err %d\n", i, err))
129 goto out_close;
130
131 CPU_CLR(i, &cpu_seen);
132 if (trigger_on_cpu(i))
133 goto out_close;
134
135 err = perf_buffer__consume_buffer(pb, j);
136 if (CHECK(err, "consume_buf", "cpu %d, err %d\n", j, err))
137 goto out_close;
138
139 if (CHECK(!CPU_ISSET(i, &cpu_seen), "cpu_seen", "cpu %d not seen\n", i))
140 goto out_close;
141 j++;
142 }
143
144 out_free_pb:
145 perf_buffer__free(pb);
146 out_close:
147 test_perf_buffer__destroy(skel);
148 free(online);
149 }
150