From 976207df1fcebf76a5f732b26424d6a4896b359e Mon Sep 17 00:00:00 2001 From: Francesco Andreuzzi Date: Wed, 17 Sep 2025 08:26:31 +0000 Subject: [PATCH] 8367476: Shenandoah: Remove use of CollectedHeap::_soft_ref_policy Reviewed-by: ayang, wkemper --- src/hotspot/share/gc/epsilon/epsilonHeap.hpp | 1 - src/hotspot/share/gc/g1/g1CollectedHeap.hpp | 1 - .../gc/parallel/parallelScavengeHeap.cpp | 7 +--- .../gc/parallel/parallelScavengeHeap.hpp | 2 - src/hotspot/share/gc/serial/serialHeap.cpp | 7 +--- src/hotspot/share/gc/serial/serialHeap.hpp | 5 --- src/hotspot/share/gc/shared/collectedHeap.cpp | 1 - src/hotspot/share/gc/shared/collectedHeap.hpp | 6 --- src/hotspot/share/gc/shared/gcCause.hpp | 7 ++++ .../share/gc/shared/gcVMOperations.cpp | 1 - src/hotspot/share/gc/shared/softRefPolicy.hpp | 42 ------------------- .../gc/shenandoah/shenandoahConcurrentGC.cpp | 3 +- .../gc/shenandoah/shenandoahControlThread.cpp | 5 ++- .../shenandoahGenerationalControlThread.cpp | 8 ++-- .../share/gc/shenandoah/shenandoahHeap.hpp | 1 - .../shenandoahReferenceProcessor.cpp | 9 ++-- .../shenandoahReferenceProcessor.hpp | 2 + .../share/gc/shenandoah/shenandoahSTWMark.cpp | 1 - src/hotspot/share/gc/z/zCollectedHeap.hpp | 1 - src/hotspot/share/prims/whitebox.cpp | 1 - 20 files changed, 26 insertions(+), 85 deletions(-) delete mode 100644 src/hotspot/share/gc/shared/softRefPolicy.hpp diff --git a/src/hotspot/share/gc/epsilon/epsilonHeap.hpp b/src/hotspot/share/gc/epsilon/epsilonHeap.hpp index f8aa9d7dbf1..e23e24a5afc 100644 --- a/src/hotspot/share/gc/epsilon/epsilonHeap.hpp +++ b/src/hotspot/share/gc/epsilon/epsilonHeap.hpp @@ -29,7 +29,6 @@ #include "gc/epsilon/epsilonBarrierSet.hpp" #include "gc/epsilon/epsilonMonitoringSupport.hpp" #include "gc/shared/collectedHeap.hpp" -#include "gc/shared/softRefPolicy.hpp" #include "gc/shared/space.hpp" #include "memory/virtualspace.hpp" #include "services/memoryManager.hpp" diff --git a/src/hotspot/share/gc/g1/g1CollectedHeap.hpp b/src/hotspot/share/gc/g1/g1CollectedHeap.hpp index 0bb16edaf78..8d26bcb1c0b 100644 --- a/src/hotspot/share/gc/g1/g1CollectedHeap.hpp +++ b/src/hotspot/share/gc/g1/g1CollectedHeap.hpp @@ -50,7 +50,6 @@ #include "gc/shared/collectedHeap.hpp" #include "gc/shared/gcHeapSummary.hpp" #include "gc/shared/plab.hpp" -#include "gc/shared/softRefPolicy.hpp" #include "gc/shared/taskqueue.hpp" #include "memory/allocation.hpp" #include "memory/iterator.hpp" diff --git a/src/hotspot/share/gc/parallel/parallelScavengeHeap.cpp b/src/hotspot/share/gc/parallel/parallelScavengeHeap.cpp index 21841330fa7..81412e2f614 100644 --- a/src/hotspot/share/gc/parallel/parallelScavengeHeap.cpp +++ b/src/hotspot/share/gc/parallel/parallelScavengeHeap.cpp @@ -469,14 +469,9 @@ void ParallelScavengeHeap::collect(GCCause::Cause cause) { VMThread::execute(&op); } -bool ParallelScavengeHeap::must_clear_all_soft_refs() { - return _gc_cause == GCCause::_metadata_GC_clear_soft_refs || - _gc_cause == GCCause::_wb_full_gc; -} - void ParallelScavengeHeap::collect_at_safepoint(bool full) { assert(!GCLocker::is_active(), "precondition"); - bool clear_soft_refs = must_clear_all_soft_refs(); + bool clear_soft_refs = GCCause::should_clear_all_soft_refs(_gc_cause); if (!full) { bool success = PSScavenge::invoke(clear_soft_refs); diff --git a/src/hotspot/share/gc/parallel/parallelScavengeHeap.hpp b/src/hotspot/share/gc/parallel/parallelScavengeHeap.hpp index bd701ae8be3..b1176a1637b 100644 --- a/src/hotspot/share/gc/parallel/parallelScavengeHeap.hpp +++ b/src/hotspot/share/gc/parallel/parallelScavengeHeap.hpp @@ -96,8 +96,6 @@ class ParallelScavengeHeap : public CollectedHeap { void update_parallel_worker_threads_cpu_time(); - bool must_clear_all_soft_refs(); - HeapWord* allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size) override; inline bool should_alloc_in_eden(size_t size) const; diff --git a/src/hotspot/share/gc/serial/serialHeap.cpp b/src/hotspot/share/gc/serial/serialHeap.cpp index 662a6be695b..e019144d628 100644 --- a/src/hotspot/share/gc/serial/serialHeap.cpp +++ b/src/hotspot/share/gc/serial/serialHeap.cpp @@ -340,11 +340,6 @@ HeapWord* SerialHeap::mem_allocate(size_t size) { false /* is_tlab */); } -bool SerialHeap::must_clear_all_soft_refs() { - return _gc_cause == GCCause::_metadata_GC_clear_soft_refs || - _gc_cause == GCCause::_wb_full_gc; -} - bool SerialHeap::is_young_gc_safe() const { if (!_young_gen->to()->is_empty()) { return false; @@ -497,7 +492,7 @@ void SerialHeap::scan_evacuated_objs(YoungGenScanClosure* young_cl, void SerialHeap::collect_at_safepoint(bool full) { assert(!GCLocker::is_active(), "precondition"); - bool clear_soft_refs = must_clear_all_soft_refs(); + bool clear_soft_refs = GCCause::should_clear_all_soft_refs(_gc_cause); if (!full) { bool success = do_young_collection(clear_soft_refs); diff --git a/src/hotspot/share/gc/serial/serialHeap.hpp b/src/hotspot/share/gc/serial/serialHeap.hpp index 72778981eee..86fb286f33f 100644 --- a/src/hotspot/share/gc/serial/serialHeap.hpp +++ b/src/hotspot/share/gc/serial/serialHeap.hpp @@ -31,7 +31,6 @@ #include "gc/shared/collectedHeap.hpp" #include "gc/shared/oopStorageParState.hpp" #include "gc/shared/preGCValues.hpp" -#include "gc/shared/softRefPolicy.hpp" #include "utilities/growableArray.hpp" class CardTableRS; @@ -104,10 +103,6 @@ private: void do_full_collection(bool clear_all_soft_refs) override; - // Does the "cause" of GC indicate that - // we absolutely __must__ clear soft refs? - bool must_clear_all_soft_refs(); - bool is_young_gc_safe() const; void gc_prologue(); diff --git a/src/hotspot/share/gc/shared/collectedHeap.cpp b/src/hotspot/share/gc/shared/collectedHeap.cpp index 71017817d14..9b6956ca75a 100644 --- a/src/hotspot/share/gc/shared/collectedHeap.cpp +++ b/src/hotspot/share/gc/shared/collectedHeap.cpp @@ -276,7 +276,6 @@ bool CollectedHeap::is_oop(oop object) const { CollectedHeap::CollectedHeap() : _capacity_at_last_gc(0), _used_at_last_gc(0), - _soft_ref_policy(), _is_stw_gc_active(false), _last_whole_heap_examined_time_ns(os::javaTimeNanos()), _total_collections(0), diff --git a/src/hotspot/share/gc/shared/collectedHeap.hpp b/src/hotspot/share/gc/shared/collectedHeap.hpp index 57bd9316731..f4f5ce79074 100644 --- a/src/hotspot/share/gc/shared/collectedHeap.hpp +++ b/src/hotspot/share/gc/shared/collectedHeap.hpp @@ -27,7 +27,6 @@ #include "gc/shared/gcCause.hpp" #include "gc/shared/gcWhen.hpp" -#include "gc/shared/softRefPolicy.hpp" #include "gc/shared/verifyOption.hpp" #include "memory/allocation.hpp" #include "memory/metaspace.hpp" @@ -104,8 +103,6 @@ class CollectedHeap : public CHeapObj { size_t _capacity_at_last_gc; size_t _used_at_last_gc; - SoftRefPolicy _soft_ref_policy; - // First, set it to java_lang_Object. // Then, set it to FillerObject after the FillerObject_klass loading is complete. static Klass* _filler_object_klass; @@ -395,9 +392,6 @@ protected: } } - // Return the SoftRefPolicy for the heap; - SoftRefPolicy* soft_ref_policy() { return &_soft_ref_policy; } - virtual MemoryUsage memory_usage(); virtual GrowableArray memory_managers() = 0; virtual GrowableArray memory_pools() = 0; diff --git a/src/hotspot/share/gc/shared/gcCause.hpp b/src/hotspot/share/gc/shared/gcCause.hpp index f775d41340d..aac4b801b74 100644 --- a/src/hotspot/share/gc/shared/gcCause.hpp +++ b/src/hotspot/share/gc/shared/gcCause.hpp @@ -103,6 +103,13 @@ class GCCause : public AllStatic { cause == _codecache_GC_aggressive); } + // Does the "cause" of GC indicate that + // we absolutely __must__ clear soft refs? + inline static bool should_clear_all_soft_refs(GCCause::Cause cause) { + return cause == GCCause::_metadata_GC_clear_soft_refs || + cause == GCCause::_wb_full_gc; + } + // Return a string describing the GCCause. static const char* to_string(GCCause::Cause cause); }; diff --git a/src/hotspot/share/gc/shared/gcVMOperations.cpp b/src/hotspot/share/gc/shared/gcVMOperations.cpp index 1299f64995f..97bae4f6d44 100644 --- a/src/hotspot/share/gc/shared/gcVMOperations.cpp +++ b/src/hotspot/share/gc/shared/gcVMOperations.cpp @@ -29,7 +29,6 @@ #include "gc/shared/gcId.hpp" #include "gc/shared/gcLocker.hpp" #include "gc/shared/gcVMOperations.hpp" -#include "gc/shared/softRefPolicy.hpp" #include "interpreter/oopMapCache.hpp" #include "logging/log.hpp" #include "memory/classLoaderMetaspace.hpp" diff --git a/src/hotspot/share/gc/shared/softRefPolicy.hpp b/src/hotspot/share/gc/shared/softRefPolicy.hpp deleted file mode 100644 index 1e8ec356c40..00000000000 --- a/src/hotspot/share/gc/shared/softRefPolicy.hpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - * - */ - -#ifndef SHARE_GC_SHARED_SOFTREFPOLICY_HPP -#define SHARE_GC_SHARED_SOFTREFPOLICY_HPP - -class SoftRefPolicy { - private: - // Set to true when policy wants soft refs cleared. - // Reset to false by gc after it clears all soft refs. - bool _should_clear_all_soft_refs; - - public: - SoftRefPolicy() : - _should_clear_all_soft_refs(false) {} - - bool should_clear_all_soft_refs() { return _should_clear_all_soft_refs; } - void set_should_clear_all_soft_refs(bool v) { _should_clear_all_soft_refs = v; } -}; - -#endif // SHARE_GC_SHARED_SOFTREFPOLICY_HPP diff --git a/src/hotspot/share/gc/shenandoah/shenandoahConcurrentGC.cpp b/src/hotspot/share/gc/shenandoah/shenandoahConcurrentGC.cpp index 111e0073470..6d3b93ac406 100644 --- a/src/hotspot/share/gc/shenandoah/shenandoahConcurrentGC.cpp +++ b/src/hotspot/share/gc/shenandoah/shenandoahConcurrentGC.cpp @@ -114,6 +114,8 @@ void ShenandoahConcurrentGC::entry_concurrent_update_refs_prepare(ShenandoahHeap bool ShenandoahConcurrentGC::collect(GCCause::Cause cause) { ShenandoahHeap* const heap = ShenandoahHeap::heap(); + _generation->ref_processor()->set_soft_reference_policy( + GCCause::should_clear_all_soft_refs(cause)); ShenandoahBreakpointGCScope breakpoint_gc_scope(cause); @@ -732,7 +734,6 @@ void ShenandoahConcurrentGC::op_init_mark() { // Weak reference processing ShenandoahReferenceProcessor* rp = _generation->ref_processor(); rp->reset_thread_locals(); - rp->set_soft_reference_policy(heap->soft_ref_policy()->should_clear_all_soft_refs()); // Make above changes visible to worker threads OrderAccess::fence(); diff --git a/src/hotspot/share/gc/shenandoah/shenandoahControlThread.cpp b/src/hotspot/share/gc/shenandoah/shenandoahControlThread.cpp index 6290101bc49..80e5b709ada 100644 --- a/src/hotspot/share/gc/shenandoah/shenandoahControlThread.cpp +++ b/src/hotspot/share/gc/shenandoah/shenandoahControlThread.cpp @@ -34,6 +34,7 @@ #include "gc/shenandoah/shenandoahGeneration.hpp" #include "gc/shenandoah/shenandoahHeap.inline.hpp" #include "gc/shenandoah/shenandoahMonitoringSupport.hpp" +#include "gc/shenandoah/shenandoahReferenceProcessor.hpp" #include "gc/shenandoah/shenandoahUtils.hpp" #include "logging/log.hpp" #include "memory/metaspaceStats.hpp" @@ -118,7 +119,7 @@ void ShenandoahControlThread::run_service() { // Blow all soft references on this cycle, if handling allocation failure, // either implicit or explicit GC request, or we are requested to do so unconditionally. if (alloc_failure_pending || is_gc_requested || ShenandoahAlwaysClearSoftRefs) { - heap->soft_ref_policy()->set_should_clear_all_soft_refs(true); + heap->global_generation()->ref_processor()->set_soft_reference_policy(true); } const bool gc_requested = (mode != none); @@ -193,7 +194,7 @@ void ShenandoahControlThread::run_service() { heap->set_forced_counters_update(false); // Retract forceful part of soft refs policy - heap->soft_ref_policy()->set_should_clear_all_soft_refs(false); + heap->global_generation()->ref_processor()->set_soft_reference_policy(false); // Clear metaspace oom flag, if current cycle unloaded classes if (heap->unload_classes()) { diff --git a/src/hotspot/share/gc/shenandoah/shenandoahGenerationalControlThread.cpp b/src/hotspot/share/gc/shenandoah/shenandoahGenerationalControlThread.cpp index ce8d96308ba..761ba02d569 100644 --- a/src/hotspot/share/gc/shenandoah/shenandoahGenerationalControlThread.cpp +++ b/src/hotspot/share/gc/shenandoah/shenandoahGenerationalControlThread.cpp @@ -37,6 +37,7 @@ #include "gc/shenandoah/shenandoahMonitoringSupport.hpp" #include "gc/shenandoah/shenandoahOldGC.hpp" #include "gc/shenandoah/shenandoahOldGeneration.hpp" +#include "gc/shenandoah/shenandoahReferenceProcessor.hpp" #include "gc/shenandoah/shenandoahUtils.hpp" #include "gc/shenandoah/shenandoahYoungGeneration.hpp" #include "logging/log.hpp" @@ -216,8 +217,9 @@ void ShenandoahGenerationalControlThread::run_gc_cycle(const ShenandoahGCRequest // Blow away all soft references on this cycle, if handling allocation failure, // either implicit or explicit GC request, or we are requested to do so unconditionally. - if (request.generation->is_global() && (ShenandoahCollectorPolicy::is_allocation_failure(request.cause) || ShenandoahCollectorPolicy::is_explicit_gc(request.cause) || ShenandoahAlwaysClearSoftRefs)) { - _heap->soft_ref_policy()->set_should_clear_all_soft_refs(true); + if (GCCause::should_clear_all_soft_refs(request.cause) || (request.generation->is_global() && + (ShenandoahCollectorPolicy::is_allocation_failure(request.cause) || ShenandoahCollectorPolicy::is_explicit_gc(request.cause) || ShenandoahAlwaysClearSoftRefs))) { + request.generation->ref_processor()->set_soft_reference_policy(true); } // GC is starting, bump the internal ID @@ -289,7 +291,7 @@ void ShenandoahGenerationalControlThread::run_gc_cycle(const ShenandoahGCRequest _heap->set_forced_counters_update(false); // Retract forceful part of soft refs policy - _heap->soft_ref_policy()->set_should_clear_all_soft_refs(false); + request.generation->ref_processor()->set_soft_reference_policy(false); // Clear metaspace oom flag, if current cycle unloaded classes if (_heap->unload_classes()) { diff --git a/src/hotspot/share/gc/shenandoah/shenandoahHeap.hpp b/src/hotspot/share/gc/shenandoah/shenandoahHeap.hpp index eafd1b28b3a..322ac26e254 100644 --- a/src/hotspot/share/gc/shenandoah/shenandoahHeap.hpp +++ b/src/hotspot/share/gc/shenandoah/shenandoahHeap.hpp @@ -29,7 +29,6 @@ #include "gc/shared/collectedHeap.hpp" #include "gc/shared/markBitMap.hpp" -#include "gc/shared/softRefPolicy.hpp" #include "gc/shenandoah/mode/shenandoahMode.hpp" #include "gc/shenandoah/shenandoahAllocRequest.hpp" #include "gc/shenandoah/shenandoahAsserts.hpp" diff --git a/src/hotspot/share/gc/shenandoah/shenandoahReferenceProcessor.cpp b/src/hotspot/share/gc/shenandoah/shenandoahReferenceProcessor.cpp index f8726386b5d..4ca6f2fdf49 100644 --- a/src/hotspot/share/gc/shenandoah/shenandoahReferenceProcessor.cpp +++ b/src/hotspot/share/gc/shenandoah/shenandoahReferenceProcessor.cpp @@ -221,8 +221,10 @@ void ShenandoahRefProcThreadLocal::set_discovered_list_head(oop head) { *discovered_list_addr() = head; } +AlwaysClearPolicy ShenandoahReferenceProcessor::_always_clear_policy; + ShenandoahReferenceProcessor::ShenandoahReferenceProcessor(uint max_workers) : - _soft_reference_policy(nullptr), + _soft_reference_policy(&_always_clear_policy), _ref_proc_thread_locals(NEW_C_HEAP_ARRAY(ShenandoahRefProcThreadLocal, max_workers, mtGC)), _pending_list(nullptr), _pending_list_tail(&_pending_list), @@ -245,12 +247,11 @@ void ShenandoahReferenceProcessor::set_mark_closure(uint worker_id, ShenandoahMa } void ShenandoahReferenceProcessor::set_soft_reference_policy(bool clear) { - static AlwaysClearPolicy always_clear_policy; static LRUMaxHeapPolicy lru_max_heap_policy; if (clear) { log_info(gc, ref)("Clearing All SoftReferences"); - _soft_reference_policy = &always_clear_policy; + _soft_reference_policy = &_always_clear_policy; } else { _soft_reference_policy = &lru_max_heap_policy; } @@ -284,7 +285,7 @@ bool ShenandoahReferenceProcessor::is_softly_live(oop reference, ReferenceType t // Ask SoftReference policy const jlong clock = java_lang_ref_SoftReference::clock(); assert(clock != 0, "Clock not initialized"); - assert(_soft_reference_policy != nullptr, "Policy not initialized"); + assert(_soft_reference_policy != nullptr, "Should never be null"); return !_soft_reference_policy->should_clear_reference(reference, clock); } diff --git a/src/hotspot/share/gc/shenandoah/shenandoahReferenceProcessor.hpp b/src/hotspot/share/gc/shenandoah/shenandoahReferenceProcessor.hpp index 682c4268754..11099f1303d 100644 --- a/src/hotspot/share/gc/shenandoah/shenandoahReferenceProcessor.hpp +++ b/src/hotspot/share/gc/shenandoah/shenandoahReferenceProcessor.hpp @@ -127,6 +127,8 @@ public: class ShenandoahReferenceProcessor : public ReferenceDiscoverer { private: + static AlwaysClearPolicy _always_clear_policy; + ReferencePolicy* _soft_reference_policy; ShenandoahRefProcThreadLocal* _ref_proc_thread_locals; diff --git a/src/hotspot/share/gc/shenandoah/shenandoahSTWMark.cpp b/src/hotspot/share/gc/shenandoah/shenandoahSTWMark.cpp index c2bfea664fd..260c1e0276f 100644 --- a/src/hotspot/share/gc/shenandoah/shenandoahSTWMark.cpp +++ b/src/hotspot/share/gc/shenandoah/shenandoahSTWMark.cpp @@ -77,7 +77,6 @@ void ShenandoahSTWMark::mark() { ShenandoahReferenceProcessor* rp = _generation->ref_processor(); shenandoah_assert_generations_reconciled(); rp->reset_thread_locals(); - rp->set_soft_reference_policy(heap->soft_ref_policy()->should_clear_all_soft_refs()); // Init mark, do not expect forwarded pointers in roots if (ShenandoahVerify) { diff --git a/src/hotspot/share/gc/z/zCollectedHeap.hpp b/src/hotspot/share/gc/z/zCollectedHeap.hpp index c124976c80f..bbcddec917f 100644 --- a/src/hotspot/share/gc/z/zCollectedHeap.hpp +++ b/src/hotspot/share/gc/z/zCollectedHeap.hpp @@ -25,7 +25,6 @@ #define SHARE_GC_Z_ZCOLLECTEDHEAP_HPP #include "gc/shared/collectedHeap.hpp" -#include "gc/shared/softRefPolicy.hpp" #include "gc/z/zBarrierSet.hpp" #include "gc/z/zHeap.hpp" #include "gc/z/zInitialize.hpp" diff --git a/src/hotspot/share/prims/whitebox.cpp b/src/hotspot/share/prims/whitebox.cpp index ce559d47b24..e8e873fcb66 100644 --- a/src/hotspot/share/prims/whitebox.cpp +++ b/src/hotspot/share/prims/whitebox.cpp @@ -1501,7 +1501,6 @@ WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString WB_END WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o)) - Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(true); Universe::heap()->collect(GCCause::_wb_full_gc); WB_END