1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/perf_event.h>
10 #include <sys/ioctl.h>
12 #include <sys/types.h>
17 #include <bpf/libbpf.h>
20 #define SAMPLE_PERIOD 0x7fffffffffffffffULL
22 /* counters, values, values2 */
25 static void check_on_cpu(int cpu, struct perf_event_attr *attr)
27 struct bpf_perf_event_value value2;
28 int pmu_fd, error = 0;
32 /* Move to target CPU */
35 assert(sched_setaffinity(0, sizeof(set), &set) == 0);
36 /* Open perf event and attach to the perf_event_array */
37 pmu_fd = sys_perf_event_open(attr, -1/*pid*/, cpu/*cpu*/, -1/*group_fd*/, 0);
39 fprintf(stderr, "sys_perf_event_open failed on CPU %d\n", cpu);
43 assert(bpf_map_update_elem(map_fd[0], &cpu, &pmu_fd, BPF_ANY) == 0);
44 assert(ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0) == 0);
45 /* Trigger the kprobe */
46 bpf_map_get_next_key(map_fd[1], &cpu, NULL);
48 if (bpf_map_lookup_elem(map_fd[1], &cpu, &value)) {
49 fprintf(stderr, "Value missing for CPU %d\n", cpu);
53 fprintf(stderr, "CPU %d: %llu\n", cpu, value);
55 /* The above bpf_map_lookup_elem should trigger the second kprobe */
56 if (bpf_map_lookup_elem(map_fd[2], &cpu, &value2)) {
57 fprintf(stderr, "Value2 missing for CPU %d\n", cpu);
61 fprintf(stderr, "CPU %d: counter: %llu, enabled: %llu, running: %llu\n", cpu,
62 value2.counter, value2.enabled, value2.running);
66 assert(bpf_map_delete_elem(map_fd[0], &cpu) == 0 || error);
67 assert(ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE, 0) == 0 || error);
68 assert(close(pmu_fd) == 0 || error);
69 assert(bpf_map_delete_elem(map_fd[1], &cpu) == 0 || error);
73 static void test_perf_event_array(struct perf_event_attr *attr,
76 int i, status, nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
80 printf("Test reading %s counters\n", name);
82 for (i = 0; i < nr_cpus; i++) {
86 check_on_cpu(i, attr);
91 for (i = 0; i < nr_cpus; i++) {
92 assert(waitpid(pid[i], &status, 0) == pid[i]);
97 printf("Test: %s FAILED\n", name);
100 static void test_bpf_perf_event(void)
102 struct perf_event_attr attr_cycles = {
104 .sample_period = SAMPLE_PERIOD,
106 .type = PERF_TYPE_HARDWARE,
109 .config = PERF_COUNT_HW_CPU_CYCLES,
111 struct perf_event_attr attr_clock = {
113 .sample_period = SAMPLE_PERIOD,
115 .type = PERF_TYPE_SOFTWARE,
118 .config = PERF_COUNT_SW_CPU_CLOCK,
120 struct perf_event_attr attr_raw = {
122 .sample_period = SAMPLE_PERIOD,
124 .type = PERF_TYPE_RAW,
127 /* Intel Instruction Retired */
130 struct perf_event_attr attr_l1d_load = {
132 .sample_period = SAMPLE_PERIOD,
134 .type = PERF_TYPE_HW_CACHE,
138 PERF_COUNT_HW_CACHE_L1D |
139 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
140 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
142 struct perf_event_attr attr_llc_miss = {
144 .sample_period = SAMPLE_PERIOD,
146 .type = PERF_TYPE_HW_CACHE,
150 PERF_COUNT_HW_CACHE_LL |
151 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
152 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
154 struct perf_event_attr attr_msr_tsc = {
158 /* From /sys/bus/event_source/devices/msr/ */
165 test_perf_event_array(&attr_cycles, "HARDWARE-cycles");
166 test_perf_event_array(&attr_clock, "SOFTWARE-clock");
167 test_perf_event_array(&attr_raw, "RAW-instruction-retired");
168 test_perf_event_array(&attr_l1d_load, "HW_CACHE-L1D-load");
170 /* below tests may fail in qemu */
171 test_perf_event_array(&attr_llc_miss, "HW_CACHE-LLC-miss");
172 test_perf_event_array(&attr_msr_tsc, "Dynamic-msr-tsc");
175 int main(int argc, char **argv)
177 struct bpf_link *links[2];
178 struct bpf_program *prog;
179 struct bpf_object *obj;
183 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
184 obj = bpf_object__open_file(filename, NULL);
185 if (libbpf_get_error(obj)) {
186 fprintf(stderr, "ERROR: opening BPF object file failed\n");
190 /* load BPF program */
191 if (bpf_object__load(obj)) {
192 fprintf(stderr, "ERROR: loading BPF object file failed\n");
196 map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counters");
197 map_fd[1] = bpf_object__find_map_fd_by_name(obj, "values");
198 map_fd[2] = bpf_object__find_map_fd_by_name(obj, "values2");
199 if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0) {
200 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
204 bpf_object__for_each_program(prog, obj) {
205 links[i] = bpf_program__attach(prog);
206 if (libbpf_get_error(links[i])) {
207 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
214 test_bpf_perf_event();
217 for (i--; i >= 0; i--)
218 bpf_link__destroy(links[i]);
220 bpf_object__close(obj);