37 #include <linux/perf_event.h>
38 #include <sys/ioctl.h>
39 #include <sys/syscall.h>
42 #if HAVE_PTHREAD_SETAFFINITY_NP
48static int perf_sysfd = -1;
51static int get_pinned_cpu(
void)
53#if HAVE_PTHREAD_SETAFFINITY_NP && defined(CPU_SET)
57 if (pthread_getaffinity_np(pthread_self(),
sizeof(
mask), &
mask))
60 for (
int i = 0;
i < CPU_SETSIZE;
i++) {
61 if (!CPU_ISSET(
i, &
mask))
74static uint64_t perf_start(
void)
76 ioctl(perf_sysfd, PERF_EVENT_IOC_RESET, 0);
77 ioctl(perf_sysfd, PERF_EVENT_IOC_ENABLE, 0);
81static uint64_t perf_stop(uint64_t t)
83 ioctl(perf_sysfd, PERF_EVENT_IOC_DISABLE, 0);
84 long ret =
read(perf_sysfd, &t,
sizeof(t));
91 struct perf_event_attr attr = {
92 .type = PERF_TYPE_HARDWARE,
93 .size =
sizeof(
struct perf_event_attr),
94 .
config = PERF_COUNT_HW_CPU_CYCLES,
103 if (perf_sysfd == -1) {
104 perf_sysfd = (int) syscall(SYS_perf_event_open, &attr, 0,
105 get_pinned_cpu(), -1,
107 if (perf_sysfd == -1) {
108 perror(
"perf_event_open");
113 perf->
start = perf_start;
114 perf->
stop = perf_stop;
115 perf->
name =
"linux (perf)";
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
#define i(width, name, range_min, range_max)
const CheckasmCpuInfo * cpu
static const uint16_t mask[17]
uint64_t(* start)(void)
Start timing measurement.
const char * unit
Unit of measurement (e.g., "ns", "cycles")
const char * name
Name of the timing mechanism (e.g., "clock_gettime")
uint64_t(* stop)(uint64_t start_time)
Stop timing measurement.
int checkasm_perf_init_linux(CheckasmPerf *perf)
int checkasm_perf_validate_start_stop(const CheckasmPerf *perf)