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
#[cfg(target_arch = "x86")]
#[inline(always)]
pub fn rdtsc_arch() -> u64 {
unsafe { ::core::arch::x86::_rdtsc() as u64 }
}
#[cfg(target_arch = "x86_64")]
#[inline(always)]
pub fn rdtsc_arch() -> u64 {
unsafe { ::core::arch::x86_64::_rdtsc() as u64 }
}
#[cfg(not(any(arget_arch = "x86", target_arch = "x86_64")))]
compile_error!("Expected x86 or x86_64 architecture.");
#[cfg(target_arch = "x86")]
pub fn lfence() {
unsafe { ::core::arch::x86::_mm_lfence() }
}
#[cfg(target_arch = "x86_64")]
pub fn lfence() {
unsafe { ::core::arch::x86_64::_mm_lfence() }
}
#[cfg(not(any(arget_arch = "x86", target_arch = "x86_64")))]
compile_error!("Expected x86 or x86_64 architecture.");
#[inline(always)]
pub fn rdtsc_precise() -> u64 {
lfence();
rdtsc_arch()
}
#[inline(always)]
pub fn calc_tsc_hz() -> u64 {
const ROUNDUP_UNIT: u64 = 10_000_000;
let start = rdtsc_arch();
std::thread::sleep(std::time::Duration::from_secs(1));
let end = rdtsc_arch();
(((end - start) + (ROUNDUP_UNIT - 1)) / ROUNDUP_UNIT) * ROUNDUP_UNIT
}