Skip to content

Heap profiling - #910

Closed
Jaya Kasa (jayakasadev) wants to merge 40 commits into
microsoft:mainfrom
jayakasadev:heap-profiling-v2
Closed

Jaya Kasa (jayakasadev) wants to merge 40 commits into
microsoft:mainfrom
jayakasadev:heap-profiling-v2

Conversation

@jayakasadev

Copy link
Copy Markdown
Contributor

What

Adds opt-in heap profiling (SNMALLOC_PROFILE), allocator stats (SNMALLOC_STATS_BASIC / SNMALLOC_STATS_FULL), Rust APIs with pprof and flamegraph output (snmalloc-rs), and CLI analysis tools (snmalloc-tools) for #853.

Everything is off by default. When off, profiling hooks compile away and stats take 0 bytes.

Includes four profiling modes to compare two choices:

  • When to sample: per-allocation counter vs. freelist refill (SNMALLOC_PROFILE_REFILL_SAMPLING)
  • Where to store samples: main heap vs. dedicated side heap (SNMALLOC_PROFILE_SECONDARY_STORAGE)

Organized into 6 commits: core primitives, profiling core, stats, allocator hooks, Rust/tools, and build/CI/docs.

Why

Users need a way to see which call stacks allocate memory and how the allocator is behaving, without slowing down normal builds. Comparing all four modes side by side shows which design is cheapest before splitting the winner into small upstream PRs on #853.

Cost

Measured on Apple M4 Pro (macOS, Release, 1 thread, 32 B objects, 512 KiB sampling, 10 runs, mean ± 95% CI). Full tables in docs/heap-profiling-experimental.md.

Mode Extra instr / malloc Extra instr / free p99 (64 ops) Peak RSS @ 512 KiB Peak RSS @ 4 KiB
Profiling off (baseline) 52.9 total 43.5 total 346 ± 74 ns 15.0 MiB 15.0 MiB
Off + frame pointers +1.1 ± 0.0 +1.0 ± 0.0 325 ± 56 ns 15.0 MiB 15.0 MiB
Counter + main heap +34.1 ± 0.0 +28.1 ± 0.0 450 ± 12 ns 15.5 MiB 28.3 MiB
Refill + main heap +3.2 ± 0.1 +31.1 ± 0.0 375 ± 0 ns 15.5 MiB 28.3 MiB
Counter + side heap +80.1 ± 0.0 +1.0 ± 0.0 671 ± 207 ns 15.5 MiB 63–64 MiB
Refill + side heap +2.3 ± 0.0 +4.0 ± 0.0 333 ± 0 ns 15.5 MiB 63–64 MiB
  • Fastest mode: refill + side heap is within ~1–3 instructions/op of just keeping frame pointers.
  • Memory trade-off: side heap uses one page per live sample (like tcmalloc), so RSS grows at very dense sampling (4 KiB).
  • Limits: single-threaded macOS/arm64 only; multi-threaded and Linux/x86 numbers are not measured yet.

Test plan

Run on macOS arm64 against the final tree (bf6aa13):

  • CMake (Release, -fast and -check tests): 8 configs (off, all 4 profiling modes, basic stats, full stats, profile + full stats) — 141/141 ctest each
  • TSan (Debug): 3 configs (profile, refill + side heap, profile + full stats) — 42/42 profile_|fast_path|statistics tests each, 0 warnings
  • Rust (snmalloc-rs): 22/22 dev + release cargo test runs across 11 feature sets, 0 warnings
  • Bazel: bazel test //snmalloc-rs:all — 10/10
  • Formatting: clang-format 15.0.7 and cargo fmt --check clean
  • Not yet run on final tree: full non-TSan Debug ctest, Windows, CHERI, and Rust 1.100 allocator-api

Jaya Kasa (jayakasadev) and others added 30 commits December 2, 2026 12:13
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
… profiler page mapping; trim aligned large samples
- Per-size-class counters are now two monotone counts per thread, and the
  snapshot sums every allocator in the pool. The old teardown drain into a
  global could double-count or drop a thread, and the derived
  cumulative_alloc fell when an owner received remote frees.
- Snapshot clamps peak >= in use and mapped >= committed.
- snmalloc-sys rebuilds when the C++ sources change.
- Tests: regression tests for the above, DROP_FRAMES and hashed crate names;
  serialise tests that change the global sampling rate or read global
  counters.
- CI: symbolicate and stats-full Rust steps, a full-stats CMake config.
- Docs: bench README and sanitizer claims match the code.
- freelist count() is a plain member again, so gcc and newer clang build
  the default config.
- Remote-dealloc stats move into dealloc_remote's success paths, so
  free(NULL) is not counted and the read-only default allocator is never
  written.
- snmalloc_get_full_stats is built once per library after the library
  picks its config, so profile builds of the C/C++ shims read the right
  allocator.
- AllocPool::iterate reads the list head under the pool lock.
- The frontend alloc and dealloc counters cover small objects only.
- subtract_request clamps instead of branching, which gcc's
  -Wstringop-overflow otherwise flags in profile Release builds.
- Tests: shim stats regression, concurrent snapshot under TSan, large
  object and free(NULL) checks, subtract_request saturation.
- CI: gcc basic-stats job; secondary-storage Rust stats step.
With the default pass-through secondary this keeps realloc's codegen
identical to upstream.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant