perf_buffer.c 3.5 KB
Newer Older
1 2 3 4 5 6
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h>
#include <sys/socket.h>
#include <test_progs.h>
7
#include "test_perf_buffer.skel.h"
8
#include "bpf/libbpf_internal.h"
9

10 11
static int duration;

12 13 14 15 16
/* AddressSanitizer sometimes crashes due to data dereference below, due to
 * this being mmap()'ed memory. Disable instrumentation with
 * no_sanitize_address attribute
 */
__attribute__((no_sanitize_address))
17 18 19 20 21 22 23 24 25 26 27 28
static void on_sample(void *ctx, int cpu, void *data, __u32 size)
{
	int cpu_data = *(int *)data, duration = 0;
	cpu_set_t *cpu_seen = ctx;

	if (cpu_data != cpu)
		CHECK(cpu_data != cpu, "check_cpu_data",
		      "cpu_data %d != cpu %d\n", cpu_data, cpu);

	CPU_SET(cpu, cpu_seen);
}

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
int trigger_on_cpu(int cpu)
{
	cpu_set_t cpu_set;
	int err;

	CPU_ZERO(&cpu_set);
	CPU_SET(cpu, &cpu_set);

	err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set);
	if (err && CHECK(err, "set_affinity", "cpu #%d, err %d\n", cpu, err))
		return err;

	usleep(1);

	return 0;
}

Y
Yucong Sun 已提交
46
void serial_test_perf_buffer(void)
47
{
48
	int err, on_len, nr_on_cpus = 0, nr_cpus, i, j;
49
	struct perf_buffer_opts pb_opts = {};
50
	struct test_perf_buffer *skel;
51
	cpu_set_t cpu_seen;
52
	struct perf_buffer *pb;
53
	int last_fd = -1, fd;
54
	bool *online;
55 56 57 58 59

	nr_cpus = libbpf_num_possible_cpus();
	if (CHECK(nr_cpus < 0, "nr_cpus", "err %d\n", nr_cpus))
		return;

60 61 62 63 64 65 66 67 68
	err = parse_cpu_mask_file("/sys/devices/system/cpu/online",
				  &online, &on_len);
	if (CHECK(err, "nr_on_cpus", "err %d\n", err))
		return;

	for (i = 0; i < on_len; i++)
		if (online[i])
			nr_on_cpus++;

69
	/* load program */
70 71
	skel = test_perf_buffer__open_and_load();
	if (CHECK(!skel, "skel_load", "skeleton open/load failed\n"))
72 73
		goto out_close;

74 75 76
	/* attach probe */
	err = test_perf_buffer__attach(skel);
	if (CHECK(err, "attach_kprobe", "err %d\n", err))
77 78 79 80 81
		goto out_close;

	/* set up perf buffer */
	pb_opts.sample_cb = on_sample;
	pb_opts.ctx = &cpu_seen;
82
	pb = perf_buffer__new(bpf_map__fd(skel->maps.perf_buf_map), 1, &pb_opts);
83
	if (!ASSERT_OK_PTR(pb, "perf_buf__new"))
84
		goto out_close;
85

86 87 88
	CHECK(perf_buffer__epoll_fd(pb) < 0, "epoll_fd",
	      "bad fd: %d\n", perf_buffer__epoll_fd(pb));

89 90 91
	/* trigger kprobe on every CPU */
	CPU_ZERO(&cpu_seen);
	for (i = 0; i < nr_cpus; i++) {
92 93 94 95 96
		if (i >= on_len || !online[i]) {
			printf("skipping offline CPU #%d\n", i);
			continue;
		}

97
		if (trigger_on_cpu(i))
98
			goto out_close;
99 100 101 102 103 104 105
	}

	/* read perf buffer */
	err = perf_buffer__poll(pb, 100);
	if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
		goto out_free_pb;

106 107
	if (CHECK(CPU_COUNT(&cpu_seen) != nr_on_cpus, "seen_cpu_cnt",
		  "expect %d, seen %d\n", nr_on_cpus, CPU_COUNT(&cpu_seen)))
108 109
		goto out_free_pb;

110 111
	if (CHECK(perf_buffer__buffer_cnt(pb) != nr_on_cpus, "buf_cnt",
		  "got %zu, expected %d\n", perf_buffer__buffer_cnt(pb), nr_on_cpus))
112 113
		goto out_close;

114
	for (i = 0, j = 0; i < nr_cpus; i++) {
115 116 117
		if (i >= on_len || !online[i])
			continue;

118
		fd = perf_buffer__buffer_fd(pb, j);
119 120 121
		CHECK(fd < 0 || last_fd == fd, "fd_check", "last fd %d == fd %d\n", last_fd, fd);
		last_fd = fd;

122
		err = perf_buffer__consume_buffer(pb, j);
123 124 125 126 127 128 129
		if (CHECK(err, "drain_buf", "cpu %d, err %d\n", i, err))
			goto out_close;

		CPU_CLR(i, &cpu_seen);
		if (trigger_on_cpu(i))
			goto out_close;

130 131
		err = perf_buffer__consume_buffer(pb, j);
		if (CHECK(err, "consume_buf", "cpu %d, err %d\n", j, err))
132 133 134 135
			goto out_close;

		if (CHECK(!CPU_ISSET(i, &cpu_seen), "cpu_seen", "cpu %d not seen\n", i))
			goto out_close;
136
		j++;
137 138
	}

139 140 141
out_free_pb:
	perf_buffer__free(pb);
out_close:
142
	test_perf_buffer__destroy(skel);
143
	free(online);
144
}