Skip to content

Commit 54893dc

Browse files
committed
8371985: Parallel: Move should_attempt_scavenge to ParallelScavengeHeap
Reviewed-by: fandreuzzi, iwalulya
1 parent 99135d2 commit 54893dc

File tree

4 files changed

+59
-68
lines changed

4 files changed

+59
-68
lines changed

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

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,55 @@ void ParallelScavengeHeap::do_full_collection(bool clear_all_soft_refs) {
370370
PSParallelCompact::invoke(clear_all_soft_refs, should_do_max_compaction);
371371
}
372372

373+
bool ParallelScavengeHeap::should_attempt_young_gc() const {
374+
const bool ShouldRunYoungGC = true;
375+
const bool ShouldRunFullGC = false;
376+
377+
if (!_young_gen->to_space()->is_empty()) {
378+
log_debug(gc, ergo)("To-space is not empty; run full-gc instead.");
379+
return ShouldRunFullGC;
380+
}
381+
382+
// Check if the predicted promoted bytes will overflow free space in old-gen.
383+
PSAdaptiveSizePolicy* policy = _size_policy;
384+
385+
size_t avg_promoted = (size_t) policy->padded_average_promoted_in_bytes();
386+
size_t promotion_estimate = MIN2(avg_promoted, _young_gen->used_in_bytes());
387+
// Total free size after possible old gen expansion
388+
size_t free_in_old_gen_with_expansion = _old_gen->max_gen_size() - _old_gen->used_in_bytes();
389+
390+
log_trace(gc, ergo)("average_promoted %zu; padded_average_promoted %zu",
391+
(size_t) policy->average_promoted_in_bytes(),
392+
(size_t) policy->padded_average_promoted_in_bytes());
393+
394+
if (promotion_estimate >= free_in_old_gen_with_expansion) {
395+
log_debug(gc, ergo)("Run full-gc; predicted promotion size >= max free space in old-gen: %zu >= %zu",
396+
promotion_estimate, free_in_old_gen_with_expansion);
397+
return ShouldRunFullGC;
398+
}
399+
400+
if (UseAdaptiveSizePolicy) {
401+
// Also checking OS has enough free memory to commit and expand old-gen.
402+
// Otherwise, the recorded gc-pause-time might be inflated to include time
403+
// of OS preparing free memory, resulting in inaccurate young-gen resizing.
404+
assert(_old_gen->committed().byte_size() >= _old_gen->used_in_bytes(), "inv");
405+
// Use uint64_t instead of size_t for 32bit compatibility.
406+
uint64_t free_mem_in_os;
407+
if (os::free_memory(free_mem_in_os)) {
408+
size_t actual_free = (size_t)MIN2(_old_gen->committed().byte_size() - _old_gen->used_in_bytes() + free_mem_in_os,
409+
(uint64_t)SIZE_MAX);
410+
if (promotion_estimate > actual_free) {
411+
log_debug(gc, ergo)("Run full-gc; predicted promotion size > free space in old-gen and OS: %zu > %zu",
412+
promotion_estimate, actual_free);
413+
return ShouldRunFullGC;
414+
}
415+
}
416+
}
417+
418+
// No particular reasons to run full-gc, so young-gc.
419+
return ShouldRunYoungGC;
420+
}
421+
373422
static bool check_gc_heap_free_limit(size_t free_bytes, size_t capacity_bytes) {
374423
return (free_bytes * 100 / capacity_bytes) < GCHeapFreeLimit;
375424
}
@@ -516,17 +565,18 @@ void ParallelScavengeHeap::collect(GCCause::Cause cause) {
516565
VMThread::execute(&op);
517566
}
518567

