Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 4 additions & 19 deletions analyzeme/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::timestamp::Timestamp;
use crate::event_payload::EventPayload;
use memchr::memchr;
use std::borrow::Cow;
use std::time::Duration;
Expand All @@ -8,34 +8,19 @@ pub struct Event<'a> {
pub event_kind: Cow<'a, str>,
pub label: Cow<'a, str>,
pub additional_data: Vec<Cow<'a, str>>,
pub timestamp: Timestamp,
pub payload: EventPayload,
pub thread_id: u32,
}

impl<'a> Event<'a> {
/// Returns true if the time interval of `self` completely contains the
/// time interval of `other`.
pub fn contains(&self, other: &Event<'_>) -> bool {
match self.timestamp {
Timestamp::Interval {
start: self_start,
end: self_end,
} => match other.timestamp {
Timestamp::Interval {
start: other_start,
end: other_end,
} => self_start <= other_start && other_end <= self_end,
Timestamp::Instant(other_t) => self_start <= other_t && other_t <= self_end,
},
Timestamp::Instant(_) => false,
}
self.payload.contains(&other.payload)
}

pub fn duration(&self) -> Option<Duration> {
match self.timestamp {
Timestamp::Interval { start, end } => end.duration_since(start).ok(),
Timestamp::Instant(_) => None,
}
self.payload.duration()
}

pub(crate) fn parse_event_id(event_id: Cow<'a, str>) -> (Cow<'a, str>, Vec<Cow<'a, str>>) {
Expand Down
117 changes: 117 additions & 0 deletions analyzeme/src/event_payload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use measureme::RawEvent;
use std::time::{Duration, SystemTime};

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum EventPayload {
Timestamp(Timestamp),
Integer(u64),
}

impl EventPayload {
pub fn from_raw_event(raw_event: &RawEvent, start_time: SystemTime) -> Self {
if raw_event.is_integer() {
Self::Integer(raw_event.value())
} else {
Self::Timestamp(Timestamp::from_raw_event(raw_event, start_time))
}
}

/// Returns true if the time interval of `self` completely contains the
/// time interval of `other`.
pub fn contains(&self, other: &Self) -> bool {
match self {
EventPayload::Timestamp(Timestamp::Interval {
start: self_start,
end: self_end,
}) => match other {
EventPayload::Timestamp(Timestamp::Interval {
start: other_start,
end: other_end,
}) => self_start <= other_start && other_end <= self_end,
EventPayload::Timestamp(Timestamp::Instant(other_t)) => {
self_start <= other_t && other_t <= self_end
}
EventPayload::Integer(_) => false,
},
_ => false,
}
}

pub fn duration(&self) -> Option<Duration> {
match *self {
EventPayload::Timestamp(t) => t.duration(),
_ => None,
}
}

pub fn is_interval(&self) -> bool {
matches!(self, &Self::Timestamp(Timestamp::Interval { .. }))
}

pub fn is_instant(&self) -> bool {
matches!(self, &Self::Timestamp(Timestamp::Instant(_)))
}

pub fn is_integer(&self) -> bool {
matches!(self, &Self::Integer(_))
}

pub fn timestamp(&self) -> Option<Timestamp> {
match self {
Self::Timestamp(t) => Some(*t),
Self::Integer(_) => None,
}
}
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum Timestamp {
Interval { start: SystemTime, end: SystemTime },
Instant(SystemTime),
}

impl Timestamp {
pub fn from_raw_event(raw_event: &RawEvent, start_time: SystemTime) -> Self {
debug_assert!(!raw_event.is_integer());
if raw_event.is_instant() {
let t = start_time + Duration::from_nanos(raw_event.start_value());
Self::Instant(t)
} else {
let start = start_time + Duration::from_nanos(raw_event.start_value());
let end = start_time + Duration::from_nanos(raw_event.end_value());
Timestamp::Interval { start, end }
}
}

pub fn contains(&self, t: SystemTime) -> bool {
match *self {
Timestamp::Interval { start, end } => t >= start && t < end,
Timestamp::Instant(_) => false,
}
}

pub fn is_instant(&self) -> bool {
matches!(self, &Timestamp::Instant(_))
}

pub fn start(&self) -> SystemTime {
match *self {
Timestamp::Interval { start, .. } => start,
Timestamp::Instant(t) => t,
}
}

pub fn end(&self) -> SystemTime {
match *self {
Timestamp::Interval { end, .. } => end,
Timestamp::Instant(t) => t,
}
}

pub fn duration(&self) -> Option<Duration> {
match *self {
Timestamp::Interval { start, end } => end.duration_since(start).ok(),
_ => None,
}
}
}
4 changes: 2 additions & 2 deletions analyzeme/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
//! call the [`ProfilingData::iter()`] method.

mod event;
mod event_payload;
mod lightweight_event;
mod profiling_data;
mod stack_collapse;
mod stringtable;
pub mod testing_common;
mod timestamp;

pub use crate::event::Event;
pub use crate::event_payload::{EventPayload, Timestamp};
pub use crate::lightweight_event::LightweightEvent;
pub use crate::profiling_data::{ProfilingData, ProfilingDataBuilder};
pub use crate::stack_collapse::collapse_stacks;
pub use crate::stringtable::{StringRef, StringTable};
pub use crate::timestamp::Timestamp;
49 changes: 24 additions & 25 deletions analyzeme/src/lightweight_event.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::event::Event;
use crate::event_payload::{EventPayload, Timestamp};
use crate::profiling_data::ProfilingData;
use crate::timestamp::Timestamp;
use std::hash::{Hash, Hasher};
use std::time::Duration;
use std::time::{Duration, SystemTime};

#[derive(Clone, Debug)]
pub struct LightweightEvent<'a> {
pub data: &'a ProfilingData,
pub event_index: usize,
pub thread_id: u32,
pub timestamp: Timestamp,
pub payload: EventPayload,
}

impl<'a> LightweightEvent<'a> {
Expand All @@ -20,26 +20,25 @@ impl<'a> LightweightEvent<'a> {
/// Returns true if the time interval of `self` completely contains the
/// time interval of `other`.
pub fn contains(&self, other: &LightweightEvent) -> bool {
match self.timestamp {
Timestamp::Interval {
start: self_start,
end: self_end,
} => match other.timestamp {
Timestamp::Interval {
start: other_start,
end: other_end,
} => self_start <= other_start && other_end <= self_end,
Timestamp::Instant(other_t) => self_start <= other_t && other_t <= self_end,
},
Timestamp::Instant(_) => false,
}
self.payload.contains(&other.payload)
}

pub fn duration(&self) -> Option<Duration> {
match self.timestamp {
Timestamp::Interval { start, end } => end.duration_since(start).ok(),
Timestamp::Instant(_) => None,
}
self.payload.duration()
}

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

// Returns end time if event is a timestamp
pub fn end(&self) -> Option<SystemTime> {
self.payload.timestamp().map(|t| t.end())
}

pub fn timestamp(&self) -> Option<Timestamp> {
self.payload.timestamp()
}
}

Expand All @@ -49,20 +48,20 @@ impl<'a> PartialEq for LightweightEvent<'a> {
data,
event_index,
thread_id,
timestamp,
payload,
} = *self;

let LightweightEvent {
data: other_data,
event_index: other_event_index,
thread_id: other_thread_id,
timestamp: other_timestamp,
payload: other_payload,
} = *other;

std::ptr::eq(data, other_data)
&& event_index == other_event_index
&& thread_id == other_thread_id
&& timestamp == other_timestamp
&& payload == other_payload
}
}

Expand All @@ -74,12 +73,12 @@ impl<'a> Hash for LightweightEvent<'a> {
data,
event_index,
thread_id,
timestamp,
payload,
} = *self;

std::ptr::hash(data, state);
event_index.hash(state);
thread_id.hash(state);
timestamp.hash(state);
payload.hash(state);
}
}
Loading