1 /*
  2  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 #include "precompiled.hpp"
 25 
 26 #include "gc/shared/gc_globals.hpp"
 27 #include "gc/shenandoah/shenandoahController.hpp"
 28 #include "gc/shenandoah/shenandoahHeap.hpp"
 29 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 30 
 31 void ShenandoahController::pacing_notify_alloc(size_t words) {
 32   assert(ShenandoahPacing, "should only call when pacing is enabled");
 33   Atomic::add(&_allocs_seen, words, memory_order_relaxed);
 34 }
 35 
 36 size_t ShenandoahController::reset_allocs_seen() {
 37   return Atomic::xchg(&_allocs_seen, (size_t)0, memory_order_relaxed);
 38 }
 39 
 40 void ShenandoahController::prepare_for_graceful_shutdown() {
 41   _graceful_shutdown.set();
 42 }
 43 
 44 bool ShenandoahController::in_graceful_shutdown() {
 45   return _graceful_shutdown.is_set();
 46 }
 47 
 48 void ShenandoahController::update_gc_id() {
 49   Atomic::inc(&_gc_id);
 50 }
 51 
 52 size_t ShenandoahController::get_gc_id() {
 53   return Atomic::load(&_gc_id);
 54 }
 55 
 56 void ShenandoahController::handle_alloc_failure(ShenandoahAllocRequest& req, bool block) {
 57   ShenandoahHeap* heap = ShenandoahHeap::heap();
 58 
 59   assert(current()->is_Java_thread(), "expect Java thread here");
 60   bool is_humongous = req.size() > ShenandoahHeapRegion::humongous_threshold_words();
 61 
 62   if (try_set_alloc_failure_gc(is_humongous)) {
 63     // Only report the first allocation failure
 64     log_info(gc)("Failed to allocate %s, " SIZE_FORMAT "%s",
 65                  req.type_string(),
 66                  byte_size_in_proper_unit(req.size() * HeapWordSize), proper_unit_for_byte_size(req.size() * HeapWordSize));
 67 
 68     // Now that alloc failure GC is scheduled, we can abort everything else
 69     heap->cancel_gc(GCCause::_allocation_failure);
 70   }
 71 
 72 
 73   if (block) {
 74     MonitorLocker ml(&_alloc_failure_waiters_lock);
 75     while (is_alloc_failure_gc()) {
 76       ml.wait();
 77     }
 78   }
 79 }
 80 
 81 void ShenandoahController::handle_alloc_failure_evac(size_t words) {
 82   ShenandoahHeap* heap = ShenandoahHeap::heap();
 83   bool is_humongous = (words > ShenandoahHeapRegion::region_size_words());
 84 
 85   if (try_set_alloc_failure_gc(is_humongous)) {
 86     // Only report the first allocation failure
 87     log_info(gc)("Failed to allocate " SIZE_FORMAT "%s for evacuation",
 88                  byte_size_in_proper_unit(words * HeapWordSize), proper_unit_for_byte_size(words * HeapWordSize));
 89   }
 90 
 91   // Forcefully report allocation failure
 92   heap->cancel_gc(GCCause::_shenandoah_allocation_failure_evac);
 93 }
 94 
 95 void ShenandoahController::notify_alloc_failure_waiters() {
 96   _alloc_failure_gc.unset();
 97   _humongous_alloc_failure_gc.unset();
 98   MonitorLocker ml(&_alloc_failure_waiters_lock);
 99   ml.notify_all();
100 }
101 
102 bool ShenandoahController::try_set_alloc_failure_gc(bool is_humongous) {
103   if (is_humongous) {
104     _humongous_alloc_failure_gc.try_set();
105   }
106   return _alloc_failure_gc.try_set();
107 }
108 
109 bool ShenandoahController::is_alloc_failure_gc() {
110   return _alloc_failure_gc.is_set();
111 }
112