Linux Audio

Check our new training course

Loading...
v5.4
 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
 
 
 
 
 
 8static void on_sample(void *ctx, int cpu, void *data, __u32 size)
 9{
10	int cpu_data = *(int *)data, duration = 0;
11	cpu_set_t *cpu_seen = ctx;
12
13	if (cpu_data != cpu)
14		CHECK(cpu_data != cpu, "check_cpu_data",
15		      "cpu_data %d != cpu %d\n", cpu_data, cpu);
16
17	CPU_SET(cpu, cpu_seen);
18}
19
20void test_perf_buffer(void)
21{
22	int err, prog_fd, nr_cpus, i, duration = 0;
23	const char *prog_name = "kprobe/sys_nanosleep";
24	const char *file = "./test_perf_buffer.o";
25	struct perf_buffer_opts pb_opts = {};
26	struct bpf_map *perf_buf_map;
27	cpu_set_t cpu_set, cpu_seen;
28	struct bpf_program *prog;
29	struct bpf_object *obj;
30	struct perf_buffer *pb;
31	struct bpf_link *link;
32
33	nr_cpus = libbpf_num_possible_cpus();
34	if (CHECK(nr_cpus < 0, "nr_cpus", "err %d\n", nr_cpus))
35		return;
36
37	/* load program */
38	err = bpf_prog_load(file, BPF_PROG_TYPE_KPROBE, &obj, &prog_fd);
39	if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno))
40		return;
41
42	prog = bpf_object__find_program_by_title(obj, prog_name);
43	if (CHECK(!prog, "find_probe", "prog '%s' not found\n", prog_name))
44		goto out_close;
45
46	/* load map */
47	perf_buf_map = bpf_object__find_map_by_name(obj, "perf_buf_map");
48	if (CHECK(!perf_buf_map, "find_perf_buf_map", "not found\n"))
49		goto out_close;
50
51	/* attach kprobe */
52	link = bpf_program__attach_kprobe(prog, false /* retprobe */,
53					  SYS_NANOSLEEP_KPROBE_NAME);
54	if (CHECK(IS_ERR(link), "attach_kprobe", "err %ld\n", PTR_ERR(link)))
55		goto out_close;
56
57	/* set up perf buffer */
58	pb_opts.sample_cb = on_sample;
59	pb_opts.ctx = &cpu_seen;
60	pb = perf_buffer__new(bpf_map__fd(perf_buf_map), 1, &pb_opts);
61	if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb)))
62		goto out_detach;
63
64	/* trigger kprobe on every CPU */
65	CPU_ZERO(&cpu_seen);
66	for (i = 0; i < nr_cpus; i++) {
 
 
 
 
 
67		CPU_ZERO(&cpu_set);
68		CPU_SET(i, &cpu_set);
69
70		err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set),
71					     &cpu_set);
72		if (err && CHECK(err, "set_affinity", "cpu #%d, err %d\n",
73				 i, err))
74			goto out_detach;
75
76		usleep(1);
77	}
78
79	/* read perf buffer */
80	err = perf_buffer__poll(pb, 100);
81	if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
82		goto out_free_pb;
83
84	if (CHECK(CPU_COUNT(&cpu_seen) != nr_cpus, "seen_cpu_cnt",
85		  "expect %d, seen %d\n", nr_cpus, CPU_COUNT(&cpu_seen)))
86		goto out_free_pb;
87
88out_free_pb:
89	perf_buffer__free(pb);
90out_detach:
91	bpf_link__destroy(link);
92out_close:
93	bpf_object__close(obj);
 
94}
v5.9
  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))
 15static 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
 27void 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
 95out_free_pb:
 96	perf_buffer__free(pb);
 
 
 97out_close:
 98	test_perf_buffer__destroy(skel);
 99	free(online);
100}