-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmod.rs
More file actions
88 lines (76 loc) · 2.48 KB
/
Copy pathmod.rs
File metadata and controls
88 lines (76 loc) · 2.48 KB
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
use std::time::Instant;
use crate::time::{fence, FineDuration, Timer, TimerKind};
mod tsc;
pub(crate) use tsc::*;
/// A measurement timestamp.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Timestamp {
/// Time provided by the operating system.
Os(Instant),
/// [CPU timestamp counter](https://en.wikipedia.org/wiki/Time_Stamp_Counter).
Tsc(TscTimestamp),
}
impl Timestamp {
#[inline(always)]
pub fn start(timer_kind: TimerKind) -> Self {
fence::full_fence();
let value = match timer_kind {
TimerKind::Os => Self::Os(Instant::now()),
TimerKind::Tsc => Self::Tsc(TscTimestamp::start()),
};
fence::compiler_fence();
value
}
pub fn duration_since(self, earlier: Self, timer: Timer) -> FineDuration {
match (self, earlier, timer) {
(Self::Os(this), Self::Os(earlier), Timer::Os) => this.duration_since(earlier).into(),
(Self::Tsc(this), Self::Tsc(earlier), Timer::Tsc { frequency }) => {
this.duration_since(earlier, frequency)
}
_ => unreachable!(),
}
}
}
/// A [`Timestamp`] where the variant is determined by an external source of
/// truth.
///
/// By making the variant tag external to this type, we produce more optimized
/// code by:
/// - Reusing the same condition variable
/// - Reducing the size of the timestamp variables
#[derive(Clone, Copy)]
pub(crate) union UntaggedTimestamp {
/// [`Timestamp::Os`].
pub os: Instant,
/// [`Timestamp::Tsc`].
pub tsc: TscTimestamp,
}
impl UntaggedTimestamp {
#[inline(always)]
pub fn start(timer_kind: TimerKind) -> Self {
fence::full_fence();
let value = match timer_kind {
TimerKind::Os => Self { os: Instant::now() },
TimerKind::Tsc => Self { tsc: TscTimestamp::start() },
};
fence::compiler_fence();
value
}
#[inline(always)]
pub fn end(timer_kind: TimerKind) -> Self {
fence::compiler_fence();
let value = match timer_kind {
TimerKind::Os => Self { os: Instant::now() },
TimerKind::Tsc => Self { tsc: TscTimestamp::end() },
};
fence::full_fence();
value
}
#[inline(always)]
pub unsafe fn into_timestamp(self, timer_kind: TimerKind) -> Timestamp {
match timer_kind {
TimerKind::Os => Timestamp::Os(self.os),
TimerKind::Tsc => Timestamp::Tsc(self.tsc),
}
}
}