9 #include <linux/perf_event.h>
10 #include <linux/bpf.h>
14 #define SAMPLE_PERIOD 0x7fffffffffffffffULL
16 static void test_bpf_perf_event(void)
18 int nr_cpus
= sysconf(_SC_NPROCESSORS_CONF
);
19 int *pmu_fd
= malloc(nr_cpus
* sizeof(int));
22 struct perf_event_attr attr_insn_pmu
= {
24 .sample_period
= SAMPLE_PERIOD
,
26 .type
= PERF_TYPE_HARDWARE
,
29 .config
= 0,/* PMU: cycles */
32 for (i
= 0; i
< nr_cpus
; i
++) {
33 pmu_fd
[i
] = perf_event_open(&attr_insn_pmu
, -1/*pid*/, i
/*cpu*/, -1/*group_fd*/, 0);
35 printf("event syscall failed\n");
39 bpf_update_elem(map_fd
[0], &i
, &pmu_fd
[i
], BPF_ANY
);
40 ioctl(pmu_fd
[i
], PERF_EVENT_IOC_ENABLE
, 0);
43 status
= system("ls > /dev/null");
46 status
= system("sleep 2");
51 for (i
= 0; i
< nr_cpus
; i
++)
57 int main(int argc
, char **argv
)
61 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
63 if (load_bpf_file(filename
)) {
64 printf("%s", bpf_log_buf
);
68 test_bpf_perf_event();