2828#include " code/codeCache.hpp"
2929#include " compiler/oopMap.hpp"
3030#include " gc/g1/g1Allocator.inline.hpp"
31+ #include " gc/g1/g1Analytics.hpp"
3132#include " gc/g1/g1Arguments.hpp"
3233#include " gc/g1/g1BarrierSet.hpp"
3334#include " gc/g1/g1BatchedTask.hpp"
@@ -452,8 +453,15 @@ HeapWord* G1CollectedHeap::attempt_allocation_slow(uint node_index, size_t word_
452453 log_trace (gc, alloc)(" %s: Unsuccessfully scheduled collection allocating %zu words" ,
453454 Thread::current ()->name (), word_size);
454455
456+ // Has the gc overhead limit been reached in the meantime? If so, this mutator
457+ // should receive null even when unsuccessfully scheduling a collection as well
458+ // for global consistency.
459+ if (gc_overhead_limit_exceeded ()) {
460+ return nullptr ;
461+ }
462+
455463 // We can reach here if we were unsuccessful in scheduling a collection (because
456- // another thread beat us to it). In this case immeditealy retry the allocation
464+ // another thread beat us to it). In this case immediately retry the allocation
457465 // attempt because another thread successfully performed a collection and possibly
458466 // reclaimed enough space. The first attempt (without holding the Heap_lock) is
459467 // here and the follow-on attempt will be at the start of the next loop
@@ -695,6 +703,13 @@ HeapWord* G1CollectedHeap::attempt_allocation_humongous(size_t word_size) {
695703 log_trace (gc, alloc)(" %s: Unsuccessfully scheduled collection allocating %zu" ,
696704 Thread::current ()->name (), word_size);
697705
706+ // Has the gc overhead limit been reached in the meantime? If so, this mutator
707+ // should receive null even when unsuccessfully scheduling a collection as well
708+ // for global consistency.
709+ if (gc_overhead_limit_exceeded ()) {
710+ return nullptr ;
711+ }
712+
698713 // We can reach here if we were unsuccessful in scheduling a collection (because
699714 // another thread beat us to it).
700715 // Humongous object allocation always needs a lock, so we wait for the retry
@@ -897,25 +912,62 @@ void G1CollectedHeap::resize_heap_if_necessary(size_t allocation_word_size) {
897912 }
898913}
899914
915+ void G1CollectedHeap::update_gc_overhead_counter () {
916+ assert (SafepointSynchronize::is_at_safepoint (), " precondition" );
917+
918+ if (!UseGCOverheadLimit) {
919+ return ;
920+ }
921+
922+ bool gc_time_over_limit = (_policy->analytics ()->long_term_pause_time_ratio () * 100 ) >= GCTimeLimit;
923+ double free_space_percent = percent_of (num_available_regions () * G1HeapRegion::GrainBytes, max_capacity ());
924+ bool free_space_below_limit = free_space_percent < GCHeapFreeLimit;
925+
926+ log_debug (gc)(" GC Overhead Limit: GC Time %f Free Space %f Counter %zu" ,
927+ (_policy->analytics ()->long_term_pause_time_ratio () * 100 ),
928+ free_space_percent,
929+ _gc_overhead_counter);
930+
931+ if (gc_time_over_limit && free_space_below_limit) {
932+ _gc_overhead_counter++;
933+ } else {
934+ _gc_overhead_counter = 0 ;
935+ }
936+ }
937+
938+ bool G1CollectedHeap::gc_overhead_limit_exceeded () {
939+ return _gc_overhead_counter >= GCOverheadLimitThreshold;
940+ }
941+
900942HeapWord* G1CollectedHeap::satisfy_failed_allocation_helper (size_t word_size,
901943 bool do_gc,
902944 bool maximal_compaction,
903945 bool expect_null_mutator_alloc_region) {
904- // Let's attempt the allocation first.
905- HeapWord* result =
906- attempt_allocation_at_safepoint (word_size,
907- expect_null_mutator_alloc_region);
908- if (result != nullptr ) {
909- return result;
910- }
946+ // Skip allocation if GC overhead limit has been exceeded to let the mutator run
947+ // into an OOME. It can either exit "gracefully" or try to free up memory asap.
948+ // For the latter situation, keep running GCs. If the mutator frees up enough
949+ // memory quickly enough, the overhead(s) will go below the threshold(s) again
950+ // and the VM may continue running.
951+ // If we did not continue garbage collections, the (gc overhead) limit may decrease
952+ // enough by itself to not count as exceeding the limit any more, in the worst
953+ // case bouncing back-and-forth all the time.
954+ if (!gc_overhead_limit_exceeded ()) {
955+ // Let's attempt the allocation first.
956+ HeapWord* result =
957+ attempt_allocation_at_safepoint (word_size,
958+ expect_null_mutator_alloc_region);
959+ if (result != nullptr ) {
960+ return result;
961+ }
911962
912- // In a G1 heap, we're supposed to keep allocation from failing by
913- // incremental pauses. Therefore, at least for now, we'll favor
914- // expansion over collection. (This might change in the future if we can
915- // do something smarter than full collection to satisfy a failed alloc.)
916- result = expand_and_allocate (word_size);
917- if (result != nullptr ) {
918- return result;
963+ // In a G1 heap, we're supposed to keep allocation from failing by
964+ // incremental pauses. Therefore, at least for now, we'll favor
965+ // expansion over collection. (This might change in the future if we can
966+ // do something smarter than full collection to satisfy a failed alloc.)
967+ result = expand_and_allocate (word_size);
968+ if (result != nullptr ) {
969+ return result;
970+ }
919971 }
920972
921973 if (do_gc) {
@@ -939,6 +991,10 @@ HeapWord* G1CollectedHeap::satisfy_failed_allocation_helper(size_t word_size,
939991HeapWord* G1CollectedHeap::satisfy_failed_allocation (size_t word_size) {
940992 assert_at_safepoint_on_vm_thread ();
941993
994+ // Update GC overhead limits after the initial garbage collection leading to this
995+ // allocation attempt.
996+ update_gc_overhead_counter ();
997+
942998 // Attempts to allocate followed by Full GC.
943999 HeapWord* result =
9441000 satisfy_failed_allocation_helper (word_size,
@@ -973,6 +1029,10 @@ HeapWord* G1CollectedHeap::satisfy_failed_allocation(size_t word_size) {
9731029 assert (!soft_ref_policy ()->should_clear_all_soft_refs (),
9741030 " Flag should have been handled and cleared prior to this point" );
9751031
1032+ if (gc_overhead_limit_exceeded ()) {
1033+ log_info (gc)(" GC Overhead Limit exceeded too often (%zu)." , GCOverheadLimitThreshold);
1034+ }
1035+
9761036 // What else? We might try synchronous finalization later. If the total
9771037 // space available is large enough for the allocation, then a more
9781038 // complete compaction phase than we've tried so far might be
@@ -1138,6 +1198,7 @@ class HumongousRegionSetChecker : public G1HeapRegionSetChecker {
11381198
11391199G1CollectedHeap::G1CollectedHeap () :
11401200 CollectedHeap(),
1201+ _gc_overhead_counter(0 ),
11411202 _service_thread(nullptr ),
11421203 _periodic_gc_task(nullptr ),
11431204 _free_arena_memory_task(nullptr ),
0 commit comments