next.js
aa4c30a5 - Add MemorySample to Turbopack trace infrastructure (#90597)

Commit
151 days ago
Add MemorySample to Turbopack trace infrastructure (#90597) ### What? Adds periodic process memory sampling to the Turbopack trace system. Two new capabilities: 1. **`TraceRow::MemorySample`** — A new trace row variant that captures `TurboMalloc::memory_usage()` at ~10ms intervals 2. **`memory_samples`** **in** **`QueryResult`** — When querying a span, returns up to 200 memory usage samples covering the span's time range ### Why? To provide visibility into process memory usage over time in the Turbopack trace viewer. Currently the trace system tracks per-span allocations/deallocations but has no global memory usage timeline. ### How? **Sampling (`turbopack-trace-utils`):** - New `TraceRow::MemorySample { ts, memory }` variant - Sampling triggered from `on_enter` using a three-tier dedup strategy to minimize overhead: 1. Thread-local check (no synchronization) 2. Global atomic read (single load) 3. CAS to claim the sample (only one thread wins per interval) - This ensures at most one sample per 10ms across all threads **Storage (`turbopack-trace-server`):** - Global sorted `Vec<(Timestamp, u64)>` in the `Store` - Insertion-sort step on add (efficient since samples arrive nearly sorted) - Binary search for range queries **Query (`turbopack-trace-server`):** - `memory_samples_for_range(start, end)` returns up to 200 samples - When more exist, merges groups of N by taking the max value per group - Added `memory_samples: Vec<u64>` field to `QueryResult` https://github.com/vercel/turbo-trace-viewer/pull/24
Author
Parents
Loading