1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #if defined(__i386__) || defined(__x86_64__)
9 /* These architectures support querying the cycle counter
10 * from user space, no need for any syscall overhead.
12 void perf_init(void) { }
13 void perf_fini(void) { }
15 #elif defined(__linux__)
18 #include <sys/syscall.h>
19 #include <linux/perf_event.h>
22 static struct perf_event_attr attr
;
26 attr
.type
= PERF_TYPE_HARDWARE
;
27 attr
.config
= PERF_COUNT_HW_CPU_CYCLES
;
28 fd
= syscall(__NR_perf_event_open
, &attr
, 0, -1, -1, 0);
38 uint64_t perf_cpucycles(void)
41 if (fd
== -1 || read(fd
, &result
, sizeof(result
)) < (ssize_t
)sizeof(result
)) {
47 #else /* Unhandled platform */
49 void perf_init(void) { }
50 void perf_fini(void) { }
51 uint64_t perf_cpucycles(void) { return 0; }