Skip to content

Commit 09b25cd

Browse files
committed
8371465: Parallel: Revise asserts around heap expansion
Reviewed-by: aboldtch, tschatzl
1 parent 69e3024 commit 09b25cd

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

src/hotspot/share/gc/parallel/mutableSpace.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,6 @@ bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) {
180180
return AtomicAccess::cmpxchg(top_addr(), expected_top, obj) == expected_top;
181181
}
182182

183-
// Only used by oldgen allocation.
184-
bool MutableSpace::needs_expand(size_t word_size) const {
185-
// This method can be invoked either outside of safepoint by java threads or
186-
// in safepoint by gc workers. Such accesses are synchronized by holding one
187-
// of the following locks.
188-
assert(Heap_lock->is_locked() || PSOldGenExpand_lock->is_locked(), "precondition");
189-
190-
// Holding the lock means end is stable. So while top may be advancing
191-
// via concurrent allocations, there is no need to order the reads of top
192-
// and end here, unlike in cas_allocate.
193-
return pointer_delta(end(), top()) < word_size;
194-
}
195-
196183
void MutableSpace::oop_iterate(OopIterateClosure* cl) {
197184
HeapWord* obj_addr = bottom();
198185
HeapWord* t = top();

src/hotspot/share/gc/parallel/mutableSpace.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ class MutableSpace: public CHeapObj<mtGC> {
127127
virtual HeapWord* cas_allocate(size_t word_size);
128128
// Optional deallocation. Used in NUMA-allocator.
129129
bool cas_deallocate(HeapWord *obj, size_t size);
130-
// Return true if this space needs to be expanded in order to satisfy an
131-
// allocation request of the indicated size. Concurrent allocations and
132-
// resizes may change the result of a later call. Used by oldgen allocator.
133-
// precondition: holding PSOldGenExpand_lock if not VM thread
134-
bool needs_expand(size_t word_size) const;
135130

136131
// Iteration.
137132
void oop_iterate(OopIterateClosure* cl);

src/hotspot/share/gc/parallel/parallelScavengeHeap.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,16 @@ bool ParallelScavengeHeap::check_gc_overhead_limit() {
403403
}
404404

405405
HeapWord* ParallelScavengeHeap::expand_heap_and_allocate(size_t size, bool is_tlab) {
406+
#ifdef ASSERT
406407
assert(Heap_lock->is_locked(), "precondition");
408+
if (is_init_completed()) {
409+
assert(SafepointSynchronize::is_at_safepoint(), "precondition");
410+
assert(Thread::current()->is_VM_thread(), "precondition");
411+
} else {
412+
assert(Thread::current()->is_Java_thread(), "precondition");
413+
assert(Heap_lock->owned_by_self(), "precondition");
414+
}
415+
#endif
407416

408417
HeapWord* result = young_gen()->expand_and_allocate(size);
409418

src/hotspot/share/gc/parallel/psOldGen.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "gc/shared/spaceDecorator.hpp"
3434
#include "logging/log.hpp"
3535
#include "oops/oop.inline.hpp"
36+
#include "runtime/init.hpp"
3637
#include "runtime/java.hpp"
3738
#include "utilities/align.hpp"
3839

@@ -118,13 +119,22 @@ void PSOldGen::initialize_performance_counters() {
118119
}
119120

120121
HeapWord* PSOldGen::expand_and_allocate(size_t word_size) {
122+
#ifdef ASSERT
121123
assert(Heap_lock->is_locked(), "precondition");
124+
if (is_init_completed()) {
125+
assert(SafepointSynchronize::is_at_safepoint(), "precondition");
126+
assert(Thread::current()->is_VM_thread(), "precondition");
127+
} else {
128+
assert(Thread::current()->is_Java_thread(), "precondition");
129+
assert(Heap_lock->owned_by_self(), "precondition");
130+
}
131+
#endif
122132

123-
if (object_space()->needs_expand(word_size)) {
133+
if (pointer_delta(object_space()->end(), object_space()->top()) < word_size) {
124134
expand(word_size*HeapWordSize);
125135
}
126136

127-
// Reuse the CAS API even though this is VM thread in safepoint. This method
137+
// Reuse the CAS API even though this is in a critical section. This method
128138
// is not invoked repeatedly, so the CAS overhead should be negligible.
129139
return cas_allocate_noexpand(word_size);
130140
}
@@ -168,7 +178,7 @@ bool PSOldGen::expand_for_allocate(size_t word_size) {
168178
// true until we expand, since we have the lock. Other threads may take
169179
// the space we need before we can allocate it, regardless of whether we
170180
// expand. That's okay, we'll just try expanding again.
171-
if (object_space()->needs_expand(word_size)) {
181+
if (pointer_delta(object_space()->end(), object_space()->top()) < word_size) {
172182
result = expand(word_size*HeapWordSize);
173183
}
174184
}
@@ -192,10 +202,21 @@ void PSOldGen::try_expand_till_size(size_t target_capacity_bytes) {
192202

193203
bool PSOldGen::expand(size_t bytes) {
194204
#ifdef ASSERT
195-
if (!Thread::current()->is_VM_thread()) {
196-
assert_lock_strong(PSOldGenExpand_lock);
205+
// During startup (is_init_completed() == false), expansion can occur for
206+
// 1. java-threads invoking heap-allocation (using Heap_lock)
207+
// 2. CDS construction by a single thread (using PSOldGenExpand_lock but not needed)
208+
//
209+
// After startup (is_init_completed() == true), expansion can occur for
210+
// 1. GC workers for promoting to old-gen (using PSOldGenExpand_lock)
211+
// 2. VM thread to satisfy the pending allocation
212+
// Both cases are inside safepoint pause, but are never overlapping.
213+
//
214+
if (is_init_completed()) {
215+
assert(SafepointSynchronize::is_at_safepoint(), "precondition");
216+
assert(Thread::current()->is_VM_thread() || PSOldGenExpand_lock->owned_by_self(), "precondition");
217+
} else {
218+
assert(Heap_lock->owned_by_self() || PSOldGenExpand_lock->owned_by_self(), "precondition");
197219
}
198-
assert_locked_or_safepoint(Heap_lock);
199220
assert(bytes > 0, "precondition");
200221
#endif
201222
const size_t remaining_bytes = virtual_space()->uncommitted_size();

0 commit comments

Comments
 (0)