Skip to content

Commit 2f68b69

Browse files
authored
Merge pull request #169 from rylev/integer-support
Add support for artifact size events
2 parents ca49d9e + be73829 commit 2f68b69

18 files changed

+585
-272
lines changed

analyzeme/src/event.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::timestamp::Timestamp;
1+
use crate::event_payload::EventPayload;
22
use memchr::memchr;
33
use std::borrow::Cow;
44
use std::time::Duration;
@@ -8,34 +8,19 @@ pub struct Event<'a> {
88
pub event_kind: Cow<'a, str>,
99
pub label: Cow<'a, str>,
1010
pub additional_data: Vec<Cow<'a, str>>,
11-
pub timestamp: Timestamp,
11+
pub payload: EventPayload,
1212
pub thread_id: u32,
1313
}
1414

1515
impl<'a> Event<'a> {
1616
/// Returns true if the time interval of `self` completely contains the
1717
/// time interval of `other`.
1818
pub fn contains(&self, other: &Event<'_>) -> bool {
19-
match self.timestamp {
20-
Timestamp::Interval {
21-
start: self_start,
22-
end: self_end,
23-
} => match other.timestamp {
24-
Timestamp::Interval {
25-
start: other_start,
26-
end: other_end,
27-
} => self_start <= other_start && other_end <= self_end,
28-
Timestamp::Instant(other_t) => self_start <= other_t && other_t <= self_end,
29-
},
30-
Timestamp::Instant(_) => false,
31-
}
19+
self.payload.contains(&other.payload)
3220
}
3321

3422
pub fn duration(&self) -> Option<Duration> {
35-
match self.timestamp {
36-
Timestamp::Interval { start, end } => end.duration_since(start).ok(),
37-
Timestamp::Instant(_) => None,
38-
}
23+
self.payload.duration()
3924
}
4025

4126
pub(crate) fn parse_event_id(event_id: Cow<'a, str>) -> (Cow<'a, str>, Vec<Cow<'a, str>>) {

analyzeme/src/event_payload.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use measureme::RawEvent;
2+
use std::time::{Duration, SystemTime};
3+
4+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
5+
pub enum EventPayload {
6+
Timestamp(Timestamp),
7+
Integer(u64),
8+
}
9+
10+
impl EventPayload {
11+
pub fn from_raw_event(raw_event: &RawEvent, start_time: SystemTime) -> Self {
12+
if raw_event.is_integer() {
13+
Self::Integer(raw_event.value())
14+
} else {
15+
Self::Timestamp(Timestamp::from_raw_event(raw_event, start_time))
16+
}
17+
}
18+
19+
/// Returns true if the time interval of `self` completely contains the
20+
/// time interval of `other`.
21+
pub fn contains(&self, other: &Self) -> bool {
22+
match self {
23+
EventPayload::Timestamp(Timestamp::Interval {
24+
start: self_start,
25+
end: self_end,
26+
}) => match other {
27+
EventPayload::Timestamp(Timestamp::Interval {
28+
start: other_start,
29+
end: other_end,
30+
}) => self_start <= other_start && other_end <= self_end,
31+
EventPayload::Timestamp(Timestamp::Instant(other_t)) => {
32+
self_start <= other_t && other_t <= self_end
33+
}
34+
EventPayload::Integer(_) => false,
35+
},
36+
EventPayload::Timestamp(Timestamp::Instant(_)) | EventPayload::Integer(_) => false,
37+
}
38+
}
39+
40+
pub fn duration(&self) -> Option<Duration> {
41+
if let EventPayload::Timestamp(t) = *self {
42+
t.duration()
43+
} else {
44+
None
45+
}
46+
}
47+
48+
pub fn is_interval(&self) -> bool {
49+
matches!(self, &Self::Timestamp(Timestamp::Interval { .. }))
50+
}
51+
52+
pub fn is_instant(&self) -> bool {
53+
matches!(self, &Self::Timestamp(Timestamp::Instant(_)))
54+
}
55+
56+
pub fn is_integer(&self) -> bool {
57+
matches!(self, &Self::Integer(_))
58+
}
59+
60+
pub fn timestamp(&self) -> Option<Timestamp> {
61+
match self {
62+
Self::Timestamp(t) => Some(*t),
63+
Self::Integer(_) => None,
64+
}
65+
}
66+
}
67+
68+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
69+
pub enum Timestamp {
70+
Interval { start: SystemTime, end: SystemTime },
71+
Instant(SystemTime),
72+
}
73+
74+
impl Timestamp {
75+
pub fn from_raw_event(raw_event: &RawEvent, start_time: SystemTime) -> Self {
76+
debug_assert!(!raw_event.is_integer());
77+
if raw_event.is_instant() {
78+
let t = start_time + Duration::from_nanos(raw_event.start_value());
79+
Self::Instant(t)
80+
} else {
81+
let start = start_time + Duration::from_nanos(raw_event.start_value());
82+
let end = start_time + Duration::from_nanos(raw_event.end_value());
83+
Timestamp::Interval { start, end }
84+
}
85+
}
86+
87+
pub fn contains(&self, t: SystemTime) -> bool {
88+
match *self {
89+
Timestamp::Interval { start, end } => t >= start && t < end,
90+
Timestamp::Instant(_) => false,
91+
}
92+
}
93+
94+
pub fn is_instant(&self) -> bool {
95+
matches!(self, &Timestamp::Instant(_))
96+
}
97+
98+
pub fn start(&self) -> SystemTime {
99+
match *self {
100+
Timestamp::Interval { start, .. } => start,
101+
Timestamp::Instant(t) => t,
102+
}
103+
}
104+
105+
pub fn end(&self) -> SystemTime {
106+
match *self {
107+
Timestamp::Interval { end, .. } => end,
108+
Timestamp::Instant(t) => t,
109+
}
110+
}
111+
112+
pub fn duration(&self) -> Option<Duration> {
113+
if let Timestamp::Interval { start, end } = *self {
114+
end.duration_since(start).ok()
115+
} else {
116+
None
117+
}
118+
}
119+
}

analyzeme/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
//! call the [`ProfilingData::iter()`] method.
1212
1313
mod event;
14+
mod event_payload;
1415
mod lightweight_event;
1516
mod profiling_data;
1617
mod stack_collapse;
1718
mod stringtable;
1819
pub mod testing_common;
19-
mod timestamp;
2020

2121
pub use crate::event::Event;
22+
pub use crate::event_payload::{EventPayload, Timestamp};
2223
pub use crate::lightweight_event::LightweightEvent;
2324
pub use crate::profiling_data::{ProfilingData, ProfilingDataBuilder};
2425
pub use crate::stack_collapse::collapse_stacks;
2526
pub use crate::stringtable::{StringRef, StringTable};
26-
pub use crate::timestamp::Timestamp;

analyzeme/src/lightweight_event.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use crate::event::Event;
2+
use crate::event_payload::{EventPayload, Timestamp};
23
use crate::profiling_data::ProfilingData;
3-
use crate::timestamp::Timestamp;
44
use std::hash::{Hash, Hasher};
5-
use std::time::Duration;
5+
use std::time::{Duration, SystemTime};
66

77
#[derive(Clone, Debug)]
88
pub struct LightweightEvent<'a> {
99
pub data: &'a ProfilingData,
1010
pub event_index: usize,
1111
pub thread_id: u32,
12-
pub timestamp: Timestamp,
12+
pub payload: EventPayload,
1313
}
1414

1515
impl<'a> LightweightEvent<'a> {
@@ -20,26 +20,25 @@ impl<'a> LightweightEvent<'a> {
2020
/// Returns true if the time interval of `self` completely contains the
2121
/// time interval of `other`.
2222
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-
}
23+
self.payload.contains(&other.payload)
3624
}
3725

3826
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-
}
27+
self.payload.duration()
28+
}
29+
30+
// Returns start time if event is a timestamp
31+
pub fn start(&self) -> Option<SystemTime> {
32+
self.payload.timestamp().map(|t| t.start())
33+
}
34+
35+
// Returns end time if event is a timestamp
36+
pub fn end(&self) -> Option<SystemTime> {
37+
self.payload.timestamp().map(|t| t.end())
38+
}
39+
40+
pub fn timestamp(&self) -> Option<Timestamp> {
41+
self.payload.timestamp()
4342
}
4443
}
4544

@@ -49,20 +48,20 @@ impl<'a> PartialEq for LightweightEvent<'a> {
4948
data,
5049
event_index,
5150
thread_id,
52-
timestamp,
51+
payload,
5352
} = *self;
5453

5554
let LightweightEvent {
5655
data: other_data,
5756
event_index: other_event_index,
5857
thread_id: other_thread_id,
59-
timestamp: other_timestamp,
58+
payload: other_payload,
6059
} = *other;
6160

6261
std::ptr::eq(data, other_data)
6362
&& event_index == other_event_index
6463
&& thread_id == other_thread_id
65-
&& timestamp == other_timestamp
64+
&& payload == other_payload
6665
}
6766
}
6867

@@ -74,12 +73,12 @@ impl<'a> Hash for LightweightEvent<'a> {
7473
data,
7574
event_index,
7675
thread_id,
77-
timestamp,
76+
payload,
7877
} = *self;
7978

8079
std::ptr::hash(data, state);
8180
event_index.hash(state);
8281
thread_id.hash(state);
83-
timestamp.hash(state);
82+
payload.hash(state);
8483
}
8584
}

0 commit comments

Comments
 (0)