Report OS memory pressure in TurboMalloc and trace samples#92939
Closed
Report OS memory pressure in TurboMalloc and trace samples#92939
Conversation
Merging this PR will improve performance by 3.76%
Performance Changes
Comparing Footnotes
|
Contributor
Stats from current PR✅ No significant changes detected📊 All Metrics📖 Metrics GlossaryDev Server Metrics:
Build Metrics:
Change Thresholds:
⚡ Dev Server
📦 Dev Server (Webpack) (Legacy)📦 Dev Server (Webpack)
⚡ Production Builds
📦 Production Builds (Webpack) (Legacy)📦 Production Builds (Webpack)
📦 Bundle SizesBundle Sizes⚡ TurbopackClient Main Bundles
Server Middleware
Build DetailsBuild Manifests
📦 WebpackClient Main Bundles
Polyfills
Pages
Server Edge SSR
Middleware
Build DetailsBuild Manifests
Build Cache
🔄 Shared (bundler-independent)Runtimes
📎 Tarball URL |
Adds `TurboMalloc::memory_pressure()` returning a normalized `Option<u8>` in `0..=100` backed by `/proc/pressure/memory` (Linux), the `kern.memorystatus_level` sysctl (macOS) and `GlobalMemoryStatusEx` (Windows). Unsupported targets return `None`. The value is attached to `TraceRow::MemorySample` and propagated through the trace-server store, so that span queries return a parallel `memory_pressure_samples` vector alongside `memory_samples`, enabling future task-eviction heuristics based on real OS pressure. Co-Authored-By: Claude <noreply@anthropic.com>
PSI (`/proc/pressure/memory`) is not available on all Linux kernels (older than 4.20, built without `CONFIG_PSI`, or in restricted containers). Falling back to `(MemTotal - MemAvailable) / MemTotal` from `/proc/meminfo` ensures `TurboMalloc::memory_pressure()` returns a sane value on any standard Linux system, matching the semantics of Windows' `dwMemoryLoad`. The unit test is tightened to assert `Some(_)` on every supported platform (Linux, macOS, Windows). Co-Authored-By: Claude <noreply@anthropic.com>
e7f22c1 to
24d0c6a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Adds a new
TurboMalloc::memory_pressure()method that returns a normalized OS-level memory pressure value in the range0..=100asOption<u8>. That value is attached to every memory sample in the tracing layer (TraceRow::MemorySample) and propagated through the trace-server so that span queries return amemory_pressure_samplesvector next to the existingmemory_samples.Why?
Our current tracing only records the in-process allocator usage (
TurboMalloc::memory_usage()), which does not tell us when the operating system is actually under memory pressure. We want that signal in the trace output to:turbo-tasks(see branch description). This PR lands the plumbing; the eviction heuristic is not part of this change.How?
TurboMalloc::memory_pressure() -> Option<u8>— new, inturbopack/crates/turbo-tasks-malloc/. Values are normalized so that0= no pressure,100= maximum pressure. Platform-specific backends:/proc/pressure/memory(someavg10), fallback to(MemTotal - MemAvailable) / MemTotalfrom/proc/meminfoCONFIG_PSI, restricted containers). The meminfo fallback keeps the signal meaningful on any standard Linux system and matches the semantics of Windows'dwMemoryLoad.kern.memorystatus_levelsysctl (% free memory)100 - level, read vialibc::sysctlbyname.MEMORYSTATUSEX::dwMemoryLoadviaGlobalMemoryStatusEx(windows-sys)None.All runtime failures (missing file, sysctl error, failed API call, unparseable content) silently yield
Nonerather than panicking.Wiring into tracing:
TraceRow::MemorySamplegains amemory_pressure: u8field.0is used whenmemory_pressure()returnsNoneon unsupported platforms.RawTraceLayer::maybe_report_memory_samplepopulates it on every sample (sampling cadence unchanged).MemorySample; old trace files cannot be read by the newturbopack-trace-server. Given the dev-only nature of this data that seemed acceptable — let me know if a migration is desired.Wiring into the trace-server:
Store::memory_samplesis nowVec<(Timestamp, u64, u8)>andadd_memory_sample(ts, memory, memory_pressure).Store::memory_pressure_samples_for_range(start, end) -> Vec<u8>mirrorsmemory_samples_for_range: sameMAX_MEMORY_SAMPLES = 200cap and same group-and-max downsampling, so both vectors align index-by-index for a given span query.ServerToClientMessage::QueryResultgains amemory_pressure_samples: Vec<u8>field next tomemory_samples.Dependencies:
libc(macOS only,cfg-gated),windows-syswith theWin32_System_SystemInformationfeature (Windows only,cfg-gated). No new deps on Linux.Verification
cargo build -p turbo-tasks-malloc -p turbopack-trace-utils -p turbopack-trace-servercargo clippy -p turbo-tasks-malloc -p turbopack-trace-utils -p turbopack-trace-server --all-targets -- -D warningscargo test -p turbo-tasks-malloc— 7 tests pass, including:memory_pressure_is_in_range: assertsSome(_)and≤ 100on Linux, macOS and Windows (viacfg-gated.expect()), and allowsNoneelsewhere./proc/meminfocode paths (typical content, malformed input, clamping)./proc/pressure/memoryis absent (kernel 5.10 withoutCONFIG_PSI); the/proc/meminfofallback returnedSome(3)as expected.