From 925121cab2fad623c487df49cc85dde7abcc45e6 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Thu, 29 Oct 2026 12:20:30 +0100 Subject: [PATCH 01/37] Fix reference-chain collector running without a leak signal The collector ran whole-heap searches in healthy processes through two authorization paths that bypassed the candidate requirement: - hasLeakSignal() returned true unconditionally when generations tracking was off, so a liveheap-only session passed the gate with zero leak candidates; the search cannot exit early (the no-progress abandonment is suppressed while urgent) and ran to the auto-tuned TTL. - The urgent-OOM latch entitled one candidate-less search per episode, and a warmup heap-floor ramp projected imminent OOM from a seconds-long observation window. hasLeakSignal() now keys on an effective flag (generations OR liveheap tracking) and requires selectLeakCandidates() > 0; urgency only sharpens the OOM ramp while a candidate-driven search runs. secondsToOOM() returns no projection until the leak signal has been active for a burn-in period, so transient warmup growth cannot latch urgency. The per-search urgent-entitlement flag was removed (behaviorally dead). --- ddprof-lib/src/main/cpp/livenessTracker.cpp | 73 +++-- ddprof-lib/src/main/cpp/livenessTracker.h | 102 +++++-- .../src/main/cpp/referenceChainEvents.cpp | 8 +- ddprof-lib/src/main/cpp/referenceChains.cpp | 76 ++--- ddprof-lib/src/main/cpp/referenceChains.h | 11 +- .../src/test/cpp/livenessTracker_ut.cpp | 274 ++++++++++++++++++ .../src/test/cpp/referenceChainsBfsTests.inc | 43 ++- .../cpp/referenceChainsLifecycleTests.inc | 44 ++- .../src/test/cpp/referenceChainsOomTests.inc | 120 +++++++- .../test/cpp/referenceChainsTestAccessors.h | 6 +- .../test/cpp/referenceChainsTrackerTests.inc | 119 ++++++-- .../src/test/cpp/referenceChains_ut.cpp | 7 + doc/ReferenceChainsCollectionSummary.md | 8 +- .../LiveHeapReferenceChainsImplementation.md | 9 +- .../ReferenceChainsSignalsExplained.md | 21 +- 15 files changed, 770 insertions(+), 151 deletions(-) diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index 538854d9f6..1b12ee7bb7 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -162,9 +162,10 @@ void LivenessTracker::cleanup_table(bool forced, bool allow_resolve, bool account_epoch) { u64 current = load(_last_gc_epoch); u64 target_gc_epoch = load(_gc_epoch); - TEST_LOG_SUMMARY("LivenessTracker::cleanup_table forced=%d gc_generations=%d current_epoch=%llu " + TEST_LOG_SUMMARY("LivenessTracker::cleanup_table forced=%d leak_signal=%d gc_generations=%d current_epoch=%llu " "target_epoch=%llu table_size=%d", - forced, _gc_generations.load(std::memory_order_relaxed), (unsigned long long)current, + forced, _leak_signal_enabled.load(std::memory_order_relaxed), + _gc_generations.load(std::memory_order_relaxed), (unsigned long long)current, (unsigned long long)target_gc_epoch, _table_size); // is_epoch_owner is true iff this call is the one that moves _last_gc_epoch @@ -275,12 +276,14 @@ void LivenessTracker::cleanup_table(bool forced, bool allow_resolve, } } - if (_gc_generations.load(std::memory_order_relaxed) && is_epoch_owner) { + if (_leak_signal_enabled.load(std::memory_order_relaxed) && is_epoch_owner) { // Per-klass population tracking (design doc's Open Question 3) - - // gated on _gc_generations so this new cost is paid only when the - // caller actually asked for generation/survival-shaped data - // (Arguments::_gc_generations), not for every liveness-tracking - // session. Gated on is_epoch_owner (not !forced) so a forced + // gated on the effective leak-signal availability: generations + // tracking and liveheap tracking drive the same mechanisms, so the + // cost is paid whenever either option is on. With both off there is + // no consumer for the data (the reference-chain collector stays + // idle, hasLeakSignal() -> false), and paying it would be pure + // overhead. Gated on is_epoch_owner (not !forced) so a forced // (table-overflow) sweep still contributes one population sample // per genuinely new GC epoch instead of silently dropping it. // Reuse an already-resolved id first so the per-sweep budget is @@ -359,7 +362,7 @@ void LivenessTracker::cleanup_table(bool forced, bool allow_resolve, 1.0f * (end - start) / 1000 / 1000, 1.0f * (end - start) / 1000 / sz); } - if (_gc_generations.load(std::memory_order_relaxed) && is_epoch_owner) { + if (_leak_signal_enabled.load(std::memory_order_relaxed) && is_epoch_owner) { // Outside the `sz > 0` block: a sweep that finds the table already empty // still owns this epoch. foldKlassCountsLocked() records zero population // samples for klasses whose every tracked instance died (they never @@ -1571,9 +1574,20 @@ double LivenessTracker::secondsToOOM() const { jlong max_heap = _max_heap_bytes; jlong container_limit = _container_memory_limit; #endif - if (!_gc_generations.load(std::memory_order_relaxed) || max_heap <= 0) { - TEST_LOG("LivenessTracker::secondsToOOM -> -1 (gc_generations=%d max_heap=%lld)", - (int)_gc_generations.load(std::memory_order_relaxed), (long long)max_heap); + if (!_leak_signal_enabled.load(std::memory_order_relaxed) || max_heap <= 0) { + TEST_LOG("LivenessTracker::secondsToOOM -> -1 (leak_signal=%d max_heap=%lld)", + (int)_leak_signal_enabled.load(std::memory_order_relaxed), (long long)max_heap); + return -1; + } + // Age-based burn-in (see OOM_PROJECTION_BURN_IN_NS): no projection until the + // leak signal has been active for the full burn-in, independent of how much + // wall-clock time the heap-floor ring happens to span. The subtraction is + // modular; production stamps are always <= now. + u64 activated_ns = _leak_signal_activated_ns.load(std::memory_order_acquire); + u64 since_ns = OS::nanotime() - activated_ns; + if (activated_ns == 0 || since_ns < OOM_PROJECTION_BURN_IN_NS) { + TEST_LOG("LivenessTracker::secondsToOOM -> -1 (BURN_IN activated_ns=%llu since_ns=%llu)", + (unsigned long long)activated_ns, (unsigned long long)since_ns); return -1; } @@ -1936,7 +1950,7 @@ void LivenessTracker::flush_table(std::set *tracked_thread_ids) { int class_id = 0; if (_table[i].cached_klass_id != 0 && class_map_generation_matches) { // Already resolved by cleanup_table()'s survivor loop this epoch - // (resolveKlassId(), only when _gc_generations is enabled) - reuse + // (resolveKlassId(), only when leak-signal tracking is enabled) - reuse // it instead of repeating the resolution JNI round-trip for the same // object. class_id = _table[i].cached_klass_id; @@ -2021,6 +2035,7 @@ Error LivenessTracker::initialize_table(JNIEnv *jni, int sampling_interval) { void LivenessTracker::resetSession() { _enabled = false; _gc_generations.store(false, std::memory_order_relaxed); + _leak_signal_enabled.store(false, std::memory_order_relaxed); } Error LivenessTracker::start(Arguments &args) { @@ -2095,13 +2110,19 @@ void LivenessTracker::stop() { Error LivenessTracker::initialize(Arguments &args) { _enabled = args._gc_generations || args._record_liveness; - // Gates per-klass population tracking (see the _gc_generations member's - // own comment in livenessTracker.h). Updated unconditionally alongside - // _record_heap_usage below, ahead of the _initialized guard, for the same - // reason: each profiler start should observe the flag it was actually - // started with, even though the tracking table itself persists across - // recordings. + // Gates per-klass population tracking (see the _gc_generations and + // _leak_signal_enabled members' own comments in livenessTracker.h). Updated + // unconditionally alongside _record_heap_usage below, ahead of the + // _initialized guard, for the same reason: each profiler start should + // observe the flag it was actually started with, even though the tracking + // table itself persists across recordings. _gc_generations.store(args._gc_generations, std::memory_order_relaxed); + // Either generations tracking or liveheap tracking is a sufficient source + // for the population-trend signal (they drive the same mechanisms here; + // liveheap additionally preserves stack traces for its samples) - see + // _leak_signal_enabled's own comment (livenessTracker.h). + _leak_signal_enabled.store(args._gc_generations || args._record_liveness, + std::memory_order_relaxed); if (!_enabled) { return Error::OK; @@ -2121,6 +2142,13 @@ Error LivenessTracker::initialize(Arguments &args) { __atomic_store_n(&_watched_tid_count, 0, __ATOMIC_RELEASE); __atomic_store_n(&_urgent_tracking, false, __ATOMIC_RELEASE); + // First activation stamps the burn-in clock (see _leak_signal_activated_ns). + // Only reached with _enabled == true, i.e. generations || liveness, so the + // leak signal is on. CAS from 0: later starts never re-stamp it. + u64 never_activated = 0; + _leak_signal_activated_ns.compare_exchange_strong( + never_activated, OS::nanotime(), std::memory_order_release, std::memory_order_relaxed); + if (_initialized) { // if the tracker was previously initialized return the stored result for // consistency this hack also means that if the profiler is started with @@ -2479,7 +2507,7 @@ void LivenessTracker::track(JNIEnv *env, AllocEvent &event, jint tid, } void LivenessTracker::maybeForceCleanup(u64 now_ns) { - if (!_enabled || !_gc_generations.load(std::memory_order_relaxed)) { + if (!_enabled || !_leak_signal_enabled.load(std::memory_order_relaxed)) { return; } constexpr u64 FORCE_CLEANUP_INTERVAL_NS = 30ULL * 1000 * 1000 * 1000; @@ -2535,10 +2563,11 @@ void LivenessTracker::onGC() { store(_used_after_last_gc, HeapUsage::get(false)._used); } - if (_gc_generations.load(std::memory_order_relaxed)) { + if (_leak_signal_enabled.load(std::memory_order_relaxed)) { // Feeds heapFloorRising()'s corroboration check (selectLeakCandidates()) - // - gated on _gc_generations, same as the per-klass population table - // itself, since this ring exists purely to support that feature. + // - gated on the effective leak-signal availability, same as the + // per-klass population table itself, since this ring exists purely to + // support that feature. // recordHeapFloorSample() itself checks _heap_floor_recording_disabled_for_test // (debug-only) so a test can seed the ring exclusively. size_t used = resolvePostGcHeapUsage(nullptr); diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index 20afe41418..6185348355 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -38,7 +38,7 @@ typedef struct TrackingEntry { jlong leak_tag; // 0 = untagged; otherwise a tag from the leak tag pool Context ctx; // Set by cleanup_table()'s survivor loop via resolveKlassId() when - // _gc_generations is enabled (0 otherwise, or if resolution failed - 0 is + // leak-signal tracking is enabled (0 otherwise, or if resolution failed - 0 is // StringDictionary's own "no entry" sentinel, so a real id is never 0). // flush_table() reuses this instead of re-resolving the same object's // class via a second resolveKlassId() JNI round-trip - an object's class @@ -464,6 +464,30 @@ class alignas(alignof(SpinLock)) LivenessTracker { // still be reading it from a session that persists across a restart. std::atomic _gc_generations; + // Effective leak-signal availability: true when the population-trend signal + // consumed by ReferenceChainTracker::hasLeakSignal() can be constructed at + // all. generations tracking and liveheap tracking drive the same + // mechanisms here (per-klass population table, heap-floor ring, + // secondsToOOM() projection) - liveheap additionally preserves stack + // traces for its live samples - so either option is sufficient. When + // neither is on, there is no signal source and the reference-chain + // collector must stay idle (hasLeakSignal() -> false): running whole-heap + // searches with zero candidates "just in case" is the exact waste the + // leak-signal gate exists to prevent (observed as a frontier table grown + // to its cap with reference_chain_candidate_count == 0 in a healthy + // process when this used to fall back to "always true"). Set from + // args._gc_generations || args._record_liveness in initialize(). Same + // atomicity/reader argument as _gc_generations above. + std::atomic _leak_signal_enabled; + + // OS::nanotime() of the first initialize() that enabled the leak signal; 0 + // means never activated. Set once by a CAS from 0 and never reset by + // production code (not by stop()/resetSession(), not by the ring resets), so + // it is per process, not per recording. Drives secondsToOOM()'s age-based + // burn-in (see OOM_PROJECTION_BURN_IN_NS). Written with release, read with + // acquire. + std::atomic _leak_signal_activated_ns; + // Per-klass population history table (see KlassPopulationEntry above). // Populated only from cleanup_table()'s GC-epoch-advance pass, never from // track() (the allocation sampling hot path) - see @@ -614,7 +638,7 @@ class alignas(alignof(SpinLock)) LivenessTracker { // force=true is used by track()'s table-overflow branch to run a cleanup // synchronously from the allocation-sampling call stack, bypassing the // GC-epoch-changed check below. The per-klass population tracking below - // (_gc_generations) runs on both paths, once per genuinely new GC epoch + // (leak-signal gated) runs on both paths, once per genuinely new GC epoch // (see "is_epoch_owner" in livenessTracker.cpp). // // allow_resolve gates resolveKlassId() - GetClassSignature plus a class-map @@ -854,6 +878,8 @@ class alignas(alignof(SpinLock)) LivenessTracker { _max_heap_bytes(-1), _container_memory_limit(-1), _container_limit_resolved(false), _gc_generations(false), + _leak_signal_enabled(false), + _leak_signal_activated_ns(0), _klass_population_size(0), _klass_count_scratch_size(0), _last_class_map_generation(0), _leak_tag_free_count(LEAK_TAG_POOL_SIZE), @@ -935,7 +961,7 @@ class alignas(alignof(SpinLock)) LivenessTracker { // population trend is positive (growing), ranked by trend magnitude // descending, capped at MAX_LEAK_CANDIDATES regardless of `max` (design // doc's Open Question 3 "top 3-5" cutoff). Returns the number of - // candidates written (0 if _gc_generations was never enabled - + // candidates written (0 if leak-signal tracking was never enabled - // _klass_population stays empty in that case, since population tracking // is gated on it, so no separate guard is needed here). Called on demand // by the BFS-pass poll, not on any timer of its own; does no JNI work, so it is safe @@ -1014,16 +1040,23 @@ class alignas(alignof(SpinLock)) LivenessTracker { jobject resolveCandidateRepresentative(JNIEnv *env, u32 klass_id); // Exposes the _gc_generations gate (see that member's own comment) so a - // caller outside this class - ReferenceChainTracker::pollWatchedTargets() - // (referenceChains.cpp), the LivenessTracker-to-ReferenceChainTracker - // bridging step - can skip calling selectLeakCandidates() entirely when - // the feature isn't in use, - // rather than relying on that method's own "returns 0" fallback to make - // the no-op cheap. Read-only; this accessor never toggles the flag. + // caller outside this class can skip work that only makes sense for the + // explicit generations option. Read-only; this accessor never toggles the + // flag. bool gcGenerationsEnabled() const { return _gc_generations.load(std::memory_order_relaxed); } + // Exposes the effective leak-signal availability (see _leak_signal_enabled's + // own comment) - the gate ReferenceChainTracker::hasLeakSignal() keys on. + // True when generations tracking OR liveheap tracking is on; false means no + // population-trend signal can ever be constructed and the reference-chain + // collector must not search. Read-only; this accessor never toggles the + // flag. + bool leakSignalEnabled() const { + return _leak_signal_enabled.load(std::memory_order_relaxed); + } + // Time-to-OOM projection against whichever of two independent boundaries // is tighter: the JVM heap (_heap_floor_ring vs _max_heap_bytes) or the // container/cgroup memory limit (_container_mem_ring vs @@ -1063,8 +1096,23 @@ class alignas(alignof(SpinLock)) LivenessTracker { // this method additionally divides by elapsed time, so the same gap // understates the growth rate (overstates the projected time-to-OOM) // rather than the reverse - not solved here. + // Short-horizon linear extrapolation of the rising JVM heap-floor and + // container-memory trends against their limits (see the long comment in + // livenessTracker.h). Returns -1 while the leak signal is in its burn-in + // (see OOM_PROJECTION_BURN_IN_NS). double secondsToOOM() const; + // secondsToOOM() withholds its projection (returns -1) for this long after + // the leak signal first activated (_leak_signal_activated_ns): a fresh + // process's warmup heap growth projects an imminent OOM exactly like a leak + // does. The burn-in is age-based rather than based on the heap-floor ring's + // time span on purpose - the ring holds one sample per GC, so near OOM + // back-to-back GCs compress its span to seconds and a span-based guard + // would suppress urgency in exactly the imminent-OOM case. Must be >= + // ReferenceChainTracker::OOM_URGENT_THRESHOLD_S (static_assert in + // referenceChains.cpp). + static constexpr u64 OOM_PROJECTION_BURN_IN_NS = 300ULL * 1000000000ULL; + // Third trigger for cleanup_table(), alongside track()'s table-overflow // branch (forced) and flush_table()'s JFR-flush cadence (organic): those // two both depend on ObjectSampler's allocation-sampling callback firing @@ -1545,7 +1593,7 @@ class alignas(alignof(SpinLock)) LivenessTracker { _test_klass_alias_count = 0; _table_lock.unlock(); // Also reset the heap-floor ring: it is a sibling piece of the same - // _gc_generations-gated feature, read by every selectLeakCandidates() + // leak-signal-gated feature, read by every selectLeakCandidates() // scan (heapFloorRising()), so leaving it populated across tests in the // same gtest binary would leak one test's heap-usage history into the // next test's hysteresis threshold. storeRelease() (not plain store()) @@ -1642,16 +1690,32 @@ class alignas(alignof(SpinLock)) LivenessTracker { } #endif - // Sets _gc_generations directly, bypassing initialize() (which requires a - // live JVM - VM::hotspot_version()/VM::jni(), see that method's own code - - // out of gtest's reach the same way foldKlassCountsLocked()'s + // Sets the two leak-signal gates directly, bypassing initialize() (which + // requires a live JVM - VM::hotspot_version()/VM::jni(), see that method's + // own code - out of gtest's reach the same way foldKlassCountsLocked()'s // representative-minting step is, per this seam block's own comment - // above). Callers outside this class that only need to exercise - // gcGenerationsEnabled()'s gate (e.g. referenceChains_ut.cpp's - // pollWatchedTargets() tests) use this instead of standing up a full - // initialize()/start() call. - void setGcGenerationsForTest(bool v) { - _gc_generations.store(v, std::memory_order_relaxed); + // above), with the same derivation initialize() applies: + // _gc_generations = generations, _leak_signal_enabled = generations || + // liveness. (generations=false, liveness=true) models a liveheap-only + // session (memory=N:l), so gates that must key on leakSignalEnabled() + // rather than gcGenerationsEnabled() are distinguishable under test. + void setLeakSignalSourcesForTest(bool generations, bool liveness) { + _gc_generations.store(generations, std::memory_order_relaxed); + _leak_signal_enabled.store(generations || liveness, std::memory_order_relaxed); + } + + // Kept for existing callers (javaApi.cpp, the test fixtures) that only need + // to exercise leakSignalEnabled()'s gate: sets BOTH flags to v - the + // generations option alone is a sufficient signal source, so the effective + // flag follows it. + void setGcGenerationsForTest(bool v) { setLeakSignalSourcesForTest(v, /*liveness=*/false); } + + // Overrides the burn-in stamp (see _leak_signal_activated_ns). initialize() + // needs a live JVM, so gtest never stamps it; 0 restores "never activated". + // Tests backdate it with modular u64 arithmetic, e.g. + // OS::nanotime() - OOM_PROJECTION_BURN_IN_NS - margin. + void setLeakSignalActivatedNsForTest(u64 ns) { + _leak_signal_activated_ns.store(ns, std::memory_order_release); } private: diff --git a/ddprof-lib/src/main/cpp/referenceChainEvents.cpp b/ddprof-lib/src/main/cpp/referenceChainEvents.cpp index 7290ce94a0..1a5de250ff 100644 --- a/ddprof-lib/src/main/cpp/referenceChainEvents.cpp +++ b/ddprof-lib/src/main/cpp/referenceChainEvents.cpp @@ -145,8 +145,12 @@ void ReferenceChainTracker::appendStaticFieldRootType( void ReferenceChainTracker::pollWatchedTargets(jvmtiEnv *jvmti, JNIEnv *jni) { if (!enabled() || jvmti == nullptr || jni == nullptr || - !LivenessTracker::instance()->gcGenerationsEnabled()) { - // Avoid candidate-table work when generation tracking is disabled. + !LivenessTracker::instance()->leakSignalEnabled()) { + // Avoid candidate-table work when no leak-signal source (generations or + // liveheap tracking) is active: the population table stays empty, so + // selectLeakCandidates() below would always return 0 anyway. Must match + // hasLeakSignal()'s gate - a search legitimately started on a liveheap + // source still needs its candidates polled here. return; } diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index 211686301e..4d4ea42edb 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -388,7 +388,7 @@ void ReferenceChainTracker::stop() { // - _search_start_ns still holds the previous recording's clock, so the entire stopped gap // would count toward the new recording's TTL and could abandon "its" search on the first // pass with a spurious abandonment event for a search the new recording never started; - // - _urgent_latched/_urgent_release_ticks/_urgent_search_spent would carry the previous + // - _urgent_latched/_urgent_release_ticks would carry the previous // recording's urgency episode into the new one; // - the frontier's referrer_klass slots hold StringDictionary ids from a _class_map that // Profiler::start() is about to wipe (ids restart at 1) - they cannot be remapped, so a @@ -411,7 +411,6 @@ void ReferenceChainTracker::stop() { store(_search_start_ns, (u64)0); _urgent_latched = false; _urgent_release_ticks = 0; - _urgent_search_spent = false; // Same per-search candidate/discovered canary state runPass()'s terminal branch and // restartSearch() clear - zeroing the counts here closes the stale-chain windows in // pollWatchedTargets() (dead-representative canary path, discovered-instances path) for the @@ -652,9 +651,6 @@ bool ReferenceChainTracker::shouldRunPass(u64 now_ns) { if (!afford) { return false; } - // This episode's one urgency-authorized search (_urgent_search_spent's own comment, - // referenceChains.h) is the one about to start. - _urgent_search_spent = _urgent_latched; TEST_LOG_SUMMARY("ReferenceChainTracker::shouldRunPass -> true (search not started yet)"); return true; // nothing has run yet - always worth taking the first pass } @@ -677,12 +673,10 @@ bool ReferenceChainTracker::shouldRunPass(u64 now_ns) { _safepoint_pain_budget.spend(_search_pain_ms); _search_pain_ms = 0; // Restart (this class's own header comment) if the pain budget has drained and there is still - // (or again) a leak indication to chase - canAffordNewSearch() is always true when - // LivenessTracker's population trends are not in use at all, so this only ever changes behavior - // for a search that already ran once. + // (or again) a leak indication to chase - canAffordNewSearch() is always false when no + // leak-signal source (generations or liveheap tracking) is active at all, so a misconfigured + // tracker stays idle instead of searching without a signal. if (canAffordNewSearch(now_ns)) { - // Same entitlement bookkeeping as the first-search branch above. - _urgent_search_spent = _urgent_latched; restartSearch(); TEST_LOG_SUMMARY("ReferenceChainTracker::shouldRunPass -> true (restarting search)"); return true; @@ -772,13 +766,18 @@ bool ReferenceChainTracker::shouldRunPass(u64 now_ns) { // threadLoop()'s own iteration and needs the *list* to poll each candidate's tag; this only needs // to know whether at least one exists. bool ReferenceChainTracker::isUrgent() const { + static_assert(LivenessTracker::OOM_PROJECTION_BURN_IN_NS / 1e9 >= OOM_URGENT_THRESHOLD_S, + "burn-in must cover the urgent horizon"); + // Warmup is filtered inside secondsToOOM(): no projection (-1) until + // OOM_PROJECTION_BURN_IN_NS after the leak signal first activated. Age-based + // on purpose - a guard on the heap-floor ring's time span shrinks with GC + // frequency and suppressed the latch in exactly the back-to-back-GC + // imminent-OOM case. double seconds_to_oom = LivenessTracker::instance()->secondsToOOM(); if (seconds_to_oom >= 0 && seconds_to_oom < OOM_URGENT_THRESHOLD_S) { _urgent_release_ticks = 0; if (!_urgent_latched) { _urgent_latched = true; - // A fresh episode gets a fresh entitlement to one search. - _urgent_search_spent = false; TEST_LOG_SUMMARY("ReferenceChainTracker::isUrgent latching urgency " "(secondsToOOM=%.1f < OOM_URGENT_THRESHOLD_S=%.1f)", seconds_to_oom, OOM_URGENT_THRESHOLD_S); @@ -786,13 +785,12 @@ bool ReferenceChainTracker::isUrgent() const { return true; } if (_urgent_latched) { - // Negative means "no rising trend to project from" (secondsToOOM()'s own unknown/NOT_RISING + // Negative means "no projection" (secondsToOOM()'s own unknown/NOT_RISING/BURN_IN // encoding), which counts toward release just like a comfortably distant projection does. if (seconds_to_oom < 0 || seconds_to_oom >= OOM_URGENT_RELEASE_S) { if (++_urgent_release_ticks >= URGENT_RELEASE_CONSECUTIVE) { _urgent_latched = false; _urgent_release_ticks = 0; - _urgent_search_spent = false; TEST_LOG_SUMMARY("ReferenceChainTracker::isUrgent releasing urgency " "(secondsToOOM=%.1f clear of OOM_URGENT_RELEASE_S=%.1f for " "%d consecutive observations)", @@ -811,32 +809,39 @@ bool ReferenceChainTracker::isUrgent() const { } bool ReferenceChainTracker::hasLeakSignal() { - if (!LivenessTracker::instance()->gcGenerationsEnabled()) { - // No population-trend signal to gate on at all - see this method's own header comment for why - // that means "always true" here. - return true; + if (!LivenessTracker::instance()->leakSignalEnabled()) { + // No population-trend signal can be constructed - generations tracking + // and liveheap tracking are both off, so the per-klass population table + // and the heap-floor ring never fill (LivenessTracker gates them on the + // same effective flag). Stay idle: without a signal there is nothing to + // chase, and running whole-heap searches with zero candidates "just in + // case" burns safepoint time and frontier-table memory for zero + // diagnostic value (observed: frontier grown to its cap with + // reference_chain_candidate_count == 0 in a healthy process when this + // branch used to return true). + return false; } double seconds_to_oom = LivenessTracker::instance()->secondsToOOM(); - // isUrgent() is called unconditionally, not short-circuited behind _urgent_search_spent: it is - // what maintains the latch/release counter, so skipping it would freeze the episode state (see - // _urgent_latched). + // isUrgent() is called unconditionally, not short-circuited behind any + // branch: it maintains the latch/release state (see _urgent_latched), which + // still drives the urgency ramp and the no-progress-abandonment suppression + // once a search runs. bool urgent = isUrgent(); - if (urgent && !_urgent_search_spent) { - // Heap-wide floor is rising fast enough that OOM is imminent - don't wait for a specific klass - // to clear selectLeakCandidates()'s own per-klass ring-fill/hysteresis gate; see - // OOM_URGENT_THRESHOLD_S's own comment (referenceChains.h) for why that gate alone is too slow - // here. - TEST_LOG_SUMMARY("ReferenceChainTracker::hasLeakSignal -> true (urgent, " - "secondsToOOM=%.1f)", - seconds_to_oom); - return true; - } KlassCandidate probe[1]; int n = LivenessTracker::instance()->selectLeakCandidates(probe, 1); + // The urgent latch does NOT entitle a candidate-less search. With nothing + // tagged the search can never emit a chain, and while urgent the 30-pass + // no-progress abandonment is suppressed (runPass()'s terminal gate), so a + // candidate-less search would run to the full auto-tuned TTL doing whole-heap + // BFS - pure cost. Observed in staging: a fresh process's warmup heap-floor + // rise tripped the 5-minute projection and a candidate-less search grew a + // 262,144-entry frontier (liveness_urgent_boost_admits=2495) with zero chains + // for the whole recording. Urgency still applies through the ramp (cadence, + // pause target, budget boost, 100% liveness admission) once a real candidate + // justifies the search. TEST_LOG_SUMMARY("ReferenceChainTracker::hasLeakSignal -> %s (secondsToOOM=%.1f, " - "candidates=%d, urgent=%d, urgentSearchSpent=%d)", - n > 0 ? "true" : "false", seconds_to_oom, n, urgent, - _urgent_search_spent); + "candidates=%d, urgent=%d)", + n > 0 ? "true" : "false", seconds_to_oom, n, urgent); return n > 0; } @@ -952,10 +957,9 @@ void ReferenceChainTracker::resetSearchStateForTest(jvmtiEnv *jvmti, // _safepoint_pain_budget above. _cpu_pain_budget = PainBudget(_pain_budget_refill_rate); // Same test-isolation rationale: a latched urgency episode left behind by an earlier test would - // otherwise deny this one its own urgency-authorized search (see _urgent_search_spent). + // otherwise ramp this one's cadence and pause target from a stale projection. _urgent_latched = false; _urgent_release_ticks = 0; - _urgent_search_spent = false; if (_frontier != nullptr) { // Rebuilds the table at this test's own _configured_frontier_cap, undoing any smaller framecap= diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index bfde3616ab..5b3615c993 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -338,7 +338,6 @@ class ReferenceChainTracker { // Hysteresis state behind isUrgent(). mutable bool _urgent_latched; mutable int _urgent_release_ticks; - mutable bool _urgent_search_spent; // Set (once) at the same point runPass() moves _search_state to ABANDONED - see // SearchAbandonReason's own comment for why this exists and buildAbandonedEvent()/abandonReason() @@ -738,7 +737,7 @@ class ReferenceChainTracker { _effective_budget(0), _effective_cadence_ns(PASS_CADENCE_NS), _pause_pid(1, 1.0, 1.0, 1.0, 1, 1.0), _search_started(false), _tags_released(true), _urgent_latched(false), - _urgent_release_ticks(0), _urgent_search_spent(false), + _urgent_release_ticks(0), _urgency_budget_boosted(false), _configured_budget(0), _search_state(SearchState::RUNNING), _abandon_reason(SearchAbandonReason::NONE), _search_start_ns(0), @@ -767,7 +766,9 @@ class ReferenceChainTracker { // Cheap probe (max=1, not the real poll pollWatchedTargets() makes) into LivenessTracker's // population-trend table: true if at least one klass shows a positive population slope worth - // chasing. + // chasing. Always false when no leak-signal source (generations or liveheap tracking) is + // active - the population table and heap-floor ring never fill without one, and a search + // would run whole-heap BFS passes with zero candidates for zero diagnostic value. bool hasLeakSignal(); // Latched, hysteretic view of LivenessTracker::secondsToOOM() crossing OOM_URGENT_THRESHOLD_S - @@ -1144,15 +1145,13 @@ class ReferenceChainTracker { void setSearchStartedForTest(bool v) { _search_started = v; } // OOM-urgency hysteresis state (isUrgent()'s latch/release machine, // referenceChains.cpp): tests drive it via isUrgentForTest() and assert on - // the raw latch/tick/entitlement reads. + // the raw latch/tick reads. bool isUrgentForTest() const { return isUrgent(); } bool urgentLatchedForTest() const { return _urgent_latched; } int urgentReleaseTicksForTest() const { return _urgent_release_ticks; } - bool urgentSearchSpentForTest() const { return _urgent_search_spent; } void resetUrgencyForTest() { _urgent_latched = false; _urgent_release_ticks = 0; - _urgent_search_spent = false; } // Applied vs configured CPU pain-budget refill rate: shouldRunPass() raises // the applied rate CANARY_PAIN_BUDGET_REFILL_MULTIPLIER-fold only while a diff --git a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp index 5b6e10f90f..d04ccf0950 100644 --- a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp +++ b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp @@ -19,6 +19,7 @@ #include "arguments.h" #include "counters.h" #include "objectSampler.h" +#include "os.h" #include "profiler.h" #include "referenceChainsTestAccessors.h" #include "vmEntry.h" @@ -1162,16 +1163,55 @@ class LivenessTrackerTestAccessor { return t->_last_gc_epoch; } + // Mirrors the generations value into the effective leak-signal flag, so it + // CANNOT model a liveheap-only session (generations off, signal on). Use + // initializeGatesOnlyForTest() (real initialize() derivation) or + // LivenessTracker::setLeakSignalSourcesForTest() for that. static void setSessionGatesForTest(LivenessTracker *t, bool enabled, bool generations) { t->_enabled = enabled; t->_gc_generations.store(generations, std::memory_order_relaxed); + t->_leak_signal_enabled.store(generations, std::memory_order_relaxed); } static bool enabledForTest(LivenessTracker *t) { return t->_enabled; } + static bool initializedForTest(LivenessTracker *t) { + return t->_initialized; + } + + static u64 leakSignalActivatedNsForTest(LivenessTracker *t) { + return t->_leak_signal_activated_ns.load(std::memory_order_acquire); + } + + // Runs the real (private) initialize() with _initialized forced true, so it + // takes the early return right after deriving the session gates - no + // VM/JNI/table init and no persistent _initialized flip on the singleton. + // Restores every other field initialize() touches ahead of that guard + // (_initialized, _enabled, _record_heap_usage, _watched_tid_count, + // _urgent_tracking and the burn-in stamp); only _gc_generations and + // _leak_signal_enabled keep the values initialize() derived. + static void initializeGatesOnlyForTest(LivenessTracker *t, Arguments &args) { + bool saved_initialized = t->_initialized; + bool saved_enabled = t->_enabled; + bool saved_heap_usage = t->_record_heap_usage; + int saved_watched = __atomic_load_n(&t->_watched_tid_count, __ATOMIC_ACQUIRE); + bool saved_urgent = __atomic_load_n(&t->_urgent_tracking, __ATOMIC_ACQUIRE); + u64 saved_stamp = t->_leak_signal_activated_ns.load(std::memory_order_acquire); + t->_initialized = true; + // Returns _stored_error from any prior real start - not under test. + Error ignored = t->initialize(args); + (void)ignored; + t->_initialized = saved_initialized; + t->_enabled = saved_enabled; + t->_record_heap_usage = saved_heap_usage; + __atomic_store_n(&t->_watched_tid_count, saved_watched, __ATOMIC_RELEASE); + __atomic_store_n(&t->_urgent_tracking, saved_urgent, __ATOMIC_RELEASE); + t->_leak_signal_activated_ns.store(saved_stamp, std::memory_order_release); + } + static bool entryAgeAndAdmissionForTest(LivenessTracker *t, u32 idx, jlong *age, u64 *admission_epoch) { @@ -1192,6 +1232,106 @@ TEST(LivenessTrackerSessionTest, ResetSessionClearsOnlyRecordingGates) { tracker->resetSession(); EXPECT_FALSE(LivenessTrackerTestAccessor::enabledForTest(tracker)); EXPECT_FALSE(tracker->gcGenerationsEnabled()); + EXPECT_FALSE(tracker->leakSignalEnabled()); +} + +// hasLeakSignal() must never authorize a search without a leak-signal source: +// with neither generations nor liveheap tracking active, the population table +// and heap-floor ring never fill, and searching would run whole-heap BFS +// passes with zero candidates (observed in staging as a frontier table grown +// to its cap with reference_chain_candidate_count == 0 in a healthy process). +TEST(LivenessTrackerSessionTest, LeakSignalRequiresGenerationsOrLiveness) { + LivenessTracker *tracker = LivenessTracker::instance(); + + LivenessTrackerTestAccessor::setSessionGatesForTest( + tracker, /*enabled=*/false, /*generations=*/false); + EXPECT_FALSE(tracker->leakSignalEnabled()); + + // Liveheap alone is a sufficient source: it drives the same population + // machinery, only additionally preserving stack traces for its samples. + // Driven through the real initialize() derivation from parsed arguments + // (setSessionGatesForTest cannot model liveheap-only). + Arguments live; + ASSERT_FALSE(live.parse("memory=1048576:l")); + ASSERT_TRUE(live._record_liveness); + ASSERT_FALSE(live._gc_generations); + u64 stamp_before = LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker); + bool initialized_before = LivenessTrackerTestAccessor::initializedForTest(tracker); + bool enabled_before = LivenessTrackerTestAccessor::enabledForTest(tracker); + LivenessTrackerTestAccessor::initializeGatesOnlyForTest(tracker, live); + EXPECT_FALSE(tracker->gcGenerationsEnabled()); + EXPECT_TRUE(tracker->leakSignalEnabled()) + << "initialize() must derive the leak signal from generations || liveheap"; + // The helper restores every non-gate field it touched. + EXPECT_EQ(stamp_before, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)); + EXPECT_EQ(initialized_before, LivenessTrackerTestAccessor::initializedForTest(tracker)); + EXPECT_EQ(enabled_before, LivenessTrackerTestAccessor::enabledForTest(tracker)); + + LivenessTrackerTestAccessor::setSessionGatesForTest( + tracker, /*enabled=*/false, /*generations=*/false); +} + +// initialize() derives the two gates from the parsed options: +// gcGenerationsEnabled() == generations, leakSignalEnabled() == generations || +// liveheap. The reference-chain collector keys on the effective flag, so +// liveheap tracking alone must arm it. +TEST(LivenessTrackerSessionTest, LeakSignalDerivedByInitialize) { + LivenessTracker *tracker = LivenessTracker::instance(); + struct Row { + const char *spec; + bool liveness; + bool generations; + bool expect_signal; + }; + const Row rows[] = { + {"memory=1048576:l", true, false, true}, + {"generations=true", false, true, true}, + {"generations=true,memory=1048576:l", true, true, true}, + {"memory=1048576:a", false, false, false}, + }; + for (const Row &row : rows) { + Arguments args; + ASSERT_FALSE(args.parse(row.spec)) << "row " << row.spec; + ASSERT_EQ(row.liveness, args._record_liveness) << "row " << row.spec; + ASSERT_EQ(row.generations, args._gc_generations) << "row " << row.spec; + bool initialized_before = LivenessTrackerTestAccessor::initializedForTest(tracker); + bool enabled_before = LivenessTrackerTestAccessor::enabledForTest(tracker); + u64 stamp_before = LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker); + LivenessTrackerTestAccessor::initializeGatesOnlyForTest(tracker, args); + EXPECT_EQ(row.generations, tracker->gcGenerationsEnabled()) << "row " << row.spec; + EXPECT_EQ(row.expect_signal, tracker->leakSignalEnabled()) << "row " << row.spec; + EXPECT_EQ(initialized_before, LivenessTrackerTestAccessor::initializedForTest(tracker)) + << "row " << row.spec; + EXPECT_EQ(enabled_before, LivenessTrackerTestAccessor::enabledForTest(tracker)) + << "row " << row.spec; + EXPECT_EQ(stamp_before, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)) + << "row " << row.spec; + } + LivenessTrackerTestAccessor::setSessionGatesForTest( + tracker, /*enabled=*/false, /*generations=*/false); + EXPECT_FALSE(tracker->leakSignalEnabled()); +} + +// setLeakSignalSourcesForTest() applies initialize()'s derivation, and +// setGcGenerationsForTest(v) keeps setting BOTH flags to v (existing callers +// depend on it). +TEST(LivenessTrackerSessionTest, LeakSignalSourcesSeamMatchesDerivation) { + LivenessTracker *tracker = LivenessTracker::instance(); + tracker->setLeakSignalSourcesForTest(/*generations=*/false, /*liveness=*/true); + EXPECT_FALSE(tracker->gcGenerationsEnabled()); + EXPECT_TRUE(tracker->leakSignalEnabled()); + tracker->setLeakSignalSourcesForTest(/*generations=*/true, /*liveness=*/false); + EXPECT_TRUE(tracker->gcGenerationsEnabled()); + EXPECT_TRUE(tracker->leakSignalEnabled()); + tracker->setLeakSignalSourcesForTest(/*generations=*/false, /*liveness=*/false); + EXPECT_FALSE(tracker->gcGenerationsEnabled()); + EXPECT_FALSE(tracker->leakSignalEnabled()); + tracker->setGcGenerationsForTest(true); + EXPECT_TRUE(tracker->gcGenerationsEnabled()); + EXPECT_TRUE(tracker->leakSignalEnabled()); + tracker->setGcGenerationsForTest(false); + EXPECT_FALSE(tracker->gcGenerationsEnabled()); + EXPECT_FALSE(tracker->leakSignalEnabled()); } // cleanup_table() calls VM::jni(), so install a fake JavaVM. @@ -1314,15 +1454,24 @@ class SecondsToOOMTest : public ::testing::Test { installGtestCrashHandler(); LivenessTracker::instance()->klassPopulationResetForTest(); LivenessTracker::instance()->setGcGenerationsForTest(true); + // Past the burn-in (gtest never runs initialize(), so nothing stamps it). + LivenessTracker::instance()->setLeakSignalActivatedNsForTest(backdatedStamp()); } void TearDown() override { LivenessTracker::instance()->klassPopulationResetForTest(); LivenessTracker::instance()->setGcGenerationsForTest(false); LivenessTracker::instance()->setMaxHeapBytesForTest(-1); + // 0 = "never activated"; the next start() re-stamps it through the CAS from 0. + LivenessTracker::instance()->setLeakSignalActivatedNsForTest(0); restoreDefaultSignalHandlers(); } + // A burn-in stamp one second past OOM_PROJECTION_BURN_IN_NS (modular u64). + static u64 backdatedStamp() { + return OS::nanotime() - LivenessTracker::OOM_PROJECTION_BURN_IN_NS - SEC_NS; + } + // Ten samples, one second apart, growing by 100MiB each: earliest third // (indices 0-2) means to 1100MiB at t=1s, recent third (indices 7-9) // means to 1800MiB at t=8s - a 700MiB rise over 7s, i.e. exactly @@ -1461,6 +1610,67 @@ TEST_F(SecondsToOOMTest, FloorAtMaxHeapReturnsZero) { EXPECT_EQ(tracker->secondsToOOM(), 0.0); } +// Within OOM_PROJECTION_BURN_IN_NS of activation no projection is offered, even +// for a history that projects cleanly once the burn-in has passed. +TEST_F(SecondsToOOMTest, BurnInWithholdsProjection) { + LivenessTracker *tracker = LivenessTracker::instance(); + tracker->setLeakSignalActivatedNsForTest(OS::nanotime()); + tracker->setMaxHeapBytesForTest((jlong)(2800 * MiB)); + seedRisingFloor(tracker); + EXPECT_LT(tracker->secondsToOOM(), 0.0) << "projection must be withheld during burn-in"; + + tracker->setLeakSignalActivatedNsForTest(backdatedStamp()); + EXPECT_NEAR(tracker->secondsToOOM(), 9.0, 1e-6); +} + +// A 0 stamp means "never activated", not "activated at the epoch" (which would +// make the modular age equal nanotime() and usually exceed the burn-in). +TEST_F(SecondsToOOMTest, NeverActivatedWithholdsProjection) { + LivenessTracker *tracker = LivenessTracker::instance(); + tracker->setLeakSignalActivatedNsForTest(0); + tracker->setMaxHeapBytesForTest((jlong)(2800 * MiB)); + seedRisingFloor(tracker); + EXPECT_LT(tracker->secondsToOOM(), 0.0); +} + +// The burn-in stamp is per process: neither the ring resets nor +// resetSession() may zero or re-stamp it. +TEST_F(SecondsToOOMTest, StampSurvivesResetSeams) { + LivenessTracker *tracker = LivenessTracker::instance(); + bool enabled_before = LivenessTrackerTestAccessor::enabledForTest(tracker); + u64 stamp = LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker); + ASSERT_NE(0u, stamp); + + tracker->klassPopulationResetForTest(); +#ifdef DEBUG + tracker->setHeapFloorRecordingForTest(false); + tracker->heapFloorResetForTest(); + tracker->setHeapFloorRecordingForTest(true); +#endif + tracker->resetSession(); + // resetSession() clears _enabled and both gates; re-arm the signal and + // restore _enabled in one call so neither leaks into later tests. + LivenessTrackerTestAccessor::setSessionGatesForTest(tracker, enabled_before, + /*generations=*/true); + EXPECT_EQ(stamp, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)); + + tracker->setMaxHeapBytesForTest((jlong)(2800 * MiB)); + seedRisingFloor(tracker); + EXPECT_NEAR(tracker->secondsToOOM(), 9.0, 1e-6); +} + +// onGC() records heap-floor samples whenever the effective leak signal is on - +// a liveheap-only session (generations off) must still feed secondsToOOM(). +TEST_F(SecondsToOOMTest, OnGCRecordsHeapFloorWithLiveheapOnlySource) { + LivenessTracker *tracker = LivenessTracker::instance(); + tracker->setLeakSignalSourcesForTest(/*generations=*/false, /*liveness=*/true); + ASSERT_FALSE(tracker->gcGenerationsEnabled()); + ASSERT_TRUE(tracker->leakSignalEnabled()); + // The helper EXPECTs that onGC() grew the heap-floor ring by one sample. + (void)LivenessTrackerTestAccessor::containerUsageRecordedByOnGC(tracker, + /*limit_resolved=*/false); +} + // --------------------------------------------------------------------------- // Leak tag pool (design A: direct tagging of leaking objects) // --------------------------------------------------------------------------- @@ -2507,6 +2717,8 @@ class LivenessTrackerMockJvmTest : public ::testing::Test { tracker->admissionResetForTest(); tracker->setSubsampleRatioForTest(0.1); tracker->setGcGenerationsForTest(false); + // 0 = "never activated"; the next start() re-stamps it through the CAS from 0. + tracker->setLeakSignalActivatedNsForTest(0); // start() resolved the heap limits from the mock JVM. tracker->heapLimitsResetForTest(); VMTestAccessor::setVm(saved_vm); @@ -2936,3 +3148,65 @@ TEST_F(LivenessTrackerMockJvmTest, SlotsPastTableSizeAreClearedAfterCompaction) } } } + +// Liveheap tracking alone (memory=...:l, no generations) must arm the +// effective leak signal through the real start() -> initialize() path, and +// every consumer gated on it (secondsToOOM(), maybeForceCleanup() and +// cleanup_table()'s population fold) must work without the generations +// option. The other fixtures arm the gate only through seams that mirror the +// generations value into it, so they cannot tell the two flags apart. +TEST_F(LivenessTrackerMockJvmTest, LiveheapOnlyStartArmsLeakSignalConsumers) { + constexpr u64 SEC_NS = 1000000000ULL; + constexpr u64 MiB = 1ULL << 20; + LivenessTracker *tracker = LivenessTracker::instance(); + + // 1. Re-start as liveheap-only. The gate stores (and the burn-in CAS) + // precede initialize()'s _initialized guard, so a repeated start() + // re-derives them. + tracker->setLeakSignalActivatedNsForTest(0); + Arguments args; + ASSERT_FALSE(args.parse("memory=1048576:l")); + ASSERT_TRUE(args._record_liveness); + ASSERT_FALSE(args._gc_generations); + ASSERT_FALSE(tracker->start(args)); + tracker->setSubsampleRatioForTest(1.0); + EXPECT_FALSE(tracker->gcGenerationsEnabled()); + EXPECT_TRUE(tracker->leakSignalEnabled()) + << "initialize() must derive the leak signal from generations || liveheap"; + + // 2. Burn-in stamp lifecycle: start() stamps it from 0 and never re-stamps. + EXPECT_NE(0u, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)) + << "the first enabled start() must stamp the burn-in clock"; + u64 backdated = OS::nanotime() - LivenessTracker::OOM_PROJECTION_BURN_IN_NS - SEC_NS; + tracker->setLeakSignalActivatedNsForTest(backdated); + ASSERT_FALSE(tracker->start(args)); + EXPECT_EQ(backdated, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)) + << "a later start() must not re-stamp the burn-in clock"; + + // 3. secondsToOOM() projects in a liveheap-only session. Runs before any + // sweep(): onGC() records real-clock floor samples while the signal is on. + tracker->klassPopulationResetForTest(); // also clears the heap-floor ring + tracker->setMaxHeapBytesForTest((jlong)(2800 * MiB)); + for (int i = 0; i < 10; i++) { + tracker->heapFloorRecordForTest(1000 * MiB + (u64)i * 100 * MiB, (u64)i * SEC_NS); + } + // GE rather than NEAR: start() resolved the real container limit. + EXPECT_GE(tracker->secondsToOOM(), 0.0) + << "secondsToOOM() must project in a liveheap-only session"; + tracker->klassPopulationResetForTest(); // drop the seeded ring before the sweep + tracker->setMaxHeapBytesForTest(-1); + + // 4. maybeForceCleanup() sweeps and folds the population. track() never + // folds, so the only route to a population entry is that sweep. + u32 retained_id = classIdOf(kRetainedSignature); + ASSERT_NE(0u, retained_id); + trackNew(&jvm.retained_class, /*tid=*/1); + KlassPopulationEntry entry{}; + ASSERT_FALSE(tracker->klassPopulationLookupForTest(retained_id, &entry)); + sweep(); // GarbageCollectionFinish + maybeForceCleanup(fake_now_ns) + EXPECT_TRUE(tracker->klassPopulationLookupForTest(retained_id, &entry)) + << "maybeForceCleanup() must sweep and fold the population in a liveheap-only session"; + + // 5. TearDown clears the gates via setGcGenerationsForTest(false). + tracker->setLeakSignalActivatedNsForTest(0); +} diff --git a/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc b/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc index 6295e26850..6043216620 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc @@ -712,25 +712,30 @@ TEST_F(ReferenceChainsBfsTest, CanaryStuckLimitDoublesPerRestartAndResetsOnClean // frontier-stall precondition, checked first) is deliberately suppressed by isUrgent(), and // at the base limit both conditions would fire on the same pass. Seed a ~224s-to-OOM // projection the same way referenceChainsOomTests.inc does (linear ramp over synthetic - // timestamps); restored at the end so later tests see default heap state. + // timestamps; the 60s spacing is arbitrary) with secondsToOOM()'s age-based burn-in stamp + // backdated past OOM_PROJECTION_BURN_IN_NS; restored at the end so later tests see default + // heap state. constexpr u64 SEC_NS = 1000000000ULL; constexpr u64 MiB = 1ULL << 20; constexpr u64 MAX_HEAP_TEST = 1024 * MiB; LivenessTracker::instance()->setMaxHeapBytesForTest((jlong)MAX_HEAP_TEST); LivenessTracker::instance()->setGcGenerationsForTest(true); + LivenessTracker::instance()->setLeakSignalActivatedNsForTest( + OS::nanotime() - LivenessTracker::OOM_PROJECTION_BURN_IN_NS - SEC_NS); #ifdef DEBUG LivenessTracker::instance()->setHeapFloorRecordingForTest(false); #endif { u64 clock_ns = 0; - u64 last_used = MAX_HEAP_TEST - 224 * MiB; + // remaining * 60s / 1MiB == 240 => remaining == 4MiB + u64 last_used = MAX_HEAP_TEST - 4 * MiB; for (int i = 0; i < 30; i++) { LivenessTracker::instance()->heapFloorRecordForTest( last_used - MiB * (u64)(29 - i), clock_ns, 0); - clock_ns += SEC_NS; + clock_ns += 60 * SEC_NS; } } - ASSERT_NEAR(LivenessTracker::instance()->secondsToOOM(), 224.0, 1.0); + ASSERT_NEAR(LivenessTracker::instance()->secondsToOOM(), 240.0, 1.0); constexpr int kChainLength = 10; std::vector nodes; @@ -804,6 +809,8 @@ TEST_F(ReferenceChainsBfsTest, CanaryStuckLimitDoublesPerRestartAndResetsOnClean // fixtures use in SetUp, applied to the leaked half. LivenessTracker::instance()->setMaxHeapBytesForTest(-1); LivenessTracker::instance()->setGcGenerationsForTest(false); + // 0 = "never activated"; the next start() re-stamps it through the CAS from 0. + LivenessTracker::instance()->setLeakSignalActivatedNsForTest(0); tracker->stop(); ReferenceChainsTestAccessor::resetUrgencyForTest(); ReferenceChainsTestAccessor::setWatchedLeakKlassIdsForTest({}); @@ -888,6 +895,34 @@ TEST_F(ReferenceChainsBfsTest, CanaryLaneBacksOffWithoutProgressAndResetsOnProgr tracker->stop(); } +// With no leak-signal source active (neither generations nor liveheap +// tracking), the collector must stay completely idle: no first pass and no +// restart, ever. Searching without a signal runs whole-heap BFS passes with +// zero candidates - the exact waste the leak-signal gate exists to prevent +// (observed in staging: frontier grown to its cap with +// reference_chain_candidate_count == 0 in a healthy process, because the +// no-signal branch used to return true unconditionally). +TEST_F(ReferenceChainsBfsTest, NoSearchWithoutLeakSignalSource) { + Arguments args; + ASSERT_FALSE(args.parse("referencechains=true")); + ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); + ASSERT_FALSE(tracker->start(args)); + + // Force the no-signal state regardless of what earlier tests left behind: + // setGcGenerationsForTest(false) clears both the raw option flag and the + // effective leak-signal gate (initialize() would derive the effective + // flag from generations || liveheap; this seam bypasses initialize()). + LivenessTracker::instance()->setGcGenerationsForTest(false); + ASSERT_FALSE(LivenessTracker::instance()->leakSignalEnabled()); + + // First-search gate: a brand-new tracker must not start a pass. + EXPECT_FALSE(ReferenceChainsTestAccessor::canAffordNewSearch(OS::nanotime())); + EXPECT_FALSE(ReferenceChainsTestAccessor::shouldRunPass(OS::nanotime())) + << "no leak-signal source means no search, not a search without a gate"; + + tracker->stop(); +} + TEST_F(ReferenceChainsBfsTest, ChainCompletesWithoutAbandonment) { Arguments args; // budget=1 on a graph where each pass admits exactly one new edge until the chain is exhausted, diff --git a/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc b/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc index e3e58058c9..397c7c4328 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc @@ -316,8 +316,7 @@ TEST_F(ReferenceChainsRecordingBoundaryTest, // A search still in flight when the profiler stops: started, RUNNING, a TTL clock that // predates the stop, a latched urgency episode, and live canary candidate state. ReferenceChainsTestAccessor::setInFlightSearchForTest(1ULL << 40); - ReferenceChainsTestAccessor::setUrgencyEpisodeForTest(/*latched=*/true, /*ticks=*/2, - /*spent=*/true); + ReferenceChainsTestAccessor::setUrgencyEpisodeForTest(/*latched=*/true, /*ticks=*/2); ReferenceChainsTestAccessor::setCandidateCountForTest(2); ReferenceChainsTestAccessor::setCandidateFoundBitsForTest(0x1); ReferenceChainsTestAccessor::setCandidateFrontierTagForTest(0, 42); @@ -337,7 +336,6 @@ TEST_F(ReferenceChainsRecordingBoundaryTest, // The urgency episode does not carry over into the next recording. EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()); EXPECT_EQ(0, ReferenceChainsTestAccessor::urgentReleaseTicksForTest()); - EXPECT_FALSE(ReferenceChainsTestAccessor::urgentSearchSpentForTest()); // The canary/discovered candidate state is cleared with the search that owned it. EXPECT_EQ(0, ReferenceChainsTestAccessor::candidateCountForTest()); EXPECT_EQ(0u, ReferenceChainsTestAccessor::candidateFoundBitsForTest()); @@ -375,8 +373,25 @@ TEST_F(ReferenceChainsRecordingBoundaryTest, // The release pass was the terminal no-op: no walk ran. EXPECT_EQ(0, tracker->passesRun()); - // With generations disabled (this fixture's SetUp), canAffordNewSearch() is unconditional, - // so the next shouldRunPass() restarts the search - exactly like a brand-new tracker. + // With no leak-signal source (this fixture's SetUp), canAffordNewSearch() always refuses, so + // the terminal search stays terminal. Arm the signal (generations tracking alone is a + // sufficient source) AND seed a growing candidate: the restart gate requires both the signal + // source and a live leak indication (hasLeakSignal()'s selectLeakCandidates() probe); + // TearDown restores the no-signal state. + LivenessTracker::instance()->setGcGenerationsForTest(true); + int fake_object_storage = 0; + { + int slot; + bool created; + for (u16 i = 1; i <= 20; i++) { + LivenessTracker::instance()->klassPopulationRecordForTest( + /*klass_id=*/1, i, i, &slot, &created); + LivenessTracker::instance()->tidTrendRecordForTest( + /*klass_id=*/1, /*tid=*/4242, (u32)i, (u64)i); + } + LivenessTracker::instance()->klassPopulationSetRepresentativeForTest( + nullptr, /*klass_id=*/1, (jweak)&fake_object_storage); + } EXPECT_TRUE(ReferenceChainsTestAccessor::shouldRunPass(2000)); EXPECT_EQ(SearchState::RUNNING, ReferenceChainsTestAccessor::searchStateForTest()); @@ -419,7 +434,24 @@ TEST_F(ReferenceChainsRecordingBoundaryTest, NoStaleFrontierIdsAfterRecordingBou EXPECT_TRUE(ReferenceChainsTestAccessor::tagsReleased()); // ... and the restart that follows resets the frontier content, so no chain can ever be // built from the previous _class_map generation's ids (which cannot be remapped: the - // dictionary contents are gone and its id namespace restarts at 1). + // dictionary contents are gone and its id namespace restarts at 1). Arm the leak signal AND + // seed a growing candidate for the restart step only - the restart gate requires both the + // signal source (generations or liveheap) and a live leak indication; the no-signal state + // this fixture runs in would keep the search terminal. TearDown restores it. + LivenessTracker::instance()->setGcGenerationsForTest(true); + { + int fake_object_storage = 0; + int slot; + bool created; + for (u16 i = 1; i <= 20; i++) { + LivenessTracker::instance()->klassPopulationRecordForTest( + /*klass_id=*/1, i, i, &slot, &created); + LivenessTracker::instance()->tidTrendRecordForTest( + /*klass_id=*/1, /*tid=*/4242, (u32)i, (u64)i); + } + LivenessTracker::instance()->klassPopulationSetRepresentativeForTest( + nullptr, /*klass_id=*/1, (jweak)&fake_object_storage); + } EXPECT_TRUE(ReferenceChainsTestAccessor::shouldRunPass(2000)); EXPECT_EQ(SearchState::RUNNING, ReferenceChainsTestAccessor::searchStateForTest()); diff --git a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc index c21cbf9936..2b90c80739 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc @@ -3,17 +3,23 @@ // OOM-ramp hysteresis (C5): isUrgent()'s latch/release machine and // shouldRunPass()'s canary-refill raise, driven deterministically. // -// No threads and no wall-clock dependence: the heap trajectory is faked by +// No threads: the heap trajectory is faked by // seeding LivenessTracker's usage rings with synthetic timestamps // (heapFloorRecordForTest() + setMaxHeapBytesForTest() + setGcGenerationsForTest()), // and both machines under test take their clock as an argument (isUrgent() // reads live state; shouldRunPass(now_ns) takes now_ns). For a perfectly -// linear usage ramp of R bytes per 1s sample against max heap M, the -// projection (livenessTracker.cpp's regression form) reduces to -// secondsToOOM() == (M - last_used) / R, so each scenario seeds the exact -// urgency reading it wants and re-seeds between observations - the -// production loop re-reads the projection once per ~1s iteration the same -// way. +// linear usage ramp of RATE bytes per SAMPLE_SPACING_S sample against max +// heap M, the projection (livenessTracker.cpp's regression form) reduces to +// secondsToOOM() == (M - last_used) * SAMPLE_SPACING_S / RATE, so each +// scenario seeds the exact urgency reading it wants and re-seeds between +// observations - the production loop re-reads the projection once per ~1s +// iteration the same way. +// +// The 15s sample spacing is arbitrary, kept for readable arithmetic. Warmup +// is filtered by secondsToOOM()'s age-based burn-in (OOM_PROJECTION_BURN_IN_NS +// after the leak signal first activated), not by the time span of the sample +// ring, so SetUp() backdates the burn-in stamp against the real monotonic +// clock - the only wall-clock read in these scenarios. // --------------------------------------------------------------------------- #include @@ -25,13 +31,17 @@ protected: static constexpr u64 SEC_NS = 1000000000ULL; static constexpr u64 MiB = 1ULL << 20; static constexpr u64 MAX_HEAP = 1024 * MiB; - // Bytes of heap growth per 1s sample - the projection's slope. + // Bytes of heap growth per sample - the projection's slope is + // RATE / SAMPLE_SPACING_S per second. static constexpr u64 RATE = MiB; + // Arbitrary - see the file comment above. + static constexpr u64 SAMPLE_SPACING_S = 15; void SetUp() override { ReferenceChainsTestAccessor::resetUrgencyForTest(); LivenessTracker::instance()->setGcGenerationsForTest(true); LivenessTracker::instance()->setMaxHeapBytesForTest((jlong)MAX_HEAP); + LivenessTracker::instance()->setLeakSignalActivatedNsForTest(backdatedStamp()); #ifdef DEBUG // No real GC may interleave a sample into the ring while a scenario's // synthetic trajectory is mid-description. @@ -43,6 +53,13 @@ protected: ReferenceChainsTestAccessor::resetUrgencyForTest(); LivenessTracker::instance()->setMaxHeapBytesForTest(-1); LivenessTracker::instance()->setGcGenerationsForTest(false); + // 0 = "never activated"; the next start() re-stamps it through the CAS from 0. + LivenessTracker::instance()->setLeakSignalActivatedNsForTest(0); + } + + // A burn-in stamp one second past OOM_PROJECTION_BURN_IN_NS (modular u64). + static u64 backdatedStamp() { + return OS::nanotime() - LivenessTracker::OOM_PROJECTION_BURN_IN_NS - SEC_NS; } // Overwrites the whole usage ring with a linear ramp whose projection is @@ -52,13 +69,16 @@ protected: // which secondsToOOM() reports as -1 (no rising trend - the // recent-half corroboration rejects the flat regression). double project(double seconds, bool flat = false) { - u64 last_used = MAX_HEAP - (u64)(seconds * (double)RATE); + // remaining * SAMPLE_SPACING_S / RATE == seconds => + // remaining == seconds * RATE / SAMPLE_SPACING_S + u64 last_used = MAX_HEAP - (u64)(seconds * (double)RATE / + (double)SAMPLE_SPACING_S); for (int i = 0; i < 30; i++) { u64 used = flat ? MAX_HEAP / 2 : last_used - RATE * (u64)(29 - i); LivenessTracker::instance()->heapFloorRecordForTest( used, _clock_ns, 0); - _clock_ns += SEC_NS; + _clock_ns += SAMPLE_SPACING_S * SEC_NS; } return LivenessTracker::instance()->secondsToOOM(); } @@ -142,8 +162,7 @@ TEST_F(OomUrgencyTest, UnknownProjectionCountsTowardRelease) { } // After a release, mid-band projections stay unlatched (no residual state), -// and a fresh sub-threshold crossing re-latches with a fresh search -// entitlement (_urgent_search_spent reset). +// and a fresh sub-threshold crossing re-latches cleanly. TEST_F(OomUrgencyTest, RelatchAfterReleaseGetsFreshEntitlement) { ASSERT_NEAR(project(299.0), 299.0, 1.0); ASSERT_TRUE(ReferenceChainsTestAccessor::isUrgentForTest()); @@ -160,12 +179,83 @@ TEST_F(OomUrgencyTest, RelatchAfterReleaseGetsFreshEntitlement) { EXPECT_FALSE(ReferenceChainsTestAccessor::isUrgentForTest()); EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()); - // Re-cross the threshold: re-latch, and the entitlement to spend one - // urgency-authorized search is restored. + // Re-cross the threshold: re-latch. EXPECT_NEAR(project(299.0), 299.0, 1.0); EXPECT_TRUE(ReferenceChainsTestAccessor::isUrgentForTest()); EXPECT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()); - EXPECT_FALSE(ReferenceChainsTestAccessor::urgentSearchSpentForTest()); +} + +// During the burn-in (OOM_PROJECTION_BURN_IN_NS after the leak signal first +// activated) no projection is offered, so urgency never latches: a fresh +// process's warmup growth curve projects an imminent OOM just as a real leak +// does (the staging incident: a fresh process latched urgency and ran a +// candidate-less whole-heap search to the TTL). +TEST_F(OomUrgencyTest, BurnInDoesNotLatchUrgency) { + LivenessTracker::instance()->setLeakSignalActivatedNsForTest(OS::nanotime()); + // Earlier scenarios advanced the shared seeding clock far past this test's + // 0-based timestamps - clear the ring so it holds exactly this test's + // 10 samples. + LivenessTracker::instance()->klassPopulationResetForTest(); + constexpr u64 RATE_1S = 100 * MiB; // 100MiB/s - projects ~10s once past burn-in + for (int i = 0; i < 10; i++) { + LivenessTracker::instance()->heapFloorRecordForTest( + MAX_HEAP - 1000 * MiB + (u64)i * RATE_1S, (u64)i * SEC_NS, 0); + } + EXPECT_LT(LivenessTracker::instance()->secondsToOOM(), 0.0) + << "projection must be withheld during burn-in"; + EXPECT_FALSE(ReferenceChainsTestAccessor::isUrgentForTest()); + EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()); + + // Control: the same seeds are urgent-grade once the burn-in has passed, + // so the negative half above is not trivially satisfied. + LivenessTracker::instance()->setLeakSignalActivatedNsForTest(backdatedStamp()); + double seconds = LivenessTracker::instance()->secondsToOOM(); + ASSERT_GE(seconds, 0.0); + ASSERT_LT(seconds, 300.0) << "scenario must project inside the urgent band"; +} + +// Regression: near OOM, back-to-back GCs record heap-floor samples less than a +// second apart, so the whole ring spans only seconds. Past the burn-in such a +// GC-compressed history must still project and latch urgency on the first +// sub-threshold reading (the removed span-based guard compared a ~10s +// earliest-to-recent span against the 300s horizon and never latched here). +TEST_F(OomUrgencyTest, GcCompressedWindowLatchesAfterBurnIn) { + LivenessTracker::instance()->klassPopulationResetForTest(); + // project()'s linear ramp with the spacing compressed from 15s to 0.5s: + // RATE (1MiB) every 0.5s is 2MiB/s; 200MiB of headroom projects to 100s. + // project() itself is not used - its shared clock and 15s spacing would + // defeat the compression. + u64 last_used = MAX_HEAP - 200 * MiB; + for (int i = 0; i < 30; i++) { + LivenessTracker::instance()->heapFloorRecordForTest( + last_used - RATE * (u64)(29 - i), (u64)i * SEC_NS / 2, 0); + } + double seconds = LivenessTracker::instance()->secondsToOOM(); + ASSERT_GE(seconds, 0.0); + ASSERT_LT(seconds, 300.0) << "scenario must project inside the urgent band"; + EXPECT_NEAR(seconds, 100.0, 1.0); + + EXPECT_TRUE(ReferenceChainsTestAccessor::isUrgentForTest()) + << "a GC-compressed ring past the burn-in must latch urgency"; + EXPECT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()); +} + +// A burn-in -1 is "no projection" like any other negative reading: it counts +// toward release, neither holding the latch as urgent nor as mid-band. +TEST_F(OomUrgencyTest, BurnInProjectionCountsTowardRelease) { + ASSERT_NEAR(project(299.0), 299.0, 1.0); + ASSERT_TRUE(ReferenceChainsTestAccessor::isUrgentForTest()); + + LivenessTracker::instance()->setLeakSignalActivatedNsForTest(OS::nanotime()); + for (int i = 1; i <= 4; i++) { + EXPECT_LT(project(299.0), 0.0); + EXPECT_TRUE(ReferenceChainsTestAccessor::isUrgentForTest()) + << "released after only " << i << " burn-in observations"; + EXPECT_EQ(i, ReferenceChainsTestAccessor::urgentReleaseTicksForTest()); + } + EXPECT_LT(project(299.0), 0.0); + EXPECT_FALSE(ReferenceChainsTestAccessor::isUrgentForTest()); + EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()); } // --------------------------------------------------------------------------- diff --git a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h index a7045e8582..19c93ad761 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h +++ b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h @@ -170,9 +170,6 @@ class ReferenceChainsTestAccessor { static int urgentReleaseTicksForTest() { return ReferenceChainTracker::instance()->urgentReleaseTicksForTest(); } - static bool urgentSearchSpentForTest() { - return ReferenceChainTracker::instance()->urgentSearchSpentForTest(); - } static void resetUrgencyForTest() { ReferenceChainTracker::instance()->resetUrgencyForTest(); } @@ -361,11 +358,10 @@ class ReferenceChainsTestAccessor { // Recording-boundary tests: seed a latched urgency episode (the state the OOM-projection // machine leaves behind) without driving LivenessTracker's heap-floor history. - static void setUrgencyEpisodeForTest(bool latched, int ticks, bool spent) { + static void setUrgencyEpisodeForTest(bool latched, int ticks) { ReferenceChainTracker *t = ReferenceChainTracker::instance(); t->_urgent_latched = latched; t->_urgent_release_ticks = ticks; - t->_urgent_search_spent = spent; } static u64 searchStartNsForTest() { diff --git a/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc b/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc index 44f68b6e8b..284ec7c3e4 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc @@ -435,8 +435,8 @@ TEST_F(PollWatchedTargetsTest, NoOpWhenGcGenerationsDisabled) { ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); ASSERT_FALSE(tracker->start(args)); - // Overrides SetUp(): exercises pollWatchedTargets()'s LivenessTracker _gc_generations guard, - // not just the tracker's own _enabled. + // Overrides SetUp(): exercises pollWatchedTargets()'s LivenessTracker leak-signal gate (the + // effective generations-or-liveheap flag), not just the tracker's own _enabled. LivenessTracker::instance()->setGcGenerationsForTest(false); int fake_object_storage = 0; @@ -453,6 +453,39 @@ TEST_F(PollWatchedTargetsTest, NoOpWhenGcGenerationsDisabled) { tracker->stop(); } +// pollWatchedTargets() must gate on the effective leak-signal flag, not the raw generations option: +// a search started on a liveheap-only source (hasLeakSignal() keys on leakSignalEnabled()) still +// needs its candidates polled here. +TEST_F(PollWatchedTargetsTest, PollsCandidatesWithLiveheapOnlySignalSource) { + Arguments args; + ASSERT_FALSE(args.parse("referencechains=true")); + ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); + ASSERT_FALSE(tracker->start(args)); + + // Overrides SetUp(): liveheap-only source - generations off, effective leak signal on. + LivenessTracker::instance()->setLeakSignalSourcesForTest(/*generations=*/false, + /*liveness=*/true); + ASSERT_FALSE(LivenessTracker::instance()->gcGenerationsEnabled()); + ASSERT_TRUE(LivenessTracker::instance()->leakSignalEnabled()); + + int fake_object_storage = 0; + jobject obj = reinterpret_cast(&fake_object_storage); + seedGrowingCandidate(/*source_tag=*/1, /*rep=*/(jweak)obj); + ASSERT_TRUE(tracker->frontierTable()->insert( + /*tag=*/7, /*parent_tag=*/0, /*referrer_klass=*/1, /*depth=*/0, + FrontierEntryState::EDGE)); + tags[obj] = 7; + ReferenceChainsTestAccessor::setCandidateFrontierTagForTest(0, 7); + + tracker->pollWatchedTargets(&mock_jvmti, &mock_jni); + + EXPECT_EQ(1u, ReferenceChainsTestAccessor::resolvedChainCount()) + << "a liveheap-only leak signal must still poll candidates"; + EXPECT_TRUE(ReferenceChainsTestAccessor::hasResolvedChainForTag(7)); + + tracker->stop(); +} + class ResolvedChainCacheTest : public ::testing::Test { protected: void SetUp() override { @@ -1165,6 +1198,8 @@ protected: // Set by UrgentOOMProjectionBypassesCandidateGate; reset here so a failed ASSERT_* there // can't leak a stale max heap into the next test. LivenessTracker::instance()->setMaxHeapBytesForTest(-1); + // 0 = "never activated"; the next start() re-stamps it through the CAS from 0. + LivenessTracker::instance()->setLeakSignalActivatedNsForTest(0); } // No loaded classes to resolve - resolveLoadedClasses() reports 0 and does nothing further. @@ -1214,9 +1249,12 @@ protected: } }; -TEST_F(SearchRestartTest, WithoutGenerationsSignalRestartStaysUnconditional) { - // gc_generations off (SetUp default): canAffordNewSearch() has no candidate signal, so a - // terminal search is immediately eligible to restart. +TEST_F(SearchRestartTest, WithoutLeakSignalSourceRestartStaysIdle) { + // No leak-signal source (SetUp default: neither generations nor liveheap + // tracking): canAffordNewSearch() can never see a candidate signal, so a + // terminal search stays terminal - the tracker is idle until a signal + // source exists. Searching without a signal runs whole-heap BFS passes + // with zero candidates, which is the waste this gate exists to prevent. Arguments args; ASSERT_FALSE(args.parse("referencechains=true")); ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); @@ -1225,8 +1263,8 @@ TEST_F(SearchRestartTest, WithoutGenerationsSignalRestartStaysUnconditional) { ASSERT_TRUE(tracker->runPass(&mock_jvmti, nullptr)); ASSERT_EQ(SearchState::COMPLETED, tracker->searchState()); - EXPECT_TRUE(ReferenceChainsTestAccessor::shouldRunPass(1)); - EXPECT_EQ(SearchState::RUNNING, tracker->searchState()); + EXPECT_FALSE(ReferenceChainsTestAccessor::shouldRunPass(1)); + EXPECT_EQ(SearchState::COMPLETED, tracker->searchState()); tracker->stop(); } @@ -1252,6 +1290,29 @@ TEST_F(SearchRestartTest, GenerationsEnabledButNoCandidateBlocksFirstSearch) { tracker->stop(); } +// hasLeakSignal() must gate on the effective leak-signal flag, not the raw generations option: a +// liveheap-only session (generations off) with a real candidate starts its first search. +TEST_F(SearchRestartTest, LiveheapOnlySourceWithCandidateStartsFirstSearch) { + LivenessTracker::instance()->setLeakSignalSourcesForTest(/*generations=*/false, + /*liveness=*/true); + ASSERT_FALSE(LivenessTracker::instance()->gcGenerationsEnabled()); + ASSERT_TRUE(LivenessTracker::instance()->leakSignalEnabled()); + Arguments args; + ASSERT_FALSE(args.parse("referencechains=true")); + ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); + ASSERT_FALSE(tracker->start(args)); + + EXPECT_FALSE(ReferenceChainsTestAccessor::shouldRunPass(1)) << "no candidate yet"; + + int fake_object_storage = 0; + seedGrowingCandidate(/*source_tag=*/1, /*rep=*/(jweak)&fake_object_storage); + + EXPECT_TRUE(ReferenceChainsTestAccessor::shouldRunPass(2)) + << "a liveheap-only leak signal with a candidate must start the search"; + + tracker->stop(); +} + TEST_F(SearchRestartTest, GenerationsEnabledButNoCandidateBlocksRestart) { LivenessTracker::instance()->setGcGenerationsForTest(true); Arguments args; @@ -1333,21 +1394,28 @@ TEST_F(SearchRestartTest, PainBudgetBlocksARestartUntilItDrains) { tracker->stop(); } -// hasLeakSignal()'s OOM_URGENT_THRESHOLD_S fast path (referenceChains.h/.cpp): a heap-wide leak -// growing fast enough to project exhaustion sooner than the threshold must start a search -// immediately, without waiting for any klass to clear selectLeakCandidates()'s per-klass -// ring-fill/hysteresis gate. -TEST_F(SearchRestartTest, UrgentOOMProjectionBypassesCandidateGate) { +// hasLeakSignal()'s urgent latch does NOT authorize a candidate-less search: with +// nothing tagged the search can never emit a chain, and while urgent the 30-pass +// no-progress abandonment is suppressed, so it would run to the full TTL doing +// whole-heap BFS (observed in staging: a warmup heap-floor rise latched urgency +// and a candidate-less search grew a 262,144-entry frontier for the whole +// recording). A search starts only when a klass clears selectLeakCandidates(); +// urgency then applies through the ramp. The burn-in stamp is backdated so +// secondsToOOM()'s age-based burn-in does not mask the latch - this test +// isolates the candidate requirement, not the burn-in. +TEST_F(SearchRestartTest, UrgentLatchWithoutCandidateDoesNotStartSearch) { LivenessTracker::instance()->setGcGenerationsForTest(true); constexpr u64 SEC_NS = 1000000000ULL; constexpr u64 MiB = 1ULL << 20; - // Same worked example as livenessTracker_ut.cpp's - // SecondsToOOMTest.RisingFloorProjectsExpectedSeconds: 700MiB rise over 7s against a 2800MiB - // max heap projects to 10s - comfortably under OOM_URGENT_THRESHOLD_S (5 minutes). - LivenessTracker::instance()->setMaxHeapBytesForTest((jlong)(2800 * MiB)); - for (int i = 0; i < 10; i++) { + LivenessTracker::instance()->setLeakSignalActivatedNsForTest( + OS::nanotime() - LivenessTracker::OOM_PROJECTION_BURN_IN_NS - SEC_NS); + // Rising floor against a 4000MiB max heap, 29 samples x 60s (the spacing + // is arbitrary): slope = 100MiB/60s, remaining = 280MiB -> projection = + // 280 * 60 / 100 = 168s - inside the urgent band. + LivenessTracker::instance()->setMaxHeapBytesForTest((jlong)(4000 * MiB)); + for (int i = 0; i < 30; i++) { LivenessTracker::instance()->heapFloorRecordForTest( - 1000 * MiB + (u64)i * 100 * MiB, (u64)i * SEC_NS); + 820 * MiB + (u64)i * 100 * MiB, (u64)i * 60 * SEC_NS); } Arguments args; @@ -1355,9 +1423,20 @@ TEST_F(SearchRestartTest, UrgentOOMProjectionBypassesCandidateGate) { ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); ASSERT_FALSE(tracker->start(args)); - EXPECT_TRUE(ReferenceChainsTestAccessor::shouldRunPass(1)); - EXPECT_EQ(SearchState::RUNNING, tracker->searchState()); + // Urgency latched, but no candidate has cleared the leak-signal probe. + ASSERT_TRUE(ReferenceChainsTestAccessor::isUrgentForTest()); + EXPECT_FALSE(ReferenceChainsTestAccessor::shouldRunPass(1)) + << "urgent without a candidate must not start a candidate-less search"; + EXPECT_EQ(0, tracker->passesRun()); + + // Once a klass clears the probe, the search starts - and urgency applies + // through the ramp from that point on. + int fake_object_storage = 0; + seedGrowingCandidate(/*source_tag=*/1, /*rep=*/(jweak)&fake_object_storage); + EXPECT_TRUE(ReferenceChainsTestAccessor::shouldRunPass(2)); + // 0 = "never activated"; the next start() re-stamps it through the CAS from 0. + LivenessTracker::instance()->setLeakSignalActivatedNsForTest(0); tracker->stop(); } diff --git a/ddprof-lib/src/test/cpp/referenceChains_ut.cpp b/ddprof-lib/src/test/cpp/referenceChains_ut.cpp index ebcae873a1..84ae571497 100644 --- a/ddprof-lib/src/test/cpp/referenceChains_ut.cpp +++ b/ddprof-lib/src/test/cpp/referenceChains_ut.cpp @@ -46,6 +46,9 @@ static ReferenceChainsGlobalSetup global_setup; [[maybe_unused]] static const bool kRcDebugLevelPinnedForTests = setenv("DD_PROFILING_REFERENCE_CHAINS_DEBUG", "2", 1) == 0; +// NOTE: the .inc files below are NOT registered as compile inputs by the gtest +// plugin (GtestTaskBuilder wires only the .cpp into `sources`), so editing an +// .inc alone leaves this binary stale - touch this file to force recompilation. #include "referenceChainsCoreTests.inc" #include "referenceChainsBfsTests.inc" #include "referenceChainsLifecycleTests.inc" @@ -57,3 +60,7 @@ static ReferenceChainsGlobalSetup global_setup; #include "referenceChainsAnchorTests.inc" #include "referenceChainsEventTests.inc" #include "referenceChainsPesTests.inc" + +// Recompilation marker: .inc files are not compile inputs (see note above). + + diff --git a/doc/ReferenceChainsCollectionSummary.md b/doc/ReferenceChainsCollectionSummary.md index e2351f29ff..d1aebfb163 100644 --- a/doc/ReferenceChainsCollectionSummary.md +++ b/doc/ReferenceChainsCollectionSummary.md @@ -6,9 +6,11 @@ Reference-chain collection helps explain why a sampled object remains live. It records a bounded chain of referrer classes from a GC root to a sampled object. It does not replace a heap dump or compute an exact retained set. -The profiler disables this feature by default. Enable generation tracking and reference-chain -collection with `generations=true,referencechains=true`. The bare `referencechains=true` option -does not activate the allocation engine that supplies candidates. +The profiler disables this feature by default. Enable generation tracking or liveheap tracking +(either supplies the leak signal) together with reference-chain collection, e.g. +`generations=true,referencechains=true`. The bare `referencechains=true` option does not activate +the allocation engine that supplies candidates, and without a signal source the collector stays +idle: it never starts a search. ## Data flow diff --git a/doc/architecture/LiveHeapReferenceChainsImplementation.md b/doc/architecture/LiveHeapReferenceChainsImplementation.md index bf382ea2a3..5e8a14e31c 100644 --- a/doc/architecture/LiveHeapReferenceChainsImplementation.md +++ b/doc/architecture/LiveHeapReferenceChainsImplementation.md @@ -200,14 +200,17 @@ These values are tuning inputs. They do not state guaranteed overhead. ## Configuration -Reference-chain collection requires the allocation engine and generation tracking. Enable them in -the same command: +Reference-chain collection requires the allocation engine and a leak-signal source. The signal +source is generations tracking OR liveheap tracking (the two drive the same liveness mechanisms; +liveheap additionally preserves stack traces for its samples). With neither, the leak-signal gate +can never pass and the collector stays idle. Enable one of them in the same command: ```text generations=true,referencechains=true:hops=N:budget=N:ttl=N:framecap=N:pausetarget=N:painbudget=N:firstpassbudget=N ``` -- `generations=true` activates the allocation engine and supplies leak candidates. +- `generations=true` activates the allocation engine and supplies leak candidates. Liveheap + tracking (`memory=b:l|L`) is an equivalent signal source. - `hops` limits chain depth. - `budget` supplies the base edge-admission budget. - `ttl` sets search lifetime in milliseconds. A value at or below zero disables this time check. diff --git a/doc/architecture/ReferenceChainsSignalsExplained.md b/doc/architecture/ReferenceChainsSignalsExplained.md index 5da376657f..56fac83a52 100644 --- a/doc/architecture/ReferenceChainsSignalsExplained.md +++ b/doc/architecture/ReferenceChainsSignalsExplained.md @@ -18,14 +18,11 @@ The GC callback does not wake the thread. The normal idle loop checks the state ## Starting a search -Before the first search, `shouldRunPass()` calls `canAffordNewSearch()`. The gate requires a leak indication when GC-generation tracking is active. The indication is one of these conditions: +Before the first search, `shouldRunPass()` calls `canAffordNewSearch()`. The gate always requires a leak indication: `selectLeakCandidates()` finds a class with sustained population growth. The safepoint cost budget must also permit the search. -- `selectLeakCandidates()` finds a class with sustained population growth. -- The time-to-OOM estimate enters the urgent range and the current urgent episode has not started a search. +Urgency does not authorize a search. A candidate-less search can never emit a chain, and while urgent the 30-pass no-progress abandonment is suppressed, so it would run to the full TTL doing whole-heap BFS. Urgency applies through the ramp once a search is running. -The safepoint cost budget must also permit the search. - -If GC-generation tracking is not active, the leak-signal part of this gate does not apply. +The population-trend signal is constructed by the liveness tracker, which fills the per-klass population table and the heap-floor ring when generations tracking OR liveheap tracking is active (the two options drive the same tracking mechanisms; liveheap additionally preserves stack traces for its samples). When neither is active, no signal can ever be produced: `hasLeakSignal()` returns false, and the collector stays idle - it never runs a pass, restart, or search. ## Scheduling a running search @@ -113,16 +110,20 @@ threshold is a hard real-time guarantee. `secondsToOOM()` independently projects exhaustion from confirmed rising JVM heap-floor and container-memory trends. It returns the shorter valid estimate. A negative result means that -neither source provides a valid estimate. +neither source provides a valid estimate, or that the projection is still in its burn-in period. Two ranges have different purposes: - At less than 30 minutes, the OOM ramp increases the pause target, shortens the cadence, increases the edge-budget ceiling, and admits all liveness samples. -- At less than 5 minutes, `isUrgent()` latches an urgent episode. The episode can authorize one search without waiting for a class to complete normal hysteresis. +- At less than 5 minutes, `isUrgent()` latches an urgent episode. The latch does not start searches; it sharpens the ramp's response while a candidate-driven search runs. + +Both ranges apply only after the burn-in period. + +For `OOM_PROJECTION_BURN_IN_NS` (5 minutes) after the leak signal first activates in the process, `secondsToOOM()` returns -1, so neither the 30-minute ramp nor the urgent latch fires. A burn-in is needed because warmup growth projects an imminent OOM just as a real leak does. The burn-in depends on how long the leak signal has been active, not on the time span of the heap-floor samples. The sampler records one sample per GC, so near OOM back-to-back GCs compress the sample history to a few seconds. Once the burn-in has passed, that compressed history no longer blocks the latch. -The urgent latch clears after five consecutive checks at 10 minutes or more, or after five consecutive checks with no valid rising-trend estimate. This hysteresis prevents repeated search restarts when the estimate moves around the threshold. +The urgent latch clears after five consecutive checks at 10 minutes or more, or after five consecutive checks with no valid rising-trend estimate. This hysteresis prevents the ramp's sharpened response from flapping when the estimate moves around the threshold. -The ramp and the latch use different thresholds. Entering the ramp does not by itself activate the urgent search entitlement. +The ramp and the latch use different thresholds. Entering the ramp does not by itself latch urgency. ## Terminal state and restart From 707f069ff860fa5ba6b9745a2315a1e9459bba04 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Thu, 29 Oct 2026 12:20:30 +0100 Subject: [PATCH 02/37] Ignore AI-scratch directories --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 61e98e96a3..543983a2d6 100644 --- a/.gitignore +++ b/.gitignore @@ -56,6 +56,7 @@ CLAUDE.md # AI review/agent tooling scratch state .sphinx/ +/.scratch/ .skill-builder-temp/ .claude/scheduled_tasks.lock @@ -64,3 +65,4 @@ CLAUDE.md # AI investigation scratch state (never committed) .investigations/ +/notes/ From 1c3ee2a06f1f6ef2807e2d2d6533869f9394117e Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Thu, 29 Oct 2026 12:41:13 +0100 Subject: [PATCH 03/37] Update Java reference-chain tests for candidate-only admission AggressiveLeakReferenceChainTest asserted that the BFS thread runs passes from the urgent-OOM projection alone with zero per-klass candidates - the behavior the gate fix removed. It now asserts the opposite: with the leak signal armed and the projection deep in the urgent band, the gate stays closed (live-thread idle window plus a race-free direct shouldRunPass() evaluation - the removed one-shot entitlement was what made the direct call order-dependent). ReferenceChainTrackingTest.shouldReportAbandonedSearchOnTinyFrontierCap qualifies its candidate only through the debug-only population seams; in a release build no candidate exists and the search now (correctly) stays idle, so the method assumes a debug build. The profiler-command comment still described the removed hasLeakSignal() short-circuit; updated. --- .../AggressiveLeakReferenceChainTest.java | 103 ++++++++++-------- .../ReferenceChainTrackingTest.java | 15 ++- 2 files changed, 70 insertions(+), 48 deletions(-) diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java index 187a715777..31d07fd94e 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java @@ -15,13 +15,19 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue; /** - * E2E coverage for {@code LivenessTracker::secondsToOOM()}'s urgent-OOM bypass + * E2E coverage for the search-restart gate the reference-chain collector's pass loop polls * ({@code ReferenceChainTracker::hasLeakSignal()}, referenceChains.cpp). Complements * {@link ReferenceChainTrackingTest}'s organic coverage of a slow leak - a gradually-growing * population that clears {@code selectLeakCandidates()}'s per-klass hysteresis gate over many - * rounds - with the opposite, previously-unexercised case: a heap-wide leak growing fast enough - * that a real deployment would OOM before any single klass's own population history ever clears - * that same hysteresis gate. + * rounds - with the aggregate heap-floor case: a heap-wide growth fast enough that a real + * deployment would OOM before any single klass's own population history ever clears that same + * hysteresis gate. + * + *

This class is a regression guard for the candidate-gate contract: the aggregate urgent-OOM + * projection alone must NEVER open the gate while the per-klass population table is provably + * empty. Staging recordings showed the pre-fix behavior - the urgent latch entitled a + * candidate-less search that cannot exit early and ran whole-heap BFS passes to the auto-tuned + * TTL with zero chains to show for it. * *

Reproducing a genuine near-OOM heap in this shared, no-{@code forkEvery} test JVM is * impractical (see {@link ReferenceChainTrackingTest}'s own header comment on the JVM this class @@ -31,20 +37,20 @@ * directly, so {@code secondsToOOM()}'s projection can be driven under * {@code OOM_URGENT_THRESHOLD_S} (referenceChains.h) deterministically, decoupled from this JVM's * real {@code -Xmx} and from real GC timing. {@code shouldRunPassForTest0()} then evaluates - * {@code ReferenceChainTracker::shouldRunPass()} - the search-restart gate {@code hasLeakSignal()} - * feeds - directly, rather than running a real pass, so this class never depends on + * {@code ReferenceChainTracker::shouldRunPass()} - the gate {@code hasLeakSignal()} feeds - + * directly, rather than running a real pass, so this class never depends on * {@code selectLeakCandidates()} ranking anything, a real JVMTI heap walk, or a JFR event. */ public class AggressiveLeakReferenceChainTest extends AbstractProfilerTest { @Override protected String getProfilerCommand() { - // generations=true: gates LivenessTracker::gcGenerationsEnabled() - hasLeakSignal() (which - // shouldRunPassForTest0() ultimately reads) short-circuits to true immediately when this is - // unset (its own first check, referenceChains.cpp), which would make every test below - // trivially pass regardless of the urgent-OOM bypass this class exists to exercise. - // referencechains=true: constructs the FrontierTable/ReferenceChainTracker singleton at all - - // shouldRunPassForTest0() has nothing to read otherwise. + // generations=true: arms LivenessTracker's effective leak signal (generations OR liveheap + // tracking drive the same population mechanisms) - without it hasLeakSignal() returns false + // trivially, which would make every test below pass regardless of the candidate-gate + // contract this class exists to exercise. referencechains=true: constructs the + // FrontierTable/ReferenceChainTracker singleton at all - shouldRunPassForTest0() has + // nothing to read otherwise. return "generations=true,referencechains=true:hops=32:budget=500:ttl=60000:framecap=2000000"; } @@ -67,10 +73,9 @@ private static void assumeDebugBuild() { * (head/fill - {@code klassPopulationResetForTest()}, livenessTracker.h), so * {@code secondsToOOM()} reports "insufficient fill" until real GC samples build up again; it * runs while heap-floor recording is still disabled so no real sample can interleave. - * {@code resetReferenceChainSearchForTest0()} then clears any urgency latch and spent one-shot - * entitlement ({@code _urgent_latched}/{@code _urgent_search_spent}) plus the search state the - * urgent bypass started - after the ring reset, so the BFS thread cannot re-latch from the - * stale projection. Recording is re-enabled last. + * {@code resetReferenceChainSearchForTest0()} then clears any urgency latch plus the search + * state an earlier evaluation started - after the ring reset, so the BFS thread cannot + * re-latch from the stale projection. Recording is re-enabled last. */ private static void restoreHeapFloorDefaults() { ReferenceChainTestHooks.setMaxHeapBytesForTest0(-1); @@ -83,30 +88,25 @@ private static void restoreHeapFloorDefaults() { * Seeds ten heap-floor-ring samples rising fast enough that {@code secondsToOOM()}'s projection * lands well under {@code OOM_URGENT_THRESHOLD_S} (300s), with LivenessTracker's per-klass * population table left empty throughout - {@code selectLeakCandidateKlassIds0()} returns - * nothing at any point in this test. Then waits for the BFS thread to actually run passes, - * proving the search-restart gate opened from the urgent-OOM projection alone. + * nothing at any point in this test. Then asserts the search-restart gate stays closed on the + * live BFS thread: the aggregate urgent-OOM projection must never start a candidate-less + * search. * - *

The gate is asserted through the live BFS thread rather than by calling - * {@code shouldRunPassForTest0()} directly: opening the gate spends the episode's one-shot - * urgency entitlement ({@code _urgent_search_spent}) on the FIRST evaluation, and the BFS - * thread - whose cadence ramps down to ~10ms once urgency latches - races the test's own - * call for that first evaluation. A direct call therefore only returns true when it wins - * that race, which made this test flaky by construction. A pass only ever runs after - * {@code shouldRunPass()} returned true, and with {@code generations=true} plus a provably - * empty per-klass population table the urgent-OOM projection is the only possible trigger - - * so "passes ran while zero candidates existed" is the same assertion, without the race. + *

The gate is asserted both through the live BFS thread (no pass may run within the wait + * window) and through a direct {@code shouldRunPassForTest0()} call after a search reset. The + * direct call is race-free under the candidate-gate contract - the old one-shot urgency + * entitlement ({@code _urgent_search_spent}) that made a direct call lose a race against the + * BFS thread was removed together with the bypass itself, and the gate's answer no longer + * depends on evaluation order. */ @Test - public void shouldOpenSearchGateOnAggressiveHeapWideGrowthWithNoLeakCandidate() + public void shouldNotOpenSearchGateOnAggressiveHeapWideGrowthWithNoLeakCandidate() throws InterruptedException { assumeDebugBuild(); ReferenceChainTestHooks.setHeapFloorRecordingForTest0(false); ReferenceChainTestHooks.resetKlassPopulationForTest0(); ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); try { - // Baseline BEFORE publishing any urgency input: the live BFS thread may run the one - // urgency-authorized search as soon as the samples below land, and that pass must count. - int passesBefore = ReferenceChainTestHooks.referenceChainPassesRunForTest0(); // 1000 MiB fake max heap, rising from 100 MiB to 991 MiB over 9 (fake) seconds: ~99 MiB/s, // 9 MiB of headroom left at the last sample -> projected time-to-OOM ~= 9/99 s, far under // OOM_URGENT_THRESHOLD_S = 300s. @@ -116,27 +116,40 @@ public void shouldOpenSearchGateOnAggressiveHeapWideGrowthWithNoLeakCandidate() long timestampNs = i * 1_000_000_000L; ReferenceChainTestHooks.heapFloorRecordForTest0(usedBytes, timestampNs); } + assertTrue(ReferenceChainTestHooks.heapFloorRisingForTest0(), + "Precondition: the seeded samples must read as a sustained rise, so the projection " + + "below is a genuine urgent-band one - the gate must stay closed for the " + + "candidate requirement, not because the projection is unknown"); int[] candidates = ReferenceChainTestHooks.selectLeakCandidateKlassIds0(); assertTrue(candidates == null || candidates.length == 0, - "This test's own precondition: no per-klass candidate should exist, so passes running " - + "below can only come from the aggregate urgent-OOM bypass"); - - // Idle BFS cadence is ~1s/pass; urgency ramps it to ~10ms after the latch. 20s covers the - // first evaluation landing up to one idle cadence after the seeding loop. - long deadline = System.currentTimeMillis() + 20_000; - while (ReferenceChainTestHooks.referenceChainPassesRunForTest0() == passesBefore - && System.currentTimeMillis() < deadline) { + "This test's own precondition: no per-klass candidate should exist, so a pass running " + + "below could only come from the aggregate urgent-OOM projection"); + + // Idle BFS cadence is ~1s/pass. No pass may run: with zero per-klass candidates the gate + // has no indication, and urgency alone must not supply one. + int passesBefore = ReferenceChainTestHooks.referenceChainPassesRunForTest0(); + long deadline = System.currentTimeMillis() + 5_000; + while (System.currentTimeMillis() < deadline) { + assertTrue(ReferenceChainTestHooks.referenceChainPassesRunForTest0() == passesBefore, + "The BFS thread must stay idle: an urgent-OOM projection with zero per-klass leak " + + "candidates must not start a search"); Thread.sleep(50); } - assertTrue(ReferenceChainTestHooks.referenceChainPassesRunForTest0() > passesBefore, - "Expected the BFS thread to run passes from the urgent heap-wide OOM projection alone, " - + "with zero per-klass leak candidate"); + + // Same assertion through the predicate itself, on a freshly reset search so any mutation + // the live thread's evaluations made lands on clean state (see the flat-floor control + // below for why the reset must not touch the population). This direct call is race-free: + // the gate's answer no longer depends on evaluation order. + ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); + assertFalse(ReferenceChainTestHooks.shouldRunPassForTest0(), + "Expected the search-restart gate to stay closed: the urgent-OOM projection is not a " + + "leak indication - only selectLeakCandidates() is"); candidates = ReferenceChainTestHooks.selectLeakCandidateKlassIds0(); assertTrue(candidates == null || candidates.length == 0, - "The passes that just ran must still have had zero per-klass candidates - the " - + "aggregate urgent-OOM projection was the only possible trigger"); + "The idle window must have kept the per-klass population table empty - the " + + "aggregate urgent-OOM projection was the only signal this test seeded"); } finally { restoreHeapFloorDefaults(); } @@ -166,7 +179,7 @@ public void shouldNotOpenSearchGateOnFlatHeapFloorWithNoLeakCandidate() { // shouldRunPassForTest0() is NOT a pure predicate (see JavaProfiler's own javadoc): under the // native engine lock it may account a finished search's pain, restart terminal search state, - // or spend the urgent one-shot entitlement. Reset the search right before it so any mutation + // or advance urgency bookkeeping. Reset the search right before it so any mutation // lands on a fresh tracker (_search_started == false) instead of whatever search an earlier // class in this shared JVM left behind. Only the search is reset here, not the population: // resetKlassPopulationForTest0() also zeroes the heap-floor ring seeded above, which would diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java index 28de9d6789..e4f3ce717c 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java @@ -31,6 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; /** * PROF-15341 (+ lifecycle-wiring follow-up): end-to-end coverage for @@ -47,7 +48,8 @@ *

Target-selection bridging: {@code pollWatchedTargets()} (referenceChains.cpp), called * from {@code threadLoop()} once per scheduling cycle after {@code runPass()}, polls * {@code LivenessTracker::selectLeakCandidates()} (positive population-slope ranking over a - * rolling per-klass survivor-count window, gated on {@code _gc_generations}) and, for each ranked + * rolling per-klass survivor-count window, gated on the effective leak signal - generations OR + * liveheap tracking) and, for each ranked * klass whose representative instance an ordinary {@code runPass()} walk has *already* tagged * ({@code getTag() > 0} - a read, never a {@code SetTag} seed), reconstructs and emits its chain * via {@code Profiler::writeReferenceChain()}. {@link #shouldReconstructReferrerChainToGcRoot()} @@ -194,8 +196,11 @@ protected String getProfilerCommand() { if ("shouldReportAbandonedSearchOnTinyFrontierCap".equals(testName)) { // memory=64:l:1.0 is load-bearing: Profiler::start() only starts the ReferenceChainTracker // when allocation sampling activated (activated & EM_ALLOC); a bare referencechains=... falls - // back to cpu-only and never runs the search. No generations=true: LivenessTracker::initialize() - // then clears _gc_generations, so hasLeakSignal() returns true without any population trend. + // back to cpu-only and never runs the search. No generations=true: the effective leak signal + // still arms from liveheap tracking alone, but hasLeakSignal() requires a per-klass + // candidate on top of it - which this test seeds through the debug-only seams (see the + // method's own javadoc). Release builds cannot seed a candidate deterministically, so the + // method assumes a debug build. return "memory=64:l:1.0,referencechains=true:hops=32:budget=500:framecap=1:ttl=100:painbudget=100"; } // shouldExposeReferenceChainsSettingWhenEnabled: a referencechains-only command. Arguments::parse() @@ -702,6 +707,10 @@ public void shouldReconstructReferrerChainThroughUnboundedCacheLeak() throws Exc @Test @Order(3) public void shouldReportAbandonedSearchOnTinyFrontierCap() throws Exception { + assumeTrue(ReferenceChainAssertions.isDebugInstrumentedBuild(), + "The qualifying-candidate seeding below rides the debug-only population seams; in a " + + "release build the search stays idle without a candidate (the candidate-gate " + + "contract), so no abandonment event could ever be produced"); if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { // See the javadoc above for why the seeding is needed despite no generations=true of this // method's own, and why both a gc() and a throwaway dump() must precede it. From 24e0ed4f518232eac08ef3c7efbe734d773eaa37 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Thu, 29 Oct 2026 14:14:01 +0100 Subject: [PATCH 04/37] Gate urgent liveness admission on fresh heap growth evidence The OOM ramp's 100% tracking admission followed the raw projection-band comparison, but the smoothed 30-sample regression keeps the projection inside the band for several recovery GCs after the heap has physically stopped growing. Outside the emergency band (<5min) the boost now requires the newest heap-floor sample to exceed the value observed at episode entry; a recovered spike never satisfies that. Emergency-band admission stays immediate, and the ramp's budget/cadence effects are unchanged. Also corrects two stale claims about the urgency latch: the ramp calculation never consults it (its only consumer is no-progress abandonment suppression), and the budget boost follows the raw band, not the latch. --- ddprof-lib/src/main/cpp/livenessTracker.cpp | 15 +++ ddprof-lib/src/main/cpp/livenessTracker.h | 15 ++- ddprof-lib/src/main/cpp/referenceChains.cpp | 53 +++++++-- ddprof-lib/src/main/cpp/referenceChains.h | 25 ++++ .../src/test/cpp/referenceChainsOomTests.inc | 111 ++++++++++++++++++ .../test/cpp/referenceChainsTestAccessors.h | 7 ++ .../ReferenceChainsSignalsExplained.md | 6 +- 7 files changed, 219 insertions(+), 13 deletions(-) diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index 1b12ee7bb7..58044e8d71 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -1477,6 +1477,21 @@ void LivenessTracker::recordTidTrendSamplesLocked( } } +u64 LivenessTracker::newestHeapFloorSample() const { + // Matches recordHeapFloorSampleUnchecked()'s storeRelease() on both index + // fields - loadAcquire() here is what makes the payload read below visible. + u8 fill = loadAcquire(_heap_floor_ring_fill); + u8 head = loadAcquire(_heap_floor_ring_head); + if (fill == 0) { + return 0; + } + // head points one past the newest slot (recordHeapFloorSampleUnchecked() + // stores then advances) - step back one, wrapping over the ring size. + u8 newest = (u8)((head + KLASS_POPULATION_RING_SIZE - 1) % + KLASS_POPULATION_RING_SIZE); + return load(_heap_floor_ring[newest]); +} + void LivenessTracker::recordHeapFloorSample(u64 used, u64 timestamp_ns, u64 container_used) { TEST_LOG("LivenessTracker::recordHeapFloorSample called used=%llu disabled=%d", (unsigned long long)used, diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index 6185348355..15c5663667 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -862,6 +862,14 @@ class alignas(alignof(SpinLock)) LivenessTracker { // and secondsToOOM(). See _max_heap_bytes's own comment above. jlong maxHeapBytes() const { return _max_heap_bytes; } + // Reads the used-bytes value of the newest _heap_floor_ring sample (0 when + // the ring is empty). Matches recordHeapFloorSampleUnchecked()'s SPSC + // publish order - loadAcquire() on the index fields is what makes the + // payload store visible. Consumed by ReferenceChainTracker's urgent-admission + // gate as the freshest physical growth evidence; it must observe the same + // samples the projection does, hence the same acquire pairing. + u64 newestHeapFloorSample() const; + constexpr static int MAX_LEAK_CANDIDATES = 5; // Delete copy constructor and assignment operator to prevent copies LivenessTracker(const LivenessTracker&) = delete; @@ -943,8 +951,11 @@ class alignas(alignof(SpinLock)) LivenessTracker { void noteSelectedCandidates(const KlassCandidate *candidates, int count); // Urgency raise toggle - see admitForTracking()'s comment above. Set every - // threadLoop() iteration with that iteration's urgency computation, so the - // boost tracks the ramp exactly (both engaging and releasing). + // threadLoop() iteration with that iteration's urgency computation, except + // that outside the emergency band (< OOM_URGENT_THRESHOLD_S) the boost + // additionally requires fresh heap growth evidence - see + // ReferenceChainTracker::updateUrgentAdmission() for why the raw ramp + // comparison alone is not safe to admit on. void setUrgentTracking(bool urgent); void flush(std::set &tracked_thread_ids); diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index 4d4ea42edb..9d120a45af 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -573,12 +573,16 @@ void ReferenceChainTracker::threadLoop() { DEBUG_ONLY(rcDebugLevelRefresh()); // Hand this iteration's ramp state to shouldRunPass() before it decides - the canary-backoff - // gate is bypassed while the OOM urgency ramp is active (see _oom_ramp_active's own comment) - - // and raise LivenessTracker's tracking admission to 100% for the same ramp (see - // setUrgentTracking()'s own comment, livenessTracker.h): same state, same iteration, so the - // boost tracks the ramp exactly, engaging and releasing together. + // gate is bypassed while the OOM urgency ramp is active (see _oom_ramp_active's own comment). + // The 100% tracking admission (setUrgentTracking(), livenessTracker.h) follows the same ramp + // state, but outside the emergency band it is additionally gated on fresh heap growth evidence + // (updateUrgentAdmission()/the _admission_growth_baseline constraint): the smoothed projection + // can stay inside the band for many GCs after the heap has physically stopped growing, and + // boosting admission on a recovered spike only buys table churn. The budget/cadence ramp above + // stays keyed on the raw band comparison - without a running search (the candidate gate) those + // effects cost nothing, while mis-admission during a transient does. _oom_ramp_active = urgent; - LivenessTracker::instance()->setUrgentTracking(urgent); + LivenessTracker::instance()->setUrgentTracking(updateUrgentAdmission(seconds_to_oom, urgent)); // shouldRunPass()'s restart branch (restartSearch()) clears _pending_expand/_priority_expand // the same way a pass does - take _engine_lock around the call so that mutation is serialized @@ -808,6 +812,37 @@ bool ReferenceChainTracker::isUrgent() const { return false; } +bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool urgent) { + if (!urgent) { + // Left set after an episode so the next episode re-snapshots against that + // episode's own entry state instead of inheriting a stale baseline. + _admission_growth_baseline = 0; + return false; + } + // Emergency band: the projection says minutes at most - capture what the + // last chapter can before the process dies, without waiting for growth + // evidence. OOM_URGENT_THRESHOLD_S is isUrgent()'s own latch threshold, so + // the emergency band and the hysteretic urgent episode cover the same range. + if (seconds_to_oom < OOM_URGENT_THRESHOLD_S) { + return true; + } + u64 newest_used = LivenessTracker::instance()->newestHeapFloorSample(); + if (newest_used == 0) { + // No sample to reason about (the ring cannot be empty while a projection + // exists - secondsToOOM() requires MIN_FILL samples - so this is purely + // defensive). Hold admission rather than admit on nothing. + return false; + } + if (_admission_growth_baseline == 0) { + // First observation of the episode: establish the entry baseline. Admit + // only once a newer sample has grown past it - one GC away for a real + // leak, never for a heap that has stopped growing. + _admission_growth_baseline = newest_used; + return false; + } + return newest_used > _admission_growth_baseline; +} + bool ReferenceChainTracker::hasLeakSignal() { if (!LivenessTracker::instance()->leakSignalEnabled()) { // No population-trend signal can be constructed - generations tracking @@ -823,9 +858,11 @@ bool ReferenceChainTracker::hasLeakSignal() { } double seconds_to_oom = LivenessTracker::instance()->secondsToOOM(); // isUrgent() is called unconditionally, not short-circuited behind any - // branch: it maintains the latch/release state (see _urgent_latched), which - // still drives the urgency ramp and the no-progress-abandonment suppression - // once a search runs. + // branch: it maintains the latch/release state (see _urgent_latched), whose + // only consumer is the no-progress-abandonment suppression once a search + // runs (runPass()'s terminal gate). The OOM ramp's budget/cadence/admission + // effects in threadLoop() follow the raw band comparison instead - they do + // not wait for this latch. bool urgent = isUrgent(); KlassCandidate probe[1]; int n = LivenessTracker::instance()->selectLeakCandidates(probe, 1); diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index 5b3615c993..f09ef2fd34 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -344,6 +344,19 @@ class ReferenceChainTracker { // below for how it is read. volatile u8 _abandon_reason; + // CONSTRAINT (urgent-admission growth gate): the heap-floor projection is a + // least-squares regression over a 30-sample window, so a single spike sample + // keeps the projection inside the ramp band for ~6-7 recovery GCs AFTER the + // heap has physically stopped growing - band residence alone does not + // establish growth. updateUrgentAdmission() therefore snapshots the newest + // heap-floor sample's used bytes when a non-emergency ramp episode begins + // and admits the 100% tracking boost only while a newer sample has grown + // past that snapshot; a recovered spike never does. Written and read only + // from threadLoop() on the BFS thread - no atomics. 0 = no baseline for the + // current episode (a used value of 0 cannot be a real sample: onGC() only + // records used > 0). + u64 _admission_growth_baseline; + // Wall-clock timestamp (OS::nanotime()) of the search's first pass - baseline for the TTL cutoff // above. volatile u64 _search_start_ns; @@ -739,6 +752,7 @@ class ReferenceChainTracker { _tags_released(true), _urgent_latched(false), _urgent_release_ticks(0), _urgency_budget_boosted(false), _configured_budget(0), + _admission_growth_baseline(0), _search_state(SearchState::RUNNING), _abandon_reason(SearchAbandonReason::NONE), _search_start_ns(0), _last_pass_gc_finish_epoch(0), _last_pass_ns(0), @@ -775,6 +789,14 @@ class ReferenceChainTracker { // see _urgent_latched for the latch/release rules and why the raw comparison flaps. bool isUrgent() const; + // Computes the 100% tracking-admission decision for one threadLoop() + // iteration and maintains _admission_growth_baseline. Raw `urgent` still + // drives the budget/cadence ramp; admission alone is additionally gated on + // heap growth evidence outside the emergency band - see + // _admission_growth_baseline's comment for why band residence is not + // sufficient, and OOM_URGENT_THRESHOLD_S for the emergency split. + bool updateUrgentAdmission(double seconds_to_oom, bool urgent); + // Search restart gate: true once _safepoint_pain_budget has // drained back to zero (canStartNow()) *and* hasLeakSignal() above reports at least one leak // candidate. @@ -1152,6 +1174,9 @@ class ReferenceChainTracker { void resetUrgencyForTest() { _urgent_latched = false; _urgent_release_ticks = 0; + // updateUrgentAdmission()'s episode baseline - a stale one across tests + // would make an episode's "first observation" branch unreachable. + _admission_growth_baseline = 0; } // Applied vs configured CPU pain-budget refill rate: shouldRunPass() raises // the applied rate CANARY_PAIN_BUDGET_REFILL_MULTIPLIER-fold only while a diff --git a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc index 2b90c80739..fd2ddebde5 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc @@ -259,6 +259,117 @@ TEST_F(OomUrgencyTest, BurnInProjectionCountsTowardRelease) { } // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- +// Urgent-admission growth gate (updateUrgentAdmission()): inside the OOM ramp +// band, the 100% tracking boost additionally requires fresh heap growth +// evidence outside the emergency band (< OOM_URGENT_THRESHOLD_S). The +// projection is a least-squares regression over a 30-sample window, so a +// single spike sample keeps it inside the band for several recovery GCs after +// the heap has physically stopped growing - admitting on band residence alone +// would buy tracker-table churn for a recovered spike. threadLoop() computes +// the projection once per iteration and hands it in, so these scenarios pass +// the projection the same way and drive the ring directly through +// heapFloorRecordForTest(). +// --------------------------------------------------------------------------- + +// A non-emergency in-band projection admits only once the newest heap-floor +// sample has grown past the episode's entry snapshot - one GC later for a +// real leak - and stops admitting as soon as a newer sample falls back below +// it, even while the projection still reads in-band. +TEST_F(OomUrgencyTest, AdmissionBoostRequiresGrowthPastEntryBaseline) { + // Seeds a ramp projecting 1500s: inside the ramp band (1800s), well + // outside the emergency band. The ramp's last sample is the peak. + ASSERT_NEAR(project(1500.0), 1500.0, 1.0); + u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); + + // First in-band observation establishes the baseline; no growth evidence + // relative to it yet, so no boost. + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + // One more sample above the snapshot: real growth -> boost. + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1490.0, true)); + + // A newer sample back below the entry snapshot: growth evidence gone, the + // boost must drop even though the (passed-in) projection still reads deep + // inside the band. + LivenessTracker::instance()->heapFloorRecordForTest(peak - RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1490.0, true)); +} + +// The regression-smoothing counterexample: a single spike sample keeps the +// smoothed projection inside the band for several recovery GCs after the heap +// has physically recovered. The scenario proves both halves: the projection +// genuinely keeps firing (asserted via secondsToOOM()) while the boost stays +// off for every recovery observation, because no sample grows past the +// entry snapshot (the spike's peak). +TEST_F(OomUrgencyTest, RecoveredSpikeStaysUnboostedWhileProjectionLags) { + ASSERT_NEAR(project(700.0), 700.0, 1.0); + u64 peak = MAX_HEAP - (u64)(700.0 * (double)RATE / (double)SAMPLE_SPACING_S); + + // Entry snapshot = the spike's peak. + ASSERT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(700.0, true)); + + // Recovery: each GC lands one RATE below the previous. The recent-half + // corroboration keeps the projection positive through these (the spike + // dominates the newest-15 regression until most of the recent half is + // recovery samples); the lagging projection must not admit any of them. + for (int r = 1; r <= 6; r++) { + LivenessTracker::instance()->heapFloorRecordForTest( + peak - (u64)r * RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + double seconds = LivenessTracker::instance()->secondsToOOM(); + ASSERT_GE(seconds, 0.0) + << "recovery sample " << r + << ": scenario requires the smoothed projection to lag the recovery"; + ASSERT_LT(seconds, 1800.0) + << "recovery sample " << r + << ": scenario requires the projection to stay inside the ramp band"; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(seconds, true)) + << "recovery sample " << r << " must not be admitted on a lagging projection"; + } +} + +// The emergency band (< OOM_URGENT_THRESHOLD_S) waives the growth-evidence +// hold: minutes from death means capturing whatever the sampler offers, +// including the very first observation and a heap whose newest sample is +// below the entry snapshot. +TEST_F(OomUrgencyTest, EmergencyBandAdmitsWithoutGrowthEvidence) { + ASSERT_NEAR(project(299.0), 299.0, 1.0); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(299.0, true)); + + // A declining heap does not flip the boost off inside the band. + u64 newest = MAX_HEAP - (u64)(299.0 * (double)RATE / (double)SAMPLE_SPACING_S); + LivenessTracker::instance()->heapFloorRecordForTest(newest - RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(299.0, true)); +} + +// Leaving the band clears the entry snapshot: the next episode re-snapshots +// against its own entry state instead of inheriting the previous episode's +// baseline (which could admit on no growth at all if the heap is now above +// the old snapshot for unrelated reasons). +TEST_F(OomUrgencyTest, BandExitResetsGrowthBaseline) { + ASSERT_NEAR(project(1500.0), 1500.0, 1.0); + u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); + ASSERT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + ASSERT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + // An out-of-band observation clears the baseline. + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(-1.0, false)); + + // New episode: the first observation re-snapshots (no inherited growth), + // then one growing sample admits again. + ASSERT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + LivenessTracker::instance()->heapFloorRecordForTest(peak + 2 * RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); +} + // The canary-refill half: shouldRunPass() raises the CPU pain budget's // refill rate CANARY_PAIN_BUDGET_REFILL_MULTIPLIER-fold exactly while a // canary chase is open (candidates exist and not all are found), and puts it diff --git a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h index 19c93ad761..8d5d59ef4e 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h +++ b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h @@ -173,6 +173,13 @@ class ReferenceChainsTestAccessor { static void resetUrgencyForTest() { ReferenceChainTracker::instance()->resetUrgencyForTest(); } + // Urgent-admission growth gate: updateUrgentAdmission()'s per-iteration + // decision (referenceChains.cpp) - the admission half of threadLoop()'s + // ramp handling, testable without the BFS thread. + static bool updateUrgentAdmissionForTest(double seconds_to_oom, bool urgent) { + return ReferenceChainTracker::instance()->updateUrgentAdmission(seconds_to_oom, + urgent); + } // Applied vs configured CPU pain-budget refill rate (shouldRunPass()'s // canary-refill raise). static double cpuPainBudgetRefillRateForTest() { diff --git a/doc/architecture/ReferenceChainsSignalsExplained.md b/doc/architecture/ReferenceChainsSignalsExplained.md index 56fac83a52..c5a95f1347 100644 --- a/doc/architecture/ReferenceChainsSignalsExplained.md +++ b/doc/architecture/ReferenceChainsSignalsExplained.md @@ -114,14 +114,14 @@ neither source provides a valid estimate, or that the projection is still in its Two ranges have different purposes: -- At less than 30 minutes, the OOM ramp increases the pause target, shortens the cadence, increases the edge-budget ceiling, and admits all liveness samples. -- At less than 5 minutes, `isUrgent()` latches an urgent episode. The latch does not start searches; it sharpens the ramp's response while a candidate-driven search runs. +- At less than 30 minutes, the OOM ramp increases the pause target, shortens the cadence, and increases the edge-budget ceiling. The 100% liveness-sampling admission follows the same band, but outside the emergency band it additionally requires fresh growth evidence: the newest heap-floor sample must exceed the value observed when the ramp episode began. The projection is a smoothed regression over a 30-sample window, so a single spike sample keeps the projection inside the band for several recovery GCs after the heap has physically stopped growing. Growth evidence is what keeps the admission boost off for such a recovered spike. Inside the emergency band (less than 5 minutes), admission is immediate. +- At less than 5 minutes, `isUrgent()` latches an urgent episode. The latch does not start searches, and the ramp calculation does not consult it. Its only consumer is the no-progress abandonment of a running search, which it suppresses while the latch is held. Both ranges apply only after the burn-in period. For `OOM_PROJECTION_BURN_IN_NS` (5 minutes) after the leak signal first activates in the process, `secondsToOOM()` returns -1, so neither the 30-minute ramp nor the urgent latch fires. A burn-in is needed because warmup growth projects an imminent OOM just as a real leak does. The burn-in depends on how long the leak signal has been active, not on the time span of the heap-floor samples. The sampler records one sample per GC, so near OOM back-to-back GCs compress the sample history to a few seconds. Once the burn-in has passed, that compressed history no longer blocks the latch. -The urgent latch clears after five consecutive checks at 10 minutes or more, or after five consecutive checks with no valid rising-trend estimate. This hysteresis prevents the ramp's sharpened response from flapping when the estimate moves around the threshold. +The urgent latch clears after five consecutive checks at 10 minutes or more, or after five consecutive checks with no valid rising-trend estimate. This hysteresis keeps the urgent episode itself from flapping when the estimate moves around the threshold; the admission boost, which releases on the raw band comparison, does not follow this hysteresis. The ramp and the latch use different thresholds. Entering the ramp does not by itself latch urgency. From 5f1b3d64671f00d576eec8721af1e863d5cad495 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Thu, 29 Oct 2026 17:08:18 +0100 Subject: [PATCH 05/37] Clear admission growth baseline at recording boundary; guard test seams stop() and resetSearchStateForTest() now clear _admission_growth_baseline alongside the urgency latch: a stale baseline would let the next episode's first urgent observation skip updateUrgentAdmission()'s entry re-snapshot and admit against the previous recording's heap level. hasLeakSignal() increments reference_chain_urgent_no_candidate when urgency is latched but no klass explains the growth, so the candidate-only gate's refused evaluations are visible in diagnostics instead of silent. The leak-signal test mutators (setLeakSignalSourcesForTest, setLeakSignalActivatedNsForTest) move behind DEBUG/UNIT_TEST with JNI seams (backdateLeakSignalActivationForTest0 and companions) so no release consumer can flip the gate or backdate the burn-in. Stale comments on OOM_URGENT_THRESHOLD_S, gcGenerationsEnabled() and secondsToOOM() corrected to match the candidate-only gate. --- ddprof-lib/src/main/cpp/counters.h | 6 ++ ddprof-lib/src/main/cpp/javaApi.cpp | 64 +++++++++++++++++++ ddprof-lib/src/main/cpp/livenessTracker.h | 42 ++++++++---- ddprof-lib/src/main/cpp/referenceChains.cpp | 18 +++++- ddprof-lib/src/main/cpp/referenceChains.h | 22 ++++--- .../com/datadoghq/profiler/JavaProfiler.java | 49 +++++++++++--- 6 files changed, 166 insertions(+), 35 deletions(-) diff --git a/ddprof-lib/src/main/cpp/counters.h b/ddprof-lib/src/main/cpp/counters.h index ad58859eb3..c431285097 100644 --- a/ddprof-lib/src/main/cpp/counters.h +++ b/ddprof-lib/src/main/cpp/counters.h @@ -290,6 +290,12 @@ X(REFERENCE_CHAIN_FRONTIER_TABLE_CAPACITY, "reference_chain_frontier_table_capacity") \ X(REFERENCE_CHAIN_CANDIDATE_COUNT, "reference_chain_candidate_count") \ X(REFERENCE_CHAIN_CANDIDATES_FOUND, "reference_chain_candidates_found") \ + /* hasLeakSignal() gate evaluations refused while the OOM urgency latch is \ + * set and selectLeakCandidates() found zero candidates - a heap-wide leak \ + * no single klass explains, so no reference-chain search may start. \ + * Counts evaluations (about 1/s while latched and idle), not urgency \ + * episodes: read it as a rate. */ \ + X(REFERENCE_CHAIN_URGENT_NO_CANDIDATE, "reference_chain_urgent_no_candidate") \ /* admitStaticFieldRoots() per-class non-static quota: non-STATIC_FIELD \ * edges (CONSTANT_POOL, INTERFACE, SUPERCLASS, CLASS_LOADER, ...) that \ * were dropped because the class already hit \ diff --git a/ddprof-lib/src/main/cpp/javaApi.cpp b/ddprof-lib/src/main/cpp/javaApi.cpp index 59ab1ec452..a689a23f30 100644 --- a/ddprof-lib/src/main/cpp/javaApi.cpp +++ b/ddprof-lib/src/main/cpp/javaApi.cpp @@ -1404,6 +1404,48 @@ Java_com_datadoghq_profiler_JavaProfiler_referenceChainPassesRunForTest0( return (jint)ReferenceChainTracker::instance()->passesRun(); } +// Moves the leak-signal burn-in stamp one second past OOM_PROJECTION_BURN_IN_NS so secondsToOOM() +// projects from seeded samples however long this JVM has been profiling. Returns the previous stamp +// (0 = never activated) so setLeakSignalActivatedNsForTest0() can restore it. +extern "C" DLLEXPORT jlong JNICALL +Java_com_datadoghq_profiler_JavaProfiler_backdateLeakSignalActivationForTest0( + JNIEnv *env, jclass unused) { + ProfiledThread::initCurrentThreadSignalSafe(); + LivenessTracker *lt = LivenessTracker::instance(); + u64 previous = lt->leakSignalActivatedNsForTest(); + // One second past the burn-in (modular u64), mirroring the gtest fixtures' backdated stamp. + lt->setLeakSignalActivatedNsForTest(OS::nanotime() - LivenessTracker::OOM_PROJECTION_BURN_IN_NS - + 1000000000ULL); + return (jlong)previous; +} + +// Restores a burn-in stamp saved by backdateLeakSignalActivationForTest0(). +extern "C" DLLEXPORT void JNICALL +Java_com_datadoghq_profiler_JavaProfiler_setLeakSignalActivatedNsForTest0( + JNIEnv *env, jclass unused, jlong activatedNs) { + ProfiledThread::initCurrentThreadSignalSafe(); + LivenessTracker::instance()->setLeakSignalActivatedNsForTest((u64)activatedNs); +} + +// The current LivenessTracker::secondsToOOM() projection; negative means no projection. +extern "C" DLLEXPORT jdouble JNICALL +Java_com_datadoghq_profiler_JavaProfiler_secondsToOOMForTest0(JNIEnv *env, + jclass unused) { + ProfiledThread::initCurrentThreadSignalSafe(); + return (jdouble)LivenessTracker::instance()->secondsToOOM(); +} + +// isUrgent()'s latch. It is mutated under _engine_lock (threadLoop()/shouldRunPassForTest()), so +// read it under the same lock. +extern "C" DLLEXPORT jboolean JNICALL +Java_com_datadoghq_profiler_JavaProfiler_urgentLatchedForTest0(JNIEnv *env, + jclass unused) { + ProfiledThread::initCurrentThreadSignalSafe(); + ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); + MutexLocker engine_guard(tracker->_engine_lock); + return tracker->urgentLatchedForTest() ? JNI_TRUE : JNI_FALSE; +} + #else // !DEBUG // Release builds: no-op stubs so the Java natives still link. @@ -1520,6 +1562,28 @@ Java_com_datadoghq_profiler_JavaProfiler_referenceChainPassesRunForTest0( return 0; } +extern "C" DLLEXPORT jlong JNICALL +Java_com_datadoghq_profiler_JavaProfiler_backdateLeakSignalActivationForTest0( + JNIEnv *env, jclass unused) { + return 0; +} + +extern "C" DLLEXPORT void JNICALL +Java_com_datadoghq_profiler_JavaProfiler_setLeakSignalActivatedNsForTest0( + JNIEnv *env, jclass unused, jlong activatedNs) {} + +extern "C" DLLEXPORT jdouble JNICALL +Java_com_datadoghq_profiler_JavaProfiler_secondsToOOMForTest0(JNIEnv *env, + jclass unused) { + return -1.0; +} + +extern "C" DLLEXPORT jboolean JNICALL +Java_com_datadoghq_profiler_JavaProfiler_urgentLatchedForTest0(JNIEnv *env, + jclass unused) { + return JNI_FALSE; +} + #endif // DEBUG // ---- Test-only reads of the current thread's OTEP record ----------------------------------- diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index 15c5663667..802a05118e 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -1050,10 +1050,13 @@ class alignas(alignof(SpinLock)) LivenessTracker { // pattern selectLeakCandidates()/getLiveTraceIds() already use. jobject resolveCandidateRepresentative(JNIEnv *env, u32 klass_id); - // Exposes the _gc_generations gate (see that member's own comment) so a - // caller outside this class can skip work that only makes sense for the - // explicit generations option. Read-only; this accessor never toggles the - // flag. + // Read-only observation point for the explicit generations option + // (_gc_generations; see that member's own comment), so tests and + // diagnostics can tell it apart from the effective leakSignalEnabled() gate + // (generations OR liveheap). No production code keys on it: since the + // reference-chain signal gates moved to leakSignalEnabled(), the + // population, heap-floor and search gates all read _leak_signal_enabled. + // This accessor never toggles the flag. bool gcGenerationsEnabled() const { return _gc_generations.load(std::memory_order_relaxed); } @@ -1083,16 +1086,15 @@ class alignas(alignof(SpinLock)) LivenessTracker { // under-warn right up until an OOM-kill from container memory pressure // that had nothing to do with heap occupancy. // - // Exists because selectLeakCandidates()'s per-klass gate - // (KLASS_POPULATION_MIN_FILL_FOR_TREND ring samples plus - // LEAK_TREND_HYSTERESIS_BASE/CORROBORATED consecutive qualifying epochs) - // can take longer to trust a candidate than a fast leak has left before - // OOM - this gives ReferenceChainTracker::hasLeakSignal() an independent, - // rate-based signal to start a search immediately instead of waiting on - // that gate. Not a claim that growth stays linear, only that a - // short-horizon linear extrapolation is a reasonable urgency signal. + // The projection feeds the urgency latch (ReferenceChainTracker::isUrgent()), + // the urgent admission ramp (ReferenceChainTracker::updateUrgentAdmission()) + // and threadLoop()'s OOM cadence/budget ramp. It does NOT start a search: + // ReferenceChainTracker::hasLeakSignal() still returns true only when + // selectLeakCandidates() yields a klass, urgency or not. Not a claim that + // growth stays linear, only that a short-horizon linear extrapolation is a + // reasonable urgency signal. // Returns a negative value if the chosen ring is not filled enough yet - // (gcGenerationsEnabled() is off, or too few GC epochs have happened), not + // (leakSignalEnabled() is off, or too few GC epochs have happened), not // rising, or neither _max_heap_bytes nor _container_memory_limit was ever // resolved - callers must treat any non-positive return as "no projection // available", not "zero seconds". Returns 0 if the chosen ring's recent @@ -1701,6 +1703,13 @@ class alignas(alignof(SpinLock)) LivenessTracker { } #endif +#if defined(DEBUG) || defined(UNIT_TEST) + // Test-only mutators of the leak-signal gates and the burn-in stamp. + // Compiled in for DEBUG builds (they back the DEBUG-only JNI seams in + // javaApi.cpp) and for gtest builds (GtestTaskBuilder adds -DUNIT_TEST to + // every gtest compile); never in the shipped release library, so no release + // consumer can flip the leak-signal gate or backdate the burn-in. + // Sets the two leak-signal gates directly, bypassing initialize() (which // requires a live JVM - VM::hotspot_version()/VM::jni(), see that method's // own code - out of gtest's reach the same way foldKlassCountsLocked()'s @@ -1729,6 +1738,13 @@ class alignas(alignof(SpinLock)) LivenessTracker { _leak_signal_activated_ns.store(ns, std::memory_order_release); } + // Read side of setLeakSignalActivatedNsForTest(): lets a DEBUG seam save the + // current stamp before backdating it and restore it afterwards. + u64 leakSignalActivatedNsForTest() const { + return _leak_signal_activated_ns.load(std::memory_order_acquire); + } +#endif // DEBUG || UNIT_TEST + private: void getLiveTraceIds(CallTraceIdSet& out_buffer); }; diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index 9d120a45af..6286df5e7d 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -388,8 +388,10 @@ void ReferenceChainTracker::stop() { // - _search_start_ns still holds the previous recording's clock, so the entire stopped gap // would count toward the new recording's TTL and could abandon "its" search on the first // pass with a spurious abandonment event for a search the new recording never started; - // - _urgent_latched/_urgent_release_ticks would carry the previous - // recording's urgency episode into the new one; + // - _urgent_latched/_urgent_release_ticks/_admission_growth_baseline would carry the previous + // recording's urgency episode into the new one (a stale growth baseline would let the new + // recording's first urgent observation skip updateUrgentAdmission()'s entry re-snapshot and + // admit against the old recording's heap level); // - the frontier's referrer_klass slots hold StringDictionary ids from a _class_map that // Profiler::start() is about to wipe (ids restart at 1) - they cannot be remapped, so a // chain built from a surviving entry after the boundary would name whichever class the new @@ -411,6 +413,8 @@ void ReferenceChainTracker::stop() { store(_search_start_ns, (u64)0); _urgent_latched = false; _urgent_release_ticks = 0; + // BFS-thread owned like the two fields above; stopThread() has joined that thread. + _admission_growth_baseline = 0; // Same per-search candidate/discovered canary state runPass()'s terminal branch and // restartSearch() clear - zeroing the counts here closes the stale-chain windows in // pollWatchedTargets() (dead-representative canary path, discovered-instances path) for the @@ -879,6 +883,12 @@ bool ReferenceChainTracker::hasLeakSignal() { TEST_LOG_SUMMARY("ReferenceChainTracker::hasLeakSignal -> %s (secondsToOOM=%.1f, " "candidates=%d, urgent=%d)", n > 0 ? "true" : "false", seconds_to_oom, n, urgent); + if (urgent && n == 0) { + // Observability for the candidate-only gate (TEST_LOG_SUMMARY above is debug/test-only): + // urgency is latched but no klass explains the growth, so no search starts and no chain is + // produced. One increment per refused evaluation (see counters.h). + Counters::increment(REFERENCE_CHAIN_URGENT_NO_CANDIDATE); + } return n > 0; } @@ -994,9 +1004,11 @@ void ReferenceChainTracker::resetSearchStateForTest(jvmtiEnv *jvmti, // _safepoint_pain_budget above. _cpu_pain_budget = PainBudget(_pain_budget_refill_rate); // Same test-isolation rationale: a latched urgency episode left behind by an earlier test would - // otherwise ramp this one's cadence and pause target from a stale projection. + // otherwise ramp this one's cadence and pause target from a stale projection, and a stale + // admission growth baseline would skip this test's first-observation re-snapshot. _urgent_latched = false; _urgent_release_ticks = 0; + _admission_growth_baseline = 0; if (_frontier != nullptr) { // Rebuilds the table at this test's own _configured_frontier_cap, undoing any smaller framecap= diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index f09ef2fd34..b3a00819a9 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -635,11 +635,15 @@ class ReferenceChainTracker { // Fallback cadence between passes. static constexpr u64 PASS_CADENCE_NS = 1000000000ULL; // 1s - // Heap-wide time-to-OOM urgency threshold (LivenessTracker::secondsToOOM()) - hasLeakSignal() - // below forces a search to start immediately once the projection drops under this, rather than - // waiting for a klass to clear selectLeakCandidates()'s own per-klass ring-fill/hysteresis gate - // (KLASS_POPULATION_MIN_FILL_FOR_TREND plus LEAK_TREND_HYSTERESIS_BASE/ CORROBORATED epochs, - // livenessTracker.h). + // Heap-wide time-to-OOM urgency threshold (LivenessTracker::secondsToOOM()). It has two uses: + // (a) isUrgent()'s latch threshold (released via OOM_URGENT_RELEASE_S below); the latch is + // consumed by runPass()'s no-progress abandonment branch, which it suppresses while latched; + // (b) updateUrgentAdmission()'s emergency band, where tracking admission goes to 100% without + // heap-growth evidence. + // It never authorizes starting a search: hasLeakSignal() still requires selectLeakCandidates() + // to return at least one klass past its per-klass ring-fill/hysteresis gate + // (KLASS_POPULATION_MIN_FILL_FOR_TREND plus LEAK_TREND_HYSTERESIS_BASE/CORROBORATED epochs, + // livenessTracker.h), and urgency never stands in for that. static constexpr double OOM_URGENT_THRESHOLD_S = 300.0; // 5 minutes // Release side of OOM_URGENT_THRESHOLD_S's hysteresis (see _urgent_latched). @@ -1416,10 +1420,10 @@ class ReferenceChainTracker { // Test seam - not part of the production API. Evaluates the real private scheduling gate without // executing a BFS pass. This is not read-only: while holding _engine_lock like threadLoop(), the - // gate may account a completed search's pain and restart/reset terminal search state. It also - // exposes whether LivenessTracker::secondsToOOM()'s urgent-OOM bypass opens the gate even with no - // per-klass candidate; runReferenceChainPass0() cannot show that because it calls runPass() - // directly and never consults the gate. + // gate may account a completed search's pain and restart/reset terminal search state. It lets a + // test confirm the gate stays closed with no per-klass candidate, urgency latched or not; + // runReferenceChainPass0() cannot show that because it calls runPass() directly and never + // consults the gate. bool shouldRunPassForTest(u64 now_ns) { MutexLocker engine_guard(_engine_lock); return shouldRunPass(now_ns); diff --git a/ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java b/ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java index aae738a477..1f91d50f0c 100644 --- a/ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java +++ b/ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java @@ -731,8 +731,9 @@ private static native void setTraceContext0(long localRootSpanId, long spanId, l /** * Test seam (debug native builds only): reports whether the seeded heap-floor ring * currently shows a sustained rise ({@code heapFloorRising()}) - the aggregate growth - * read {@code hasLeakSignal()}'s gate uses alongside {@code secondsToOOM()}'s urgent - * projection. Returns {@code false} both before any seeding and after + * read {@code selectLeakCandidates()} uses to pick the required per-klass hysteresis + * (corroborated vs base); neither {@code secondsToOOM()} nor {@code hasLeakSignal()} reads + * it directly. Returns {@code false} both before any seeding and after * {@link #heapFloorResetForTest0()} emptied the ring, which is how tests assert the * reset actually cleared the indices. */ @@ -741,14 +742,13 @@ private static native void setTraceContext0(long localRootSpanId, long spanId, l /** * Test seam (debug native builds only): reports whether ReferenceChainTracker's search-restart * gate ({@code canAffordNewSearch()} -> {@code hasLeakSignal()}) would currently allow a - * fresh/terminal search to start - in particular, whether {@code secondsToOOM()}'s urgent-OOM - * bypass opens this gate even with zero per-klass leak candidate (confirmable in the same test - * via {@link #selectLeakCandidateKlassIds0()}). Unlike {@link #runReferenceChainPass0()}, which - * calls {@code runPass()} unconditionally, this evaluates the real gate without executing a - * BFS pass - but it is NOT a read-only query: under the native engine lock it may account a - * completed search's pain and restart/reset terminal search state, and opening the gate on - * the urgent-OOM bypass spends the episode's one-shot urgency entitlement. Do not treat it - * as a pure predicate (e.g. inside an assertion) when a scenario's search state matters. + * fresh/terminal search to start now. Opening it requires a per-klass leak candidate + * (confirmable in the same test via {@link #selectLeakCandidateKlassIds0()}), whether or not + * OOM urgency is latched. Unlike {@link #runReferenceChainPass0()}, which calls + * {@code runPass()} unconditionally, this evaluates the real gate without executing a BFS + * pass - but it is NOT a read-only query: under the native engine lock it may account a + * completed search's pain and restart/reset terminal search state. Do not treat it as a pure + * predicate (e.g. inside an assertion) when a scenario's search state matters. */ static native boolean shouldRunPassForTest0(); @@ -762,6 +762,35 @@ private static native void setTraceContext0(long localRootSpanId, long spanId, l */ static native int referenceChainPassesRunForTest0(); + /** + * Test seam (debug native builds only): backdates LivenessTracker's leak-signal burn-in stamp + * one second past {@code OOM_PROJECTION_BURN_IN_NS}, so {@code secondsToOOM()} projects from + * seeded heap-floor samples however long this JVM has been profiling. Returns the previous + * stamp, which the caller must hand back to {@link #setLeakSignalActivatedNsForTest0(long)} + * in a finally block. The release stub returns 0 and changes nothing. + */ + static native long backdateLeakSignalActivationForTest0(); + + /** + * Test seam (debug native builds only): restores a leak-signal burn-in stamp saved by + * {@link #backdateLeakSignalActivationForTest0()} (0 means "never activated"). + */ + static native void setLeakSignalActivatedNsForTest0(long activatedNs); + + /** + * Test seam (debug native builds only): the current {@code secondsToOOM()} projection. A + * negative value means "no projection" (burn-in, insufficient ring fill, not rising, or no + * resolved limit). The release stub returns -1. + */ + static native double secondsToOOMForTest0(); + + /** + * Test seam (debug native builds only): ReferenceChainTracker's OOM-urgency latch + * ({@code _urgent_latched}), read under the native engine lock. The release stub returns + * {@code false}. + */ + static native boolean urgentLatchedForTest0(); + // ---- Test-only reads of the current thread's OTEP record ---------------------------------- // Each resolves the current carrier's record directly (like the write primitives above) with // no cached buffer and no per-thread Java object; introspection/test use only. From 0a2fa061c66863a4cb172aba9cfe51c9f64c8843 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Thu, 29 Oct 2026 17:08:34 +0100 Subject: [PATCH 06/37] Pin baseline reset and vacuous-negative guards in reference-chain tests - StopClearsAdmissionGrowthBaseline pins the stop() baseline reset. - AggressiveLeak: the negative search-gate test now asserts the projection is actually engaged (backdated burn-in stamp via the new seam), so its assertions cannot pass vacuously while secondsToOOM() is inside burn-in; cleanup goes through the shared restoreHeapFloorDefaults() helper. - ReferenceChainTrackingTest: the abandonment retry loop is capped at 3 dumps with a passesRun probe and paced last dump, bounding the O(recording-size) dump I/O; the dead if-guard behind the debug-build assumption is removed; a growth-gate equality scenario pins > vs >=. --- .../src/test/cpp/referenceChainsBfsTests.inc | 6 +- .../src/test/cpp/referenceChainsOomTests.inc | 84 ++++++ .../test/cpp/referenceChainsTrackerTests.inc | 33 ++- .../profiler/JavaProfilerApiSurfaceTest.java | 4 +- .../profiler/ReferenceChainTestHooks.java | 23 ++ .../AggressiveLeakReferenceChainTest.java | 277 +++++++++++++----- .../ReferenceChainTrackingTest.java | 94 ++++-- 7 files changed, 406 insertions(+), 115 deletions(-) diff --git a/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc b/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc index 6043216620..d5bf85f16d 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc @@ -802,9 +802,9 @@ TEST_F(ReferenceChainsBfsTest, CanaryStuckLimitDoublesPerRestartAndResetsOnClean ReferenceChainsTestAccessor::canaryStuckPassLimitForTest()); // This test deliberately reaches states the fixtures' SetUp reset() cannot fully neutralize - // for a *latched* tracker: the urgency latch (isUrgent() keeps bypassing the candidate gate - // until it releases on its own, breaking later tests' gate expectations under shuffled - // order), the watched-leak-klass list (restartSearch() does not clear it), and whatever + // for a *latched* tracker: the urgency latch (the latched tracker keeps suppressing + // runPass()'s no-progress abandonment until it releases on its own, breaking later tests' + // terminal-state expectations under shuffled order), the watched-leak-klass list (restartSearch() does not clear it), and whatever // candidate/found state the last runPass() left. Restore all of it - the same seam the // fixtures use in SetUp, applied to the leaked half. LivenessTracker::instance()->setMaxHeapBytesForTest(-1); diff --git a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc index fd2ddebde5..297d7e6b3c 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc @@ -299,6 +299,32 @@ TEST_F(OomUrgencyTest, AdmissionBoostRequiresGrowthPastEntryBaseline) { EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1490.0, true)); } +// Boundary of the growth gate: a newer sample exactly equal to the entry +// snapshot is not growth evidence (a plateaued heap re-recording the same +// floor). Only a strictly larger sample admits, so a `>` -> `>=` slip in +// updateUrgentAdmission() fails here. +TEST_F(OomUrgencyTest, AdmissionBoostRejectsSampleEqualToEntryBaseline) { + ASSERT_NEAR(project(1500.0), 1500.0, 1.0); + u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); + ASSERT_EQ(peak, LivenessTracker::instance()->newestHeapFloorSample()) + << "scenario requires the entry snapshot to be the ramp peak"; + + // Entry snapshot = peak. + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + // Follow-up sample exactly at the snapshot: equality is not growth. + LivenessTracker::instance()->heapFloorRecordForTest(peak, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "a sample equal to the entry baseline is not growth evidence"; + + // Control: one RATE past the snapshot admits, so the negative above is + // not trivially satisfied. + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); +} + // The regression-smoothing counterexample: a single spike sample keeps the // smoothed projection inside the band for several recovery GCs after the heap // has physically recovered. The scenario proves both halves: the projection @@ -451,6 +477,64 @@ TEST_F(ReferenceChainsCanaryRefillTest, RefillRaiseTracksOpenChaseExactly) { ReferenceChainsTestAccessor::reset(); } +// stop() ends the urgency episode at a recording boundary, so it must clear +// updateUrgentAdmission()'s growth baseline along with the latch: the +// heap-floor ring survives the boundary, and a baseline left over from the +// previous recording would let the new recording's first urgent observation +// skip its entry re-snapshot and admit against the old recording's heap level. +// updateUrgentAdmission() reads only the newest heap-floor sample, so no max +// heap is set - and must not be before start(): with a max heap > 0, start()'s +// autoTuneDefaults() calls GetAvailableProcessors, which this stub jvmti lacks. +TEST_F(ReferenceChainsCanaryRefillTest, StopClearsAdmissionGrowthBaseline) { + + constexpr u64 MiB = 1ULL << 20; + LivenessTracker *lt = LivenessTracker::instance(); + ReferenceChainsTestAccessor::reset(); // also resetUrgencyForTest() +#ifdef DEBUG + lt->setHeapFloorRecordingForTest(false); +#endif + lt->heapFloorResetForTest(); + ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); + Arguments args; + ASSERT_FALSE(args.parse("referencechains=true")); + ASSERT_FALSE(tracker->start(args)); + + // Restores the shared singletons even when an ASSERT below returns early. + // stop() on an already-stopped tracker is a no-op (it returns unless + // enabled()). + struct Cleanup { + ReferenceChainTracker *tracker; + LivenessTracker *lt; + ~Cleanup() { + tracker->stop(); + ReferenceChainsTestAccessor::resetUrgencyForTest(); + lt->heapFloorResetForTest(); // while recording is still disabled +#ifdef DEBUG + lt->setHeapFloorRecordingForTest(true); +#endif + ReferenceChainsTestAccessor::reset(); + } + } cleanup{tracker, lt}; + + // Recording 1: the first urgent non-emergency observation snapshots + // 500MiB and does not admit. + lt->heapFloorRecordForTest(500 * MiB, 1, 0); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + tracker->stop(); + ASSERT_FALSE(tracker->start(args)); + + // Recording 2: the heap is now above the OLD baseline. A fresh episode + // must re-snapshot (false); a baseline leaked through stop() would admit + // on 600 > 500. + lt->heapFloorRecordForTest(600 * MiB, 2, 0); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "stop() must clear _admission_growth_baseline"; + // Positive control: growth past the new snapshot admits. + lt->heapFloorRecordForTest(700 * MiB, 3, 0); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); +} + TEST_F(ReferenceChainsCanaryRefillTest, BackoffGateHoldsUnlessOomRampActive) { ReferenceChainsTestAccessor::reset(); ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); diff --git a/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc b/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc index 284ec7c3e4..8980e096de 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc @@ -1195,8 +1195,9 @@ protected: VMTestAccessor::setJvmti(orig_jvmti); LivenessTracker::instance()->klassPopulationResetForTest(); LivenessTracker::instance()->setGcGenerationsForTest(false); - // Set by UrgentOOMProjectionBypassesCandidateGate; reset here so a failed ASSERT_* there - // can't leak a stale max heap into the next test. + // Max heap, leak-signal gates and burn-in stamp are set by individual tests (e.g. + // UrgentLatchWithoutCandidateDoesNotStartSearch); restored here so a failed ASSERT_* in + // one of them can't leak them into the next test. LivenessTracker::instance()->setMaxHeapBytesForTest(-1); // 0 = "never activated"; the next start() re-stamps it through the CAS from 0. LivenessTracker::instance()->setLeakSignalActivatedNsForTest(0); @@ -1210,8 +1211,8 @@ protected: return JVMTI_ERROR_NONE; } - // start() -> autoTuneDefaults() queries this when max heap > 0 - // (UrgentOOMProjectionBypassesCandidateGate). + // start() -> autoTuneDefaults() queries this when max heap > 0 (tests that set a fake max + // heap before start(), e.g. UrgentLatchWithoutCandidateDoesNotStartSearch). static jvmtiError JNICALL mock_GetAvailableProcessors(jvmtiEnv *, jint *nprocs) { *nprocs = 1; @@ -1425,15 +1426,39 @@ TEST_F(SearchRestartTest, UrgentLatchWithoutCandidateDoesNotStartSearch) { // Urgency latched, but no candidate has cleared the leak-signal probe. ASSERT_TRUE(ReferenceChainsTestAccessor::isUrgentForTest()); +#ifdef COUNTERS + long long urgent_no_cand = Counters::getCounter(REFERENCE_CHAIN_URGENT_NO_CANDIDATE); +#endif EXPECT_FALSE(ReferenceChainsTestAccessor::shouldRunPass(1)) << "urgent without a candidate must not start a candidate-less search"; EXPECT_EQ(0, tracker->passesRun()); +#ifdef COUNTERS + // The refusal is observable in production diagnostics, not only in the + // debug-only TEST_LOG_SUMMARY. + EXPECT_EQ(urgent_no_cand + 1, Counters::getCounter(REFERENCE_CHAIN_URGENT_NO_CANDIDATE)) + << "one refused latched/zero-candidate evaluation counts exactly once"; + urgent_no_cand = Counters::getCounter(REFERENCE_CHAIN_URGENT_NO_CANDIDATE); +#endif + + // Disabled leak-signal source: hasLeakSignal() returns before isUrgent() + // and the counter, so nothing is counted. + LivenessTracker::instance()->setGcGenerationsForTest(false); + EXPECT_FALSE(ReferenceChainsTestAccessor::shouldRunPass(1)); +#ifdef COUNTERS + EXPECT_EQ(urgent_no_cand, Counters::getCounter(REFERENCE_CHAIN_URGENT_NO_CANDIDATE)) + << "a disabled leak-signal source must not count"; +#endif + LivenessTracker::instance()->setGcGenerationsForTest(true); // Once a klass clears the probe, the search starts - and urgency applies // through the ramp from that point on. int fake_object_storage = 0; seedGrowingCandidate(/*source_tag=*/1, /*rep=*/(jweak)&fake_object_storage); EXPECT_TRUE(ReferenceChainsTestAccessor::shouldRunPass(2)); +#ifdef COUNTERS + EXPECT_EQ(urgent_no_cand, Counters::getCounter(REFERENCE_CHAIN_URGENT_NO_CANDIDATE)) + << "a gate opened by a real candidate must not count"; +#endif // 0 = "never activated"; the next start() re-stamps it through the CAS from 0. LivenessTracker::instance()->setLeakSignalActivatedNsForTest(0); diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/JavaProfilerApiSurfaceTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/JavaProfilerApiSurfaceTest.java index 6ce81762cc..6dde526f50 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/JavaProfilerApiSurfaceTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/JavaProfilerApiSurfaceTest.java @@ -38,7 +38,9 @@ public void ownedBlockHooksAreNotPublicApiBeforeTaskBlockInstrumentation() throw "getReferenceChainFrontierStateForTest0", "getReferenceChainResolvedTargetTagForTest0", "heapFloorRecordForTest0", "setMaxHeapBytesForTest0", "setHeapFloorRecordingForTest0", "heapFloorResetForTest0", "heapFloorRisingForTest0", - "shouldRunPassForTest0", "referenceChainPassesRunForTest0")); + "shouldRunPassForTest0", "referenceChainPassesRunForTest0", + "backdateLeakSignalActivationForTest0", "setLeakSignalActivatedNsForTest0", + "secondsToOOMForTest0", "urgentLatchedForTest0")); @Test public void referenceChainTestSeamsAreNotPublicApi() { diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/ReferenceChainTestHooks.java b/ddprof-test/src/test/java/com/datadoghq/profiler/ReferenceChainTestHooks.java index fb5d5f427c..285894c7a5 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/ReferenceChainTestHooks.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/ReferenceChainTestHooks.java @@ -117,4 +117,27 @@ public static boolean shouldRunPassForTest0() { public static int referenceChainPassesRunForTest0() { return JavaProfiler.referenceChainPassesRunForTest0(); } + + /** + * Backdates the leak-signal burn-in stamp (debug native builds only; see JavaProfiler). + * Returns the previous stamp for {@link #setLeakSignalActivatedNsForTest0(long)}. + */ + public static long backdateLeakSignalActivationForTest0() { + return JavaProfiler.backdateLeakSignalActivationForTest0(); + } + + /** Restores a saved leak-signal burn-in stamp (debug native builds only; see JavaProfiler). */ + public static void setLeakSignalActivatedNsForTest0(long activatedNs) { + JavaProfiler.setLeakSignalActivatedNsForTest0(activatedNs); + } + + /** The current {@code secondsToOOM()} projection (debug native builds only; see JavaProfiler). */ + public static double secondsToOOMForTest0() { + return JavaProfiler.secondsToOOMForTest0(); + } + + /** The OOM-urgency latch (debug native builds only; see JavaProfiler). */ + public static boolean urgentLatchedForTest0() { + return JavaProfiler.urgentLatchedForTest0(); + } } diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java index 31d07fd94e..352fc36f2c 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java @@ -10,8 +10,14 @@ import com.datadoghq.profiler.ReferenceChainTestHooks; import org.junit.jupiter.api.Test; +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assumptions.assumeFalse; import static org.junit.jupiter.api.Assumptions.assumeTrue; /** @@ -27,7 +33,8 @@ * projection alone must NEVER open the gate while the per-klass population table is provably * empty. Staging recordings showed the pre-fix behavior - the urgent latch entitled a * candidate-less search that cannot exit early and ran whole-heap BFS passes to the auto-tuned - * TTL with zero chains to show for it. + * TTL with zero chains to show for it. That bypass and its one-shot urgency entitlement were + * removed. * *

Reproducing a genuine near-OOM heap in this shared, no-{@code forkEvery} test JVM is * impractical (see {@link ReferenceChainTrackingTest}'s own header comment on the JVM this class @@ -36,13 +43,28 @@ * seed {@code LivenessTracker}'s aggregate heap-floor-ring history and its max-heap-bytes cache * directly, so {@code secondsToOOM()}'s projection can be driven under * {@code OOM_URGENT_THRESHOLD_S} (referenceChains.h) deterministically, decoupled from this JVM's - * real {@code -Xmx} and from real GC timing. {@code shouldRunPassForTest0()} then evaluates - * {@code ReferenceChainTracker::shouldRunPass()} - the gate {@code hasLeakSignal()} feeds - - * directly, rather than running a real pass, so this class never depends on - * {@code selectLeakCandidates()} ranking anything, a real JVMTI heap walk, or a JFR event. + * real {@code -Xmx} and from real GC timing. {@code secondsToOOM()} withholds its projection for + * {@code OOM_PROJECTION_BURN_IN_NS} (5 minutes) after the leak signal first activated, and this + * class may run inside that window, so each gate test backdates the burn-in stamp with + * {@code backdateLeakSignalActivationForTest0()} and restores it in its finally block. Positive + * controls ({@code secondsToOOMForTest0()} for the projection, {@code urgentLatchedForTest0()} for + * the urgency latch) prove the urgency machine is engaged (or provably not, for the flat-floor + * control) before any negative gate assertion is trusted. {@code shouldRunPassForTest0()} then + * evaluates {@code ReferenceChainTracker::shouldRunPass()} - the gate {@code hasLeakSignal()} + * feeds - directly on a freshly reset search, rather than running a real pass or waiting on the + * live BFS thread, so this class never depends on {@code selectLeakCandidates()} ranking + * anything, a real JVMTI heap walk, or a JFR event. */ public class AggressiveLeakReferenceChainTest extends AbstractProfilerTest { + // Mirrors ReferenceChainTracker::OOM_URGENT_THRESHOLD_S (referenceChains.h). + private static final double OOM_URGENT_THRESHOLD_S = 300.0; + + // Sentinel for restoreHeapFloorDefaults(): skip the burn-in stamp restore (the caller never + // backdated it). A real stamp is an OS::nanotime() u64 cast to a jlong, so Long.MIN_VALUE + // cannot collide with one in practice. + private static final long ACTIVATION_STAMP_UNCHANGED = Long.MIN_VALUE; + @Override protected String getProfilerCommand() { // generations=true: arms LivenessTracker's effective leak signal (generations OR liveheap @@ -69,43 +91,102 @@ private static void assumeDebugBuild() { /** * Undoes this class's synthetic seeding so later tests in this shared, no-forkEvery JVM never - * see it. {@code resetKlassPopulationForTest0()} also zeroes LivenessTracker's heap-floor ring - * (head/fill - {@code klassPopulationResetForTest()}, livenessTracker.h), so + * see it. This helper is the only place the cleanup ordering lives; every test's finally block + * ends with it. {@code resetKlassPopulationForTest0()} also zeroes LivenessTracker's heap-floor + * ring (head/fill - {@code klassPopulationResetForTest()}, livenessTracker.h), so * {@code secondsToOOM()} reports "insufficient fill" until real GC samples build up again; it - * runs while heap-floor recording is still disabled so no real sample can interleave. - * {@code resetReferenceChainSearchForTest0()} then clears any urgency latch plus the search - * state an earlier evaluation started - after the ring reset, so the BFS thread cannot - * re-latch from the stale projection. Recording is re-enabled last. + * runs while heap-floor recording is still disabled so no real sample can interleave. The + * burn-in stamp is restored next (unless {@code savedActivationNs} is + * {@link #ACTIVATION_STAMP_UNCHANGED}) - after the ring reset and before the search reset, so + * the restarted BFS thread can neither re-latch from a stale ring nor project against a + * backdated clock. {@code resetReferenceChainSearchForTest0()} then clears any urgency latch + * plus the search state an earlier evaluation started. Recording is re-enabled last. */ - private static void restoreHeapFloorDefaults() { + private static void restoreHeapFloorDefaults(long savedActivationNs) { ReferenceChainTestHooks.setMaxHeapBytesForTest0(-1); ReferenceChainTestHooks.resetKlassPopulationForTest0(); + if (savedActivationNs != ACTIVATION_STAMP_UNCHANGED) { + ReferenceChainTestHooks.setLeakSignalActivatedNsForTest0(savedActivationNs); + } ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); ReferenceChainTestHooks.setHeapFloorRecordingForTest0(true); } + /** Result of one bracketed direct gate evaluation. */ + private static final class GateEvaluation { + boolean gateOpen; + boolean candidateSeen; + int passesAfter; + String detail; + } + + /** + * Evaluates the search gate directly on a freshly reset search. The reset clears the urgency + * latch and the pain budget and leaves {@code _search_started} false, so + * {@code canAffordNewSearch()} reaches {@code hasLeakSignal()}; the gate's answer does not + * depend on evaluation order relative to the BFS thread. The evaluation is bracketed by + * candidate and pass-count reads so organic activity from the rest of this shared JVM can be + * told apart from a contract violation (see {@link #checkNoCandidateFreeActivity}). + */ + private static GateEvaluation evaluateGateAfterSearchReset() { + ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); + int[] candidatesBefore = ReferenceChainTestHooks.selectLeakCandidateKlassIds0(); + int passesBefore = ReferenceChainTestHooks.referenceChainPassesRunForTest0(); + GateEvaluation e = new GateEvaluation(); + e.gateOpen = ReferenceChainTestHooks.shouldRunPassForTest0(); + int[] candidatesAfter = ReferenceChainTestHooks.selectLeakCandidateKlassIds0(); + e.passesAfter = ReferenceChainTestHooks.referenceChainPassesRunForTest0(); + e.candidateSeen = !isEmpty(candidatesBefore) || !isEmpty(candidatesAfter); + e.detail = "candidatesBefore=" + Arrays.toString(candidatesBefore) + " candidatesAfter=" + + Arrays.toString(candidatesAfter) + " passesBefore=" + passesBefore + + " passesAfter=" + e.passesAfter; + return e; + } + + /** + * Aborts only on an observed per-klass candidate: a candidate legitimately opens the gate, so + * the candidate-gate contract cannot be isolated in that run. A BFS pass since the search reset + * (the reset zeroes the pass count) with no candidate in either read means the gate opened + * without a candidate - e.g. a re-introduced urgency bypass on the restarted BFS thread - and + * must FAIL, not skip. + */ + private static void checkNoCandidateFreeActivity(GateEvaluation e) { + assumeFalse(e.candidateSeen, "Aborting: organic per-klass candidate around the direct " + + "evaluation (" + e.detail + ") - the candidate-gate contract cannot be isolated in " + + "this run"); + if (e.passesAfter != 0) { + fail("A BFS pass ran after the search reset although no per-klass candidate was seen in " + + "either read - the gate opened without a candidate (" + e.detail + ")"); + } + } + + private static boolean isEmpty(int[] a) { + return a == null || a.length == 0; + } + /** * Seeds ten heap-floor-ring samples rising fast enough that {@code secondsToOOM()}'s projection * lands well under {@code OOM_URGENT_THRESHOLD_S} (300s), with LivenessTracker's per-klass - * population table left empty throughout - {@code selectLeakCandidateKlassIds0()} returns - * nothing at any point in this test. Then asserts the search-restart gate stays closed on the - * live BFS thread: the aggregate urgent-OOM projection must never start a candidate-less - * search. + * population table left empty throughout. The burn-in stamp is backdated first, so the + * projection is live in this shared JVM regardless of class ordering. Two positive controls + * run before the negative assertion: the projection must be inside the urgent band, and the + * gate evaluation must latch urgency - otherwise a closed gate would prove nothing. Then the + * directly evaluated search gate must stay closed: the aggregate urgent-OOM projection must + * never start a candidate-less search. * - *

The gate is asserted both through the live BFS thread (no pass may run within the wait - * window) and through a direct {@code shouldRunPassForTest0()} call after a search reset. The - * direct call is race-free under the candidate-gate contract - the old one-shot urgency - * entitlement ({@code _urgent_search_spent}) that made a direct call lose a race against the - * BFS thread was removed together with the bypass itself, and the gate's answer no longer - * depends on evaluation order. + *

The live BFS thread is deliberately not watched over a wait window: its pass counter also + * counts passes legitimately started by unrelated per-klass candidates in this shared JVM. The + * run is aborted (not passed) when an organic candidate appears around the direct evaluation, + * and fails when a BFS pass ran since the search reset without any candidate - the gate opened + * without one. */ @Test - public void shouldNotOpenSearchGateOnAggressiveHeapWideGrowthWithNoLeakCandidate() - throws InterruptedException { + public void shouldNotOpenSearchGateOnAggressiveHeapWideGrowthWithNoLeakCandidate() { assumeDebugBuild(); ReferenceChainTestHooks.setHeapFloorRecordingForTest0(false); ReferenceChainTestHooks.resetKlassPopulationForTest0(); ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); + long savedActivationNs = ReferenceChainTestHooks.backdateLeakSignalActivationForTest0(); try { // 1000 MiB fake max heap, rising from 100 MiB to 991 MiB over 9 (fake) seconds: ~99 MiB/s, // 9 MiB of headroom left at the last sample -> projected time-to-OOM ~= 9/99 s, far under @@ -121,45 +202,46 @@ public void shouldNotOpenSearchGateOnAggressiveHeapWideGrowthWithNoLeakCandidate + "below is a genuine urgent-band one - the gate must stay closed for the " + "candidate requirement, not because the projection is unknown"); - int[] candidates = ReferenceChainTestHooks.selectLeakCandidateKlassIds0(); - assertTrue(candidates == null || candidates.length == 0, - "This test's own precondition: no per-klass candidate should exist, so a pass running " - + "below could only come from the aggregate urgent-OOM projection"); - - // Idle BFS cadence is ~1s/pass. No pass may run: with zero per-klass candidates the gate - // has no indication, and urgency alone must not supply one. - int passesBefore = ReferenceChainTestHooks.referenceChainPassesRunForTest0(); - long deadline = System.currentTimeMillis() + 5_000; - while (System.currentTimeMillis() < deadline) { - assertTrue(ReferenceChainTestHooks.referenceChainPassesRunForTest0() == passesBefore, - "The BFS thread must stay idle: an urgent-OOM projection with zero per-klass leak " - + "candidates must not start a search"); - Thread.sleep(50); - } + // Positive control 1: past the backdated burn-in, the seeded rise must project inside the + // urgent band. Fails fast, with the value, if the burn-in still masks the projection. + double secondsToOom = ReferenceChainTestHooks.secondsToOOMForTest0(); + assertTrue(secondsToOom >= 0 && secondsToOom < OOM_URGENT_THRESHOLD_S, + "Positive control: urgent band [0, " + OOM_URGENT_THRESHOLD_S + ")s required; got " + + secondsToOom + " - the burn-in backdate did not take effect or the seeds no " + + "longer project, so the closed gate below would prove nothing"); + + GateEvaluation e = evaluateGateAfterSearchReset(); + checkNoCandidateFreeActivity(e); + + // Positive control 2: the gate evaluation must have latched urgency from the seeded + // projection, so the gate is closed by the candidate requirement and not by a dormant + // urgency machine. + assertTrue(ReferenceChainTestHooks.urgentLatchedForTest0(), + "Urgency must be latched after evaluating the gate on an urgent-band projection (" + + secondsToOom + "s)"); - // Same assertion through the predicate itself, on a freshly reset search so any mutation - // the live thread's evaluations made lands on clean state (see the flat-floor control - // below for why the reset must not touch the population). This direct call is race-free: - // the gate's answer no longer depends on evaluation order. - ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); - assertFalse(ReferenceChainTestHooks.shouldRunPassForTest0(), - "Expected the search-restart gate to stay closed: the urgent-OOM projection is not a " - + "leak indication - only selectLeakCandidates() is"); - - candidates = ReferenceChainTestHooks.selectLeakCandidateKlassIds0(); - assertTrue(candidates == null || candidates.length == 0, - "The idle window must have kept the per-klass population table empty - the " - + "aggregate urgent-OOM projection was the only signal this test seeded"); + assertFalse(e.gateOpen, + "Expected the search gate to stay closed: urgency is latched but no per-klass " + + "candidate exists - only selectLeakCandidates() is a leak indication (" + + e.detail + ")"); } finally { - restoreHeapFloorDefaults(); + restoreHeapFloorDefaults(savedActivationNs); } } /** - * Same empty per-klass population table as above, but a flat heap floor (no growth at all) - - * {@code secondsToOOM()} has nothing to project from, so this test's own gate must stay closed. - * Without this control, a stubbed-out {@code secondsToOOM()} that always signals urgency would - * pass the test above just as well. + * Control for the test above: same empty per-klass population table and same backdated + * burn-in, but a flat heap floor (no growth). {@code secondsToOOM()} has nothing to project + * from - asserted directly through {@code secondsToOOMForTest0()}, and because the burn-in is + * backdated the "no projection" comes from the flat ring, not from the burn-in - so urgency + * must not latch and the gate must stay closed. This complements the primary test's + * urgent-band case with the no-projection case. + * + *

The gate's closed answer alone cannot tell the two cases apart: under the candidate-gate + * contract {@code hasLeakSignal()} answers only from {@code selectLeakCandidates()}, so with an + * empty population table the gate stays closed whatever the projection says. A stubbed or + * dormant {@code secondsToOOM()} is caught instead by the direct projection reads (urgent band + * above, none here) and by the urgency-latch reads after each gate evaluation. */ @Test public void shouldNotOpenSearchGateOnFlatHeapFloorWithNoLeakCandidate() { @@ -167,31 +249,68 @@ public void shouldNotOpenSearchGateOnFlatHeapFloorWithNoLeakCandidate() { ReferenceChainTestHooks.setHeapFloorRecordingForTest0(false); ReferenceChainTestHooks.resetKlassPopulationForTest0(); ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); + long savedActivationNs = ReferenceChainTestHooks.backdateLeakSignalActivationForTest0(); try { ReferenceChainTestHooks.setMaxHeapBytesForTest0(1000L * 1024 * 1024); for (int i = 0; i < 10; i++) { ReferenceChainTestHooks.heapFloorRecordForTest0(200L * 1024 * 1024, i * 1_000_000_000L); } - int[] candidates = ReferenceChainTestHooks.selectLeakCandidateKlassIds0(); - assertTrue(candidates == null || candidates.length == 0, - "This test's own precondition: no per-klass candidate should exist either"); - - // shouldRunPassForTest0() is NOT a pure predicate (see JavaProfiler's own javadoc): under the - // native engine lock it may account a finished search's pain, restart terminal search state, - // or advance urgency bookkeeping. Reset the search right before it so any mutation - // lands on a fresh tracker (_search_started == false) instead of whatever search an earlier - // class in this shared JVM left behind. Only the search is reset here, not the population: - // resetKlassPopulationForTest0() also zeroes the heap-floor ring seeded above, which would - // turn this control into a trivial "insufficient fill" pass. - ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); - boolean gateOpen = ReferenceChainTestHooks.shouldRunPassForTest0(); - assertFalse(gateOpen, + double secondsToOom = ReferenceChainTestHooks.secondsToOOMForTest0(); + assertTrue(secondsToOom < 0, + "Control precondition: past the backdated burn-in, a flat heap floor must yield no " + + "projection; got secondsToOOM=" + secondsToOom); + + // Only the search is reset right before the gate call (inside + // evaluateGateAfterSearchReset()), not the population: resetKlassPopulationForTest0() also + // zeroes the heap-floor ring seeded above, which would turn this control into a trivial + // "insufficient fill" pass. + GateEvaluation e = evaluateGateAfterSearchReset(); + checkNoCandidateFreeActivity(e); + + assertFalse(ReferenceChainTestHooks.urgentLatchedForTest0(), + "A flat heap floor gives no projection, so it must leave urgency unlatched"); + assertFalse(e.gateOpen, "Expected the search-restart gate to stay closed: no heap growth and no per-klass " - + "candidate give hasLeakSignal() nothing to trust"); + + "candidate give hasLeakSignal() nothing to trust (" + e.detail + ")"); + } finally { + restoreHeapFloorDefaults(savedActivationNs); + } + } + + /** + * Guards the burn-in stamp handling in {@link #restoreHeapFloorDefaults(long)}: the + * {@link #ACTIVATION_STAMP_UNCHANGED} sentinel must leave the stamp alone (and never be written + * as a stamp), and a saved stamp must be restored exactly. Fails if the helper drops the + * stamp-restore step, restores on the sentinel, or the JNI setter is a no-op. + */ + @Test + public void shouldRestoreActivationStampOnlyWhenRequested() { + assumeDebugBuild(); + ReferenceChainTestHooks.setHeapFloorRecordingForTest0(false); + ReferenceChainTestHooks.resetKlassPopulationForTest0(); + ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); + long savedActivationNs = ReferenceChainTestHooks.backdateLeakSignalActivationForTest0(); + long afterSentinel; + try { + // Must leave the backdate in place. + restoreHeapFloorDefaults(ACTIVATION_STAMP_UNCHANGED); + ReferenceChainTestHooks.setHeapFloorRecordingForTest0(false); + // Reads the stamp the sentinel path left behind (and backdates again; the finally block + // below restores the original). + afterSentinel = ReferenceChainTestHooks.backdateLeakSignalActivationForTest0(); } finally { - restoreHeapFloorDefaults(); + restoreHeapFloorDefaults(savedActivationNs); } + long afterRestore = ReferenceChainTestHooks.backdateLeakSignalActivationForTest0(); + // Undo the probe's own backdate. + ReferenceChainTestHooks.setLeakSignalActivatedNsForTest0(afterRestore); + assertNotEquals(ACTIVATION_STAMP_UNCHANGED, afterSentinel, + "The sentinel must never be written as a stamp"); + assertNotEquals(savedActivationNs, afterSentinel, + "The sentinel path must not restore the stamp"); + assertEquals(savedActivationNs, afterRestore, + "restoreHeapFloorDefaults(saved) must restore the stamp"); } /** @@ -199,8 +318,8 @@ public void shouldNotOpenSearchGateOnFlatHeapFloorWithNoLeakCandidate() { * a finally block that no assertion observes, so without this test a reset whose body was * dropped (or whose JNI entry was left as the release stub) would leave the whole suite * green while leaking one test's seeded samples into the next test's JVM. The rising-then- - * empty pair is asserted directly through {@code heapFloorRising()} - the same read the - * aggregate gate uses - so the reset's effect is checked, not assumed. + * empty pair is asserted directly through {@code heapFloorRising()}, the ring's trend read, so + * the reset's effect is checked, not assumed. */ @Test public void shouldEmptyHeapFloorRingOnReset() { @@ -209,7 +328,7 @@ public void shouldEmptyHeapFloorRingOnReset() { ReferenceChainTestHooks.resetKlassPopulationForTest0(); ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); try { - // Same rising seeds as shouldOpenSearchGateOnAggressiveHeapWideGrowthWithNoLeakCandidate: + // Same rising seeds as shouldNotOpenSearchGateOnAggressiveHeapWideGrowthWithNoLeakCandidate: // 100 MiB to 991 MiB over 9 fake seconds, which clears heapFloorRising()'s growth and // floor bars at exactly the min-fill trend window (fill = 10 samples). for (int i = 0; i < 10; i++) { @@ -224,9 +343,11 @@ public void shouldEmptyHeapFloorRingOnReset() { "heapFloorResetForTest0() must empty the ring: fill = 0 falls below the min-fill " + "trend gate heapFloorRising() requires"); } finally { - // Same order as the other tests: ring reset first, still disabled. + // Ring reset first, while recording is still disabled; the shared helper owns the rest of + // the ordering (max heap, population ring, search reset, re-enable recording). This test + // never backdates the burn-in stamp. ReferenceChainTestHooks.heapFloorResetForTest0(); - ReferenceChainTestHooks.setHeapFloorRecordingForTest0(true); + restoreHeapFloorDefaults(ACTIVATION_STAMP_UNCHANGED); } } } diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java index e4f3ce717c..4230200214 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java @@ -112,6 +112,15 @@ public class ReferenceChainTrackingTest extends AbstractProfilerTest { // for the same cheap-insurance-against-collision reason those two are distinct from each other. private static final int ABANDON_TEST_KLASS_ID = 987203; + // shouldReportAbandonedSearchOnTinyFrontierCap()'s dump pacing. Each dump() rewrites this shared + // JVM's whole recording (O(recording size) I/O), so the test caps its dumps, waits for the first + // post-reset pass without I/O first, and paces the remaining dumps so the last one still lands + // ABANDON_LAST_DUMP_MIN_DELAY_MS after the grace period started. + private static final int ABANDON_MAX_DUMPS = 3; + private static final long ABANDON_PASS_WAIT_MS = 6_000; + private static final long ABANDON_DUMP_RETRY_SPACING_MS = 1_000; + private static final long ABANDON_LAST_DUMP_MIN_DELAY_MS = 8_000; + /** * The {@code datadog.ReferenceChain} ActiveSetting's {@code id} (flightRecorder.cpp's * T_REFERENCE_CHAIN). ActiveSetting events from cpu/wall/memory/nativemem also read @@ -711,30 +720,30 @@ public void shouldReportAbandonedSearchOnTinyFrontierCap() throws Exception { "The qualifying-candidate seeding below rides the debug-only population seams; in a " + "release build the search stays idle without a candidate (the candidate-gate " + "contract), so no abandonment event could ever be produced"); - if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { - // See the javadoc above for why the seeding is needed despite no generations=true of this - // method's own, and why both a gc() and a throwaway dump() must precede it. - System.gc(); - Path warmupDumpPath = Files.createTempFile("referencechains-abandoned-test-warmup-", ".jfr"); + // Release builds never get here (the JUnit assumption above skips them): the seeding below + // needs the debug-only population seams. See the javadoc above for why the seeding is needed + // despite no generations=true of this method's own, and why both a gc() and a throwaway + // dump() must precede it. + System.gc(); + Path warmupDumpPath = Files.createTempFile("referencechains-abandoned-test-warmup-", ".jfr"); + Files.deleteIfExists(warmupDumpPath); + try { + dump(warmupDumpPath); + } finally { Files.deleteIfExists(warmupDumpPath); - try { - dump(warmupDumpPath); - } finally { - Files.deleteIfExists(warmupDumpPath); - } - ReferenceChainTestHooks.resetKlassPopulationForTest0(); - // Gate-only usage: this test drives the search to ABANDONED and asserts on the abandon - // marker, no tagged instance needs to match - so the tid is this thread's real one but any - // distinct tid would do. - int abandonTid = ReferenceChainTestHooks.getTid(); - for (int epoch = 1; epoch <= SEED_EPOCHS_FOR_HYSTERESIS; epoch++) { - ReferenceChainTestHooks.seedKlassPopulationSample0( - ABANDON_TEST_KLASS_ID, epoch * 10, epoch); - ReferenceChainTestHooks.seedTidTrendSample0( - ABANDON_TEST_KLASS_ID, abandonTid, epoch * 3, epoch); - } - ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); } + ReferenceChainTestHooks.resetKlassPopulationForTest0(); + // Gate-only usage: this test drives the search to ABANDONED and asserts on the abandon + // marker, no tagged instance needs to match - so the tid is this thread's real one but any + // distinct tid would do. + int abandonTid = ReferenceChainTestHooks.getTid(); + for (int epoch = 1; epoch <= SEED_EPOCHS_FOR_HYSTERESIS; epoch++) { + ReferenceChainTestHooks.seedKlassPopulationSample0( + ABANDON_TEST_KLASS_ID, epoch * 10, epoch); + ReferenceChainTestHooks.seedTidTrendSample0( + ABANDON_TEST_KLASS_ID, abandonTid, epoch * 3, epoch); + } + ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); List gcRootHolder = new ArrayList<>(); gcRootHolder.add(new ChainLink("middle", new ChainLink("leaf"))); @@ -746,28 +755,55 @@ public void shouldReportAbandonedSearchOnTinyFrontierCap() throws Exception { System.gc(); Thread.sleep(100); } + long graceStartNs = System.nanoTime(); Thread.sleep(1500); + // Dump-free probe: every dump() rewrites this shared JVM's whole recording, so wait in memory + // for the BFS thread's first post-reset pass (the one that hits the framecap/ttl cutoff and + // enqueues the abandoned event) before taking the first dump. An accelerator only, never an + // assertion: a search restart also zeroes passesRun, so on timeout just fall through to the + // dumps. + long probeDeadlineNs = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(ABANDON_PASS_WAIT_MS); + int passesRun = ReferenceChainTestHooks.referenceChainPassesRunForTest0(); + while (passesRun <= 0 && System.nanoTime() - probeDeadlineNs < 0) { + Thread.sleep(100); + passesRun = ReferenceChainTestHooks.referenceChainPassesRunForTest0(); + } + Path dumpPath = Files.createTempFile("referencechains-abandoned-test-", ".jfr"); Files.deleteIfExists(dumpPath); try { // Even with the seeding above clearing the gate, the first pass still has to actually reach // the frontier cap/ttl cutoff inside whatever wall-clock time this shared JVM's scheduling - // grants it - a single fixed-length sleep-then-dump-then-assert previously failed whenever - // that one attempt landed a beat early. Retry across a few more dumps before concluding the - // search genuinely never abandoned. + // grants it. The abandoned event only reaches the recording through a dump(), so each check + // needs a fresh dump - and each dump rewrites the whole shared recording. Dumps are + // therefore capped at ABANDON_MAX_DUMPS and paced so the last one lands at least + // ABANDON_LAST_DUMP_MIN_DELAY_MS after the grace period started; the passesRun probe above + // absorbs most of the scheduling slack without any I/O. + long lastDumpEarliestNs = + graceStartNs + TimeUnit.MILLISECONDS.toNanos(ABANDON_LAST_DUMP_MIN_DELAY_MS); JfrEvents abandoned = null; - for (int attempt = 0; attempt < 8; attempt++) { + int dumpsTaken = 0; + for (int attempt = 0; attempt < ABANDON_MAX_DUMPS; attempt++) { + if (attempt > 0) { + int attemptsLeft = ABANDON_MAX_DUMPS - attempt; // includes the dump about to be taken + long remainingMs = + TimeUnit.NANOSECONDS.toMillis(lastDumpEarliestNs - System.nanoTime()); + Thread.sleep(Math.max(ABANDON_DUMP_RETRY_SPACING_MS, remainingMs / attemptsLeft)); + } dump(dumpPath); + dumpsTaken++; abandoned = verifyEvents(dumpPath, "datadog.ReferenceChainAbandoned", false); if (abandoned.hasItems()) { break; } - Thread.sleep(1000); } assertTrue(abandoned != null && abandoned.hasItems(), - "Expected at least one datadog.ReferenceChainAbandoned event after 3 GCs, an initial " - + "1500ms grace period, and 8 retries 1s apart"); + "Expected a datadog.ReferenceChainAbandoned event within " + dumpsTaken + " of at most " + + ABANDON_MAX_DUMPS + " dumps (after 3 GCs and a 1500ms grace period; the pass " + + "probe waited up to " + ABANDON_PASS_WAIT_MS + "ms, passesRun=" + + ReferenceChainTestHooks.referenceChainPassesRunForTest0() + "; last dump >= " + + ABANDON_LAST_DUMP_MIN_DELAY_MS + "ms after grace start)"); } finally { Files.deleteIfExists(dumpPath); } From 5d227985e725b21213fc97078cc8a67aa709013a Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Thu, 29 Oct 2026 17:08:40 +0100 Subject: [PATCH 07/37] Document urgency latch scope and urgent-no-candidate counter The latch neither starts searches nor feeds the ramp calculation; its only consumer is no-progress-abandonment suppression. The admission boost releases on the raw band comparison, not the latch hysteresis. The candidate-only gate's refused urgent evaluations are now counted. --- doc/architecture/ReferenceChainsSignalsExplained.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/architecture/ReferenceChainsSignalsExplained.md b/doc/architecture/ReferenceChainsSignalsExplained.md index c5a95f1347..8ef3295b56 100644 --- a/doc/architecture/ReferenceChainsSignalsExplained.md +++ b/doc/architecture/ReferenceChainsSignalsExplained.md @@ -22,6 +22,8 @@ Before the first search, `shouldRunPass()` calls `canAffordNewSearch()`. The gat Urgency does not authorize a search. A candidate-less search can never emit a chain, and while urgent the 30-pass no-progress abandonment is suppressed, so it would run to the full TTL doing whole-heap BFS. Urgency applies through the ramp once a search is running. +The tradeoff of this candidate-only gate: a heap-wide leak spread across many classes, none of which individually clears `selectLeakCandidates()`'s per-klass ring-fill/hysteresis gate, never opens the gate, even in the emergency band (projected OOM under 5 minutes). No chains are produced in that case. Each such refused evaluation increments the `reference_chain_urgent_no_candidate` counter (about once per second while urgency is latched and no candidate exists), so the gap is visible in the diagnostics rather than silent, and it can be told apart from an idle healthy process. + The population-trend signal is constructed by the liveness tracker, which fills the per-klass population table and the heap-floor ring when generations tracking OR liveheap tracking is active (the two options drive the same tracking mechanisms; liveheap additionally preserves stack traces for its samples). When neither is active, no signal can ever be produced: `hasLeakSignal()` returns false, and the collector stays idle - it never runs a pass, restart, or search. ## Scheduling a running search From 39f6a1a1c6170f552a96cda38824b96e2df27ed1 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 10:56:58 +0100 Subject: [PATCH 08/37] Harden the fix-round test seams against review-delta findings - setLeakSignalActivatedNsForTest0 rejects negative arguments instead of casting them to a near-UINT64_MAX burn-in stamp that would read as burn-in-never-passed. - evaluateGateAfterSearchReset samples the per-klass candidate table before the search reset: a pass decided on an organic candidate can straddle the reset (the pass counter increments at pass end), and the candidate stays selectable from the not-reset population table, so the pre-reset read keeps the candidateSeen bracket honest. - The abandonment test's warmup dump path is deleted inside the try/finally guard, closing the leak window between createTempFile and guard entry. --- ddprof-lib/src/main/cpp/javaApi.cpp | 8 ++++++++ .../AggressiveLeakReferenceChainTest.java | 9 ++++++++- .../referencechains/ReferenceChainTrackingTest.java | 4 +++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/ddprof-lib/src/main/cpp/javaApi.cpp b/ddprof-lib/src/main/cpp/javaApi.cpp index a689a23f30..f506215f41 100644 --- a/ddprof-lib/src/main/cpp/javaApi.cpp +++ b/ddprof-lib/src/main/cpp/javaApi.cpp @@ -1423,6 +1423,14 @@ Java_com_datadoghq_profiler_JavaProfiler_backdateLeakSignalActivationForTest0( extern "C" DLLEXPORT void JNICALL Java_com_datadoghq_profiler_JavaProfiler_setLeakSignalActivatedNsForTest0( JNIEnv *env, jclass unused, jlong activatedNs) { + if (activatedNs < 0) { + // A raw jlong->u64 cast would turn a negative argument into a stamp near UINT64_MAX, + // which secondsToOOM() reads as burn-in-never-passed - a mis-wired test would then look + // like a dormant projection instead of an invalid input. + throwNew(env, "java/lang/IllegalArgumentException", + "activatedNs must be >= 0 (0 = never activated)"); + return; + } ProfiledThread::initCurrentThreadSignalSafe(); LivenessTracker::instance()->setLeakSignalActivatedNsForTest((u64)activatedNs); } diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java index 352fc36f2c..3140c2c431 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java @@ -129,8 +129,15 @@ private static final class GateEvaluation { * told apart from a contract violation (see {@link #checkNoCandidateFreeActivity}). */ private static GateEvaluation evaluateGateAfterSearchReset() { - ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); + // Sampled BEFORE the reset: a pass whose gate decision was made on an organic candidate + // executes under the engine lock, so the decision and its pass counter increment can + // straddle the reset (the counter is zeroed by the reset, and only at pass END - a long + // in-flight pass is invisible to a stable-counter check). The candidate that legitimized + // such a pass stays selectable from the population table - deliberately not reset here - + // so reading it before the reset, while the decision is fresh, is what keeps the + // candidateSeen bracket honest. int[] candidatesBefore = ReferenceChainTestHooks.selectLeakCandidateKlassIds0(); + ReferenceChainTestHooks.resetReferenceChainSearchForTest0(); int passesBefore = ReferenceChainTestHooks.referenceChainPassesRunForTest0(); GateEvaluation e = new GateEvaluation(); e.gateOpen = ReferenceChainTestHooks.shouldRunPassForTest0(); diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java index 4230200214..b13b7478b0 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java @@ -726,8 +726,10 @@ public void shouldReportAbandonedSearchOnTinyFrontierCap() throws Exception { // dump() must precede it. System.gc(); Path warmupDumpPath = Files.createTempFile("referencechains-abandoned-test-warmup-", ".jfr"); - Files.deleteIfExists(warmupDumpPath); try { + // Inside the guard: createTempFile() already created the file, so a throw between + // creation and the finally below would otherwise leak it. + Files.deleteIfExists(warmupDumpPath); dump(warmupDumpPath); } finally { Files.deleteIfExists(warmupDumpPath); From 421f830d3467629ccde8dc3800d548e3570c90da Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 12:33:51 +0100 Subject: [PATCH 09/37] Clarify concurrency-legality comments in reference-chain stop path - The stop()-side reset of the urgency/baseline fields now states the full claim: production writes go through the BFS thread only, and the join in stopThread() is what makes the control-thread plain stores safe. - The leak-signal test-seam block now records why UNIT_TEST is required beyond DEBUG (gtestRelease compiles with -DNDEBUG and no -DDEBUG) and which consumer class needs each arm. --- ddprof-lib/src/main/cpp/livenessTracker.h | 12 ++++++++---- ddprof-lib/src/main/cpp/referenceChains.cpp | 7 ++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index 802a05118e..4e595eeb94 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -1705,10 +1705,14 @@ class alignas(alignof(SpinLock)) LivenessTracker { #if defined(DEBUG) || defined(UNIT_TEST) // Test-only mutators of the leak-signal gates and the burn-in stamp. - // Compiled in for DEBUG builds (they back the DEBUG-only JNI seams in - // javaApi.cpp) and for gtest builds (GtestTaskBuilder adds -DUNIT_TEST to - // every gtest compile); never in the shipped release library, so no release - // consumer can flip the leak-signal gate or backdate the burn-in. + // Two consumer classes, neither sufficient alone: DEBUG backs the JNI seams + // in javaApi.cpp used by ddprof-test against the debug library build (not a + // gtest build, so no UNIT_TEST there), and UNIT_TEST keeps the symbols in + // the gtest binaries built from non-debug configurations - gtestRelease + // compiles with -DNDEBUG and no -DDEBUG (ConfigurationPresets), and its + // gtests link against these directly. The shipped release library defines + // neither, so no release consumer can flip the leak-signal gate or backdate + // the burn-in. // Sets the two leak-signal gates directly, bypassing initialize() (which // requires a live JVM - VM::hotspot_version()/VM::jni(), see that method's diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index 6286df5e7d..ae00c455e2 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -410,10 +410,15 @@ void ReferenceChainTracker::stop() { store(_abandon_reason, (u8)SearchAbandonReason::RECORDING_END); storeRelease(_search_state, (u8)SearchState::ABANDONED); } + // The four fields reset below are written in production only by the BFS thread + // (isUrgent(), updateUrgentAdmission(), restartSearch()), as plain non-atomic + // stores. Resetting them from this control thread is safe only because + // stopThread() has already joined that thread: the join both removes the only + // concurrent writer and orders these resets after everything the BFS thread + // ever wrote. store(_search_start_ns, (u64)0); _urgent_latched = false; _urgent_release_ticks = 0; - // BFS-thread owned like the two fields above; stopThread() has joined that thread. _admission_growth_baseline = 0; // Same per-search candidate/discovered canary state runPass()'s terminal branch and // restartSearch() clear - zeroing the counts here closes the stale-chain windows in From f7a26d20c4de370e9667e218224e420aa02c82ee Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 12:49:55 +0100 Subject: [PATCH 10/37] Trim comment verbosity in the two review-follow-up blocks --- ddprof-lib/src/main/cpp/livenessTracker.h | 12 ++++-------- ddprof-lib/src/main/cpp/referenceChains.cpp | 9 +++------ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index 4e595eeb94..eadc23bc81 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -1705,14 +1705,10 @@ class alignas(alignof(SpinLock)) LivenessTracker { #if defined(DEBUG) || defined(UNIT_TEST) // Test-only mutators of the leak-signal gates and the burn-in stamp. - // Two consumer classes, neither sufficient alone: DEBUG backs the JNI seams - // in javaApi.cpp used by ddprof-test against the debug library build (not a - // gtest build, so no UNIT_TEST there), and UNIT_TEST keeps the symbols in - // the gtest binaries built from non-debug configurations - gtestRelease - // compiles with -DNDEBUG and no -DDEBUG (ConfigurationPresets), and its - // gtests link against these directly. The shipped release library defines - // neither, so no release consumer can flip the leak-signal gate or backdate - // the burn-in. + // DEBUG backs the JNI seams javaApi.cpp exposes to ddprof-test (debug + // library build); UNIT_TEST covers the gtest binaries, whose release + // configuration compiles with -DNDEBUG and no -DDEBUG. The shipped + // release library defines neither. // Sets the two leak-signal gates directly, bypassing initialize() (which // requires a live JVM - VM::hotspot_version()/VM::jni(), see that method's diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index ae00c455e2..22653f5ac3 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -410,12 +410,9 @@ void ReferenceChainTracker::stop() { store(_abandon_reason, (u8)SearchAbandonReason::RECORDING_END); storeRelease(_search_state, (u8)SearchState::ABANDONED); } - // The four fields reset below are written in production only by the BFS thread - // (isUrgent(), updateUrgentAdmission(), restartSearch()), as plain non-atomic - // stores. Resetting them from this control thread is safe only because - // stopThread() has already joined that thread: the join both removes the only - // concurrent writer and orders these resets after everything the BFS thread - // ever wrote. + // Written in production only by the BFS thread (isUrgent(), + // updateUrgentAdmission(), restartSearch()) as plain stores; safe to reset + // from this thread only because stopThread() has joined that thread. store(_search_start_ns, (u64)0); _urgent_latched = false; _urgent_release_ticks = 0; From 1deb976a01cf60c76f26e1794d3f44d8eb968be0 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 12:56:47 +0100 Subject: [PATCH 11/37] Deduplicate comment content across the leak-signal gate seams Each fact now has one home: the generations/liveheap equivalence lives on _leak_signal_enabled, the SPSC acquire pairing on newestHeapFloorSample()'s definition, the burn-in rationale on OOM_PROJECTION_BURN_IN_NS; the other sites point instead of restating. --- ddprof-lib/src/main/cpp/livenessTracker.cpp | 5 +- ddprof-lib/src/main/cpp/livenessTracker.h | 57 +++++++-------------- ddprof-lib/src/main/cpp/referenceChains.cpp | 7 +-- ddprof-lib/src/main/cpp/referenceChains.h | 6 +-- 4 files changed, 23 insertions(+), 52 deletions(-) diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index 58044e8d71..4c207bd152 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -2132,10 +2132,7 @@ Error LivenessTracker::initialize(Arguments &args) { // observe the flag it was actually started with, even though the tracking // table itself persists across recordings. _gc_generations.store(args._gc_generations, std::memory_order_relaxed); - // Either generations tracking or liveheap tracking is a sufficient source - // for the population-trend signal (they drive the same mechanisms here; - // liveheap additionally preserves stack traces for its samples) - see - // _leak_signal_enabled's own comment (livenessTracker.h). + // See _leak_signal_enabled's own comment for why either source suffices. _leak_signal_enabled.store(args._gc_generations || args._record_liveness, std::memory_order_relaxed); diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index eadc23bc81..876daf60d5 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -466,18 +466,13 @@ class alignas(alignof(SpinLock)) LivenessTracker { // Effective leak-signal availability: true when the population-trend signal // consumed by ReferenceChainTracker::hasLeakSignal() can be constructed at - // all. generations tracking and liveheap tracking drive the same - // mechanisms here (per-klass population table, heap-floor ring, - // secondsToOOM() projection) - liveheap additionally preserves stack - // traces for its live samples - so either option is sufficient. When - // neither is on, there is no signal source and the reference-chain - // collector must stay idle (hasLeakSignal() -> false): running whole-heap - // searches with zero candidates "just in case" is the exact waste the - // leak-signal gate exists to prevent (observed as a frontier table grown - // to its cap with reference_chain_candidate_count == 0 in a healthy - // process when this used to fall back to "always true"). Set from - // args._gc_generations || args._record_liveness in initialize(). Same - // atomicity/reader argument as _gc_generations above. + // all. Generations tracking and liveheap tracking drive the same mechanisms + // here (per-klass population table, heap-floor ring, secondsToOOM() + // projection) - liveheap additionally preserves stack traces - so either + // option suffices; with neither on, hasLeakSignal() -> false and the + // reference-chain collector must stay idle instead of searching with zero + // candidates. Set from args._gc_generations || args._record_liveness in + // initialize(). Same atomicity/reader argument as _gc_generations above. std::atomic _leak_signal_enabled; // OS::nanotime() of the first initialize() that enabled the leak signal; 0 @@ -863,11 +858,8 @@ class alignas(alignof(SpinLock)) LivenessTracker { jlong maxHeapBytes() const { return _max_heap_bytes; } // Reads the used-bytes value of the newest _heap_floor_ring sample (0 when - // the ring is empty). Matches recordHeapFloorSampleUnchecked()'s SPSC - // publish order - loadAcquire() on the index fields is what makes the - // payload store visible. Consumed by ReferenceChainTracker's urgent-admission - // gate as the freshest physical growth evidence; it must observe the same - // samples the projection does, hence the same acquire pairing. + // the ring is empty) - the urgent-admission gate's freshest physical growth + // evidence. See the definition for the SPSC acquire pairing. u64 newestHeapFloorSample() const; constexpr static int MAX_LEAK_CANDIDATES = 5; @@ -1050,23 +1042,14 @@ class alignas(alignof(SpinLock)) LivenessTracker { // pattern selectLeakCandidates()/getLiveTraceIds() already use. jobject resolveCandidateRepresentative(JNIEnv *env, u32 klass_id); - // Read-only observation point for the explicit generations option - // (_gc_generations; see that member's own comment), so tests and - // diagnostics can tell it apart from the effective leakSignalEnabled() gate - // (generations OR liveheap). No production code keys on it: since the - // reference-chain signal gates moved to leakSignalEnabled(), the - // population, heap-floor and search gates all read _leak_signal_enabled. - // This accessor never toggles the flag. + // Read-only observation point for the explicit generations option (see + // _gc_generations's own comment); no production code keys on it. bool gcGenerationsEnabled() const { return _gc_generations.load(std::memory_order_relaxed); } - // Exposes the effective leak-signal availability (see _leak_signal_enabled's - // own comment) - the gate ReferenceChainTracker::hasLeakSignal() keys on. - // True when generations tracking OR liveheap tracking is on; false means no - // population-trend signal can ever be constructed and the reference-chain - // collector must not search. Read-only; this accessor never toggles the - // flag. + // The effective gate ReferenceChainTracker::hasLeakSignal() keys on (see + // _leak_signal_enabled's own comment). bool leakSignalEnabled() const { return _leak_signal_enabled.load(std::memory_order_relaxed); } @@ -1088,11 +1071,9 @@ class alignas(alignof(SpinLock)) LivenessTracker { // // The projection feeds the urgency latch (ReferenceChainTracker::isUrgent()), // the urgent admission ramp (ReferenceChainTracker::updateUrgentAdmission()) - // and threadLoop()'s OOM cadence/budget ramp. It does NOT start a search: - // ReferenceChainTracker::hasLeakSignal() still returns true only when - // selectLeakCandidates() yields a klass, urgency or not. Not a claim that - // growth stays linear, only that a short-horizon linear extrapolation is a - // reasonable urgency signal. + // and threadLoop()'s OOM cadence/budget ramp; it never starts a search (see + // OOM_URGENT_THRESHOLD_S). It is a short-horizon linear extrapolation, not a + // claim that growth stays linear. // Returns a negative value if the chosen ring is not filled enough yet // (leakSignalEnabled() is off, or too few GC epochs have happened), not // rising, or neither _max_heap_bytes nor _container_memory_limit was ever @@ -1109,10 +1090,8 @@ class alignas(alignof(SpinLock)) LivenessTracker { // this method additionally divides by elapsed time, so the same gap // understates the growth rate (overstates the projected time-to-OOM) // rather than the reverse - not solved here. - // Short-horizon linear extrapolation of the rising JVM heap-floor and - // container-memory trends against their limits (see the long comment in - // livenessTracker.h). Returns -1 while the leak signal is in its burn-in - // (see OOM_PROJECTION_BURN_IN_NS). + // Returns -1 while the leak signal is in its burn-in (see + // OOM_PROJECTION_BURN_IN_NS); otherwise see the comment above. double secondsToOOM() const; // secondsToOOM() withholds its projection (returns -1) for this long after diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index 22653f5ac3..b125df970e 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -778,11 +778,8 @@ bool ReferenceChainTracker::shouldRunPass(u64 now_ns) { bool ReferenceChainTracker::isUrgent() const { static_assert(LivenessTracker::OOM_PROJECTION_BURN_IN_NS / 1e9 >= OOM_URGENT_THRESHOLD_S, "burn-in must cover the urgent horizon"); - // Warmup is filtered inside secondsToOOM(): no projection (-1) until - // OOM_PROJECTION_BURN_IN_NS after the leak signal first activated. Age-based - // on purpose - a guard on the heap-floor ring's time span shrinks with GC - // frequency and suppressed the latch in exactly the back-to-back-GC - // imminent-OOM case. + // Warmup is filtered inside secondsToOOM(): no projection (-1) until the + // burn-in passes (see OOM_PROJECTION_BURN_IN_NS for why it is age-based). double seconds_to_oom = LivenessTracker::instance()->secondsToOOM(); if (seconds_to_oom >= 0 && seconds_to_oom < OOM_URGENT_THRESHOLD_S) { _urgent_release_ticks = 0; diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index b3a00819a9..9e2ba53637 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -640,10 +640,8 @@ class ReferenceChainTracker { // consumed by runPass()'s no-progress abandonment branch, which it suppresses while latched; // (b) updateUrgentAdmission()'s emergency band, where tracking admission goes to 100% without // heap-growth evidence. - // It never authorizes starting a search: hasLeakSignal() still requires selectLeakCandidates() - // to return at least one klass past its per-klass ring-fill/hysteresis gate - // (KLASS_POPULATION_MIN_FILL_FOR_TREND plus LEAK_TREND_HYSTERESIS_BASE/CORROBORATED epochs, - // livenessTracker.h), and urgency never stands in for that. + // It never authorizes starting a search - urgency never stands in for + // selectLeakCandidates()'s per-klass gate (see hasLeakSignal()). static constexpr double OOM_URGENT_THRESHOLD_S = 300.0; // 5 minutes // Release side of OOM_URGENT_THRESHOLD_S's hysteresis (see _urgent_latched). From 8c89fa247244ad8945f8f698d61f3d0480a60513 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 13:30:11 +0100 Subject: [PATCH 12/37] Update architecture indexes for the leak-signal gate The reference-chain subsystem contract changed: collection stays idle without a generations or liveheap leak-signal source. Reflect that in the AGENTS.md subsystem row and the doc/README.md index entries. --- AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index abd038fadc..f20f8a63e5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -170,7 +170,7 @@ before working on a subsystem. | TLS context (thread-local tracing context, distributed tracing) | [doc/architecture/TLSContext.md](doc/architecture/TLSContext.md) | | Profiling engines (CPU, wall-clock, allocation, native memory, liveness) | [doc/architecture/ProfilingEngines.md](doc/architecture/ProfilingEngines.md) | | JFR integration (FlightRecorder, metadata, constant pools, buffers) | [doc/architecture/JfrIntegration.md](doc/architecture/JfrIntegration.md) | -| Live-heap reference chains (implementation, signal scheduling, design) | [doc/architecture/LiveHeapReferenceChainsImplementation.md](doc/architecture/LiveHeapReferenceChainsImplementation.md), [doc/architecture/ReferenceChainsSignalsExplained.md](doc/architecture/ReferenceChainsSignalsExplained.md), [doc/ReferenceChainsDesign.md](doc/ReferenceChainsDesign.md), [doc/ReferenceChainsCollectionSummary.md](doc/ReferenceChainsCollectionSummary.md) | +| Live-heap reference chains (implementation, leak-signal gate and scheduling, design) — collection stays idle without a generations or liveheap signal | [doc/architecture/LiveHeapReferenceChainsImplementation.md](doc/architecture/LiveHeapReferenceChainsImplementation.md), [doc/architecture/ReferenceChainsSignalsExplained.md](doc/architecture/ReferenceChainsSignalsExplained.md), [doc/ReferenceChainsDesign.md](doc/ReferenceChainsDesign.md), [doc/ReferenceChainsCollectionSummary.md](doc/ReferenceChainsCollectionSummary.md) | | Native memory profiling (malloc/free interception, sampling) | [doc/architecture/NativeMemoryProfiling.md](doc/architecture/NativeMemoryProfiling.md) | | Startup sanity checks | [doc/architecture/SanityChecks.md](doc/architecture/SanityChecks.md) | | SIGSEGV-safe library patching | [doc/architecture/SigsegvPatching.md](doc/architecture/SigsegvPatching.md) | From 636093972e94619366cde39a38ed13922c52e3e9 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 13:37:44 +0100 Subject: [PATCH 13/37] Drop gate semantics from the index entries Indexes only name the subsystem; activation semantics belong to the linked pages. --- AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index f20f8a63e5..6a4da52f33 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -170,7 +170,7 @@ before working on a subsystem. | TLS context (thread-local tracing context, distributed tracing) | [doc/architecture/TLSContext.md](doc/architecture/TLSContext.md) | | Profiling engines (CPU, wall-clock, allocation, native memory, liveness) | [doc/architecture/ProfilingEngines.md](doc/architecture/ProfilingEngines.md) | | JFR integration (FlightRecorder, metadata, constant pools, buffers) | [doc/architecture/JfrIntegration.md](doc/architecture/JfrIntegration.md) | -| Live-heap reference chains (implementation, leak-signal gate and scheduling, design) — collection stays idle without a generations or liveheap signal | [doc/architecture/LiveHeapReferenceChainsImplementation.md](doc/architecture/LiveHeapReferenceChainsImplementation.md), [doc/architecture/ReferenceChainsSignalsExplained.md](doc/architecture/ReferenceChainsSignalsExplained.md), [doc/ReferenceChainsDesign.md](doc/ReferenceChainsDesign.md), [doc/ReferenceChainsCollectionSummary.md](doc/ReferenceChainsCollectionSummary.md) | +| Live-heap reference chains (implementation, leak-signal gate, scheduling, design) | [doc/architecture/LiveHeapReferenceChainsImplementation.md](doc/architecture/LiveHeapReferenceChainsImplementation.md), [doc/architecture/ReferenceChainsSignalsExplained.md](doc/architecture/ReferenceChainsSignalsExplained.md), [doc/ReferenceChainsDesign.md](doc/ReferenceChainsDesign.md), [doc/ReferenceChainsCollectionSummary.md](doc/ReferenceChainsCollectionSummary.md) | | Native memory profiling (malloc/free interception, sampling) | [doc/architecture/NativeMemoryProfiling.md](doc/architecture/NativeMemoryProfiling.md) | | Startup sanity checks | [doc/architecture/SanityChecks.md](doc/architecture/SanityChecks.md) | | SIGSEGV-safe library patching | [doc/architecture/SigsegvPatching.md](doc/architecture/SigsegvPatching.md) | From a7ff84504da5006c87802d23fe15956bfeb4bee1 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 13:42:49 +0100 Subject: [PATCH 14/37] Track gtest .inc/.h includes in the up-to-date check The per-test compile task wired only the test .cpp into sources, while the .inc test partitions and test-local headers resolve via quoted includes relative to the test directory and appear in no include path. An .inc-only edit therefore reported UP-TO-DATE and silently ran a stale binary with the old tests. Register them as task inputs (not sources: they must not be compiled standalone) and drop the touch-the-.cpp workaround comment. --- .../datadoghq/native/gtest/GtestTaskBuilder.kt | 15 +++++++++++++++ ddprof-lib/src/test/cpp/referenceChains_ut.cpp | 5 ----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt index 3a0ea225cc..da47ac79cb 100644 --- a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt +++ b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt @@ -122,6 +122,21 @@ class GtestTaskBuilder( testFile ) } + + // The test .cpp pulls sibling .inc/.h files in via quoted includes, + // which resolve relative to the test file's directory without any -I + // flag, so they appear in no include path. They must not be compile + // sources (each .inc is a test partition included exactly once; + // compiling them standalone would emit duplicate symbols), but they + // are real compile dependencies. Registered as task inputs instead, + // so an .inc/.h-only edit invalidates the up-to-date check instead + // of silently running a stale binary with the old tests. + inputs.files( + project.fileTree(testFile.parentFile) { + include("**/*.inc", "**/*.h") + } + ) + includes.from(includeFiles) objectFileDir.set(objDir) } diff --git a/ddprof-lib/src/test/cpp/referenceChains_ut.cpp b/ddprof-lib/src/test/cpp/referenceChains_ut.cpp index 84ae571497..4bf35ef141 100644 --- a/ddprof-lib/src/test/cpp/referenceChains_ut.cpp +++ b/ddprof-lib/src/test/cpp/referenceChains_ut.cpp @@ -46,9 +46,6 @@ static ReferenceChainsGlobalSetup global_setup; [[maybe_unused]] static const bool kRcDebugLevelPinnedForTests = setenv("DD_PROFILING_REFERENCE_CHAINS_DEBUG", "2", 1) == 0; -// NOTE: the .inc files below are NOT registered as compile inputs by the gtest -// plugin (GtestTaskBuilder wires only the .cpp into `sources`), so editing an -// .inc alone leaves this binary stale - touch this file to force recompilation. #include "referenceChainsCoreTests.inc" #include "referenceChainsBfsTests.inc" #include "referenceChainsLifecycleTests.inc" @@ -61,6 +58,4 @@ static ReferenceChainsGlobalSetup global_setup; #include "referenceChainsEventTests.inc" #include "referenceChainsPesTests.inc" -// Recompilation marker: .inc files are not compile inputs (see note above). - From 64d7650c98c49a2961ac2612ca3c9da2a52fcabc Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 13:49:16 +0100 Subject: [PATCH 15/37] Shorten the gtest .inc inputs comment --- .../com/datadoghq/native/gtest/GtestTaskBuilder.kt | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt index da47ac79cb..5c2a561207 100644 --- a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt +++ b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt @@ -123,14 +123,10 @@ class GtestTaskBuilder( ) } - // The test .cpp pulls sibling .inc/.h files in via quoted includes, - // which resolve relative to the test file's directory without any -I - // flag, so they appear in no include path. They must not be compile - // sources (each .inc is a test partition included exactly once; - // compiling them standalone would emit duplicate symbols), but they - // are real compile dependencies. Registered as task inputs instead, - // so an .inc/.h-only edit invalidates the up-to-date check instead - // of silently running a stale binary with the old tests. + // Test .inc/.h files resolve via quoted includes relative to the test + // directory and appear in no include path, so register them here or an + // .inc-only edit reports UP-TO-DATE and runs a stale binary. Inputs, + // not sources: compiling an .inc standalone would emit duplicate symbols. inputs.files( project.fileTree(testFile.parentFile) { include("**/*.inc", "**/*.h") From cd55cf5791ba9b977899dc282dd44a8c5861f906 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 14:11:36 +0100 Subject: [PATCH 16/37] Remove internal episode references from comments Comments must state the mechanism, not how it was observed. Drop the environment, counter values and recording-size anecdotes; the factual claim each comment makes is unchanged. --- ddprof-lib/src/main/cpp/referenceChains.cpp | 10 ++++------ ddprof-lib/src/test/cpp/livenessTracker_ut.cpp | 4 ++-- ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc | 5 +---- ddprof-lib/src/test/cpp/referenceChainsOomTests.inc | 8 ++++---- .../src/test/cpp/referenceChainsTrackerTests.inc | 6 +++--- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index b125df970e..82c6c5b7c2 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -873,12 +873,10 @@ bool ReferenceChainTracker::hasLeakSignal() { // tagged the search can never emit a chain, and while urgent the 30-pass // no-progress abandonment is suppressed (runPass()'s terminal gate), so a // candidate-less search would run to the full auto-tuned TTL doing whole-heap - // BFS - pure cost. Observed in staging: a fresh process's warmup heap-floor - // rise tripped the 5-minute projection and a candidate-less search grew a - // 262,144-entry frontier (liveness_urgent_boost_admits=2495) with zero chains - // for the whole recording. Urgency still applies through the ramp (cadence, - // pause target, budget boost, 100% liveness admission) once a real candidate - // justifies the search. + // BFS - pure cost. A warmup heap-floor rise can trip the 5-minute projection + // with no tagged klass, so the refusal is unconditional, not a heuristic. + // Urgency still applies through the ramp (cadence, pause target, budget + // boost, 100% liveness admission) once a real candidate justifies the search. TEST_LOG_SUMMARY("ReferenceChainTracker::hasLeakSignal -> %s (secondsToOOM=%.1f, " "candidates=%d, urgent=%d)", n > 0 ? "true" : "false", seconds_to_oom, n, urgent); diff --git a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp index d04ccf0950..f276b0af07 100644 --- a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp +++ b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp @@ -1238,8 +1238,8 @@ TEST(LivenessTrackerSessionTest, ResetSessionClearsOnlyRecordingGates) { // hasLeakSignal() must never authorize a search without a leak-signal source: // with neither generations nor liveheap tracking active, the population table // and heap-floor ring never fill, and searching would run whole-heap BFS -// passes with zero candidates (observed in staging as a frontier table grown -// to its cap with reference_chain_candidate_count == 0 in a healthy process). +// passes with zero candidates, growing the frontier table to its cap while +// emitting nothing. TEST(LivenessTrackerSessionTest, LeakSignalRequiresGenerationsOrLiveness) { LivenessTracker *tracker = LivenessTracker::instance(); diff --git a/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc b/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc index d5bf85f16d..96c82590e2 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc @@ -898,10 +898,7 @@ TEST_F(ReferenceChainsBfsTest, CanaryLaneBacksOffWithoutProgressAndResetsOnProgr // With no leak-signal source active (neither generations nor liveheap // tracking), the collector must stay completely idle: no first pass and no // restart, ever. Searching without a signal runs whole-heap BFS passes with -// zero candidates - the exact waste the leak-signal gate exists to prevent -// (observed in staging: frontier grown to its cap with -// reference_chain_candidate_count == 0 in a healthy process, because the -// no-signal branch used to return true unconditionally). +// zero candidates - the exact waste the leak-signal gate exists to prevent. TEST_F(ReferenceChainsBfsTest, NoSearchWithoutLeakSignalSource) { Arguments args; ASSERT_FALSE(args.parse("referencechains=true")); diff --git a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc index 297d7e6b3c..2080bc541c 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc @@ -186,10 +186,10 @@ TEST_F(OomUrgencyTest, RelatchAfterReleaseGetsFreshEntitlement) { } // During the burn-in (OOM_PROJECTION_BURN_IN_NS after the leak signal first -// activated) no projection is offered, so urgency never latches: a fresh -// process's warmup growth curve projects an imminent OOM just as a real leak -// does (the staging incident: a fresh process latched urgency and ran a -// candidate-less whole-heap search to the TTL). +// activated) no projection is offered, so urgency never latches: a warmup +// growth curve projects an imminent OOM just as a real leak does, and a +// burn-in-less gate would latch urgency and start a candidate-less +// whole-heap search that runs to the TTL. TEST_F(OomUrgencyTest, BurnInDoesNotLatchUrgency) { LivenessTracker::instance()->setLeakSignalActivatedNsForTest(OS::nanotime()); // Earlier scenarios advanced the shared seeding clock far past this test's diff --git a/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc b/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc index 8980e096de..b43e48d6c9 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsTrackerTests.inc @@ -1398,9 +1398,9 @@ TEST_F(SearchRestartTest, PainBudgetBlocksARestartUntilItDrains) { // hasLeakSignal()'s urgent latch does NOT authorize a candidate-less search: with // nothing tagged the search can never emit a chain, and while urgent the 30-pass // no-progress abandonment is suppressed, so it would run to the full TTL doing -// whole-heap BFS (observed in staging: a warmup heap-floor rise latched urgency -// and a candidate-less search grew a 262,144-entry frontier for the whole -// recording). A search starts only when a klass clears selectLeakCandidates(); +// whole-heap BFS. A warmup heap-floor rise can latch urgency with nothing +// tagged, so the candidate requirement must be unconditional. A search starts +// only when a klass clears selectLeakCandidates(); // urgency then applies through the ramp. The burn-in stamp is backdated so // secondsToOOM()'s age-based burn-in does not mask the latch - this test // isolates the candidate requirement, not the burn-in. From 246f2adb041894c36590b6ba9de489753968ca95 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 15:19:27 +0100 Subject: [PATCH 17/37] Stabilize flaky ReferenceChainTrackingTest success paths (PROF-16383) The cache-leak and gc-root tests flake in-order because the search's root walk runs once per search (no re-visit of walked regions), the fixtures' HashMap/ArrayList resize churn invalidates the walked path mid-test, and the BFS thread's own backoff/cooldown scheduling leaves too few passes to retry discovery. Fix, test-only: - Pre-size the fixture cache (1<<17) so the walked table/Node path never dies to resize churn. - Drive bounded passes via the runReferenceChainPass0() debug seams in both success-path tests' retry loops (release stubs are no-ops), so discovery depends on cumulative frontier progress, not BFS cadence. - Widen both grace loops to 10 attempts; add failure diagnostics (leak-signal candidates, BFS pass count, representative frontier state, observed chain[0]s) plus the observedChainHeads() helper. - Correct stale comments (dump() drain semantics, nonexistent writeReferenceChain symbol, MIN_EFFECTIVE_BUDGET floor). Verified: 6/6 + 7/7 forced genuine in-order sweeps (cleanTestSlowDebug), isolated pass; the unmodified baseline reproduces the original flake. --- .../ReferenceChainAssertions.java | 26 +++ .../ReferenceChainTrackingTest.java | 215 ++++++++++++++---- 2 files changed, 196 insertions(+), 45 deletions(-) diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainAssertions.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainAssertions.java index 28717b4046..36b5295202 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainAssertions.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainAssertions.java @@ -154,6 +154,32 @@ private static ChainMatch findMatch(IItemCollection events, Class targetClass return null; } + /** + * {@code chain[0]} (full name, same notation as {@link JfrChainMatch#chain}) of every observed + * {@code datadog.ReferenceChain} event in {@code events}, in file order. Diagnostic helper for + * failure messages: a non-empty result whose heads all differ from the target class means the + * candidate mechanism fired for OTHER klasses, while an empty result means no chain was ever + * built. + */ + public static List observedChainHeads(JfrEvents events) { + List heads = new ArrayList<>(); + if (events == null || !events.hasItems()) { + return heads; + } + for (JfrEvent item : events) { + Object chainValue = item.get("chain"); + if (!(chainValue instanceof Object[])) { + continue; + } + Object[] rawChain = (Object[]) chainValue; + if (rawChain.length == 0) { + continue; + } + heads.add(classFullName(rawChain[0])); + } + return heads; + } + /** * jafar/{@code JfrEvents}-backed counterpart to {@link #findMatchForClass(IItemCollection, Class)} - * see this class's own header comment for why these are two separate overloads rather than one. diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java index b13b7478b0..2f25d8cc95 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java @@ -24,6 +24,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -51,8 +52,10 @@ * rolling per-klass survivor-count window, gated on the effective leak signal - generations OR * liveheap tracking) and, for each ranked * klass whose representative instance an ordinary {@code runPass()} walk has *already* tagged - * ({@code getTag() > 0} - a read, never a {@code SetTag} seed), reconstructs and emits its chain - * via {@code Profiler::writeReferenceChain()}. {@link #shouldReconstructReferrerChainToGcRoot()} + * ({@code getTag() > 0} - a read, never a {@code SetTag} seed), reconstructs and caches its chain + * ({@code buildChainEvent()}/{@code cacheResolvedChain()}); {@code Profiler::dump()} copies that + * cache into the recording via {@code FlightRecorder::recordReferenceChain()}. + * {@link #shouldReconstructReferrerChainToGcRoot()} * exercises this end to end against a real JVM, real GCs, and a real JVMTI heap walk - the * synthetic-frontier reconstruction-correctness proof lives in * {@code referenceChainJfrRoundtrip_ut.cpp}/{@code ReferenceChainJfrParserTest}. @@ -146,17 +149,28 @@ protected String getProfilerCommand() { // root-reachable graph, and a long ttl avoids a TTL abandonment mid-test - either would call // releaseSearchTags() and permanently zero every tag this test depends on. // + // budget=4000: each pass admits at most this many new frontier edges, resuming the persisted + // frontier from the previous pass. A fully-populated 69k-entry cache puts its CachedPayload + // values several hundred thousand admitted edges deep into the walk (roughly three edges per + // entry - Node, key, value - plus the test's own 69k-element keys array hanging off the + // same stack root), and the traversal position of that subtree varies with heap layout - so + // the number of passes (not the per-pass budget) decides when the leak-tag interception and + // the representative tagging finally see a CachedPayload. The retry loops below therefore + // drive bounded passes via the runReferenceChainPass0() seam instead of relying on the BFS + // thread's own cadence, and the budget stays small so each driven pass is cheap. + // // pausetarget=5000: the pacing controller measures this JVM's real FollowReferences call // latency at ~150ms early on, climbing past a full second as this test's own allocation // keeps handing the search more to discover (each pass re-scans the whole already-discovered // set, so cost grows with cumulative progress, not with this test's allocation rate). Against // the default 5ms ceiling every observed pass is "over ceiling even at the floor", so the - // controller settles at MIN_EFFECTIVE_BUDGET=50 edges/pass and 4s between passes - a real but - // glacial rate this test's wall-clock budget cannot outwait, and one a merely-generous - // ceiling (e.g. 500ms) only postpones: once cumulative progress pushes the real per-pass cost - // past *that* ceiling too, the same throttling recurs. A ceiling comfortably above the - // highest per-call cost this test's population growth is expected to reach lets the - // controller keep the configured budget=4000 for the whole run. + // controller collapses to its MIN_EFFECTIVE_BUDGET floor (2000 edges/pass) and widens the + // gap between passes up to 4s - a real but glacial rate this test's wall-clock budget cannot + // outwait, and one a merely-generous ceiling (e.g. 500ms) only postpones: once cumulative + // progress pushes the real per-pass cost past *that* ceiling too, the same throttling + // recurs. A ceiling comfortably above the highest per-call cost this test's population + // growth is expected to reach lets the controller keep the configured budget (plus its + // borrowing headroom) for the whole run. // // painbudget=100: the cache-leak test shares this singleton search and needs a restart once // the first search reaches a terminal state. PainBudget gates that restart on a cooldown of @@ -164,13 +178,14 @@ protected String getProfilerCommand() { // search this large - far past this test's bounded retry loop. Nothing else in this JVM // competes for the pain budget, so keep the gate affordable immediately. // - // firstpassbudget=200000: root/stack-ref enumeration shares budget=4000 with expandFrontier() - // unless overridden - and in this JVM's real root-reachable graph that left this method's own - // cache HashMap (a direct stack-local root) at the mercy of whatever order JVMTI enumerates - // roots in: budget=4000 was observed exhausting itself on other roots' subtrees every single - // attempt, never once admitting a single edge from this thread's own stack. Overriding just - // root enumeration's budget gives every enumeration attempt enough headroom to reach this - // thread's stack without slowing the steady-state expansion passes that follow. + // firstpassbudget=200000: root/stack-ref enumeration gets its own budget, separate from + // expandFrontier()'s per-pass edge budget - in this JVM's real root-reachable graph a small + // root-enumeration budget left this method's own cache HashMap (a direct stack-local root) + // at the mercy of whatever order JVMTI enumerates roots in: the whole budget was observed + // exhausting itself on other roots' subtrees every single attempt, never once admitting a + // single edge from this thread's own stack. Giving root enumeration its own generous budget + // lets every enumeration attempt reach this thread's stack without slowing the steady-state + // expansion passes that follow. // memory=64:l:1.0 - the explicit 100% live-sample ratio is load-bearing: without a ratio // segment the default is 10% and LivenessTracker::track() drops 90% of tracked instances // probabilistically, thinning the pool the representative-minting and candidate trend rings @@ -346,10 +361,10 @@ public void shouldReconstructReferrerChainToGcRoot() throws Exception { // forked test JVM every other ddprof-test class shares (-Xmx512m, no forkEvery) - a prior // CI run hit "Java heap space" with the cap at 25. // - // Per-round growth is clamped to round 10: rounds past 10 exist only to give - // pollWatchedTargets() more retries against the lock-contention race below, not to keep - // building the population trend (already eligible by round 10) - unclamped rounds 11-16 - // each add strictly more retained garbage for no trend benefit. + // Per-round growth is clamped to round 10: rounds past 10 exist only to give the BFS thread + // more scheduling cycles to tag the representative and cache its chain (see the retry + // comment below), not to keep building the population trend (already eligible by round 10) + // - unclamped rounds 11-16 each add strictly more retained garbage for no trend benefit. // // Instance count is sized against ObjectSampler::check(), not this test's "memory=64" // request: the *actual* sampling interval is 256KiB regardless of the small value requested @@ -428,12 +443,19 @@ public void shouldReconstructReferrerChainToGcRoot() throws Exception { } if (match == null) { - // A quiet window with no dump() in flight: Profiler::dump() holds every entry of _locks[] - // exclusively for its own _jfr.dump() call - the same array writeReferenceChain() needs a - // slot from. Back-to-back rounds leave pollWatchedTargets() little to no window to win - // that race; sleeping briefly here, then dumping again with no intervening gc() (so - // cleanup_table()'s epoch-advance pass stays a no-op and the population trend is - // undisturbed), gives it one. + // A chain only reaches a recording after the BFS thread's passes have tagged the + // representative (or leak-tag-correlated a discovered instance) and cached it in + // _resolved_chains - Profiler::dump() copies that cache out itself, so once cached, the + // very next dump emits the event. Sleeping briefly here, then dumping again with no + // intervening gc() (so cleanup_table()'s epoch-advance pass stays a no-op and the + // population trend is undisturbed) buys wall-clock for that; the debug-only seam passes + // make the frontier progress deterministic on top (same rationale as the cache-leak + // test's grace loop below - this fixture's own ArrayList resizes invalidate the walked + // element path the same way, and the BFS thread's pass availability is governed by its + // own backoff/pain-budget scheduling). + if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { + driveReferenceChainPasses(2); + } Thread.sleep(300); dump(scratchDumpPath); match = ReferenceChainAssertions.findMatchForClass(verifyEvents(scratchDumpPath, "datadog.ReferenceChain", false), ChainLink.class); @@ -441,18 +463,42 @@ public void shouldReconstructReferrerChainToGcRoot() throws Exception { } // Grace period: the trend only became eligible (ring_fill >= 10) partway through the loop - // above, and the same lock contention can still delay the write past this method's last - // dump() - retry a few more times before concluding the mechanism genuinely did not fire. - for (int attempt = 0; match == null && attempt < 5; attempt++) { + // above, and the BFS thread may still need a few more pass/schedule cycles to tag the + // representative and cache its chain past this method's last round - retry a few more times + // before concluding the mechanism genuinely did not fire. Each attempt also drives bounded + // passes via the runReferenceChainPass0() seam (see the cache-leak test's grace-loop comment + // for why that is load-bearing; the seam stubs are no-ops in release builds). Ten attempts, + // matching the cache-leak test's loop: this fixture's ArrayList resizes churn the walked + // element path just like the cache's HashMap resizes, so late-round discovery needs the + // same scheduling-independent margin. + JfrEvents lastEvents = null; + for (int attempt = 0; match == null && attempt < 10; attempt++) { + if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { + driveReferenceChainPasses(8); + } Thread.sleep(1000); dump(scratchDumpPath); - match = ReferenceChainAssertions.findMatchForClass(verifyEvents(scratchDumpPath, "datadog.ReferenceChain", false), ChainLink.class); + lastEvents = verifyEvents(scratchDumpPath, "datadog.ReferenceChain", false); + match = ReferenceChainAssertions.findMatchForClass(lastEvents, ChainLink.class); } + // Same failure diagnostics as the cache-leak test's assertion below: empty leak-signal + // candidates mean the trend gate never opened; a non-empty list with no cached chain means + // the walk never admitted/tagged the representative. Debug-only seams; release failures + // keep the plain message. + String failureDiagnostics = ""; + if (match == null && ReferenceChainAssertions.isDebugInstrumentedBuild()) { + failureDiagnostics = " [leak-signal candidates=" + + Arrays.toString(ReferenceChainTestHooks.selectLeakCandidateKlassIds0()) + + ", bfsPassesRun=" + ReferenceChainTestHooks.referenceChainPassesRunForTest0() + + ", representativeFrontierState=" + + ReferenceChainTestHooks.getReferenceChainFrontierStateForTest0(gcRootHolder.get(0)) + + " (-1=never admitted, 0=admitted pending expansion, 1=expanded, 3=abandoned)]"; + } assertNotNull(match, "Never observed a datadog.ReferenceChain event whose chain[0] is " + ChainLink.class + " after " + gcRootHolder.size() + " retained ChainLink instances across up to " - + totalRounds + " population-growth rounds plus a grace period"); + + totalRounds + " population-growth rounds plus a grace period" + failureDiagnostics); // chain[0] is the *target* object's own class, not its holder's: reconstructChain() appends // each visited FrontierEntry's referrer_klass leaf-to-root, and referrer_klass is populated // from the *discovered object's own* GetClassSignature at insertion time. The target here is @@ -496,11 +542,19 @@ public void shouldReconstructReferrerChainToGcRoot() throws Exception { @Test @Order(2) public void shouldReconstructReferrerChainThroughUnboundedCacheLeak() throws Exception { - // Seed cache with one live entry *before* the resets below: HashMap's backing table also - // starts as a shared, lazily-replaced empty sentinel (allocated on the first put()), so the - // same one-shot-BFS-freeze hazard documented in shouldReconstructReferrerChainToGcRoot() - // applies here too. - Map cache = new HashMap<>(); + // Pre-sized to hold every round's entries without ever crossing HashMap's 0.75 load factor: + // 131072 buckets hold up to 98304 entries, and this fixture caps at 69001 + the seed below. + // This is load-bearing, not a tuning nicety. Every ordinary resize replaces the table and + // rehashes every entry into NEW HashMap$Node objects - the table/Node path the BFS walk + // admitted (and the reconstructed chain routes through) dies, the walk prunes the retired + // entries, and the replacement table is never admitted because the cache HashMap's own + // frontier entry is already EXPANDED (the frozen-children hazard documented in the class + // javadoc above) - traced live as a representative permanently stuck at frontier state -1 + // (never admitted / entry pruned) with zero leak-tag interceptions for the rest of the + // search. A fixed table keeps every Node ever admitted (and the representative's parent + // chain) alive for the whole test, so the representative path and the rotation re-walk see a + // stable, growing container. + Map cache = new HashMap<>(1 << 17); cache.put("cache-leak-seed", new CachedPayload("cache-leak-seed")); if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { // Force a genuine fresh search rather than depend on the natural restartSearch() cycle being @@ -618,9 +672,17 @@ public void shouldReconstructReferrerChainThroughUnboundedCacheLeak() throws Exc } if (match == null) { - // Same lock-contention race shouldReconstructReferrerChainToGcRoot()'s own comment - // describes - a quiet retry gives pollWatchedTargets() a window to win a _locks[] - // slot against Profiler::dump()'s own exclusive hold. + // Same retry rationale as shouldReconstructReferrerChainToGcRoot()'s: a chain only + // reaches a recording after the BFS thread's passes have tagged the representative (or + // leak-tag-correlated a discovered instance) and cached it in _resolved_chains - and the + // BFS thread's own cadence is not something this test controls (see the pass-driving + // comment in the grace loop below). Sleeping briefly, then dumping again with no + // intervening gc() (so cleanup_table()'s epoch-advance pass stays a no-op and the + // population trend is undisturbed) gives it a window; the debug-only seam passes make + // the frontier progress deterministic on top. + if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { + driveReferenceChainPasses(2); + } Thread.sleep(300); dump(scratchDumpPath); JfrEvents events3 = verifyEvents(scratchDumpPath, "datadog.ReferenceChain", false); @@ -628,17 +690,57 @@ public void shouldReconstructReferrerChainThroughUnboundedCacheLeak() throws Exc } } - for (int attempt = 0; match == null && attempt < 5; attempt++) { + // Grace period, same shape as the gc-root test's: ten 1s attempts cover the slower + // scheduling of a loaded machine. lastEvents keeps the final dump's parsed events for the + // failure diagnostics below. + // + // Each attempt also drives bounded passes via the runReferenceChainPass0() seam (debug + // builds only; the release stubs are no-ops and the loop degrades to the organic cadence). + // This is load-bearing, not an accelerator: a chain only reaches a recording once the walk + // has admitted the tagged instances' heap region, and how many admitted edges that takes + // varies with heap layout (this fixture's own 69k-element keys array hangs off the same + // stack root as the cache). The BFS thread's pass availability, in turn, is governed by its + // own scheduling - canary-backoff spacing escalates once a pass stops growing the frontier, + // a drained frontier can complete the search, and a completed search restarts only after + // the pain-budget cooldown - any of which can stall passes for tens of seconds on a graph + // this size (trace evidence of failed runs shows bfsPassesRun=3 for an entire test + // duration). Driving passes here makes discovery depend on cumulative frontier progress, + // which is deterministic, instead of on that scheduling. pollReferenceChainTargets0() after + // each pass is what tags the candidate's tracked instances and polls the representative - + // the same per-iteration tail the BFS thread runs. + JfrEvents lastEvents = null; + for (int attempt = 0; match == null && attempt < 10; attempt++) { + if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { + driveReferenceChainPasses(8); + } Thread.sleep(1000); dump(scratchDumpPath); - JfrEvents events4 = verifyEvents(scratchDumpPath, "datadog.ReferenceChain", false); - match = ReferenceChainAssertions.findMatchForClass(events4, CachedPayload.class); + lastEvents = verifyEvents(scratchDumpPath, "datadog.ReferenceChain", false); + match = ReferenceChainAssertions.findMatchForClass(lastEvents, CachedPayload.class); } + // On failure, distinguish the ways this test can come up empty: the leak-candidate gate + // never opened (empty leak-signal candidates - the seeded/organic population trend failed to + // qualify), the gate opened but the walk never admitted the representative (frontier state + // -1 - the cumulative-frontier-progress shortfall the seam-driven passes above exist to + // prevent), or chains were emitted for a DIFFERENT klass (observedChain0s non-empty - the + // candidate mechanism picked another growing klass, e.g. an incidental byte[]/String trend, + // and CachedPayload's own tags were never intercepted). Debug-only seams: release-build + // stubs cannot report any of these states, so release failures keep the plain message. + String failureDiagnostics = ""; + if (match == null && ReferenceChainAssertions.isDebugInstrumentedBuild()) { + failureDiagnostics = " [leak-signal candidates=" + + Arrays.toString(ReferenceChainTestHooks.selectLeakCandidateKlassIds0()) + + ", bfsPassesRun=" + ReferenceChainTestHooks.referenceChainPassesRunForTest0() + + ", representativeFrontierState=" + + ReferenceChainTestHooks.getReferenceChainFrontierStateForTest0(cache.get(keys[0])) + + " (-1=never admitted, 0=admitted pending expansion, 1=expanded, 3=abandoned)" + + ", observedChain0s=" + ReferenceChainAssertions.observedChainHeads(lastEvents) + "]"; + } assertNotNull(match, "Never observed a datadog.ReferenceChain event whose chain[0] is " + CachedPayload.class + " after " + cache.size() + " cached entries across up to " + totalRounds - + " population-growth rounds plus a grace period"); + + " population-growth rounds plus a grace period" + failureDiagnostics); assertEquals(CachedPayload.class.getName(), match.chain.get(0)); assertTrue(match.targetTag > 0, "targetTag should be a valid, non-zero JVMTI tag"); assertTrue(match.depth >= 0, "depth should be a non-negative hop count"); @@ -672,9 +774,9 @@ public void shouldReconstructReferrerChainThroughUnboundedCacheLeak() throws Exc * object is discovered, so {@code runPass()} abandons the search rather than silently * truncating it - or, if this method lost the race to size the shared {@code FrontierTable}, * its {@code ttl=100} fallback abandons the search instead (by the time this method runs, that - * clock always has). Either path exercises the same {@code buildAbandonedEvent()}/ - * {@code Profiler::writeReferenceChainAbandoned()}/{@code recordReferenceChainAbandoned()} - * write triggered by {@code Profiler::dump()} - this method only asserts that a + * clock always has). Either path exercises the same {@code buildAbandonedEvent()} → + * {@code FlightRecorder::recordReferenceChainAbandoned()} write triggered by + * {@code Profiler::dump()} - this method only asserts that a * {@code datadog.ReferenceChainAbandoned} event exists, not which reason produced it. * *

{@code @Order(3)}: this permanently exhausts the singleton's one-and-only search, so it @@ -837,6 +939,29 @@ private static void awaitAllocatorTid(Thread allocator, AtomicLong allocatorTid, } } + /** + * Runs up to {@code maxPasses} bounded reference-chain search passes synchronously via the + * debug-only {@link ReferenceChainTestHooks#runReferenceChainPass0()} seam, each followed by the + * per-iteration target poll ({@link ReferenceChainTestHooks#pollReferenceChainTargets0()}) - + * the same pass + poll pair one BFS-thread loop iteration performs. A no-op in release builds + * (the seam stubs return immediately); release discovery falls back to the organic BFS-thread + * cadence. + * + *

Passes against a terminal (completed/abandoned) search are no-ops until the BFS thread's + * shouldRunPass() restarts the search - the restart gate belongs to that thread - so a burst of + * seam passes never fights the search lifecycle; it only front-loads the frontier progress the + * BFS thread's own cadence may otherwise deliver late or never (see the grace-loop comment in + * {@link #shouldReconstructReferrerChainThroughUnboundedCacheLeak()}). + */ + private static void driveReferenceChainPasses(int maxPasses) { + for (int i = 0; i < maxPasses; i++) { + if (!ReferenceChainTestHooks.runReferenceChainPass0()) { + return; // reference chains disabled or the tracker never started + } + ReferenceChainTestHooks.pollReferenceChainTargets0(); + } + } + /** Bounded wait for one allocator round; same fail-fast policy as awaitAllocatorTid(). */ private static void awaitAllocatorRound(Semaphore roundDone, Thread allocator, AtomicReference allocatorFailure) throws InterruptedException { From a4db135a725c5b1e31b7cb46962e6eec893f7621 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 16:11:50 +0100 Subject: [PATCH 18/37] Centralize reference-chain failure diagnostics per review findings Review follow-ups (sphinx, 4 accepted LOW findings): - Extract ReferenceChainAssertions.referenceChainFailureDiagnostics(): one canonical failure fingerprint shared by both discovery tests, closing the gc-root test's missing observedChain0s segment. - Move the debug-build gate inside driveReferenceChainPasses(); call sites no longer repeat it. - observedChainHeads() now reports / sentinels instead of silently skipping malformed events; javadoc matches. - Pin the fingerprint format in ReferenceChainTestSeamsTest. --- .../ReferenceChainAssertions.java | 43 +++++++++-- .../ReferenceChainTestSeamsTest.java | 35 +++++++++ .../ReferenceChainTrackingTest.java | 73 ++++++++----------- 3 files changed, 104 insertions(+), 47 deletions(-) diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainAssertions.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainAssertions.java index 36b5295202..aa6f0ba7b1 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainAssertions.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainAssertions.java @@ -8,6 +8,7 @@ import com.datadoghq.profiler.JfrEvent; import com.datadoghq.profiler.JfrEvents; import com.datadoghq.profiler.NativeBuildConfig; +import com.datadoghq.profiler.ReferenceChainTestHooks; import org.openjdk.jmc.common.IMCType; import org.openjdk.jmc.common.item.IAccessorKey; import org.openjdk.jmc.common.item.IItem; @@ -18,6 +19,7 @@ import org.openjdk.jmc.common.unit.IQuantity; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; @@ -42,6 +44,9 @@ private ReferenceChainAssertions() {} public static final long LEAK_TAG_POOL_SIZE = 256; + /** {@link #observedChainHeads} entry for a {@code datadog.ReferenceChain} event with an empty chain. */ + private static final String EMPTY_CHAIN_SENTINEL = ""; + /** * True on every native build that compiles javaApi.cpp's {@code #ifdef DEBUG} test seams: debug, * coverage, asan and tsan (all add -DDEBUG, ConfigurationPresets.kt). Release builds get no-op seam stubs. @@ -155,11 +160,14 @@ private static ChainMatch findMatch(IItemCollection events, Class targetClass } /** - * {@code chain[0]} (full name, same notation as {@link JfrChainMatch#chain}) of every observed - * {@code datadog.ReferenceChain} event in {@code events}, in file order. Diagnostic helper for - * failure messages: a non-empty result whose heads all differ from the target class means the - * candidate mechanism fired for OTHER klasses, while an empty result means no chain was ever - * built. + * One entry per observed {@code datadog.ReferenceChain} event in {@code events}, in file order: + * {@code chain[0]} (full name, same notation as {@link JfrChainMatch#chain}), or the sentinel + * {@code ""} for an event with an empty {@code chain} array (or + * {@code ""} if the field did not resolve to an array). Diagnostic helper for + * failure messages: an empty result means no chain event was ever emitted; only sentinels means + * events were emitted but carried no chain; real heads that all differ from the target class mean + * the candidate mechanism fired for OTHER klasses. Never throws on malformed events, unlike + * {@link #findMatchForClass(JfrEvents, Class)}. */ public static List observedChainHeads(JfrEvents events) { List heads = new ArrayList<>(); @@ -169,10 +177,12 @@ public static List observedChainHeads(JfrEvents events) { for (JfrEvent item : events) { Object chainValue = item.get("chain"); if (!(chainValue instanceof Object[])) { + heads.add(""); continue; } Object[] rawChain = (Object[]) chainValue; if (rawChain.length == 0) { + heads.add(EMPTY_CHAIN_SENTINEL); continue; } heads.add(classFullName(rawChain[0])); @@ -180,6 +190,29 @@ public static List observedChainHeads(JfrEvents events) { return heads; } + /** + * Canonical debug-build failure fingerprint for a reference-chain discovery test that came up + * empty: empty leak-signal candidates mean the trend gate never opened; a non-empty list with + * representative frontier state -1 means the walk never admitted the representative; + * observedChain0s with real (non-sentinel) heads that all differ from the target mean chains + * fired for a DIFFERENT klass, while observedChain0s holding only sentinels means events were + * emitted but carried no chain (see {@link #observedChainHeads}). Returns "" on release builds - + * the seam stubs cannot report any of these states. Callers invoke it only when the match is + * missing, so the seam queries run on failure only. + */ + public static String referenceChainFailureDiagnostics(Object representative, JfrEvents lastEvents) { + if (!isDebugInstrumentedBuild()) { + return ""; + } + return " [leak-signal candidates=" + + Arrays.toString(ReferenceChainTestHooks.selectLeakCandidateKlassIds0()) + + ", bfsPassesRun=" + ReferenceChainTestHooks.referenceChainPassesRunForTest0() + + ", representativeFrontierState=" + + ReferenceChainTestHooks.getReferenceChainFrontierStateForTest0(representative) + + " (-1=never admitted, 0=admitted pending expansion, 1=expanded, 3=abandoned)" + + ", observedChain0s=" + observedChainHeads(lastEvents) + "]"; + } + /** * jafar/{@code JfrEvents}-backed counterpart to {@link #findMatchForClass(IItemCollection, Class)} - * see this class's own header comment for why these are two separate overloads rather than one. diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTestSeamsTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTestSeamsTest.java index bf8c073c06..c86a1aaf58 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTestSeamsTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTestSeamsTest.java @@ -10,6 +10,7 @@ import com.datadoghq.profiler.ReferenceChainTestHooks; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -159,4 +160,38 @@ public void shouldReconstructChainForDirectlyTaggedRoot() { chainTargetHolder = null; // shared-JVM hygiene } } + + /** The shared discovery-test failure fingerprint is empty on release builds (the gate lives in + * the helper) and, on debug builds, carries the canonical segments in the canonical order. Read + * only on the seams - no reset/seed calls. */ + @Test + public void failureDiagnosticsFingerprintHasCanonicalSegments() { + String d = ReferenceChainAssertions.referenceChainFailureDiagnostics(new Object(), null); + if (!ReferenceChainAssertions.isDebugInstrumentedBuild()) { + assertEquals("", d, "release builds must keep the plain failure message"); + return; + } + assertTrue(d.startsWith(" [leak-signal candidates=["), d); + assertTrue(d.endsWith(", observedChain0s=[]]"), d); + String[] segments = { + "leak-signal candidates=", + ", bfsPassesRun=", + ", representativeFrontierState=", + " (-1=never admitted, 0=admitted pending expansion, 1=expanded, 3=abandoned)", + ", observedChain0s=" + }; + int previous = -1; + for (String segment : segments) { + int index = d.indexOf(segment); + assertTrue(index > previous, "segment '" + segment + "' missing or out of order in: " + d); + previous = index; + } + assertTrue(d.matches("(?s).*representativeFrontierState=-?\\d+ \\(-1=never admitted.*"), d); + } + + /** observedChainHeads tolerates absent events: never throws, never returns null. */ + @Test + public void observedChainHeadsIsEmptyForNoEvents() { + assertTrue(ReferenceChainAssertions.observedChainHeads(null).isEmpty()); + } } diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java index 2f25d8cc95..be68959a03 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java @@ -24,7 +24,6 @@ import java.util.concurrent.atomic.AtomicReference; import java.nio.file.Path; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -453,9 +452,7 @@ public void shouldReconstructReferrerChainToGcRoot() throws Exception { // test's grace loop below - this fixture's own ArrayList resizes invalidate the walked // element path the same way, and the BFS thread's pass availability is governed by its // own backoff/pain-budget scheduling). - if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { - driveReferenceChainPasses(2); - } + driveReferenceChainPasses(2); Thread.sleep(300); dump(scratchDumpPath); match = ReferenceChainAssertions.findMatchForClass(verifyEvents(scratchDumpPath, "datadog.ReferenceChain", false), ChainLink.class); @@ -473,28 +470,24 @@ public void shouldReconstructReferrerChainToGcRoot() throws Exception { // same scheduling-independent margin. JfrEvents lastEvents = null; for (int attempt = 0; match == null && attempt < 10; attempt++) { - if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { - driveReferenceChainPasses(8); - } + driveReferenceChainPasses(8); Thread.sleep(1000); dump(scratchDumpPath); lastEvents = verifyEvents(scratchDumpPath, "datadog.ReferenceChain", false); match = ReferenceChainAssertions.findMatchForClass(lastEvents, ChainLink.class); } - // Same failure diagnostics as the cache-leak test's assertion below: empty leak-signal - // candidates mean the trend gate never opened; a non-empty list with no cached chain means - // the walk never admitted/tagged the representative. Debug-only seams; release failures - // keep the plain message. - String failureDiagnostics = ""; - if (match == null && ReferenceChainAssertions.isDebugInstrumentedBuild()) { - failureDiagnostics = " [leak-signal candidates=" - + Arrays.toString(ReferenceChainTestHooks.selectLeakCandidateKlassIds0()) - + ", bfsPassesRun=" + ReferenceChainTestHooks.referenceChainPassesRunForTest0() - + ", representativeFrontierState=" - + ReferenceChainTestHooks.getReferenceChainFrontierStateForTest0(gcRootHolder.get(0)) - + " (-1=never admitted, 0=admitted pending expansion, 1=expanded, 3=abandoned)]"; - } + // Same failure diagnostics as the cache-leak test's assertion below (one shared helper, + // ReferenceChainAssertions.referenceChainFailureDiagnostics): empty leak-signal candidates + // mean the trend gate never opened; a non-empty list with representative frontier state -1 + // means the walk never admitted the representative; observedChain0s (from lastEvents, the + // final grace-loop dump) with real (non-sentinel) heads that all differ from ChainLink means + // chains fired for a different klass, while only / + // sentinels mean events were emitted but carried no chain. Debug-only seams; release + // failures keep the plain message. + String failureDiagnostics = match == null + ? ReferenceChainAssertions.referenceChainFailureDiagnostics(gcRootHolder.get(0), lastEvents) + : ""; assertNotNull(match, "Never observed a datadog.ReferenceChain event whose chain[0] is " + ChainLink.class + " after " + gcRootHolder.size() + " retained ChainLink instances across up to " @@ -680,9 +673,7 @@ public void shouldReconstructReferrerChainThroughUnboundedCacheLeak() throws Exc // intervening gc() (so cleanup_table()'s epoch-advance pass stays a no-op and the // population trend is undisturbed) gives it a window; the debug-only seam passes make // the frontier progress deterministic on top. - if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { - driveReferenceChainPasses(2); - } + driveReferenceChainPasses(2); Thread.sleep(300); dump(scratchDumpPath); JfrEvents events3 = verifyEvents(scratchDumpPath, "datadog.ReferenceChain", false); @@ -710,9 +701,7 @@ public void shouldReconstructReferrerChainThroughUnboundedCacheLeak() throws Exc // the same per-iteration tail the BFS thread runs. JfrEvents lastEvents = null; for (int attempt = 0; match == null && attempt < 10; attempt++) { - if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { - driveReferenceChainPasses(8); - } + driveReferenceChainPasses(8); Thread.sleep(1000); dump(scratchDumpPath); lastEvents = verifyEvents(scratchDumpPath, "datadog.ReferenceChain", false); @@ -723,20 +712,17 @@ public void shouldReconstructReferrerChainThroughUnboundedCacheLeak() throws Exc // never opened (empty leak-signal candidates - the seeded/organic population trend failed to // qualify), the gate opened but the walk never admitted the representative (frontier state // -1 - the cumulative-frontier-progress shortfall the seam-driven passes above exist to - // prevent), or chains were emitted for a DIFFERENT klass (observedChain0s non-empty - the - // candidate mechanism picked another growing klass, e.g. an incidental byte[]/String trend, - // and CachedPayload's own tags were never intercepted). Debug-only seams: release-build - // stubs cannot report any of these states, so release failures keep the plain message. - String failureDiagnostics = ""; - if (match == null && ReferenceChainAssertions.isDebugInstrumentedBuild()) { - failureDiagnostics = " [leak-signal candidates=" - + Arrays.toString(ReferenceChainTestHooks.selectLeakCandidateKlassIds0()) - + ", bfsPassesRun=" + ReferenceChainTestHooks.referenceChainPassesRunForTest0() - + ", representativeFrontierState=" - + ReferenceChainTestHooks.getReferenceChainFrontierStateForTest0(cache.get(keys[0])) - + " (-1=never admitted, 0=admitted pending expansion, 1=expanded, 3=abandoned)" - + ", observedChain0s=" + ReferenceChainAssertions.observedChainHeads(lastEvents) + "]"; - } + // prevent), chains were emitted for a DIFFERENT klass (observedChain0s holds real, + // non-sentinel heads that all differ from CachedPayload - the candidate mechanism picked + // another growing klass, e.g. an incidental byte[]/String trend, and CachedPayload's own tags + // were never intercepted), or events were emitted but carried no chain (observedChain0s holds + // only / sentinels). The fingerprint comes from the shared + // ReferenceChainAssertions.referenceChainFailureDiagnostics helper (same as the gc-root + // test's). Debug-only seams: release-build stubs cannot report any of these states, so + // release failures keep the plain message. + String failureDiagnostics = match == null + ? ReferenceChainAssertions.referenceChainFailureDiagnostics(cache.get(keys[0]), lastEvents) + : ""; assertNotNull(match, "Never observed a datadog.ReferenceChain event whose chain[0] is " + CachedPayload.class + " after " + cache.size() + " cached entries across up to " + totalRounds @@ -944,8 +930,8 @@ private static void awaitAllocatorTid(Thread allocator, AtomicLong allocatorTid, * debug-only {@link ReferenceChainTestHooks#runReferenceChainPass0()} seam, each followed by the * per-iteration target poll ({@link ReferenceChainTestHooks#pollReferenceChainTargets0()}) - * the same pass + poll pair one BFS-thread loop iteration performs. A no-op in release builds - * (the seam stubs return immediately); release discovery falls back to the organic BFS-thread - * cadence. + * (checked here, so callers need not gate it); release discovery falls back to the organic + * BFS-thread cadence. * *

Passes against a terminal (completed/abandoned) search are no-ops until the BFS thread's * shouldRunPass() restarts the search - the restart gate belongs to that thread - so a burst of @@ -954,6 +940,9 @@ private static void awaitAllocatorTid(Thread allocator, AtomicLong allocatorTid, * {@link #shouldReconstructReferrerChainThroughUnboundedCacheLeak()}). */ private static void driveReferenceChainPasses(int maxPasses) { + if (!ReferenceChainAssertions.isDebugInstrumentedBuild()) { + return; // release builds: the seams are no-op stubs; discovery uses the organic BFS cadence + } for (int i = 0; i < maxPasses; i++) { if (!ReferenceChainTestHooks.runReferenceChainPass0()) { return; // reference chains disabled or the tracker never started From 35fea363d7135d5f6be8430b368915477433e829 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 18:09:50 +0100 Subject: [PATCH 19/37] Address rkennke review findings on leak-signal gating and admission - Derive the leak signal from generations || (liveheap && referencechains): the collector is the signal's only consumer, so a plain liveheap session keeps its cheaper profile (no per-epoch klass folding, heap-floor ring or forced sweeps). - Seed the urgent-admission growth baseline at emergency-band entry so the first ramp-band observation compares against the episode entry level instead of re-snapshotting and dropping the boost mid-growth. - Advance the baseline per admitted sample (high-water mark): a plateau above the entry level loses the boost until growth resumes. - Guard gcGenerationsEnabled() as test-only; rewrite comments that narrated history or an internal observation instead of current behavior. - Add copyright headers to the files this PR modifies that lacked one. --- .../native/gtest/GtestTaskBuilder.kt | 4 + ddprof-lib/src/main/cpp/livenessTracker.cpp | 5 +- ddprof-lib/src/main/cpp/livenessTracker.h | 39 +++---- ddprof-lib/src/main/cpp/referenceChains.cpp | 26 +++-- ddprof-lib/src/main/cpp/referenceChains.h | 15 +-- .../src/test/cpp/livenessTracker_ut.cpp | 100 ++++++++++++------ .../cpp/referenceChainsLifecycleTests.inc | 4 + .../src/test/cpp/referenceChainsOomTests.inc | 57 ++++++++++ .../AggressiveLeakReferenceChainTest.java | 6 +- doc/ReferenceChainsCollectionSummary.md | 8 +- .../LiveHeapReferenceChainsImplementation.md | 13 ++- .../ReferenceChainsSignalsExplained.md | 2 +- 12 files changed, 193 insertions(+), 86 deletions(-) diff --git a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt index 5c2a561207..b267e0c037 100644 --- a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt +++ b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt @@ -1,3 +1,7 @@ +/* + * Copyright 2026, Datadog, Inc. + * SPDX-License-Identifier: Apache-2.0 + */ package com.datadoghq.native.gtest diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index 4c207bd152..2850a8d569 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -2132,8 +2132,9 @@ Error LivenessTracker::initialize(Arguments &args) { // observe the flag it was actually started with, even though the tracking // table itself persists across recordings. _gc_generations.store(args._gc_generations, std::memory_order_relaxed); - // See _leak_signal_enabled's own comment for why either source suffices. - _leak_signal_enabled.store(args._gc_generations || args._record_liveness, + // See _leak_signal_enabled's comment for the derivation. + _leak_signal_enabled.store(args._gc_generations + || (args._record_liveness && args._reference_chains), std::memory_order_relaxed); if (!_enabled) { diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index 876daf60d5..582713c572 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -464,15 +464,11 @@ class alignas(alignof(SpinLock)) LivenessTracker { // still be reading it from a session that persists across a restart. std::atomic _gc_generations; - // Effective leak-signal availability: true when the population-trend signal - // consumed by ReferenceChainTracker::hasLeakSignal() can be constructed at - // all. Generations tracking and liveheap tracking drive the same mechanisms - // here (per-klass population table, heap-floor ring, secondsToOOM() - // projection) - liveheap additionally preserves stack traces - so either - // option suffices; with neither on, hasLeakSignal() -> false and the - // reference-chain collector must stay idle instead of searching with zero - // candidates. Set from args._gc_generations || args._record_liveness in - // initialize(). Same atomicity/reader argument as _gc_generations above. + // True when a leak-signal source is active: generations, or liveheap with + // referencechains enabled - the collector is the signal's only consumer, so + // a plain liveheap session keeps the flag (and its per-epoch klass folding, + // heap-floor ring and forced sweeps) off. Derived in initialize(); same + // atomicity/reader argument as _gc_generations above. std::atomic _leak_signal_enabled; // OS::nanotime() of the first initialize() that enabled the leak signal; 0 @@ -1042,11 +1038,14 @@ class alignas(alignof(SpinLock)) LivenessTracker { // pattern selectLeakCandidates()/getLiveTraceIds() already use. jobject resolveCandidateRepresentative(JNIEnv *env, u32 klass_id); - // Read-only observation point for the explicit generations option (see - // _gc_generations's own comment); no production code keys on it. + // Test-only observation point for the explicit generations option: no + // production code keys on it (the gate is leakSignalEnabled()), so it is + // compiled out of the shipped release library. +#if defined(DEBUG) || defined(UNIT_TEST) bool gcGenerationsEnabled() const { return _gc_generations.load(std::memory_order_relaxed); } +#endif // The effective gate ReferenceChainTracker::hasLeakSignal() keys on (see // _leak_signal_enabled's own comment). @@ -1690,23 +1689,17 @@ class alignas(alignof(SpinLock)) LivenessTracker { // release library defines neither. // Sets the two leak-signal gates directly, bypassing initialize() (which - // requires a live JVM - VM::hotspot_version()/VM::jni(), see that method's - // own code - out of gtest's reach the same way foldKlassCountsLocked()'s - // representative-minting step is, per this seam block's own comment - // above), with the same derivation initialize() applies: - // _gc_generations = generations, _leak_signal_enabled = generations || - // liveness. (generations=false, liveness=true) models a liveheap-only - // session (memory=N:l), so gates that must key on leakSignalEnabled() - // rather than gcGenerationsEnabled() are distinguishable under test. + // requires a live JVM, out of gtest's reach - see this seam block's own + // comment above), with the same derivation initialize() applies. + // (generations=false, liveness=true) represents liveheap + referencechains + // - the only production-reachable state where liveness alone arms the signal. void setLeakSignalSourcesForTest(bool generations, bool liveness) { _gc_generations.store(generations, std::memory_order_relaxed); _leak_signal_enabled.store(generations || liveness, std::memory_order_relaxed); } - // Kept for existing callers (javaApi.cpp, the test fixtures) that only need - // to exercise leakSignalEnabled()'s gate: sets BOTH flags to v - the - // generations option alone is a sufficient signal source, so the effective - // flag follows it. + // Convenience for callers that only need the generations-only configuration: + // sets BOTH flags to v (the generations option alone arms the signal). void setGcGenerationsForTest(bool v) { setLeakSignalSourcesForTest(v, /*liveness=*/false); } // Overrides the burn-in stamp (see _leak_signal_activated_ns). initialize() diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index 82c6c5b7c2..77af09a90e 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -817,8 +817,8 @@ bool ReferenceChainTracker::isUrgent() const { bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool urgent) { if (!urgent) { - // Left set after an episode so the next episode re-snapshots against that - // episode's own entry state instead of inheriting a stale baseline. + // Cleared when the episode ends so the next episode re-snapshots against + // its own entry state instead of inheriting a stale baseline. _admission_growth_baseline = 0; return false; } @@ -827,6 +827,15 @@ bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool ur // evidence. OOM_URGENT_THRESHOLD_S is isUrgent()'s own latch threshold, so // the emergency band and the hysteretic urgent episode cover the same range. if (seconds_to_oom < OOM_URGENT_THRESHOLD_S) { + if (_admission_growth_baseline == 0) { + // Seed at emergency-band entry: the burn-in can hide the projection + // until it is already short, and without a seed the first ramp-band + // sample would re-snapshot and drop the boost mid-growth. + u64 newest_used = LivenessTracker::instance()->newestHeapFloorSample(); + if (newest_used != 0) { + _admission_growth_baseline = newest_used; + } + } return true; } u64 newest_used = LivenessTracker::instance()->newestHeapFloorSample(); @@ -843,7 +852,14 @@ bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool ur _admission_growth_baseline = newest_used; return false; } - return newest_used > _admission_growth_baseline; + // High-water mark: every admitted sample raises the baseline, so a heap + // that plateaus above the entry level loses the boost until growth resumes + // past the previous peak - a frozen entry snapshot would keep admitting it. + if (newest_used > _admission_growth_baseline) { + _admission_growth_baseline = newest_used; + return true; + } + return false; } bool ReferenceChainTracker::hasLeakSignal() { @@ -854,9 +870,7 @@ bool ReferenceChainTracker::hasLeakSignal() { // same effective flag). Stay idle: without a signal there is nothing to // chase, and running whole-heap searches with zero candidates "just in // case" burns safepoint time and frontier-table memory for zero - // diagnostic value (observed: frontier grown to its cap with - // reference_chain_candidate_count == 0 in a healthy process when this - // branch used to return true). + // diagnostic value. return false; } double seconds_to_oom = LivenessTracker::instance()->secondsToOOM(); diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index 9e2ba53637..887892e274 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -348,13 +348,14 @@ class ReferenceChainTracker { // least-squares regression over a 30-sample window, so a single spike sample // keeps the projection inside the ramp band for ~6-7 recovery GCs AFTER the // heap has physically stopped growing - band residence alone does not - // establish growth. updateUrgentAdmission() therefore snapshots the newest - // heap-floor sample's used bytes when a non-emergency ramp episode begins - // and admits the 100% tracking boost only while a newer sample has grown - // past that snapshot; a recovered spike never does. Written and read only - // from threadLoop() on the BFS thread - no atomics. 0 = no baseline for the - // current episode (a used value of 0 cannot be a real sample: onGC() only - // records used > 0). + // establish growth. updateUrgentAdmission() therefore admits the 100% + // tracking boost only while a newer heap-floor sample grows past the + // running baseline: every admitted sample raises it (a plateau loses the + // boost, a recovered spike never passes it), and the emergency band seeds + // the same baseline at entry so the ramp-band comparison stays continuous. + // Written and read only from threadLoop() on the BFS thread - no atomics. + // 0 = no baseline for the current episode (a used value of 0 cannot be a + // real sample: onGC() only records used > 0). u64 _admission_growth_baseline; // Wall-clock timestamp (OS::nanotime()) of the search's first pass - baseline for the TTL cutoff diff --git a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp index f276b0af07..b91190d977 100644 --- a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp +++ b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp @@ -1236,8 +1236,8 @@ TEST(LivenessTrackerSessionTest, ResetSessionClearsOnlyRecordingGates) { } // hasLeakSignal() must never authorize a search without a leak-signal source: -// with neither generations nor liveheap tracking active, the population table -// and heap-floor ring never fill, and searching would run whole-heap BFS +// with neither generations nor liveheap+referencechains active, the population +// table and heap-floor ring never fill, and searching would run whole-heap BFS // passes with zero candidates, growing the frontier table to its cap while // emitting nothing. TEST(LivenessTrackerSessionTest, LeakSignalRequiresGenerationsOrLiveness) { @@ -1247,13 +1247,13 @@ TEST(LivenessTrackerSessionTest, LeakSignalRequiresGenerationsOrLiveness) { tracker, /*enabled=*/false, /*generations=*/false); EXPECT_FALSE(tracker->leakSignalEnabled()); - // Liveheap alone is a sufficient source: it drives the same population - // machinery, only additionally preserving stack traces for its samples. - // Driven through the real initialize() derivation from parsed arguments - // (setSessionGatesForTest cannot model liveheap-only). + // Liveheap plus referencechains is a sufficient source: driven through the + // real initialize() derivation from parsed arguments, which + // setSessionGatesForTest cannot model. Arguments live; - ASSERT_FALSE(live.parse("memory=1048576:l")); + ASSERT_FALSE(live.parse("memory=1048576:l,referencechains=true")); ASSERT_TRUE(live._record_liveness); + ASSERT_TRUE(live._reference_chains); ASSERT_FALSE(live._gc_generations); u64 stamp_before = LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker); bool initialized_before = LivenessTrackerTestAccessor::initializedForTest(tracker); @@ -1261,7 +1261,7 @@ TEST(LivenessTrackerSessionTest, LeakSignalRequiresGenerationsOrLiveness) { LivenessTrackerTestAccessor::initializeGatesOnlyForTest(tracker, live); EXPECT_FALSE(tracker->gcGenerationsEnabled()); EXPECT_TRUE(tracker->leakSignalEnabled()) - << "initialize() must derive the leak signal from generations || liveheap"; + << "initialize() must derive the leak signal from generations || (liveheap && referencechains)"; // The helper restores every non-gate field it touched. EXPECT_EQ(stamp_before, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)); EXPECT_EQ(initialized_before, LivenessTrackerTestAccessor::initializedForTest(tracker)); @@ -1271,29 +1271,31 @@ TEST(LivenessTrackerSessionTest, LeakSignalRequiresGenerationsOrLiveness) { tracker, /*enabled=*/false, /*generations=*/false); } -// initialize() derives the two gates from the parsed options: -// gcGenerationsEnabled() == generations, leakSignalEnabled() == generations || -// liveheap. The reference-chain collector keys on the effective flag, so -// liveheap tracking alone must arm it. +// initialize() derives the gates: gcGenerationsEnabled() == generations, +// leakSignalEnabled() == generations || (liveheap && referencechains). The +// collector is the signal's only consumer, so liveheap alone must not arm it. TEST(LivenessTrackerSessionTest, LeakSignalDerivedByInitialize) { LivenessTracker *tracker = LivenessTracker::instance(); struct Row { const char *spec; bool liveness; bool generations; + bool refchains; bool expect_signal; }; const Row rows[] = { - {"memory=1048576:l", true, false, true}, - {"generations=true", false, true, true}, - {"generations=true,memory=1048576:l", true, true, true}, - {"memory=1048576:a", false, false, false}, + {"memory=1048576:l", true, false, false, false}, + {"memory=1048576:l,referencechains=true", true, false, true, true}, + {"generations=true", false, true, false, true}, + {"generations=true,memory=1048576:l", true, true, false, true}, + {"memory=1048576:a", false, false, false, false}, }; for (const Row &row : rows) { Arguments args; ASSERT_FALSE(args.parse(row.spec)) << "row " << row.spec; ASSERT_EQ(row.liveness, args._record_liveness) << "row " << row.spec; ASSERT_EQ(row.generations, args._gc_generations) << "row " << row.spec; + ASSERT_EQ(row.refchains, args._reference_chains) << "row " << row.spec; bool initialized_before = LivenessTrackerTestAccessor::initializedForTest(tracker); bool enabled_before = LivenessTrackerTestAccessor::enabledForTest(tracker); u64 stamp_before = LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker); @@ -3149,32 +3151,61 @@ TEST_F(LivenessTrackerMockJvmTest, SlotsPastTableSizeAreClearedAfterCompaction) } } -// Liveheap tracking alone (memory=...:l, no generations) must arm the -// effective leak signal through the real start() -> initialize() path, and -// every consumer gated on it (secondsToOOM(), maybeForceCleanup() and -// cleanup_table()'s population fold) must work without the generations -// option. The other fixtures arm the gate only through seams that mirror the -// generations value into it, so they cannot tell the two flags apart. -TEST_F(LivenessTrackerMockJvmTest, LiveheapOnlyStartArmsLeakSignalConsumers) { +// The leak signal's only consumer is the reference-chain collector: a plain +// liveheap session must keep the signal and its consumers disarmed through +// the real start() -> initialize() path, and liveheap + referencechains must +// arm them. The other fixtures arm the gate only through seams, so they +// cannot tell the two flags apart. +TEST_F(LivenessTrackerMockJvmTest, LiveheapLeakSignalArmingRequiresReferenceChainConsumer) { constexpr u64 SEC_NS = 1000000000ULL; constexpr u64 MiB = 1ULL << 20; LivenessTracker *tracker = LivenessTracker::instance(); - // 1. Re-start as liveheap-only. The gate stores (and the burn-in CAS) - // precede initialize()'s _initialized guard, so a repeated start() - // re-derives them. + // 1. Liveheap-only: the gate stores (and the burn-in CAS) precede + // initialize()'s _initialized guard, so a repeated start() re-derives them. tracker->setLeakSignalActivatedNsForTest(0); Arguments args; ASSERT_FALSE(args.parse("memory=1048576:l")); ASSERT_TRUE(args._record_liveness); ASSERT_FALSE(args._gc_generations); + ASSERT_FALSE(args._reference_chains); ASSERT_FALSE(tracker->start(args)); tracker->setSubsampleRatioForTest(1.0); EXPECT_FALSE(tracker->gcGenerationsEnabled()); + EXPECT_FALSE(tracker->leakSignalEnabled()) + << "a plain liveheap session has no signal consumer - the gate must stay off"; + + // The consumers keyed on the gate stay idle: no projection even with a + // rising ring, and no forced sweep folding the population. + tracker->klassPopulationResetForTest(); // also clears the heap-floor ring + tracker->setMaxHeapBytesForTest((jlong)(2800 * MiB)); + for (int i = 0; i < 10; i++) { + tracker->heapFloorRecordForTest(1000 * MiB + (u64)i * 100 * MiB, (u64)i * SEC_NS); + } + EXPECT_LT(tracker->secondsToOOM(), 0.0) + << "secondsToOOM() must not project in a plain liveheap session"; + tracker->klassPopulationResetForTest(); + tracker->setMaxHeapBytesForTest(-1); + + u32 retained_id = classIdOf(kRetainedSignature); + ASSERT_NE(0u, retained_id); + trackNew(&jvm.retained_class, /*tid=*/1); + KlassPopulationEntry entry{}; + ASSERT_FALSE(tracker->klassPopulationLookupForTest(retained_id, &entry)); + sweep(); // GarbageCollectionFinish + maybeForceCleanup(fake_now_ns) + EXPECT_FALSE(tracker->klassPopulationLookupForTest(retained_id, &entry)) + << "maybeForceCleanup() must not fold the population in a plain liveheap session"; + + // 2. Restart as liveheap + referencechains: the collector will run, so + // the same derivation arms the signal. + ASSERT_FALSE(args.parse("memory=1048576:l,referencechains=true")); + ASSERT_TRUE(args._reference_chains); + ASSERT_FALSE(tracker->start(args)); + EXPECT_FALSE(tracker->gcGenerationsEnabled()); EXPECT_TRUE(tracker->leakSignalEnabled()) - << "initialize() must derive the leak signal from generations || liveheap"; + << "liveheap with referencechains must arm the leak signal"; - // 2. Burn-in stamp lifecycle: start() stamps it from 0 and never re-stamps. + // 3. Burn-in stamp lifecycle: start() stamps it from 0 and never re-stamps. EXPECT_NE(0u, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)) << "the first enabled start() must stamp the burn-in clock"; u64 backdated = OS::nanotime() - LivenessTracker::OOM_PROJECTION_BURN_IN_NS - SEC_NS; @@ -3183,7 +3214,7 @@ TEST_F(LivenessTrackerMockJvmTest, LiveheapOnlyStartArmsLeakSignalConsumers) { EXPECT_EQ(backdated, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)) << "a later start() must not re-stamp the burn-in clock"; - // 3. secondsToOOM() projects in a liveheap-only session. Runs before any + // 4. secondsToOOM() projects in the armed session. Runs before any // sweep(): onGC() records real-clock floor samples while the signal is on. tracker->klassPopulationResetForTest(); // also clears the heap-floor ring tracker->setMaxHeapBytesForTest((jlong)(2800 * MiB)); @@ -3192,21 +3223,20 @@ TEST_F(LivenessTrackerMockJvmTest, LiveheapOnlyStartArmsLeakSignalConsumers) { } // GE rather than NEAR: start() resolved the real container limit. EXPECT_GE(tracker->secondsToOOM(), 0.0) - << "secondsToOOM() must project in a liveheap-only session"; + << "secondsToOOM() must project once the signal is armed"; tracker->klassPopulationResetForTest(); // drop the seeded ring before the sweep tracker->setMaxHeapBytesForTest(-1); - // 4. maybeForceCleanup() sweeps and folds the population. track() never + // 5. maybeForceCleanup() sweeps and folds the population. track() never // folds, so the only route to a population entry is that sweep. - u32 retained_id = classIdOf(kRetainedSignature); + retained_id = classIdOf(kRetainedSignature); ASSERT_NE(0u, retained_id); trackNew(&jvm.retained_class, /*tid=*/1); - KlassPopulationEntry entry{}; ASSERT_FALSE(tracker->klassPopulationLookupForTest(retained_id, &entry)); sweep(); // GarbageCollectionFinish + maybeForceCleanup(fake_now_ns) EXPECT_TRUE(tracker->klassPopulationLookupForTest(retained_id, &entry)) - << "maybeForceCleanup() must sweep and fold the population in a liveheap-only session"; + << "maybeForceCleanup() must sweep and fold the population once the signal is armed"; - // 5. TearDown clears the gates via setGcGenerationsForTest(false). + // 6. TearDown clears the gates via setGcGenerationsForTest(false). tracker->setLeakSignalActivatedNsForTest(0); } diff --git a/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc b/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc index 397c7c4328..9c13bdc7ae 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc @@ -1,3 +1,7 @@ +/* + * Copyright 2026, Datadog, Inc. + * SPDX-License-Identifier: Apache-2.0 + */ // --------------------------------------------------------------------------- // Lifecycle churn (C4): stop/restart-shaped interleavings against an in-flight diff --git a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc index 2080bc541c..d6bd727404 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc @@ -1,3 +1,7 @@ +/* + * Copyright 2026, Datadog, Inc. + * SPDX-License-Identifier: Apache-2.0 + */ // --------------------------------------------------------------------------- // OOM-ramp hysteresis (C5): isUrgent()'s latch/release machine and @@ -373,6 +377,59 @@ TEST_F(OomUrgencyTest, EmergencyBandAdmitsWithoutGrowthEvidence) { EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(299.0, true)); } +// The emergency band seeds the growth baseline at entry: the burn-in can hide +// the projection until it is already short, and without a seed the first +// ramp-band observation would re-snapshot and drop the boost mid-growth. +TEST_F(OomUrgencyTest, EmergencyBandEntrySeedsGrowthBaseline) { + ASSERT_NEAR(project(299.0), 299.0, 1.0); + u64 entry = MAX_HEAP - (u64)(299.0 * (double)RATE / (double)SAMPLE_SPACING_S); + ASSERT_EQ(entry, LivenessTracker::instance()->newestHeapFloorSample()); + + // Emergency-band entry: admitted, and the baseline is seeded at `entry`. + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(299.0, true)); + + // The projection rises above the threshold into the ramp band while the + // heap keeps growing: one RATE above the seeded baseline admits. + LivenessTracker::instance()->heapFloorRecordForTest(entry + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(400.0, true)) + << "ramp-band observation after emergency entry must compare against the " + "seeded baseline, not re-snapshot"; + + // Control: with the heap flat (no growth past the seeded baseline), the + // same ramp-band projection does not admit. + LivenessTracker::instance()->heapFloorRecordForTest(entry, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(400.0, true)); +} + +// The growth baseline is a high-water mark: every admitted sample raises it, +// so a heap that plateaus above the episode's entry level loses the boost - +// comparing against a frozen entry snapshot would keep admitting a heap that +// has stopped growing. +TEST_F(OomUrgencyTest, PlateauAboveEntryBaselineLosesBoost) { + ASSERT_NEAR(project(1500.0), 1500.0, 1.0); + u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); + + // Entry snapshot = peak; one RATE of growth admits and raises the bar. + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + // The heap settles flat at a level above the entry snapshot but below the + // raised bar: no growth, no boost. + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE / 2, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "a plateau above the entry level is not growth evidence"; + + // Growth resuming past the previous peak re-admits. + LivenessTracker::instance()->heapFloorRecordForTest(peak + 2 * RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); +} + // Leaving the band clears the entry snapshot: the next episode re-snapshots // against its own entry state instead of inheriting the previous episode's // baseline (which could admit on no growth at all if the heap is now above diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java index 3140c2c431..9e7112e0d9 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java @@ -31,10 +31,8 @@ * *

This class is a regression guard for the candidate-gate contract: the aggregate urgent-OOM * projection alone must NEVER open the gate while the per-klass population table is provably - * empty. Staging recordings showed the pre-fix behavior - the urgent latch entitled a - * candidate-less search that cannot exit early and ran whole-heap BFS passes to the auto-tuned - * TTL with zero chains to show for it. That bypass and its one-shot urgency entitlement were - * removed. + * empty: a candidate-less search cannot exit early and would run whole-heap BFS passes to the + * auto-tuned TTL with zero chains to show for it. * *

Reproducing a genuine near-OOM heap in this shared, no-{@code forkEvery} test JVM is * impractical (see {@link ReferenceChainTrackingTest}'s own header comment on the JVM this class diff --git a/doc/ReferenceChainsCollectionSummary.md b/doc/ReferenceChainsCollectionSummary.md index d1aebfb163..0cf16f5f20 100644 --- a/doc/ReferenceChainsCollectionSummary.md +++ b/doc/ReferenceChainsCollectionSummary.md @@ -7,10 +7,10 @@ Reference-chain collection helps explain why a sampled object remains live. It records a bounded chain of referrer classes from a GC root to a sampled object. It does not replace a heap dump or compute an exact retained set. The profiler disables this feature by default. Enable generation tracking or liveheap tracking -(either supplies the leak signal) together with reference-chain collection, e.g. -`generations=true,referencechains=true`. The bare `referencechains=true` option does not activate -the allocation engine that supplies candidates, and without a signal source the collector stays -idle: it never starts a search. +(either supplies the leak signal; liveheap requires `referencechains=true` in the same command) +together with reference-chain collection, e.g. `generations=true,referencechains=true`. The bare +`referencechains=true` option does not activate the allocation engine that supplies candidates, +and without a signal source the collector stays idle: it never starts a search. ## Data flow diff --git a/doc/architecture/LiveHeapReferenceChainsImplementation.md b/doc/architecture/LiveHeapReferenceChainsImplementation.md index 5e8a14e31c..79860b6805 100644 --- a/doc/architecture/LiveHeapReferenceChainsImplementation.md +++ b/doc/architecture/LiveHeapReferenceChainsImplementation.md @@ -201,16 +201,21 @@ These values are tuning inputs. They do not state guaranteed overhead. ## Configuration Reference-chain collection requires the allocation engine and a leak-signal source. The signal -source is generations tracking OR liveheap tracking (the two drive the same liveness mechanisms; -liveheap additionally preserves stack traces for its samples). With neither, the leak-signal gate -can never pass and the collector stays idle. Enable one of them in the same command: +source is generations tracking OR liveheap tracking with reference chains enabled (the two drive +the same liveness mechanisms; liveheap additionally preserves stack traces for its samples). The +reference-chain collector is the signal's only consumer, so a plain liveheap session +(`memory=b:l` without `referencechains`) keeps the signal disarmed: the per-epoch +klass folding, the heap-floor ring and the forced sweeps stay off. With no signal source, the +leak-signal gate can never pass and the collector stays idle. Enable one of them in the same +command: ```text generations=true,referencechains=true:hops=N:budget=N:ttl=N:framecap=N:pausetarget=N:painbudget=N:firstpassbudget=N ``` - `generations=true` activates the allocation engine and supplies leak candidates. Liveheap - tracking (`memory=b:l|L`) is an equivalent signal source. + tracking (`memory=b:l|L`) is an equivalent signal source when combined with + `referencechains=true`. - `hops` limits chain depth. - `budget` supplies the base edge-admission budget. - `ttl` sets search lifetime in milliseconds. A value at or below zero disables this time check. diff --git a/doc/architecture/ReferenceChainsSignalsExplained.md b/doc/architecture/ReferenceChainsSignalsExplained.md index 8ef3295b56..b71b61981b 100644 --- a/doc/architecture/ReferenceChainsSignalsExplained.md +++ b/doc/architecture/ReferenceChainsSignalsExplained.md @@ -24,7 +24,7 @@ Urgency does not authorize a search. A candidate-less search can never emit a ch The tradeoff of this candidate-only gate: a heap-wide leak spread across many classes, none of which individually clears `selectLeakCandidates()`'s per-klass ring-fill/hysteresis gate, never opens the gate, even in the emergency band (projected OOM under 5 minutes). No chains are produced in that case. Each such refused evaluation increments the `reference_chain_urgent_no_candidate` counter (about once per second while urgency is latched and no candidate exists), so the gap is visible in the diagnostics rather than silent, and it can be told apart from an idle healthy process. -The population-trend signal is constructed by the liveness tracker, which fills the per-klass population table and the heap-floor ring when generations tracking OR liveheap tracking is active (the two options drive the same tracking mechanisms; liveheap additionally preserves stack traces for its samples). When neither is active, no signal can ever be produced: `hasLeakSignal()` returns false, and the collector stays idle - it never runs a pass, restart, or search. +The population-trend signal is constructed by the liveness tracker, which fills the per-klass population table and the heap-floor ring when generations tracking OR liveheap tracking with reference chains enabled is active (the two options drive the same tracking mechanisms; liveheap additionally preserves stack traces for its samples). The reference-chain collector is the signal's only consumer, so a plain liveheap session without `referencechains` keeps the signal disarmed. When no source is active, no signal can ever be produced: `hasLeakSignal()` returns false, and the collector stays idle - it never runs a pass, restart, or search. ## Scheduling a running search From 489d0c8b2b149ce0f82e9d5011bb1645d5ce6654 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Fri, 30 Oct 2026 20:01:14 +0100 Subject: [PATCH 20/37] Fix review findings from the quick sphinx review round - StopClearsAdmissionGrowthBaseline no longer races the BFS thread: the test pins bfsThreadRunning() == false before driving the updateUrgentAdmission accessor directly. - threadLoop() computes secondsToOOM() once per iteration and threads the value through shouldRunPass/canAffordNewSearch/hasLeakSignal/ isUrgent; one-arg wrappers read once and forward. - New publishUrgencyState() test drives the real admission gate and asserts _urgent_tracking follows the gated decision. - AggressiveLeak's candidate-free check retries once before failing, tolerating a transient candidate in the shared test JVM. - setLeakSignalActivatedNsForTest0 negative-argument guard now has a test; the stamp is restored in finally. - GtestTaskBuilder declares PathSensitivity.RELATIVE on the test include file tree as a named task input. - Ownership comments on _admission_growth_baseline/_oom_ramp_active amended to name the non-BFS writers. --- .../native/gtest/GtestTaskBuilder.kt | 9 ++ ddprof-lib/src/main/cpp/referenceChains.cpp | 53 +++++--- ddprof-lib/src/main/cpp/referenceChains.h | 38 +++++- .../src/test/cpp/referenceChainsOomTests.inc | 127 ++++++++++++++++++ .../test/cpp/referenceChainsTestAccessors.h | 31 +++++ .../AggressiveLeakReferenceChainTest.java | 86 ++++++++++-- .../ReferenceChainTestSeamsTest.java | 29 ++++ 7 files changed, 340 insertions(+), 33 deletions(-) diff --git a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt index b267e0c037..31ac9cf8eb 100644 --- a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt +++ b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt @@ -13,6 +13,7 @@ import com.datadoghq.native.util.PlatformUtils import org.gradle.api.Project import org.gradle.api.file.FileCollection import org.gradle.api.tasks.Exec +import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskProvider import java.io.File @@ -131,11 +132,19 @@ class GtestTaskBuilder( // directory and appear in no include path, so register them here or an // .inc-only edit reports UP-TO-DATE and runs a stale binary. Inputs, // not sources: compiling an .inc standalone would emit duplicate symbols. + // The sweep is deliberately broad (every test-dir .inc/.h for every gtest + // task): the test dir is flat with a handful of shared headers, mapping + // headers to the tests that include them would need include parsing, and + // over-invalidation is preferred to running a stale binary. The input is + // named and RELATIVE so relocating the checkout/build dir does not change + // its fingerprint. inputs.files( project.fileTree(testFile.parentFile) { include("**/*.inc", "**/*.h") } ) + .withPropertyName("testIncludeInputs") + .withPathSensitivity(PathSensitivity.RELATIVE) includes.from(includeFiles) objectFileDir.set(objDir) diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index 77af09a90e..7c5ba992bb 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -523,6 +523,8 @@ void ReferenceChainTracker::threadLoop() { while (_running.load(std::memory_order_acquire)) { // Fixed ~1s cadence, no early wake on GC (see onGCFinish()'s own comment) - stopThread() still // interrupts this via its own pthread_kill so shutdown stays prompt. + // The iteration's only projection read - consumed by the ramp below, publishUrgencyState() and + // shouldRunPass() (restart gate / isUrgent() latch), so every decision sees one sample. double seconds_to_oom = LivenessTracker::instance()->secondsToOOM(); bool urgent = seconds_to_oom >= 0 && seconds_to_oom < OOM_RAMP_START_S; long target_ms = _pause_target_ms; @@ -586,9 +588,9 @@ void ReferenceChainTracker::threadLoop() { // can stay inside the band for many GCs after the heap has physically stopped growing, and // boosting admission on a recovered spike only buys table churn. The budget/cadence ramp above // stays keyed on the raw band comparison - without a running search (the candidate gate) those - // effects cost nothing, while mis-admission during a transient does. - _oom_ramp_active = urgent; - LivenessTracker::instance()->setUrgentTracking(updateUrgentAdmission(seconds_to_oom, urgent)); + // effects cost nothing, while mis-admission during a transient does. publishUrgencyState() + // performs both hand-offs. + publishUrgencyState(seconds_to_oom, urgent); // shouldRunPass()'s restart branch (restartSearch()) clears _pending_expand/_priority_expand // the same way a pass does - take _engine_lock around the call so that mutation is serialized @@ -597,7 +599,7 @@ void ReferenceChainTracker::threadLoop() { bool should_run; { MutexLocker engine_guard(_engine_lock); - should_run = shouldRunPass(now_ns); + should_run = shouldRunPass(now_ns, seconds_to_oom); } // Only sleep when idle (no pass will run). When a canary search is active or a pass is about to // run, skip the sleep to run passes back-to-back. @@ -652,10 +654,14 @@ void ReferenceChainTracker::onGCFinish() { } bool ReferenceChainTracker::shouldRunPass(u64 now_ns) { + return shouldRunPass(now_ns, LivenessTracker::instance()->secondsToOOM()); +} + +bool ReferenceChainTracker::shouldRunPass(u64 now_ns, double seconds_to_oom) { if (!_search_started) { // Same gate as a restart (canAffordNewSearch() below) - a brand-new tracker must not pay for // the first whole-heap walk/tagging pass either when there is no leak candidate to justify it. - bool afford = canAffordNewSearch(now_ns); + bool afford = canAffordNewSearch(now_ns, seconds_to_oom); TEST_LOG_SUMMARY("ReferenceChainTracker::shouldRunPass search_not_started " "canAffordNewSearch=%d", (int)afford); if (!afford) { @@ -686,7 +692,7 @@ bool ReferenceChainTracker::shouldRunPass(u64 now_ns) { // (or again) a leak indication to chase - canAffordNewSearch() is always false when no // leak-signal source (generations or liveheap tracking) is active at all, so a misconfigured // tracker stays idle instead of searching without a signal. - if (canAffordNewSearch(now_ns)) { + if (canAffordNewSearch(now_ns, seconds_to_oom)) { restartSearch(); TEST_LOG_SUMMARY("ReferenceChainTracker::shouldRunPass -> true (restarting search)"); return true; @@ -776,11 +782,15 @@ bool ReferenceChainTracker::shouldRunPass(u64 now_ns) { // threadLoop()'s own iteration and needs the *list* to poll each candidate's tag; this only needs // to know whether at least one exists. bool ReferenceChainTracker::isUrgent() const { + return isUrgent(LivenessTracker::instance()->secondsToOOM()); +} + +bool ReferenceChainTracker::isUrgent(double seconds_to_oom) const { static_assert(LivenessTracker::OOM_PROJECTION_BURN_IN_NS / 1e9 >= OOM_URGENT_THRESHOLD_S, "burn-in must cover the urgent horizon"); - // Warmup is filtered inside secondsToOOM(): no projection (-1) until the - // burn-in passes (see OOM_PROJECTION_BURN_IN_NS for why it is age-based). - double seconds_to_oom = LivenessTracker::instance()->secondsToOOM(); + // Warmup is filtered inside secondsToOOM(), which produced the caller's projection: no + // projection (-1) until the burn-in passes (see OOM_PROJECTION_BURN_IN_NS for why it is + // age-based). if (seconds_to_oom >= 0 && seconds_to_oom < OOM_URGENT_THRESHOLD_S) { _urgent_release_ticks = 0; if (!_urgent_latched) { @@ -862,7 +872,12 @@ bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool ur return false; } -bool ReferenceChainTracker::hasLeakSignal() { +void ReferenceChainTracker::publishUrgencyState(double seconds_to_oom, bool urgent) { + _oom_ramp_active = urgent; + LivenessTracker::instance()->setUrgentTracking(updateUrgentAdmission(seconds_to_oom, urgent)); +} + +bool ReferenceChainTracker::hasLeakSignal(double seconds_to_oom) { if (!LivenessTracker::instance()->leakSignalEnabled()) { // No population-trend signal can be constructed - generations tracking // and liveheap tracking are both off, so the per-klass population table @@ -873,14 +888,16 @@ bool ReferenceChainTracker::hasLeakSignal() { // diagnostic value. return false; } - double seconds_to_oom = LivenessTracker::instance()->secondsToOOM(); // isUrgent() is called unconditionally, not short-circuited behind any // branch: it maintains the latch/release state (see _urgent_latched), whose // only consumer is the no-progress-abandonment suppression once a search - // runs (runPass()'s terminal gate). The OOM ramp's budget/cadence/admission - // effects in threadLoop() follow the raw band comparison instead - they do - // not wait for this latch. - bool urgent = isUrgent(); + // runs (runPass()'s terminal gate). It evaluates the caller's projection + // (threadLoop()'s single per-iteration read), not a fresh secondsToOOM(). + // The OOM ramp's budget/cadence effects in threadLoop() follow the raw band + // comparison, and the 100% tracking admission follows + // updateUrgentAdmission()'s growth gate (via publishUrgencyState()) - + // neither waits for this latch. + bool urgent = isUrgent(seconds_to_oom); KlassCandidate probe[1]; int n = LivenessTracker::instance()->selectLeakCandidates(probe, 1); // The urgent latch does NOT entitle a candidate-less search. With nothing @@ -904,6 +921,10 @@ bool ReferenceChainTracker::hasLeakSignal() { } bool ReferenceChainTracker::canAffordNewSearch(u64 now_ns) { + return canAffordNewSearch(now_ns, LivenessTracker::instance()->secondsToOOM()); +} + +bool ReferenceChainTracker::canAffordNewSearch(u64 now_ns, double seconds_to_oom) { if (!_safepoint_pain_budget.canStartNow(now_ns)) { TEST_LOG_SUMMARY("ReferenceChainTracker::canAffordNewSearch blocked by " "safepoint_pain_budget balance=%.1fms refill_rate=%.4f", @@ -911,7 +932,7 @@ bool ReferenceChainTracker::canAffordNewSearch(u64 now_ns) { _pain_budget_refill_rate); return false; // still cooling down from the last search's own cost } - return hasLeakSignal(); + return hasLeakSignal(seconds_to_oom); } // Search restart (this class's own header comment). Called only from shouldRunPass() once diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index 887892e274..e8a26361eb 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -147,8 +147,10 @@ class ReferenceChainTracker { // End-of-pass timestamp of the last pass that ran with a canary chase still open (the reference // point for the spacing above). u64 _last_canary_pass_ns; - // Whether threadLoop()'s OOM urgency ramp is currently active, set each loop iteration BEFORE - // shouldRunPass() (same thread, no atomics needed). + // Whether threadLoop()'s OOM urgency ramp is currently active, set each loop iteration by + // publishUrgencyState() BEFORE shouldRunPass() (same BFS thread), and otherwise only by test + // seams (setOomRampActiveForTest(), publishUrgencyStateForTest()) while no BFS thread is live - + // no atomics needed. bool _oom_ramp_active; // How many consecutive times in a row runPass()'s canary-stuck check @@ -353,7 +355,12 @@ class ReferenceChainTracker { // running baseline: every admitted sample raises it (a plateau loses the // boost, a recovered spike never passes it), and the emergency band seeds // the same baseline at entry so the ramp-band comparison stays continuous. - // Written and read only from threadLoop() on the BFS thread - no atomics. + // Written and read by updateUrgentAdmission() on the BFS thread (threadLoop() + // via publishUrgencyState()); also reset by stop() and resetSearchStateForTest() + // (both only after stopThread() has joined the BFS thread) and by test seams + // (resetUrgencyForTest(), updateUrgentAdmissionForTest(), + // publishUrgencyStateForTest()) that run only while no BFS thread is live - + // so there is no concurrent access and no atomics are needed. // 0 = no baseline for the current episode (a used value of 0 cannot be a // real sample: onGC() only records used > 0). u64 _admission_growth_baseline; @@ -778,19 +785,28 @@ class ReferenceChainTracker { } void threadLoop(); - // Runs a pass when the GC-finish epoch advanced or the cadence elapsed. + // Runs a pass when the GC-finish epoch advanced or the cadence elapsed. The two-arg form + // consumes the caller's per-iteration OOM projection (threadLoop()'s single secondsToOOM() + // read, passed down to the restart gate and the urgency latch so every decision of one + // iteration sees the same sample); the one-arg form reads secondsToOOM() once and forwards + // (JNI shouldRunPassForTest(), gtest accessors). bool shouldRunPass(u64 now_ns); + bool shouldRunPass(u64 now_ns, double seconds_to_oom); // Cheap probe (max=1, not the real poll pollWatchedTargets() makes) into LivenessTracker's // population-trend table: true if at least one klass shows a positive population slope worth // chasing. Always false when no leak-signal source (generations or liveheap tracking) is // active - the population table and heap-floor ring never fill without one, and a search // would run whole-heap BFS passes with zero candidates for zero diagnostic value. - bool hasLeakSignal(); + // seconds_to_oom is the caller's projection (passed to isUrgent()), never re-read here. + bool hasLeakSignal(double seconds_to_oom); // Latched, hysteretic view of LivenessTracker::secondsToOOM() crossing OOM_URGENT_THRESHOLD_S - - // see _urgent_latched for the latch/release rules and why the raw comparison flaps. + // see _urgent_latched for the latch/release rules and why the raw comparison flaps. The + // one-arg form evaluates the caller's projection; the zero-arg form reads secondsToOOM() and + // forwards (runPass()'s no-progress check in referenceChainTraversal.cpp, isUrgentForTest()). bool isUrgent() const; + bool isUrgent(double seconds_to_oom) const; // Computes the 100% tracking-admission decision for one threadLoop() // iteration and maintains _admission_growth_baseline. Raw `urgent` still @@ -800,10 +816,18 @@ class ReferenceChainTracker { // sufficient, and OOM_URGENT_THRESHOLD_S for the emergency split. bool updateUrgentAdmission(double seconds_to_oom, bool urgent); + // Publishes one threadLoop() iteration's urgency: _oom_ramp_active follows the raw band + // (`urgent`) for shouldRunPass()'s canary-backoff bypass; LivenessTracker's 100% tracking boost + // (setUrgentTracking()) follows updateUrgentAdmission()'s growth-gated decision, never raw + // `urgent`. Extracted from threadLoop() so the wiring is testable without a BFS thread. + void publishUrgencyState(double seconds_to_oom, bool urgent); + // Search restart gate: true once _safepoint_pain_budget has // drained back to zero (canStartNow()) *and* hasLeakSignal() above reports at least one leak - // candidate. + // candidate. The two-arg form consumes the caller's projection; the one-arg form reads + // secondsToOOM() once and forwards. bool canAffordNewSearch(u64 now_ns); + bool canAffordNewSearch(u64 now_ns, double seconds_to_oom); // Resets every per-search field back to its just-constructed value so the next runPass() call // takes the "first pass of a search" branch again, exactly like a fresh ReferenceChainTracker diff --git a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc index d6bd727404..6724c91132 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc @@ -262,6 +262,65 @@ TEST_F(OomUrgencyTest, BurnInProjectionCountsTowardRelease) { EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()); } +// threadLoop() reads secondsToOOM() once per iteration and passes that value +// down the restart gate: shouldRunPass(u64, double) -> canAffordNewSearch(u64, +// double) -> hasLeakSignal(double) -> isUrgent(double). Each overload must +// latch on the PASSED projection, never re-read the live ring, so every +// decision of one iteration sees the same sample. Each block seeds a live +// projection that disagrees with the passed one. Return values of the +// candidate-dependent gates are not asserted - only the latch they drive. +TEST_F(OomUrgencyTest, UrgencyDecisionsConsumeThePassedProjection) { + // Restores the tracker's search/pain-budget state on every exit path. + struct ResetTracker { + ~ResetTracker() { ReferenceChainsTestAccessor::reset(); } + } reset_tracker; + + // isUrgent(double). + ASSERT_LT(project(0.0, /*flat=*/true), 0.0) << "live ring must report no projection"; + EXPECT_TRUE(ReferenceChainsTestAccessor::isUrgentWithProjectionForTest(200.0)); + EXPECT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()) + << "isUrgent(double) must latch on the passed projection"; + ReferenceChainsTestAccessor::resetUrgencyForTest(); + ASSERT_NEAR(project(200.0), 200.0, 1.0); + EXPECT_FALSE(ReferenceChainsTestAccessor::isUrgentWithProjectionForTest(-1.0)); + EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()) + << "isUrgent(double) must not re-read the live (urgent) projection"; + + // hasLeakSignal(double). + ReferenceChainsTestAccessor::resetUrgencyForTest(); + ASSERT_LT(project(0.0, /*flat=*/true), 0.0); + ReferenceChainsTestAccessor::hasLeakSignalForTest(200.0); + EXPECT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()) + << "hasLeakSignal() must latch on the passed projection, not re-read secondsToOOM()"; + ReferenceChainsTestAccessor::resetUrgencyForTest(); + ASSERT_NEAR(project(200.0), 200.0, 1.0); + ReferenceChainsTestAccessor::hasLeakSignalForTest(-1.0); + EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()); + + // canAffordNewSearch(u64, double) and shouldRunPass(u64, double): reset() + // leaves a fresh PainBudget (balance 0, canStartNow() true) and the search + // not started, so both reach hasLeakSignal(). + ReferenceChainsTestAccessor::reset(); + ReferenceChainsTestAccessor::resetUrgencyForTest(); + ASSERT_LT(project(0.0, /*flat=*/true), 0.0); + ReferenceChainsTestAccessor::canAffordNewSearch(OS::nanotime(), 200.0); + EXPECT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()) + << "canAffordNewSearch() must forward the passed projection"; + ReferenceChainsTestAccessor::resetUrgencyForTest(); + ReferenceChainsTestAccessor::shouldRunPass(OS::nanotime(), 200.0); + EXPECT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()) + << "shouldRunPass() must forward the passed projection"; + + ReferenceChainsTestAccessor::reset(); + ReferenceChainsTestAccessor::resetUrgencyForTest(); + ASSERT_NEAR(project(200.0), 200.0, 1.0); + ReferenceChainsTestAccessor::canAffordNewSearch(OS::nanotime(), -1.0); + EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()); + ReferenceChainsTestAccessor::resetUrgencyForTest(); + ReferenceChainsTestAccessor::shouldRunPass(OS::nanotime(), -1.0); + EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()); +} + // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- // Urgent-admission growth gate (updateUrgentAdmission()): inside the OOM ramp @@ -362,6 +421,62 @@ TEST_F(OomUrgencyTest, RecoveredSpikeStaysUnboostedWhileProjectionLags) { } } +// threadLoop()'s publish step (publishUrgencyState()) must feed +// setUrgentTracking() the growth-gated admission decision, not the raw band +// comparison: on a recovered spike the ramp stays active while the 100% +// boost stays off. Observed through LivenessTracker's real admission gate: +// after admissionResetForTest() (ratio 0, no watched tids) only the urgency +// boost can admit a tid. +TEST_F(OomUrgencyTest, PublishedBoostFollowsGrowthGateNotRawBand) { + LivenessTracker *lt = LivenessTracker::instance(); + struct Restore { + ~Restore() { + ReferenceChainsTestAccessor::publishUrgencyStateForTest(-1.0, false); + LivenessTracker::instance()->admissionResetForTest(); + LivenessTracker::instance()->setSubsampleRatioForTest(0.1); + } + } restore; + lt->admissionResetForTest(); + const jint TID = 1234; + + ASSERT_NEAR(project(700.0), 700.0, 1.0); + u64 peak = MAX_HEAP - (u64)(700.0 * (double)RATE / (double)SAMPLE_SPACING_S); + + // Entry: ramp active (raw band), boost off (snapshot only). + ReferenceChainsTestAccessor::publishUrgencyStateForTest(700.0, true); + EXPECT_TRUE(ReferenceChainsTestAccessor::oomRampActiveForTest()); + EXPECT_FALSE(lt->admitForTrackingForTest(TID)) << "entry observation must not boost"; + + // Lagging recovery: ramp stays on, boost stays off. + for (int r = 1; r <= 6; r++) { + lt->heapFloorRecordForTest(peak - (u64)r * RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + double seconds = lt->secondsToOOM(); + ASSERT_GE(seconds, 300.0) + << "recovery sample " << r << ": must stay out of the emergency band"; + ASSERT_LT(seconds, 1800.0) + << "recovery sample " << r << ": must stay inside the ramp band"; + ReferenceChainsTestAccessor::publishUrgencyStateForTest(seconds, true); + EXPECT_TRUE(ReferenceChainsTestAccessor::oomRampActiveForTest()) << r; + EXPECT_FALSE(lt->admitForTrackingForTest(TID)) + << "recovery sample " << r << ": boost must follow the growth gate, not the raw band"; + } + + // Non-vacuity control: growth past the peak boosts. An explicit in-band, + // non-emergency projection is passed - any value < OOM_URGENT_THRESHOLD_S + // (including -1 with urgent=true) would admit through the emergency band + // regardless of growth. + lt->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + ReferenceChainsTestAccessor::publishUrgencyStateForTest(1490.0, true); + EXPECT_TRUE(lt->admitForTrackingForTest(TID)) << "growth past the high-water mark must boost"; + + // Episode end: both drop. + ReferenceChainsTestAccessor::publishUrgencyStateForTest(-1.0, false); + EXPECT_FALSE(ReferenceChainsTestAccessor::oomRampActiveForTest()); + EXPECT_FALSE(lt->admitForTrackingForTest(TID)); +} + // The emergency band (< OOM_URGENT_THRESHOLD_S) waives the growth-evidence // hold: minutes from death means capturing whatever the sampler offers, // including the very first observation and a heap whose newest sample is @@ -542,6 +657,14 @@ TEST_F(ReferenceChainsCanaryRefillTest, RefillRaiseTracksOpenChaseExactly) { // updateUrgentAdmission() reads only the newest heap-floor sample, so no max // heap is set - and must not be before start(): with a max heap > 0, start()'s // autoTuneDefaults() calls GetAvailableProcessors, which this stub jvmti lacks. +// +// updateUrgentAdmissionForTest() writes the BFS-thread-only +// _admission_growth_baseline from the gtest main thread. That is race-free only +// because start() does not spawn the BFS thread (threadLoop() needs an attached +// VM - see the end of ReferenceChainTracker::start()), so no threadLoop() +// calls updateUrgentAdmission() concurrently here. The ASSERTs on +// bfsThreadRunning() pin that precondition: a future start() that spawns the +// thread fails here instead of racing. TEST_F(ReferenceChainsCanaryRefillTest, StopClearsAdmissionGrowthBaseline) { constexpr u64 MiB = 1ULL << 20; @@ -572,6 +695,8 @@ TEST_F(ReferenceChainsCanaryRefillTest, StopClearsAdmissionGrowthBaseline) { ReferenceChainsTestAccessor::reset(); } } cleanup{tracker, lt}; + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()) + << "updateUrgentAdmissionForTest() would race threadLoop()"; // Recording 1: the first urgent non-emergency observation snapshots // 500MiB and does not admit. @@ -580,6 +705,8 @@ TEST_F(ReferenceChainsCanaryRefillTest, StopClearsAdmissionGrowthBaseline) { tracker->stop(); ASSERT_FALSE(tracker->start(args)); + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()) + << "updateUrgentAdmissionForTest() would race threadLoop()"; // Recording 2: the heap is now above the OLD baseline. A fresh episode // must re-snapshot (false); a baseline leaked through stop() would admit diff --git a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h index 8d5d59ef4e..ec5774a696 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h +++ b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h @@ -124,6 +124,16 @@ class ReferenceChainsTestAccessor { return ReferenceChainTracker::instance()->shouldRunPass(now_ns); } + // Projection-taking overloads: threadLoop()'s single per-iteration secondsToOOM() read is + // passed through instead of re-read. + static bool canAffordNewSearch(u64 now_ns, double seconds_to_oom) { + return ReferenceChainTracker::instance()->canAffordNewSearch(now_ns, seconds_to_oom); + } + + static bool shouldRunPass(u64 now_ns, double seconds_to_oom) { + return ReferenceChainTracker::instance()->shouldRunPass(now_ns, seconds_to_oom); + } + static void setSearchPainMs(u64 ms) { ReferenceChainTracker::instance()->_search_pain_ms = ms; } @@ -180,6 +190,27 @@ class ReferenceChainsTestAccessor { return ReferenceChainTracker::instance()->updateUrgentAdmission(seconds_to_oom, urgent); } + // True while the BFS thread (startThread()/threadLoop()) is live. start() never spawns it + // (only startThread() does - profiler.cpp and the JNI-only resetSearchStateForTest()), so + // gtests may drive BFS-thread-only seams after start(); tests that rely on that assert this + // is false. + static bool bfsThreadRunning() { + return ReferenceChainTracker::instance()->_running.load(std::memory_order_acquire); + } + // threadLoop()'s publish step: ramp flag + growth-gated tracking boost. + static void publishUrgencyStateForTest(double seconds_to_oom, bool urgent) { + ReferenceChainTracker::instance()->publishUrgencyState(seconds_to_oom, urgent); + } + static bool oomRampActiveForTest() { + return ReferenceChainTracker::instance()->_oom_ramp_active; + } + // Projection-taking urgency decisions (consume the passed value, never re-read). + static bool isUrgentWithProjectionForTest(double seconds_to_oom) { + return ReferenceChainTracker::instance()->isUrgent(seconds_to_oom); + } + static bool hasLeakSignalForTest(double seconds_to_oom) { + return ReferenceChainTracker::instance()->hasLeakSignal(seconds_to_oom); + } // Applied vs configured CPU pain-budget refill rate (shouldRunPass()'s // canary-refill raise). static double cpuPainBudgetRefillRateForTest() { diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java index 9e7112e0d9..5fb113074b 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java @@ -9,12 +9,16 @@ import com.datadoghq.profiler.Platform; import com.datadoghq.profiler.ReferenceChainTestHooks; import org.junit.jupiter.api.Test; +import org.opentest4j.AssertionFailedError; +import org.opentest4j.TestAbortedException; import java.util.Arrays; +import java.util.function.Supplier; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assumptions.assumeFalse; @@ -149,20 +153,81 @@ private static GateEvaluation evaluateGateAfterSearchReset() { } /** - * Aborts only on an observed per-klass candidate: a candidate legitimately opens the gate, so - * the candidate-gate contract cannot be isolated in that run. A BFS pass since the search reset - * (the reset zeroes the pass count) with no candidate in either read means the gate opened - * without a candidate - e.g. a re-introduced urgency bypass on the restarted BFS thread - and - * must FAIL, not skip. + * Aborts on an observed per-klass candidate: a candidate legitimately opens the gate, so the + * candidate-gate contract cannot be isolated in that run. A BFS pass since the search reset + * (the reset zeroes the pass count) with no candidate in either read is re-evaluated once on a + * fresh reset: it FAILS if the candidate-free pass reproduces - the gate opened without a + * candidate, e.g. a re-introduced urgency bypass on the restarted BFS thread - and ABORTS if it + * does not reproduce (or the retry sees a candidate), because the bracket alone cannot tell a + * transient organic candidate apart from a gate regression. Never a silent pass. */ private static void checkNoCandidateFreeActivity(GateEvaluation e) { + checkNoCandidateFreeActivity(e, + AggressiveLeakReferenceChainTest::evaluateGateAfterSearchReset); + } + + /** Decision table of {@link #checkNoCandidateFreeActivity(GateEvaluation)}, retry injectable. */ + private static void checkNoCandidateFreeActivity(GateEvaluation e, + Supplier retry) { assumeFalse(e.candidateSeen, "Aborting: organic per-klass candidate around the direct " + "evaluation (" + e.detail + ") - the candidate-gate contract cannot be isolated in " + "this run"); - if (e.passesAfter != 0) { - fail("A BFS pass ran after the search reset although no per-klass candidate was seen in " - + "either read - the gate opened without a candidate (" + e.detail + ")"); + if (e.passesAfter == 0) { + return; + } + // One candidate-free pass is not proof: the restarted BFS thread's gate decision is not + // atomic with the two candidate reads, so an organic candidate can authorize the pass and + // leave selectLeakCandidates() between them (each change needs a population fold, at most + // one per GC epoch). That cannot plausibly repeat back-to-back; a gate that opens without a + // candidate does. Re-evaluate once on a fresh reset and fail only if it reproduces. + GateEvaluation r = retry.get(); + String both = "first: " + e.detail + "; retry: " + r.detail; + assumeFalse(r.candidateSeen, "Aborting: a BFS pass ran with no per-klass candidate seen, " + + "and the re-evaluation then saw an organic candidate (" + both + ") - the " + + "candidate-gate contract cannot be isolated in this run"); + if (r.passesAfter != 0) { + fail("A BFS pass ran after the search reset with no per-klass candidate in any read, on " + + "two independent evaluations - the gate opened without a candidate (" + both + ")"); } + assumeTrue(false, "Aborting: a candidate-free BFS pass after the search reset did not " + + "reproduce on a re-evaluation (" + both + ") - a transient organic candidate in this " + + "shared JVM cannot be ruled out, so the run cannot be isolated"); + } + + private static GateEvaluation syntheticEvaluation(boolean candidateSeen, int passesAfter) { + GateEvaluation e = new GateEvaluation(); + e.candidateSeen = candidateSeen; + e.passesAfter = passesAfter; + e.detail = "synthetic(candidateSeen=" + candidateSeen + ", passesAfter=" + passesAfter + ")"; + return e; + } + + /** + * Pure-logic check of {@link #checkNoCandidateFreeActivity}'s abort/fail table (no native + * seams): a candidate aborts; a clean evaluation returns without consuming the retry; a + * candidate-free pass fails only when it reproduces on the retry and otherwise aborts - never + * a silent pass, and never a hard fail on a single unreproduced observation. + */ + @Test + public void checkNoCandidateFreeActivityRequiresReproductionBeforeFailing() { + Supplier noRetry = () -> { + throw new IllegalStateException("retry must not be consumed"); + }; + checkNoCandidateFreeActivity(syntheticEvaluation(false, 0), noRetry); + assertThrows(TestAbortedException.class, + () -> checkNoCandidateFreeActivity(syntheticEvaluation(true, 0), noRetry)); + assertThrows(AssertionFailedError.class, + () -> checkNoCandidateFreeActivity(syntheticEvaluation(false, 1), + () -> syntheticEvaluation(false, 1)), + "a reproduced candidate-free pass must fail"); + assertThrows(TestAbortedException.class, + () -> checkNoCandidateFreeActivity(syntheticEvaluation(false, 1), + () -> syntheticEvaluation(false, 0)), + "an unreproduced candidate-free pass must abort, not fail or pass"); + assertThrows(TestAbortedException.class, + () -> checkNoCandidateFreeActivity(syntheticEvaluation(false, 1), + () -> syntheticEvaluation(true, 1)), + "a retry that sees a candidate must abort"); } private static boolean isEmpty(int[] a) { @@ -182,8 +247,9 @@ private static boolean isEmpty(int[] a) { *

The live BFS thread is deliberately not watched over a wait window: its pass counter also * counts passes legitimately started by unrelated per-klass candidates in this shared JVM. The * run is aborted (not passed) when an organic candidate appears around the direct evaluation, - * and fails when a BFS pass ran since the search reset without any candidate - the gate opened - * without one. + * and fails when a BFS pass since the search reset, with no candidate in any read, reproduces on + * a second independent evaluation - the gate opened without one (a single unreproduced + * occurrence aborts the run). */ @Test public void shouldNotOpenSearchGateOnAggressiveHeapWideGrowthWithNoLeakCandidate() { diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTestSeamsTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTestSeamsTest.java index c86a1aaf58..4fe1d3ff16 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTestSeamsTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTestSeamsTest.java @@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -40,6 +41,9 @@ public class ReferenceChainTestSeamsTest extends AbstractProfilerTest { // genuine history outranks the seeded ramp. private static final int SLOPE_TEST_KLASS_ID = 987001; private static final int CHAIN_TEST_KLASS_ID = 987002; + // Arbitrary non-zero burn-in stamp: LivenessTracker::initialize() only CASes the stamp from 0, + // so a non-zero value cannot be overwritten by a concurrent profiler start mid-test. + private static final long KNOWN_ACTIVATION_STAMP_NS = 42L; private static final class ChainTarget {} @@ -194,4 +198,29 @@ public void failureDiagnosticsFingerprintHasCanonicalSegments() { public void observedChainHeadsIsEmptyForNoEvents() { assertTrue(ReferenceChainAssertions.observedChainHeads(null).isEmpty()); } + + /** + * Kills a dropped negative-argument guard in setLeakSignalActivatedNsForTest0 (javaApi.cpp): + * without it a negative jlong would be stored as a u64 near UINT64_MAX, which secondsToOOM() + * reads as burn-in-never-passed. Asserts the throw AND that the stored stamp is unchanged; the + * original stamp is restored on every exit path (shared, no-forkEvery JVM). + */ + @Test + public void shouldRejectNegativeLeakSignalActivationStamp() { + assumeDebugBuild(); + long savedActivationNs = ReferenceChainTestHooks.backdateLeakSignalActivationForTest0(); + long observed; + try { + ReferenceChainTestHooks.setLeakSignalActivatedNsForTest0(KNOWN_ACTIVATION_STAMP_NS); + assertThrows(IllegalArgumentException.class, + () -> ReferenceChainTestHooks.setLeakSignalActivatedNsForTest0(-1L), + "A negative burn-in stamp must be rejected, not cast to a huge u64"); + // Returns the current stamp (and backdates; the finally block restores the original). + observed = ReferenceChainTestHooks.backdateLeakSignalActivationForTest0(); + } finally { + ReferenceChainTestHooks.setLeakSignalActivatedNsForTest0(savedActivationNs); + } + assertEquals(KNOWN_ACTIVATION_STAMP_NS, observed, + "A rejected negative stamp must leave the previous stamp in place"); + } } From 743c7cb629a329624514ed81a9b09a377cf57e15 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Mon, 2 Nov 2026 11:42:09 +0100 Subject: [PATCH 21/37] sphinx: address review feedback on PR #921 --- ddprof-lib/src/main/cpp/livenessTracker.cpp | 47 ++- ddprof-lib/src/main/cpp/livenessTracker.h | 28 +- ddprof-lib/src/main/cpp/referenceChains.cpp | 49 ++- ddprof-lib/src/main/cpp/referenceChains.h | 35 +- .../src/test/cpp/livenessTracker_ut.cpp | 100 ++++++ .../src/test/cpp/referenceChainsOomTests.inc | 311 ++++++++++++++++++ .../test/cpp/referenceChainsTestAccessors.h | 5 +- .../ReferenceChainsSignalsExplained.md | 2 +- 8 files changed, 546 insertions(+), 31 deletions(-) diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index 2850a8d569..ba286893aa 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -1477,19 +1477,32 @@ void LivenessTracker::recordTidTrendSamplesLocked( } } -u64 LivenessTracker::newestHeapFloorSample() const { +u64 LivenessTracker::newestHeapFloorSample(u64 *seq_out) const { // Matches recordHeapFloorSampleUnchecked()'s storeRelease() on both index // fields - loadAcquire() here is what makes the payload read below visible. u8 fill = loadAcquire(_heap_floor_ring_fill); u8 head = loadAcquire(_heap_floor_ring_head); if (fill == 0) { + if (seq_out != nullptr) { + *seq_out = 0; + } return 0; } // head points one past the newest slot (recordHeapFloorSampleUnchecked() // stores then advances) - step back one, wrapping over the ring size. u8 newest = (u8)((head + KLASS_POPULATION_RING_SIZE - 1) % KLASS_POPULATION_RING_SIZE); - return load(_heap_floor_ring[newest]); + u64 used = load(_heap_floor_ring[newest]); + if (seq_out != nullptr) { + // Read from the SAME slot as `used`, never from _heap_floor_sample_seq: a + // global counter read could tear against the slot (the seq of sample N+1 + // paired with the value of sample N) and make the urgent-admission gate + // treat a re-read as a new sample. The newest slot is not rewritten until + // KLASS_POPULATION_RING_SIZE further samples are recorded, so the pair is + // consistent. + *seq_out = load(_heap_floor_seq_ring[newest]); + } + return used; } void LivenessTracker::recordHeapFloorSample(u64 used, u64 timestamp_ns, u64 container_used) { @@ -1520,13 +1533,18 @@ void LivenessTracker::recordHeapFloorSampleUnchecked(u64 used, u64 timestamp_ns, // (The previous version had this backwards - storeRelease() on the // payload with a plain store on the index - which does not establish any // ordering between "the index says this slot is valid" and "the payload - // for that slot is visible".) _heap_floor_time_ring's and - // _container_mem_ring's payload writes below are plain stores for the - // same reason - they precede the same storeRelease() in program order. + // for that slot is visible".) _heap_floor_time_ring's, + // _container_mem_ring's and _heap_floor_seq_ring's payload writes below are + // plain stores for the same reason - they precede the same storeRelease() + // in program order. _heap_floor_sample_seq itself is only ever read and + // written here (single writer at a time), readers see it via the slot ring. u8 head = load(_heap_floor_ring_head); store(_heap_floor_ring[head], used); store(_heap_floor_time_ring[head], timestamp_ns); store(_container_mem_ring[head], container_used); + u64 seq = _heap_floor_sample_seq + 1; + _heap_floor_sample_seq = seq; + store(_heap_floor_seq_ring[head], seq); storeRelease(_heap_floor_ring_head, (u8)((head + 1) % KLASS_POPULATION_RING_SIZE)); u8 fill = load(_heap_floor_ring_fill); if (fill < KLASS_POPULATION_RING_SIZE) { @@ -2155,12 +2173,19 @@ Error LivenessTracker::initialize(Arguments &args) { __atomic_store_n(&_watched_tid_count, 0, __ATOMIC_RELEASE); __atomic_store_n(&_urgent_tracking, false, __ATOMIC_RELEASE); - // First activation stamps the burn-in clock (see _leak_signal_activated_ns). - // Only reached with _enabled == true, i.e. generations || liveness, so the - // leak signal is on. CAS from 0: later starts never re-stamp it. - u64 never_activated = 0; - _leak_signal_activated_ns.compare_exchange_strong( - never_activated, OS::nanotime(), std::memory_order_release, std::memory_order_relaxed); + // First effective leak-signal activation stamps the burn-in clock (see + // _leak_signal_activated_ns). _enabled (generations || liveness) is coarser + // than the leak signal (generations || (liveness && referencechains)): a + // plain liveheap start must not stamp, or a later recording that enables + // reference chains would inherit an already-expired burn-in. CAS from 0: + // later starts never re-stamp it. Stays ahead of the _initialized early + // return so the first signal-on start after earlier non-signal starts can + // still stamp. + if (_leak_signal_enabled.load(std::memory_order_relaxed)) { + u64 never_activated = 0; + _leak_signal_activated_ns.compare_exchange_strong( + never_activated, OS::nanotime(), std::memory_order_release, std::memory_order_relaxed); + } if (_initialized) { // if the tracker was previously initialized return the stored result for diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index 582713c572..9e31ae7dcf 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -392,8 +392,22 @@ class alignas(alignof(SpinLock)) LivenessTracker { // libraries) - see secondsToOOM()'s own comment for how this ring and // _heap_floor_ring are used as two independent OOM boundaries. u64 _container_mem_ring[KLASS_POPULATION_RING_SIZE]; + // Per-sample identity, paired index-for-index with _heap_floor_ring (same + // head/fill, pushed by the same recordHeapFloorSampleUnchecked() call before + // the head storeRelease). Values come from _heap_floor_sample_seq below, so a + // slot's seq names exactly one recorded sample for the process lifetime - + // the urgent-admission gate (ReferenceChainTracker::updateUrgentAdmission()) + // uses it to tell "a new GC sample arrived" from "re-reading the same one". + u64 _heap_floor_seq_ring[KLASS_POPULATION_RING_SIZE]; volatile u8 _heap_floor_ring_head; volatile u8 _heap_floor_ring_fill; + // Monotonic heap-floor sample counter (first sample = 1, 0 = "no sample"). + // Written only by recordHeapFloorSampleUnchecked() (single writer at a time, + // same argument as _heap_floor_ring) and published through the slot ring + // above; never reset - not by heapFloorResetForTest(), not by + // klassPopulationResetForTest(), not by stop()/start() - so a sequence value + // can never alias an earlier sample. + u64 _heap_floor_sample_seq; // Debug-only test seam: when true, onGC() skips recordHeapFloorSample() // so a test can seed the ring exclusively via heapFloorRecordForTest() @@ -855,8 +869,12 @@ class alignas(alignof(SpinLock)) LivenessTracker { // Reads the used-bytes value of the newest _heap_floor_ring sample (0 when // the ring is empty) - the urgent-admission gate's freshest physical growth - // evidence. See the definition for the SPSC acquire pairing. - u64 newestHeapFloorSample() const; + // evidence. When seq_out is non-null it is ALWAYS written: the newest slot's + // _heap_floor_seq_ring value, read from the same slot as the returned used + // value (0 when the ring is empty), so a caller can tell a new sample from a + // re-read of the one it already consumed. See the definition for the SPSC + // acquire pairing. + u64 newestHeapFloorSample(u64 *seq_out = nullptr) const; constexpr static int MAX_LEAK_CANDIDATES = 5; // Delete copy constructor and assignment operator to prevent copies @@ -870,7 +888,7 @@ class alignas(alignof(SpinLock)) LivenessTracker { _record_heap_usage(false), _Class(NULL), _Class_getName(0), _gc_epoch(0), _last_gc_epoch(0), _last_cleanup_ns(0), _used_after_last_gc(0), - _heap_floor_ring_head(0), _heap_floor_ring_fill(0), + _heap_floor_ring_head(0), _heap_floor_ring_fill(0), _heap_floor_sample_seq(0), _max_heap_bytes(-1), _container_memory_limit(-1), _container_limit_resolved(false), _gc_generations(false), @@ -1590,7 +1608,8 @@ class alignas(alignof(SpinLock)) LivenessTracker { // next test's hysteresis threshold. storeRelease() (not plain store()) // so the reset is published to the BFS/GC thread on arm64's weak memory // model - a relaxed store may never be visible to a reader doing - // loadAcquire() on these same indices. + // loadAcquire() on these same indices. _heap_floor_sample_seq is + // deliberately not reset (see its comment). storeRelease(_heap_floor_ring_head, (u8)0); storeRelease(_heap_floor_ring_fill, (u8)0); } @@ -1628,6 +1647,7 @@ class alignas(alignof(SpinLock)) LivenessTracker { // enabled fails the assert instead of racing a live reader. Non-DEBUG // builds compile the reset bare - the flag does not exist there and the // JNI entry that reaches this seam is compiled out. + // _heap_floor_sample_seq is deliberately not reset (see its comment). void heapFloorResetForTest() { #ifdef DEBUG if (!_heap_floor_recording_disabled_for_test.load(std::memory_order_acquire)) { diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index 7c5ba992bb..e29b98def6 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -388,10 +388,14 @@ void ReferenceChainTracker::stop() { // - _search_start_ns still holds the previous recording's clock, so the entire stopped gap // would count toward the new recording's TTL and could abandon "its" search on the first // pass with a spurious abandonment event for a search the new recording never started; - // - _urgent_latched/_urgent_release_ticks/_admission_growth_baseline would carry the previous + // - _urgent_latched/_urgent_release_ticks/_admission_growth_baseline (and its + // _admission_sample_seq/_admission_decision freshness state) would carry the previous // recording's urgency episode into the new one (a stale growth baseline would let the new // recording's first urgent observation skip updateUrgentAdmission()'s entry re-snapshot and - // admit against the old recording's heap level); + // admit against the old recording's heap level). The freshness pair needs its own reset: + // an emergency-band first observation in the new recording seeds the baseline without + // touching the pair, and a leaked pair would then make the next ramp-band call with no new + // GC sample return the old recording's retained admission; // - the frontier's referrer_klass slots hold StringDictionary ids from a _class_map that // Profiler::start() is about to wipe (ids restart at 1) - they cannot be remapped, so a // chain built from a surviving entry after the boundary would name whichever class the new @@ -417,6 +421,8 @@ void ReferenceChainTracker::stop() { _urgent_latched = false; _urgent_release_ticks = 0; _admission_growth_baseline = 0; + _admission_sample_seq = 0; + _admission_decision = false; // Same per-search candidate/discovered canary state runPass()'s terminal branch and // restartSearch() clear - zeroing the counts here closes the stale-chain windows in // pollWatchedTargets() (dead-representative canary path, discovered-instances path) for the @@ -584,7 +590,9 @@ void ReferenceChainTracker::threadLoop() { // gate is bypassed while the OOM urgency ramp is active (see _oom_ramp_active's own comment). // The 100% tracking admission (setUrgentTracking(), livenessTracker.h) follows the same ramp // state, but outside the emergency band it is additionally gated on fresh heap growth evidence - // (updateUrgentAdmission()/the _admission_growth_baseline constraint): the smoothed projection + // (updateUrgentAdmission()/the _admission_growth_baseline constraint), evaluated once per new + // heap-floor sample (GC) and held until the next sample - so an admitted growth sample keeps + // the boost on until the next GC rather than for a single iteration: the smoothed projection // can stay inside the band for many GCs after the heap has physically stopped growing, and // boosting admission on a recovered spike only buys table churn. The budget/cadence ramp above // stays keyed on the raw band comparison - without a running search (the candidate gate) those @@ -828,8 +836,12 @@ bool ReferenceChainTracker::isUrgent(double seconds_to_oom) const { bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool urgent) { if (!urgent) { // Cleared when the episode ends so the next episode re-snapshots against - // its own entry state instead of inheriting a stale baseline. + // its own entry state instead of inheriting a stale baseline. The + // freshness state goes with it so a retained decision never carries into + // the next episode. _admission_growth_baseline = 0; + _admission_sample_seq = 0; + _admission_decision = false; return false; } // Emergency band: the projection says minutes at most - capture what the @@ -846,9 +858,13 @@ bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool ur _admission_growth_baseline = newest_used; } } + // Emergency admission is not growth evidence: _admission_sample_seq / + // _admission_decision stay untouched, so a later ramp-band observation + // still evaluates the newest sample against the (seeded) baseline. return true; } - u64 newest_used = LivenessTracker::instance()->newestHeapFloorSample(); + u64 newest_seq = 0; + u64 newest_used = LivenessTracker::instance()->newestHeapFloorSample(&newest_seq); if (newest_used == 0) { // No sample to reason about (the ring cannot be empty while a projection // exists - secondsToOOM() requires MIN_FILL samples - so this is purely @@ -860,16 +876,26 @@ bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool ur // only once a newer sample has grown past it - one GC away for a real // leak, never for a heap that has stopped growing. _admission_growth_baseline = newest_used; + _admission_sample_seq = newest_seq; + _admission_decision = false; return false; } + if (newest_seq == _admission_sample_seq) { + // No GC has recorded a new sample since the last evaluation: hold the + // decision. Re-comparing this sample against the baseline it may just + // have raised would always read "no growth" and drop the boost after a + // single iteration. + return _admission_decision; + } + _admission_sample_seq = newest_seq; // High-water mark: every admitted sample raises the baseline, so a heap // that plateaus above the entry level loses the boost until growth resumes // past the previous peak - a frozen entry snapshot would keep admitting it. - if (newest_used > _admission_growth_baseline) { + _admission_decision = newest_used > _admission_growth_baseline; + if (_admission_decision) { _admission_growth_baseline = newest_used; - return true; } - return false; + return _admission_decision; } void ReferenceChainTracker::publishUrgencyState(double seconds_to_oom, bool urgent) { @@ -1037,10 +1063,15 @@ void ReferenceChainTracker::resetSearchStateForTest(jvmtiEnv *jvmti, _cpu_pain_budget = PainBudget(_pain_budget_refill_rate); // Same test-isolation rationale: a latched urgency episode left behind by an earlier test would // otherwise ramp this one's cadence and pause target from a stale projection, and a stale - // admission growth baseline would skip this test's first-observation re-snapshot. + // admission growth baseline would skip this test's first-observation re-snapshot, or a retained + // admission decision (_admission_sample_seq/_admission_decision - an emergency-band re-entry + // seeds the baseline without touching them, same reason as stop()) would be returned for this + // test's first ramp-band call without a new sample. _urgent_latched = false; _urgent_release_ticks = 0; _admission_growth_baseline = 0; + _admission_sample_seq = 0; + _admission_decision = false; if (_frontier != nullptr) { // Rebuilds the table at this test's own _configured_frontier_cap, undoing any smaller framecap= diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index e8a26361eb..a5c7a5b24f 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -355,8 +355,12 @@ class ReferenceChainTracker { // running baseline: every admitted sample raises it (a plateau loses the // boost, a recovered spike never passes it), and the emergency band seeds // the same baseline at entry so the ramp-band comparison stays continuous. + // The ramp-band comparison is evaluated once per NEW heap-floor sample (one + // per GC); the decision is retained until the next sample arrives (see + // _admission_sample_seq/_admission_decision below). // Written and read by updateUrgentAdmission() on the BFS thread (threadLoop() - // via publishUrgencyState()); also reset by stop() and resetSearchStateForTest() + // via publishUrgencyState()); also reset - together with + // _admission_sample_seq/_admission_decision - by stop() and resetSearchStateForTest() // (both only after stopThread() has joined the BFS thread) and by test seams // (resetUrgencyForTest(), updateUrgentAdmissionForTest(), // publishUrgencyStateForTest()) that run only while no BFS thread is live - @@ -364,6 +368,18 @@ class ReferenceChainTracker { // 0 = no baseline for the current episode (a used value of 0 cannot be a // real sample: onGC() only records used > 0). u64 _admission_growth_baseline; + // Sample-freshness companions of _admission_growth_baseline, same owner and + // same no-atomics argument (BFS thread; reset only with no BFS thread live). + // _admission_sample_seq is the LivenessTracker heap-floor sequence number + // (newestHeapFloorSample(&seq)) of the sample the ramp-band gate last + // evaluated (0 = none this episode); _admission_decision is that + // evaluation's result. threadLoop() runs far more often than GCs record + // samples, so updateUrgentAdmission() re-evaluates only when a NEW sample + // arrives and otherwise returns the retained decision - re-comparing the + // same sample against the baseline it just raised would turn every admitted + // growth sample into a one-iteration pulse. + u64 _admission_sample_seq; + bool _admission_decision; // Wall-clock timestamp (OS::nanotime()) of the search's first pass - baseline for the TTL cutoff // above. @@ -762,7 +778,7 @@ class ReferenceChainTracker { _tags_released(true), _urgent_latched(false), _urgent_release_ticks(0), _urgency_budget_boosted(false), _configured_budget(0), - _admission_growth_baseline(0), + _admission_growth_baseline(0), _admission_sample_seq(0), _admission_decision(false), _search_state(SearchState::RUNNING), _abandon_reason(SearchAbandonReason::NONE), _search_start_ns(0), _last_pass_gc_finish_epoch(0), _last_pass_ns(0), @@ -808,8 +824,11 @@ class ReferenceChainTracker { bool isUrgent() const; bool isUrgent(double seconds_to_oom) const; - // Computes the 100% tracking-admission decision for one threadLoop() - // iteration and maintains _admission_growth_baseline. Raw `urgent` still + // Computes the 100% tracking-admission decision. Outside the emergency band + // the decision is re-evaluated only when a new heap-floor sample arrives and + // is retained between samples (threadLoop() iterates far more often than GCs + // record samples); maintains _admission_growth_baseline, + // _admission_sample_seq and _admission_decision. Raw `urgent` still // drives the budget/cadence ramp; admission alone is additionally gated on // heap growth evidence outside the emergency band - see // _admission_growth_baseline's comment for why band residence is not @@ -1204,6 +1223,14 @@ class ReferenceChainTracker { // updateUrgentAdmission()'s episode baseline - a stale one across tests // would make an episode's "first observation" branch unreachable. _admission_growth_baseline = 0; + // The freshness pair needs its own reset even though the baseline reset + // already forces a ramp-band re-entry through the first-observation + // branch: an emergency-band re-entry seeds the baseline without touching + // _admission_sample_seq/_admission_decision, so a leaked pair would make + // the next ramp-band call with no new sample return the previous test's + // retained decision. + _admission_sample_seq = 0; + _admission_decision = false; } // Applied vs configured CPU pain-budget refill rate: shouldRunPass() raises // the applied rate CANARY_PAIN_BUDGET_REFILL_MULTIPLIER-fold only while a diff --git a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp index b91190d977..09d6015d77 100644 --- a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp +++ b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp @@ -1212,6 +1212,35 @@ class LivenessTrackerTestAccessor { t->_leak_signal_activated_ns.store(saved_stamp, std::memory_order_release); } + // Like initializeGatesOnlyForTest(), but presets the burn-in stamp to + // `preset`, runs initialize() (with _initialized forced true, so it reaches + // the burn-in CAS and then takes the early return), returns the stamp + // initialize() left behind, and restores every field it touched - the stamp + // included - so the singleton is unchanged afterwards (only + // _gc_generations and _leak_signal_enabled keep the derived values, as in + // initializeGatesOnlyForTest()). + static u64 stampAfterInitializeForTest(LivenessTracker *t, Arguments &args, u64 preset) { + bool saved_initialized = t->_initialized; + bool saved_enabled = t->_enabled; + bool saved_heap_usage = t->_record_heap_usage; + int saved_watched = __atomic_load_n(&t->_watched_tid_count, __ATOMIC_ACQUIRE); + bool saved_urgent = __atomic_load_n(&t->_urgent_tracking, __ATOMIC_ACQUIRE); + u64 saved_stamp = t->_leak_signal_activated_ns.load(std::memory_order_acquire); + t->_leak_signal_activated_ns.store(preset, std::memory_order_release); + t->_initialized = true; + // Returns _stored_error from any prior real start - not under test. + Error ignored = t->initialize(args); + (void)ignored; + u64 result = t->_leak_signal_activated_ns.load(std::memory_order_acquire); + t->_initialized = saved_initialized; + t->_enabled = saved_enabled; + t->_record_heap_usage = saved_heap_usage; + __atomic_store_n(&t->_watched_tid_count, saved_watched, __ATOMIC_RELEASE); + __atomic_store_n(&t->_urgent_tracking, saved_urgent, __ATOMIC_RELEASE); + t->_leak_signal_activated_ns.store(saved_stamp, std::memory_order_release); + return result; + } + static bool entryAgeAndAdmissionForTest(LivenessTracker *t, u32 idx, jlong *age, u64 *admission_epoch) { @@ -1314,6 +1343,75 @@ TEST(LivenessTrackerSessionTest, LeakSignalDerivedByInitialize) { EXPECT_FALSE(tracker->leakSignalEnabled()); } +// The burn-in stamp (_leak_signal_activated_ns) marks the first EFFECTIVE +// leak-signal activation: initialize() must gate its CAS on the derived leak +// signal (generations || (liveheap && referencechains)), not on the coarser +// _enabled (generations || liveheap). A plain liveheap start stamping it would +// let a later reference-chain recording inherit an already-expired burn-in. +// A pre-existing stamp is never overwritten. Every row runs with _initialized +// forced true, so a stamp proves the CAS sits ahead of the early return. +TEST(LivenessTrackerSessionTest, BurnInStampFollowsEffectiveLeakSignal) { + LivenessTracker *tracker = LivenessTracker::instance(); + constexpr u64 SENTINEL = 0x1234ULL; + struct Row { + const char *spec; + u64 preset; + bool expect_fresh_stamp; // true: non-zero, != preset; false: == preset + }; + const Row rows[] = { + {"memory=1048576:l", 0, false}, + {"memory=1048576:l,referencechains=true", 0, true}, + {"generations=true", 0, true}, + {"generations=true,memory=1048576:l", 0, true}, + {"memory=1048576:a", 0, false}, + {"memory=1048576:l,referencechains=true", SENTINEL, false}, + {"generations=true", SENTINEL, false}, + {"memory=1048576:l", SENTINEL, false}, + }; + for (const Row &row : rows) { + Arguments args; + ASSERT_FALSE(args.parse(row.spec)) << "row " << row.spec; + u64 stamp_before = LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker); + u64 stamp = LivenessTrackerTestAccessor::stampAfterInitializeForTest(tracker, args, + row.preset); + if (row.expect_fresh_stamp) { + EXPECT_NE(0u, stamp) << "row " << row.spec << " preset " << row.preset + << ": a leak-signal start must stamp the burn-in clock"; + } else { + EXPECT_EQ(row.preset, stamp) + << "row " << row.spec << " preset " << row.preset + << ": the stamp must be left alone (no leak signal, or already stamped)"; + } + EXPECT_EQ(stamp_before, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)) + << "row " << row.spec << ": the accessor must restore the stamp"; + } + LivenessTrackerTestAccessor::setSessionGatesForTest( + tracker, /*enabled=*/false, /*generations=*/false); +} + +// Reviewer scenario: a first plain liveheap recording followed (later) by a +// liveheap + referencechains recording. The burn-in must start at the second +// (first leak-signal) start, not at the plain liveheap start. +TEST(LivenessTrackerSessionTest, PlainLiveheapStartDoesNotConsumeBurnIn) { + LivenessTracker *tracker = LivenessTracker::instance(); + Arguments plain; + ASSERT_FALSE(plain.parse("memory=1048576:l")); + Arguments chains; + ASSERT_FALSE(chains.parse("memory=1048576:l,referencechains=true")); + + u64 s = LivenessTrackerTestAccessor::stampAfterInitializeForTest(tracker, plain, 0); + EXPECT_EQ(0u, s) << "a plain liveheap start must not stamp the burn-in clock"; + + u64 before_second = OS::nanotime(); + u64 s2 = LivenessTrackerTestAccessor::stampAfterInitializeForTest(tracker, chains, s); + EXPECT_NE(0u, s2) << "the first leak-signal start must stamp the burn-in clock"; + EXPECT_GE(s2, before_second) + << "the burn-in must start at the first leak-signal start, not the plain liveheap start"; + + LivenessTrackerTestAccessor::setSessionGatesForTest( + tracker, /*enabled=*/false, /*generations=*/false); +} + // setLeakSignalSourcesForTest() applies initialize()'s derivation, and // setGcGenerationsForTest(v) keeps setting BOTH flags to v (existing callers // depend on it). @@ -3174,6 +3272,8 @@ TEST_F(LivenessTrackerMockJvmTest, LiveheapLeakSignalArmingRequiresReferenceChai EXPECT_FALSE(tracker->gcGenerationsEnabled()); EXPECT_FALSE(tracker->leakSignalEnabled()) << "a plain liveheap session has no signal consumer - the gate must stay off"; + EXPECT_EQ(0u, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)) + << "a plain liveheap start() must not consume the burn-in stamp"; // The consumers keyed on the gate stay idle: no projection even with a // rising ring, and no forced sweep folding the population. diff --git a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc index 6724c91132..19f7a00179 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc @@ -568,6 +568,256 @@ TEST_F(OomUrgencyTest, BandExitResetsGrowthBaseline) { EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); } +// The ramp-band growth gate is evaluated once per NEW heap-floor sample (one +// per GC), but threadLoop() calls it every iteration (~1s). Admitting raises +// the baseline to the admitted sample, so re-comparing that same sample on +// the next iteration would read "no growth" and turn the boost into a +// one-iteration pulse per growing GC. The decision must be held until a +// genuinely new sample arrives. +TEST_F(OomUrgencyTest, AdmissionHeldUntilNextHeapFloorSample) { + ASSERT_NEAR(project(1500.0), 1500.0, 1.0); + u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + // No new sample: the admission must hold across iterations, also while the + // (passed-in) projection drifts inside the ramp band. + for (double s : {1500.0, 1490.0, 1480.0, 1500.0}) { + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(s, true)) + << "admission must hold until a new heap-floor sample arrives (projection " << s + << ")"; + } + + // A new sample below the raised high-water mark closes the held state, and + // the closed state is held as well. + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE / 2, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); +} + +// A non-admitting evaluation is held just like an admitting one: repeated +// iterations without a new sample must not spuriously re-admit. +TEST_F(OomUrgencyTest, NonAdmittingDecisionHeldUntilNextSample) { + ASSERT_NEAR(project(1500.0), 1500.0, 1.0); + u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + // New sample equal to the raised mark: not growth. + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + for (int i = 0; i < 3; i++) { + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "repeat " << i << ": a held non-admitting decision must not re-admit"; + } + + // Non-vacuity: growth past the high-water mark re-admits. + LivenessTracker::instance()->heapFloorRecordForTest(peak + 2 * RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); +} + +// The first observation of an episode snapshots the baseline and does not +// admit; repeating it without a new sample must neither admit nor move the +// baseline. +TEST_F(OomUrgencyTest, FirstObservationStaysUnadmittedWithoutNewSample) { + ASSERT_NEAR(project(1500.0), 1500.0, 1.0); + u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + for (int i = 0; i < 3; i++) { + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "repeat " << i; + } + // Control: the baseline is still the entry sample. + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); +} + +// Emergency admission is not growth evidence and does not consume sample +// freshness: a ramp-band observation of the very sample the emergency band +// seeded evaluates it (equality is not growth) and holds that false - a stale +// true must not be retained from the emergency call. +TEST_F(OomUrgencyTest, EmergencySeedThenRampSameSampleStaysUnadmitted) { + ASSERT_NEAR(project(299.0), 299.0, 1.0); + u64 entry = MAX_HEAP - (u64)(299.0 * (double)RATE / (double)SAMPLE_SPACING_S); + ASSERT_EQ(entry, LivenessTracker::instance()->newestHeapFloorSample()); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(299.0, true)); + + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(400.0, true)) + << "the emergency-seeded sample is not growth past itself"; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(400.0, true)); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(400.0, true)); + + LivenessTracker::instance()->heapFloorRecordForTest(entry + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(400.0, true)); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(400.0, true)) + << "admission must hold until a new heap-floor sample arrives"; +} + +// An emergency-band observation in the middle of a held ramp-band admission +// admits (as always) and neither resets nor re-evaluates the retained ramp +// decision. +TEST_F(OomUrgencyTest, EmergencyBandDoesNotDisturbRetainedRampDecision) { + ASSERT_NEAR(project(1500.0), 1500.0, 1.0); + u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(299.0, true)); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "the retained ramp decision must survive an emergency-band observation"; + + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); +} + +// The reset tests below re-enter through the emergency band on purpose: every +// reset site also zeroes the growth baseline, so a ramp-band re-entry with no +// new sample would take the first-observation branch (which overwrites the +// freshness pair) and return false either way. The emergency band seeds the +// baseline WITHOUT touching the freshness pair, so only a leaked pair can make +// the following ramp-band call (same sample) return the stale true. + +// Episode end (!urgent) must drop the retained admission along with the +// baseline. +TEST_F(OomUrgencyTest, EpisodeEndDropsRetainedAdmission) { + ASSERT_NEAR(project(1500.0), 1500.0, 1.0); + u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(-1.0, false)); + + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(299.0, true)); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "the !urgent branch must clear the retained admission decision and sample seq"; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + LivenessTracker::instance()->heapFloorRecordForTest(peak + 2 * RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); +} + +// resetUrgencyForTest() must clear the retained admission as well. +TEST_F(OomUrgencyTest, UrgencyResetSeamClearsRetainedAdmission) { + ASSERT_NEAR(project(1500.0), 1500.0, 1.0); + u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + ReferenceChainsTestAccessor::resetUrgencyForTest(); + + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(299.0, true)); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(400.0, true)) + << "resetUrgencyForTest() must clear the retained admission decision and sample seq"; + + LivenessTracker::instance()->heapFloorRecordForTest(peak + 2 * RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(400.0, true)); +} + +// publishUrgencyState() must forward the retained decision: the 100% boost +// observed through LivenessTracker's admission gate stays on between GCs. +TEST_F(OomUrgencyTest, PublishedBoostHeldUntilNextHeapFloorSample) { + LivenessTracker *lt = LivenessTracker::instance(); + struct Restore { + ~Restore() { + ReferenceChainsTestAccessor::publishUrgencyStateForTest(-1.0, false); + LivenessTracker::instance()->admissionResetForTest(); + LivenessTracker::instance()->setSubsampleRatioForTest(0.1); + } + } restore; + lt->admissionResetForTest(); + const jint TID = 1234; + + ASSERT_NEAR(project(1500.0), 1500.0, 1.0); + u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); + + ReferenceChainsTestAccessor::publishUrgencyStateForTest(1500.0, true); + EXPECT_FALSE(lt->admitForTrackingForTest(TID)) << "entry observation must not boost"; + + lt->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + ReferenceChainsTestAccessor::publishUrgencyStateForTest(1490.0, true); + EXPECT_TRUE(lt->admitForTrackingForTest(TID)) << "growth must boost"; + + for (int i = 0; i < 2; i++) { + ReferenceChainsTestAccessor::publishUrgencyStateForTest(1490.0, true); + EXPECT_TRUE(ReferenceChainsTestAccessor::oomRampActiveForTest()) << i; + EXPECT_TRUE(lt->admitForTrackingForTest(TID)) + << "repeat " << i << ": boost must persist until the next GC sample"; + } + + ReferenceChainsTestAccessor::publishUrgencyStateForTest(-1.0, false); + EXPECT_FALSE(lt->admitForTrackingForTest(TID)); +} + +// newestHeapFloorSample(&seq) contract: seq identifies one recorded sample, +// strictly increases per sample (also for equal used/timestamp values), is +// stable on re-read, is 0 for an empty ring, and survives the ring resets - +// so a reset ring followed by a new sample never reproduces a cached seq. +TEST_F(OomUrgencyTest, HeapFloorSequenceIsMonotonicAcrossResets) { + LivenessTracker *lt = LivenessTracker::instance(); + lt->heapFloorResetForTest(); + u64 seq = 123; + EXPECT_EQ(0u, lt->newestHeapFloorSample(&seq)); + EXPECT_EQ(0u, seq) << "an empty ring must write 0 to seq_out"; + + lt->heapFloorRecordForTest(500 * MiB, 7, 0); + u64 s1 = 0; + EXPECT_EQ(500 * MiB, lt->newestHeapFloorSample(&s1)); + EXPECT_NE(0u, s1); + EXPECT_EQ(500 * MiB, lt->newestHeapFloorSample()); + + lt->heapFloorRecordForTest(500 * MiB, 7, 0); + u64 s2 = 0; + EXPECT_EQ(500 * MiB, lt->newestHeapFloorSample(&s2)); + EXPECT_GT(s2, s1) << "identity is per sample, not per value/timestamp"; + + u64 s2_again = 0; + lt->newestHeapFloorSample(&s2_again); + EXPECT_EQ(s2, s2_again) << "a re-read must report the same sample"; + + lt->heapFloorResetForTest(); + lt->heapFloorRecordForTest(500 * MiB, 7, 0); + u64 s3 = 0; + lt->newestHeapFloorSample(&s3); + EXPECT_GT(s3, s2) << "heapFloorResetForTest() must not reset the sample counter"; + + lt->klassPopulationResetForTest(); + lt->heapFloorRecordForTest(500 * MiB, 7, 0); + u64 s4 = 0; + lt->newestHeapFloorSample(&s4); + EXPECT_GT(s4, s3) << "klassPopulationResetForTest() must not reset the sample counter"; + + for (int i = 0; i < 31; i++) { + lt->heapFloorRecordForTest(500 * MiB + (u64)i, 7, 0); + } + u64 s5 = 0; + EXPECT_EQ(500 * MiB + 30, lt->newestHeapFloorSample(&s5)); + EXPECT_EQ(31u, s5 - s4) << "one increment per sample, newest slot after a ring wrap"; + + lt->heapFloorResetForTest(); +} + // The canary-refill half: shouldRunPass() raises the CPU pain budget's // refill rate CANARY_PAIN_BUDGET_REFILL_MULTIPLIER-fold exactly while a // canary chase is open (candidates exist and not all are found), and puts it @@ -719,6 +969,67 @@ TEST_F(ReferenceChainsCanaryRefillTest, StopClearsAdmissionGrowthBaseline) { EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); } +// stop() must also clear the retained admission (_admission_sample_seq / +// _admission_decision), not only the growth baseline. The heap-floor ring +// survives the recording boundary, so the admitted sample stays newest; an +// emergency-band first observation in the new recording seeds the baseline +// without touching the freshness pair, and a leaked pair would then make the +// next ramp-band call (no new GC sample) return the old recording's retained +// admission. Same BFS-thread precondition as StopClearsAdmissionGrowthBaseline. +TEST_F(ReferenceChainsCanaryRefillTest, StopClearsRetainedAdmission) { + + constexpr u64 MiB = 1ULL << 20; + LivenessTracker *lt = LivenessTracker::instance(); + ReferenceChainsTestAccessor::reset(); // also resetUrgencyForTest() +#ifdef DEBUG + lt->setHeapFloorRecordingForTest(false); +#endif + lt->heapFloorResetForTest(); + ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); + Arguments args; + ASSERT_FALSE(args.parse("referencechains=true")); + ASSERT_FALSE(tracker->start(args)); + + struct Cleanup { + ReferenceChainTracker *tracker; + LivenessTracker *lt; + ~Cleanup() { + tracker->stop(); + ReferenceChainsTestAccessor::resetUrgencyForTest(); + lt->heapFloorResetForTest(); // while recording is still disabled +#ifdef DEBUG + lt->setHeapFloorRecordingForTest(true); +#endif + ReferenceChainsTestAccessor::reset(); + } + } cleanup{tracker, lt}; + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()) + << "updateUrgentAdmissionForTest() would race threadLoop()"; + + // Recording 1: snapshot 500MiB, admit 600MiB, hold the admission. + lt->heapFloorRecordForTest(500 * MiB, 1, 0); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + lt->heapFloorRecordForTest(600 * MiB, 2, 0); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + tracker->stop(); + ASSERT_FALSE(tracker->start(args)); + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()) + << "updateUrgentAdmissionForTest() would race threadLoop()"; + + // Recording 2, no new sample: emergency entry seeds the baseline at + // 600MiB; the ramp-band call must evaluate (600 > 600 is false), not + // return recording 1's retained true. + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(299.0, true)); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "stop() must clear the retained admission decision and sample seq"; + + // Positive control: growth past the seeded baseline admits. + lt->heapFloorRecordForTest(700 * MiB, 3, 0); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); +} + TEST_F(ReferenceChainsCanaryRefillTest, BackoffGateHoldsUnlessOomRampActive) { ReferenceChainsTestAccessor::reset(); ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); diff --git a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h index ec5774a696..1134cf7460 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h +++ b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h @@ -183,8 +183,9 @@ class ReferenceChainsTestAccessor { static void resetUrgencyForTest() { ReferenceChainTracker::instance()->resetUrgencyForTest(); } - // Urgent-admission growth gate: updateUrgentAdmission()'s per-iteration - // decision (referenceChains.cpp) - the admission half of threadLoop()'s + // Urgent-admission growth gate: updateUrgentAdmission()'s urgent admission + // decision (referenceChains.cpp; re-evaluated only when a new heap-floor + // sample arrives, retained between samples) - the admission half of threadLoop()'s // ramp handling, testable without the BFS thread. static bool updateUrgentAdmissionForTest(double seconds_to_oom, bool urgent) { return ReferenceChainTracker::instance()->updateUrgentAdmission(seconds_to_oom, diff --git a/doc/architecture/ReferenceChainsSignalsExplained.md b/doc/architecture/ReferenceChainsSignalsExplained.md index b71b61981b..22a296fbf7 100644 --- a/doc/architecture/ReferenceChainsSignalsExplained.md +++ b/doc/architecture/ReferenceChainsSignalsExplained.md @@ -116,7 +116,7 @@ neither source provides a valid estimate, or that the projection is still in its Two ranges have different purposes: -- At less than 30 minutes, the OOM ramp increases the pause target, shortens the cadence, and increases the edge-budget ceiling. The 100% liveness-sampling admission follows the same band, but outside the emergency band it additionally requires fresh growth evidence: the newest heap-floor sample must exceed the value observed when the ramp episode began. The projection is a smoothed regression over a 30-sample window, so a single spike sample keeps the projection inside the band for several recovery GCs after the heap has physically stopped growing. Growth evidence is what keeps the admission boost off for such a recovered spike. Inside the emergency band (less than 5 minutes), admission is immediate. +- At less than 30 minutes, the OOM ramp increases the pause target, shortens the cadence, and increases the edge-budget ceiling. The 100% liveness-sampling admission follows the same band, but outside the emergency band it additionally requires fresh growth evidence: the newest heap-floor sample must exceed the episode's running high-water mark (the entry value, raised by every admitted sample). The growth check is evaluated once per new heap-floor sample (one per GC), and its result is held until the next sample arrives, so an admitted growth sample keeps the boost on until the next GC. The projection is a smoothed regression over a 30-sample window, so a single spike sample keeps the projection inside the band for several recovery GCs after the heap has physically stopped growing. Growth evidence is what keeps the admission boost off for such a recovered spike. Inside the emergency band (less than 5 minutes), admission is immediate. - At less than 5 minutes, `isUrgent()` latches an urgent episode. The latch does not start searches, and the ramp calculation does not consult it. Its only consumer is the no-progress abandonment of a running search, which it suppresses while the latch is held. Both ranges apply only after the burn-in period. From 801beb165003b29d2a23a473119669fdb9da7464 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Mon, 2 Nov 2026 15:24:55 +0100 Subject: [PATCH 22/37] Address quick-review findings: atomic heap-floor seq, 300s boundary test, baseline mutant control, wider include patterns --- .../native/gtest/GtestTaskBuilder.kt | 19 ++++---- ddprof-lib/src/main/cpp/livenessTracker.cpp | 19 ++++++-- ddprof-lib/src/main/cpp/livenessTracker.h | 15 +++--- .../src/test/cpp/referenceChainsOomTests.inc | 48 ++++++++++++++++++- 4 files changed, 82 insertions(+), 19 deletions(-) diff --git a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt index 31ac9cf8eb..f4fe062e9c 100644 --- a/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt +++ b/build-logic/conventions/src/main/kotlin/com/datadoghq/native/gtest/GtestTaskBuilder.kt @@ -128,19 +128,22 @@ class GtestTaskBuilder( ) } - // Test .inc/.h files resolve via quoted includes relative to the test - // directory and appear in no include path, so register them here or an - // .inc-only edit reports UP-TO-DATE and runs a stale binary. Inputs, - // not sources: compiling an .inc standalone would emit duplicate symbols. - // The sweep is deliberately broad (every test-dir .inc/.h for every gtest - // task): the test dir is flat with a handful of shared headers, mapping - // headers to the tests that include them would need include parsing, and + // Test header/include files (.inc/.h/.hpp/.hh/.hxx/.ipp) resolve via quoted + // includes relative to the test directory and appear in no include path, + // so register them here or an .inc-only edit reports UP-TO-DATE and runs a + // stale binary. Inputs, not sources: compiling an .inc standalone would + // emit duplicate symbols. A new test-local include extension must be + // added to the pattern list below, or edits to such files will run a + // stale test binary. + // The sweep is deliberately broad (every test-dir header/include file for + // every gtest task): the test dir is flat with a handful of shared + // headers, mapping headers to the tests that include them would need include parsing, and // over-invalidation is preferred to running a stale binary. The input is // named and RELATIVE so relocating the checkout/build dir does not change // its fingerprint. inputs.files( project.fileTree(testFile.parentFile) { - include("**/*.inc", "**/*.h") + include("**/*.inc", "**/*.h", "**/*.hpp", "**/*.hh", "**/*.hxx", "**/*.ipp") } ) .withPropertyName("testIncludeInputs") diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index ba286893aa..25e18033f9 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -1536,14 +1536,25 @@ void LivenessTracker::recordHeapFloorSampleUnchecked(u64 used, u64 timestamp_ns, // for that slot is visible".) _heap_floor_time_ring's, // _container_mem_ring's and _heap_floor_seq_ring's payload writes below are // plain stores for the same reason - they precede the same storeRelease() - // in program order. _heap_floor_sample_seq itself is only ever read and - // written here (single writer at a time), readers see it via the slot ring. + // in program order. + // + // _heap_floor_sample_seq is bumped with an atomic relaxed fetch-add, so two + // overlapping callers can never obtain the same seq; readers see it only via + // the slot ring. The head/slot writes themselves still assume one writer at + // a time: HotSpot never posts GarbageCollectionFinish concurrently, but + // JVMTI does not document that, so on other VMs (J9, Zing via the + // abstraction layer) two overlapping callbacks could overwrite the same + // slot. Overlap can lose a sample but never produces a duplicate seq, so the + // urgent-admission freshness gate cannot mistake a new sample for an + // already-consumed one. u8 head = load(_heap_floor_ring_head); store(_heap_floor_ring[head], used); store(_heap_floor_time_ring[head], timestamp_ns); store(_container_mem_ring[head], container_used); - u64 seq = _heap_floor_sample_seq + 1; - _heap_floor_sample_seq = seq; + // Atomic bump: seq uniqueness must not depend on the single-writer + // assumption (see above). Relaxed suffices - readers only see seq via the + // slot store below, published by the head storeRelease(). + u64 seq = __atomic_add_fetch(&_heap_floor_sample_seq, (u64)1, __ATOMIC_RELAXED); store(_heap_floor_seq_ring[head], seq); storeRelease(_heap_floor_ring_head, (u8)((head + 1) % KLASS_POPULATION_RING_SIZE)); u8 fill = load(_heap_floor_ring_fill); diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index 9e31ae7dcf..e4e74f49ca 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -371,7 +371,9 @@ class alignas(alignof(SpinLock)) LivenessTracker { // resolveKlassId()) under the exclusive _table_lock on another thread, and // such a call can wait for that stop to end - spinning on _table_lock here // would then never return. GC completions are never - // concurrent with each other (HotSpot never runs two GCs at once), so + // concurrent with each other (HotSpot never runs two GCs at once - a + // HotSpot behaviour, not a documented JVMTI contract; other VMs such as + // J9/Zing behind the abstraction layer may differ), so // onGC() is always a single writer at a time, matching the existing // lock-free _gc_epoch/_used_after_last_gc fields' own assumption - see // recordHeapFloorSample()/heapFloorRising() (livenessTracker.cpp) for the @@ -402,11 +404,12 @@ class alignas(alignof(SpinLock)) LivenessTracker { volatile u8 _heap_floor_ring_head; volatile u8 _heap_floor_ring_fill; // Monotonic heap-floor sample counter (first sample = 1, 0 = "no sample"). - // Written only by recordHeapFloorSampleUnchecked() (single writer at a time, - // same argument as _heap_floor_ring) and published through the slot ring - // above; never reset - not by heapFloorResetForTest(), not by - // klassPopulationResetForTest(), not by stop()/start() - so a sequence value - // can never alias an earlier sample. + // Bumped only by recordHeapFloorSampleUnchecked() via an atomic relaxed + // fetch-add (__atomic_add_fetch), so values stay unique even if the + // single-writer assumption on _heap_floor_ring does not hold on a given VM, + // and published through the slot ring above; never reset - not by + // heapFloorResetForTest(), not by klassPopulationResetForTest(), not by + // stop()/start() - so a sequence value can never alias an earlier sample. u64 _heap_floor_sample_seq; // Debug-only test seam: when true, onGC() skips recordHeapFloorSample() diff --git a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc index 19f7a00179..cfc59bb68e 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc @@ -518,10 +518,47 @@ TEST_F(OomUrgencyTest, EmergencyBandEntrySeedsGrowthBaseline) { EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(400.0, true)); } +// Boundary of the emergency band: a projection of exactly OOM_URGENT_THRESHOLD_S +// (300s) is ramp band, not emergency. updateUrgentAdmission() keeps the +// growth-evidence hold (first observation snapshots and does not admit; only a +// strictly larger newer sample admits), and isUrgent() does not latch. A +// `<` -> `<=` slip at either threshold comparison fails here. +TEST_F(OomUrgencyTest, ProjectionAtUrgentThresholdIsRampBandAndDoesNotLatch) { + ASSERT_NEAR(project(300.0), 300.0, 1.0); + u64 peak = MAX_HEAP - (u64)(300.0 * (double)RATE / (double)SAMPLE_SPACING_S); + ASSERT_EQ(peak, LivenessTracker::instance()->newestHeapFloorSample()) + << "scenario requires the entry snapshot to be the ramp peak"; + + // Exactly at the threshold: no emergency waiver, so the first observation + // only snapshots the entry baseline. + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(300.0, true)) + << "a projection equal to OOM_URGENT_THRESHOLD_S must not take the emergency band"; + + // Equal newer sample: not growth. + LivenessTracker::instance()->heapFloorRecordForTest(peak, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(300.0, true)) + << "an equal newer sample is not growth evidence"; + + // Strictly larger newer sample: ramp-band growth admits (non-vacuity). + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(300.0, true)) + << "ramp-band growth must admit at the threshold projection"; + + // isUrgent(): 300.0 does not latch; just below does. + ASSERT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()); + EXPECT_FALSE(ReferenceChainsTestAccessor::isUrgentWithProjectionForTest(300.0)) + << "a projection equal to OOM_URGENT_THRESHOLD_S must not latch urgency"; + EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()); + EXPECT_TRUE(ReferenceChainsTestAccessor::isUrgentWithProjectionForTest(299.9)); + EXPECT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()); +} + // The growth baseline is a high-water mark: every admitted sample raises it, // so a heap that plateaus above the episode's entry level loses the boost - // comparing against a frozen entry snapshot would keep admitting a heap that -// has stopped growing. +// has stopped growing - and non-admitted samples never lower it. TEST_F(OomUrgencyTest, PlateauAboveEntryBaselineLosesBoost) { ASSERT_NEAR(project(1500.0), 1500.0, 1.0); u64 peak = MAX_HEAP - (u64)(1500.0 * (double)RATE / (double)SAMPLE_SPACING_S); @@ -539,6 +576,15 @@ TEST_F(OomUrgencyTest, PlateauAboveEntryBaselineLosesBoost) { EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) << "a plateau above the entry level is not growth evidence"; + // A non-admitted sample must not move the high-water mark: a later sample + // equal to the previous peak (peak + RATE) is still not growth. Raising + // the baseline on every sample would have dragged it down to + // peak + RATE / 2 and admitted this one. + LivenessTracker::instance()->heapFloorRecordForTest(peak + RATE, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "a non-admitted sample must not lower the growth baseline"; + // Growth resuming past the previous peak re-admits. LivenessTracker::instance()->heapFloorRecordForTest(peak + 2 * RATE, _clock_ns, 0); _clock_ns += SAMPLE_SPACING_S * SEC_NS; From 853a6fd32c20daffb60d2da4581103ca4949472f Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Mon, 2 Nov 2026 16:23:13 +0100 Subject: [PATCH 23/37] Fix churn test losing races against the abort thread's stop_flag The abort thread stopped the pass thread once its 30 abort windows completed; on a loaded runner the flag's duty cycle truncates every walk, so the churn could end before any search reached a terminal outcome (restarts==0, observed in fuzz-build-and-soak). The pass loop now exits only via its own bounds or the !ok early-exit. --- .../src/test/cpp/referenceChainsLifecycleTests.inc | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc b/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc index 9c13bdc7ae..e1b2e8a68d 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsLifecycleTests.inc @@ -80,7 +80,6 @@ TEST_F(ReferenceChainsLifecycleTest, ChurnDuringConcurrentPassesStaysConsistent) constexpr int kGcCycles = 200; constexpr int kAbortCycles = 30; - std::atomic stop_flag{false}; std::atomic pass_done{false}; std::atomic restarts{0}; std::atomic aborted_passes{0}; @@ -99,6 +98,10 @@ TEST_F(ReferenceChainsLifecycleTest, ChurnDuringConcurrentPassesStaysConsistent) // construction. std::atomic first_window_open{false}; + // No stop_flag: the pass loop's only exits are its own bounds (kMaxSearches/kMaxPasses) and + // the !ok early-exit. The abort thread must not stop it - its 30 windows can complete faster + // than the first search terminates (the abort flag's duty cycle truncates every walk in + // between), and stopping then would end the churn with zero terminal outcomes. std::thread pass_thread([&] { // Deterministic window 1: do not start passes until the abort thread's first store is // visible (see first_window_open's comment). Bounded so a dead abort thread cannot hang @@ -110,8 +113,7 @@ TEST_F(ReferenceChainsLifecycleTest, ChurnDuringConcurrentPassesStaysConsistent) } for (int searches = 0; searches < kMaxSearches && - total_passes.load(std::memory_order_relaxed) < kMaxPasses && - !stop_flag.load(std::memory_order_relaxed); + total_passes.load(std::memory_order_relaxed) < kMaxPasses; /* advanced by the restart below */) { total_passes.fetch_add(1, std::memory_order_relaxed); bool truncated = true; @@ -123,7 +125,6 @@ TEST_F(ReferenceChainsLifecycleTest, ChurnDuringConcurrentPassesStaysConsistent) if (!ok) { // Disabled/no-frontier early exit - nothing sensible is // running anymore; stop churning. - stop_flag.store(true, std::memory_order_relaxed); break; } // The request is held far longer than one pass, so a read here is @@ -161,7 +162,6 @@ TEST_F(ReferenceChainsLifecycleTest, ChurnDuringConcurrentPassesStaysConsistent) restarts.fetch_add(1, std::memory_order_relaxed); searches++; } - stop_flag.store(true, std::memory_order_relaxed); pass_done.store(true, std::memory_order_relaxed); }); @@ -208,7 +208,6 @@ TEST_F(ReferenceChainsLifecycleTest, ChurnDuringConcurrentPassesStaysConsistent) usleep(100); } ReferenceChainsTestAccessor::setAbortPassRequestedForTest(false); - stop_flag.store(true, std::memory_order_relaxed); }); pass_thread.join(); From e0134d72e5fdd4df1cf64d690bb7280994084549 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 12:22:22 +0100 Subject: [PATCH 24/37] livenessTracker: per-slot seqlock validation for heap-floor ring reads newestHeapFloorSample() could pair one sample's used value with another sample's seq after a full ring wrap; validate each slot's payload with a seqlock word (CAS claim, bounded reader retries, drop-on-overlap writers) and count dropped samples and exhausted retries for diagnostics. --- ddprof-lib/src/main/cpp/counters.h | 20 +- ddprof-lib/src/main/cpp/livenessTracker.cpp | 115 ++++--- ddprof-lib/src/main/cpp/livenessTracker.h | 65 ++-- .../src/test/cpp/livenessTracker_ut.cpp | 282 ++++++++++++++++++ 4 files changed, 412 insertions(+), 70 deletions(-) diff --git a/ddprof-lib/src/main/cpp/counters.h b/ddprof-lib/src/main/cpp/counters.h index c431285097..38285c0ca3 100644 --- a/ddprof-lib/src/main/cpp/counters.h +++ b/ddprof-lib/src/main/cpp/counters.h @@ -293,9 +293,25 @@ /* hasLeakSignal() gate evaluations refused while the OOM urgency latch is \ * set and selectLeakCandidates() found zero candidates - a heap-wide leak \ * no single klass explains, so no reference-chain search may start. \ - * Counts evaluations (about 1/s while latched and idle), not urgency \ - * episodes: read it as a rate. */ \ + * One increment per refused evaluation, not per urgency episode: evaluation \ + * runs only after canAffordNewSearch()'s safepoint budget check and follows \ + * the ramped cadence. */ \ X(REFERENCE_CHAIN_URGENT_NO_CANDIDATE, "reference_chain_urgent_no_candidate") \ + /* Heap-floor seqlock: samples DROPPED by a writer that lost the per-slot \ + * claim (head slot already at HEAP_FLOOR_SEQ_UNSTABLE, or the claim CAS \ + * lost to an overlapping writer). One increment per dropped sample, in \ + * GC-callback context - no logging there, so this counter is the only \ + * observability for overlap-driven sample loss. Never fires while GC \ + * callbacks are serialized (HotSpot). */ \ + X(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP, "heap_floor_sample_dropped_overlap") \ + /* Heap-floor seqlock: reads that exhausted HEAP_FLOOR_READ_ATTEMPTS \ + * without a consistent (used, seq) pair ("unstable read", not strictly \ + * "frozen admission evaluation" - it also counts the emergency-band \ + * baseline seed's seq_out==nullptr read). One increment per exhausted \ + * read, never per attempt; the fill==0 empty-ring return does not count. \ + * A steadily rising value means the urgent admission gate is holding its \ + * previous decision because overlapping writers pin the newest slot. */ \ + X(HEAP_FLOOR_READ_UNSTABLE, "heap_floor_read_unstable") \ /* admitStaticFieldRoots() per-class non-static quota: non-STATIC_FIELD \ * edges (CONSTANT_POOL, INTERFACE, SUPERCLASS, CLASS_LOADER, ...) that \ * were dropped because the class already hit \ diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index 25e18033f9..c1135c133c 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -1478,31 +1478,56 @@ void LivenessTracker::recordTidTrendSamplesLocked( } u64 LivenessTracker::newestHeapFloorSample(u64 *seq_out) const { - // Matches recordHeapFloorSampleUnchecked()'s storeRelease() on both index - // fields - loadAcquire() here is what makes the payload read below visible. - u8 fill = loadAcquire(_heap_floor_ring_fill); - u8 head = loadAcquire(_heap_floor_ring_head); - if (fill == 0) { - if (seq_out != nullptr) { - *seq_out = 0; + // Per-slot seqlock read: the slot's seq validates the payload below it + // (writer: recordHeapFloorSampleUnchecked()). Bounded retry, never a torn + // (used, seq) pair. + for (int attempt = 0; attempt < HEAP_FLOOR_READ_ATTEMPTS; attempt++) { + // Acquire pairs with the writer's head/fill release stores; it makes the + // payload of every slot the indices name visible. + u8 fill = loadAcquire(_heap_floor_ring_fill); + u8 head = loadAcquire(_heap_floor_ring_head); + if (fill == 0) { + if (seq_out != nullptr) { + *seq_out = 0; + } + return 0; + } + // head points one past the newest slot (the writer stores then advances) + // - step back one, wrapping over the ring size. + u8 newest = (u8)((head + KLASS_POPULATION_RING_SIZE - 1) % + KLASS_POPULATION_RING_SIZE); + u64 seq_before = loadAcquire(_heap_floor_seq_ring[newest]); + if (seq_before == HEAP_FLOOR_SEQ_UNSTABLE) { + continue; // a writer holds the slot; retry on a later attempt + } + u64 used = load(_heap_floor_ring[newest]); +#ifdef DEBUG + if (_heap_floor_read_hook_for_test != nullptr) { + _heap_floor_read_hook_for_test(_heap_floor_read_hook_arg_for_test); + } +#endif + // The acquire fence orders the relaxed seq re-load below after the + // payload load above; an unchanged re-load rules out an overwrite only + // on x86-TSO (store order) and arm64 (other-multi-copy atomicity plus + // the writer's release fence), not from the C++ abstract model alone. + __atomic_thread_fence(__ATOMIC_ACQUIRE); + u64 seq_after = load(_heap_floor_seq_ring[newest]); + if (seq_before == seq_after) { + if (seq_out != nullptr) { + *seq_out = seq_before; + } + return used; } - return 0; } - // head points one past the newest slot (recordHeapFloorSampleUnchecked() - // stores then advances) - step back one, wrapping over the ring size. - u8 newest = (u8)((head + KLASS_POPULATION_RING_SIZE - 1) % - KLASS_POPULATION_RING_SIZE); - u64 used = load(_heap_floor_ring[newest]); + // No consistent pair within the bound: the sentinel tells the admission + // gate "no new sample this pass" - it must not consume an unevaluated seq. + // Counted once per exhausted read (lock-free, no logging) so a persistently + // frozen admission gate is diagnosable. + Counters::increment(HEAP_FLOOR_READ_UNSTABLE); if (seq_out != nullptr) { - // Read from the SAME slot as `used`, never from _heap_floor_sample_seq: a - // global counter read could tear against the slot (the seq of sample N+1 - // paired with the value of sample N) and make the urgent-admission gate - // treat a re-read as a new sample. The newest slot is not rewritten until - // KLASS_POPULATION_RING_SIZE further samples are recorded, so the pair is - // consistent. - *seq_out = load(_heap_floor_seq_ring[newest]); + *seq_out = HEAP_FLOOR_SEQ_UNSTABLE; } - return used; + return 0; } void LivenessTracker::recordHeapFloorSample(u64 used, u64 timestamp_ns, u64 container_used) { @@ -1525,37 +1550,33 @@ void LivenessTracker::recordHeapFloorSampleUnchecked(u64 used, u64 timestamp_ns, // Lock-free, single-writer-at-a-time - see _heap_floor_ring's own comment // (livenessTracker.h) for why onGC() cannot take _table_lock here. // - // Standard SPSC publish order: the payload is a plain store, and the index - // that gates which slots are valid is what carries the release. A reader - // that loadAcquire()s the index is then guaranteed to see this payload - // write too, since it precedes the index's storeRelease() in program - // order and a release store cannot be reordered before an earlier store. - // (The previous version had this backwards - storeRelease() on the - // payload with a plain store on the index - which does not establish any - // ordering between "the index says this slot is valid" and "the payload - // for that slot is visible".) _heap_floor_time_ring's, - // _container_mem_ring's and _heap_floor_seq_ring's payload writes below are - // plain stores for the same reason - they precede the same storeRelease() - // in program order. - // - // _heap_floor_sample_seq is bumped with an atomic relaxed fetch-add, so two - // overlapping callers can never obtain the same seq; readers see it only via - // the slot ring. The head/slot writes themselves still assume one writer at - // a time: HotSpot never posts GarbageCollectionFinish concurrently, but - // JVMTI does not document that, so on other VMs (J9, Zing via the - // abstraction layer) two overlapping callbacks could overwrite the same - // slot. Overlap can lose a sample but never produces a duplicate seq, so the - // urgent-admission freshness gate cannot mistake a new sample for an - // already-consumed one. + // Per-slot seqlock claim: CAS the target slot's seq word to + // HEAP_FLOOR_SEQ_UNSTABLE. The explicit sentinel check stops a + // sentinel-over-sentinel CAS from admitting a second writer into a slot + // the first writer still claims; a failed CAS means another writer + // overlaps (mid-claim, or already finished with a newer seq) - drop this + // sample, counted as HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP (lock-free relaxed + // counter; no logging - GC-callback context). u8 head = load(_heap_floor_ring_head); + u64 prev = load(_heap_floor_seq_ring[head]); + if (prev == HEAP_FLOOR_SEQ_UNSTABLE || + !__atomic_compare_exchange_n(&_heap_floor_seq_ring[head], &prev, + HEAP_FLOOR_SEQ_UNSTABLE, false, + __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { + Counters::increment(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP); + return; + } + // Release fence: orders the sentinel claim before the payload stores (the + // release seq store alone orders the payloads before the seq), pairing + // with the reader's acquire fence - its seq re-load is relaxed. + __atomic_thread_fence(__ATOMIC_RELEASE); store(_heap_floor_ring[head], used); store(_heap_floor_time_ring[head], timestamp_ns); store(_container_mem_ring[head], container_used); - // Atomic bump: seq uniqueness must not depend on the single-writer - // assumption (see above). Relaxed suffices - readers only see seq via the - // slot store below, published by the head storeRelease(). + // Atomic bump: seq uniqueness holds even under overlapping writers. + // Relaxed suffices - the seq is published only by the release store below. u64 seq = __atomic_add_fetch(&_heap_floor_sample_seq, (u64)1, __ATOMIC_RELAXED); - store(_heap_floor_seq_ring[head], seq); + storeRelease(_heap_floor_seq_ring[head], seq); storeRelease(_heap_floor_ring_head, (u8)((head + 1) % KLASS_POPULATION_RING_SIZE)); u8 fill = load(_heap_floor_ring_fill); if (fill < KLASS_POPULATION_RING_SIZE) { diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index e4e74f49ca..4232d4c2af 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -394,22 +394,18 @@ class alignas(alignof(SpinLock)) LivenessTracker { // libraries) - see secondsToOOM()'s own comment for how this ring and // _heap_floor_ring are used as two independent OOM boundaries. u64 _container_mem_ring[KLASS_POPULATION_RING_SIZE]; - // Per-sample identity, paired index-for-index with _heap_floor_ring (same - // head/fill, pushed by the same recordHeapFloorSampleUnchecked() call before - // the head storeRelease). Values come from _heap_floor_sample_seq below, so a - // slot's seq names exactly one recorded sample for the process lifetime - - // the urgent-admission gate (ReferenceChainTracker::updateUrgentAdmission()) - // uses it to tell "a new GC sample arrived" from "re-reading the same one". + // Per-slot seqlock word and sample identity: HEAP_FLOOR_SEQ_UNSTABLE + // marks a write in progress; any other value names the one recorded sample + // in that slot (from _heap_floor_sample_seq below) for the process + // lifetime. The static singleton's zero-initialization guarantees no slot + // starts at the sentinel - a slot stuck there would stall the ring + // forever. u64 _heap_floor_seq_ring[KLASS_POPULATION_RING_SIZE]; volatile u8 _heap_floor_ring_head; volatile u8 _heap_floor_ring_fill; - // Monotonic heap-floor sample counter (first sample = 1, 0 = "no sample"). - // Bumped only by recordHeapFloorSampleUnchecked() via an atomic relaxed - // fetch-add (__atomic_add_fetch), so values stay unique even if the - // single-writer assumption on _heap_floor_ring does not hold on a given VM, - // and published through the slot ring above; never reset - not by - // heapFloorResetForTest(), not by klassPopulationResetForTest(), not by - // stop()/start() - so a sequence value can never alias an earlier sample. + // Monotonic sample counter (first sample = 1, 0 = "no sample"): the + // atomic fetch-add keeps a seq unique per sample even under overlapping + // writers; never reset by any seam or stop()/start(), so seqs never alias. u64 _heap_floor_sample_seq; // Debug-only test seam: when true, onGC() skips recordHeapFloorSample() @@ -425,6 +421,10 @@ class alignas(alignof(SpinLock)) LivenessTracker { // at the actual write point, not just in onGC(), so a GC already past the // onGC() gate when the test sets this flag still cannot corrupt the ring. std::atomic _heap_floor_recording_disabled_for_test{false}; + + // Plain pointers, not atomic: tests set them only while no reader runs. + static inline void (*_heap_floor_read_hook_for_test)(void *) = nullptr; + static inline void *_heap_floor_read_hook_arg_for_test = nullptr; #endif // Runtime.maxMemory(), resolved once by initialize_table() (the same call @@ -850,7 +850,12 @@ class alignas(alignof(SpinLock)) LivenessTracker { // _heap_floor_recording_disabled_for_test. Called by // recordHeapFloorSample() (after the debug-only flag check) and by // heapFloorRecordForTest() (which bypasses the check so a test can - // seed the rings even while real GC recording is disabled). + // seed the rings even while real GC recording is disabled). Claims the + // target slot via its seqlock word; an overlapping writer's claim drops + // this sample instead of tearing the slot. The seqlock covers each + // slot's payload+seq word only; head/fill index writes still assume one + // writer at a time - HotSpot never posts GarbageCollectionFinish + // concurrently, but JVMTI does not document that. void recordHeapFloorSampleUnchecked(u64 used, u64 timestamp_ns, u64 container_used); // Reads whether the aggregate post-GC live heap has itself shown a @@ -870,16 +875,22 @@ class alignas(alignof(SpinLock)) LivenessTracker { // and secondsToOOM(). See _max_heap_bytes's own comment above. jlong maxHeapBytes() const { return _max_heap_bytes; } - // Reads the used-bytes value of the newest _heap_floor_ring sample (0 when - // the ring is empty) - the urgent-admission gate's freshest physical growth - // evidence. When seq_out is non-null it is ALWAYS written: the newest slot's - // _heap_floor_seq_ring value, read from the same slot as the returned used - // value (0 when the ring is empty), so a caller can tell a new sample from a - // re-read of the one it already consumed. See the definition for the SPSC - // acquire pairing. + // Coherent (used, seq) snapshot of the newest heap-floor sample, validated + // by the slot's seqlock word (see the definition); 0/0 for an empty ring. + // No consistent pair within HEAP_FLOOR_READ_ATTEMPTS: returns 0 and writes + // HEAP_FLOOR_SEQ_UNSTABLE to seq_out (and increments the + // HEAP_FLOOR_READ_UNSTABLE counter once). The reader's initial acquire seq + // load pairs with the writer's release seq store, making that sample's + // payload visible before the payload load. u64 newestHeapFloorSample(u64 *seq_out = nullptr) const; constexpr static int MAX_LEAK_CANDIDATES = 5; + + // Heap-floor seqlock sentinel + reader retry bound (see + // newestHeapFloorSample()). In a slot: write in progress; in seq_out: no + // consistent pair within the bound. _heap_floor_sample_seq never reaches it. + static constexpr u64 HEAP_FLOOR_SEQ_UNSTABLE = ~0ULL; + static constexpr int HEAP_FLOOR_READ_ATTEMPTS = 4; // Delete copy constructor and assignment operator to prevent copies LivenessTracker(const LivenessTracker&) = delete; LivenessTracker& operator=(const LivenessTracker&) = delete; @@ -1702,6 +1713,18 @@ class alignas(alignof(SpinLock)) LivenessTracker { void setHeapFloorRecordingForTest(bool enabled) { _heap_floor_recording_disabled_for_test.store(!enabled, std::memory_order_release); } + + // Reader-interleaving hook: fires inside newestHeapFloorSample() between + // its payload load and its seq validation; nullptr clears it. + static void setHeapFloorReadHookForTest(void (*hook)(void *), void *arg) { + _heap_floor_read_hook_for_test = hook; + _heap_floor_read_hook_arg_for_test = arg; + } + // The private ring size, for tests that must record a full ring wrap + // (KLASS_POPULATION_RING_SIZE itself is private). + static constexpr int heapFloorRingSizeForTest() { + return KLASS_POPULATION_RING_SIZE; + } #endif #if defined(DEBUG) || defined(UNIT_TEST) diff --git a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp index 09d6015d77..f622217b26 100644 --- a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp +++ b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp @@ -1252,6 +1252,43 @@ class LivenessTrackerTestAccessor { *admission_epoch = t->_table[idx].admission_epoch; return true; } + + // The heap-floor ring size, for tests that wrap it a full lap. + static constexpr int heapFloorRingSizeForTest() { + return LivenessTracker::KLASS_POPULATION_RING_SIZE; + } + + // Swaps the head slot's seqlock word for `value` (e.g. the busy + // sentinel) and returns the previous value so the caller can restore it. + static u64 swapHeapFloorHeadSeqForTest(LivenessTracker *t, u64 value) { + u8 head = t->_heap_floor_ring_head; + u64 prev = t->_heap_floor_seq_ring[head]; + t->_heap_floor_seq_ring[head] = value; + return prev; + } + + // Swaps the newest slot's ((head + ring - 1) % ring) seqlock word for + // `value` and returns the previous value - the same slot the reader + // validates in newestHeapFloorSample(). + static u64 swapHeapFloorNewestSeqForTest(LivenessTracker *t, u64 value) { + constexpr int ring = LivenessTracker::KLASS_POPULATION_RING_SIZE; + u8 newest = (u8)((t->_heap_floor_ring_head + ring - 1) % ring); + u64 prev = t->_heap_floor_seq_ring[newest]; + t->_heap_floor_seq_ring[newest] = value; + return prev; + } + + static u8 heapFloorRingHeadForTest(LivenessTracker *t) { + return t->_heap_floor_ring_head; + } + + static u8 heapFloorRingFillForTest(LivenessTracker *t) { + return t->_heap_floor_ring_fill; + } + + static u64 heapFloorSampleSeqForTest(LivenessTracker *t) { + return t->_heap_floor_sample_seq; + } }; TEST(LivenessTrackerSessionTest, ResetSessionClearsOnlyRecordingGates) { @@ -1771,6 +1808,251 @@ TEST_F(SecondsToOOMTest, OnGCRecordsHeapFloorWithLiveheapOnlySource) { /*limit_resolved=*/false); } +#ifdef DEBUG +// --------------------------------------------------------------------------- +// Heap-floor seqlock: newestHeapFloorSample() coherence +// --------------------------------------------------------------------------- +// The read hook fires inside the production reader between its payload +// load and its seq validation, reproducing the writer-wraps-the-ring race +// deterministically (a probabilistic stress loop cannot). +struct HeapFloorWrapHook { + LivenessTracker *lt; + int calls; + bool fire_once; + + static void invoke(void *p) { + HeapFloorWrapHook *h = static_cast(p); + h->calls++; + if (h->fire_once) { + LivenessTracker::setHeapFloorReadHookForTest(nullptr, nullptr); + } + for (int i = 0; i < LivenessTrackerTestAccessor::heapFloorRingSizeForTest(); + i++) { + h->lt->heapFloorRecordForTest(2000 + (u64)i, 0); + } + } +}; + +// Clears the read hook on every exit path, including ASSERT/EXPECT failures. +struct HeapFloorReadHookGuard { + ~HeapFloorReadHookGuard() { + LivenessTracker::setHeapFloorReadHookForTest(nullptr, nullptr); + } +}; + +class HeapFloorSeqlockTest : public ::testing::Test { +protected: + void SetUp() override { + installGtestCrashHandler(); + LivenessTracker::instance()->setHeapFloorRecordingForTest(false); + LivenessTracker::instance()->heapFloorResetForTest(); + } + + void TearDown() override { + LivenessTracker::instance()->heapFloorResetForTest(); + LivenessTracker::instance()->setHeapFloorRecordingForTest(true); + restoreDefaultSignalHandlers(); + } +}; + +// The reader must never pair one sample's used value with another sample's +// seq, even when the writer wraps the whole 30-slot ring while the reader is +// between its two slot loads. +TEST_F(HeapFloorSeqlockTest, NewestSampleCoherentAcrossRingWrap) { + LivenessTracker *lt = LivenessTracker::instance(); + HeapFloorReadHookGuard hook_guard; + lt->heapFloorRecordForTest(1000, 1); + u64 s0 = 0; + ASSERT_EQ(1000u, lt->newestHeapFloorSample(&s0)); + ASSERT_NE(0u, s0); + + HeapFloorWrapHook hook{lt, 0, /*fire_once=*/true}; + LivenessTracker::setHeapFloorReadHookForTest(&HeapFloorWrapHook::invoke, + &hook); + u64 seq = 0; + u64 used = lt->newestHeapFloorSample(&seq); + constexpr int ring = LivenessTrackerTestAccessor::heapFloorRingSizeForTest(); + // The interleaving really happened: the writer advanced a full ring (30 + // samples) past the read slot while the reader was between its two loads. + EXPECT_EQ(1, hook.calls) << "the read hook never fired - no interleaving"; + EXPECT_EQ((u64)ring, seq - s0) << "the writer's ring wrap never reached the read slot"; + // Coherent pair: used must be the seq's own recorded value. + EXPECT_EQ(2000u + (seq - s0 - 1), used) + << "used and seq belong to different recorded samples"; +} + +// When every attempt finds the slot rewritten mid-read, the reader gives up +// after HEAP_FLOOR_READ_ATTEMPTS tries and reports "no consistent sample" +// (0 + HEAP_FLOOR_SEQ_UNSTABLE) instead of a torn pair. Each exhausted read +// - and only an exhausted read - bumps HEAP_FLOOR_READ_UNSTABLE exactly once, +// whether or not the caller asked for the seq. +TEST_F(HeapFloorSeqlockTest, NewestSampleReportsUnstableAfterRetryBound) { + LivenessTracker *lt = LivenessTracker::instance(); + HeapFloorReadHookGuard hook_guard; + + // Empty ring (SetUp reset it): 0/0, not an unstable read. + long long unstable = Counters::getCounter(HEAP_FLOOR_READ_UNSTABLE); + u64 empty_seq = 1; + EXPECT_EQ(0u, lt->newestHeapFloorSample(&empty_seq)); + EXPECT_EQ(0u, empty_seq); + EXPECT_EQ(unstable, Counters::getCounter(HEAP_FLOOR_READ_UNSTABLE)) + << "the empty-ring return must not count as an unstable read"; + + lt->heapFloorRecordForTest(1000, 1); + u64 ok_seq = 0; + EXPECT_EQ(1000u, lt->newestHeapFloorSample(&ok_seq)); + EXPECT_EQ(unstable, Counters::getCounter(HEAP_FLOOR_READ_UNSTABLE)) + << "a successful read must not count as an unstable read"; + + HeapFloorWrapHook hook{lt, 0, /*fire_once=*/false}; + LivenessTracker::setHeapFloorReadHookForTest(&HeapFloorWrapHook::invoke, + &hook); + u64 seq = 0; + EXPECT_EQ(0u, lt->newestHeapFloorSample(&seq)); + EXPECT_EQ(LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE, seq); + EXPECT_EQ(LivenessTracker::HEAP_FLOOR_READ_ATTEMPTS, hook.calls); + EXPECT_EQ(unstable + 1, Counters::getCounter(HEAP_FLOOR_READ_UNSTABLE)) + << "an exhausted read must be counted once, not per attempt"; + + // Same exhaustion without a seq_out (the emergency-band baseline seed's + // call shape) must count too. + unstable = Counters::getCounter(HEAP_FLOOR_READ_UNSTABLE); + EXPECT_EQ(0u, lt->newestHeapFloorSample(nullptr)); + EXPECT_EQ(unstable + 1, Counters::getCounter(HEAP_FLOOR_READ_UNSTABLE)) + << "an exhausted read with seq_out == nullptr must be counted"; +} + +// A slot pinned at the sentinel models an overlapping writer mid-claim: +// the writer drops its sample (head, fill and the sample counter unchanged) +// and the next accepted sample takes the very next seq - no gap, no duplicate. +TEST_F(HeapFloorSeqlockTest, OverlappingWriterDropsSampleWithoutDuplicateSeq) { + LivenessTracker *lt = LivenessTracker::instance(); + HeapFloorReadHookGuard hook_guard; + lt->heapFloorRecordForTest(1000, 1); + u64 prev_seq = 0; + ASSERT_EQ(1000u, lt->newestHeapFloorSample(&prev_seq)); + + u8 head = LivenessTrackerTestAccessor::heapFloorRingHeadForTest(lt); + u8 fill = LivenessTrackerTestAccessor::heapFloorRingFillForTest(lt); + u64 sample_seq = LivenessTrackerTestAccessor::heapFloorSampleSeqForTest(lt); + u64 saved = LivenessTrackerTestAccessor::swapHeapFloorHeadSeqForTest( + lt, LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE); + + long long dropped = Counters::getCounter(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP); + lt->heapFloorRecordForTest(1100, 2); + EXPECT_EQ(dropped + 1, Counters::getCounter(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP)) + << "a dropped sample must be counted"; + EXPECT_EQ(head, LivenessTrackerTestAccessor::heapFloorRingHeadForTest(lt)) + << "a dropped sample must not advance the ring"; + EXPECT_EQ(fill, LivenessTrackerTestAccessor::heapFloorRingFillForTest(lt)) + << "a dropped sample must not change the fill"; + EXPECT_EQ(sample_seq, LivenessTrackerTestAccessor::heapFloorSampleSeqForTest(lt)) + << "a dropped sample must not consume a sequence number"; + + LivenessTrackerTestAccessor::swapHeapFloorHeadSeqForTest(lt, saved); + lt->heapFloorRecordForTest(1100, 2); + EXPECT_EQ(dropped + 1, Counters::getCounter(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP)) + << "an accepted sample must not be counted as dropped"; + u64 seq = 0; + EXPECT_EQ(1100u, lt->newestHeapFloorSample(&seq)); + EXPECT_EQ(prev_seq + 1, seq) + << "the dropped writer must leave neither a gap nor a duplicate"; +} + +// Counting-only hook: observes whether the reader reached its payload/hook +// path without perturbing the ring. +struct HeapFloorCountHook { + int calls; + static void invoke(void *p) { static_cast(p)->calls++; } +}; + +// Restores the newest slot's seq word pinned by the enclosing scope, on +// every exit path including ASSERT failures. +struct HeapFloorNewestPinGuard { + LivenessTracker *lt; + u64 saved; + explicit HeapFloorNewestPinGuard(LivenessTracker *t) + : lt(t), + saved(LivenessTrackerTestAccessor::swapHeapFloorNewestSeqForTest( + t, LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE)) {} + ~HeapFloorNewestPinGuard() { + LivenessTrackerTestAccessor::swapHeapFloorNewestSeqForTest(lt, saved); + } +}; + +// Fire-once hook that pins the newest slot's seq word to the sentinel +// between the reader's payload load and its validating re-load, forcing the +// next attempt into the reader's sentinel short-circuit. saved/pinned let +// the test restore the slot after the read returns. +struct HeapFloorPinNewestHook { + LivenessTracker *lt; + int calls; + u64 saved; + bool pinned; + + static void invoke(void *p) { + HeapFloorPinNewestHook *h = static_cast(p); + h->calls++; + LivenessTracker::setHeapFloorReadHookForTest(nullptr, nullptr); + h->saved = LivenessTrackerTestAccessor::swapHeapFloorNewestSeqForTest( + h->lt, LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE); + h->pinned = true; + } +}; + +// A newest slot already at the sentinel (a writer mid-claim) must never be +// consumed as a sample seq: the reader retries past it and, with no writer +// to finish the claim, reports the documented unstable pair. hook.calls == +// 0 pins the sentinel short-circuit to happen before the payload read and +// the validation hook. +TEST_F(HeapFloorSeqlockTest, NewestSlotPinnedAtSentinelShortCircuitsPayloadAndHook) { + LivenessTracker *lt = LivenessTracker::instance(); + HeapFloorReadHookGuard hook_guard; + lt->heapFloorRecordForTest(1000, 1); + HeapFloorCountHook hook{0}; + LivenessTracker::setHeapFloorReadHookForTest(&HeapFloorCountHook::invoke, + &hook); + u64 seq = 0; + u64 used = 0; + { + HeapFloorNewestPinGuard pin_guard(lt); + used = lt->newestHeapFloorSample(&seq); + } + EXPECT_EQ(0u, used); + EXPECT_EQ(LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE, seq) + << "the pinned slot must be retried past, not consumed as a seq"; + EXPECT_EQ(0, hook.calls) + << "the sentinel short-circuit must skip the payload read and the hook"; +} + +// A sentinel pinned only mid-retry (between the reader's payload load and +// its validating re-load) forces the retry; the next attempt then sees the +// sentinel at seq_before and takes the short-circuit. hook.calls == 1 is +// deterministic: the hook uninstalls itself after its first call, and +// attempt 1 always reaches it because the slot is valid until that call +// pins it - no writer runs to unpublish or rewrite the slot in between. +TEST_F(HeapFloorSeqlockTest, SentinelPinnedMidRetryTakesShortCircuitNextAttempt) { + LivenessTracker *lt = LivenessTracker::instance(); + HeapFloorReadHookGuard hook_guard; + lt->heapFloorRecordForTest(1000, 1); + HeapFloorPinNewestHook hook{lt, 0, 0, false}; + LivenessTracker::setHeapFloorReadHookForTest(&HeapFloorPinNewestHook::invoke, + &hook); + u64 seq = 0; + u64 used = lt->newestHeapFloorSample(&seq); + // Restore before any ASSERT: an early exit must never leak the sentinel. + if (hook.pinned) { + LivenessTrackerTestAccessor::swapHeapFloorNewestSeqForTest(lt, + hook.saved); + } + ASSERT_EQ(1, hook.calls) << "attempt 1 never reached the mid-read hook"; + // Never a torn pair: the pinned slot yields the documented unstable + // pair, not (payload, sentinel) or (payload, stale seq). + EXPECT_EQ(0u, used); + EXPECT_EQ(LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE, seq); +} +#endif + // --------------------------------------------------------------------------- // Leak tag pool (design A: direct tagging of leaking objects) // --------------------------------------------------------------------------- From 3eec21f75d504e5bb2b94e166e3e1e497343a539 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 12:22:30 +0100 Subject: [PATCH 25/37] referenceChains: restore fallback cadence on urgency exit; guard emergency admission applyOomRamp() (extracted from threadLoop()) now returns _effective_cadence_ns to PASS_CADENCE_NS when an urgency episode ends instead of leaving the ramped 10ms floor latched forever; the emergency band requires a non-negative projection like isUrgent(); budget boost/restore and pause-target transitions are pinned by tests. --- ddprof-lib/src/main/cpp/referenceChains.cpp | 133 +++--- ddprof-lib/src/main/cpp/referenceChains.h | 23 +- .../src/test/cpp/referenceChainsOomTests.inc | 422 ++++++++++++++++++ .../test/cpp/referenceChainsTestAccessors.h | 78 ++++ 4 files changed, 599 insertions(+), 57 deletions(-) diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index e29b98def6..724a9705bb 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -396,6 +396,9 @@ void ReferenceChainTracker::stop() { // an emergency-band first observation in the new recording seeds the baseline without // touching the pair, and a leaked pair would then make the next ramp-band call with no new // GC sample return the old recording's retained admission; + // - an urgency episode still open (applyOomRamp()) would carry its boosted _budget ceiling + // (_urgency_budget_boosted) and its ramped _effective_cadence_ns past the boundary - closed + // here with the same restore as applyOomRamp()'s own episode-end branch; // - the frontier's referrer_klass slots hold StringDictionary ids from a _class_map that // Profiler::start() is about to wipe (ids restart at 1) - they cannot be remapped, so a // chain built from a surviving entry after the boundary would name whichever class the new @@ -423,6 +426,19 @@ void ReferenceChainTracker::stop() { _admission_growth_baseline = 0; _admission_sample_seq = 0; _admission_decision = false; + // Close the urgency episode the BFS thread may have left open: the boosted + // budget ceiling and the ramped cadence are episode state - the same state + // applyOomRamp()'s first non-urgent iteration restores on the BFS thread - + // and a mid-episode stop() would otherwise leak them past the recording + // boundary (buildAbandonedEvent() reads _budget after this point). + // Guarded on the boost flag because outside an episode updatePacing() + // owns _effective_cadence_ns; stopThread() has already joined the BFS + // thread, so these writes are as safe as the urgency resets above. + if (_urgency_budget_boosted) { + _urgency_budget_boosted = false; + _budget = _configured_budget; + _effective_cadence_ns = PASS_CADENCE_NS; + } // Same per-search candidate/discovered canary state runPass()'s terminal branch and // restartSearch() clear - zeroing the counts here closes the stale-chain windows in // pollWatchedTargets() (dead-representative canary path, discovered-instances path) for the @@ -498,6 +514,52 @@ void ReferenceChainTracker::stopThread() { } } +// threadLoop()'s per-iteration OOM ramp: pause target, cadence and the urgency budget +// boost/restore, keyed on the raw band comparison (never the latch). Returns the raw `urgent`. +bool ReferenceChainTracker::applyOomRamp(double seconds_to_oom) { + bool urgent = seconds_to_oom >= 0 && seconds_to_oom < OOM_RAMP_START_S; + long target_ms = _pause_target_ms; + u64 cadence_ns = _effective_cadence_ns; + if (urgent) { + double x = 1.0 - seconds_to_oom / OOM_RAMP_START_S; // 0 at 30min out, 1 at OOM + target_ms = std::lround(_pause_target_ms * + std::pow((double)URGENT_PAUSE_TARGET_MS / std::max(_pause_target_ms, 1L), x)); + // Ramp from the fixed configured cadence, not the currently-adaptive _effective_cadence_ns - + // using the live value as the ramp's own moving anchor would compound the exponent across + // iterations instead of tracking urgency directly from a stable baseline. + cadence_ns = (u64)std::llround((double)PASS_CADENCE_NS * + std::pow((double)URGENT_CADENCE_NS / (double)PASS_CADENCE_NS, x)); + // Cadence ownership on every branch: urgent iterations set it here, the episode's end (or + // stop()) restores PASS_CADENCE_NS, and otherwise updatePacing() owns it. + _effective_cadence_ns = cadence_ns; + } + if (target_ms != _effective_pause_target_ms) { + _effective_pause_target_ms = target_ms; + _pause_pid = PidController((u64)std::max(_effective_pause_target_ms, 0L), + 10, 1, 2, 1, 5.0); + } + // Once in the ramp window, hold the budget ceiling raised for the urgency episode's entire + // duration rather than only right before OOM: the process is likely to die anyway, so it's + // worth spending whatever budget it takes to collect good diagnostic data for as long as we + // have. + if (urgent && !_urgency_budget_boosted) { + _urgency_budget_boosted = true; + _budget = std::min(_budget * 4, MAX_REFERENCE_CHAINS_BUDGET); + TEST_LOG_SUMMARY("ReferenceChainTracker::threadLoop urgency budget boost " + "budget=%d configured=%d", + _budget, _configured_budget); + } else if (!urgent && _urgency_budget_boosted) { + _urgency_budget_boosted = false; + _budget = _configured_budget; + // The episode owned the cadence while it ran; hand it back at the fallback in the same + // iteration so the idle loop stops waking at the ramped rate. + _effective_cadence_ns = PASS_CADENCE_NS; + TEST_LOG_SUMMARY("ReferenceChainTracker::threadLoop urgency budget restore " + "budget=%d", _budget); + } + return urgent; +} + // Runs scheduled passes on an attached agent thread. void ReferenceChainTracker::threadLoop() { struct Cleanup { @@ -532,43 +594,10 @@ void ReferenceChainTracker::threadLoop() { // The iteration's only projection read - consumed by the ramp below, publishUrgencyState() and // shouldRunPass() (restart gate / isUrgent() latch), so every decision sees one sample. double seconds_to_oom = LivenessTracker::instance()->secondsToOOM(); - bool urgent = seconds_to_oom >= 0 && seconds_to_oom < OOM_RAMP_START_S; - long target_ms = _pause_target_ms; + bool urgent = applyOomRamp(seconds_to_oom); + // applyOomRamp() keeps _effective_cadence_ns current, so the sleep below and + // shouldRunPass()'s cadence gate always agree on the same value. u64 cadence_ns = _effective_cadence_ns; - if (urgent) { - double x = 1.0 - seconds_to_oom / OOM_RAMP_START_S; // 0 at 30min out, 1 at OOM - target_ms = std::lround(_pause_target_ms * - std::pow((double)URGENT_PAUSE_TARGET_MS / std::max(_pause_target_ms, 1L), x)); - // Ramp from the fixed configured cadence, not the currently-adaptive _effective_cadence_ns - - // using the live value as the ramp's own moving anchor would compound the exponent across - // iterations instead of tracking urgency directly from a stable baseline. - cadence_ns = (u64)std::llround((double)PASS_CADENCE_NS * - std::pow((double)URGENT_CADENCE_NS / (double)PASS_CADENCE_NS, x)); - // While urgent, the ramp owns _effective_cadence_ns outright so shouldRunPass()'s cadence - // gate and the per-pass log actually reflect it. - _effective_cadence_ns = cadence_ns; - } - if (target_ms != _effective_pause_target_ms) { - _effective_pause_target_ms = target_ms; - _pause_pid = PidController((u64)std::max(_effective_pause_target_ms, 0L), - 10, 1, 2, 1, 5.0); - } - // Once in the ramp window, hold the budget ceiling raised for the urgency episode's entire - // duration rather than only right before OOM: the process is likely to die anyway, so it's - // worth spending whatever budget it takes to collect good diagnostic data for as long as we - // have. - if (urgent && !_urgency_budget_boosted) { - _urgency_budget_boosted = true; - _budget = std::min(_budget * 4, MAX_REFERENCE_CHAINS_BUDGET); - TEST_LOG_SUMMARY("ReferenceChainTracker::threadLoop urgency budget boost " - "budget=%d configured=%d", - _budget, _configured_budget); - } else if (!urgent && _urgency_budget_boosted) { - _urgency_budget_boosted = false; - _budget = _configured_budget; - TEST_LOG_SUMMARY("ReferenceChainTracker::threadLoop urgency budget restore " - "budget=%d", _budget); - } // Third trigger for LivenessTracker::cleanup_table() (see // LivenessTracker::maybeForceCleanup()'s own comment): track()'s table-overflow branch and // flush_table()'s JFR cadence can both starve under ObjectSampler's PID-controlled sampling @@ -586,18 +615,9 @@ void ReferenceChainTracker::threadLoop() { // callbacks, which only read the cached atomic. DEBUG_ONLY(rcDebugLevelRefresh()); - // Hand this iteration's ramp state to shouldRunPass() before it decides - the canary-backoff - // gate is bypassed while the OOM urgency ramp is active (see _oom_ramp_active's own comment). - // The 100% tracking admission (setUrgentTracking(), livenessTracker.h) follows the same ramp - // state, but outside the emergency band it is additionally gated on fresh heap growth evidence - // (updateUrgentAdmission()/the _admission_growth_baseline constraint), evaluated once per new - // heap-floor sample (GC) and held until the next sample - so an admitted growth sample keeps - // the boost on until the next GC rather than for a single iteration: the smoothed projection - // can stay inside the band for many GCs after the heap has physically stopped growing, and - // boosting admission on a recovered spike only buys table churn. The budget/cadence ramp above - // stays keyed on the raw band comparison - without a running search (the candidate gate) those - // effects cost nothing, while mis-admission during a transient does. publishUrgencyState() - // performs both hand-offs. + // Hand the ramp state to shouldRunPass() before it decides: _oom_ramp_active bypasses the + // canary-backoff gate, the growth-gated admission (held between heap-floor samples) follows + // the same hand-off, and the cadence ramp shortens the idle sleep with no search running. publishUrgencyState(seconds_to_oom, urgent); // shouldRunPass()'s restart branch (restartSearch()) clears _pending_expand/_priority_expand @@ -848,11 +868,17 @@ bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool ur // last chapter can before the process dies, without waiting for growth // evidence. OOM_URGENT_THRESHOLD_S is isUrgent()'s own latch threshold, so // the emergency band and the hysteretic urgent episode cover the same range. - if (seconds_to_oom < OOM_URGENT_THRESHOLD_S) { + // A negative projection is secondsToOOM()'s "no projection" encoding + // (burn-in, not rising, underfilled ring) - the same lower bound isUrgent() + // applies - so it is never an emergency: it falls through to the + // growth-gated path below. + if (seconds_to_oom >= 0 && seconds_to_oom < OOM_URGENT_THRESHOLD_S) { if (_admission_growth_baseline == 0) { // Seed at emergency-band entry: the burn-in can hide the projection // until it is already short, and without a seed the first ramp-band // sample would re-snapshot and drop the boost mid-growth. + // An unstable read returns 0 and skips the seed - the baseline stays 0, + // so the next emergency-band iteration retries. u64 newest_used = LivenessTracker::instance()->newestHeapFloorSample(); if (newest_used != 0) { _admission_growth_baseline = newest_used; @@ -865,6 +891,11 @@ bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool ur } u64 newest_seq = 0; u64 newest_used = LivenessTracker::instance()->newestHeapFloorSample(&newest_seq); + if (newest_seq == LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE) { + // The reader exhausted its retry bound against a wrapping GC writer: hold + // the decision without consuming a seq, so the next iteration re-reads. + return _admission_decision; + } if (newest_used == 0) { // No sample to reason about (the ring cannot be empty while a projection // exists - secondsToOOM() requires MIN_FILL samples - so this is purely @@ -932,8 +963,8 @@ bool ReferenceChainTracker::hasLeakSignal(double seconds_to_oom) { // candidate-less search would run to the full auto-tuned TTL doing whole-heap // BFS - pure cost. A warmup heap-floor rise can trip the 5-minute projection // with no tagged klass, so the refusal is unconditional, not a heuristic. - // Urgency still applies through the ramp (cadence, pause target, budget - // boost, 100% liveness admission) once a real candidate justifies the search. + // Urgency drives the ramp and the admission boost on every iteration, but + // never authorizes a search. TEST_LOG_SUMMARY("ReferenceChainTracker::hasLeakSignal -> %s (secondsToOOM=%.1f, " "candidates=%d, urgent=%d)", n > 0 ? "true" : "false", seconds_to_oom, n, urgent); diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index a5c7a5b24f..2ed62e4f55 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -333,8 +333,12 @@ class ReferenceChainTracker { // failure must NOT be treated as "released". bool _tags_released; - // Whether threadLoop()'s urgency ramp currently holds the multiplied _budget (see the urgency - // block there and _configured_budget's own comment). + // Whether the urgency episode (applyOomRamp()) currently holds the multiplied _budget. The + // episode has two closers, both restoring _budget = _configured_budget and + // _effective_cadence_ns = PASS_CADENCE_NS together with this flag: applyOomRamp()'s first + // non-urgent iteration (BFS thread), and stop() after stopThread() has joined the BFS thread + // (recording boundary). start() reseeds all three. BFS-thread-only otherwise; no atomics + // (see _configured_budget's own comment). bool _urgency_budget_boosted; // Hysteresis state behind isUrgent(). @@ -372,8 +376,10 @@ class ReferenceChainTracker { // same no-atomics argument (BFS thread; reset only with no BFS thread live). // _admission_sample_seq is the LivenessTracker heap-floor sequence number // (newestHeapFloorSample(&seq)) of the sample the ramp-band gate last - // evaluated (0 = none this episode); _admission_decision is that - // evaluation's result. threadLoop() runs far more often than GCs record + // evaluated (0 = none this episode; an unstable read reports + // HEAP_FLOOR_SEQ_UNSTABLE, which is held on, never stored here); + // _admission_decision is that evaluation's result. threadLoop() runs far + // more often than GCs record // samples, so updateUrgentAdmission() re-evaluates only when a NEW sample // arrives and otherwise returns the retained decision - re-comparing the // same sample against the baseline it just raised would turn every admitted @@ -662,8 +668,8 @@ class ReferenceChainTracker { // Heap-wide time-to-OOM urgency threshold (LivenessTracker::secondsToOOM()). It has two uses: // (a) isUrgent()'s latch threshold (released via OOM_URGENT_RELEASE_S below); the latch is // consumed by runPass()'s no-progress abandonment branch, which it suppresses while latched; - // (b) updateUrgentAdmission()'s emergency band, where tracking admission goes to 100% without - // heap-growth evidence. + // (b) updateUrgentAdmission()'s emergency band - a real (non-negative) projection below it - + // where tracking admission goes to 100% without heap-growth evidence. // It never authorizes starting a search - urgency never stands in for // selectLeakCandidates()'s per-klass gate (see hasLeakSignal()). static constexpr double OOM_URGENT_THRESHOLD_S = 300.0; // 5 minutes @@ -841,6 +847,11 @@ class ReferenceChainTracker { // `urgent`. Extracted from threadLoop() so the wiring is testable without a BFS thread. void publishUrgencyState(double seconds_to_oom, bool urgent); + // threadLoop()'s per-iteration OOM ramp: pause target, cadence and the urgency budget + // boost/restore, keyed on the raw band comparison (never the latch). Returns that raw `urgent` + // so the caller needs no second evaluation. + bool applyOomRamp(double seconds_to_oom); + // Search restart gate: true once _safepoint_pain_budget has // drained back to zero (canStartNow()) *and* hasLeakSignal() above reports at least one leak // candidate. The two-arg form consumes the caller's projection; the one-arg form reads diff --git a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc index cfc59bb68e..01bad8801f 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc @@ -492,6 +492,38 @@ TEST_F(OomUrgencyTest, EmergencyBandAdmitsWithoutGrowthEvidence) { EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(299.0, true)); } +// A latched urgent episode reading "no projection" (-1, secondsToOOM()'s +// burn-in/not-rising/underfilled encoding) is not an emergency: the +// emergency band carries the same >= 0 lower bound as isUrgent(double), so +// updateUrgentAdmission() takes the growth-gated path instead of the +// evidence-free 100% admission boost. +TEST_F(OomUrgencyTest, NoProjectionInLatchedEpisodeDoesNotArmEmergencyAdmission) { + ASSERT_NEAR(project(299.0), 299.0, 1.0); + ASSERT_TRUE(ReferenceChainsTestAccessor::isUrgentWithProjectionForTest(299.0)); + ASSERT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()); + // Real emergency-band entry: admitted, and the growth baseline is seeded + // at the newest sample; the freshness pair stays untouched. + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(299.0, true)); + ASSERT_EQ(0u, ReferenceChainsTestAccessor::admissionSampleSeqForTest()); + + // One no-projection reading: the latch holds (release needs consecutive + // clear observations), so the episode is still urgent. + ASSERT_TRUE(ReferenceChainsTestAccessor::isUrgentWithProjectionForTest(-1.0)); + u64 newest_seq = 0; + ASSERT_NE(0u, LivenessTracker::instance()->newestHeapFloorSample(&newest_seq)); + ASSERT_NE(LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE, newest_seq); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(-1.0, true)) + << "a negative projection is 'no projection', not the emergency band"; + // The growth-gated path evaluated the sample (the emergency branch never + // touches the freshness pair): no growth past the seeded baseline. + EXPECT_EQ(newest_seq, ReferenceChainsTestAccessor::admissionSampleSeqForTest()); + EXPECT_FALSE(ReferenceChainsTestAccessor::admissionDecisionForTest()); + + // No new sample: the non-admitting decision is held, not re-armed. + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(-1.0, true)); + EXPECT_EQ(newest_seq, ReferenceChainsTestAccessor::admissionSampleSeqForTest()); +} + // The emergency band seeds the growth baseline at entry: the burn-in can hide // the projection until it is already short, and without a seed the first // ramp-band observation would re-snapshot and drop the boost mid-growth. @@ -864,6 +896,329 @@ TEST_F(OomUrgencyTest, HeapFloorSequenceIsMonotonicAcrossResets) { lt->heapFloorResetForTest(); } +#ifdef DEBUG +// Context for the heap-floor read hook (setHeapFloorReadHookForTest()): each +// invocation records one full ring wrap of samples between the reader's +// payload load and its validating seq re-load; fire_once limits that to the +// first invocation. +struct HeapFloorReadHookCtx { + int calls = 0; // hook invocations (one per reader attempt) + int records = 0; // invocations that actually recorded a ring wrap + bool fire_once = false; + u64 clock = 0; +}; + +static void heapFloorReadHookThunk(void *arg) { + HeapFloorReadHookCtx *ctx = static_cast(arg); + int n = ctx->calls++; + if (ctx->fire_once && n > 0) { + return; + } + ctx->records++; + LivenessTracker *lt = LivenessTracker::instance(); + // A full wrap returns the head to the slot the reader is holding - the + // torn-read interleaving the gate must survive. + constexpr int RING_SIZE = LivenessTracker::heapFloorRingSizeForTest(); + constexpr u64 WRAP_BASE = 1100ULL << 20; + for (int i = 0; i < RING_SIZE; i++) { + lt->heapFloorRecordForTest(WRAP_BASE + ((u64)i << 20), ctx->clock, 0); + ctx->clock += 1; + } +} +#endif + +// When the read hook interleaves a full ring wrap inside newestHeapFloorSample() +// (the torn-read window), the admission gate must evaluate - and consume the +// seq of - only a coherent pair. A torn read would pair sample 1's payload +// with a newer sample's seq; consuming that seq would hide the true newest +// sample from every later call with an unchanged ring. +TEST_F(OomUrgencyTest, AdmissionNeverConsumesSeqOfUnevaluatedSample) { +#ifdef DEBUG + LivenessTracker *lt = LivenessTracker::instance(); + lt->heapFloorResetForTest(); + // The hook must never outlive this test, on any exit path. + struct ClearHook { + ~ClearHook() { LivenessTracker::setHeapFloorReadHookForTest(nullptr, nullptr); } + } clear_hook; + + lt->heapFloorRecordForTest(1000 * MiB, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + u64 baseline_seq = 0; + ASSERT_EQ(1000 * MiB, lt->newestHeapFloorSample(&baseline_seq)); + + // First ramp-band observation: sets the entry baseline, consumes its seq. + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + ASSERT_EQ(baseline_seq, ReferenceChainsTestAccessor::admissionSampleSeqForTest()); + + HeapFloorReadHookCtx ctx; + ctx.fire_once = true; + LivenessTracker::setHeapFloorReadHookForTest(heapFloorReadHookThunk, &ctx); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "the gate must evaluate the coherent newest sample (1129 MiB), not the torn payload"; + ASSERT_EQ(1, ctx.records) << "the interleaving must have happened exactly once"; + + // The consumed seq must be the true newest sample's seq - read here with + // the hook cleared - never one whose value the gate did not compare. + LivenessTracker::setHeapFloorReadHookForTest(nullptr, nullptr); + u64 newest_seq = 0; + ASSERT_EQ(1129 * MiB, lt->newestHeapFloorSample(&newest_seq)); + ASSERT_NE(baseline_seq, newest_seq) << "the hook must have wrapped the ring"; + EXPECT_EQ(newest_seq, ReferenceChainsTestAccessor::admissionSampleSeqForTest()); + + // Unchanged ring afterwards: the evaluated decision is held. + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + + lt->heapFloorResetForTest(); +#else + GTEST_SKIP() << "the heap-floor read hook is DEBUG-only"; +#endif +} + +// A read that exhausts the reader's retry bound returns (0, +// HEAP_FLOOR_SEQ_UNSTABLE) - "no sample evaluated": the previous decision +// holds and the sentinel never lands in _admission_sample_seq, for both +// decision values (a dropped true would pulse the boost off for one +// iteration; a re-admitted false would revive a dead boost). The retained +// _admission_decision is checked directly through admissionDecisionForTest() +// after every call, not only via the returned value. +TEST_F(OomUrgencyTest, AdmissionHeldOnUnstableHeapFloorRead) { +#ifdef DEBUG + LivenessTracker *lt = LivenessTracker::instance(); + lt->heapFloorResetForTest(); + struct ClearHook { + ~ClearHook() { LivenessTracker::setHeapFloorReadHookForTest(nullptr, nullptr); } + } clear_hook; + + // Establish a held true decision: entry snapshot, then growth past it. + lt->heapFloorRecordForTest(1000 * MiB, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + lt->heapFloorRecordForTest(1100 * MiB, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + ASSERT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + u64 true_seq = ReferenceChainsTestAccessor::admissionSampleSeqForTest(); + ASSERT_NE(0u, true_seq); + ASSERT_TRUE(ReferenceChainsTestAccessor::admissionDecisionForTest()) + << "the growth evaluation must retain a true decision"; + + // Every reader attempt fires the hook and wraps the ring, so the retry + // bound exhausts and the read reports no consistent sample. + HeapFloorReadHookCtx ctx; + LivenessTracker::setHeapFloorReadHookForTest(heapFloorReadHookThunk, &ctx); + EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "an unstable read must hold the true decision, not drop the boost"; + ASSERT_EQ(LivenessTracker::HEAP_FLOOR_READ_ATTEMPTS, ctx.records) + << "the reader retry bound must have been exhausted"; + u64 sentinel_seq = 0; + EXPECT_EQ(0u, lt->newestHeapFloorSample(&sentinel_seq)); + EXPECT_EQ(LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE, sentinel_seq); + ASSERT_EQ(2 * LivenessTracker::HEAP_FLOOR_READ_ATTEMPTS, ctx.records) + << "the direct reader check must also have exhausted the bound"; + EXPECT_EQ(true_seq, ReferenceChainsTestAccessor::admissionSampleSeqForTest()) + << "the sentinel seq must never be consumed"; + EXPECT_TRUE(ReferenceChainsTestAccessor::admissionDecisionForTest()) + << "an unstable read must leave the retained true decision untouched"; + + // Same hold for a false decision: evaluate one flat sample with the hook + // cleared, then make the ring unreadable again. + LivenessTracker::setHeapFloorReadHookForTest(nullptr, nullptr); + lt->heapFloorRecordForTest(1050 * MiB, _clock_ns, 0); + _clock_ns += SAMPLE_SPACING_S * SEC_NS; + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); + u64 false_seq = ReferenceChainsTestAccessor::admissionSampleSeqForTest(); + ASSERT_NE(false_seq, true_seq); + ASSERT_FALSE(ReferenceChainsTestAccessor::admissionDecisionForTest()) + << "the flat-sample evaluation must retain a false decision"; + + LivenessTracker::setHeapFloorReadHookForTest(heapFloorReadHookThunk, &ctx); + EXPECT_FALSE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)) + << "an unstable read must hold the false decision, not re-admit"; + ASSERT_EQ(3 * LivenessTracker::HEAP_FLOOR_READ_ATTEMPTS, ctx.records) + << "the second unstable read must also have exhausted the bound"; + EXPECT_EQ(false_seq, ReferenceChainsTestAccessor::admissionSampleSeqForTest()); + EXPECT_FALSE(ReferenceChainsTestAccessor::admissionDecisionForTest()) + << "an unstable read must leave the retained false decision untouched"; + + LivenessTracker::setHeapFloorReadHookForTest(nullptr, nullptr); + lt->heapFloorResetForTest(); +#else + GTEST_SKIP() << "the heap-floor read hook is DEBUG-only"; +#endif +} + +// --------------------------------------------------------------------------- +// The urgency episode's cadence half (applyOomRamp()): an episode owns +// _effective_cadence_ns while it runs; its end must hand the field back at +// the fallback cadence in the same iteration, or the idle loop keeps waking +// at the ramped rate for the rest of the recording. +// --------------------------------------------------------------------------- + +// Drives one urgency episode end-to-end: urgent iterations pin the ramped +// cadence (10ms at OOM, ~21.5ms at 300s out), and the first non-urgent +// iteration restores the 1s fallback - not just the first one after boost, +// but on every later iteration too. +TEST_F(OomUrgencyTest, UrgencyExitRestoresFallbackCadence) { + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()) + << "applyOomRampForTest() would race threadLoop()"; + const u64 pass_ns = ReferenceChainsTestAccessor::baselineCadenceNs(); + const u64 urgent_ns = ReferenceChainsTestAccessor::urgentCadenceNsForTest(); + struct Restore { + ReferenceChainsTestAccessor::OomRampState saved; + ~Restore() { + // Close any still-open episode first so the budget restore runs, + // then put the saved scheduling state back. + ReferenceChainsTestAccessor::applyOomRampForTest(-1.0); + ReferenceChainsTestAccessor::setEffectiveCadenceNs( + ReferenceChainsTestAccessor::baselineCadenceNs()); + ReferenceChainsTestAccessor::restoreOomRampStateForTest(saved); + } + } restore{ReferenceChainsTestAccessor::saveOomRampStateForTest()}; + + // Seed a known budget ceiling and pause target: the singleton may never + // have been start()ed (zeros would make the boost and the ramp vacuous). + // The 7-field Restore above puts the singleton's own values back. + const int configured_budget = 1000; + const int boosted_budget = std::min(configured_budget * 4, MAX_REFERENCE_CHAINS_BUDGET); + const long configured_pause_ms = 20; + ReferenceChainsTestAccessor::setBudgetsForTest(configured_budget, configured_budget); + ReferenceChainsTestAccessor::setPauseTargetsForTest(configured_pause_ms); + + // No episode open: the non-urgent iteration leaves the fallback cadence, + // the budget ceiling and the pause target alone. + ReferenceChainsTestAccessor::setEffectiveCadenceNs(pass_ns); + EXPECT_FALSE(ReferenceChainsTestAccessor::applyOomRampForTest(-1.0)); + EXPECT_EQ(pass_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()); + EXPECT_FALSE(ReferenceChainsTestAccessor::urgencyBudgetBoostedForTest()); + EXPECT_EQ(configured_budget, ReferenceChainsTestAccessor::budgetForTest()); + EXPECT_EQ(configured_pause_ms, ReferenceChainsTestAccessor::effectivePauseTargetMsForTest()); + + // At OOM the ramp reaches its urgent ceiling (scenario reached). + EXPECT_TRUE(ReferenceChainsTestAccessor::applyOomRampForTest(0.0)); + EXPECT_EQ(urgent_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "a 0s projection must pin the urgent cadence floor"; + EXPECT_TRUE(ReferenceChainsTestAccessor::urgencyBudgetBoostedForTest()) + << "the first urgent iteration must open the episode"; + EXPECT_EQ(boosted_budget, ReferenceChainsTestAccessor::budgetForTest()) + << "episode entry boosts the budget ceiling 4x (clamped to MAX)"; + EXPECT_EQ(ReferenceChainsTestAccessor::urgentPauseTargetMsForTest(), + ReferenceChainsTestAccessor::effectivePauseTargetMsForTest()) + << "a 0s projection must ramp the pause target to its urgent ceiling"; + + // Mid-ramp: 300s out of the 1800s horizon lands at 21.54ms (+/- 1ms) - a + // fixed constant, not the formula, so a drifted exponent fails here. + EXPECT_TRUE(ReferenceChainsTestAccessor::applyOomRampForTest(300.0)); + EXPECT_NEAR((double)ReferenceChainsTestAccessor::effectiveCadenceNs(), + 21.54e6, 1.0e6) + << "300s out of the 1800s ramp horizon"; + EXPECT_EQ(boosted_budget, ReferenceChainsTestAccessor::budgetForTest()) + << "the boost applies once per episode, never compounded"; + EXPECT_EQ(76L, ReferenceChainsTestAccessor::effectivePauseTargetMsForTest()) + << "20ms * 5^(1 - 300/1800) = 76.47ms: the pause target tracks the ramp"; + + // Episode end: the first non-urgent iteration restores the fallback, and + // repeated non-urgent iterations keep it there (not a one-shot edge). + for (int i = 0; i < 100; i++) { + EXPECT_FALSE(ReferenceChainsTestAccessor::applyOomRampForTest(-1.0)); + } + EXPECT_EQ(pass_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "the urgency episode's end must restore the fallback cadence"; + EXPECT_FALSE(ReferenceChainsTestAccessor::urgencyBudgetBoostedForTest()) + << "the episode end must clear the boost flag"; + EXPECT_EQ(ReferenceChainsTestAccessor::configuredBudgetForTest(), + ReferenceChainsTestAccessor::budgetForTest()) + << "the episode end must restore the configured budget"; + EXPECT_EQ(configured_budget, ReferenceChainsTestAccessor::budgetForTest()); + EXPECT_EQ(configured_pause_ms, ReferenceChainsTestAccessor::effectivePauseTargetMsForTest()) + << "the episode end must restore the configured pause target"; +} + +// Outside an episode, a non-urgent iteration must not touch a cadence that +// updatePacing() has adapted (2s here, past the fallback, to prove the write +// path is not merely "leave PASS_CADENCE_NS alone"). Closing an episode is +// the only non-urgent iteration that writes the field. +TEST_F(OomUrgencyTest, NonUrgentIterationKeepsPacedCadence) { + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()) + << "applyOomRampForTest() would race threadLoop()"; + const u64 pass_ns = ReferenceChainsTestAccessor::baselineCadenceNs(); + const u64 paced_ns = 2 * pass_ns; + struct Restore { + ReferenceChainsTestAccessor::OomRampState saved; + ~Restore() { + ReferenceChainsTestAccessor::applyOomRampForTest(-1.0); + ReferenceChainsTestAccessor::setEffectiveCadenceNs( + ReferenceChainsTestAccessor::baselineCadenceNs()); + ReferenceChainsTestAccessor::restoreOomRampStateForTest(saved); + } + } restore{ReferenceChainsTestAccessor::saveOomRampStateForTest()}; + + ReferenceChainsTestAccessor::setEffectiveCadenceNs(paced_ns); + EXPECT_FALSE(ReferenceChainsTestAccessor::applyOomRampForTest(-1.0)); + EXPECT_EQ(paced_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "no episode open: a non-urgent iteration must not rewrite the cadence"; + EXPECT_FALSE(ReferenceChainsTestAccessor::applyOomRampForTest(3600.0)) + << "a projection past the ramp horizon is not urgent"; + EXPECT_EQ(paced_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()); + + // Open and close one episode: the closing iteration restores the fallback. + EXPECT_TRUE(ReferenceChainsTestAccessor::applyOomRampForTest(0.0)); + EXPECT_EQ(ReferenceChainsTestAccessor::urgentCadenceNsForTest(), + ReferenceChainsTestAccessor::effectiveCadenceNs()); + EXPECT_FALSE(ReferenceChainsTestAccessor::applyOomRampForTest(-1.0)); + EXPECT_EQ(pass_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "closing the episode must return the cadence to the fallback, not to " + "the pre-episode adapted value"; +} + +// Flapping across the ramp horizon's edge: each far-side (1801s) iteration +// restores the fallback, and each near-side (1799s) iteration recomputes +// the ramp value from scratch - identical every time, since the ramp anchors +// on PASS_CADENCE_NS, never on the live (previously restored) cadence. +TEST_F(OomUrgencyTest, RampEdgeFlapRestoresAndRecomputesCadence) { + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()) + << "applyOomRampForTest() would race threadLoop()"; + const u64 pass_ns = ReferenceChainsTestAccessor::baselineCadenceNs(); + const u64 urgent_ns = ReferenceChainsTestAccessor::urgentCadenceNsForTest(); + struct Restore { + ReferenceChainsTestAccessor::OomRampState saved; + ~Restore() { + ReferenceChainsTestAccessor::applyOomRampForTest(-1.0); + ReferenceChainsTestAccessor::setEffectiveCadenceNs( + ReferenceChainsTestAccessor::baselineCadenceNs()); + ReferenceChainsTestAccessor::restoreOomRampStateForTest(saved); + } + } restore{ReferenceChainsTestAccessor::saveOomRampStateForTest()}; + + // Half the ceiling: the 4x boost must clamp at MAX_REFERENCE_CHAINS_BUDGET + // on every episode open and restore the configured value on every close. + const int configured_budget = MAX_REFERENCE_CHAINS_BUDGET / 2; + ReferenceChainsTestAccessor::setBudgetsForTest(configured_budget, configured_budget); + + u64 first_ramp_ns = 0; + for (int i = 0; i < 3; i++) { + EXPECT_TRUE(ReferenceChainsTestAccessor::applyOomRampForTest(1799.0)) + << "flap " << i << ": 1799s is inside the ramp horizon"; + EXPECT_EQ(MAX_REFERENCE_CHAINS_BUDGET, ReferenceChainsTestAccessor::budgetForTest()) + << "flap " << i << ": the 4x boost must clamp at the budget ceiling"; + u64 ramp_ns = ReferenceChainsTestAccessor::effectiveCadenceNs(); + EXPECT_GT(ramp_ns, urgent_ns) << "flap " << i; + EXPECT_LT(ramp_ns, pass_ns) + << "flap " << i << ": 1799s must not reach the fallback"; + if (i == 0) { + first_ramp_ns = ramp_ns; + } else { + EXPECT_EQ(first_ramp_ns, ramp_ns) + << "flap " << i << ": the ramp must recompute, not compound"; + } + + EXPECT_FALSE(ReferenceChainsTestAccessor::applyOomRampForTest(1801.0)) + << "flap " << i << ": 1801s is outside the ramp horizon"; + EXPECT_EQ(pass_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "flap " << i << ": the episode end must restore the fallback"; + EXPECT_EQ(configured_budget, ReferenceChainsTestAccessor::budgetForTest()) + << "flap " << i << ": the episode end must restore the configured budget"; + } +} + // The canary-refill half: shouldRunPass() raises the CPU pain budget's // refill rate CANARY_PAIN_BUDGET_REFILL_MULTIPLIER-fold exactly while a // canary chase is open (candidates exist and not all are found), and puts it @@ -1076,6 +1431,73 @@ TEST_F(ReferenceChainsCanaryRefillTest, StopClearsRetainedAdmission) { EXPECT_TRUE(ReferenceChainsTestAccessor::updateUrgentAdmissionForTest(1500.0, true)); } +// stop() is the urgency episode's second closer: an episode still open at the +// recording boundary must not leak its boosted budget ceiling or its ramped +// cadence past it. The closer is guarded on the boost flag, exactly like +// applyOomRamp()'s episode-end branch - outside an episode updatePacing() owns +// the cadence, so stop() must leave a paced cadence alone. Two recordings, +// because stop() returns early once the tracker is disabled. Same BFS-thread +// precondition as StopClearsAdmissionGrowthBaseline. +TEST_F(ReferenceChainsCanaryRefillTest, StopClosesOpenUrgencyEpisode) { + ReferenceChainsTestAccessor::reset(); // also resetUrgencyForTest() + ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); + const ReferenceChainsTestAccessor::OomRampState saved = + ReferenceChainsTestAccessor::saveOomRampStateForTest(); + Arguments args; + ASSERT_FALSE(args.parse("referencechains=true")); + ASSERT_FALSE(tracker->start(args)); + + struct Cleanup { + ReferenceChainTracker *tracker; + ReferenceChainsTestAccessor::OomRampState saved; + ~Cleanup() { + tracker->stop(); + ReferenceChainsTestAccessor::resetUrgencyForTest(); + ReferenceChainsTestAccessor::restoreOomRampStateForTest(saved); + ReferenceChainsTestAccessor::reset(); + } + } cleanup{tracker, saved}; + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()) + << "applyOomRampForTest() would race threadLoop()"; + const u64 pass_ns = ReferenceChainsTestAccessor::baselineCadenceNs(); + const int configured = ReferenceChainsTestAccessor::configuredBudgetForTest(); + ASSERT_GT(configured, 0) << "start() must record a positive configured budget"; + + // Recording 1, no episode open: a paced (non-fallback) cadence survives + // stop() - the closer must not fire without the boost flag. + ASSERT_FALSE(ReferenceChainsTestAccessor::urgencyBudgetBoostedForTest()); + ReferenceChainsTestAccessor::setEffectiveCadenceNs(2 * pass_ns); + tracker->stop(); + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()); + EXPECT_EQ(2 * pass_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "stop() must not rewrite a cadence updatePacing() owns outside an episode"; + EXPECT_EQ(configured, ReferenceChainsTestAccessor::budgetForTest()); + + // Recording 2: start() reseeds the cadence, then an episode opens. + ASSERT_FALSE(tracker->start(args)); + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()) + << "applyOomRampForTest() would race threadLoop()"; + EXPECT_EQ(pass_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "start() reseeds the cadence"; + ASSERT_TRUE(ReferenceChainsTestAccessor::applyOomRampForTest(0.0)); + ASSERT_TRUE(ReferenceChainsTestAccessor::urgencyBudgetBoostedForTest()); + ASSERT_EQ(std::min(configured * 4, MAX_REFERENCE_CHAINS_BUDGET), + ReferenceChainsTestAccessor::budgetForTest()); + ASSERT_EQ(ReferenceChainsTestAccessor::urgentCadenceNsForTest(), + ReferenceChainsTestAccessor::effectiveCadenceNs()); + + tracker->stop(); + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()); + EXPECT_FALSE(ReferenceChainsTestAccessor::urgencyBudgetBoostedForTest()) + << "stop() must close the open urgency episode"; + EXPECT_EQ(ReferenceChainsTestAccessor::configuredBudgetForTest(), + ReferenceChainsTestAccessor::budgetForTest()) + << "stop() must restore the configured budget ceiling"; + EXPECT_EQ(configured, ReferenceChainsTestAccessor::budgetForTest()); + EXPECT_EQ(pass_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "stop() must restore the fallback cadence"; +} + TEST_F(ReferenceChainsCanaryRefillTest, BackoffGateHoldsUnlessOomRampActive) { ReferenceChainsTestAccessor::reset(); ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); diff --git a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h index 1134cf7460..be66159adc 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h +++ b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h @@ -191,6 +191,15 @@ class ReferenceChainsTestAccessor { return ReferenceChainTracker::instance()->updateUrgentAdmission(seconds_to_oom, urgent); } + // updateUrgentAdmission()'s freshness pair (referenceChains.h): the + // heap-floor seq the ramp-band gate last evaluated, and that evaluation's + // retained result. + static u64 admissionSampleSeqForTest() { + return ReferenceChainTracker::instance()->_admission_sample_seq; + } + static bool admissionDecisionForTest() { + return ReferenceChainTracker::instance()->_admission_decision; + } // True while the BFS thread (startThread()/threadLoop()) is live. start() never spawns it // (only startThread() does - profiler.cpp and the JNI-only resetSearchStateForTest()), so // gtests may drive BFS-thread-only seams after start(); tests that rely on that assert this @@ -577,6 +586,75 @@ class ReferenceChainsTestAccessor { static u64 baselineCadenceNs() { return ReferenceChainTracker::PASS_CADENCE_NS; } + static u64 urgentCadenceNsForTest() { + return ReferenceChainTracker::URGENT_CADENCE_NS; + } + + // threadLoop()'s OOM ramp (applyOomRamp()), drivable without the BFS thread. + static bool applyOomRampForTest(double seconds_to_oom) { + return ReferenceChainTracker::instance()->applyOomRamp(seconds_to_oom); + } + + // Every scheduling field an urgency episode (applyOomRamp()) mutates or a test seeds to drive + // it - cadence, budget + boost flag, configured budget, configured/effective pause target and + // the pause PidController - so a test can restore the singleton exactly as it found it. + // Field order is fixed for aggregate init. + struct OomRampState { + u64 cadence_ns; + int budget; + bool budget_boosted; + int configured_budget; + long pause_target_ms; + long effective_pause_target_ms; + PidController pause_pid; + }; + static OomRampState saveOomRampStateForTest() { + ReferenceChainTracker *t = ReferenceChainTracker::instance(); + return {t->_effective_cadence_ns, t->_budget, t->_urgency_budget_boosted, + t->_configured_budget, t->_pause_target_ms, t->_effective_pause_target_ms, + t->_pause_pid}; + } + static void restoreOomRampStateForTest(const OomRampState &state) { + ReferenceChainTracker *t = ReferenceChainTracker::instance(); + t->_effective_cadence_ns = state.cadence_ns; + t->_budget = state.budget; + t->_urgency_budget_boosted = state.budget_boosted; + t->_configured_budget = state.configured_budget; + t->_pause_target_ms = state.pause_target_ms; + t->_effective_pause_target_ms = state.effective_pause_target_ms; + t->_pause_pid = state.pause_pid; + } + + // Urgency-episode budget state (applyOomRamp()'s boost/restore, stop()'s closer). + static int budgetForTest() { return ReferenceChainTracker::instance()->_budget; } + static int configuredBudgetForTest() { + return ReferenceChainTracker::instance()->_configured_budget; + } + static bool urgencyBudgetBoostedForTest() { + return ReferenceChainTracker::instance()->_urgency_budget_boosted; + } + // Seeds a closed episode with a known budget ceiling (the singleton may never have been + // start()ed); callers wanting equal values pass the same value twice. + static void setBudgetsForTest(int budget, int configured) { + ReferenceChainTracker *t = ReferenceChainTracker::instance(); + t->_budget = budget; + t->_configured_budget = configured; + t->_urgency_budget_boosted = false; + } + + // applyOomRamp()'s pause-target ramp. + static long effectivePauseTargetMsForTest() { + return ReferenceChainTracker::instance()->_effective_pause_target_ms; + } + static long urgentPauseTargetMsForTest() { + return ReferenceChainTracker::URGENT_PAUSE_TARGET_MS; + } + static void setPauseTargetsForTest(long configured_ms) { + ReferenceChainTracker *t = ReferenceChainTracker::instance(); + t->_pause_target_ms = configured_ms; + t->_effective_pause_target_ms = configured_ms; + } + // Start the controller from a chosen below-ceiling/above-baseline point with a fresh // _pause_pid, so a prior sequence's integral windup cannot leak into per-step assertions. static void setEffectiveBudget(int v) { From 04f4fa761f97864d441a8231f8270a336130f799 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 12:22:30 +0100 Subject: [PATCH 26/37] test: correct the unbounded-cache test comments resize() relinks existing HashMap objects and EXPANDED entries are revisited by the bounded rotation, so the previous permanent-freeze wording described mechanisms that do not exist. --- .../ReferenceChainTrackingTest.java | 33 ++++++++----------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java index be68959a03..9bdbfe26b7 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/ReferenceChainTrackingTest.java @@ -523,30 +523,23 @@ public void shouldReconstructReferrerChainToGcRoot() throws Exception { * through {@code java.util.HashMap}'s own internal storage ({@code HashMap$Node}) - that the * mechanism correctly walks a real JDK collection's internals, not only purpose-built fixtures. * - *

Why {@code cache} is a local variable, not a {@code static} field: - * {@code expandFrontier()} freezes whatever children it observes for a node the moment that node - * is expanded - it never re-examines an already-{@code EXPANDED} entry for newly-added children. - * A {@code static} field becomes a root at class-load time, essentially guaranteeing some early - * pass catches it (and marks it {@code EXPANDED}) while still empty - permanently blocking - * discovery of anything added afterward. A local variable created fresh at the top of this - * method, immediately followed by round-1 allocation, gives root enumeration a real chance to - * catch it only after it already holds live data. + *

Why {@code cache} is a local variable, not a {@code static} field: a static field + * becomes a root at class-load time and is likely expanded while still empty, so additions made + * afterward are discovered only when the bounded rotation revisit + * ({@code collectStaleExpandedEntriesForRotation()} in referenceChainWalk.cpp) re-queues that + * {@code EXPANDED} entry and re-walks its fields. A local variable created fresh at the top of + * this method, immediately followed by round-1 allocation, is first enumerated as a root only + * after it already holds live data. */ @Test @Order(2) public void shouldReconstructReferrerChainThroughUnboundedCacheLeak() throws Exception { - // Pre-sized to hold every round's entries without ever crossing HashMap's 0.75 load factor: - // 131072 buckets hold up to 98304 entries, and this fixture caps at 69001 + the seed below. - // This is load-bearing, not a tuning nicety. Every ordinary resize replaces the table and - // rehashes every entry into NEW HashMap$Node objects - the table/Node path the BFS walk - // admitted (and the reconstructed chain routes through) dies, the walk prunes the retired - // entries, and the replacement table is never admitted because the cache HashMap's own - // frontier entry is already EXPANDED (the frozen-children hazard documented in the class - // javadoc above) - traced live as a representative permanently stuck at frontier state -1 - // (never admitted / entry pruned) with zero leak-tag interceptions for the rest of the - // search. A fixed table keeps every Node ever admitted (and the representative's parent - // chain) alive for the whole test, so the representative path and the rotation re-walk see a - // stable, growing container. + // Pre-sized so the walk never sees a resize: 131072 buckets admit up to 98304 entries at + // HashMap's 0.75 load factor, and this fixture adds 69001 in total (69000 across the rounds + // plus the seed below). The table is allocated once and never replaced, so the table/Node + // path the walk admits stays the same live objects for the whole test; a replacement table + // (e.g. after a resize) would instead be discovered only via the bounded rotation revisit of + // EXPANDED entries, within the walk's bounded budget. Map cache = new HashMap<>(1 << 17); cache.put("cache-leak-seed", new CachedPayload("cache-leak-seed")); if (ReferenceChainAssertions.isDebugInstrumentedBuild()) { From 7a9a8396bc78523de2e561584a530da9beaf6552 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 12:22:30 +0100 Subject: [PATCH 27/37] doc: urgency applies before the search gate; cadence has two writers threadLoop() publishes urgency and ramps cadence/admission on every iteration whether or not a search runs; state the restore-on-exit and the counter's per-refused-evaluation semantics. --- .../LiveHeapReferenceChainsImplementation.md | 2 +- doc/architecture/ReferenceChainsSignalsExplained.md | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/doc/architecture/LiveHeapReferenceChainsImplementation.md b/doc/architecture/LiveHeapReferenceChainsImplementation.md index 79860b6805..d6ad2d2c7e 100644 --- a/doc/architecture/LiveHeapReferenceChainsImplementation.md +++ b/doc/architecture/LiveHeapReferenceChainsImplementation.md @@ -173,7 +173,7 @@ The implementation controls different costs separately: - `_effective_budget` limits edge admission for ordinary work. - `_pause_pid` adjusts that budget from measured in-safepoint time. -- `_effective_cadence_ns` changes when the budget controller reaches a limit. +- `_effective_cadence_ns` changes when the budget controller reaches a limit and while the OOM urgency ramp is active. When an urgency episode ends, the ramp restores the one-second fallback in the same iteration. - `_safepoint_pain_budget` gates a later search. - `_cpu_pain_budget` gates passes in the current search. - `_configured_frontier_cap` limits native metadata. diff --git a/doc/architecture/ReferenceChainsSignalsExplained.md b/doc/architecture/ReferenceChainsSignalsExplained.md index 22a296fbf7..22dbcfaabb 100644 --- a/doc/architecture/ReferenceChainsSignalsExplained.md +++ b/doc/architecture/ReferenceChainsSignalsExplained.md @@ -20,9 +20,11 @@ The GC callback does not wake the thread. The normal idle loop checks the state Before the first search, `shouldRunPass()` calls `canAffordNewSearch()`. The gate always requires a leak indication: `selectLeakCandidates()` finds a class with sustained population growth. The safepoint cost budget must also permit the search. -Urgency does not authorize a search. A candidate-less search can never emit a chain, and while urgent the 30-pass no-progress abandonment is suppressed, so it would run to the full TTL doing whole-heap BFS. Urgency applies through the ramp once a search is running. +Urgency does not authorize a search. A candidate-less search can never emit a chain, and while urgent the 30-pass no-progress abandonment is suppressed, so it would run to the full TTL doing whole-heap BFS. Urgency instead acts before the gate: on every iteration, `threadLoop()` computes the OOM ramp from the OOM projection and calls `publishUrgencyState()`, which publishes the ramp state and the tracking-admission decision, before `shouldRunPass()` evaluates the search gate. -The tradeoff of this candidate-only gate: a heap-wide leak spread across many classes, none of which individually clears `selectLeakCandidates()`'s per-klass ring-fill/hysteresis gate, never opens the gate, even in the emergency band (projected OOM under 5 minutes). No chains are produced in that case. Each such refused evaluation increments the `reference_chain_urgent_no_candidate` counter (about once per second while urgency is latched and no candidate exists), so the gap is visible in the diagnostics rather than silent, and it can be told apart from an idle healthy process. +In the emergency band, where the projection is under `OOM_URGENT_THRESHOLD_S` (5 minutes), `updateUrgentAdmission()` turns on the global 100% tracking admission with zero candidates and no running search. `admitForTracking()` falls back to the normal admission rules while the tracking table is at `_table_max_cap`; that case increments `LIVENESS_URGENT_BOOST_BACKED_OFF`, and each admitted allocation increments `LIVENESS_URGENT_BOOST_ADMITS`. + +The tradeoff of this candidate-only gate: a heap-wide leak spread across many classes, none of which individually clears `selectLeakCandidates()`'s per-klass ring-fill/hysteresis gate, never opens the gate, even in the emergency band (projected OOM under 5 minutes). No chains are produced in that case. Each such refused evaluation increments the `reference_chain_urgent_no_candidate` counter. The evaluation runs only after the safepoint cost budget permits it: `canAffordNewSearch()` checks the budget before it calls `hasLeakSignal()`. The evaluation rate therefore follows the ramped cadence, not a fixed rate. The gap is visible in the diagnostics rather than silent, and it can be told apart from an idle healthy process. The population-trend signal is constructed by the liveness tracker, which fills the per-klass population table and the heap-floor ring when generations tracking OR liveheap tracking with reference chains enabled is active (the two options drive the same tracking mechanisms; liveheap additionally preserves stack traces for its samples). The reference-chain collector is the signal's only consumer, so a plain liveheap session without `referencechains` keeps the signal disarmed. When no source is active, no signal can ever be produced: `hasLeakSignal()` returns false, and the collector stays idle - it never runs a pass, restart, or search. @@ -50,7 +52,7 @@ The OOM ramp disables this backoff. The non-safepoint CPU cost budget still appl When no unresolved candidate controls scheduling, a completed GC authorizes a pass. The tracker compares the current GC-finish epoch with the epoch recorded by the previous pass. -The fallback cadence also authorizes a pass. It starts at one second and the pause controller can adjust it between 10 milliseconds and four seconds. +The fallback cadence also authorizes a pass. It starts at one second. Two writers adjust it between 10 milliseconds and four seconds: the pause controller (`updatePacing()`) and the OOM urgency ramp. When an urgency episode ends, the ramp restores the one-second fallback in the same iteration. ## Leak signal @@ -116,7 +118,7 @@ neither source provides a valid estimate, or that the projection is still in its Two ranges have different purposes: -- At less than 30 minutes, the OOM ramp increases the pause target, shortens the cadence, and increases the edge-budget ceiling. The 100% liveness-sampling admission follows the same band, but outside the emergency band it additionally requires fresh growth evidence: the newest heap-floor sample must exceed the episode's running high-water mark (the entry value, raised by every admitted sample). The growth check is evaluated once per new heap-floor sample (one per GC), and its result is held until the next sample arrives, so an admitted growth sample keeps the boost on until the next GC. The projection is a smoothed regression over a 30-sample window, so a single spike sample keeps the projection inside the band for several recovery GCs after the heap has physically stopped growing. Growth evidence is what keeps the admission boost off for such a recovered spike. Inside the emergency band (less than 5 minutes), admission is immediate. +- At less than 30 minutes, the OOM ramp increases the pause target, shortens the cadence toward its 10 millisecond floor, and increases the edge-budget ceiling. The 100% liveness-sampling admission follows the same band, but outside the emergency band it additionally requires fresh growth evidence: the newest heap-floor sample must exceed the episode's running high-water mark (the entry value, raised by every admitted sample). The growth check is evaluated once per new heap-floor sample (one per GC), and its result is held until the next sample arrives, so an admitted growth sample keeps the boost on until the next GC. The projection is a smoothed regression over a 30-sample window, so a single spike sample keeps the projection inside the band for several recovery GCs after the heap has physically stopped growing. Growth evidence is what keeps the admission boost off for such a recovered spike. Inside the emergency band (less than 5 minutes), admission is immediate. - At less than 5 minutes, `isUrgent()` latches an urgent episode. The latch does not start searches, and the ramp calculation does not consult it. Its only consumer is the no-progress abandonment of a running search, which it suppresses while the latch is held. Both ranges apply only after the burn-in period. @@ -127,6 +129,8 @@ The urgent latch clears after five consecutive checks at 10 minutes or more, or The ramp and the latch use different thresholds. Entering the ramp does not by itself latch urgency. +The ramp shortens both the idle sleep and the fallback cadence gate. It runs from one second (`PASS_CADENCE_NS`) at 30 minutes out to 10 milliseconds (`URGENT_CADENCE_NS`) at OOM, following `cadence = 1 s × 0.01^(1 − s/1800)` for a projection of s seconds. This allows up to about 100 loop evaluations per second, not counting evaluation time. At the 5-minute boundary the ramp gives about 21.5 milliseconds, about 46 evaluations per second. These values follow from the formula; execution time per evaluation is additional, and none of them is a guarantee. + ## Terminal state and restart A search can become `COMPLETED` or `ABANDONED`. Before a restart, the tracker must confirm that it released all search-owned tags. From f7bb70a7b5a3f078cc1245543dedf76330ce08fd Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 15:06:52 +0100 Subject: [PATCH 28/37] referenceChains: run the urgency latch once per iteration The latch machine advanced only inside hasLeakSignal(), which sits behind canAffordNewSearch()'s cooldown - a latched episode stayed frozen (potentially stale-latched after the heap recovered) for as long as the cooldown blocked restarts, and runPass() advanced it again from a second, freshly-read projection. The machine now runs in publishUrgencyState() on threadLoop()'s single projection; hasLeakSignal() and runPass() consume urgencyLatched() without updating it. Also extract resetUrgencyState()/ closeUrgencyEpisode() from the three copied reset blocks and close a leaked urgency episode (boosted budget, ramped cadence) in resetSearchStateForTest(). --- .../src/main/cpp/referenceChainTraversal.cpp | 10 +- ddprof-lib/src/main/cpp/referenceChains.cpp | 93 ++++++++++++------- ddprof-lib/src/main/cpp/referenceChains.h | 31 +++---- .../src/test/cpp/referenceChainsBfsTests.inc | 8 ++ .../src/test/cpp/referenceChainsOomTests.inc | 43 +++++++-- .../test/cpp/referenceChainsTestAccessors.h | 3 +- 6 files changed, 120 insertions(+), 68 deletions(-) diff --git a/ddprof-lib/src/main/cpp/referenceChainTraversal.cpp b/ddprof-lib/src/main/cpp/referenceChainTraversal.cpp index ceb9fb1292..57f3179a0d 100644 --- a/ddprof-lib/src/main/cpp/referenceChainTraversal.cpp +++ b/ddprof-lib/src/main/cpp/referenceChainTraversal.cpp @@ -1128,9 +1128,11 @@ bool ReferenceChainTracker::runPass(jvmtiEnv *jvmti, JNIEnv *jni, (unsigned long long)((OS::nanotime() - load(_search_start_ns)) / 1000000)); } else if (_passes_since_last_progress >= NO_PROGRESS_PASS_LIMIT && - !isUrgent()) { + !urgencyLatched()) { // The frontier hasn't grown for NO_PROGRESS_PASS_LIMIT consecutive passes — the search is - // genuinely stuck (not just slow), so abandon. + // genuinely stuck (not just slow), so abandon. Reads the latch published by + // publishUrgencyState()'s per-iteration isUrgent() call; calling isUrgent() here would + // advance the machine from a second, freshly-read projection. store(_abandon_reason, (u8)SearchAbandonReason::TTL); storeRelease(_search_state, (u8)SearchState::ABANDONED); enqueuePendingAbandonedEvent(); @@ -1145,8 +1147,8 @@ bool ReferenceChainTracker::runPass(jvmtiEnv *jvmti, JNIEnv *jni, _passes_since_last_progress >= NO_PROGRESS_PASS_LIMIT && _passes_since_last_candidate_progress >= canaryStuckPassLimit()) { - // Canary-specific stuck detector - deliberately NOT suppressed by isUrgent() (contrast the - // ordinary TTL check above). + // Canary-specific stuck detector - deliberately NOT suppressed by the urgency latch + // urgencyLatched() (contrast the ordinary TTL check above). store(_abandon_reason, (u8)SearchAbandonReason::CANARY_STUCK); storeRelease(_search_state, (u8)SearchState::ABANDONED); enqueuePendingAbandonedEvent(); diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index 724a9705bb..ea27584bd7 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -421,24 +421,15 @@ void ReferenceChainTracker::stop() { // updateUrgentAdmission(), restartSearch()) as plain stores; safe to reset // from this thread only because stopThread() has joined that thread. store(_search_start_ns, (u64)0); - _urgent_latched = false; - _urgent_release_ticks = 0; - _admission_growth_baseline = 0; - _admission_sample_seq = 0; - _admission_decision = false; + resetUrgencyState(); // Close the urgency episode the BFS thread may have left open: the boosted // budget ceiling and the ramped cadence are episode state - the same state // applyOomRamp()'s first non-urgent iteration restores on the BFS thread - // and a mid-episode stop() would otherwise leak them past the recording // boundary (buildAbandonedEvent() reads _budget after this point). - // Guarded on the boost flag because outside an episode updatePacing() - // owns _effective_cadence_ns; stopThread() has already joined the BFS - // thread, so these writes are as safe as the urgency resets above. - if (_urgency_budget_boosted) { - _urgency_budget_boosted = false; - _budget = _configured_budget; - _effective_cadence_ns = PASS_CADENCE_NS; - } + // stopThread() has already joined the BFS thread, so these writes are as + // safe as the urgency resets above. + closeUrgencyEpisode(); // Same per-search candidate/discovered canary state runPass()'s terminal branch and // restartSearch() clear - zeroing the counts here closes the stale-chain windows in // pollWatchedTargets() (dead-representative canary path, discovered-instances path) for the @@ -549,11 +540,9 @@ bool ReferenceChainTracker::applyOomRamp(double seconds_to_oom) { "budget=%d configured=%d", _budget, _configured_budget); } else if (!urgent && _urgency_budget_boosted) { - _urgency_budget_boosted = false; - _budget = _configured_budget; + closeUrgencyEpisode(); // The episode owned the cadence while it ran; hand it back at the fallback in the same // iteration so the idle loop stops waking at the ramped rate. - _effective_cadence_ns = PASS_CADENCE_NS; TEST_LOG_SUMMARY("ReferenceChainTracker::threadLoop urgency budget restore " "budget=%d", _budget); } @@ -809,7 +798,10 @@ bool ReferenceChainTracker::shouldRunPass(u64 now_ns, double seconds_to_oom) { // reusing pollWatchedTargets()'s own selectLeakCandidates() call - that one runs after runPass() in // threadLoop()'s own iteration and needs the *list* to poll each candidate's tag; this only needs // to know whether at least one exists. -bool ReferenceChainTracker::isUrgent() const { +// Test-driving form of the latch machine: evaluates the live projection +// (what the zero-arg form used to read) so tests advance the same machine +// publishUrgencyState() runs per iteration. +bool ReferenceChainTracker::isUrgentForTest() const { return isUrgent(LivenessTracker::instance()->secondsToOOM()); } @@ -929,8 +921,42 @@ bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool ur return _admission_decision; } +// The urgency/admission reset shared by stop(), resetSearchStateForTest() and +// resetUrgencyForTest(): the latch/release machine, the episode baseline, and +// the freshness pair - the pair needs its own reset because an emergency-band +// re-entry seeds the baseline without touching it, so a leaked pair would +// make the next ramp-band call with no new GC sample return the previous +// recording's (or test's) retained admission decision. +void ReferenceChainTracker::resetUrgencyState() { + _urgent_latched = false; + _urgent_release_ticks = 0; + _admission_growth_baseline = 0; + _admission_sample_seq = 0; + _admission_decision = false; +} + +// Closes an urgency episode: restores the boosted budget ceiling and the +// ramped cadence. Guarded on the boost flag because outside an episode +// updatePacing() owns _effective_cadence_ns; applyOomRamp() and stop() (the +// latter only because stopThread() has already joined the BFS thread) are the +// production callers. +void ReferenceChainTracker::closeUrgencyEpisode() { + if (_urgency_budget_boosted) { + _urgency_budget_boosted = false; + _budget = _configured_budget; + _effective_cadence_ns = PASS_CADENCE_NS; + } +} + void ReferenceChainTracker::publishUrgencyState(double seconds_to_oom, bool urgent) { _oom_ramp_active = urgent; + // The latch machine runs here, once per iteration on threadLoop()'s single + // projection read. It must not run inside hasLeakSignal(): that gate sits + // behind canAffordNewSearch()'s cooldown check, and while the cooldown + // blocks restarts the latch would stay frozen - possibly still latched + // after the heap recovered, suppressing runPass()'s no-progress + // abandonment for a search the stale latch then starts. + isUrgent(seconds_to_oom); LivenessTracker::instance()->setUrgentTracking(updateUrgentAdmission(seconds_to_oom, urgent)); } @@ -945,16 +971,15 @@ bool ReferenceChainTracker::hasLeakSignal(double seconds_to_oom) { // diagnostic value. return false; } - // isUrgent() is called unconditionally, not short-circuited behind any - // branch: it maintains the latch/release state (see _urgent_latched), whose - // only consumer is the no-progress-abandonment suppression once a search - // runs (runPass()'s terminal gate). It evaluates the caller's projection - // (threadLoop()'s single per-iteration read), not a fresh secondsToOOM(). - // The OOM ramp's budget/cadence effects in threadLoop() follow the raw band - // comparison, and the 100% tracking admission follows - // updateUrgentAdmission()'s growth gate (via publishUrgencyState()) - + // Reads the latch published by this iteration's publishUrgencyState() call + // (which runs isUrgent() unconditionally, on threadLoop()'s single + // projection read) - never a fresh secondsToOOM(), and never an update: + // this gate sits behind canAffordNewSearch()'s cooldown, so advancing the + // latch here would freeze it for the cooldown's duration. The OOM ramp's + // budget/cadence effects follow the raw band comparison and the 100% + // tracking admission follows updateUrgentAdmission()'s growth gate; // neither waits for this latch. - bool urgent = isUrgent(seconds_to_oom); + bool urgent = _urgent_latched; KlassCandidate probe[1]; int n = LivenessTracker::instance()->selectLeakCandidates(probe, 1); // The urgent latch does NOT entitle a candidate-less search. With nothing @@ -1093,16 +1118,12 @@ void ReferenceChainTracker::resetSearchStateForTest(jvmtiEnv *jvmti, // _safepoint_pain_budget above. _cpu_pain_budget = PainBudget(_pain_budget_refill_rate); // Same test-isolation rationale: a latched urgency episode left behind by an earlier test would - // otherwise ramp this one's cadence and pause target from a stale projection, and a stale - // admission growth baseline would skip this test's first-observation re-snapshot, or a retained - // admission decision (_admission_sample_seq/_admission_decision - an emergency-band re-entry - // seeds the baseline without touching them, same reason as stop()) would be returned for this - // test's first ramp-band call without a new sample. - _urgent_latched = false; - _urgent_release_ticks = 0; - _admission_growth_baseline = 0; - _admission_sample_seq = 0; - _admission_decision = false; + // otherwise ramp this one's cadence and pause target from a stale projection. resetUrgencyState() + // covers the latch/baseline/freshness pair (the pair rationale matches stop()); + // closeUrgencyEpisode() also restores a boosted _budget/_effective_cadence_ns, which the reset + // previously missed - a prior test's mid-episode stop leaked the boost into this one. + resetUrgencyState(); + closeUrgencyEpisode(); if (_frontier != nullptr) { // Rebuilds the table at this test's own _configured_frontier_cap, undoing any smaller framecap= diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index 2ed62e4f55..701beb1363 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -824,11 +824,13 @@ class ReferenceChainTracker { bool hasLeakSignal(double seconds_to_oom); // Latched, hysteretic view of LivenessTracker::secondsToOOM() crossing OOM_URGENT_THRESHOLD_S - - // see _urgent_latched for the latch/release rules and why the raw comparison flaps. The - // one-arg form evaluates the caller's projection; the zero-arg form reads secondsToOOM() and - // forwards (runPass()'s no-progress check in referenceChainTraversal.cpp, isUrgentForTest()). - bool isUrgent() const; + // see _urgent_latched for the latch/release rules and why the raw comparison flaps. The latch + // machine runs once per iteration in publishUrgencyState(), on the caller's projection + // (threadLoop()'s single read); runPass() and hasLeakSignal() consume the latched state via + // urgencyLatched() without advancing the machine. bool isUrgent(double seconds_to_oom) const; + // Read-only view of the latch (see isUrgent()). + bool urgencyLatched() const { return _urgent_latched; } // Computes the 100% tracking-admission decision. Outside the emergency band // the decision is re-evaluated only when a new heap-floor sample arrives and @@ -843,6 +845,9 @@ class ReferenceChainTracker { // Publishes one threadLoop() iteration's urgency: _oom_ramp_active follows the raw band // (`urgent`) for shouldRunPass()'s canary-backoff bypass; LivenessTracker's 100% tracking boost + void resetUrgencyState(); + void closeUrgencyEpisode(); + // (setUrgentTracking()) follows updateUrgentAdmission()'s growth-gated decision, never raw // `urgent`. Extracted from threadLoop() so the wiring is testable without a BFS thread. void publishUrgencyState(double seconds_to_oom, bool urgent); @@ -1225,24 +1230,10 @@ class ReferenceChainTracker { // OOM-urgency hysteresis state (isUrgent()'s latch/release machine, // referenceChains.cpp): tests drive it via isUrgentForTest() and assert on // the raw latch/tick reads. - bool isUrgentForTest() const { return isUrgent(); } + bool isUrgentForTest() const; bool urgentLatchedForTest() const { return _urgent_latched; } int urgentReleaseTicksForTest() const { return _urgent_release_ticks; } - void resetUrgencyForTest() { - _urgent_latched = false; - _urgent_release_ticks = 0; - // updateUrgentAdmission()'s episode baseline - a stale one across tests - // would make an episode's "first observation" branch unreachable. - _admission_growth_baseline = 0; - // The freshness pair needs its own reset even though the baseline reset - // already forces a ramp-band re-entry through the first-observation - // branch: an emergency-band re-entry seeds the baseline without touching - // _admission_sample_seq/_admission_decision, so a leaked pair would make - // the next ramp-band call with no new sample return the previous test's - // retained decision. - _admission_sample_seq = 0; - _admission_decision = false; - } + void resetUrgencyForTest() { resetUrgencyState(); } // Applied vs configured CPU pain-budget refill rate: shouldRunPass() raises // the applied rate CANARY_PAIN_BUDGET_REFILL_MULTIPLIER-fold only while a // canary chase is open - the engage/disengage pair the refill test asserts. diff --git a/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc b/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc index 96c82590e2..34696f137e 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsBfsTests.inc @@ -736,6 +736,11 @@ TEST_F(ReferenceChainsBfsTest, CanaryStuckLimitDoublesPerRestartAndResetsOnClean } } ASSERT_NEAR(LivenessTracker::instance()->secondsToOOM(), 240.0, 1.0); + // runPass() consumes the iteration's latched urgency (publishUrgencyState() + // is threadLoop()'s per-iteration latch update and does not run here), so + // seed the latch the way threadLoop() would with this projection. + ReferenceChainsTestAccessor::publishUrgencyStateForTest(240.0, true); + ASSERT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()); constexpr int kChainLength = 10; std::vector nodes; @@ -813,6 +818,9 @@ TEST_F(ReferenceChainsBfsTest, CanaryStuckLimitDoublesPerRestartAndResetsOnClean LivenessTracker::instance()->setLeakSignalActivatedNsForTest(0); tracker->stop(); ReferenceChainsTestAccessor::resetUrgencyForTest(); + // The per-iteration publish (publishUrgencyStateForTest above) set the ramp flag; the latch + // reset does not cover it, and the next test's canary-backoff gate keys on it. + ReferenceChainsTestAccessor::setOomRampActive(false); ReferenceChainsTestAccessor::setWatchedLeakKlassIdsForTest({}); ReferenceChainsTestAccessor::reset(); } diff --git a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc index 01bad8801f..82e134132c 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc @@ -118,6 +118,31 @@ TEST_F(OomUrgencyTest, MidBandOscillationNeverFlaps) { } } +// The latch machine runs in publishUrgencyState(), once per iteration on +// threadLoop()'s single projection - not in hasLeakSignal(), which sits +// behind canAffordNewSearch()'s cooldown and would otherwise freeze the +// latch (possibly stale-latched after the heap recovered) for as long as +// the cooldown blocks restarts. Drives the machine through the production +// entry point the loop uses, including the release after 5 clear +// observations. +TEST_F(OomUrgencyTest, LatchMachineRunsThroughPublishUrgencyState) { + ASSERT_NEAR(project(299.0), 299.0, 1.0); + ReferenceChainsTestAccessor::publishUrgencyStateForTest(299.0, true); + ASSERT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()); + + // Five consecutive clear observations through the production entry point + // release the latch, one tick at a time. + for (int i = 1; i <= 4; i++) { + ReferenceChainsTestAccessor::publishUrgencyStateForTest(700.0, false); + EXPECT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()) + << "released after only " << i << " clear observations"; + EXPECT_EQ(i, ReferenceChainsTestAccessor::urgentReleaseTicksForTest()); + } + ReferenceChainsTestAccessor::publishUrgencyStateForTest(700.0, false); + EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()); + EXPECT_EQ(0, ReferenceChainsTestAccessor::urgentReleaseTicksForTest()); +} + // A single observation past the release bar must NOT release; only // URGENT_RELEASE_CONSECUTIVE (5) consecutive clear observations do, and any // mid-band reading in between restarts the count. @@ -286,12 +311,16 @@ TEST_F(OomUrgencyTest, UrgencyDecisionsConsumeThePassedProjection) { EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()) << "isUrgent(double) must not re-read the live (urgent) projection"; - // hasLeakSignal(double). + // hasLeakSignal(double) - and everything forwarding into it + // (canAffordNewSearch, shouldRunPass) - only READS the latch: the machine + // advances once per iteration in publishUrgencyState(), never from the + // gates behind canAffordNewSearch()'s cooldown (a stale latch frozen for + // the cooldown's duration was the regression this contract pins against). ReferenceChainsTestAccessor::resetUrgencyForTest(); ASSERT_LT(project(0.0, /*flat=*/true), 0.0); ReferenceChainsTestAccessor::hasLeakSignalForTest(200.0); - EXPECT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()) - << "hasLeakSignal() must latch on the passed projection, not re-read secondsToOOM()"; + EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()) + << "hasLeakSignal() must not advance the latch machine"; ReferenceChainsTestAccessor::resetUrgencyForTest(); ASSERT_NEAR(project(200.0), 200.0, 1.0); ReferenceChainsTestAccessor::hasLeakSignalForTest(-1.0); @@ -304,12 +333,12 @@ TEST_F(OomUrgencyTest, UrgencyDecisionsConsumeThePassedProjection) { ReferenceChainsTestAccessor::resetUrgencyForTest(); ASSERT_LT(project(0.0, /*flat=*/true), 0.0); ReferenceChainsTestAccessor::canAffordNewSearch(OS::nanotime(), 200.0); - EXPECT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()) - << "canAffordNewSearch() must forward the passed projection"; + EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()) + << "canAffordNewSearch() must not advance the latch machine"; ReferenceChainsTestAccessor::resetUrgencyForTest(); ReferenceChainsTestAccessor::shouldRunPass(OS::nanotime(), 200.0); - EXPECT_TRUE(ReferenceChainsTestAccessor::urgentLatchedForTest()) - << "shouldRunPass() must forward the passed projection"; + EXPECT_FALSE(ReferenceChainsTestAccessor::urgentLatchedForTest()) + << "shouldRunPass() must not advance the latch machine"; ReferenceChainsTestAccessor::reset(); ReferenceChainsTestAccessor::resetUrgencyForTest(); diff --git a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h index be66159adc..fc4c9af746 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h +++ b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h @@ -207,7 +207,8 @@ class ReferenceChainsTestAccessor { static bool bfsThreadRunning() { return ReferenceChainTracker::instance()->_running.load(std::memory_order_acquire); } - // threadLoop()'s publish step: ramp flag + growth-gated tracking boost. + // threadLoop()'s publish step: ramp flag + the latch machine (once per + // iteration on the caller's projection) + growth-gated tracking boost. static void publishUrgencyStateForTest(double seconds_to_oom, bool urgent) { ReferenceChainTracker::instance()->publishUrgencyState(seconds_to_oom, urgent); } From 29d5bac9a14a16e47cdd479fff03b8ec0e0427c7 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 15:06:52 +0100 Subject: [PATCH 29/37] livenessTracker: release/acquire the leak-signal flag; clear it on chain-start failure The flag gates klass folding, heap-floor sampling and forced sweeps whose readers consume table state populated under the same flag, so the start() write and every load are now release/acquire. When ReferenceChainTracker::start() fails, Profiler::start() clears the (liveness && reference_chains) leg again and unstamps the burn-in clock - without the tracker that work has no consumer, and a stale stamp would hand a later chains-enabled recording an already-expired burn-in. --- ddprof-lib/src/main/cpp/livenessTracker.cpp | 25 +++++++------ ddprof-lib/src/main/cpp/livenessTracker.h | 41 ++++++++++++++++----- ddprof-lib/src/main/cpp/profiler.cpp | 4 ++ 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index c1135c133c..b1f9bb4ffe 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -164,7 +164,7 @@ void LivenessTracker::cleanup_table(bool forced, bool allow_resolve, u64 target_gc_epoch = load(_gc_epoch); TEST_LOG_SUMMARY("LivenessTracker::cleanup_table forced=%d leak_signal=%d gc_generations=%d current_epoch=%llu " "target_epoch=%llu table_size=%d", - forced, _leak_signal_enabled.load(std::memory_order_relaxed), + forced, _leak_signal_enabled.load(std::memory_order_acquire), _gc_generations.load(std::memory_order_relaxed), (unsigned long long)current, (unsigned long long)target_gc_epoch, _table_size); @@ -276,7 +276,7 @@ void LivenessTracker::cleanup_table(bool forced, bool allow_resolve, } } - if (_leak_signal_enabled.load(std::memory_order_relaxed) && is_epoch_owner) { + if (_leak_signal_enabled.load(std::memory_order_acquire) && is_epoch_owner) { // Per-klass population tracking (design doc's Open Question 3) - // gated on the effective leak-signal availability: generations // tracking and liveheap tracking drive the same mechanisms, so the @@ -362,7 +362,7 @@ void LivenessTracker::cleanup_table(bool forced, bool allow_resolve, 1.0f * (end - start) / 1000 / 1000, 1.0f * (end - start) / 1000 / sz); } - if (_leak_signal_enabled.load(std::memory_order_relaxed) && is_epoch_owner) { + if (_leak_signal_enabled.load(std::memory_order_acquire) && is_epoch_owner) { // Outside the `sz > 0` block: a sweep that finds the table already empty // still owns this epoch. foldKlassCountsLocked() records zero population // samples for klasses whose every tracked instance died (they never @@ -1639,9 +1639,9 @@ double LivenessTracker::secondsToOOM() const { jlong max_heap = _max_heap_bytes; jlong container_limit = _container_memory_limit; #endif - if (!_leak_signal_enabled.load(std::memory_order_relaxed) || max_heap <= 0) { + if (!_leak_signal_enabled.load(std::memory_order_acquire) || max_heap <= 0) { TEST_LOG("LivenessTracker::secondsToOOM -> -1 (leak_signal=%d max_heap=%lld)", - (int)_leak_signal_enabled.load(std::memory_order_relaxed), (long long)max_heap); + (int)_leak_signal_enabled.load(std::memory_order_acquire), (long long)max_heap); return -1; } // Age-based burn-in (see OOM_PROJECTION_BURN_IN_NS): no projection until the @@ -2100,7 +2100,7 @@ Error LivenessTracker::initialize_table(JNIEnv *jni, int sampling_interval) { void LivenessTracker::resetSession() { _enabled = false; _gc_generations.store(false, std::memory_order_relaxed); - _leak_signal_enabled.store(false, std::memory_order_relaxed); + _leak_signal_enabled.store(false, std::memory_order_release); } Error LivenessTracker::start(Arguments &args) { @@ -2182,10 +2182,13 @@ Error LivenessTracker::initialize(Arguments &args) { // observe the flag it was actually started with, even though the tracking // table itself persists across recordings. _gc_generations.store(args._gc_generations, std::memory_order_relaxed); - // See _leak_signal_enabled's comment for the derivation. + // See _leak_signal_enabled's comment for the derivation. Release: the + // flag gates klass-folding/heap-floor/forced-sweep work whose readers + // consume table state populated under the same flag (AGENTS.md's default + // for config fields written in start()); acquire on every load. _leak_signal_enabled.store(args._gc_generations || (args._record_liveness && args._reference_chains), - std::memory_order_relaxed); + std::memory_order_release); if (!_enabled) { return Error::OK; @@ -2213,7 +2216,7 @@ Error LivenessTracker::initialize(Arguments &args) { // later starts never re-stamp it. Stays ahead of the _initialized early // return so the first signal-on start after earlier non-signal starts can // still stamp. - if (_leak_signal_enabled.load(std::memory_order_relaxed)) { + if (_leak_signal_enabled.load(std::memory_order_acquire)) { u64 never_activated = 0; _leak_signal_activated_ns.compare_exchange_strong( never_activated, OS::nanotime(), std::memory_order_release, std::memory_order_relaxed); @@ -2577,7 +2580,7 @@ void LivenessTracker::track(JNIEnv *env, AllocEvent &event, jint tid, } void LivenessTracker::maybeForceCleanup(u64 now_ns) { - if (!_enabled || !_leak_signal_enabled.load(std::memory_order_relaxed)) { + if (!_enabled || !_leak_signal_enabled.load(std::memory_order_acquire)) { return; } constexpr u64 FORCE_CLEANUP_INTERVAL_NS = 30ULL * 1000 * 1000 * 1000; @@ -2633,7 +2636,7 @@ void LivenessTracker::onGC() { store(_used_after_last_gc, HeapUsage::get(false)._used); } - if (_leak_signal_enabled.load(std::memory_order_relaxed)) { + if (_leak_signal_enabled.load(std::memory_order_acquire)) { // Feeds heapFloorRising()'s corroboration check (selectLeakCandidates()) // - gated on the effective leak-signal availability, same as the // per-klass population table itself, since this ring exists purely to diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index 4232d4c2af..18af221505 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -484,16 +484,22 @@ class alignas(alignof(SpinLock)) LivenessTracker { // True when a leak-signal source is active: generations, or liveheap with // referencechains enabled - the collector is the signal's only consumer, so // a plain liveheap session keeps the flag (and its per-epoch klass folding, - // heap-floor ring and forced sweeps) off. Derived in initialize(); same - // atomicity/reader argument as _gc_generations above. + // heap-floor ring and forced sweeps) off. Derived in initialize(); written + // with release, read with acquire (the readers consume table state + // populated under the same flag). Cleared again by + // disableChainOnlyLeakSignal() when Profiler::start() armed it for the + // (liveness && reference_chains) leg but ReferenceChainTracker::start() + // then failed - without the tracker the flag's work has no consumer. std::atomic _leak_signal_enabled; // OS::nanotime() of the first initialize() that enabled the leak signal; 0 - // means never activated. Set once by a CAS from 0 and never reset by - // production code (not by stop()/resetSession(), not by the ring resets), so - // it is per process, not per recording. Drives secondsToOOM()'s age-based - // burn-in (see OOM_PROJECTION_BURN_IN_NS). Written with release, read with - // acquire. + // means never activated. Set once by a CAS from 0; the only production + // reset is disableChainOnlyLeakSignal()'s chains-start-failure path (which + // also clears the flag), so a later chains-enabled start re-stamps and + // gets its own burn-in. stop()/resetSession() and the ring resets never + // touch it: per process, not per recording. Drives secondsToOOM()'s + // age-based burn-in (see OOM_PROJECTION_BURN_IN_NS). Written with release, + // read with acquire. std::atomic _leak_signal_activated_ns; // Per-klass population history table (see KlassPopulationEntry above). @@ -1082,7 +1088,24 @@ class alignas(alignof(SpinLock)) LivenessTracker { // The effective gate ReferenceChainTracker::hasLeakSignal() keys on (see // _leak_signal_enabled's own comment). bool leakSignalEnabled() const { - return _leak_signal_enabled.load(std::memory_order_relaxed); + return _leak_signal_enabled.load(std::memory_order_acquire); + } + + // Profiler::start() calls this when ReferenceChainTracker::start() failed: + // the flag's (liveness && reference_chains) leg then has no consumer, so a + // plain-liveheap session must not pay klass folding, heap-floor sampling + // and forced sweeps. The generations leg keeps its own consumer - cleared + // only when generations tracking is off. Also unstamps the burn-in clock + // if this start stamped it: a chains-requested-but-failed start must not + // leave a later chains-enabled recording with an already-expired burn-in + // (same rationale as the plain-liveheap rule at the stamping site). Safe: + // with the tracker failed to start no sampler work reads the flag/stamp. + void disableChainOnlyLeakSignal() { + if (_gc_generations.load(std::memory_order_acquire)) { + return; + } + _leak_signal_enabled.store(false, std::memory_order_release); + _leak_signal_activated_ns.store(0, std::memory_order_release); } // Time-to-OOM projection against whichever of two independent boundaries @@ -1741,7 +1764,7 @@ class alignas(alignof(SpinLock)) LivenessTracker { // - the only production-reachable state where liveness alone arms the signal. void setLeakSignalSourcesForTest(bool generations, bool liveness) { _gc_generations.store(generations, std::memory_order_relaxed); - _leak_signal_enabled.store(generations || liveness, std::memory_order_relaxed); + _leak_signal_enabled.store(generations || liveness, std::memory_order_release); } // Convenience for callers that only need the generations-only configuration: diff --git a/ddprof-lib/src/main/cpp/profiler.cpp b/ddprof-lib/src/main/cpp/profiler.cpp index 94f8df8d0d..c0e97a98a9 100644 --- a/ddprof-lib/src/main/cpp/profiler.cpp +++ b/ddprof-lib/src/main/cpp/profiler.cpp @@ -1724,6 +1724,10 @@ Error Profiler::start(Arguments &args, bool reset) { error = ReferenceChainTracker::instance()->start(args); if (error) { Log::warn("%s", error.message()); + // The leak-signal flag was armed for the (liveness && reference_chains) leg before this + // start; with the tracker failed, that leg's work has no consumer - clear it again (and + // the burn-in stamp it stamped) so a plain-liveheap session doesn't pay for it. + LivenessTracker::instance()->disableChainOnlyLeakSignal(); error = Error::OK; // recoverable - recording continues without chains } else { ReferenceChainTracker::instance()->startThread(); From 7ff8a3feaa94ba617f55c94844e94e06b990423a Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 15:06:52 +0100 Subject: [PATCH 30/37] javaApi: clamp the leak-signal backdate stamp On a freshly booted CI VM the monotonic clock can sit under the burn-in horizon; the modular u64 underflow read back as a negative jlong that setLeakSignalActivatedNsForTest0() rejects, throwing out of a test's finally block. Clamp to 1 when the clock is too young. --- ddprof-lib/src/main/cpp/javaApi.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ddprof-lib/src/main/cpp/javaApi.cpp b/ddprof-lib/src/main/cpp/javaApi.cpp index f506215f41..d95a38f10a 100644 --- a/ddprof-lib/src/main/cpp/javaApi.cpp +++ b/ddprof-lib/src/main/cpp/javaApi.cpp @@ -1414,8 +1414,15 @@ Java_com_datadoghq_profiler_JavaProfiler_backdateLeakSignalActivationForTest0( LivenessTracker *lt = LivenessTracker::instance(); u64 previous = lt->leakSignalActivatedNsForTest(); // One second past the burn-in (modular u64), mirroring the gtest fixtures' backdated stamp. - lt->setLeakSignalActivatedNsForTest(OS::nanotime() - LivenessTracker::OOM_PROJECTION_BURN_IN_NS - - 1000000000ULL); + // Clamped: on a freshly booted CI VM the monotonic clock can be under the burn-in horizon, + // and the modular u64 underflow would read back as a negative jlong that + // setLeakSignalActivatedNsForTest0() rejects - a throwing finally block with the stamp + // left backdated. Stamp 1 is a valid "just activated" substitute there. + u64 now = OS::nanotime(); + u64 stamp = now > (u64)LivenessTracker::OOM_PROJECTION_BURN_IN_NS + 1000000000ULL + ? now - LivenessTracker::OOM_PROJECTION_BURN_IN_NS - 1000000000ULL + : 1; + lt->setLeakSignalActivatedNsForTest(stamp); return (jlong)previous; } From 43c165ee481629f96b52ca2a5b2f732e861e1b05 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 15:06:52 +0100 Subject: [PATCH 31/37] counters, tests: trim counter comments to the review budget; dedupe the ring-size test accessor --- ddprof-lib/src/main/cpp/counters.h | 19 ++++++++----------- .../src/test/cpp/livenessTracker_ut.cpp | 9 ++------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/ddprof-lib/src/main/cpp/counters.h b/ddprof-lib/src/main/cpp/counters.h index 38285c0ca3..a697d72b2e 100644 --- a/ddprof-lib/src/main/cpp/counters.h +++ b/ddprof-lib/src/main/cpp/counters.h @@ -298,19 +298,16 @@ * the ramped cadence. */ \ X(REFERENCE_CHAIN_URGENT_NO_CANDIDATE, "reference_chain_urgent_no_candidate") \ /* Heap-floor seqlock: samples DROPPED by a writer that lost the per-slot \ - * claim (head slot already at HEAP_FLOOR_SEQ_UNSTABLE, or the claim CAS \ - * lost to an overlapping writer). One increment per dropped sample, in \ - * GC-callback context - no logging there, so this counter is the only \ - * observability for overlap-driven sample loss. Never fires while GC \ - * callbacks are serialized (HotSpot). */ \ + * claim (CAS lost, or head already unstable). One increment per dropped \ + * sample, in GC-callback context - no logging there, so this is the only \ + * observability for overlap-driven loss. Never fires while GC callbacks \ + * are serialized (HotSpot). */ \ X(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP, "heap_floor_sample_dropped_overlap") \ /* Heap-floor seqlock: reads that exhausted HEAP_FLOOR_READ_ATTEMPTS \ - * without a consistent (used, seq) pair ("unstable read", not strictly \ - * "frozen admission evaluation" - it also counts the emergency-band \ - * baseline seed's seq_out==nullptr read). One increment per exhausted \ - * read, never per attempt; the fill==0 empty-ring return does not count. \ - * A steadily rising value means the urgent admission gate is holding its \ - * previous decision because overlapping writers pin the newest slot. */ \ + * without a consistent (used, seq) pair (also counts the emergency-band \ + * seed's seq_out==nullptr read; one increment per exhausted read, never \ + * per attempt). A rising value means overlapping writers pin the newest \ + * slot and the urgent admission gate holds its previous decision. */ \ X(HEAP_FLOOR_READ_UNSTABLE, "heap_floor_read_unstable") \ /* admitStaticFieldRoots() per-class non-static quota: non-STATIC_FIELD \ * edges (CONSTANT_POOL, INTERFACE, SUPERCLASS, CLASS_LOADER, ...) that \ diff --git a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp index f622217b26..25a4ca426c 100644 --- a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp +++ b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp @@ -1253,11 +1253,6 @@ class LivenessTrackerTestAccessor { return true; } - // The heap-floor ring size, for tests that wrap it a full lap. - static constexpr int heapFloorRingSizeForTest() { - return LivenessTracker::KLASS_POPULATION_RING_SIZE; - } - // Swaps the head slot's seqlock word for `value` (e.g. the busy // sentinel) and returns the previous value so the caller can restore it. static u64 swapHeapFloorHeadSeqForTest(LivenessTracker *t, u64 value) { @@ -1826,7 +1821,7 @@ struct HeapFloorWrapHook { if (h->fire_once) { LivenessTracker::setHeapFloorReadHookForTest(nullptr, nullptr); } - for (int i = 0; i < LivenessTrackerTestAccessor::heapFloorRingSizeForTest(); + for (int i = 0; i < LivenessTracker::heapFloorRingSizeForTest(); i++) { h->lt->heapFloorRecordForTest(2000 + (u64)i, 0); } @@ -1871,7 +1866,7 @@ TEST_F(HeapFloorSeqlockTest, NewestSampleCoherentAcrossRingWrap) { &hook); u64 seq = 0; u64 used = lt->newestHeapFloorSample(&seq); - constexpr int ring = LivenessTrackerTestAccessor::heapFloorRingSizeForTest(); + constexpr int ring = LivenessTracker::heapFloorRingSizeForTest(); // The interleaving really happened: the writer advanced a full ring (30 // samples) past the read slot while the reader was between its two loads. EXPECT_EQ(1, hook.calls) << "the read hook never fired - no interleaving"; From 16c04d5bee3634b537c62f5b804391bf504704ee Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 15:23:49 +0100 Subject: [PATCH 32/37] comments, docs: condense to the review comment budget Keep the constraint lines (ordering, safety, boundary rationale); drop the narration around them. Doc: state the latch's per-iteration refresh and the leak-signal gate's read of it. --- ddprof-lib/src/main/cpp/counters.h | 15 ++- ddprof-lib/src/main/cpp/javaApi.cpp | 7 +- ddprof-lib/src/main/cpp/livenessTracker.cpp | 5 +- ddprof-lib/src/main/cpp/livenessTracker.h | 42 +++----- .../src/main/cpp/referenceChainTraversal.cpp | 5 +- ddprof-lib/src/main/cpp/referenceChains.cpp | 101 +++++++----------- ddprof-lib/src/main/cpp/referenceChains.h | 9 +- .../ReferenceChainsSignalsExplained.md | 2 +- 8 files changed, 70 insertions(+), 116 deletions(-) diff --git a/ddprof-lib/src/main/cpp/counters.h b/ddprof-lib/src/main/cpp/counters.h index a697d72b2e..e13a785ccb 100644 --- a/ddprof-lib/src/main/cpp/counters.h +++ b/ddprof-lib/src/main/cpp/counters.h @@ -298,16 +298,13 @@ * the ramped cadence. */ \ X(REFERENCE_CHAIN_URGENT_NO_CANDIDATE, "reference_chain_urgent_no_candidate") \ /* Heap-floor seqlock: samples DROPPED by a writer that lost the per-slot \ - * claim (CAS lost, or head already unstable). One increment per dropped \ - * sample, in GC-callback context - no logging there, so this is the only \ - * observability for overlap-driven loss. Never fires while GC callbacks \ - * are serialized (HotSpot). */ \ + * claim (CAS lost, or head already unstable) - the only observability for \ + * overlap-driven loss, since GC callbacks cannot log. Never fires while GC \ + * callbacks are serialized (HotSpot). */ \ X(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP, "heap_floor_sample_dropped_overlap") \ - /* Heap-floor seqlock: reads that exhausted HEAP_FLOOR_READ_ATTEMPTS \ - * without a consistent (used, seq) pair (also counts the emergency-band \ - * seed's seq_out==nullptr read; one increment per exhausted read, never \ - * per attempt). A rising value means overlapping writers pin the newest \ - * slot and the urgent admission gate holds its previous decision. */ \ + /* Heap-floor seqlock: reads that exhausted HEAP_FLOOR_READ_ATTEMPTS without a consistent \ + * (used, seq) pair (also the emergency-band seed's seq_out==nullptr read; per exhausted read, \ + * never per attempt). Rising: overlapping writers pin the newest slot. */ \ X(HEAP_FLOOR_READ_UNSTABLE, "heap_floor_read_unstable") \ /* admitStaticFieldRoots() per-class non-static quota: non-STATIC_FIELD \ * edges (CONSTANT_POOL, INTERFACE, SUPERCLASS, CLASS_LOADER, ...) that \ diff --git a/ddprof-lib/src/main/cpp/javaApi.cpp b/ddprof-lib/src/main/cpp/javaApi.cpp index d95a38f10a..f8e61f062a 100644 --- a/ddprof-lib/src/main/cpp/javaApi.cpp +++ b/ddprof-lib/src/main/cpp/javaApi.cpp @@ -1414,10 +1414,9 @@ Java_com_datadoghq_profiler_JavaProfiler_backdateLeakSignalActivationForTest0( LivenessTracker *lt = LivenessTracker::instance(); u64 previous = lt->leakSignalActivatedNsForTest(); // One second past the burn-in (modular u64), mirroring the gtest fixtures' backdated stamp. - // Clamped: on a freshly booted CI VM the monotonic clock can be under the burn-in horizon, - // and the modular u64 underflow would read back as a negative jlong that - // setLeakSignalActivatedNsForTest0() rejects - a throwing finally block with the stamp - // left backdated. Stamp 1 is a valid "just activated" substitute there. + // Clamped to 1: on a freshly booted CI VM the clock can be under the burn-in horizon, and the + // u64 underflow would read back as a negative jlong that setLeakSignalActivatedNsForTest0() + // rejects, throwing out of a test's finally block. u64 now = OS::nanotime(); u64 stamp = now > (u64)LivenessTracker::OOM_PROJECTION_BURN_IN_NS + 1000000000ULL ? now - LivenessTracker::OOM_PROJECTION_BURN_IN_NS - 1000000000ULL diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index b1f9bb4ffe..c01ee47d77 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -2182,10 +2182,7 @@ Error LivenessTracker::initialize(Arguments &args) { // observe the flag it was actually started with, even though the tracking // table itself persists across recordings. _gc_generations.store(args._gc_generations, std::memory_order_relaxed); - // See _leak_signal_enabled's comment for the derivation. Release: the - // flag gates klass-folding/heap-floor/forced-sweep work whose readers - // consume table state populated under the same flag (AGENTS.md's default - // for config fields written in start()); acquire on every load. + // See _leak_signal_enabled's comment for the derivation and the ordering rationale. _leak_signal_enabled.store(args._gc_generations || (args._record_liveness && args._reference_chains), std::memory_order_release); diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index 18af221505..8d738952b6 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -481,25 +481,19 @@ class alignas(alignof(SpinLock)) LivenessTracker { // still be reading it from a session that persists across a restart. std::atomic _gc_generations; - // True when a leak-signal source is active: generations, or liveheap with - // referencechains enabled - the collector is the signal's only consumer, so - // a plain liveheap session keeps the flag (and its per-epoch klass folding, - // heap-floor ring and forced sweeps) off. Derived in initialize(); written - // with release, read with acquire (the readers consume table state - // populated under the same flag). Cleared again by - // disableChainOnlyLeakSignal() when Profiler::start() armed it for the - // (liveness && reference_chains) leg but ReferenceChainTracker::start() - // then failed - without the tracker the flag's work has no consumer. + // True when a leak-signal source is active: generations, or liveheap with referencechains + // enabled - the collector is the signal's only consumer, so a plain liveheap session keeps the + // flag (and its klass folding, heap-floor ring and forced sweeps) off. Derived in initialize(); + // written release, read acquire. Cleared again by disableChainOnlyLeakSignal() when the + // (liveness && reference_chains) leg was armed but ReferenceChainTracker::start() failed. std::atomic _leak_signal_enabled; - // OS::nanotime() of the first initialize() that enabled the leak signal; 0 - // means never activated. Set once by a CAS from 0; the only production - // reset is disableChainOnlyLeakSignal()'s chains-start-failure path (which - // also clears the flag), so a later chains-enabled start re-stamps and - // gets its own burn-in. stop()/resetSession() and the ring resets never - // touch it: per process, not per recording. Drives secondsToOOM()'s - // age-based burn-in (see OOM_PROJECTION_BURN_IN_NS). Written with release, - // read with acquire. + // OS::nanotime() of the first initialize() that enabled the leak signal; 0 means never + // activated. Set once by a CAS from 0; the only production reset is disableChainOnlyLeakSignal()'s + // chains-start-failure path (with the flag), so a later chains-enabled start re-stamps and gets + // its own burn-in. stop()/resetSession() and the ring resets never touch it: per process, not per + // recording. Drives secondsToOOM()'s age-based burn-in (see OOM_PROJECTION_BURN_IN_NS). Release + // write, acquire read. std::atomic _leak_signal_activated_ns; // Per-klass population history table (see KlassPopulationEntry above). @@ -1091,15 +1085,11 @@ class alignas(alignof(SpinLock)) LivenessTracker { return _leak_signal_enabled.load(std::memory_order_acquire); } - // Profiler::start() calls this when ReferenceChainTracker::start() failed: - // the flag's (liveness && reference_chains) leg then has no consumer, so a - // plain-liveheap session must not pay klass folding, heap-floor sampling - // and forced sweeps. The generations leg keeps its own consumer - cleared - // only when generations tracking is off. Also unstamps the burn-in clock - // if this start stamped it: a chains-requested-but-failed start must not - // leave a later chains-enabled recording with an already-expired burn-in - // (same rationale as the plain-liveheap rule at the stamping site). Safe: - // with the tracker failed to start no sampler work reads the flag/stamp. + // Profiler::start() calls this when ReferenceChainTracker::start() failed: the flag's + // (liveness && reference_chains) leg then has no consumer, so a plain-liveheap session must not + // pay for it; generations keeps its own consumer. Also unstamps the burn-in clock so a later + // chains-enabled start re-stamps and gets its own burn-in. Safe: with the tracker failed, no + // sampler work reads the flag or the stamp. void disableChainOnlyLeakSignal() { if (_gc_generations.load(std::memory_order_acquire)) { return; diff --git a/ddprof-lib/src/main/cpp/referenceChainTraversal.cpp b/ddprof-lib/src/main/cpp/referenceChainTraversal.cpp index 57f3179a0d..1e3440a7a8 100644 --- a/ddprof-lib/src/main/cpp/referenceChainTraversal.cpp +++ b/ddprof-lib/src/main/cpp/referenceChainTraversal.cpp @@ -1130,9 +1130,8 @@ bool ReferenceChainTracker::runPass(jvmtiEnv *jvmti, JNIEnv *jni, } else if (_passes_since_last_progress >= NO_PROGRESS_PASS_LIMIT && !urgencyLatched()) { // The frontier hasn't grown for NO_PROGRESS_PASS_LIMIT consecutive passes — the search is - // genuinely stuck (not just slow), so abandon. Reads the latch published by - // publishUrgencyState()'s per-iteration isUrgent() call; calling isUrgent() here would - // advance the machine from a second, freshly-read projection. + // genuinely stuck (not just slow), so abandon. Reads the published latch; calling isUrgent() + // here would advance the machine from a second, freshly-read projection. store(_abandon_reason, (u8)SearchAbandonReason::TTL); storeRelease(_search_state, (u8)SearchState::ABANDONED); enqueuePendingAbandonedEvent(); diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index ea27584bd7..be39ad3037 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -382,27 +382,15 @@ void ReferenceChainTracker::stop() { Log::info("Reference chain tracking stopped"); // Recording-boundary search teardown: stopThread() has already joined the BFS thread, so no - // pass can be in flight here, but a search may still be RUNNING. Ending it now - rather than - // letting the next recording inherit it - because nothing about it survives the boundary - // intact: - // - _search_start_ns still holds the previous recording's clock, so the entire stopped gap - // would count toward the new recording's TTL and could abandon "its" search on the first - // pass with a spurious abandonment event for a search the new recording never started; - // - _urgent_latched/_urgent_release_ticks/_admission_growth_baseline (and its - // _admission_sample_seq/_admission_decision freshness state) would carry the previous - // recording's urgency episode into the new one (a stale growth baseline would let the new - // recording's first urgent observation skip updateUrgentAdmission()'s entry re-snapshot and - // admit against the old recording's heap level). The freshness pair needs its own reset: - // an emergency-band first observation in the new recording seeds the baseline without - // touching the pair, and a leaked pair would then make the next ramp-band call with no new - // GC sample return the old recording's retained admission; - // - an urgency episode still open (applyOomRamp()) would carry its boosted _budget ceiling - // (_urgency_budget_boosted) and its ramped _effective_cadence_ns past the boundary - closed - // here with the same restore as applyOomRamp()'s own episode-end branch; - // - the frontier's referrer_klass slots hold StringDictionary ids from a _class_map that - // Profiler::start() is about to wipe (ids restart at 1) - they cannot be remapped, so a - // chain built from a surviving entry after the boundary would name whichever class the new - // recording assigned that id to. + // pass can be in flight here, but a search may still be RUNNING. Nothing about it survives the + // boundary intact - the stale _search_start_ns clock would count the stopped gap toward the new + // recording's TTL, the urgency/admission state (resetUrgencyState() below, including the + // freshness pair: an emergency-band entry seeds the baseline without touching it, so a leaked + // pair would return the old recording's retained admission) and an open episode's boosted + // _budget/ramped cadence (closeUrgencyEpisode() below) would carry into the new recording, and + // the frontier's referrer_klass slots hold StringDictionary ids from a _class_map that + // Profiler::start() is about to wipe (ids restart at 1) - unremappable, so a surviving entry + // would name whichever class the new recording assigned that id to. // Forcing the terminal state here routes the next recording through the existing // release-then-restart sequence: its first BFS pass hits shouldRunPass()'s terminal gate // (tags are deliberately NOT released in stop() - the next start()'s search does it via that @@ -422,13 +410,9 @@ void ReferenceChainTracker::stop() { // from this thread only because stopThread() has joined that thread. store(_search_start_ns, (u64)0); resetUrgencyState(); - // Close the urgency episode the BFS thread may have left open: the boosted - // budget ceiling and the ramped cadence are episode state - the same state - // applyOomRamp()'s first non-urgent iteration restores on the BFS thread - - // and a mid-episode stop() would otherwise leak them past the recording - // boundary (buildAbandonedEvent() reads _budget after this point). - // stopThread() has already joined the BFS thread, so these writes are as - // safe as the urgency resets above. + // A mid-episode stop() must not leak the boosted budget/ramped cadence past the recording + // boundary (buildAbandonedEvent() reads _budget after this point); stopThread() has already + // joined the BFS thread, so these writes are as safe as the urgency resets above. closeUrgencyEpisode(); // Same per-search candidate/discovered canary state runPass()'s terminal branch and // restartSearch() clear - zeroing the counts here closes the stale-chain windows in @@ -529,10 +513,8 @@ bool ReferenceChainTracker::applyOomRamp(double seconds_to_oom) { _pause_pid = PidController((u64)std::max(_effective_pause_target_ms, 0L), 10, 1, 2, 1, 5.0); } - // Once in the ramp window, hold the budget ceiling raised for the urgency episode's entire - // duration rather than only right before OOM: the process is likely to die anyway, so it's - // worth spending whatever budget it takes to collect good diagnostic data for as long as we - // have. + // Hold the budget ceiling raised for the episode's entire duration, not only right before OOM: + // the process is likely to die anyway, so spend what it takes to collect good diagnostics. if (urgent && !_urgency_budget_boosted) { _urgency_budget_boosted = true; _budget = std::min(_budget * 4, MAX_REFERENCE_CHAINS_BUDGET); @@ -798,9 +780,8 @@ bool ReferenceChainTracker::shouldRunPass(u64 now_ns, double seconds_to_oom) { // reusing pollWatchedTargets()'s own selectLeakCandidates() call - that one runs after runPass() in // threadLoop()'s own iteration and needs the *list* to poll each candidate's tag; this only needs // to know whether at least one exists. -// Test-driving form of the latch machine: evaluates the live projection -// (what the zero-arg form used to read) so tests advance the same machine -// publishUrgencyState() runs per iteration. +// Test-driving form of the latch machine: evaluates the live projection (what the zero-arg form +// used to read) so tests advance the same machine publishUrgencyState() runs per iteration. bool ReferenceChainTracker::isUrgentForTest() const { return isUrgent(LivenessTracker::instance()->secondsToOOM()); } @@ -921,12 +902,11 @@ bool ReferenceChainTracker::updateUrgentAdmission(double seconds_to_oom, bool ur return _admission_decision; } -// The urgency/admission reset shared by stop(), resetSearchStateForTest() and -// resetUrgencyForTest(): the latch/release machine, the episode baseline, and -// the freshness pair - the pair needs its own reset because an emergency-band -// re-entry seeds the baseline without touching it, so a leaked pair would -// make the next ramp-band call with no new GC sample return the previous -// recording's (or test's) retained admission decision. +// The urgency/admission reset shared by stop(), resetSearchStateForTest() and resetUrgencyForTest(): +// latch/release machine, episode baseline, and the freshness pair - the pair needs its own reset +// because an emergency-band re-entry seeds the baseline without touching it, so a leaked pair would +// make the next ramp-band call with no new GC sample return the previous recording's retained +// admission decision. void ReferenceChainTracker::resetUrgencyState() { _urgent_latched = false; _urgent_release_ticks = 0; @@ -935,11 +915,9 @@ void ReferenceChainTracker::resetUrgencyState() { _admission_decision = false; } -// Closes an urgency episode: restores the boosted budget ceiling and the -// ramped cadence. Guarded on the boost flag because outside an episode -// updatePacing() owns _effective_cadence_ns; applyOomRamp() and stop() (the -// latter only because stopThread() has already joined the BFS thread) are the -// production callers. +// Closes an urgency episode: restores the boosted budget ceiling and the ramped cadence. Guarded on +// the boost flag because outside an episode updatePacing() owns _effective_cadence_ns. Production +// callers: applyOomRamp(), and stop() (safe only because stopThread() has joined the BFS thread). void ReferenceChainTracker::closeUrgencyEpisode() { if (_urgency_budget_boosted) { _urgency_budget_boosted = false; @@ -950,12 +928,10 @@ void ReferenceChainTracker::closeUrgencyEpisode() { void ReferenceChainTracker::publishUrgencyState(double seconds_to_oom, bool urgent) { _oom_ramp_active = urgent; - // The latch machine runs here, once per iteration on threadLoop()'s single - // projection read. It must not run inside hasLeakSignal(): that gate sits - // behind canAffordNewSearch()'s cooldown check, and while the cooldown - // blocks restarts the latch would stay frozen - possibly still latched - // after the heap recovered, suppressing runPass()'s no-progress - // abandonment for a search the stale latch then starts. + // The latch machine runs here - once per iteration on threadLoop()'s single projection. It must + // not run in hasLeakSignal(): that gate sits behind canAffordNewSearch()'s cooldown, and the + // latch would stay frozen (possibly stale-latched after the heap recovered) for the cooldown's + // duration, suppressing runPass()'s no-progress abandonment on a search the stale latch starts. isUrgent(seconds_to_oom); LivenessTracker::instance()->setUrgentTracking(updateUrgentAdmission(seconds_to_oom, urgent)); } @@ -971,14 +947,11 @@ bool ReferenceChainTracker::hasLeakSignal(double seconds_to_oom) { // diagnostic value. return false; } - // Reads the latch published by this iteration's publishUrgencyState() call - // (which runs isUrgent() unconditionally, on threadLoop()'s single - // projection read) - never a fresh secondsToOOM(), and never an update: - // this gate sits behind canAffordNewSearch()'s cooldown, so advancing the - // latch here would freeze it for the cooldown's duration. The OOM ramp's - // budget/cadence effects follow the raw band comparison and the 100% - // tracking admission follows updateUrgentAdmission()'s growth gate; - // neither waits for this latch. + // Reads the latch published by this iteration's publishUrgencyState() call - never a fresh + // secondsToOOM(), and never an update: this gate sits behind canAffordNewSearch()'s cooldown, + // so advancing the latch here would freeze it for the cooldown's duration. The OOM ramp's + // budget/cadence effects follow the raw band comparison and the 100% tracking admission follows + // updateUrgentAdmission()'s growth gate; neither waits for this latch. bool urgent = _urgent_latched; KlassCandidate probe[1]; int n = LivenessTracker::instance()->selectLeakCandidates(probe, 1); @@ -1117,11 +1090,9 @@ void ReferenceChainTracker::resetSearchStateForTest(jvmtiEnv *jvmti, // Mirror the reset for the non-safepoint budget - same test-isolation rationale as // _safepoint_pain_budget above. _cpu_pain_budget = PainBudget(_pain_budget_refill_rate); - // Same test-isolation rationale: a latched urgency episode left behind by an earlier test would - // otherwise ramp this one's cadence and pause target from a stale projection. resetUrgencyState() - // covers the latch/baseline/freshness pair (the pair rationale matches stop()); - // closeUrgencyEpisode() also restores a boosted _budget/_effective_cadence_ns, which the reset - // previously missed - a prior test's mid-episode stop leaked the boost into this one. + // Same test-isolation rationale: a latched episode left behind by an earlier test would ramp this + // one from a stale projection, and a boosted _budget/_effective_cadence_ns the old reset missed + // would leak into it - hence both helpers. resetUrgencyState(); closeUrgencyEpisode(); diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index 701beb1363..adf45bbd04 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -824,10 +824,9 @@ class ReferenceChainTracker { bool hasLeakSignal(double seconds_to_oom); // Latched, hysteretic view of LivenessTracker::secondsToOOM() crossing OOM_URGENT_THRESHOLD_S - - // see _urgent_latched for the latch/release rules and why the raw comparison flaps. The latch - // machine runs once per iteration in publishUrgencyState(), on the caller's projection - // (threadLoop()'s single read); runPass() and hasLeakSignal() consume the latched state via - // urgencyLatched() without advancing the machine. + // see _urgent_latched for the latch/release rules and why the raw comparison flaps. The machine + // runs once per iteration in publishUrgencyState() on the caller's projection; runPass() and + // hasLeakSignal() consume it read-only via urgencyLatched(). bool isUrgent(double seconds_to_oom) const; // Read-only view of the latch (see isUrgent()). bool urgencyLatched() const { return _urgent_latched; } @@ -845,6 +844,8 @@ class ReferenceChainTracker { // Publishes one threadLoop() iteration's urgency: _oom_ramp_active follows the raw band // (`urgent`) for shouldRunPass()'s canary-backoff bypass; LivenessTracker's 100% tracking boost + // Shared urgency/admission reset (stop()/resetSearchStateForTest()/resetUrgencyForTest()) and + // episode close - see the definitions in referenceChains.cpp. void resetUrgencyState(); void closeUrgencyEpisode(); diff --git a/doc/architecture/ReferenceChainsSignalsExplained.md b/doc/architecture/ReferenceChainsSignalsExplained.md index 22dbcfaabb..6f7f6ec9ec 100644 --- a/doc/architecture/ReferenceChainsSignalsExplained.md +++ b/doc/architecture/ReferenceChainsSignalsExplained.md @@ -119,7 +119,7 @@ neither source provides a valid estimate, or that the projection is still in its Two ranges have different purposes: - At less than 30 minutes, the OOM ramp increases the pause target, shortens the cadence toward its 10 millisecond floor, and increases the edge-budget ceiling. The 100% liveness-sampling admission follows the same band, but outside the emergency band it additionally requires fresh growth evidence: the newest heap-floor sample must exceed the episode's running high-water mark (the entry value, raised by every admitted sample). The growth check is evaluated once per new heap-floor sample (one per GC), and its result is held until the next sample arrives, so an admitted growth sample keeps the boost on until the next GC. The projection is a smoothed regression over a 30-sample window, so a single spike sample keeps the projection inside the band for several recovery GCs after the heap has physically stopped growing. Growth evidence is what keeps the admission boost off for such a recovered spike. Inside the emergency band (less than 5 minutes), admission is immediate. -- At less than 5 minutes, `isUrgent()` latches an urgent episode. The latch does not start searches, and the ramp calculation does not consult it. Its only consumer is the no-progress abandonment of a running search, which it suppresses while the latch is held. +- At less than 5 minutes, `isUrgent()` latches an urgent episode. The latch does not start searches, and the ramp calculation does not consult it. It is refreshed once per iteration from the same OOM projection the ramp uses, so it tracks heap recovery even while the search cooldown gates restarts. Its only behavioral consumer is the no-progress abandonment of a running search, which it suppresses while the latch is held; the leak-signal gate also reads it to refuse a candidate-less search. Both ranges apply only after the burn-in period. From 1f774f80674b0746c74e3c47836b6f05285fd5ca Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 16:23:59 +0100 Subject: [PATCH 33/37] referenceChains: clear the chain-only leak signal on every non-active path ReferenceChainTracker::start() is skipped entirely when the alloc engine fails recoverably, so the flag-stayed-armed hole was wider than the tracker-failure branch: gate the whole block on args._reference_chains and clear the flag whenever chains did not activate. Also document hasLeakSignal()'s logging-only use of seconds_to_oom, the zeroed-stamp transient's dependence on secondsToOOM()'s activated_ns == 0 guard, and cover disableChainOnlyLeakSignal() with accessor tests (generations leg preserved, chain-only leg cleared and unstamped). --- ddprof-lib/src/main/cpp/livenessTracker.h | 13 +++- ddprof-lib/src/main/cpp/profiler.cpp | 31 +++++---- ddprof-lib/src/main/cpp/referenceChains.cpp | 6 ++ ddprof-lib/src/main/cpp/referenceChains.h | 9 ++- .../src/test/cpp/livenessTracker_ut.cpp | 64 +++++++++++++++++++ 5 files changed, 105 insertions(+), 18 deletions(-) diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index 8d738952b6..c1de89daa5 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -1085,15 +1085,22 @@ class alignas(alignof(SpinLock)) LivenessTracker { return _leak_signal_enabled.load(std::memory_order_acquire); } - // Profiler::start() calls this when ReferenceChainTracker::start() failed: the flag's + // Profiler::start() calls this when reference chains were requested but did not activate + // (alloc engine not activated, or ReferenceChainTracker::start() failed): the flag's // (liveness && reference_chains) leg then has no consumer, so a plain-liveheap session must not // pay for it; generations keeps its own consumer. Also unstamps the burn-in clock so a later - // chains-enabled start re-stamps and gets its own burn-in. Safe: with the tracker failed, no - // sampler work reads the flag or the stamp. + // chains-enabled start re-stamps and gets its own burn-in. Concurrent readers are tolerated: + // see the ordering note on the stores below. void disableChainOnlyLeakSignal() { if (_gc_generations.load(std::memory_order_acquire)) { return; } + // Ordering: flag first, stamp second (both release) - a reader that sees the flag already + // false never reaches the stamp read. A reader that acquired the flag as true just before + // this can still observe the zeroed stamp; that transient is safe only because + // secondsToOOM() returns -1 on activated_ns == 0. Keep that guard if secondsToOOM() is + // refactored, or this unstamping can yield a spurious projection and a spurious urgent + // latch. _leak_signal_enabled.store(false, std::memory_order_release); _leak_signal_activated_ns.store(0, std::memory_order_release); } diff --git a/ddprof-lib/src/main/cpp/profiler.cpp b/ddprof-lib/src/main/cpp/profiler.cpp index c0e97a98a9..a838a36354 100644 --- a/ddprof-lib/src/main/cpp/profiler.cpp +++ b/ddprof-lib/src/main/cpp/profiler.cpp @@ -1715,24 +1715,31 @@ Error Profiler::start(Arguments &args, bool reset) { } } } - if ((activated & EM_ALLOC) && args._reference_chains) { + if (args._reference_chains) { // Reference-chain tracking chases LivenessTracker's leak-tagged // candidates, so it only runs when allocation sampling actually // activated. Must run AFTER ObjectSampler::start() -> // LivenessTracker::start() (ordering noted in referenceChains.cpp) - // the block above is that ordering point. - error = ReferenceChainTracker::instance()->start(args); - if (error) { - Log::warn("%s", error.message()); - // The leak-signal flag was armed for the (liveness && reference_chains) leg before this - // start; with the tracker failed, that leg's work has no consumer - clear it again (and - // the burn-in stamp it stamped) so a plain-liveheap session doesn't pay for it. + bool chains_active = false; + if (activated & EM_ALLOC) { + error = ReferenceChainTracker::instance()->start(args); + if (error) { + Log::warn("%s", error.message()); + error = Error::OK; // recoverable - recording continues without chains + } else { + ReferenceChainTracker::instance()->startThread(); + // Pre-existing threads are registered below, once thread events are on. + _reference_chains_active = true; + chains_active = true; + } + } + if (!chains_active) { + // initialize() may have armed the leak signal (and stamped burn-in) for the + // (liveness && reference_chains) leg; with chains not active - alloc engine did not + // activate, or the tracker failed to start - that leg has no consumer. Clear it + // (generations keeps its own leg). LivenessTracker::instance()->disableChainOnlyLeakSignal(); - error = Error::OK; // recoverable - recording continues without chains - } else { - ReferenceChainTracker::instance()->startThread(); - // Pre-existing threads are registered below, once thread events are on. - _reference_chains_active = true; } } if (_event_mask & EM_NATIVEMEM) { diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index be39ad3037..c4fe7705e9 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -936,6 +936,12 @@ void ReferenceChainTracker::publishUrgencyState(double seconds_to_oom, bool urge LivenessTracker::instance()->setUrgentTracking(updateUrgentAdmission(seconds_to_oom, urgent)); } +// Contract: the decision depends only on leakSignalEnabled(), selectLeakCandidates() and the +// latch this iteration's publishUrgencyState() already published (read here, never advanced). +// seconds_to_oom is used only for logging and does not affect the decision: it is never compared +// and never forwarded to isUrgent() or updateUrgentAdmission() - it reaches only the +// TEST_LOG_SUMMARY diagnostic below, staying in the signature so the iteration's projection +// appears in the gate's log next to the latch it produced. bool ReferenceChainTracker::hasLeakSignal(double seconds_to_oom) { if (!LivenessTracker::instance()->leakSignalEnabled()) { // No population-trend signal can be constructed - generations tracking diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index adf45bbd04..a91e5c349d 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -820,7 +820,10 @@ class ReferenceChainTracker { // chasing. Always false when no leak-signal source (generations or liveheap tracking) is // active - the population table and heap-floor ring never fill without one, and a search // would run whole-heap BFS passes with zero candidates for zero diagnostic value. - // seconds_to_oom is the caller's projection (passed to isUrgent()), never re-read here. + // seconds_to_oom is the caller's projection: used only for the gate's debug/test log + // (TEST_LOG_SUMMARY); it does not affect the decision - the urgency latch was already + // advanced once per iteration in publishUrgencyState(), and hasLeakSignal() reads it + // (_urgent_latched) read-only, never re-reading secondsToOOM(). bool hasLeakSignal(double seconds_to_oom); // Latched, hysteretic view of LivenessTracker::secondsToOOM() crossing OOM_URGENT_THRESHOLD_S - @@ -842,13 +845,13 @@ class ReferenceChainTracker { // sufficient, and OOM_URGENT_THRESHOLD_S for the emergency split. bool updateUrgentAdmission(double seconds_to_oom, bool urgent); - // Publishes one threadLoop() iteration's urgency: _oom_ramp_active follows the raw band - // (`urgent`) for shouldRunPass()'s canary-backoff bypass; LivenessTracker's 100% tracking boost // Shared urgency/admission reset (stop()/resetSearchStateForTest()/resetUrgencyForTest()) and // episode close - see the definitions in referenceChains.cpp. void resetUrgencyState(); void closeUrgencyEpisode(); + // Publishes one threadLoop() iteration's urgency: _oom_ramp_active follows the raw band + // (`urgent`) for shouldRunPass()'s canary-backoff bypass; LivenessTracker's 100% tracking boost // (setUrgentTracking()) follows updateUrgentAdmission()'s growth-gated decision, never raw // `urgent`. Extracted from threadLoop() so the wiring is testable without a BFS thread. void publishUrgencyState(double seconds_to_oom, bool urgent); diff --git a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp index 25a4ca426c..01ff82031e 100644 --- a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp +++ b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp @@ -1466,6 +1466,70 @@ TEST(LivenessTrackerSessionTest, LeakSignalSourcesSeamMatchesDerivation) { EXPECT_FALSE(tracker->leakSignalEnabled()); } +// Profiler::start() calls disableChainOnlyLeakSignal() whenever referencechains was requested but +// the chain tracker did not activate (alloc engine not activated, or tracker start failed). It +// must clear ONLY the chain-only leg: the flag and the burn-in stamp go when chains armed the +// signal without a consumer, and both survive untouched when generations keeps its own consumer. +// The SENTINEL is set AFTER initializeGatesOnlyForTest() because that seam restores the burn-in +// stamp (but keeps the derived session gates). +TEST(LivenessTrackerSessionTest, DisableChainOnlyLeakSignalClearsOnlyChainLeg) { + LivenessTracker *tracker = LivenessTracker::instance(); + constexpr u64 SENTINEL = 0x5678ULL; + u64 saved_stamp = LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker); + + // Row 1 - chain-only via the real initialize() derivation: liveheap + referencechains arms + // the signal without generations; the call must clear both the flag and the burn-in stamp. + { + Arguments args; + ASSERT_FALSE(args.parse("memory=1048576:l,referencechains=true")); + LivenessTrackerTestAccessor::initializeGatesOnlyForTest(tracker, args); + ASSERT_FALSE(tracker->gcGenerationsEnabled()); + ASSERT_TRUE(tracker->leakSignalEnabled()); + tracker->setLeakSignalActivatedNsForTest(SENTINEL); + tracker->disableChainOnlyLeakSignal(); + EXPECT_FALSE(tracker->gcGenerationsEnabled()); + EXPECT_FALSE(tracker->leakSignalEnabled()) + << "a chain-only leak signal must be cleared when chains did not activate"; + EXPECT_EQ(0u, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)) + << "the burn-in stamp must be unstamped so a later chains start re-stamps"; + } + + // Row 2 - generations on: the generations leg keeps its own consumer, so the early-return + // guard must leave BOTH the flag and the stamp alone. + { + Arguments args; + ASSERT_FALSE(args.parse("generations=true,referencechains=true")); + LivenessTrackerTestAccessor::initializeGatesOnlyForTest(tracker, args); + ASSERT_TRUE(tracker->gcGenerationsEnabled()); + ASSERT_TRUE(tracker->leakSignalEnabled()); + tracker->setLeakSignalActivatedNsForTest(SENTINEL); + tracker->disableChainOnlyLeakSignal(); + EXPECT_TRUE(tracker->gcGenerationsEnabled()); + EXPECT_TRUE(tracker->leakSignalEnabled()) + << "the generations leg must keep its leak signal"; + EXPECT_EQ(SENTINEL, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)) + << "the generations burn-in stamp must be preserved"; + } + + // Row 3 - unarmed / resetSession() state (alloc engine never started LivenessTracker): the + // call is idempotent and still clears a stale stamp left from an earlier start. + { + tracker->resetSession(); + ASSERT_FALSE(tracker->leakSignalEnabled()); + tracker->setLeakSignalActivatedNsForTest(SENTINEL); + tracker->disableChainOnlyLeakSignal(); + EXPECT_FALSE(tracker->leakSignalEnabled()); + EXPECT_EQ(0u, LivenessTrackerTestAccessor::leakSignalActivatedNsForTest(tracker)) + << "a stale burn-in stamp must not survive a chains-less session"; + } + + // Restore the entry stamp and reset the session gates (singleton hygiene). + tracker->setLeakSignalActivatedNsForTest(saved_stamp); + LivenessTrackerTestAccessor::setSessionGatesForTest( + tracker, /*enabled=*/false, /*generations=*/false); + EXPECT_FALSE(tracker->leakSignalEnabled()); +} + // cleanup_table() calls VM::jni(), so install a fake JavaVM. static JNIEnv_ g_cleanup_table_mock_jni_env{}; From 8d4c6dcbae20d1c6711fd4d6019974103ebf67a7 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 18:24:53 +0100 Subject: [PATCH 34/37] livenessTracker: claim heap-floor slots head-first; end windows at the last published sample --- ddprof-lib/src/main/cpp/counters.h | 8 +- ddprof-lib/src/main/cpp/livenessTracker.cpp | 95 ++++++++++--- ddprof-lib/src/main/cpp/livenessTracker.h | 14 +- .../src/test/cpp/livenessTracker_ut.cpp | 130 +++++++++++++++--- 4 files changed, 204 insertions(+), 43 deletions(-) diff --git a/ddprof-lib/src/main/cpp/counters.h b/ddprof-lib/src/main/cpp/counters.h index e13a785ccb..5f00c2da71 100644 --- a/ddprof-lib/src/main/cpp/counters.h +++ b/ddprof-lib/src/main/cpp/counters.h @@ -297,10 +297,10 @@ * runs only after canAffordNewSearch()'s safepoint budget check and follows \ * the ramped cadence. */ \ X(REFERENCE_CHAIN_URGENT_NO_CANDIDATE, "reference_chain_urgent_no_candidate") \ - /* Heap-floor seqlock: samples DROPPED by a writer that lost the per-slot \ - * claim (CAS lost, or head already unstable) - the only observability for \ - * overlap-driven loss, since GC callbacks cannot log. Never fires while GC \ - * callbacks are serialized (HotSpot). */ \ + /* Heap-floor seqlock: samples DROPPED when a writer claims a slot whose \ + * seq is still the sentinel - only possible if the ring lapped an \ + * in-flight writer (RING_SIZE concurrent writers; never under the \ + * single-writer onGC() contract). Defensive observability only. */ \ X(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP, "heap_floor_sample_dropped_overlap") \ /* Heap-floor seqlock: reads that exhausted HEAP_FLOOR_READ_ATTEMPTS without a consistent \ * (used, seq) pair (also the emergency-band seed's seq_out==nullptr read; per exhausted read, \ diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index c01ee47d77..17b35c3e04 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -1492,8 +1492,11 @@ u64 LivenessTracker::newestHeapFloorSample(u64 *seq_out) const { } return 0; } - // head points one past the newest slot (the writer stores then advances) - // - step back one, wrapping over the ring size. + // head points one past the newest slot: the claim-first writer advances + // head when it claims the index, before the payload/seq publish, so the + // newest slot can transiently hold the sentinel (retry) and otherwise + // carries the newest published sample - step back one, wrapping over the + // ring size. u8 newest = (u8)((head + KLASS_POPULATION_RING_SIZE - 1) % KLASS_POPULATION_RING_SIZE); u64 seq_before = loadAcquire(_heap_floor_seq_ring[newest]); @@ -1550,14 +1553,39 @@ void LivenessTracker::recordHeapFloorSampleUnchecked(u64 used, u64 timestamp_ns, // Lock-free, single-writer-at-a-time - see _heap_floor_ring's own comment // (livenessTracker.h) for why onGC() cannot take _table_lock here. // - // Per-slot seqlock claim: CAS the target slot's seq word to - // HEAP_FLOOR_SEQ_UNSTABLE. The explicit sentinel check stops a - // sentinel-over-sentinel CAS from admitting a second writer into a slot - // the first writer still claims; a failed CAS means another writer - // overlaps (mid-claim, or already finished with a newer seq) - drop this - // sample, counted as HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP (lock-free relaxed - // counter; no logging - GC-callback context). - u8 head = load(_heap_floor_ring_head); + // Claim the slot index through head FIRST: winning the CAS on + // _heap_floor_ring_head (h -> h+1) hands this writer slot h exclusively - + // no other writer can target it until the ring wraps RING_SIZE claims + // later - and advances head BEFORE any payload byte moves. The prior + // order - write, publish seq, then advance head - left a gap where a + // second writer loading the not-yet-advanced head saw the first writer's + // just-published seq as a valid claim baseline, won the per-slot CAS, and + // overwrote the completed sample (both writers then bumped fill + // non-atomically). Settling the index before the seq claim closes that + // gap: the seq-claim CAS below runs on an exclusively-owned slot. + u8 head; + for (;;) { + head = loadAcquire(_heap_floor_ring_head); + if (__atomic_compare_exchange_n(&_heap_floor_ring_head, &head, + (u8)((head + 1) % KLASS_POPULATION_RING_SIZE), + false, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) { + break; + } + // Lost the head race - another writer claimed slot `head`; retry with + // the new head (GC-callback context: bounded contention, no blocking). + } + // We own slot `head`. Its seq holds the previous occupant's sample (a + // stale valid value, written RING_SIZE samples ago) - or the sentinel if + // a lapped writer is still mid-write on it, which requires RING_SIZE + // concurrent in-flight writers and is impossible while the single-writer + // contract holds (GC callbacks are serialized). Claim the seq word for + // the payload stores: the sentinel marks the write in progress for + // concurrent readers (the seq-pairing checks in newestHeapFloorSample()/ + // heapFloorRising()/secondsToOOM() make them retry instead of pairing the + // new used with the old timestamp). Keep the drop as the lap's defensive + // guard - counted, silent (GC-callback context) - but do not build on it: + // the dropped sample leaves this slot at the sentinel until the ring wraps + // back around, degrading the newest read in the meantime. u64 prev = load(_heap_floor_seq_ring[head]); if (prev == HEAP_FLOOR_SEQ_UNSTABLE || !__atomic_compare_exchange_n(&_heap_floor_seq_ring[head], &prev, @@ -1577,21 +1605,51 @@ void LivenessTracker::recordHeapFloorSampleUnchecked(u64 used, u64 timestamp_ns, // Relaxed suffices - the seq is published only by the release store below. u64 seq = __atomic_add_fetch(&_heap_floor_sample_seq, (u64)1, __ATOMIC_RELAXED); storeRelease(_heap_floor_seq_ring[head], seq); - storeRelease(_heap_floor_ring_head, (u8)((head + 1) % KLASS_POPULATION_RING_SIZE)); u8 fill = load(_heap_floor_ring_fill); if (fill < KLASS_POPULATION_RING_SIZE) { storeRelease(_heap_floor_ring_fill, (u8)(fill + 1)); } } +// The heap-floor window's exclusive end for ringWindowStats(): head, stepped +// back one slot when the newest slot is still in-flight (seq sentinel). The +// claim-first writer (recordHeapFloorSampleUnchecked()) advances head before +// the payload/seq publish, so the newest slot can be in-flight; the window +// must end at the last published sample. Benign race with the publish: the +// window then just excludes the freshest sample (stale-but-consistent values) +// instead of spanning the in-flight slot. Single-writer contract: at most one +// slot can be in-flight, and it is always head-1. +// On a step-back *fill is clamped to RING_SIZE-1: a full ring's window would +// otherwise start at (newest - RING_SIZE) == newest and read the in-flight +// slot as its oldest sample. A non-full fill needs no clamp - the writer +// increments fill only after the seq publish, so it never counts the +// in-flight slot. +u8 LivenessTracker::heapFloorWindowHead(u8 head, u8 *fill) const { + u8 newest = (u8)((head + KLASS_POPULATION_RING_SIZE - 1) % + KLASS_POPULATION_RING_SIZE); + if (loadAcquire(_heap_floor_seq_ring[newest]) == HEAP_FLOOR_SEQ_UNSTABLE) { + if (*fill > KLASS_POPULATION_RING_SIZE - 1) { + *fill = (u8)(KLASS_POPULATION_RING_SIZE - 1); + } + return newest; + } + return head; +} + bool LivenessTracker::heapFloorRising() const { // Matches recordHeapFloorSample()'s storeRelease() on both index fields - // loadAcquire() here is what makes the payload writes below visible. u8 fill = loadAcquire(_heap_floor_ring_fill); u8 head = loadAcquire(_heap_floor_ring_head); + // The claim-first writer advances head before the payload/seq publish, so + // the newest slot can be in-flight (seq sentinel); end the window at the + // last published sample instead. Benign race: the writer may publish right + // after the check - the window then just excludes the freshest sample + // (stale-but-consistent values) rather than spanning the in-flight slot. + u8 window_head = heapFloorWindowHead(head, &fill); RingWindowStats stats; if (!ringWindowStats( - head, fill, KLASS_POPULATION_RING_SIZE, + window_head, fill, KLASS_POPULATION_RING_SIZE, KLASS_POPULATION_MIN_FILL_FOR_TREND, [this](int i) { return (double)load(_heap_floor_ring[i]); }, &stats)) { @@ -1668,6 +1726,9 @@ double LivenessTracker::secondsToOOM() const { // its projection simply does not fire. u8 fill = loadAcquire(_heap_floor_ring_fill); u8 head = loadAcquire(_heap_floor_ring_head); + // Same in-flight-slot step-back as heapFloorRising(): the claim-first writer + // can have head one past an unpublished slot. + u8 window_head = heapFloorWindowHead(head, &fill); if (fill < KLASS_POPULATION_MIN_FILL_FOR_TREND) { TEST_LOG("LivenessTracker::secondsToOOM -> -1 (INSUFFICIENT_FILL fill=%d need=%d)", (int)fill, KLASS_POPULATION_MIN_FILL_FOR_TREND); @@ -1675,7 +1736,7 @@ double LivenessTracker::secondsToOOM() const { } RingWindowStats time_stats; if (!ringWindowStats( - head, fill, KLASS_POPULATION_RING_SIZE, + window_head, fill, KLASS_POPULATION_RING_SIZE, KLASS_POPULATION_MIN_FILL_FOR_TREND, [this](int i) { return (double)load(_heap_floor_time_ring[i]); }, &time_stats)) { @@ -1694,11 +1755,11 @@ double LivenessTracker::secondsToOOM() const { RingWindowStats heap_bytes; if (max_heap > 0 && - ringWindowStats(head, fill, KLASS_POPULATION_RING_SIZE, + ringWindowStats(window_head, fill, KLASS_POPULATION_RING_SIZE, KLASS_POPULATION_MIN_FILL_FOR_TREND, [this](int i) { return (double)load(_heap_floor_ring[i]); }, &heap_bytes) && - corroborateRecentHalf(head, fill, KLASS_POPULATION_RING_SIZE, + corroborateRecentHalf(window_head, fill, KLASS_POPULATION_RING_SIZE, HEAP_FLOOR_RECENT_HALF_MIN_FILL, [this](int i) { return (double)load(_heap_floor_ring[i]); })) { // A dip-then-recover window passes corroborateRecentHalf() but can have a @@ -1719,11 +1780,11 @@ double LivenessTracker::secondsToOOM() const { RingWindowStats container_bytes; if (container_limit > 0 && - ringWindowStats(head, fill, KLASS_POPULATION_RING_SIZE, + ringWindowStats(window_head, fill, KLASS_POPULATION_RING_SIZE, KLASS_POPULATION_MIN_FILL_FOR_TREND, [this](int i) { return (double)load(_container_mem_ring[i]); }, &container_bytes) && - corroborateRecentHalf(head, fill, KLASS_POPULATION_RING_SIZE, + corroborateRecentHalf(window_head, fill, KLASS_POPULATION_RING_SIZE, HEAP_FLOOR_RECENT_HALF_MIN_FILL, [this](int i) { return (double)load(_container_mem_ring[i]); })) { // Same guard as for the heap. diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index c1de89daa5..e854b64ce7 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -375,9 +375,11 @@ class alignas(alignof(SpinLock)) LivenessTracker { // HotSpot behaviour, not a documented JVMTI contract; other VMs such as // J9/Zing behind the abstraction layer may differ), so // onGC() is always a single writer at a time, matching the existing - // lock-free _gc_epoch/_used_after_last_gc fields' own assumption - see - // recordHeapFloorSample()/heapFloorRising() (livenessTracker.cpp) for the - // load/store ordering this relies on. + // lock-free _gc_epoch/_used_after_last_gc fields' own assumption. + // recordHeapFloorSampleUnchecked() still claims each slot index through + // an atomic head CAS before writing, so a contract violation degrades to + // lost samples and sentinel-guarded reads rather than torn ones - see that + // method's comment for the order and what each guard covers. u64 _heap_floor_ring[KLASS_POPULATION_RING_SIZE]; // OS::nanotime() paired index-for-index with _heap_floor_ring above (same // head/fill, always pushed together by recordHeapFloorSample() - see that @@ -846,6 +848,12 @@ class alignas(alignof(SpinLock)) LivenessTracker { // itself deterministic for the heapFloorRecordForTest() test seam below. void recordHeapFloorSample(u64 used, u64 timestamp_ns, u64 container_used); + // The heap-floor window's exclusive end for ringWindowStats(): head, stepped + // back one slot when the newest slot is still in-flight (seq sentinel), with + // *fill clamped so the window excludes that slot - see the definition in + // livenessTracker.cpp. + u8 heapFloorWindowHead(u8 head, u8 *fill) const; + // Internal: pushes to the rings without checking // _heap_floor_recording_disabled_for_test. Called by // recordHeapFloorSample() (after the debug-only flag check) and by diff --git a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp index 01ff82031e..4633919a3f 100644 --- a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp +++ b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp @@ -1253,15 +1253,6 @@ class LivenessTrackerTestAccessor { return true; } - // Swaps the head slot's seqlock word for `value` (e.g. the busy - // sentinel) and returns the previous value so the caller can restore it. - static u64 swapHeapFloorHeadSeqForTest(LivenessTracker *t, u64 value) { - u8 head = t->_heap_floor_ring_head; - u64 prev = t->_heap_floor_seq_ring[head]; - t->_heap_floor_seq_ring[head] = value; - return prev; - } - // Swaps the newest slot's ((head + ring - 1) % ring) seqlock word for // `value` and returns the previous value - the same slot the reader // validates in newestHeapFloorSample(). @@ -1273,6 +1264,17 @@ class LivenessTrackerTestAccessor { return prev; } + // Stores `value` into seq slot `idx` and returns the previous value. + static u64 swapHeapFloorSeqAtForTest(LivenessTracker *t, u8 idx, u64 value) { + u64 prev = t->_heap_floor_seq_ring[idx]; + t->_heap_floor_seq_ring[idx] = value; + return prev; + } + + static u8 heapFloorWindowHeadForTest(LivenessTracker *t, u8 head, u8 *fill) { + return t->heapFloorWindowHead(head, fill); + } + static u8 heapFloorRingHeadForTest(LivenessTracker *t) { return t->_heap_floor_ring_head; } @@ -1981,10 +1983,77 @@ TEST_F(HeapFloorSeqlockTest, NewestSampleReportsUnstableAfterRetryBound) { << "an exhausted read with seq_out == nullptr must be counted"; } -// A slot pinned at the sentinel models an overlapping writer mid-claim: -// the writer drops its sample (head, fill and the sample counter unchanged) -// and the next accepted sample takes the very next seq - no gap, no duplicate. -TEST_F(HeapFloorSeqlockTest, OverlappingWriterDropsSampleWithoutDuplicateSeq) { +// A slot pinned at the sentinel models a lapped writer still mid-write on +// the slot the current claim targets (RING_SIZE concurrent writers - the +// only way the seq-claim guard can fail under the claim-first order). The +// writer drops its sample (fill and the sample counter unchanged) but the +// head claim has already advanced past the sentinel slot, which stays +// pinned until the ring wraps back around; window reads step over it +// (heapFloorWindowHead()). +// heapFloorWindowHead(): a published newest slot leaves head and fill alone; +// an in-flight newest slot (seq sentinel) steps the window end back one slot +// and, on a full ring only, clamps fill to RING_SIZE-1 - otherwise the +// window would start at the in-flight slot and read it as the oldest sample. +TEST_F(HeapFloorSeqlockTest, WindowHeadExcludesInFlightSlot) { + LivenessTracker *lt = LivenessTracker::instance(); + const int ring = LivenessTracker::heapFloorRingSizeForTest(); + struct NewestSeqRestore { + LivenessTracker *t; + bool armed; + u64 saved; + ~NewestSeqRestore() { + if (armed) { + LivenessTrackerTestAccessor::swapHeapFloorNewestSeqForTest(t, saved); + } + } + } pin{lt, false, 0}; + + // Non-full ring. + for (int i = 0; i < 5; i++) { + lt->heapFloorRecordForTest(1000 + (u64)i, (u64)i + 1); + } + u8 head = LivenessTrackerTestAccessor::heapFloorRingHeadForTest(lt); + u8 fill = LivenessTrackerTestAccessor::heapFloorRingFillForTest(lt); + ASSERT_EQ(5, fill); + u8 f = fill; + EXPECT_EQ(head, LivenessTrackerTestAccessor::heapFloorWindowHeadForTest(lt, head, &f)) + << "a published newest slot must leave the window end at head"; + EXPECT_EQ(fill, f); + + pin.saved = LivenessTrackerTestAccessor::swapHeapFloorNewestSeqForTest( + lt, LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE); + pin.armed = true; + u8 newest = (u8)((head + ring - 1) % ring); + f = fill; + EXPECT_EQ(newest, LivenessTrackerTestAccessor::heapFloorWindowHeadForTest(lt, head, &f)) + << "an in-flight newest slot must step the window end back one slot"; + EXPECT_EQ(fill, f) << "a non-full fill never counts the in-flight slot - no clamp"; + LivenessTrackerTestAccessor::swapHeapFloorNewestSeqForTest(lt, pin.saved); + pin.armed = false; + + // Full ring. + for (int i = 5; i < ring + 3; i++) { + lt->heapFloorRecordForTest(1000 + (u64)i, (u64)i + 1); + } + head = LivenessTrackerTestAccessor::heapFloorRingHeadForTest(lt); + fill = LivenessTrackerTestAccessor::heapFloorRingFillForTest(lt); + ASSERT_EQ(ring, fill); + f = fill; + EXPECT_EQ(head, LivenessTrackerTestAccessor::heapFloorWindowHeadForTest(lt, head, &f)); + EXPECT_EQ(fill, f) << "a published full ring keeps the full window"; + + pin.saved = LivenessTrackerTestAccessor::swapHeapFloorNewestSeqForTest( + lt, LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE); + pin.armed = true; + newest = (u8)((head + ring - 1) % ring); + f = fill; + EXPECT_EQ(newest, LivenessTrackerTestAccessor::heapFloorWindowHeadForTest(lt, head, &f)); + EXPECT_EQ(ring - 1, f) + << "a full ring's stepped-back window must drop one slot, or it starts at the " + "in-flight slot"; +} + +TEST_F(HeapFloorSeqlockTest, LapOverlapDropsSampleWithoutDuplicateSeq) { LivenessTracker *lt = LivenessTracker::instance(); HeapFloorReadHookGuard hook_guard; lt->heapFloorRecordForTest(1000, 1); @@ -1994,25 +2063,48 @@ TEST_F(HeapFloorSeqlockTest, OverlappingWriterDropsSampleWithoutDuplicateSeq) { u8 head = LivenessTrackerTestAccessor::heapFloorRingHeadForTest(lt); u8 fill = LivenessTrackerTestAccessor::heapFloorRingFillForTest(lt); u64 sample_seq = LivenessTrackerTestAccessor::heapFloorSampleSeqForTest(lt); - u64 saved = LivenessTrackerTestAccessor::swapHeapFloorHeadSeqForTest( - lt, LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE); + // Pin the head slot at the sentinel for the rest of the test: under the + // claim-first order the drop consumes the head claim, so the slot stays + // pinned while the lapped writer is (simulated as) still mid-write. The + // ring resets do not touch the seq ring, so the guard restores the slot + // on every exit - a leaked sentinel makes later tests drop samples there. + struct SeqSlotRestore { + LivenessTracker *t; + u8 idx; + u64 saved; + ~SeqSlotRestore() { + LivenessTrackerTestAccessor::swapHeapFloorSeqAtForTest(t, idx, saved); + } + } pin{lt, head, + LivenessTrackerTestAccessor::swapHeapFloorSeqAtForTest( + lt, head, LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE)}; long long dropped = Counters::getCounter(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP); lt->heapFloorRecordForTest(1100, 2); EXPECT_EQ(dropped + 1, Counters::getCounter(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP)) << "a dropped sample must be counted"; - EXPECT_EQ(head, LivenessTrackerTestAccessor::heapFloorRingHeadForTest(lt)) - << "a dropped sample must not advance the ring"; + EXPECT_EQ((u8)((head + 1) % LivenessTracker::heapFloorRingSizeForTest()), + LivenessTrackerTestAccessor::heapFloorRingHeadForTest(lt)) + << "the head claim settles before the seq claim, so a dropped sample\n" + "leaves head advanced past the pinned slot"; EXPECT_EQ(fill, LivenessTrackerTestAccessor::heapFloorRingFillForTest(lt)) << "a dropped sample must not change the fill"; EXPECT_EQ(sample_seq, LivenessTrackerTestAccessor::heapFloorSampleSeqForTest(lt)) << "a dropped sample must not consume a sequence number"; - LivenessTrackerTestAccessor::swapHeapFloorHeadSeqForTest(lt, saved); + // The pinned slot degrades the newest read until the ring wraps onto it: + // the read retries on the sentinel, exhausts, and reports the sentinel + // seq (counted as READ_UNSTABLE) - the documented cost of the lap drop. + u64 seq = 0xdeadbeef; + EXPECT_EQ(0u, lt->newestHeapFloorSample(&seq)); + EXPECT_EQ(LivenessTracker::HEAP_FLOOR_SEQ_UNSTABLE, seq); + + // A following accepted sample takes the very next seq - no gap, no + // duplicate - and the newest read recovers (it targets the next slot; + // the pinned slot stays sentinel until the ring wraps onto it). lt->heapFloorRecordForTest(1100, 2); EXPECT_EQ(dropped + 1, Counters::getCounter(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP)) << "an accepted sample must not be counted as dropped"; - u64 seq = 0; EXPECT_EQ(1100u, lt->newestHeapFloorSample(&seq)); EXPECT_EQ(prev_seq + 1, seq) << "the dropped writer must leave neither a gap nor a duplicate"; From e4af72b291841f792f18e5802cb4f6a000d99d98 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 18:24:57 +0100 Subject: [PATCH 35/37] tests: drive the urgency latch via isUrgentForTest0 seam --- ddprof-lib/src/main/cpp/javaApi.cpp | 19 ++++++++++ .../com/datadoghq/profiler/JavaProfiler.java | 8 +++++ .../profiler/JavaProfilerApiSurfaceTest.java | 2 +- .../profiler/ReferenceChainTestHooks.java | 5 +++ .../AggressiveLeakReferenceChainTest.java | 36 +++++++++++++------ 5 files changed, 59 insertions(+), 11 deletions(-) diff --git a/ddprof-lib/src/main/cpp/javaApi.cpp b/ddprof-lib/src/main/cpp/javaApi.cpp index f8e61f062a..671168222d 100644 --- a/ddprof-lib/src/main/cpp/javaApi.cpp +++ b/ddprof-lib/src/main/cpp/javaApi.cpp @@ -1460,6 +1460,19 @@ Java_com_datadoghq_profiler_JavaProfiler_urgentLatchedForTest0(JNIEnv *env, return tracker->urgentLatchedForTest() ? JNI_TRUE : JNI_FALSE; } +// Drives the latch machine once on the live projection - what threadLoop()'s +// publishUrgencyState() does per iteration. The gate (hasLeakSignal()) no +// longer advances the machine, so tests that need a latched episode drive it +// here. Mutated under _engine_lock, same as the read above. +extern "C" DLLEXPORT jboolean JNICALL +Java_com_datadoghq_profiler_JavaProfiler_isUrgentForTest0(JNIEnv *env, + jclass unused) { + ProfiledThread::initCurrentThreadSignalSafe(); + ReferenceChainTracker *tracker = ReferenceChainTracker::instance(); + MutexLocker engine_guard(tracker->_engine_lock); + return tracker->isUrgentForTest() ? JNI_TRUE : JNI_FALSE; +} + #else // !DEBUG // Release builds: no-op stubs so the Java natives still link. @@ -1598,6 +1611,12 @@ Java_com_datadoghq_profiler_JavaProfiler_urgentLatchedForTest0(JNIEnv *env, return JNI_FALSE; } +extern "C" DLLEXPORT jboolean JNICALL +Java_com_datadoghq_profiler_JavaProfiler_isUrgentForTest0(JNIEnv *env, + jclass unused) { + return JNI_FALSE; +} + #endif // DEBUG // ---- Test-only reads of the current thread's OTEP record ----------------------------------- diff --git a/ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java b/ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java index 1f91d50f0c..9e406c75c0 100644 --- a/ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java +++ b/ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java @@ -791,6 +791,14 @@ private static native void setTraceContext0(long localRootSpanId, long spanId, l */ static native boolean urgentLatchedForTest0(); + /** + * Test seam (debug native builds only): drives ReferenceChainTracker's urgency latch machine + * once on the live projection - what {@code publishUrgencyState()} does per iteration. The + * gate ({@code hasLeakSignal()}) only reads the latch, so tests that need a latched episode + * drive it here. The release stub returns {@code false}. + */ + static native boolean isUrgentForTest0(); + // ---- Test-only reads of the current thread's OTEP record ---------------------------------- // Each resolves the current carrier's record directly (like the write primitives above) with // no cached buffer and no per-thread Java object; introspection/test use only. diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/JavaProfilerApiSurfaceTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/JavaProfilerApiSurfaceTest.java index 6dde526f50..0a9883b2f7 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/JavaProfilerApiSurfaceTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/JavaProfilerApiSurfaceTest.java @@ -40,7 +40,7 @@ public void ownedBlockHooksAreNotPublicApiBeforeTaskBlockInstrumentation() throw "heapFloorResetForTest0", "heapFloorRisingForTest0", "shouldRunPassForTest0", "referenceChainPassesRunForTest0", "backdateLeakSignalActivationForTest0", "setLeakSignalActivatedNsForTest0", - "secondsToOOMForTest0", "urgentLatchedForTest0")); + "secondsToOOMForTest0", "urgentLatchedForTest0", "isUrgentForTest0")); @Test public void referenceChainTestSeamsAreNotPublicApi() { diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/ReferenceChainTestHooks.java b/ddprof-test/src/test/java/com/datadoghq/profiler/ReferenceChainTestHooks.java index 285894c7a5..9a96808345 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/ReferenceChainTestHooks.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/ReferenceChainTestHooks.java @@ -140,4 +140,9 @@ public static double secondsToOOMForTest0() { public static boolean urgentLatchedForTest0() { return JavaProfiler.urgentLatchedForTest0(); } + + /** Drives the urgency latch machine once on the live projection (debug native builds only). */ + public static boolean isUrgentForTest0() { + return JavaProfiler.isUrgentForTest0(); + } } diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java index 5fb113074b..38de748a9f 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/referencechains/AggressiveLeakReferenceChainTest.java @@ -49,9 +49,9 @@ * {@code OOM_PROJECTION_BURN_IN_NS} (5 minutes) after the leak signal first activated, and this * class may run inside that window, so each gate test backdates the burn-in stamp with * {@code backdateLeakSignalActivationForTest0()} and restores it in its finally block. Positive - * controls ({@code secondsToOOMForTest0()} for the projection, {@code urgentLatchedForTest0()} for - * the urgency latch) prove the urgency machine is engaged (or provably not, for the flat-floor - * control) before any negative gate assertion is trusted. {@code shouldRunPassForTest0()} then + * controls ({@code secondsToOOMForTest0()} for the projection, {@code isUrgentForTest0()}/{@code + * urgentLatchedForTest0()} for the urgency machine) prove the urgency machine is engaged (or + * provably not, for the flat-floor control) before any negative gate assertion is trusted. {@code shouldRunPassForTest0()} then * evaluates {@code ReferenceChainTracker::shouldRunPass()} - the gate {@code hasLeakSignal()} * feeds - directly on a freshly reset search, rather than running a real pass or waiting on the * live BFS thread, so this class never depends on {@code selectLeakCandidates()} ranking @@ -281,15 +281,26 @@ public void shouldNotOpenSearchGateOnAggressiveHeapWideGrowthWithNoLeakCandidate + secondsToOom + " - the burn-in backdate did not take effect or the seeds no " + "longer project, so the closed gate below would prove nothing"); + // Positive control 2: drive the urgency machine on the seeded projection - the same call + // threadLoop()'s publishUrgencyState() makes per iteration - and it must latch, proving the + // projection genuinely sits in the urgent band. The gate below is evaluated after the + // search reset (which clears this latch again); under the candidate-gate contract the + // gate's decision reads only selectLeakCandidates() - the latch suppresses no-progress + // abandonment in a running search, it never opens the gate. + assertTrue(ReferenceChainTestHooks.isUrgentForTest0(), + "The urgency machine must latch on the seeded urgent-band projection (" + + secondsToOom + "s)"); + assertTrue(ReferenceChainTestHooks.urgentLatchedForTest0(), + "The drive must leave the latch held"); + GateEvaluation e = evaluateGateAfterSearchReset(); checkNoCandidateFreeActivity(e); - // Positive control 2: the gate evaluation must have latched urgency from the seeded - // projection, so the gate is closed by the candidate requirement and not by a dormant - // urgency machine. - assertTrue(ReferenceChainTestHooks.urgentLatchedForTest0(), - "Urgency must be latched after evaluating the gate on an urgent-band projection (" - + secondsToOom + "s)"); + // The gate must not have advanced the latch machine: after the reset's clear, an + // evaluation on a candidate-less urgent-band projection leaves it cleared (the machine + // advances only in publishUrgencyState()). + assertFalse(ReferenceChainTestHooks.urgentLatchedForTest0(), + "The gate evaluation must not advance the latch machine"); assertFalse(e.gateOpen, "Expected the search gate to stay closed: urgency is latched but no per-klass " @@ -336,11 +347,16 @@ public void shouldNotOpenSearchGateOnFlatHeapFloorWithNoLeakCandidate() { // evaluateGateAfterSearchReset()), not the population: resetKlassPopulationForTest0() also // zeroes the heap-floor ring seeded above, which would turn this control into a trivial // "insufficient fill" pass. + // Control: driving the urgency machine on the flat projection must not latch - the + // machine's own answer, independent of the gate. + assertFalse(ReferenceChainTestHooks.isUrgentForTest0(), + "A flat heap floor gives no projection, so driving the urgency machine must not latch"); GateEvaluation e = evaluateGateAfterSearchReset(); checkNoCandidateFreeActivity(e); assertFalse(ReferenceChainTestHooks.urgentLatchedForTest0(), - "A flat heap floor gives no projection, so it must leave urgency unlatched"); + "A flat heap floor gives no projection, so it must leave urgency unlatched - and the " + + "gate evaluation must not advance the latch machine"); assertFalse(e.gateOpen, "Expected the search-restart gate to stay closed: no heap growth and no per-klass " + "candidate give hasLeakSignal() nothing to trust (" + e.detail + ")"); From 3f9dd792deab09e061c5b62b958e77c03b6c79ea Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 19:01:31 +0100 Subject: [PATCH 36/37] livenessTracker: bump heap-floor fill with a CAS --- ddprof-lib/src/main/cpp/livenessTracker.cpp | 31 +++++++----- ddprof-lib/src/main/cpp/livenessTracker.h | 11 +++-- .../src/test/cpp/livenessTracker_ut.cpp | 49 +++++++++++++++++++ 3 files changed, 73 insertions(+), 18 deletions(-) diff --git a/ddprof-lib/src/main/cpp/livenessTracker.cpp b/ddprof-lib/src/main/cpp/livenessTracker.cpp index 17b35c3e04..e2e9014521 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.cpp +++ b/ddprof-lib/src/main/cpp/livenessTracker.cpp @@ -1556,13 +1556,10 @@ void LivenessTracker::recordHeapFloorSampleUnchecked(u64 used, u64 timestamp_ns, // Claim the slot index through head FIRST: winning the CAS on // _heap_floor_ring_head (h -> h+1) hands this writer slot h exclusively - // no other writer can target it until the ring wraps RING_SIZE claims - // later - and advances head BEFORE any payload byte moves. The prior - // order - write, publish seq, then advance head - left a gap where a - // second writer loading the not-yet-advanced head saw the first writer's - // just-published seq as a valid claim baseline, won the per-slot CAS, and - // overwrote the completed sample (both writers then bumped fill - // non-atomically). Settling the index before the seq claim closes that - // gap: the seq-claim CAS below runs on an exclusively-owned slot. + // later - and advances head BEFORE any payload byte moves. Advancing head + // after the seq publish instead would let a second writer, loading the + // not-yet-advanced head, take the just-published seq as its claim + // baseline and overwrite the completed sample. u8 head; for (;;) { head = loadAcquire(_heap_floor_ring_head); @@ -1580,9 +1577,11 @@ void LivenessTracker::recordHeapFloorSampleUnchecked(u64 used, u64 timestamp_ns, // concurrent in-flight writers and is impossible while the single-writer // contract holds (GC callbacks are serialized). Claim the seq word for // the payload stores: the sentinel marks the write in progress for - // concurrent readers (the seq-pairing checks in newestHeapFloorSample()/ - // heapFloorRising()/secondsToOOM() make them retry instead of pairing the - // new used with the old timestamp). Keep the drop as the lap's defensive + // concurrent readers - newestHeapFloorSample() retries on it, and the + // window readers (heapFloorRising()/secondsToOOM()) end their window + // before it (heapFloorWindowHead()). The window readers check only the + // newest slot, so they rely on the single-writer contract's "at most one + // slot in flight". Keep the drop as the lap's defensive // guard - counted, silent (GC-callback context) - but do not build on it: // the dropped sample leaves this slot at the sentinel until the ring wraps // back around, degrading the newest read in the meantime. @@ -1605,9 +1604,15 @@ void LivenessTracker::recordHeapFloorSampleUnchecked(u64 used, u64 timestamp_ns, // Relaxed suffices - the seq is published only by the release store below. u64 seq = __atomic_add_fetch(&_heap_floor_sample_seq, (u64)1, __ATOMIC_RELAXED); storeRelease(_heap_floor_seq_ring[head], seq); - u8 fill = load(_heap_floor_ring_fill); - if (fill < KLASS_POPULATION_RING_SIZE) { - storeRelease(_heap_floor_ring_fill, (u8)(fill + 1)); + // Saturating CAS increment: a plain load/store pair would lose an + // increment to an overlapping writer, leaving fill below the number of + // published samples. Release on success publishes this sample's payload + // to readers that acquire-load fill. + u8 fill = loadAcquire(_heap_floor_ring_fill); + while (fill < KLASS_POPULATION_RING_SIZE && + !__atomic_compare_exchange_n(&_heap_floor_ring_fill, &fill, + (u8)(fill + 1), false, + __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) { } } diff --git a/ddprof-lib/src/main/cpp/livenessTracker.h b/ddprof-lib/src/main/cpp/livenessTracker.h index e854b64ce7..6f058125d2 100644 --- a/ddprof-lib/src/main/cpp/livenessTracker.h +++ b/ddprof-lib/src/main/cpp/livenessTracker.h @@ -377,9 +377,10 @@ class alignas(alignof(SpinLock)) LivenessTracker { // onGC() is always a single writer at a time, matching the existing // lock-free _gc_epoch/_used_after_last_gc fields' own assumption. // recordHeapFloorSampleUnchecked() still claims each slot index through - // an atomic head CAS before writing, so a contract violation degrades to - // lost samples and sentinel-guarded reads rather than torn ones - see that - // method's comment for the order and what each guard covers. + // an atomic head CAS and bumps fill with a CAS, so overlapping writers + // never share a slot or lose a fill increment. The window readers still + // rely on the contract: they skip only the newest in-flight slot - see + // that method's comment for the order and what each guard covers. u64 _heap_floor_ring[KLASS_POPULATION_RING_SIZE]; // OS::nanotime() paired index-for-index with _heap_floor_ring above (same // head/fill, always pushed together by recordHeapFloorSample() - see that @@ -400,8 +401,8 @@ class alignas(alignof(SpinLock)) LivenessTracker { // marks a write in progress; any other value names the one recorded sample // in that slot (from _heap_floor_sample_seq below) for the process // lifetime. The static singleton's zero-initialization guarantees no slot - // starts at the sentinel - a slot stuck there would stall the ring - // forever. + // starts at the sentinel - a slot stuck there would drop every sample + // that claims it. u64 _heap_floor_seq_ring[KLASS_POPULATION_RING_SIZE]; volatile u8 _heap_floor_ring_head; volatile u8 _heap_floor_ring_fill; diff --git a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp index 4633919a3f..335c1ed713 100644 --- a/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp +++ b/ddprof-lib/src/test/cpp/livenessTracker_ut.cpp @@ -2053,6 +2053,55 @@ TEST_F(HeapFloorSeqlockTest, WindowHeadExcludesInFlightSlot) { "in-flight slot"; } +// Overlapping writers (a single-writer contract violation) must each get a +// distinct slot and a counted fill: with fewer samples than ring slots, fill +// ends at exactly the number of samples recorded, every seq is unique, and +// nothing is dropped. A plain load/store fill bump loses increments here. +TEST_F(HeapFloorSeqlockTest, OverlappingWritersKeepFillAndSeqExact) { + LivenessTracker *lt = LivenessTracker::instance(); + const int ring = LivenessTracker::heapFloorRingSizeForTest(); + constexpr int kThreads = 4; + const int per_thread = (ring - 1) / kThreads; + const int total = kThreads * per_thread; + ASSERT_LT(total, ring) << "the ring must not saturate fill"; + + // The sample counter is never reset, so compare deltas. + u64 seq_before = LivenessTrackerTestAccessor::heapFloorSampleSeqForTest(lt); + long long dropped = Counters::getCounter(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP); + constexpr int kRounds = 200; + for (int round = 0; round < kRounds; round++) { + lt->heapFloorResetForTest(); + std::atomic ready{0}; + std::atomic go{false}; + std::vector threads; + for (int t = 0; t < kThreads; t++) { + threads.emplace_back([&, t]() { + ready.fetch_add(1); + while (!go.load(std::memory_order_acquire)) { + } + for (int i = 0; i < per_thread; i++) { + lt->heapFloorRecordForTest(1000 + (u64)t, (u64)(t * per_thread + i) + 1); + } + }); + } + while (ready.load() < kThreads) { + } + go.store(true, std::memory_order_release); + for (auto &th : threads) { + th.join(); + } + ASSERT_EQ(total, LivenessTrackerTestAccessor::heapFloorRingFillForTest(lt)) + << "round " << round << ": an overlapping writer lost a fill increment"; + ASSERT_EQ((u8)(total % ring), LivenessTrackerTestAccessor::heapFloorRingHeadForTest(lt)) + << "round " << round << ": every sample must claim its own slot"; + } + EXPECT_EQ(seq_before + (u64)kRounds * total, + LivenessTrackerTestAccessor::heapFloorSampleSeqForTest(lt)) + << "every sample must consume exactly one seq"; + EXPECT_EQ(dropped, Counters::getCounter(HEAP_FLOOR_SAMPLE_DROPPED_OVERLAP)) + << "writers that do not lap the ring must never drop"; +} + TEST_F(HeapFloorSeqlockTest, LapOverlapDropsSampleWithoutDuplicateSeq) { LivenessTracker *lt = LivenessTracker::instance(); HeapFloorReadHookGuard hook_guard; From de265dd35a8d67a96897f331d8022cd022623232 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 3 Nov 2026 19:14:31 +0100 Subject: [PATCH 37/37] referenceChains: ramp the cadence only while a search is running --- ddprof-lib/src/main/cpp/referenceChains.cpp | 26 +++++--- ddprof-lib/src/main/cpp/referenceChains.h | 5 +- .../src/test/cpp/referenceChainsOomTests.inc | 63 +++++++++++++++++++ .../test/cpp/referenceChainsTestAccessors.h | 25 ++++++-- .../ReferenceChainsSignalsExplained.md | 2 +- 5 files changed, 107 insertions(+), 14 deletions(-) diff --git a/ddprof-lib/src/main/cpp/referenceChains.cpp b/ddprof-lib/src/main/cpp/referenceChains.cpp index c4fe7705e9..88d4ddc6f8 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.cpp +++ b/ddprof-lib/src/main/cpp/referenceChains.cpp @@ -499,11 +499,23 @@ bool ReferenceChainTracker::applyOomRamp(double seconds_to_oom) { double x = 1.0 - seconds_to_oom / OOM_RAMP_START_S; // 0 at 30min out, 1 at OOM target_ms = std::lround(_pause_target_ms * std::pow((double)URGENT_PAUSE_TARGET_MS / std::max(_pause_target_ms, 1L), x)); - // Ramp from the fixed configured cadence, not the currently-adaptive _effective_cadence_ns - - // using the live value as the ramp's own moving anchor would compound the exponent across - // iterations instead of tracking urgency directly from a stable baseline. - cadence_ns = (u64)std::llround((double)PASS_CADENCE_NS * - std::pow((double)URGENT_CADENCE_NS / (double)PASS_CADENCE_NS, x)); + // The cadence ramp applies only while a search is running: it paces passes. With no search + // running (not started, or terminal awaiting a restart) a wake does no pass, only the restart + // gate's selectLeakCandidates() probe under the shared _table_lock - ramping that idle wake + // toward URGENT_CADENCE_NS would spend up to ~100 probes/s for at most 1s faster candidate + // discovery against a 30-minute horizon. Such iterations hold PASS_CADENCE_NS; a search + // started by the restart gate runs its first pass without sleeping, and the next iteration + // sees it running and ramps. + bool search_running = _search_started && load(_search_state) == (u8)SearchState::RUNNING; + if (search_running) { + // Ramp from the fixed configured cadence, not the currently-adaptive _effective_cadence_ns - + // using the live value as the ramp's own moving anchor would compound the exponent across + // iterations instead of tracking urgency directly from a stable baseline. + cadence_ns = (u64)std::llround((double)PASS_CADENCE_NS * + std::pow((double)URGENT_CADENCE_NS / (double)PASS_CADENCE_NS, x)); + } else { + cadence_ns = PASS_CADENCE_NS; + } // Cadence ownership on every branch: urgent iterations set it here, the episode's end (or // stop()) restores PASS_CADENCE_NS, and otherwise updatePacing() owns it. _effective_cadence_ns = cadence_ns; @@ -587,8 +599,8 @@ void ReferenceChainTracker::threadLoop() { DEBUG_ONLY(rcDebugLevelRefresh()); // Hand the ramp state to shouldRunPass() before it decides: _oom_ramp_active bypasses the - // canary-backoff gate, the growth-gated admission (held between heap-floor samples) follows - // the same hand-off, and the cadence ramp shortens the idle sleep with no search running. + // canary-backoff gate, and the growth-gated admission (held between heap-floor samples) + // follows the same hand-off. publishUrgencyState(seconds_to_oom, urgent); // shouldRunPass()'s restart branch (restartSearch()) clears _pending_expand/_priority_expand diff --git a/ddprof-lib/src/main/cpp/referenceChains.h b/ddprof-lib/src/main/cpp/referenceChains.h index a91e5c349d..668b4a3199 100644 --- a/ddprof-lib/src/main/cpp/referenceChains.h +++ b/ddprof-lib/src/main/cpp/referenceChains.h @@ -857,8 +857,9 @@ class ReferenceChainTracker { void publishUrgencyState(double seconds_to_oom, bool urgent); // threadLoop()'s per-iteration OOM ramp: pause target, cadence and the urgency budget - // boost/restore, keyed on the raw band comparison (never the latch). Returns that raw `urgent` - // so the caller needs no second evaluation. + // boost/restore, keyed on the raw band comparison (never the latch). The cadence ramps only while + // a search is running; an urgent iteration with no search running holds PASS_CADENCE_NS. Returns + // that raw `urgent` so the caller needs no second evaluation. bool applyOomRamp(double seconds_to_oom); // Search restart gate: true once _safepoint_pain_budget has diff --git a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc index 82e134132c..f65a361a28 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc +++ b/ddprof-lib/src/test/cpp/referenceChainsOomTests.inc @@ -1102,6 +1102,8 @@ TEST_F(OomUrgencyTest, UrgencyExitRestoresFallbackCadence) { ReferenceChainsTestAccessor::restoreOomRampStateForTest(saved); } } restore{ReferenceChainsTestAccessor::saveOomRampStateForTest()}; + // The cadence ramp paces passes, so it applies only while a search runs. + ReferenceChainsTestAccessor::setSearchRunningForTest(true); // Seed a known budget ceiling and pause target: the singleton may never // have been start()ed (zeros would make the boost and the ramp vacuous). @@ -1161,6 +1163,62 @@ TEST_F(OomUrgencyTest, UrgencyExitRestoresFallbackCadence) { << "the episode end must restore the configured pause target"; } +// The cadence ramp paces passes, so with no search running - not started, or +// terminal awaiting a restart - an urgent iteration holds the 1s fallback: +// the idle wake only probes selectLeakCandidates(), and ramping it would +// spend up to ~100 probes/s. The rest of the episode (budget boost, pause +// target) still applies, and the ramp follows the search state both ways. +TEST_F(OomUrgencyTest, UrgentIdleWakeHoldsFallbackCadence) { + ASSERT_FALSE(ReferenceChainsTestAccessor::bfsThreadRunning()) + << "applyOomRampForTest() would race threadLoop()"; + const u64 pass_ns = ReferenceChainsTestAccessor::baselineCadenceNs(); + const u64 urgent_ns = ReferenceChainsTestAccessor::urgentCadenceNsForTest(); + struct Restore { + ReferenceChainsTestAccessor::OomRampState saved; + ~Restore() { + ReferenceChainsTestAccessor::applyOomRampForTest(-1.0); + ReferenceChainsTestAccessor::setEffectiveCadenceNs( + ReferenceChainsTestAccessor::baselineCadenceNs()); + ReferenceChainsTestAccessor::restoreOomRampStateForTest(saved); + } + } restore{ReferenceChainsTestAccessor::saveOomRampStateForTest()}; + const int configured_budget = 1000; + ReferenceChainsTestAccessor::setBudgetsForTest(configured_budget, configured_budget); + ReferenceChainsTestAccessor::setPauseTargetsForTest(20); + ReferenceChainsTestAccessor::setEffectiveCadenceNs(2 * pass_ns); + + // Not started (a fresh tracker or a just-restarted search). + ReferenceChainsTestAccessor::setSearchNotStartedForTest(); + EXPECT_TRUE(ReferenceChainsTestAccessor::applyOomRampForTest(0.0)); + EXPECT_EQ(pass_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "no search started: the urgent idle wake must hold the fallback cadence"; + EXPECT_TRUE(ReferenceChainsTestAccessor::urgencyBudgetBoostedForTest()) + << "the episode still opens with no search running"; + EXPECT_EQ(std::min(configured_budget * 4, MAX_REFERENCE_CHAINS_BUDGET), + ReferenceChainsTestAccessor::budgetForTest()); + EXPECT_EQ(ReferenceChainsTestAccessor::urgentPauseTargetMsForTest(), + ReferenceChainsTestAccessor::effectivePauseTargetMsForTest()) + << "the pause-target ramp does not depend on the search state"; + + // Terminal, awaiting a restart. + ReferenceChainsTestAccessor::setSearchRunningForTest(false); + EXPECT_TRUE(ReferenceChainsTestAccessor::applyOomRampForTest(0.0)); + EXPECT_EQ(pass_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "terminal search: the urgent idle wake must hold the fallback cadence"; + + // Running: the ramp paces passes. + ReferenceChainsTestAccessor::setSearchRunningForTest(true); + EXPECT_TRUE(ReferenceChainsTestAccessor::applyOomRampForTest(0.0)); + EXPECT_EQ(urgent_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "a running search must ramp to the urgent cadence floor"; + + // The search ends mid-episode: the next urgent iteration drops back. + ReferenceChainsTestAccessor::setSearchRunningForTest(false); + EXPECT_TRUE(ReferenceChainsTestAccessor::applyOomRampForTest(0.0)); + EXPECT_EQ(pass_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) + << "a search ending mid-episode must return the idle wake to the fallback"; +} + // Outside an episode, a non-urgent iteration must not touch a cadence that // updatePacing() has adapted (2s here, past the fallback, to prove the write // path is not merely "leave PASS_CADENCE_NS alone"). Closing an episode is @@ -1179,6 +1237,8 @@ TEST_F(OomUrgencyTest, NonUrgentIterationKeepsPacedCadence) { ReferenceChainsTestAccessor::restoreOomRampStateForTest(saved); } } restore{ReferenceChainsTestAccessor::saveOomRampStateForTest()}; + // The cadence ramp paces passes, so it applies only while a search runs. + ReferenceChainsTestAccessor::setSearchRunningForTest(true); ReferenceChainsTestAccessor::setEffectiveCadenceNs(paced_ns); EXPECT_FALSE(ReferenceChainsTestAccessor::applyOomRampForTest(-1.0)); @@ -1216,6 +1276,8 @@ TEST_F(OomUrgencyTest, RampEdgeFlapRestoresAndRecomputesCadence) { ReferenceChainsTestAccessor::restoreOomRampStateForTest(saved); } } restore{ReferenceChainsTestAccessor::saveOomRampStateForTest()}; + // The cadence ramp paces passes, so it applies only while a search runs. + ReferenceChainsTestAccessor::setSearchRunningForTest(true); // Half the ceiling: the 4x boost must clamp at MAX_REFERENCE_CHAINS_BUDGET // on every episode open and restore the configured value on every close. @@ -1508,6 +1570,7 @@ TEST_F(ReferenceChainsCanaryRefillTest, StopClosesOpenUrgencyEpisode) { << "applyOomRampForTest() would race threadLoop()"; EXPECT_EQ(pass_ns, ReferenceChainsTestAccessor::effectiveCadenceNs()) << "start() reseeds the cadence"; + ReferenceChainsTestAccessor::setSearchRunningForTest(true); ASSERT_TRUE(ReferenceChainsTestAccessor::applyOomRampForTest(0.0)); ASSERT_TRUE(ReferenceChainsTestAccessor::urgencyBudgetBoostedForTest()); ASSERT_EQ(std::min(configured * 4, MAX_REFERENCE_CHAINS_BUDGET), diff --git a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h index fc4c9af746..37082defe2 100644 --- a/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h +++ b/ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h @@ -597,9 +597,9 @@ class ReferenceChainsTestAccessor { } // Every scheduling field an urgency episode (applyOomRamp()) mutates or a test seeds to drive - // it - cadence, budget + boost flag, configured budget, configured/effective pause target and - // the pause PidController - so a test can restore the singleton exactly as it found it. - // Field order is fixed for aggregate init. + // it - cadence, budget + boost flag, configured budget, configured/effective pause target, the + // pause PidController and the search-running state the cadence ramp keys on - so a test can + // restore the singleton exactly as it found it. Field order is fixed for aggregate init. struct OomRampState { u64 cadence_ns; int budget; @@ -608,12 +608,14 @@ class ReferenceChainsTestAccessor { long pause_target_ms; long effective_pause_target_ms; PidController pause_pid; + bool search_started; + u8 search_state; }; static OomRampState saveOomRampStateForTest() { ReferenceChainTracker *t = ReferenceChainTracker::instance(); return {t->_effective_cadence_ns, t->_budget, t->_urgency_budget_boosted, t->_configured_budget, t->_pause_target_ms, t->_effective_pause_target_ms, - t->_pause_pid}; + t->_pause_pid, t->_search_started, t->_search_state}; } static void restoreOomRampStateForTest(const OomRampState &state) { ReferenceChainTracker *t = ReferenceChainTracker::instance(); @@ -624,6 +626,21 @@ class ReferenceChainsTestAccessor { t->_pause_target_ms = state.pause_target_ms; t->_effective_pause_target_ms = state.effective_pause_target_ms; t->_pause_pid = state.pause_pid; + t->_search_started = state.search_started; + t->_search_state = state.search_state; + } + // applyOomRamp() ramps the cadence only while a search is running: seeds a started RUNNING + // search (running) or a started COMPLETED one awaiting a restart (!running). + static void setSearchRunningForTest(bool running) { + ReferenceChainTracker *t = ReferenceChainTracker::instance(); + t->_search_started = true; + t->_search_state = running ? SearchState::RUNNING : SearchState::COMPLETED; + } + // The state restartSearch() and a fresh tracker leave: not started, RUNNING. + static void setSearchNotStartedForTest() { + ReferenceChainTracker *t = ReferenceChainTracker::instance(); + t->_search_started = false; + t->_search_state = SearchState::RUNNING; } // Urgency-episode budget state (applyOomRamp()'s boost/restore, stop()'s closer). diff --git a/doc/architecture/ReferenceChainsSignalsExplained.md b/doc/architecture/ReferenceChainsSignalsExplained.md index 6f7f6ec9ec..80f731cd36 100644 --- a/doc/architecture/ReferenceChainsSignalsExplained.md +++ b/doc/architecture/ReferenceChainsSignalsExplained.md @@ -129,7 +129,7 @@ The urgent latch clears after five consecutive checks at 10 minutes or more, or The ramp and the latch use different thresholds. Entering the ramp does not by itself latch urgency. -The ramp shortens both the idle sleep and the fallback cadence gate. It runs from one second (`PASS_CADENCE_NS`) at 30 minutes out to 10 milliseconds (`URGENT_CADENCE_NS`) at OOM, following `cadence = 1 s × 0.01^(1 − s/1800)` for a projection of s seconds. This allows up to about 100 loop evaluations per second, not counting evaluation time. At the 5-minute boundary the ramp gives about 21.5 milliseconds, about 46 evaluations per second. These values follow from the formula; execution time per evaluation is additional, and none of them is a guarantee. +The cadence ramp applies only while a search is running. It shortens both the sleep between passes and the fallback cadence gate. It runs from one second (`PASS_CADENCE_NS`) at 30 minutes out to 10 milliseconds (`URGENT_CADENCE_NS`) at OOM, following `cadence = 1 s × 0.01^(1 − s/1800)` for a projection of s seconds. This allows up to about 100 loop evaluations per second, not counting evaluation time. With no search running (not started, or terminal and waiting for a restart), an urgent iteration keeps the cadence at `PASS_CADENCE_NS`. Such a wake runs no pass, only the restart gate's `selectLeakCandidates()` probe, so a ramped idle wake would cost up to about 100 probes per second for at most one second of faster candidate discovery. The budget boost and the pause-target ramp do not depend on the search state. At the 5-minute boundary the ramp gives about 21.5 milliseconds, about 46 evaluations per second. These values follow from the formula; execution time per evaluation is additional, and none of them is a guarantee. ## Terminal state and restart