Merge #10114: [tests] sync_with_ping should assert that ping hasn't timed out
[bitcoinplatinum.git] / src / bench / perf.cpp
bloba549ec29eaafdf97eac290251c3f7f8d33519a6f
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.
5 #include "perf.h"
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__)
17 #include <unistd.h>
18 #include <sys/syscall.h>
19 #include <linux/perf_event.h>
21 static int fd = -1;
22 static struct perf_event_attr attr;
24 void perf_init(void)
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);
31 void perf_fini(void)
33 if (fd != -1) {
34 close(fd);
38 uint64_t perf_cpucycles(void)
40 uint64_t result = 0;
41 if (fd == -1 || read(fd, &result, sizeof(result)) < (ssize_t)sizeof(result)) {
42 return 0;
44 return result;
47 #else /* Unhandled platform */
49 void perf_init(void) { }
50 void perf_fini(void) { }
51 uint64_t perf_cpucycles(void) { return 0; }
53 #endif