Raven Core  3.0.0
P2P Digital Currency
perf.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "perf.h"
7 
8 #if defined(__i386__) || defined(__x86_64__)
9 
10 /* These architectures support querying the cycle counter
11  * from user space, no need for any syscall overhead.
12  */
13 void perf_init(void) { }
14 void perf_fini(void) { }
15 
16 #elif defined(__linux__)
17 
18 #include <unistd.h>
19 #include <sys/syscall.h>
20 #include <linux/perf_event.h>
21 
22 static int fd = -1;
23 static struct perf_event_attr attr;
24 
25 void perf_init(void)
26 {
27  attr.type = PERF_TYPE_HARDWARE;
28  attr.config = PERF_COUNT_HW_CPU_CYCLES;
29  fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
30 }
31 
32 void perf_fini(void)
33 {
34  if (fd != -1) {
35  close(fd);
36  }
37 }
38 
39 uint64_t perf_cpucycles(void)
40 {
41  uint64_t result = 0;
42  if (fd == -1 || read(fd, &result, sizeof(result)) < (ssize_t)sizeof(result)) {
43  return 0;
44  }
45  return result;
46 }
47 
48 #else /* Unhandled platform */
49 
50 void perf_init(void) { }
51 void perf_fini(void) { }
52 uint64_t perf_cpucycles(void) { return 0; }
53 
54 #endif
void perf_fini(void)
Definition: perf.cpp:51
void perf_init(void)
Definition: perf.cpp:50
uint64_t perf_cpucycles(void)
Functions for measurement of CPU cycles.
Definition: perf.cpp:52