Unverified Commit e51db393 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Sample global trace concurrency across span ranges (#99254)

## Summary

Show how much traced work was happening concurrently throughout a
selected span. The trace server divides its range into up to 200
equal-duration segments and returns the time-weighted average number of
active global self-time intervals per segment as `concurrencySamples` in
the WebSocket query result. It traverses the self-time tree once and
rounds averages to hundredths.

**Depends on [#99233](https://github.com/vercel/next.js/pull/99233)**
and targets its branch so this PR only adds derived concurrency samples.
The matching [trace-viewer
PR](https://github.com/vercel/turbo-trace-viewer/pull/36) graphs the new
values.

## Verification

- `cargo test --locked -p turbopack-trace-server` (28 library and 27
binary tests pass)
- `cargo check --locked -p next-napi-bindings`
- Local native build, `pnpm build-all`, and traced
`bench/heavy-npm-deps` Turbopack build passed. The generated 272 MB
trace contained 851,901 spans and showed nonzero concurrency for the
selected top-level span in the updated viewer.

<!-- NEXT_JS_LLM -->


<!-- fleet 0c297c2c-8e55-4012-be8a-752ff8175d1b -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 202bba59
...@@ -288,6 +288,61 @@ impl<T> SelfTimeTree<T> { ...@@ -288,6 +288,61 @@ impl<T> SelfTimeTree<T> {
Timestamp::from_value(factor_times_1000 / 1000) Timestamp::from_value(factor_times_1000 / 1000)
} }
/// Time-weighted average number of active self-time intervals per segment.
/// Enumerates the tree only once, using a difference array for intervals
/// that span whole segments instead of visiting each such segment.
pub fn lookup_range_concurrency_samples(
&self,
start: Timestamp,
end: Timestamp,
max_samples: usize,
) -> Vec<f64> {
if start >= end || max_samples == 0 {
return Vec::new();
}
let duration = end - start;
let count = max_samples.min(usize::try_from(*duration).unwrap_or(usize::MAX));
let boundaries: Vec<u64> = (0..=count)
.map(|i| *start + (u128::from(*duration) * i as u128 / count as u128) as u64)
.collect();
let mut partial_ticks = vec![0u128; count];
let mut full_segment_deltas = vec![0i64; count + 1];
self.for_each_in_range(start, end, &mut |interval_start, interval_end, _| {
let from = (*interval_start).max(*start);
let to = (*interval_end).min(*end);
if from >= to {
// The tree also enumerates intervals that only touch a boundary.
return;
}
let first = boundaries.partition_point(|&boundary| boundary <= from) - 1;
let last = boundaries.partition_point(|&boundary| boundary < to) - 1;
if first == last {
partial_ticks[first] += u128::from(to - from);
} else {
partial_ticks[first] += u128::from(boundaries[first + 1] - from);
partial_ticks[last] += u128::from(to - boundaries[last]);
if first + 1 < last {
full_segment_deltas[first + 1] += 1;
full_segment_deltas[last] -= 1;
}
}
});
let mut full_count = 0i64;
(0..count)
.map(|i| {
full_count += full_segment_deltas[i];
debug_assert!(full_count >= 0);
let width = u128::from(boundaries[i + 1] - boundaries[i]);
let total_ticks = partial_ticks[i] + (full_count as u128) * width;
// Round the time-weighted average to hundredths before serializing.
let hundredths = total_ticks.saturating_mul(100).saturating_add(width / 2) / width;
hundredths as f64 / 100.0
})
.collect()
}
pub fn for_each_in_range( pub fn for_each_in_range(
&self, &self,
start: Timestamp, start: Timestamp,
...@@ -458,6 +513,111 @@ mod tests { ...@@ -458,6 +513,111 @@ mod tests {
assert_balanced(&tree); assert_balanced(&tree);
} }
#[test]
fn concurrency_samples_are_time_weighted_and_half_open() {
let mut tree = SelfTimeTree::new();
tree.insert(Timestamp::from_value(0), Timestamp::from_value(4), 0u32);
tree.insert(Timestamp::from_value(1), Timestamp::from_value(3), 1u32);
tree.insert(Timestamp::from_value(4), Timestamp::from_value(5), 2u32);
assert_eq!(
tree.lookup_range_concurrency_samples(
Timestamp::from_value(0),
Timestamp::from_value(4),
2,
),
vec![1.5, 1.5]
);
assert_eq!(
tree.lookup_range_concurrency_samples(
Timestamp::from_value(0),
Timestamp::from_value(4),
4,
),
vec![1.0, 2.0, 2.0, 1.0]
);
}
#[test]
fn concurrency_samples_cover_short_and_empty_ranges() {
let tree: SelfTimeTree<u32> = SelfTimeTree::new();
assert_eq!(
tree.lookup_range_concurrency_samples(
Timestamp::from_value(1),
Timestamp::from_value(4),
200,
),
vec![0.0; 3]
);
assert!(
tree.lookup_range_concurrency_samples(Timestamp::ZERO, Timestamp::ZERO, 200)
.is_empty()
);
}
#[test]
fn concurrency_samples_round_and_handle_long_intervals() {
let mut tree = SelfTimeTree::new();
tree.insert(Timestamp::from_value(0), Timestamp::from_value(200), 0u32);
tree.insert(Timestamp::from_value(0), Timestamp::from_value(1), 1u32);
let samples = tree.lookup_range_concurrency_samples(
Timestamp::from_value(0),
Timestamp::from_value(200),
2,
);
assert_eq!(samples, vec![1.01, 1.0]);
assert_eq!(
tree.lookup_range_concurrency_samples(
Timestamp::from_value(0),
Timestamp::from_value(200),
200,
),
std::iter::once(2.0)
.chain(std::iter::repeat_n(1.0, 199))
.collect::<Vec<_>>()
);
}
#[test]
fn concurrency_samples_match_naive_in_a_split_tree() {
let mut tree = SelfTimeTree::new();
let mut intervals = Vec::new();
for i in 0..500u64 {
let start = 60 + (i * 73) % 1100;
let end = start + 1 + (i * 31) % 150;
tree.insert(Timestamp::from_value(start), Timestamp::from_value(end), i);
intervals.push((start, end));
}
let start = 100u64;
let end = 1103u64;
let count = 200u64;
let actual = tree.lookup_range_concurrency_samples(
Timestamp::from_value(start),
Timestamp::from_value(end),
count as usize,
);
assert_eq!(actual.len(), count as usize);
for (i, &sample) in actual.iter().enumerate() {
let from = start + (end - start) * i as u64 / count;
let to = start + (end - start) * (i as u64 + 1) / count;
let overlapping_ticks: u64 = intervals
.iter()
.map(|&(s, e)| e.min(to).saturating_sub(s.max(from)))
.sum();
let expected =
((overlapping_ticks * 100 + (to - from) / 2) / (to - from)) as f64 / 100.0;
assert_eq!(sample, expected, "segment {i}");
}
}
#[test]
fn concurrency_samples_handle_near_max_timestamp() {
let tree: SelfTimeTree<u32> = SelfTimeTree::new();
assert_eq!(
tree.lookup_range_concurrency_samples(Timestamp::ZERO, Timestamp::MAX, 200),
vec![0.0; 200]
);
}
#[test] #[test]
fn test_corrected_time_no_overlap() { fn test_corrected_time_no_overlap() {
// No intervals at all — corrected time of an empty range is zero. // No intervals at all — corrected time of an empty range is zero.
......
...@@ -46,6 +46,7 @@ pub enum ServerToClientMessage { ...@@ -46,6 +46,7 @@ pub enum ServerToClientMessage {
memory_samples: Vec<u64>, memory_samples: Vec<u64>,
memory_pressure_samples: Vec<u8>, memory_pressure_samples: Vec<u8>,
active_worker_threads_samples: Vec<u64>, active_worker_threads_samples: Vec<u64>,
concurrency_samples: Vec<f64>,
}, },
} }
...@@ -303,6 +304,8 @@ fn handle_connection( ...@@ -303,6 +304,8 @@ fn handle_connection(
span.start(), span.start(),
span.end(), span.end(),
); );
let concurrency_samples =
store.concurrency_samples_for_range(span.start(), span.end());
ServerToClientMessage::QueryResult { ServerToClientMessage::QueryResult {
id, id,
is_graph, is_graph,
...@@ -319,6 +322,7 @@ fn handle_connection( ...@@ -319,6 +322,7 @@ fn handle_connection(
memory_samples, memory_samples,
memory_pressure_samples, memory_pressure_samples,
active_worker_threads_samples, active_worker_threads_samples,
concurrency_samples,
} }
} else { } else {
ServerToClientMessage::QueryResult { ServerToClientMessage::QueryResult {
...@@ -337,6 +341,7 @@ fn handle_connection( ...@@ -337,6 +341,7 @@ fn handle_connection(
memory_samples: Vec::new(), memory_samples: Vec::new(),
memory_pressure_samples: Vec::new(), memory_pressure_samples: Vec::new(),
active_worker_threads_samples: Vec::new(), active_worker_threads_samples: Vec::new(),
concurrency_samples: Vec::new(),
} }
} }
}; };
...@@ -388,7 +393,7 @@ mod tests { ...@@ -388,7 +393,7 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn query_result_serializes_worker_samples_in_camel_case() { fn query_result_serializes_samples_in_camel_case() {
let message = ServerToClientMessage::QueryResult { let message = ServerToClientMessage::QueryResult {
id: SpanId::new(1).unwrap(), id: SpanId::new(1).unwrap(),
is_graph: false, is_graph: false,
...@@ -405,10 +410,12 @@ mod tests { ...@@ -405,10 +410,12 @@ mod tests {
memory_samples: vec![1024], memory_samples: vec![1024],
memory_pressure_samples: vec![2], memory_pressure_samples: vec![2],
active_worker_threads_samples: vec![3], active_worker_threads_samples: vec![3],
concurrency_samples: vec![1.25],
}; };
let json = serde_json::to_value(message).unwrap(); let json = serde_json::to_value(message).unwrap();
assert_eq!(json["type"], "query-result"); assert_eq!(json["type"], "query-result");
assert_eq!(json["memorySamples"], serde_json::json!([1024])); assert_eq!(json["memorySamples"], serde_json::json!([1024]));
assert_eq!(json["activeWorkerThreadsSamples"], serde_json::json!([3])); assert_eq!(json["activeWorkerThreadsSamples"], serde_json::json!([3]));
assert_eq!(json["concurrencySamples"], serde_json::json!([1.25]));
} }
} }
...@@ -31,6 +31,8 @@ type MemorySample = (Timestamp, u64, u8, u64); ...@@ -31,6 +31,8 @@ type MemorySample = (Timestamp, u64, u8, u64);
/// Maximum number of memory samples returned in a query result. /// Maximum number of memory samples returned in a query result.
const MAX_MEMORY_SAMPLES: usize = 200; const MAX_MEMORY_SAMPLES: usize = 200;
/// Maximum number of equal-duration concurrency segments in a span query.
const MAX_CONCURRENCY_SAMPLES: usize = 200;
pub struct Store { pub struct Store {
pub(crate) spans: ChunkedVec<Span>, pub(crate) spans: ChunkedVec<Span>,
...@@ -435,6 +437,15 @@ impl Store { ...@@ -435,6 +437,15 @@ impl Store {
.collect() .collect()
} }
/// Average global self-time concurrency across equal-duration segments
/// of `[start, end)`. When corrected-time indexing is disabled, there is
/// no tree to query and the series is omitted.
pub fn concurrency_samples_for_range(&self, start: Timestamp, end: Timestamp) -> Vec<f64> {
self.self_time_tree.as_ref().map_or_else(Vec::new, |tree| {
tree.lookup_range_concurrency_samples(start, end, MAX_CONCURRENCY_SAMPLES)
})
}
fn memory_samples_slice(&self, start: Timestamp, end: Timestamp) -> &[MemorySample] { fn memory_samples_slice(&self, start: Timestamp, end: Timestamp) -> &[MemorySample] {
// Binary search for the first sample >= start // Binary search for the first sample >= start
let lo = self let lo = self
...@@ -524,6 +535,17 @@ impl Store { ...@@ -524,6 +535,17 @@ impl Store {
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn concurrency_samples_are_empty_without_a_self_time_tree() {
let mut store = Store::new();
store.self_time_tree = None;
assert!(
store
.concurrency_samples_for_range(Timestamp::ZERO, Timestamp::from_value(100))
.is_empty()
);
}
#[test] #[test]
fn downsampling_keeps_worker_count_from_max_memory_sample() { fn downsampling_keeps_worker_count_from_max_memory_sample() {
let mut store = Store::new(); let mut store = Store::new();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment