Skip to content

Commit e6b27c0

Browse files
committed
Add support for integer events
1 parent ca49d9e commit e6b27c0

File tree

15 files changed

+472
-267
lines changed

15 files changed

+472
-267
lines changed

analyzeme/src/event.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
1-
use crate::timestamp::Timestamp;
1+
use crate::event_payload::EventPayload;
22
use memchr::memchr;
33
use std::borrow::Cow;
4-
use std::time::Duration;
4+
use std::time::{Duration, SystemTime};
55

66
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
77
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()
24+
}
25+
26+
pub fn start(&self) -> SystemTime {
27+
self.payload.start()
28+
}
29+
30+
pub fn end(&self) -> SystemTime {
31+
self.payload.end()
3932
}
4033

4134
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: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
_ => false,
37+
}
38+
}
39+
40+
pub fn duration(&self) -> Option<Duration> {
41+
match *self {
42+
EventPayload::Timestamp(Timestamp::Interval { start, end }) => {
43+
end.duration_since(start).ok()
44+
}
45+
_ => None,
46+
}
47+
}
48+
49+
pub fn is_interval(&self) -> bool {
50+
matches!(self, &Self::Timestamp(Timestamp::Interval { .. }))
51+
}
52+
53+
pub fn is_instant(&self) -> bool {
54+
matches!(self, &Self::Timestamp(Timestamp::Instant(_)))
55+
}
56+
57+
pub fn is_integer(&self) -> bool {
58+
matches!(self, &Self::Integer(_))
59+
}
60+
61+
pub fn start(&self) -> SystemTime {
62+
match *self {
63+
Self::Timestamp(t) => t.start(),
64+
_ => unreachable!(),
65+
}
66+
}
67+
68+
pub fn end(&self) -> SystemTime {
69+
match *self {
70+
Self::Timestamp(t) => t.end(),
71+
_ => unreachable!(),
72+
}
73+
}
74+
75+
pub fn timestamp(&self) -> Option<Timestamp> {
76+
match self {
77+
Self::Timestamp(t) => Some(*t),
78+
Self::Integer(_) => None,
79+
}
80+
}
81+
}
82+
83+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
84+
pub enum Timestamp {
85+
Interval { start: SystemTime, end: SystemTime },
86+
Instant(SystemTime),
87+
}
88+
89+
impl Timestamp {
90+
pub fn from_raw_event(raw_event: &RawEvent, start_time: SystemTime) -> Self {
91+
debug_assert!(!raw_event.is_integer());
92+
if raw_event.is_instant() {
93+
let t = start_time + Duration::from_nanos(raw_event.start_value());
94+
Self::Instant(t)
95+
} else {
96+
let start = start_time + Duration::from_nanos(raw_event.start_value());
97+
let end = start_time + Duration::from_nanos(raw_event.end_value());
98+
Timestamp::Interval { start, end }
99+
}
100+
}
101+
102+
pub fn contains(&self, t: SystemTime) -> bool {
103+
match *self {
104+
Timestamp::Interval { start, end } => t >= start && t < end,
105+
Timestamp::Instant(_) => false,
106+
}
107+
}
108+
109+
pub fn is_instant(&self) -> bool {
110+
matches!(self, &Timestamp::Instant(_))
111+
}
112+
113+
pub fn start(&self) -> SystemTime {
114+
match *self {
115+
Timestamp::Interval { start, .. } => start,
116+
Timestamp::Instant(t) => t,
117+
}
118+
}
119+
120+
pub fn end(&self) -> SystemTime {
121+
match *self {
122+
Timestamp::Interval { end, .. } => end,
123+
Timestamp::Instant(t) => t,
124+
}
125+
}
126+
}

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: 22 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,23 @@ 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+
pub fn start(&self) -> SystemTime {
31+
self.payload.start()
32+
}
33+
34+
pub fn end(&self) -> SystemTime {
35+
self.payload.end()
36+
}
37+
38+
pub fn timestamp(&self) -> Option<Timestamp> {
39+
self.payload.timestamp()
4340
}
4441
}
4542

@@ -49,20 +46,20 @@ impl<'a> PartialEq for LightweightEvent<'a> {
4946
data,
5047
event_index,
5148
thread_id,
52-
timestamp,
49+
payload,
5350
} = *self;
5451

5552
let LightweightEvent {
5653
data: other_data,
5754
event_index: other_event_index,
5855
thread_id: other_thread_id,
59-
timestamp: other_timestamp,
56+
payload: other_payload,
6057
} = *other;
6158

6259
std::ptr::eq(data, other_data)
6360
&& event_index == other_event_index
6461
&& thread_id == other_thread_id
65-
&& timestamp == other_timestamp
62+
&& payload == other_payload
6663
}
6764
}
6865

@@ -74,12 +71,12 @@ impl<'a> Hash for LightweightEvent<'a> {
7471
data,
7572
event_index,
7673
thread_id,
77-
timestamp,
74+
payload,
7875
} = *self;
7976

8077
std::ptr::hash(data, state);
8178
event_index.hash(state);
8279
thread_id.hash(state);
83-
timestamp.hash(state);
80+
payload.hash(state);
8481
}
8582
}

0 commit comments

Comments
 (0)