Skip to content

[4/4] feat(heap): enable in-place sampling heap pruner and remove restart loop - #462

Open
IlyasShabi wants to merge 4 commits into
ishabi/heap-pruner-3-sampling-prunerfrom
ishabi/heap-pruner-4-enable-pruner
Open

IlyasShabi wants to merge 4 commits into
ishabi/heap-pruner-3-sampling-prunerfrom
ishabi/heap-pruner-4-enable-pruner

Conversation

@IlyasShabi

@IlyasShabi IlyasShabi commented Dec 2, 2026 •

Copy link
Copy Markdown

Stack

# PR Base Summary
1 #459 main Lifecycle hardening, translation fast paths, and OOM callback correctness
2 #460 ishabi/heap-pruner-1-lifecycle-perf Watermarked BuildAllocationStatsByNodeId and live-only near-OOM filtering
3 #461 ishabi/heap-pruner-2-watermark-oom Self-contained SamplingHeapPruner implementation + design doc
4 #462 ← this PR ishabi/heap-pruner-3-sampling-pruner Wire SamplingHeapPruner, remove restart loop, full integration & worker tests

Motivation

Enables continuous in-place allocation profiling by wiring SamplingHeapPruner into HeapProfiler and removing the JS stop/start restart loop that previously dropped surviving live samples on every collection window (fixes DataDog/dd-trace-js#11122).

Changes

  • Native integration (bindings/profilers/heap.{hh,cc}, bindings/profilers/near-oom.{hh,cc}):
    • Replaced HeapProfilerState::allocations boolean with SamplingHeapPruner pruner (state->pruner.IsActive() as the single source of truth); call pruner.Reset() in StopSamplingHeapProfiler() and ~HeapProfilerState().
    • Removed RestartSamplingHeapProfiler; added UsesInPlacePruner.
    • Wired pruner.Start() (with raw double-start guard while active), pruner.BuildStats(), profile.reset(), and pruner.Prune() in GetAllocationProfile().
    • Updated NearHeapLimit to gate live-only filtering on state->pruner.IsActive().
  • TypeScript wrapper (ts/src/heap-profiler-bindings.ts, ts/src/heap-profiler.ts):
    • Removed restartSamplingHeapProfiler and the try/finally restart loop; exported usesInPlacePruner().
    • Added isAllocationMode(), hasAllocationStats(), flattened collectProfile(), and synced mode on startup/runtime downgrade across v8Profile(), profile(), capture(), and convertProfile().
  • Tests (ts/test/test-heap-profiler.ts, ts/test/oom-heap-limit-extension.ts, ts/test/test-worker-threads.ts):
    • Added unit tests for fallback at start, runtime downgrade during profile()/capture()/v8Profile()/convertProfile(), empty trees, and mismatched root shapes.
    • Added real Node 26 integration tests for live-object preservation without double-counting, dead-only initial windows, inspector external sampler replacement + downgrade, bounded call-tree nodes under dynamic functions, foreign sampler at start, zero-sample/mode-switching/raw-double-start/capture(), and Node version/platform startup guards.
    • Added multi-worker concurrent allocation profiling and abrupt worker termination tests.

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Overall package size

Self size: 2.98 MB
Deduped: 4.1 MB
No deduping: 4.1 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | pprof-format | 2.4.1 | 924.37 kB | 924.37 kB | | source-map | 0.8.0 | 185.66 kB | 185.66 kB | | node-gyp-build | 4.8.4 | 13.86 kB | 13.86 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@datadog-prod-us1-3

datadog-prod-us1-3 Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Pipelines

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: b879d12 | Docs | Give us feedback!

@IlyasShabi
IlyasShabi marked this pull request as ready for review December 2, 2026 14:43

@szegedi szegedi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, approving this one too. All I have are nits, but of course if you fix any I'm happy to re-approve.

Comment thread ts/src/heap-profiler.ts
startedWithAllocations = allocations;
startSamplingHeapProfiler(intervalBytes, stackDepth, allocations);
heapIntervalBytes = intervalBytes;
startedWithAllocations = allocations && usesInPlacePruner();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows/Node 26 and on Node 27+, start(i, d, true) now quietly falls back to live-only profiles. On main those runtimes got (restart-based) allocation profiles. I agree live-only is strictly better than a corrupted inuse_space, but we should call it out. Could we either:
(a) fix the JSDoc ("Requires Node.js >= 26" → requires Node 26 on non-Windows, otherwise falls back to live-only);
(b) give callers a way to know, either by documenting usesInPlacePruner() as the check or by returning it from start(), or warning once; and
(c) mention this in the PR description?

We should not forget to update usesInPlacePruner() for when Node.js 27 comes out as allocation mode will switch off for it, but I guess we'll get reminded by a falling CI if 27 gets added to the testing matrix, right? I think you'd want to first check if the internal layout stays the same or needs to be updated.

if (state->allocations) {
// In allocation mode V8 retains dead samples in node->allocations;
// derive live-only counts from GetSamples() for the OOM report.
if (state->pruner.IsActive()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Presuming live-only on pruner.IsActive() misses cases where V8 still keeps dead samples while the pruner isn't attached:

  • a foreign sampler with includeObjectsCollected* was already running at start();
  • the gap between BuildStats() setting pending_downgrade_ and Prune() (translation allocates there).

In the first case regular profile() also counts dead objects as in-use. Could we do a data check instead? On Node >= 26, scan once with std::any_of(samples, !is_live) and only then build live-only stats. That's free in the common no-flags case and covers both paths.

std::unique_ptr<v8::AllocationProfile> profile(
isolate->GetHeapProfiler()->GetAllocationProfile());
if (!profile) {
return Nan::ThrowError("Heap profiler is not enabled.");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When GetAllocationProfile() returns null, our sampler is gone (an external stop). Resetting state->pruner here before throwing would stop usesInPlacePruner() from reporting true for a stale pointer, and would remove the window where the next sampler, possibly at the same address, gets mistaken for ours.

This branch has not been deployed

No deployments
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.

2 participants