forked from mrpi/redis-cplusplus-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cycles.h
120 lines (104 loc) · 3.44 KB
/
Cycles.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright (c) 2011-2014 Stanford University
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef RAMCLOUD_CYCLES_H
#define RAMCLOUD_CYCLES_H
#include <cstdint>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <string>
#include <vector>
#include <sys/time.h>
#include <xmmintrin.h>
#include <cinttypes>
#include <typeinfo>
namespace RAMCloud {
/**
* This class provides static methods that read the fine-grain CPU
* cycle counter and translate between cycle-level times and absolute
* times.
*/
class Cycles {
public:
static void init();
/**
* Return the current value of the fine-grain CPU cycle counter
* (accessed via the RDTSC instruction).
*/
static __inline __attribute__((always_inline))
uint64_t
rdtsc()
{
#if TESTING
if (mockTscValue)
return mockTscValue;
#endif
uint32_t lo, hi;
__asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
return (((uint64_t)hi << 32) | lo);
}
#if TESTING
/**
* Return the current value of the fine-grain CPU cycle counter.
* This function ignores mockTscValue set by test code.
*/
static __inline __attribute__((always_inline))
uint64_t
rdtsc_ignoreMockTsc()
{
uint32_t lo, hi;
__asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
return (((uint64_t)hi << 32) | lo);
}
#endif
static double perSecond();
static double toSeconds(uint64_t cycles, double cyclesPerSec = 0);
static uint64_t fromSeconds(double seconds, double cyclesPerSec = 0);
static uint64_t toMicroseconds(uint64_t cycles, double cyclesPerSec = 0);
static uint64_t fromMicroseconds(uint64_t us, double cyclesPerSec = 0);
static uint64_t toNanoseconds(uint64_t cycles, double cyclesPerSec = 0);
static uint64_t fromNanoseconds(uint64_t ns, double cyclesPerSec = 0);
static void sleep(uint64_t us);
private:
Cycles();
/// Conversion factor between cycles and the seconds; computed by
/// Cycles::init.
static double cyclesPerSec;
/// Used for testing: if nonzero then this will be returned as the result
/// of the next call to rdtsc().
static uint64_t mockTscValue;
/// Used for testing: if nonzero, then this is used to convert from
/// cycles to seconds, instead of cyclesPerSec above.
static double mockCyclesPerSec;
/**
* Returns the conversion factor between cycles in seconds, using
* a mock value for testing when appropriate.
*/
static __inline __attribute__((always_inline))
double
getCyclesPerSec()
{
#if TESTING
if (mockCyclesPerSec != 0.0) {
return mockCyclesPerSec;
}
#endif
return cyclesPerSec;
}
};
} // end RAMCloud
#endif // RAMCLOUD_CYCLES_H