Skip to content

Commit

Permalink
trace-server: performance improvement (#8147)
Browse files Browse the repository at this point in the history
### Description

- avoid invalidating self time tree spans when they don't have been
accessed
- used multiple core for some computations 

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->
  • Loading branch information
sokra authored May 17, 2024
1 parent a562144 commit 14cb94b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/turbopack-trace-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ flate2 = { version = "1.0.28" }
indexmap = { workspace = true, features = ["serde"] }
itertools = { workspace = true }
postcard = { workspace = true }
rayon = "1"
rustc-demangle = "0.1"
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-trace-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn handle_connection(
Ok(())
}
loop {
match websocket.read().unwrap() {
match websocket.read()? {
Message::Frame(_frame) => {}
Message::Text(text) => {
let message: ClientToServerMessage = serde_json::from_str(&text)?;
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-trace-server/src/span_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ impl<'a> SpanRef<'a> {
if duration == 0 {
continue;
}
store.set_max_self_time_lookup(*end);
let concurrent_time = store.self_time_tree.lookup_range_count(*start, *end);
self_time += duration * duration / concurrent_time;
}
Expand Down
33 changes: 28 additions & 5 deletions crates/turbopack-trace-server/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
collections::HashSet,
mem::replace,
num::NonZeroUsize,
sync::OnceLock,
sync::{atomic::AtomicU64, OnceLock},
};

use crate::{
Expand All @@ -19,6 +19,7 @@ const CUT_OFF_DEPTH: u32 = 150;
pub struct Store {
pub(crate) spans: Vec<Span>,
pub(crate) self_time_tree: SelfTimeTree<SpanIndex>,
max_self_time_lookup_time: AtomicU64,
}

fn new_root_span() -> Span {
Expand Down Expand Up @@ -52,13 +53,15 @@ impl Store {
Self {
spans: vec![new_root_span()],
self_time_tree: SelfTimeTree::new(),
max_self_time_lookup_time: AtomicU64::new(0),
}
}

pub fn reset(&mut self) {
self.spans.truncate(1);
self.spans[0] = new_root_span();
self.self_time_tree = SelfTimeTree::new();
*self.max_self_time_lookup_time.get_mut() = 0;
}

pub fn has_time_info(&self) -> bool {
Expand Down Expand Up @@ -125,17 +128,36 @@ impl Store {
outdated_spans.insert(span_index);
}

pub fn set_max_self_time_lookup(&self, time: u64) {
let mut old = self
.max_self_time_lookup_time
.load(std::sync::atomic::Ordering::Relaxed);
while old < time {
match self.max_self_time_lookup_time.compare_exchange(
old,
time,
std::sync::atomic::Ordering::Relaxed,
std::sync::atomic::Ordering::Relaxed,
) {
Ok(_) => break,
Err(real_old) => old = real_old,
}
}
}

fn insert_self_time(
&mut self,
start: u64,
end: u64,
span_index: SpanIndex,
outdated_spans: &mut HashSet<SpanIndex>,
) {
self.self_time_tree
.for_each_in_range(start, end, |_, _, span| {
outdated_spans.insert(*span);
});
if *self.max_self_time_lookup_time.get_mut() >= start {
self.self_time_tree
.for_each_in_range(start, end, |_, _, span| {
outdated_spans.insert(*span);
});
}
self.self_time_tree.insert(start, end, span_index);
}

Expand Down Expand Up @@ -295,6 +317,7 @@ impl Store {
span.total_deallocations.take();
span.total_persistent_allocations.take();
span.total_allocation_count.take();
span.total_span_count.take();
span.extra.take();
}

Expand Down
45 changes: 45 additions & 0 deletions crates/turbopack-trace-server/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use either::Either;
use itertools::Itertools;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize};

use crate::{
Expand Down Expand Up @@ -361,6 +362,10 @@ impl Viewer {
.iter()
.min_by_key(|span| span.start())
.map_or(0, |span| span.start());
root_spans.par_iter().for_each(|span| {
span.max_depth();
QueueItem::Span(*span).value(value_mode);
});
for span in root_spans {
if matches!(value_mode, ValueMode::Duration) {
// Move current to start if needed.
Expand Down Expand Up @@ -396,6 +401,46 @@ impl Viewer {
}
}
enqueue_children(children, &mut queue);
queue.par_iter().for_each(|item| {
let QueueItem::Span(span) = item.item else {
return;
};
let view_mode = if span.is_complete() {
item.view_mode
} else {
item.view_mode.as_spans()
};

match (view_mode.bottom_up(), view_mode.aggregate_children()) {
(false, false) => {}
(false, true) => {
span.graph()
.collect::<Vec<_>>()
.par_iter()
.for_each(|event| {
value_mode.value_from_graph_event(event);
});
}
(true, false) => {
span.bottom_up()
.collect::<Vec<_>>()
.par_iter()
.for_each(|bu| {
bu.spans().collect::<Vec<_>>().par_iter().for_each(|span| {
value_mode.value_from_bottom_up_span(span);
});
});
}
(true, true) => {
span.bottom_up()
.collect::<Vec<_>>()
.par_iter()
.for_each(|bu| {
value_mode.value_from_bottom_up(bu);
});
}
}
});

let mut lines: Vec<Vec<LineEntry<'_>>> = vec![];

Expand Down

0 comments on commit 14cb94b

Please sign in to comment.