Skip to content

Commit

Permalink
correct global start by first start time (#7835)
Browse files Browse the repository at this point in the history
### Description

offset trace by first span

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->


Closes PACK-2837
  • Loading branch information
sokra committed Mar 25, 2024
1 parent 4fac8a6 commit b42e39e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 55 deletions.
98 changes: 50 additions & 48 deletions crates/turbopack-trace-server/src/server.rs
Expand Up @@ -276,54 +276,56 @@ pub fn serve(store: Arc<StoreContainer>) -> Result<()> {
)?;
}
ClientToServerMessage::Query { id } => {
let message = if let Some((span, is_graph)) =
state.store.read().span(id)
{
let span_start = span.start();
let span_end = span.end();
let duration = span.corrected_total_time();
let allocations = span.total_allocations();
let deallocations = span.total_deallocations();
let allocation_count = span.total_allocation_count();
let persistent_allocations =
span.total_persistent_allocations();
let args = span
.args()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
let mut path = Vec::new();
let mut current = span;
while let Some(parent) = current.parent() {
path.push(parent.nice_name().1.to_string());
current = parent;
}
path.reverse();
ServerToClientMessage::QueryResult {
id,
is_graph,
start: span_start,
end: span_end,
duration,
allocations,
deallocations,
allocation_count,
persistent_allocations,
args,
path,
}
} else {
ServerToClientMessage::QueryResult {
id,
is_graph: false,
start: 0,
end: 0,
duration: 0,
allocations: 0,
deallocations: 0,
allocation_count: 0,
persistent_allocations: 0,
args: Vec::new(),
path: Vec::new(),
let message = {
let store = state.store.read();
if let Some((span, is_graph)) = store.span(id) {
let root_start = store.root_span().start();
let span_start = span.start() - root_start;
let span_end = span.end() - root_start;
let duration = span.corrected_total_time();
let allocations = span.total_allocations();
let deallocations = span.total_deallocations();
let allocation_count = span.total_allocation_count();
let persistent_allocations =
span.total_persistent_allocations();
let args = span
.args()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
let mut path = Vec::new();
let mut current = span;
while let Some(parent) = current.parent() {
path.push(parent.nice_name().1.to_string());
current = parent;
}
path.reverse();
ServerToClientMessage::QueryResult {
id,
is_graph,
start: span_start,
end: span_end,
duration,
allocations,
deallocations,
allocation_count,
persistent_allocations,
args,
path,
}
} else {
ServerToClientMessage::QueryResult {
id,
is_graph: false,
start: 0,
end: 0,
duration: 0,
allocations: 0,
deallocations: 0,
allocation_count: 0,
persistent_allocations: 0,
args: Vec::new(),
path: Vec::new(),
}
}
};
let message = serde_json::to_string(&message).unwrap();
Expand Down
29 changes: 22 additions & 7 deletions crates/turbopack-trace-server/src/store.rs
@@ -1,4 +1,10 @@
use std::{cmp::max, collections::HashSet, mem::replace, num::NonZeroUsize, sync::OnceLock};
use std::{
cmp::{max, min},
collections::HashSet,
mem::replace,
num::NonZeroUsize,
sync::OnceLock,
};

use crate::{
span::{Span, SpanEvent, SpanIndex},
Expand All @@ -18,7 +24,7 @@ fn new_root_span() -> Span {
index: SpanIndex::MAX,
parent: None,
depth: 0,
start: 0,
start: u64::MAX,
ignore_self_time: false,
self_end: 0,
category: "".into(),
Expand Down Expand Up @@ -110,6 +116,7 @@ impl Store {
} else {
&mut self.spans[0]
};
parent.start = min(parent.start, start);
let depth = parent.depth + 1;
if depth < CUT_OFF_DEPTH {
parent.events.push(SpanEvent::Child { id });
Expand Down Expand Up @@ -154,8 +161,6 @@ impl Store {
total_time: u64,
outdated_spans: &mut HashSet<SpanIndex>,
) {
self.invalidate_outdated_spans(outdated_spans);
outdated_spans.clear();
let span = SpanRef {
span: &self.spans[span_index.get()],
store: self,
Expand All @@ -165,11 +170,20 @@ impl Store {
.map(|c| (c.span.start, c.span.self_end, c.span.index))
.collect::<Vec<_>>();
children.sort();
let self_end = start_time + total_time;
let mut self_time = 0;
let mut current = start_time;
let mut events = Vec::new();
for (start, end, index) in children {
if start > current {
if start > self_end {
events.push(SpanEvent::SelfTime {
start: current,
end: self_end,
});
self_time += self_end - current;
break;
}
events.push(SpanEvent::SelfTime {
start: current,
end: start,
Expand All @@ -179,15 +193,16 @@ impl Store {
events.push(SpanEvent::Child { id: index });
current = max(current, end);
}
if current < start_time + total_time {
self_time += start_time + total_time - current;
current -= start_time;
if current < total_time {
self_time += total_time - current;
}
let span = &mut self.spans[span_index.get()];
outdated_spans.insert(span_index);
span.self_time = self_time;
span.events = events;
span.start = start_time;
span.self_end = start_time + total_time;
span.self_end = self_end;
}

pub fn set_parent(
Expand Down

0 comments on commit b42e39e

Please sign in to comment.