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/shenandoahCollectorPolicy.hpp"
28 #include "gc/shenandoah/shenandoahController.hpp"
29 #include "gc/shenandoah/shenandoahHeap.hpp"
30 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
31 
32 void ShenandoahController::pacing_notify_alloc(size_t words) {
33   assert(ShenandoahPacing, "should only call when pacing is enabled");
34   Atomic::add(&_allocs_seen, words, memory_order_relaxed);
35 }
36 
37 size_t ShenandoahController::reset_allocs_seen() {
38   return Atomic::xchg(&_allocs_seen, (size_t)0, memory_order_relaxed);
39 }
40 
41 void ShenandoahController::update_gc_id() {
42   Atomic::inc(&_gc_id);
43 }
44 
45 size_t ShenandoahController::get_gc_id() {
46   return Atomic::load(&_gc_id);
47 }
48 
49 void ShenandoahController::handle_alloc_failure(const ShenandoahAllocRequest& req, bool block) {
50   assert(current()->is_Java_thread(), "expect Java thread here");
51 
52   const bool is_humongous = ShenandoahHeapRegion::requires_humongous(req.size());
53   const GCCause::Cause cause = is_humongous ? GCCause::_shenandoah_humongous_allocation_failure : GCCause::_allocation_failure;
54 
55   ShenandoahHeap* const heap = ShenandoahHeap::heap();
56   if (heap->cancel_gc(cause)) {
57     log_info(gc)("Failed to allocate %s, " PROPERFMT, req.type_string(), PROPERFMTARGS(req.size() * HeapWordSize));
58     request_gc(cause);
59   }
60 
61   if (block) {
62     MonitorLocker ml(&_alloc_failure_waiters_lock);
63     while (!should_terminate() && ShenandoahCollectorPolicy::is_allocation_failure(heap->cancelled_cause())) {
64       ml.wait();
65     }
66   }
67 }
68 
69 void ShenandoahController::handle_alloc_failure_evac(size_t words) {
70 
71   ShenandoahHeap* const heap = ShenandoahHeap::heap();
72   const bool is_humongous = ShenandoahHeapRegion::requires_humongous(words);
73   const GCCause::Cause cause = is_humongous ? GCCause::_shenandoah_humongous_allocation_failure : GCCause::_shenandoah_allocation_failure_evac;
74 
75   if (heap->cancel_gc(cause)) {
76     log_info(gc)("Failed to allocate " PROPERFMT " for evacuation", PROPERFMTARGS(words * HeapWordSize));
77   }
78 }
79 
80 void ShenandoahController::notify_alloc_failure_waiters() {
81   MonitorLocker ml(&_alloc_failure_waiters_lock);
82   ml.notify_all();
83 }
84