From 6e5bfcc6450512b4b974ed4afa067547552a0847 Mon Sep 17 00:00:00 2001 From: Thomas Schatzl Date: Tue, 7 Jul 2026 12:54:47 +0000 Subject: [PATCH] 8387764: G1: Let G1CollectedHeap::_summary_bytes_used use the Atomic API Reviewed-by: aboldtch, shade --- src/hotspot/share/gc/g1/g1CollectedHeap.cpp | 15 +++++++-------- src/hotspot/share/gc/g1/g1CollectedHeap.hpp | 2 +- src/hotspot/share/gc/g1/vmStructs_g1.hpp | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/hotspot/share/gc/g1/g1CollectedHeap.cpp b/src/hotspot/share/gc/g1/g1CollectedHeap.cpp index 9dfdb376905..3c41133e572 100644 --- a/src/hotspot/share/gc/g1/g1CollectedHeap.cpp +++ b/src/hotspot/share/gc/g1/g1CollectedHeap.cpp @@ -1761,12 +1761,11 @@ size_t G1CollectedHeap::unused_committed_regions_in_bytes() const { // Computes the sum of the storage used by the various regions. size_t G1CollectedHeap::used() const { - size_t result = _summary_bytes_used + _allocator->used_in_alloc_regions(); - return result; + return used_unlocked() + _allocator->used_in_alloc_regions(); } size_t G1CollectedHeap::used_unlocked() const { - return _summary_bytes_used; + return _summary_bytes_used.load_relaxed(); } class SumUsedClosure: public G1HeapRegionClosure { @@ -3034,18 +3033,18 @@ void G1CollectedHeap::prepare_region_for_full_compaction(G1HeapRegion* hr) { } void G1CollectedHeap::increase_used(size_t bytes) { - _summary_bytes_used += bytes; + _summary_bytes_used.add_then_fetch(bytes, memory_order_relaxed); } void G1CollectedHeap::decrease_used(size_t bytes) { - assert(_summary_bytes_used >= bytes, + assert(used_unlocked() >= bytes, "invariant: _summary_bytes_used: %zu should be >= bytes: %zu", - _summary_bytes_used, bytes); - _summary_bytes_used -= bytes; + used_unlocked(), bytes); + _summary_bytes_used.sub_then_fetch(bytes, memory_order_relaxed); } void G1CollectedHeap::set_used(size_t bytes) { - _summary_bytes_used = bytes; + _summary_bytes_used.store_relaxed(bytes); } class RebuildRegionSetsClosure : public G1HeapRegionClosure { diff --git a/src/hotspot/share/gc/g1/g1CollectedHeap.hpp b/src/hotspot/share/gc/g1/g1CollectedHeap.hpp index 718c230851f..cb466a5e120 100644 --- a/src/hotspot/share/gc/g1/g1CollectedHeap.hpp +++ b/src/hotspot/share/gc/g1/g1CollectedHeap.hpp @@ -243,7 +243,7 @@ private: // Outside of GC pauses, the number of bytes used in all regions other // than the current allocation region(s). - volatile size_t _summary_bytes_used; + Atomic _summary_bytes_used; void increase_used(size_t bytes); void decrease_used(size_t bytes); diff --git a/src/hotspot/share/gc/g1/vmStructs_g1.hpp b/src/hotspot/share/gc/g1/vmStructs_g1.hpp index af236ec8581..e0179b69646 100644 --- a/src/hotspot/share/gc/g1/vmStructs_g1.hpp +++ b/src/hotspot/share/gc/g1/vmStructs_g1.hpp @@ -42,7 +42,7 @@ nonstatic_field(G1HeapRegion, _bottom, HeapWord* const) \ nonstatic_field(G1HeapRegion, _top, Atomic) \ nonstatic_field(G1HeapRegion, _end, HeapWord* const) \ - volatile_nonstatic_field(G1HeapRegion, _pinned_object_count, Atomic)\ + nonstatic_field(G1HeapRegion, _pinned_object_count, Atomic) \ \ nonstatic_field(G1HeapRegionType, _tag, G1HeapRegionType::Tag volatile) \ \ @@ -55,7 +55,7 @@ \ nonstatic_field(G1HeapRegionManager, _regions, G1HeapRegionTable) \ \ - volatile_nonstatic_field(G1CollectedHeap, _summary_bytes_used, size_t) \ + nonstatic_field(G1CollectedHeap, _summary_bytes_used, Atomic) \ nonstatic_field(G1CollectedHeap, _hrm, G1HeapRegionManager) \ nonstatic_field(G1CollectedHeap, _monitoring_support, G1MonitoringSupport*) \ nonstatic_field(G1CollectedHeap, _old_set, G1HeapRegionSetBase) \