xref: /openbmc/linux/tools/testing/selftests/bpf/prog_tests/perf_buffer.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
1ee5cf82cSAndrii Nakryiko // SPDX-License-Identifier: GPL-2.0
2ee5cf82cSAndrii Nakryiko #define _GNU_SOURCE
3ee5cf82cSAndrii Nakryiko #include <pthread.h>
4ee5cf82cSAndrii Nakryiko #include <sched.h>
5ee5cf82cSAndrii Nakryiko #include <sys/socket.h>
6ee5cf82cSAndrii Nakryiko #include <test_progs.h>
76984cbc6SAndrii Nakryiko #include "test_perf_buffer.skel.h"
83e689141SToke Høiland-Jørgensen #include "bpf/libbpf_internal.h"
9ee5cf82cSAndrii Nakryiko 
10dca5612fSAndrii Nakryiko static int duration;
11dca5612fSAndrii Nakryiko 
1236d0b615SAndrii Nakryiko /* AddressSanitizer sometimes crashes due to data dereference below, due to
1336d0b615SAndrii Nakryiko  * this being mmap()'ed memory. Disable instrumentation with
1436d0b615SAndrii Nakryiko  * no_sanitize_address attribute
1536d0b615SAndrii Nakryiko  */
1636d0b615SAndrii Nakryiko __attribute__((no_sanitize_address))
on_sample(void * ctx,int cpu,void * data,__u32 size)17ee5cf82cSAndrii Nakryiko static void on_sample(void *ctx, int cpu, void *data, __u32 size)
18ee5cf82cSAndrii Nakryiko {
19ee5cf82cSAndrii Nakryiko 	int cpu_data = *(int *)data, duration = 0;
20ee5cf82cSAndrii Nakryiko 	cpu_set_t *cpu_seen = ctx;
21ee5cf82cSAndrii Nakryiko 
22ee5cf82cSAndrii Nakryiko 	if (cpu_data != cpu)
23ee5cf82cSAndrii Nakryiko 		CHECK(cpu_data != cpu, "check_cpu_data",
24ee5cf82cSAndrii Nakryiko 		      "cpu_data %d != cpu %d\n", cpu_data, cpu);
25ee5cf82cSAndrii Nakryiko 
26ee5cf82cSAndrii Nakryiko 	CPU_SET(cpu, cpu_seen);
27ee5cf82cSAndrii Nakryiko }
28ee5cf82cSAndrii Nakryiko 
trigger_on_cpu(int cpu)29dca5612fSAndrii Nakryiko int trigger_on_cpu(int cpu)
30dca5612fSAndrii Nakryiko {
31dca5612fSAndrii Nakryiko 	cpu_set_t cpu_set;
32dca5612fSAndrii Nakryiko 	int err;
33dca5612fSAndrii Nakryiko 
34dca5612fSAndrii Nakryiko 	CPU_ZERO(&cpu_set);
35dca5612fSAndrii Nakryiko 	CPU_SET(cpu, &cpu_set);
36dca5612fSAndrii Nakryiko 
37dca5612fSAndrii Nakryiko 	err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set);
38dca5612fSAndrii Nakryiko 	if (err && CHECK(err, "set_affinity", "cpu #%d, err %d\n", cpu, err))
39dca5612fSAndrii Nakryiko 		return err;
40dca5612fSAndrii Nakryiko 
41dca5612fSAndrii Nakryiko 	usleep(1);
42dca5612fSAndrii Nakryiko 
43dca5612fSAndrii Nakryiko 	return 0;
44dca5612fSAndrii Nakryiko }
45dca5612fSAndrii Nakryiko 
serial_test_perf_buffer(void)46d3f7b166SYucong Sun void serial_test_perf_buffer(void)
47ee5cf82cSAndrii Nakryiko {
48aa274f98SJiri Olsa 	int err, on_len, nr_on_cpus = 0, nr_cpus, i, j;
4957385ae3SAndrii Nakryiko 	int zero = 0, my_pid = getpid();
506984cbc6SAndrii Nakryiko 	struct test_perf_buffer *skel;
51dca5612fSAndrii Nakryiko 	cpu_set_t cpu_seen;
52ee5cf82cSAndrii Nakryiko 	struct perf_buffer *pb;
53dca5612fSAndrii Nakryiko 	int last_fd = -1, fd;
5491cbdf74SAndrii Nakryiko 	bool *online;
55ee5cf82cSAndrii Nakryiko 
56ee5cf82cSAndrii Nakryiko 	nr_cpus = libbpf_num_possible_cpus();
57ee5cf82cSAndrii Nakryiko 	if (CHECK(nr_cpus < 0, "nr_cpus", "err %d\n", nr_cpus))
58ee5cf82cSAndrii Nakryiko 		return;
59ee5cf82cSAndrii Nakryiko 
6091cbdf74SAndrii Nakryiko 	err = parse_cpu_mask_file("/sys/devices/system/cpu/online",
6191cbdf74SAndrii Nakryiko 				  &online, &on_len);
6291cbdf74SAndrii Nakryiko 	if (CHECK(err, "nr_on_cpus", "err %d\n", err))
6391cbdf74SAndrii Nakryiko 		return;
6491cbdf74SAndrii Nakryiko 
6591cbdf74SAndrii Nakryiko 	for (i = 0; i < on_len; i++)
6691cbdf74SAndrii Nakryiko 		if (online[i])
6791cbdf74SAndrii Nakryiko 			nr_on_cpus++;
6891cbdf74SAndrii Nakryiko 
69ee5cf82cSAndrii Nakryiko 	/* load program */
706984cbc6SAndrii Nakryiko 	skel = test_perf_buffer__open_and_load();
716984cbc6SAndrii Nakryiko 	if (CHECK(!skel, "skel_load", "skeleton open/load failed\n"))
72ee5cf82cSAndrii Nakryiko 		goto out_close;
73ee5cf82cSAndrii Nakryiko 
7457385ae3SAndrii Nakryiko 	err = bpf_map_update_elem(bpf_map__fd(skel->maps.my_pid_map), &zero, &my_pid, 0);
7557385ae3SAndrii Nakryiko 	if (!ASSERT_OK(err, "my_pid_update"))
7657385ae3SAndrii Nakryiko 		goto out_close;
7757385ae3SAndrii Nakryiko 
786984cbc6SAndrii Nakryiko 	/* attach probe */
796984cbc6SAndrii Nakryiko 	err = test_perf_buffer__attach(skel);
806984cbc6SAndrii Nakryiko 	if (CHECK(err, "attach_kprobe", "err %d\n", err))
81ee5cf82cSAndrii Nakryiko 		goto out_close;
82ee5cf82cSAndrii Nakryiko 
83ee5cf82cSAndrii Nakryiko 	/* set up perf buffer */
84*0b52a5f4SAndrii Nakryiko 	pb = perf_buffer__new(bpf_map__fd(skel->maps.perf_buf_map), 1,
85*0b52a5f4SAndrii Nakryiko 			      on_sample, NULL, &cpu_seen, NULL);
86bad2e478SAndrii Nakryiko 	if (!ASSERT_OK_PTR(pb, "perf_buf__new"))
876984cbc6SAndrii Nakryiko 		goto out_close;
88ee5cf82cSAndrii Nakryiko 
89dca5612fSAndrii Nakryiko 	CHECK(perf_buffer__epoll_fd(pb) < 0, "epoll_fd",
90dca5612fSAndrii Nakryiko 	      "bad fd: %d\n", perf_buffer__epoll_fd(pb));
91dca5612fSAndrii Nakryiko 
92ee5cf82cSAndrii Nakryiko 	/* trigger kprobe on every CPU */
93ee5cf82cSAndrii Nakryiko 	CPU_ZERO(&cpu_seen);
94ee5cf82cSAndrii Nakryiko 	for (i = 0; i < nr_cpus; i++) {
9591cbdf74SAndrii Nakryiko 		if (i >= on_len || !online[i]) {
9691cbdf74SAndrii Nakryiko 			printf("skipping offline CPU #%d\n", i);
9791cbdf74SAndrii Nakryiko 			continue;
9891cbdf74SAndrii Nakryiko 		}
9991cbdf74SAndrii Nakryiko 
100dca5612fSAndrii Nakryiko 		if (trigger_on_cpu(i))
1016984cbc6SAndrii Nakryiko 			goto out_close;
102ee5cf82cSAndrii Nakryiko 	}
103ee5cf82cSAndrii Nakryiko 
104ee5cf82cSAndrii Nakryiko 	/* read perf buffer */
105ee5cf82cSAndrii Nakryiko 	err = perf_buffer__poll(pb, 100);
106ee5cf82cSAndrii Nakryiko 	if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
107ee5cf82cSAndrii Nakryiko 		goto out_free_pb;
108ee5cf82cSAndrii Nakryiko 
10991cbdf74SAndrii Nakryiko 	if (CHECK(CPU_COUNT(&cpu_seen) != nr_on_cpus, "seen_cpu_cnt",
11091cbdf74SAndrii Nakryiko 		  "expect %d, seen %d\n", nr_on_cpus, CPU_COUNT(&cpu_seen)))
111ee5cf82cSAndrii Nakryiko 		goto out_free_pb;
112ee5cf82cSAndrii Nakryiko 
113d4121376SJiri Olsa 	if (CHECK(perf_buffer__buffer_cnt(pb) != nr_on_cpus, "buf_cnt",
114d4121376SJiri Olsa 		  "got %zu, expected %d\n", perf_buffer__buffer_cnt(pb), nr_on_cpus))
115dca5612fSAndrii Nakryiko 		goto out_close;
116dca5612fSAndrii Nakryiko 
117aa274f98SJiri Olsa 	for (i = 0, j = 0; i < nr_cpus; i++) {
118dca5612fSAndrii Nakryiko 		if (i >= on_len || !online[i])
119dca5612fSAndrii Nakryiko 			continue;
120dca5612fSAndrii Nakryiko 
121aa274f98SJiri Olsa 		fd = perf_buffer__buffer_fd(pb, j);
122dca5612fSAndrii Nakryiko 		CHECK(fd < 0 || last_fd == fd, "fd_check", "last fd %d == fd %d\n", last_fd, fd);
123dca5612fSAndrii Nakryiko 		last_fd = fd;
124dca5612fSAndrii Nakryiko 
125aa274f98SJiri Olsa 		err = perf_buffer__consume_buffer(pb, j);
126dca5612fSAndrii Nakryiko 		if (CHECK(err, "drain_buf", "cpu %d, err %d\n", i, err))
127dca5612fSAndrii Nakryiko 			goto out_close;
128dca5612fSAndrii Nakryiko 
129dca5612fSAndrii Nakryiko 		CPU_CLR(i, &cpu_seen);
130dca5612fSAndrii Nakryiko 		if (trigger_on_cpu(i))
131dca5612fSAndrii Nakryiko 			goto out_close;
132dca5612fSAndrii Nakryiko 
133aa274f98SJiri Olsa 		err = perf_buffer__consume_buffer(pb, j);
134aa274f98SJiri Olsa 		if (CHECK(err, "consume_buf", "cpu %d, err %d\n", j, err))
135dca5612fSAndrii Nakryiko 			goto out_close;
136dca5612fSAndrii Nakryiko 
137dca5612fSAndrii Nakryiko 		if (CHECK(!CPU_ISSET(i, &cpu_seen), "cpu_seen", "cpu %d not seen\n", i))
138dca5612fSAndrii Nakryiko 			goto out_close;
139aa274f98SJiri Olsa 		j++;
140dca5612fSAndrii Nakryiko 	}
141dca5612fSAndrii Nakryiko 
142ee5cf82cSAndrii Nakryiko out_free_pb:
143ee5cf82cSAndrii Nakryiko 	perf_buffer__free(pb);
144ee5cf82cSAndrii Nakryiko out_close:
1456984cbc6SAndrii Nakryiko 	test_perf_buffer__destroy(skel);
14691cbdf74SAndrii Nakryiko 	free(online);
147ee5cf82cSAndrii Nakryiko }
148