118118#include " utilities/globalDefinitions.hpp"
119119#include " utilities/stack.inline.hpp"
120120
121+ uintx G1CollectedHeap::_gc_overhead_counter = 0 ;
121122size_t G1CollectedHeap::_humongous_object_threshold_in_words = 0 ;
122123
123124// INVARIANTS/NOTES
@@ -467,8 +468,20 @@ HeapWord* G1CollectedHeap::attempt_allocation_slow(uint node_index, size_t word_
467468 log_trace (gc, alloc)(" %s: Unsuccessfully scheduled collection allocating %zu words" ,
468469 Thread::current ()->name (), word_size);
469470
471+ if (is_shutting_down ()) {
472+ stall_for_vm_shutdown ();
473+ return nullptr ;
474+ }
475+
476+ // Was the gc-overhead reached inside the safepoint? If so, this mutator
477+ // should return null even when unsuccessfully scheduling a collection as well
478+ // for global consistency.
479+ if (gc_overhead_limit_exceeded ()) {
480+ return nullptr ;
481+ }
482+
470483 // We can reach here if we were unsuccessful in scheduling a collection (because
471- // another thread beat us to it). In this case immeditealy retry the allocation
484+ // another thread beat us to it). In this case immediately retry the allocation
472485 // attempt because another thread successfully performed a collection and possibly
473486 // reclaimed enough space. The first attempt (without holding the Heap_lock) is
474487 // here and the follow-on attempt will be at the start of the next loop
@@ -485,11 +498,6 @@ HeapWord* G1CollectedHeap::attempt_allocation_slow(uint node_index, size_t word_
485498 log_warning (gc, alloc)(" %s: Retried allocation %u times for %zu words" ,
486499 Thread::current ()->name (), try_count, word_size);
487500 }
488-
489- if (is_shutting_down ()) {
490- stall_for_vm_shutdown ();
491- return nullptr ;
492- }
493501 }
494502
495503 ShouldNotReachHere ();
@@ -714,6 +722,17 @@ HeapWord* G1CollectedHeap::attempt_allocation_humongous(size_t word_size) {
714722 log_trace (gc, alloc)(" %s: Unsuccessfully scheduled collection allocating %zu" ,
715723 Thread::current ()->name (), word_size);
716724
725+ if (is_shutting_down ()) {
726+ stall_for_vm_shutdown ();
727+ return nullptr ;
728+ }
729+
730+ // Was the gc-overhead reached inside the safepoint? If so, this mutator
731+ // should return null as well for global consistency.
732+ if (gc_overhead_limit_exceeded ()) {
733+ return nullptr ;
734+ }
735+
717736 // We can reach here if we were unsuccessful in scheduling a collection (because
718737 // another thread beat us to it).
719738 // Humongous object allocation always needs a lock, so we wait for the retry
@@ -725,11 +744,6 @@ HeapWord* G1CollectedHeap::attempt_allocation_humongous(size_t word_size) {
725744 log_warning (gc, alloc)(" %s: Retried allocation %u times for %zu words" ,
726745 Thread::current ()->name (), try_count, word_size);
727746 }
728-
729- if (is_shutting_down ()) {
730- stall_for_vm_shutdown ();
731- return nullptr ;
732- }
733747 }
734748
735749 ShouldNotReachHere ();
@@ -955,25 +969,58 @@ void G1CollectedHeap::resize_heap_after_young_collection(size_t allocation_word_
955969 phase_times ()->record_resize_heap_time ((Ticks::now () - start).seconds () * 1000.0 );
956970}
957971
972+ void G1CollectedHeap::update_gc_overhead_limit_exceeded () {
973+ assert (SafepointSynchronize::is_at_safepoint (), " precondition" );
974+
975+ if (UseGCOverheadLimit) {
976+ bool little_mutator_time = (_policy->analytics ()->long_term_gc_time_ratio () * 100 ) >= GCTimeLimit;
977+ double free_space_percent = percent_of (num_available_regions () * G1HeapRegion::GrainBytes, max_capacity ());
978+ bool little_free_space = free_space_percent < GCHeapFreeLimit;
979+
980+ log_debug (gc)(" GC Overhead Limit: GC Time %f Free Space %f Counter %zu" ,
981+ (_policy->analytics ()->long_term_gc_time_ratio () * 100 ),
982+ free_space_percent,
983+ _gc_overhead_counter);
984+
985+ if (little_mutator_time && little_free_space) {
986+ _gc_overhead_counter++;
987+ return ;
988+ } else {
989+ _gc_overhead_counter = 0 ;
990+ }
991+ }
992+ }
993+
994+ bool G1CollectedHeap::gc_overhead_limit_exceeded () {
995+ return _gc_overhead_counter >= GCOverheadLimitThreshold;
996+ }
997+
958998HeapWord* G1CollectedHeap::satisfy_failed_allocation_helper (size_t word_size,
959999 bool do_gc,
9601000 bool maximal_compaction,
9611001 bool expect_null_mutator_alloc_region) {
962- // Let's attempt the allocation first.
963- HeapWord* result =
964- attempt_allocation_at_safepoint (word_size,
965- expect_null_mutator_alloc_region);
966- if (result != nullptr ) {
967- return result;
968- }
1002+ // Skip allocation if GC overhead has been exceeded to let the mutator run into
1003+ // an OOME. It can either exit "gracefully" or try to free up memory asap.
1004+ // For the latter situation, keep running GCs. If the mutator frees up enough
1005+ // memory quickly enough, the overhead(s) will go below the threshold(s) again
1006+ // and the VM may continue running.
1007+ if (!gc_overhead_limit_exceeded ()) {
1008+ // Let's attempt the allocation first.
1009+ HeapWord* result =
1010+ attempt_allocation_at_safepoint (word_size,
1011+ expect_null_mutator_alloc_region);
1012+ if (result != nullptr ) {
1013+ return result;
1014+ }
9691015
970- // In a G1 heap, we're supposed to keep allocation from failing by
971- // incremental pauses. Therefore, at least for now, we'll favor
972- // expansion over collection. (This might change in the future if we can
973- // do something smarter than full collection to satisfy a failed alloc.)
974- result = expand_and_allocate (word_size);
975- if (result != nullptr ) {
976- return result;
1016+ // In a G1 heap, we're supposed to keep allocation from failing by
1017+ // incremental pauses. Therefore, at least for now, we'll favor
1018+ // expansion over collection. (This might change in the future if we can
1019+ // do something smarter than full collection to satisfy a failed alloc.)
1020+ result = expand_and_allocate (word_size);
1021+ if (result != nullptr ) {
1022+ return result;
1023+ }
9771024 }
9781025
9791026 if (do_gc) {
@@ -997,6 +1044,10 @@ HeapWord* G1CollectedHeap::satisfy_failed_allocation_helper(size_t word_size,
9971044HeapWord* G1CollectedHeap::satisfy_failed_allocation (size_t word_size) {
9981045 assert_at_safepoint_on_vm_thread ();
9991046
1047+ // Update GC overhead limits after the initial garbage collection leading to this
1048+ // allocation attempt.
1049+ update_gc_overhead_limit_exceeded ();
1050+
10001051 // Attempts to allocate followed by Full GC.
10011052 HeapWord* result =
10021053 satisfy_failed_allocation_helper (word_size,
@@ -1028,6 +1079,10 @@ HeapWord* G1CollectedHeap::satisfy_failed_allocation(size_t word_size) {
10281079 return result;
10291080 }
10301081
1082+ if (gc_overhead_limit_exceeded ()) {
1083+ log_info (gc)(" GC Overhead Limit exceeded too often (%zu)." , GCOverheadLimitThreshold);
1084+ }
1085+
10311086 // What else? We might try synchronous finalization later. If the total
10321087 // space available is large enough for the allocation, then a more
10331088 // complete compaction phase than we've tried so far might be
0 commit comments