519-
void ParallelScavengeHeap::collect_at_safepoint(bool full) {
568+
void ParallelScavengeHeap::collect_at_safepoint(bool is_full) {
520569
assert(!GCLocker::is_active(), "precondition");
521570
bool clear_soft_refs = GCCause::should_clear_all_soft_refs(_gc_cause);
522571

523-
if (!full) {
524-
bool success = PSScavenge::invoke(clear_soft_refs);
525-
if (success) {
572+
if (!is_full && should_attempt_young_gc()) {
573+
bool young_gc_success = PSScavenge::invoke(clear_soft_refs);
574+
if (young_gc_success) {
526575
return;
527576
}
528-
// Upgrade to Full-GC if young-gc fails
577+
log_debug(gc, heap)("Upgrade to Full-GC since Young-gc failed.");
529578
}
579+
530580
const bool should_do_max_compaction = false;
531581
PSParallelCompact::invoke(clear_soft_refs, should_do_max_compaction);
532582
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ class ParallelScavengeHeap : public CollectedHeap {
119119
void print_tracing_info() const override;
120120
void stop() override {};
121121

122+
// Returns true if a young GC should be attempted, false if a full GC is preferred.
123+
bool should_attempt_young_gc() const;
124+
122125
public:
123126
ParallelScavengeHeap() :
124127
CollectedHeap(),

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

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,6 @@ bool PSScavenge::invoke(bool clear_soft_refs) {
313313
assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
314314
assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
315315

316-
// Check for potential problems.
317-
if (!should_attempt_scavenge()) {
318-
log_info(gc, ergo)("Young-gc might fail so skipping");
319-
return false;
320-
}
321-
322316
IsSTWGCActiveMark mark;
323317

324318
_gc_timer.register_gc_start();
@@ -336,8 +330,7 @@ bool PSScavenge::invoke(bool clear_soft_refs) {
336330
PSOldGen* old_gen = heap->old_gen();
337331
PSAdaptiveSizePolicy* size_policy = heap->size_policy();
338332

339-
assert(young_gen->to_space()->is_empty(),
340-
"Attempt to scavenge with live objects in to_space");
333+
assert(young_gen->to_space()->is_empty(), "precondition");
341334

342335
heap->increment_total_collections();
343336

@@ -520,59 +513,6 @@ void PSScavenge::clean_up_failed_promotion() {
520513
NOT_PRODUCT(ParallelScavengeHeap::heap()->reset_promotion_should_fail();)
521514
}
522515

523-
bool PSScavenge::should_attempt_scavenge() {
524-
const bool ShouldRunYoungGC = true;
525-
const bool ShouldRunFullGC = false;
526-
527-
ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
528-
PSYoungGen* young_gen = heap->young_gen();
529-
PSOldGen* old_gen = heap->old_gen();
530-
531-
if (!young_gen->to_space()->is_empty()) {
532-
log_debug(gc, ergo)("To-space is not empty; run full-gc instead.");
533-
return ShouldRunFullGC;
534-
}
535-
536-
// Check if the predicted promoted bytes will overflow free space in old-gen.
537-
PSAdaptiveSizePolicy* policy = heap->size_policy();
538-
539-
size_t avg_promoted = (size_t) policy->padded_average_promoted_in_bytes();
540-
size_t promotion_estimate = MIN2(avg_promoted, young_gen->used_in_bytes());
541-
// Total free size after possible old gen expansion
542-
size_t free_in_old_gen_with_expansion = old_gen->max_gen_size() - old_gen->used_in_bytes();
543-
544-
log_trace(gc, ergo)("average_promoted %zu; padded_average_promoted %zu",
545-
(size_t) policy->average_promoted_in_bytes(),
546-
(size_t) policy->padded_average_promoted_in_bytes());
547-
548-
if (promotion_estimate >= free_in_old_gen_with_expansion) {
549-
log_debug(gc, ergo)("Run full-gc; predicted promotion size >= max free space in old-gen: %zu >= %zu",
550-
promotion_estimate, free_in_old_gen_with_expansion);
551-
return ShouldRunFullGC;
552-
}
553-
554-
if (UseAdaptiveSizePolicy) {
555-
// Also checking OS has enough free memory to commit and expand old-gen.
556-
// Otherwise, the recorded gc-pause-time might be inflated to include time
557-
// of OS preparing free memory, resulting in inaccurate young-gen resizing.
558-
assert(old_gen->committed().byte_size() >= old_gen->used_in_bytes(), "inv");
559-
// Use uint64_t instead of size_t for 32bit compatibility.
560-
uint64_t free_mem_in_os;
561-
if (os::free_memory(free_mem_in_os)) {
562-
size_t actual_free = (size_t)MIN2(old_gen->committed().byte_size() - old_gen->used_in_bytes() + free_mem_in_os,
563-
(uint64_t)SIZE_MAX);
564-
if (promotion_estimate > actual_free) {
565-
log_debug(gc, ergo)("Run full-gc; predicted promotion size > free space in old-gen and OS: %zu > %zu",
566-
promotion_estimate, actual_free);
567-
return ShouldRunFullGC;
568-
}
569-
}
570-
}
571-
572-
// No particular reasons to run full-gc, so young-gc.
573-
return ShouldRunYoungGC;
574-
}
575-
576516
// Adaptive size policy support.
577517
void PSScavenge::set_young_generation_boundary(HeapWord* v) {
578518
_young_generation_boundary = v;

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ class PSScavenge: AllStatic {
6464

6565
static void clean_up_failed_promotion();
6666

67-
static bool should_attempt_scavenge();
68-
6967
// Private accessors
7068
static PSCardTable* card_table() { assert(_card_table != nullptr, "Sanity"); return _card_table; }
7169
static const ParallelScavengeTracer* gc_tracer() { return &_gc_tracer; }

0 commit comments

Comments
 (0)