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 // Classify the failure consistently with how ShenandoahFreeSet::allocate would
45 // route this request: a fresh mutator object (_alloc_shared) may expand for an
46 // identity hash-code, every other request carries an already-final size.
47 const bool may_expand_for_hash = (req.type() == ShenandoahAllocRequest::_alloc_shared);
48 const bool is_humongous = ShenandoahHeapRegion::requires_humongous(req.size(), may_expand_for_hash);
49 const GCCause::Cause cause = is_humongous ? GCCause::_shenandoah_humongous_allocation_failure : GCCause::_allocation_failure;
50
51 ShenandoahHeap* const heap = ShenandoahHeap::heap();
52 size_t req_byte = req.size() * HeapWordSize;
53 if (heap->cancel_gc(cause)) {
54 log_info(gc)("Failed to allocate %s, " PROPERFMT, req.type_string(), PROPERFMTARGS(req_byte));
55 request_gc(cause);
56 }
57 AllocTracer::send_allocation_requiring_gc_event(req_byte, checked_cast<uint>(get_gc_id()));
58
59 if (block) {
60 MonitorLocker ml(&_alloc_failure_waiters_lock);
61 while (!should_terminate() && ShenandoahCollectorPolicy::is_allocation_failure(heap->cancelled_cause())) {
62 ml.wait();
63 }
64 }
65 }
66
67 void ShenandoahController::handle_alloc_failure_evac(size_t words) {
68
69 ShenandoahHeap* const heap = ShenandoahHeap::heap();
70 const bool is_humongous = ShenandoahHeapRegion::requires_humongous(words, false /* may_expand_for_hash */);
71 const GCCause::Cause cause = is_humongous ? GCCause::_shenandoah_humongous_allocation_failure : GCCause::_shenandoah_allocation_failure_evac;
72
73 if (heap->cancel_gc(cause)) {
74 log_info(gc)("Failed to allocate " PROPERFMT " for evacuation", PROPERFMTARGS(words * HeapWordSize));
75 }
76 }
77
78 void ShenandoahController::notify_alloc_failure_waiters() {
79 MonitorLocker ml(&_alloc_failure_waiters_lock);
80 ml.notify_all();
81 }