Fabcoin Core  0.16.2
P2P Digital Currency
perf.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2017 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.
4 
5 #include <bench/perf.h>
6 
7 #if defined(__i386__) || defined(__x86_64__)
8 
9 /* These architectures support querying the cycle counter
10  * from user space, no need for any syscall overhead.
11  */
12 void perf_init(void) { }
13 void perf_fini(void) { }
14 
15 #elif defined(__linux__)
16 
17 #include <unistd.h>
18 #include <sys/syscall.h>
19 #include <linux/perf_event.h>
20 
21 static int fd = -1;
22 static struct perf_event_attr attr;
23 
24 void perf_init(void)
25 {
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);
29 }
30 
31 void perf_fini(void)
32 {
33  if (fd != -1) {
34  close(fd);
35  }
36 }
37 
38 uint64_t perf_cpucycles(void)
39 {
40  uint64_t result = 0;
41  if (fd == -1 || read(fd, &result, sizeof(result)) < (ssize_t)sizeof(result)) {
42  return 0;
43  }
44  return result;
45 }
46 
47 #else /* Unhandled platform */
48 
49 void perf_init(void) { }
50 void perf_fini(void) { }
51 uint64_t perf_cpucycles(void) { return 0; }
52 
53 #endif
void perf_fini(void)
Definition: perf.cpp:50
void perf_init(void)
Definition: perf.cpp:49
bool read(const std::string &s, Value &value)
uint64_t perf_cpucycles(void)
Functions for measurement of CPU cycles.
Definition: perf.cpp:51
#define fd(x)
Definition: rijndael.cpp:172