37 #include <linux/perf_event.h>
38 #include <sys/ioctl.h>
39 #include <sys/syscall.h>
44 static int perf_sysfd = -1;
46 static uint64_t perf_start(
void)
48 ioctl(perf_sysfd, PERF_EVENT_IOC_RESET, 0);
49 ioctl(perf_sysfd, PERF_EVENT_IOC_ENABLE, 0);
53 static uint64_t perf_stop(uint64_t t)
55 ioctl(perf_sysfd, PERF_EVENT_IOC_DISABLE, 0);
56 long ret =
read(perf_sysfd, &t,
sizeof(t));
63 struct perf_event_attr attr = {
64 .type = PERF_TYPE_HARDWARE,
65 .size =
sizeof(
struct perf_event_attr),
66 .
config = PERF_COUNT_HW_CPU_CYCLES,
75 if (perf_sysfd == -1) {
76 perf_sysfd = (int) syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0);
77 if (perf_sysfd == -1) {
78 perror(
"perf_event_open");
83 perf->
start = perf_start;
84 perf->
stop = perf_stop;
85 perf->
name =
"linux (perf)";