1 /*
 2  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
 3  * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
 4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 5  *
 6  * This code is free software; you can redistribute it and/or modify it
 7  * under the terms of the GNU General Public License version 2 only, as
 8  * published by the Free Software Foundation.
 9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  *
24  */
25 
26 #include "gc/shared/allocTracer.hpp"
27 #include "gc/shared/gc_globals.hpp"
28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
29 #include "gc/shenandoah/shenandoahController.hpp"
30 #include "gc/shenandoah/shenandoahHeap.hpp"
31 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
32 
33 void ShenandoahController::update_gc_id() {
34   _gc_id.add_then_fetch((size_t)1);
35 }
36 
37 size_t ShenandoahController::get_gc_id() {
38   return _gc_id.load_relaxed();
39 }
40 
41 void ShenandoahController::handle_alloc_failure(const ShenandoahAllocRequest& req, bool block) {
42   assert(current()->is_Java_thread(), "expect Java thread here");
43 
44   const bool is_humongous = ShenandoahHeapRegion::requires_humongous(req.size());
45   const GCCause::Cause cause = is_humongous ? GCCause::_shenandoah_humongous_allocation_failure : GCCause::_allocation_failure;
46 
47   ShenandoahHeap* const heap = ShenandoahHeap::heap();
48   size_t req_byte = req.size() * HeapWordSize;
49   if (heap->cancel_gc(cause)) {
50     log_info(gc)("Failed to allocate %s, " PROPERFMT, req.type_string(), PROPERFMTARGS(req_byte));
51     request_gc(cause);
52   }
53   AllocTracer::send_allocation_requiring_gc_event(req_byte, checked_cast<uint>(get_gc_id()));
54 
55   if (block) {
56     MonitorLocker ml(&_alloc_failure_waiters_lock);
57     while (!should_terminate() && ShenandoahCollectorPolicy::is_allocation_failure(heap->cancelled_cause())) {
58       ml.wait();
59     }
60   }
61 }
62 
63 void ShenandoahController::handle_alloc_failure_evac(size_t words) {
64 
65   ShenandoahHeap* const heap = ShenandoahHeap::heap();
66   const bool is_humongous = ShenandoahHeapRegion::requires_humongous(words);
67   const GCCause::Cause cause = is_humongous ? GCCause::_shenandoah_humongous_allocation_failure : GCCause::_shenandoah_allocation_failure_evac;
68 
69   if (heap->cancel_gc(cause)) {
70     log_info(gc)("Failed to allocate " PROPERFMT " for evacuation", PROPERFMTARGS(words * HeapWordSize));
71   }
72 }
73 
74 void ShenandoahController::notify_alloc_failure_waiters() {
75   MonitorLocker ml(&_alloc_failure_waiters_lock);
76   ml.notify_all();
77 }