-
Notifications
You must be signed in to change notification settings - Fork 4
/
tsc_chrono.h
145 lines (114 loc) · 2.82 KB
/
tsc_chrono.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#pragma once
#include <chrono>
#include <thread>
#include <cmath>
#include <type_traits>
namespace detail
{
static inline uint64_t rdtsc()
{
uint64_t rax, rdx;
__asm__ __volatile__("rdtsc" : "=a"(rax), "=d"(rdx));
return (rdx << 32) + rax;
}
static inline uint64_t rdtscp()
{
uint64_t rax, rcx, rdx;
__asm__ __volatile__("rdtscp" : "=a"(rax), "=d"(rdx), "=c"(rcx));
return (rdx << 32) + rax;
}
static inline void cpuid()
{
uint64_t rax, rbx, rcx, rdx;
__asm__ __volatile__("cpuid" : "=a"(rax), "=b"(rbx), "=d"(rdx), "=c"(rcx));
}
static inline uint64_t rdtscp(int& chip, int& core)
{
uint64_t rax, rcx, rdx;
__asm__ __volatile__("rdtscp" : "=a"(rax), "=d"(rdx), "=c"(rcx));
chip = (rcx & 0xFFF000) >> 12;
core = rcx & 0xFFF;
return (rdx << 32) + rax;
}
struct tsc
{
static double& get_freq_ghz()
{
static double tsc_freq_ghz = .0;
return tsc_freq_ghz;
}
};
inline void init()
{
double& tsc_freq_ghz = detail::tsc::get_freq_ghz();
if (tsc_freq_ghz != .0)
{
return;
}
using Clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
std::chrono::high_resolution_clock,
std::chrono::steady_clock>;
int chip, core, chip2, core2;
auto start = Clock::now();
detail::cpuid();
uint64_t rdtsc_start = detail::rdtscp(chip, core);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
uint64_t rdtsc_end = detail::rdtscp(chip2, core2);
detail::cpuid();
auto end = Clock::now();
if (core != core2 || chip != chip2)
throw std::runtime_error("please set this executable to a specific core");
auto duration_s = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
uint64_t cycles = rdtsc_end - rdtsc_start;
tsc_freq_ghz = static_cast<double>(cycles) / static_cast<double>(duration_s.count());
}
}
class tsc_chrono
{
public:
tsc_chrono() =default;
static void init()
{
detail::init();
}
void start()
{
detail::cpuid();
m_start = detail::rdtsc();
}
int64_t elapsed_cycles() const
{
uint64_t now = detail::rdtscp();
detail::cpuid();
return now - m_start;
}
int64_t elapsed_cycles_and_restart()
{
detail::cpuid();
uint64_t now = detail::rdtscp();
detail::cpuid();
int64_t ts = now - m_start;
m_start = now;
return ts;
}
std::chrono::nanoseconds elapsed_time() const
{
return from_cycles(elapsed_cycles());
}
static std::chrono::nanoseconds from_cycles(int64_t cycles)
{
const double nanoseconds{static_cast<double>(cycles) / detail::tsc::get_freq_ghz()};
return std::chrono::nanoseconds(static_cast<int64_t>(nanoseconds));
}
static double get_freq_ghz()
{
return detail::tsc::get_freq_ghz();
}
template <typename _DurationT>
static int64_t to_cycles(_DurationT duration)
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() * detail::tsc::get_freq_ghz();
}
private:
uint64_t m_start;
};