Expand Beam Python heap dump with process/native memory stats (#39244)#39466
Open
uditjainstjis wants to merge 1 commit into
Open
Expand Beam Python heap dump with process/native memory stats (#39244)#39466uditjainstjis wants to merge 1 commit into
uditjainstjis wants to merge 1 commit into
Conversation
…#39244) The heap dump (--experiments=enable_heap_dump) previously only contained the guppy Python-object heap. This adds a memory_stats() section so operators can tell whether memory is growing on the native (C) heap vs in Python objects, and can spot native-heap fragmentation: * Process memory: peak RSS (ru_maxrss, unit-corrected per platform) and, on Linux, current RSS from /proc/self/statm. * Python allocations: sys.getallocatedblocks() and per-generation gc stats. * glibc malloc: mallinfo2() arena/hblkhd/uordblks/fordblks/keepcost plus a fordblks/(arena+hblkhd) fragmentation ratio. mallinfo2 is used rather than the legacy mallinfo, whose int fields overflow past 2GB and would misreport exactly when memory is the concern. Every collector degrades gracefully and never raises when a source is unavailable on the current platform. memory_stats() is emitted even when guppy is not importable. Tests cover section presence, that stats are emitted without guppy, and that the glibc section degrades gracefully on non-glibc platforms. Generated-by: Claude (Anthropic AI assistant)
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Contributor
|
Assigning reviewers: R: @shunping for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
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.
Addresses #39244
What
Expands the SDK worker heap dump (enabled by
--experiments=enable_heap_dump) with a newmemory_stats()section so operators can distinguish memory growth on the native (C) heap from Python-object allocations, and can spot native-heap fragmentation — the two things the issue asks for.The dump previously contained only the guppy Python-object heap.
memory_stats()adds:ru_maxrss, unit-corrected: bytes on macOS/BSD, KiB on Linux) and, on Linux, current RSS from/proc/self/statm.sys.getallocatedblocks()and per-generationgccollection stats.mallinfo2()arena/hblkhd/uordblks/fordblks/keepcost, plus afordblks/(arena+hblkhd)fragmentation ratio.fordblks(free space the allocator retains rather than returning to the OS) growing relative touordblksis the fragmentation signal referenced in the issue.mallinfo2is used rather than the legacymallinfo, whoseintfields overflow past 2GB and would misreport exactly when memory is the concern; if the symbol is absent it reportsunavailable (needs glibc >= 2.33). No new dependencies — stdlib only (resource,gc,sys,ctypes,/proc).Every collector degrades gracefully and never raises when a source is unavailable, and
memory_stats()is emitted even when guppy is not importable.Example output (Linux, glibc 2.39)
Testing
worker_status_test.py: section presence, memory stats emitted without guppy, and graceful degradation of the glibc section on non-glibc platforms.worker_status_test.pypasses locally (9 passed).mallinfo2ctypes path (by-value structrestype) was executed on real Linux glibc 2.39 to confirm the struct layout and that it does not segfault; values are internally consistent (uordblks + fordblks == arena).yapf==0.43.0clean, pylint 10.00/10.This is a first cut; happy to also wire the section outside the guppy-gated dump or add
malloc_info()XML if maintainers prefer.CHANGES.mdwith noteworthy changes.