9 #include <linux/perf_event.h>
10 #include <linux/bpf.h>
15 #define SAMPLE_PERIOD 0x7fffffffffffffffULL
17 static void test_bpf_perf_event(void)
19 int nr_cpus
= sysconf(_SC_NPROCESSORS_CONF
);
20 int *pmu_fd
= malloc(nr_cpus
* sizeof(int));
23 struct perf_event_attr attr_insn_pmu
= {
25 .sample_period
= SAMPLE_PERIOD
,
27 .type
= PERF_TYPE_HARDWARE
,
30 .config
= 0,/* PMU: cycles */
33 for (i
= 0; i
< nr_cpus
; i
++) {
34 pmu_fd
[i
] = sys_perf_event_open(&attr_insn_pmu
, -1/*pid*/, i
/*cpu*/, -1/*group_fd*/, 0);
36 printf("event syscall failed\n");
40 bpf_map_update_elem(map_fd
[0], &i
, &pmu_fd
[i
], BPF_ANY
);
41 ioctl(pmu_fd
[i
], PERF_EVENT_IOC_ENABLE
, 0);
44 status
= system("ls > /dev/null");
47 status
= system("sleep 2");
52 for (i
= 0; i
< nr_cpus
; i
++)
58 int main(int argc
, char **argv
)
62 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
64 if (load_bpf_file(filename
)) {
65 printf("%s", bpf_log_buf
);
69 test_bpf_perf_event();