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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#![feature(asm, stdsimd, test)]
#![cfg_attr(not(test), no_std)]
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
compile_error!(
"The TSC crate only supports the \"x86\" and \"x86_64\" architectures"
);
extern crate test;
use core::ops;
#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64 as arch;
pub fn has_invariant_tsc() -> bool {
use self::arch::{has_cpuid, CpuidResult, __cpuid};
if !has_cpuid() {
return false;
}
let CpuidResult { eax: max_basic_leaf, .. } = unsafe { __cpuid(0_u32) };
if max_basic_leaf < 1 {
return false;
}
let CpuidResult { eax: max_extended_leaf, .. } =
unsafe { __cpuid(0x8000_0000_u32) };
if max_extended_leaf < 7 {
return false;
}
let CpuidResult { edx, .. } = unsafe { __cpuid(0x8000_0007_u32) };
edx & (1 << 8) != 0
}
pub struct Start(u64);
impl Start {
pub fn now() -> Self {
unsafe {
let _ = arch::__cpuid(0);
Start(core::mem::transmute(arch::_rdtsc()))
}
}
}
pub struct Stop(u64);
impl Stop {
pub fn now() -> Self {
unsafe {
let mut core: u32 = 0;
let r = arch::__rdtscp(&mut core as *mut _) as u64;
let _ = arch::__cpuid(0);
Stop(r)
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Duration(u64);
impl Duration {
pub fn cycles(self) -> u64 {
self.0
}
pub fn span<R, F: Fn() -> R>(f: F) -> (Self, R) {
let start = Start::now();
let result = f();
let stop = Stop::now();
let measurement_duration = stop - start;
let measurement_overhead = Self::span_overhead();
assert!(measurement_overhead <= measurement_duration);
let f_duration = measurement_duration - measurement_overhead;
(f_duration, result)
}
fn span_overhead() -> Self {
let start = Start::now();
let _result = test::black_box(0);
let stop = Stop::now();
stop - start
}
}
impl ops::Sub<Start> for Stop {
type Output = Duration;
fn sub(self, start: Start) -> Duration {
debug_assert!(
self.0 > start.0,
"stop time instant happened after start time instant"
);
Duration(self.0 - start.0)
}
}
impl ops::Sub<Duration> for Duration {
type Output = Self;
fn sub(self, other: Self) -> Self {
debug_assert!(self.0 > other.0, "subtracting durations overflows");
Duration(self.0 - other.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn start_stop() {
let start = Start::now();
let stop = Stop::now();
let dur1 = stop - start;
fn foo() -> i32 {
2_i32.pow(test::black_box(8))
}
let (dur2, r) = Duration::span(foo);
assert_eq!(r, 256);
assert!(dur2.cycles() > dur1.cycles());
}
#[test]
fn print_span_overhead() {
println!(
"span overhead: {} cycles",
Duration::span_overhead().cycles()
);
}
#[test]
fn invariant_tsc() {
assert!(has_invariant_tsc(), "the cpu does not have an invariant TSC");
}
}