Repository navigation
src: remove GCProfiler callbacks in stop() - #67799
yohannesjoya wants to merge 2 commits into
Conversation
GCProfiler::Stop() returned the profile but left the GC prologue and epilogue callbacks registered. They were only removed by the destructor, which runs when the binding object is collected. If the object had been promoted, that waited for the next mark-compact, and until then every collection called SetHeapStatistics() twice per stopped profiler and appended the result to a stream that is never read again. Code that starts and stops profilers repeatedly got slower and used more memory. Remove the callbacks in Stop(), and only remove them in the destructor if the profiler is still running. Fixes: nodejs#67790 Co-authored-by: Marcus Ording <11672035+snovader@users.noreply.github.com> Signed-off-by: Yohannes Desta <yohannesdestagebru10@gmail.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #67799 +/- ##
==========================================
- Coverage 92.99% 90.51% -2.49%
==========================================
Files 428 805 +377
Lines 195633 285869 +90236
Branches 30281 55188 +24907
==========================================
+ Hits 181937 258764 +76827
- Misses 13369 17212 +3843
- Partials 327 9893 +9566
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
I think this test is not checking what you want to fix, I don’t see a reliable way to test that from JS.
Could we add a C++ test that checks the profiler stops recording after stop()?
There was a problem hiding this comment.
Added a cctest in test/cctest/test_node_v8_gc_profiler.cc. It starts a profiler, forces a GC, calls stop(), then forces another GC while the profiler object is still alive and checks that nothing more was written to the profile stream. It fails without the change in src/node_v8.cc.
I kept the JS test because it covers the other half of the change: the destructor must not remove the callbacks again after stop(), since V8 expects them to still be registered. Happy to drop it if you think it's redundant.
Check from C++ that a stopped profiler no longer records collections while its binding object is still alive. The JavaScript test cannot observe this because the extra entries go to a stream that is not read after stop(). Refs: nodejs#67790 Signed-off-by: Yohannes Desta <yohannesdestagebru10@gmail.com>
3de94d7 to
d0d3b28
Compare
GCProfiler::Stop()returned the profile but left the GC prologue and epilogue callbacks registered. Only the destructor removed them, and it runs when the binding object is collected. If the object had been promoted, that waits for the next mark-compact, and until then every collection callsSetHeapStatistics()twice per stopped profiler and appends the result to a stream nothing reads again.This removes the callbacks in
Stop(), and the destructor now only removes them if the profiler is still running. V8'sGCCallbacks::Remove()expects the callback to be registered, so they mustn't be removed twice. The fix is the one @snovader suggested in the issue, so they're co-author.Fixes: #67790
With the reproduction from the issue (100 start/stop cycles), allocation went from ~540 to ~2250 ns per call and RSS from 60 to 319 MB on main. With this change it stays at ~470 ns and 85 MB.
The new cctest checks that a stopped profiler records nothing more when a GC runs while its object is still alive. It fails without this change. The JS test checks that stopping one profiler leaves another running profiler's callbacks in place, and that collecting stopped profilers afterwards doesn't crash. The full cctest suite and
tools/test.py parallel/test-v8-*pass locally, as domake format-cppand cpplint.AI disclosure: I used an AI assistant to help write the test and this description. I reproduced the issue, reviewed the change and ran the tests above myself.