trace-server: pack SelfTimeEntry to 16 bytes

There are approx. 2 self-time entries per span, so this is one of the
largest per-span costs. Replacing the second `Timestamp` with a `u32`
duration, and using `NonZeroU32` instead of `Option<SpanIndex>` takes
the struct down from 24 to 16 bytes, and shrinks the data carried in
the tree during ingest.

Tested against a real, enormous 9GB trace.

Interleaved A/B tests run multiple times, showed the following delta:

Peak RSS:  3797 -> 3330 MB (-12.3%)
Live heap: 3969 -> 3395 MB
tree:      1713 -> 1148 MB
ingest     271 -> 276 MB/s (+1.8%)
parent 1c91d603
...@@ -333,8 +333,14 @@ impl TraceReader { ...@@ -333,8 +333,14 @@ impl TraceReader {
// `wait_for_more_data`, so the report has to be // `wait_for_more_data`, so the report has to be
// emitted on this path too — it is the path // emitted on this path too — it is the path
// every measurement run takes. // every measurement run takes.
// Report before *and* after `optimize`. The
// difference is slack that was live at the
// high-water mark but did not need to be — which
// is the number that matters when the target
// cannot return memory to the OS.
self.print_memory_report("pre-optimize");
self.store.write().optimize(); self.store.write().optimize();
self.print_memory_report(); self.print_memory_report("post-optimize");
self.wait_for_new_file(&mut file); self.wait_for_new_file(&mut file);
return true; return true;
} }
...@@ -366,7 +372,7 @@ impl TraceReader { ...@@ -366,7 +372,7 @@ impl TraceReader {
/// Print the store's byte breakdown when `MEMORY_REPORT=1`. Takes the /// Print the store's byte breakdown when `MEMORY_REPORT=1`. Takes the
/// write lock and walks every span, so it is gated behind the env var /// write lock and walks every span, so it is gated behind the env var
/// rather than being always-on. /// rather than being always-on.
fn print_memory_report(&self) { fn print_memory_report(&self, label: &str) {
let Ok(mode) = env::var("MEMORY_REPORT") else { let Ok(mode) = env::var("MEMORY_REPORT") else {
return; return;
}; };
...@@ -375,7 +381,7 @@ impl TraceReader { ...@@ -375,7 +381,7 @@ impl TraceReader {
// the report shows the serving peak and not just the ingest peak. // the report shows the serving peak and not just the ingest peak.
self.store.read().warm_all_derived(); self.store.read().warm_all_derived();
} }
println!("{}", self.store.write().memory_report()); println!("[{label}] {}", self.store.write().memory_report());
} }
fn wait_for_more_data( fn wait_for_more_data(
...@@ -405,7 +411,7 @@ impl TraceReader { ...@@ -405,7 +411,7 @@ impl TraceReader {
} else if !stats.is_empty() { } else if !stats.is_empty() {
println!("{stats}"); println!("{stats}");
} }
self.print_memory_report(); self.print_memory_report("initial-read");
} }
loop { loop {
// No more data to read, sleep for a while to wait for more data // No more data to read, sleep for a while to wait for more data
......
...@@ -12,12 +12,49 @@ pub struct SelfTimeTree<T> { ...@@ -12,12 +12,49 @@ pub struct SelfTimeTree<T> {
count: usize, count: usize,
} }
/// One self-time interval. There are roughly two of these per span, so after
/// `Span` itself this is the largest per-span cost in the crate.
///
/// Stores a `u32` length rather than a second `Timestamp`, which keeps the whole
/// entry in 16 bytes instead of 24. Timestamps are 1/100 µs ticks, so a `u32`
/// spans 42.9 seconds; the longest self-time interval measured over 4 GB of a
/// real 9 GB trace was 6.6 s, and the item is a span index, whose largest
/// measured value was ~12M against a 4.29B ceiling.
struct SelfTimeEntry<T> { struct SelfTimeEntry<T> {
start: Timestamp, start: Timestamp,
end: Timestamp, duration: u32,
item: T, item: T,
} }
/// Longest interval a single entry can represent, given the `u32` duration
/// field: ~42.9 s at 1/100 µs per tick. [`SelfTimeTree::insert`] splits anything
/// longer, so this is never a limit on how long an interval — or a span — is.
const MAX_ENTRY_DURATION: u64 = u32::MAX as u64;
impl<T> SelfTimeEntry<T> {
/// Caller must have ensured `end - start <= MAX_ENTRY_DURATION`;
/// [`SelfTimeTree::insert`] is what guarantees that.
fn new(start: Timestamp, end: Timestamp, item: T) -> Self {
let duration = *end.saturating_sub(start);
debug_assert!(
duration <= MAX_ENTRY_DURATION,
"interval of {duration} ticks should have been split by insert()"
);
Self {
start,
duration: duration as u32,
item,
}
}
fn end(&self) -> Timestamp {
Timestamp::from_value(*self.start + self.duration as u64)
}
}
// The store instantiates this with a `NonZeroU32` item. Keep it at 16 bytes.
const _: () = assert!(std::mem::size_of::<SelfTimeEntry<std::num::NonZeroU32>>() == 16);
struct SelfTimeChildren<T> { struct SelfTimeChildren<T> {
/// Entries < split_point /// Entries < split_point
left: SelfTimeTree<T>, left: SelfTimeTree<T>,
...@@ -60,15 +97,44 @@ impl<T> SelfTimeTree<T> { ...@@ -60,15 +97,44 @@ impl<T> SelfTimeTree<T> {
bytes bytes
} }
pub fn insert(&mut self, start: Timestamp, end: Timestamp, item: T) { /// Insert a self-time interval.
///
/// Intervals longer than [`MAX_ENTRY_DURATION`] are split across several
/// entries, so the `u32` duration field is not a ceiling on interval or span
/// length. Splitting is exact rather than a compromise, because both queries
/// are coverage-based: `lookup_range_count` sums covered time, and in
/// `lookup_range_corrected_time`'s sweep the second piece's `Start` and the
/// first's `End` share a timestamp with `Start` ordered first, so the overlap
/// count nets out with no time elapsing between them.
///
/// Not hypothetical: `Store::set_total_time`, used by the Next.js JSON
/// reader, synthesizes a single self-time interval covering a childless
/// span's entire duration.
pub fn insert(&mut self, start: Timestamp, end: Timestamp, item: T)
where
T: Clone,
{
let mut chunk_start = start;
while *end.saturating_sub(chunk_start) > MAX_ENTRY_DURATION {
let chunk_end = Timestamp::from_value(*chunk_start + MAX_ENTRY_DURATION);
self.insert_entry(chunk_start, chunk_end, item.clone());
chunk_start = chunk_end;
}
self.insert_entry(chunk_start, end, item);
}
fn insert_entry(&mut self, start: Timestamp, end: Timestamp, item: T) {
self.count += 1; self.count += 1;
self.entries.push(SelfTimeEntry { start, end, item }); self.entries.push(SelfTimeEntry::new(start, end, item));
self.check_for_split(); self.check_for_split();
} }
fn insert_without_check(&mut self, start: Timestamp, end: Timestamp, item: T) { /// Move an existing entry into this node, without re-deriving its duration
/// or checking for a split. Used by redistribution, which does its own split
/// check afterwards.
fn push_entry(&mut self, entry: SelfTimeEntry<T>) {
self.count += 1; self.count += 1;
self.entries.push(SelfTimeEntry { start, end, item }); self.entries.push(entry);
} }
fn check_for_split(&mut self) { fn check_for_split(&mut self) {
...@@ -107,7 +173,7 @@ impl<T> SelfTimeTree<T> { ...@@ -107,7 +173,7 @@ impl<T> SelfTimeTree<T> {
.entries .entries
.iter() .iter()
.fold((Timestamp::MAX, Timestamp::ZERO), |(lo, hi), e| { .fold((Timestamp::MAX, Timestamp::ZERO), |(lo, hi), e| {
(lo.min(e.start), hi.max(e.end)) (lo.min(e.start), hi.max(e.end()))
}); });
let middle = (start + end) / 2; let middle = (start + end) / 2;
// Pre-allocate half the split threshold: after distributing, each child // Pre-allocate half the split threshold: after distributing, each child
...@@ -130,13 +196,14 @@ impl<T> SelfTimeTree<T> { ...@@ -130,13 +196,14 @@ impl<T> SelfTimeTree<T> {
}; };
let mut i = children.spanning_entries; let mut i = children.spanning_entries;
while i < self.entries.len() { while i < self.entries.len() {
let SelfTimeEntry { start, end, .. } = self.entries[i]; let (start, end) = {
let entry = &self.entries[i];
(entry.start, entry.end())
};
if end <= children.split_point { if end <= children.split_point {
let SelfTimeEntry { start, end, item } = self.entries.swap_remove(i); children.left.push_entry(self.entries.swap_remove(i));
children.left.insert_without_check(start, end, item);
} else if start >= children.split_point { } else if start >= children.split_point {
let SelfTimeEntry { start, end, item } = self.entries.swap_remove(i); children.right.push_entry(self.entries.swap_remove(i));
children.right.insert_without_check(start, end, item);
} else { } else {
self.entries.swap(i, children.spanning_entries); self.entries.swap(i, children.spanning_entries);
children.spanning_entries += 1; children.spanning_entries += 1;
...@@ -233,9 +300,9 @@ impl<T> SelfTimeTree<T> { ...@@ -233,9 +300,9 @@ impl<T> SelfTimeTree<T> {
pub fn lookup_range_count(&self, start: Timestamp, end: Timestamp) -> Timestamp { pub fn lookup_range_count(&self, start: Timestamp, end: Timestamp) -> Timestamp {
let mut total_count = Timestamp::ZERO; let mut total_count = Timestamp::ZERO;
for entry in &self.entries { for entry in &self.entries {
if entry.start <= end && entry.end >= start { if entry.start <= end && entry.end() >= start {
let start = std::cmp::max(entry.start, start); let start = std::cmp::max(entry.start, start);
let end = std::cmp::min(entry.end, end); let end = std::cmp::min(entry.end(), end);
let span = end - start; let span = end - start;
total_count += span; total_count += span;
} }
...@@ -308,8 +375,8 @@ impl<T> SelfTimeTree<T> { ...@@ -308,8 +375,8 @@ impl<T> SelfTimeTree<T> {
f: &mut impl FnMut(Timestamp, Timestamp, &T), f: &mut impl FnMut(Timestamp, Timestamp, &T),
) { ) {
for entry in &self.entries { for entry in &self.entries {
if entry.start <= end && entry.end >= start { if entry.start <= end && entry.end() >= start {
f(entry.start, entry.end, &entry.item); f(entry.start, entry.end(), &entry.item);
} }
} }
if let Some(children) = &self.children { if let Some(children) = &self.children {
...@@ -333,8 +400,8 @@ impl<T> SelfTimeTree<T> { ...@@ -333,8 +400,8 @@ impl<T> SelfTimeTree<T> {
self.rebalance(); self.rebalance();
} }
for entry in &self.entries { for entry in &self.entries {
if entry.start <= end && entry.end >= start { if entry.start <= end && entry.end() >= start {
f(entry.start, entry.end, &entry.item); f(entry.start, entry.end(), &entry.item);
} }
} }
if let Some(children) = &mut self.children { if let Some(children) = &mut self.children {
...@@ -390,6 +457,32 @@ mod tests { ...@@ -390,6 +457,32 @@ mod tests {
} }
} }
#[test]
fn intervals_longer_than_one_entry_are_split_exactly() {
// `set_total_time` can synthesize a self-time interval covering a whole
// childless span, which may exceed what one entry's u32 duration holds.
// Splitting must leave both queries' answers unchanged.
let long = MAX_ENTRY_DURATION + MAX_ENTRY_DURATION / 2;
let end = Timestamp::from_value(long);
let mut tree: SelfTimeTree<u32> = SelfTimeTree::new();
tree.insert(Timestamp::ZERO, end, 1);
assert!(tree.len() > 1, "expected the interval to be split");
assert_eq!(tree.lookup_range_count(Timestamp::ZERO, end), end);
// Sole interval, so corrected time is its full extent.
assert_eq!(tree.lookup_range_corrected_time(Timestamp::ZERO, end), end);
// With a second interval covering the same range, corrected time halves
// uniformly: the split must not perturb the overlap count at the seams.
let mut tree2: SelfTimeTree<u32> = SelfTimeTree::new();
tree2.insert(Timestamp::ZERO, end, 1);
tree2.insert(Timestamp::ZERO, end, 2);
assert_eq!(
tree2.lookup_range_corrected_time(Timestamp::ZERO, end),
Timestamp::from_value(long / 2)
);
}
#[test] #[test]
fn test_simple() { fn test_simple() {
let mut tree = SelfTimeTree::new(); let mut tree = SelfTimeTree::new();
......
use std::{ use std::{
cmp::{max, min}, cmp::{max, min},
env, env,
num::NonZeroUsize, num::{NonZeroU32, NonZeroUsize},
sync::{ sync::{
OnceLock, RwLock, OnceLock, RwLock,
atomic::{AtomicU8, AtomicU64, Ordering}, atomic::{AtomicU8, AtomicU64, Ordering},
...@@ -51,7 +51,10 @@ pub struct Store { ...@@ -51,7 +51,10 @@ pub struct Store {
/// The derived half of every span, lazily allocated per chunk. See /// The derived half of every span, lazily allocated per chunk. See
/// [`crate::cold`]. /// [`crate::cold`].
pub(crate) cold: ColdStore, pub(crate) cold: ColdStore,
pub(crate) self_time_tree: Option<SelfTimeTree<SpanIndex>>, /// Keyed by `NonZeroU32` rather than [`SpanIndex`] so its entries stay at
/// 16 bytes; see [`crate::self_time_tree`]. Converted at the two boundaries
/// in `insert_self_time`.
pub(crate) self_time_tree: Option<SelfTimeTree<NonZeroU32>>,
max_self_time_lookup_time: AtomicU64, max_self_time_lookup_time: AtomicU64,
/// Global sorted list of memory samples (timestamp, memory_bytes). /// Global sorted list of memory samples (timestamp, memory_bytes).
memory_samples: Vec<MemorySample>, memory_samples: Vec<MemorySample>,
...@@ -292,10 +295,21 @@ impl Store { ...@@ -292,10 +295,21 @@ impl Store {
if let Some(tree) = self.self_time_tree.as_mut() { if let Some(tree) = self.self_time_tree.as_mut() {
if Timestamp::from_value(*self.max_self_time_lookup_time.get_mut()) >= start { if Timestamp::from_value(*self.max_self_time_lookup_time.get_mut()) >= start {
tree.for_each_in_range_optimize(start, end, &mut |_, _, span| { tree.for_each_in_range_optimize(start, end, &mut |_, _, span| {
outdated_spans.insert(*span); // Widening back: a `NonZeroU32` is always a valid `SpanIndex`.
outdated_spans.insert(SpanIndex::new(span.get() as usize).unwrap());
}); });
} }
tree.insert(start, end, span_index); // Spans beyond `u32::MAX` are simply not tracked for corrected time
// rather than being truncated into a wrong index. That ceiling is
// 4.29B against a largest-measured ~12M, and corrected time is a
// display-only heuristic, so losing it is far preferable to
// invalidating the wrong spans.
if let Some(item) = u32::try_from(span_index.get())
.ok()
.and_then(NonZeroU32::new)
{
tree.insert(start, end, item);
}
} }
} }
......
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