Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
51 changes: 47 additions & 4 deletions turbopack/crates/turbopack-trace-server/src/span_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,13 @@ impl<'a> SpanRef<'a> {

// TODO(sokra) use events instead of children for visualizing span graphs
#[allow(dead_code)]
pub fn events(&self) -> impl Iterator<Item = SpanEventRef<'a>> {
pub fn events(&self) -> impl DoubleEndedIterator<Item = SpanEventRef<'a>> {
self.span.events.iter().map(|event| match event {
&SpanEvent::SelfTime { start, end } => SpanEventRef::SelfTime { start, end },
&SpanEvent::SelfTime { start, end } => SpanEventRef::SelfTime {
store: self.store,
start,
end,
},
SpanEvent::Child { index } => SpanEventRef::Child {
span: SpanRef {
span: &self.store.spans[index.get()],
Expand Down Expand Up @@ -545,6 +549,45 @@ impl Debug for SpanRef<'_> {
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum SpanEventRef<'a> {
SelfTime { start: Timestamp, end: Timestamp },
Child { span: SpanRef<'a> },
SelfTime {
store: &'a Store,
start: Timestamp,
end: Timestamp,
},
Child {
span: SpanRef<'a>,
},
}

impl SpanEventRef<'_> {
pub fn start(&self) -> Timestamp {
match self {
SpanEventRef::SelfTime { start, .. } => *start,
SpanEventRef::Child { span } => span.start(),
}
}

pub fn total_time(&self) -> Timestamp {
match self {
SpanEventRef::SelfTime { start, end, .. } => end.saturating_sub(*start),
SpanEventRef::Child { span } => span.total_time(),
}
}

pub fn corrected_self_time(&self) -> Timestamp {
match self {
SpanEventRef::SelfTime { store, start, end } => {
let duration = *end - *start;
if !duration.is_zero() {
store.set_max_self_time_lookup(*end);
store.self_time_tree.as_ref().map_or(duration, |tree| {
tree.lookup_range_corrected_time(*start, *end)
})
} else {
Timestamp::ZERO
}
}
SpanEventRef::Child { span } => span.corrected_self_time(),
}
}
}
48 changes: 33 additions & 15 deletions turbopack/crates/turbopack-trace-server/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
server::ViewRect,
span_bottom_up_ref::SpanBottomUpRef,
span_graph_ref::{SpanGraphEventRef, SpanGraphRef},
span_ref::SpanRef,
span_ref::{SpanEventRef, SpanRef},
store::{SpanId, Store},
timestamp::Timestamp,
u64_empty_string,
Expand Down Expand Up @@ -76,6 +76,17 @@ impl ValueMode {
}
}

fn value_from_event(&self, event: &SpanEventRef<'_>) -> u64 {
match self {
ValueMode::Duration => *event.corrected_self_time(),
ValueMode::Cpu => *event.total_time(),
_ => match event {
SpanEventRef::Child { span } => self.value_from_span(span),
SpanEventRef::SelfTime { .. } => 0,
},
}
}

fn value_from_graph(&self, graph: &SpanGraphRef<'_>) -> u64 {
match self {
ValueMode::Duration => *graph.corrected_total_time(),
Expand Down Expand Up @@ -651,24 +662,31 @@ impl Viewer {
}
} else if !selected_view_mode.aggregate_children() {
let spans = if selected_view_mode.sort_children() {
Either::Left(span.children().sorted_by_cached_key(|child| {
Reverse(value_mode.value_from_span(child))
Either::Left(span.events().sorted_by_cached_key(|child| {
Reverse(value_mode.value_from_event(child))
}))
} else {
Either::Right(span.children().sorted_by_key(|child| child.start()))
Either::Right(span.events().sorted_by_key(|child| child.start()))
};
for child in spans {
let filtered = get_filter_mode(child.id());
add_child_item(
&mut children,
&mut current,
view_rect,
child_line_index,
view_mode,
value_mode,
QueueItem::Span(child),
filtered,
);
match child {
SpanEventRef::SelfTime { .. } => {
current += value_mode.value_from_event(&child);
}
SpanEventRef::Child { span: child } => {
let filtered = get_filter_mode(child.id());
add_child_item(
&mut children,
&mut current,
view_rect,
child_line_index,
view_mode,
value_mode,
QueueItem::Span(child),
filtered,
);
}
}
}
} else {
let events = if selected_view_mode.sort_children() {
Expand Down
Loading