1 /*
2 * Copyright (c) 2018, 2020, Red Hat, Inc. 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
25 #include "precompiled.hpp"
26
27 #include "gc/shenandoah/shenandoahEvacOOMHandler.inline.hpp"
28 #include "gc/shenandoah/shenandoahUtils.hpp"
29 #include "runtime/os.hpp"
30 #include "runtime/thread.hpp"
31
32 const jint ShenandoahEvacOOMHandler::OOM_MARKER_MASK = 0x80000000;
33
ShenandoahEvacOOMHandler()34 ShenandoahEvacOOMHandler::ShenandoahEvacOOMHandler() :
35 _threads_in_evac(0) {
36 }
37
wait_for_no_evac_threads()38 void ShenandoahEvacOOMHandler::wait_for_no_evac_threads() {
39 while ((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) != 0) {
40 os::naked_short_sleep(1);
41 }
42 // At this point we are sure that no threads can evacuate anything. Raise
43 // the thread-local oom_during_evac flag to indicate that any attempt
44 // to evacuate should simply return the forwarding pointer instead (which is safe now).
45 ShenandoahThreadLocalData::set_oom_during_evac(Thread::current(), true);
46 }
47
register_thread(Thread * thr)48 void ShenandoahEvacOOMHandler::register_thread(Thread* thr) {
49 jint threads_in_evac = Atomic::load_acquire(&_threads_in_evac);
50
51 assert(!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current()), "TL oom-during-evac must not be set");
52 while (true) {
53 // Check for OOM.
54 // If offender has OOM_MARKER_MASK, then loop until no more threads in evac
55 if ((threads_in_evac & OOM_MARKER_MASK) != 0) {
56 wait_for_no_evac_threads();
57 return;
58 }
59
60 jint other = Atomic::cmpxchg(&_threads_in_evac, threads_in_evac, threads_in_evac + 1);
61 if (other == threads_in_evac) {
62 // Success: caller may safely enter evacuation
63 return;
64 } else {
65 threads_in_evac = other;
66 }
67 }
68 }
69
unregister_thread(Thread * thr)70 void ShenandoahEvacOOMHandler::unregister_thread(Thread* thr) {
71 if (!ShenandoahThreadLocalData::is_oom_during_evac(thr)) {
72 assert((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) > 0, "sanity");
73 // NOTE: It's ok to simply decrement, even with mask set, because unmasked value is positive.
74 Atomic::dec(&_threads_in_evac);
75 } else {
76 // If we get here, the current thread has already gone through the
77 // OOM-during-evac protocol and has thus either never entered or successfully left
78 // the evacuation region. Simply flip its TL oom-during-evac flag back off.
79 ShenandoahThreadLocalData::set_oom_during_evac(thr, false);
80 }
81 assert(!ShenandoahThreadLocalData::is_oom_during_evac(thr), "TL oom-during-evac must be turned off");
82 }
83
handle_out_of_memory_during_evacuation()84 void ShenandoahEvacOOMHandler::handle_out_of_memory_during_evacuation() {
85 assert(ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "sanity");
86 assert(!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current()), "TL oom-during-evac must not be set");
87
88 jint threads_in_evac = Atomic::load_acquire(&_threads_in_evac);
89 while (true) {
90 jint other = Atomic::cmpxchg(&_threads_in_evac, threads_in_evac, (threads_in_evac - 1) | OOM_MARKER_MASK);
91 if (other == threads_in_evac) {
92 // Success: wait for other threads to get out of the protocol and return.
93 wait_for_no_evac_threads();
94 return;
95 } else {
96 // Failure: try again with updated new value.
97 threads_in_evac = other;
98 }
99 }
100 }
101
clear()102 void ShenandoahEvacOOMHandler::clear() {
103 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
104 assert((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) == 0, "sanity");
105 Atomic::release_store_fence(&_threads_in_evac, (jint)0);
106 }
107