Skip to content

Commit 5d01e61

Browse files
Steven Gumichaelwoerister
authored andcommitted
Adds LightweightEvent for making Event string fields in lazy.
1 parent d0e6292 commit 5d01e61

File tree

10 files changed

+270
-105
lines changed

10 files changed

+270
-105
lines changed

analyzeme/src/event.rs

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use measureme::RawEvent;
1+
use crate::timestamp::Timestamp;
22
use std::borrow::Cow;
3-
use std::time::{Duration, SystemTime};
3+
use std::time::Duration;
44

55
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
66
pub struct Event<'a> {
@@ -37,50 +37,3 @@ impl<'a> Event<'a> {
3737
}
3838
}
3939
}
40-
41-
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
42-
pub enum Timestamp {
43-
Interval { start: SystemTime, end: SystemTime },
44-
Instant(SystemTime),
45-
}
46-
47-
impl Timestamp {
48-
pub fn from_raw_event(raw_event: &RawEvent, start_time: SystemTime) -> Timestamp {
49-
if raw_event.is_instant() {
50-
let t = start_time + Duration::from_nanos(raw_event.start_nanos());
51-
Timestamp::Instant(t)
52-
} else {
53-
let start = start_time + Duration::from_nanos(raw_event.start_nanos());
54-
let end = start_time + Duration::from_nanos(raw_event.end_nanos());
55-
Timestamp::Interval { start, end }
56-
}
57-
}
58-
59-
pub fn contains(&self, t: SystemTime) -> bool {
60-
match *self {
61-
Timestamp::Interval { start, end } => t >= start && t < end,
62-
Timestamp::Instant(_) => false,
63-
}
64-
}
65-
66-
pub fn is_instant(&self) -> bool {
67-
match *self {
68-
Timestamp::Interval { .. } => false,
69-
Timestamp::Instant(_) => true,
70-
}
71-
}
72-
73-
pub fn start(&self) -> SystemTime {
74-
match *self {
75-
Timestamp::Interval { start, .. } => start,
76-
Timestamp::Instant(t) => t,
77-
}
78-
}
79-
80-
pub fn end(&self) -> SystemTime {
81-
match *self {
82-
Timestamp::Interval { end, .. } => end,
83-
Timestamp::Instant(t) => t,
84-
}
85-
}
86-
}

analyzeme/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
//! [`ProfilingData::iter()`]: struct.ProfilingData.html#method.iter
1515
1616
mod event;
17+
mod lightweight_event;
1718
mod profiling_data;
1819
mod stack_collapse;
20+
mod timestamp;
1921
pub mod testing_common;
2022

21-
pub use crate::event::{Event, Timestamp};
23+
pub use crate::event::Event;
24+
pub use crate::lightweight_event::LightweightEvent;
2225
pub use crate::profiling_data::{ProfilingData, ProfilingDataBuilder};
2326
pub use crate::stack_collapse::collapse_stacks;
27+
pub use crate::timestamp::Timestamp;

analyzeme/src/lightweight_event.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use crate::event::Event;
2+
use crate::profiling_data::ProfilingData;
3+
use crate::timestamp::Timestamp;
4+
use std::hash::{Hash, Hasher};
5+
use std::time::Duration;
6+
7+
#[derive(Clone, Debug)]
8+
pub struct LightweightEvent<'a> {
9+
pub data: &'a ProfilingData,
10+
pub event_index: usize,
11+
pub thread_id: u32,
12+
pub timestamp: Timestamp,
13+
}
14+
15+
impl<'a> LightweightEvent<'a> {
16+
pub fn to_event(&self) -> Event<'a> {
17+
self.data.decode_full_event(self.event_index)
18+
}
19+
20+
/// Returns true if the time interval of `self` completely contains the
21+
/// time interval of `other`.
22+
pub fn contains(&self, other: &LightweightEvent) -> bool {
23+
match self.timestamp {
24+
Timestamp::Interval {
25+
start: self_start,
26+
end: self_end,
27+
} => match other.timestamp {
28+
Timestamp::Interval {
29+
start: other_start,
30+
end: other_end,
31+
} => self_start <= other_start && other_end <= self_end,
32+
Timestamp::Instant(other_t) => self_start <= other_t && other_t <= self_end,
33+
},
34+
Timestamp::Instant(_) => false,
35+
}
36+
}
37+
38+
pub fn duration(&self) -> Option<Duration> {
39+
match self.timestamp {
40+
Timestamp::Interval { start, end } => end.duration_since(start).ok(),
41+
Timestamp::Instant(_) => None,
42+
}
43+
}
44+
}
45+
46+
impl<'a> PartialEq for LightweightEvent<'a> {
47+
fn eq(&self, other: &LightweightEvent<'a>) -> bool {
48+
let LightweightEvent {
49+
data,
50+
event_index,
51+
thread_id,
52+
timestamp,
53+
} = *self;
54+
55+
let LightweightEvent {
56+
data: other_data,
57+
event_index: other_event_index,
58+
thread_id: other_thread_id,
59+
timestamp: other_timestamp,
60+
} = *other;
61+
62+
std::ptr::eq(data, other_data) &&
63+
event_index == other_event_index &&
64+
thread_id == other_thread_id &&
65+
timestamp == other_timestamp
66+
}
67+
}
68+
69+
impl<'a> Eq for LightweightEvent<'a> {}
70+
71+
impl<'a> Hash for LightweightEvent<'a> {
72+
fn hash<H: Hasher>(&self, state: &mut H) {
73+
let LightweightEvent {
74+
data,
75+
event_index,
76+
thread_id,
77+
timestamp,
78+
} = *self;
79+
80+
std::ptr::hash(data, state);
81+
event_index.hash(state);
82+
thread_id.hash(state);
83+
timestamp.hash(state);
84+
}
85+
}

0 commit comments

Comments
 (0)