Skip to content

Commit b42e39e

Browse files
authored
correct global start by first start time (#7835)
### Description offset trace by first span ### Testing Instructions <!-- Give a quick description of steps to test your changes. --> Closes PACK-2837
1 parent 4fac8a6 commit b42e39e

File tree

2 files changed

+72
-55
lines changed

2 files changed

+72
-55
lines changed

crates/turbopack-trace-server/src/server.rs

+50-48
Original file line numberDiff line numberDiff line change
@@ -276,54 +276,56 @@ pub fn serve(store: Arc<StoreContainer>) -> Result<()> {
276276
)?;
277277
}
278278
ClientToServerMessage::Query { id } => {
279-
let message = if let Some((span, is_graph)) =
280-
state.store.read().span(id)
281-
{
282-
let span_start = span.start();
283-
let span_end = span.end();
284-
let duration = span.corrected_total_time();
285-
let allocations = span.total_allocations();
286-
let deallocations = span.total_deallocations();
287-
let allocation_count = span.total_allocation_count();
288-
let persistent_allocations =
289-
span.total_persistent_allocations();
290-
let args = span
291-
.args()
292-
.map(|(k, v)| (k.to_string(), v.to_string()))
293-
.collect();
294-
let mut path = Vec::new();
295-
let mut current = span;
296-
while let Some(parent) = current.parent() {
297-
path.push(parent.nice_name().1.to_string());
298-
current = parent;
299-
}
300-
path.reverse();
301-
ServerToClientMessage::QueryResult {
302-
id,
303-
is_graph,
304-
start: span_start,
305-
end: span_end,
306-
duration,
307-
allocations,
308-
deallocations,
309-
allocation_count,
310-
persistent_allocations,
311-
args,
312-
path,
313-
}
314-
} else {
315-
ServerToClientMessage::QueryResult {
316-
id,
317-
is_graph: false,
318-
start: 0,
319-
end: 0,
320-
duration: 0,
321-
allocations: 0,
322-
deallocations: 0,
323-
allocation_count: 0,
324-
persistent_allocations: 0,
325-
args: Vec::new(),
326-
path: Vec::new(),
279+
let message = {
280+
let store = state.store.read();
281+
if let Some((span, is_graph)) = store.span(id) {
282+
let root_start = store.root_span().start();
283+
let span_start = span.start() - root_start;
284+
let span_end = span.end() - root_start;
285+
let duration = span.corrected_total_time();
286+
let allocations = span.total_allocations();
287+
let deallocations = span.total_deallocations();
288+
let allocation_count = span.total_allocation_count();
289+
let persistent_allocations =
290+
span.total_persistent_allocations();
291+
let args = span
292+
.args()
293+
.map(|(k, v)| (k.to_string(), v.to_string()))
294+
.collect();
295+
let mut path = Vec::new();
296+
let mut current = span;
297+
while let Some(parent) = current.parent() {
298+
path.push(parent.nice_name().1.to_string());
299+
current = parent;
300+
}
301+
path.reverse();
302+
ServerToClientMessage::QueryResult {
303+
id,
304+
is_graph,
305+
start: span_start,
306+
end: span_end,
307+
duration,
308+
allocations,
309+
deallocations,
310+
allocation_count,
311+
persistent_allocations,
312+
args,
313+
path,
314+
}
315+
} else {
316+
ServerToClientMessage::QueryResult {
317+
id,
318+
is_graph: false,
319+
start: 0,
320+
end: 0,
321+
duration: 0,
322+
allocations: 0,
323+
deallocations: 0,
324+
allocation_count: 0,
325+
persistent_allocations: 0,
326+
args: Vec::new(),
327+
path: Vec::new(),
328+
}
327329
}
328330
};
329331
let message = serde_json::to_string(&message).unwrap();

crates/turbopack-trace-server/src/store.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
use std::{cmp::max, collections::HashSet, mem::replace, num::NonZeroUsize, sync::OnceLock};
1+
use std::{
2+
cmp::{max, min},
3+
collections::HashSet,
4+
mem::replace,
5+
num::NonZeroUsize,
6+
sync::OnceLock,
7+
};
28

39
use crate::{
410
span::{Span, SpanEvent, SpanIndex},
@@ -18,7 +24,7 @@ fn new_root_span() -> Span {
1824
index: SpanIndex::MAX,
1925
parent: None,
2026
depth: 0,
21-
start: 0,
27+
start: u64::MAX,
2228
ignore_self_time: false,
2329
self_end: 0,
2430
category: "".into(),
@@ -110,6 +116,7 @@ impl Store {
110116
} else {
111117
&mut self.spans[0]
112118
};
119+
parent.start = min(parent.start, start);
113120
let depth = parent.depth + 1;
114121
if depth < CUT_OFF_DEPTH {
115122
parent.events.push(SpanEvent::Child { id });
@@ -154,8 +161,6 @@ impl Store {
154161
total_time: u64,
155162
outdated_spans: &mut HashSet<SpanIndex>,
156163
) {
157-
self.invalidate_outdated_spans(outdated_spans);
158-
outdated_spans.clear();
159164
let span = SpanRef {
160165
span: &self.spans[span_index.get()],
161166
store: self,
@@ -165,11 +170,20 @@ impl Store {
165170
.map(|c| (c.span.start, c.span.self_end, c.span.index))
166171
.collect::<Vec<_>>();
167172
children.sort();
173+
let self_end = start_time + total_time;
168174
let mut self_time = 0;
169175
let mut current = start_time;
170176
let mut events = Vec::new();
171177
for (start, end, index) in children {
172178
if start > current {
179+
if start > self_end {
180+
events.push(SpanEvent::SelfTime {
181+
start: current,
182+
end: self_end,
183+
});
184+
self_time += self_end - current;
185+
break;
186+
}
173187
events.push(SpanEvent::SelfTime {
174188
start: current,
175189
end: start,
@@ -179,15 +193,16 @@ impl Store {
179193
events.push(SpanEvent::Child { id: index });
180194
current = max(current, end);
181195
}
182-
if current < start_time + total_time {
183-
self_time += start_time + total_time - current;
196+
current -= start_time;
197+
if current < total_time {
198+
self_time += total_time - current;
184199
}
185200
let span = &mut self.spans[span_index.get()];
186201
outdated_spans.insert(span_index);
187202
span.self_time = self_time;
188203
span.events = events;
189204
span.start = start_time;
190-
span.self_end = start_time + total_time;
205+
span.self_end = self_end;
191206
}
192207

193208
pub fn set_parent(

0 commit comments

Comments
 (0)