Skip to content

Commit e5d5cec

Browse files
committed
Clean up handling of intervals
1 parent 1cfe8bc commit e5d5cec

File tree

6 files changed

+23
-36
lines changed

6 files changed

+23
-36
lines changed

analyzeme/src/event.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::event_payload::EventPayload;
22
use memchr::memchr;
33
use std::borrow::Cow;
4-
use std::time::{Duration, SystemTime};
4+
use std::time::Duration;
55

66
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
77
pub struct Event<'a> {
@@ -23,14 +23,6 @@ impl<'a> Event<'a> {
2323
self.payload.duration()
2424
}
2525

26-
pub fn start(&self) -> SystemTime {
27-
self.payload.start()
28-
}
29-
30-
pub fn end(&self) -> SystemTime {
31-
self.payload.end()
32-
}
33-
3426
pub(crate) fn parse_event_id(event_id: Cow<'a, str>) -> (Cow<'a, str>, Vec<Cow<'a, str>>) {
3527
let event_id = match event_id {
3628
Cow::Owned(s) => Cow::Owned(s.into_bytes()),

analyzeme/src/event_payload.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ impl EventPayload {
3939

4040
pub fn duration(&self) -> Option<Duration> {
4141
match *self {
42-
EventPayload::Timestamp(Timestamp::Interval { start, end }) => {
43-
end.duration_since(start).ok()
44-
}
42+
EventPayload::Timestamp(t) => t.duration(),
4543
_ => None,
4644
}
4745
}
@@ -58,20 +56,6 @@ impl EventPayload {
5856
matches!(self, &Self::Integer(_))
5957
}
6058

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-
7559
pub fn timestamp(&self) -> Option<Timestamp> {
7660
match self {
7761
Self::Timestamp(t) => Some(*t),
@@ -123,4 +107,11 @@ impl Timestamp {
123107
Timestamp::Instant(t) => t,
124108
}
125109
}
110+
111+
pub fn duration(&self) -> Option<Duration> {
112+
match *self {
113+
Timestamp::Interval { start, end } => end.duration_since(start).ok(),
114+
_ => None,
115+
}
116+
}
126117
}

analyzeme/src/lightweight_event.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ impl<'a> LightweightEvent<'a> {
2727
self.payload.duration()
2828
}
2929

30-
pub fn start(&self) -> SystemTime {
31-
self.payload.start()
30+
// Returns start time if event is a timestamp
31+
pub fn start(&self) -> Option<SystemTime> {
32+
self.payload.timestamp().map(|t| t.start())
3233
}
3334

34-
pub fn end(&self) -> SystemTime {
35-
self.payload.end()
35+
// Returns end time if event is a timestamp
36+
pub fn end(&self) -> Option<SystemTime> {
37+
self.payload.timestamp().map(|t| t.end())
3638
}
3739

3840
pub fn timestamp(&self) -> Option<Timestamp> {

analyzeme/src/stack_collapse.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ pub fn collapse_stacks<'a>(profiling_data: &ProfilingData) -> FxHashMap<String,
2626
.rev()
2727
.filter(|e| e.payload.is_interval())
2828
{
29+
let start = current_event.start().unwrap();
30+
let end = current_event.end().unwrap();
2931
let thread = threads
3032
.entry(current_event.thread_id)
3133
.or_insert(PerThreadState {
3234
stack: Vec::new(),
3335
stack_id: "rustc".to_owned(),
34-
start: current_event.start(),
35-
end: current_event.end(),
36+
start,
37+
end,
3638
total_event_time_nanos: 0,
3739
});
3840

39-
thread.start = cmp::min(thread.start, current_event.start());
41+
thread.start = cmp::min(thread.start, start);
4042

4143
// Pop all events from the stack that are not parents of the
4244
// current event.

crox/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
152152

153153
// Chrome does not seem to like how many QueryCacheHit events we generate
154154
// only handle Interval events for now
155-
for event in data.iter().filter(|e| !e.payload.is_instant()) {
155+
for event in data.iter().filter(|e| e.payload.is_interval()) {
156156
let duration = event.duration().unwrap();
157157
if let Some(minimum_duration) = opt.minimum_duration {
158158
if duration.as_micros() < minimum_duration {
@@ -164,7 +164,7 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
164164
name: full_event.label.clone().into_owned(),
165165
category: full_event.event_kind.clone().into_owned(),
166166
event_type: EventType::Complete,
167-
timestamp: event.payload.start().duration_since(UNIX_EPOCH).unwrap(),
167+
timestamp: event.start().unwrap().duration_since(UNIX_EPOCH).unwrap(),
168168
duration,
169169
process_id: data.metadata.process_id,
170170
thread_id: *thread_to_collapsed_thread

mmview/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
1818

1919
let data = ProfilingData::new(&opt.file_prefix)?;
2020

21-
if let Some(global_start_time) = data.iter().map(|e| e.payload.start()).min() {
21+
if let Some(global_start_time) = data.iter().filter_map(|e| e.start()).min() {
2222
for event in data.iter() {
2323
if let Some(thread_id) = opt.thread_id {
2424
if event.thread_id != thread_id {

0 commit comments

Comments
 (0)