1 /*
2 * Copyright (c) 2018, 2019, 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/shenandoahHeap.inline.hpp"
28 #include "gc/shenandoah/shenandoahUtils.hpp"
29 #include "gc/shenandoah/shenandoahEvacOOMHandler.hpp"
30 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
31 #include "runtime/atomic.hpp"
32 #include "runtime/os.hpp"
33 #include "runtime/thread.hpp"
34
35 const jint ShenandoahEvacOOMHandler::OOM_MARKER_MASK = 0x80000000;
36
37 ShenandoahEvacOOMHandler::ShenandoahEvacOOMHandler() :
38 _threads_in_evac(0) {
39 }
40
41 void ShenandoahEvacOOMHandler::wait_for_no_evac_threads() {
42 while ((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) != 0) {
43 os::naked_short_sleep(1);
44 }
45 // At this point we are sure that no threads can evacuate anything. Raise
46 // the thread-local oom_during_evac flag to indicate that any attempt
47 // to evacuate should simply return the forwarding pointer instead (which is safe now).
48 ShenandoahThreadLocalData::set_oom_during_evac(Thread::current(), true);
49 }
50
51 void ShenandoahEvacOOMHandler::enter_evacuation() {
52 jint threads_in_evac = Atomic::load_acquire(&_threads_in_evac);
53
54 Thread* const thr = Thread::current();
55 uint8_t level = ShenandoahThreadLocalData::push_evac_oom_scope(thr);
56 if ((threads_in_evac & OOM_MARKER_MASK) != 0) {
57 wait_for_no_evac_threads();
58 return;
59 }
60
61 // Nesting case, this thread already registered
62 if (level > 0) {
63 return;
64 }
65
66 assert(!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current()), "TL oom-during-evac must not be set");
67 while (true) {
68 jint other = Atomic::cmpxchg(&_threads_in_evac, threads_in_evac, threads_in_evac + 1);
69 if (other == threads_in_evac) {
70 // Success: caller may safely enter evacuation
71 return;
72 } else {
73 // Failure:
74 // - if offender has OOM_MARKER_MASK, then loop until no more threads in evac
75 // - otherwise re-try CAS
76 if ((other & OOM_MARKER_MASK) != 0) {
77 wait_for_no_evac_threads();
78 return;
79 }
80 threads_in_evac = other;
81 }
82 }
83 }
84
85 void ShenandoahEvacOOMHandler::leave_evacuation() {
86 Thread* const thr = Thread::current();
87 uint8_t level = ShenandoahThreadLocalData::pop_evac_oom_scope(thr);
88 // Not top level, just return
89 if (level > 1) {
90 return;
91 }
92
93 if (!ShenandoahThreadLocalData::is_oom_during_evac(thr)) {
94 assert((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) > 0, "sanity");
95 // NOTE: It's ok to simply decrement, even with mask set, because unmasked value is positive.
96 Atomic::dec(&_threads_in_evac);
97 } else {
98 // If we get here, the current thread has already gone through the
99 // OOM-during-evac protocol and has thus either never entered or successfully left
100 // the evacuation region. Simply flip its TL oom-during-evac flag back off.
101 ShenandoahThreadLocalData::set_oom_during_evac(thr, false);
102 }
103 assert(!ShenandoahThreadLocalData::is_oom_during_evac(thr), "TL oom-during-evac must be turned off");
104 }
105
106 void ShenandoahEvacOOMHandler::handle_out_of_memory_during_evacuation() {
107 assert(ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "sanity");
108 assert(!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current()), "TL oom-during-evac must not be set");
109
110 jint threads_in_evac = Atomic::load_acquire(&_threads_in_evac);
111 while (true) {
112 jint other = Atomic::cmpxchg(&_threads_in_evac, threads_in_evac, (threads_in_evac - 1) | OOM_MARKER_MASK);
113 if (other == threads_in_evac) {
114 // Success: wait for other threads to get out of the protocol and return.
115 wait_for_no_evac_threads();
116 return;
117 } else {
118 // Failure: try again with updated new value.
119 threads_in_evac = other;
120 }
121 }
122 }
123
124 void ShenandoahEvacOOMHandler::clear() {
125 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
126 assert((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) == 0, "sanity");
127 Atomic::release_store_fence(&_threads_in_evac, (jint)0);
128 }
129
130 ShenandoahEvacOOMScope::ShenandoahEvacOOMScope() {
131 ShenandoahHeap::heap()->enter_evacuation();
132 }
133
134 ShenandoahEvacOOMScope::~ShenandoahEvacOOMScope() {
135 ShenandoahHeap::heap()->leave_evacuation();
136 }
|
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/shenandoahHeap.inline.hpp"
28 #include "gc/shenandoah/shenandoahUtils.hpp"
29 #include "gc/shenandoah/shenandoahEvacOOMHandler.inline.hpp"
30 #include "runtime/os.hpp"
31 #include "runtime/thread.hpp"
32
33 const jint ShenandoahEvacOOMHandler::OOM_MARKER_MASK = 0x80000000;
34
35 ShenandoahEvacOOMHandler::ShenandoahEvacOOMHandler() :
36 _threads_in_evac(0) {
37 }
38
39 void ShenandoahEvacOOMHandler::wait_for_no_evac_threads() {
40 while ((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) != 0) {
41 os::naked_short_sleep(1);
42 }
43 // At this point we are sure that no threads can evacuate anything. Raise
44 // the thread-local oom_during_evac flag to indicate that any attempt
45 // to evacuate should simply return the forwarding pointer instead (which is safe now).
46 ShenandoahThreadLocalData::set_oom_during_evac(Thread::current(), true);
47 }
48
49 void ShenandoahEvacOOMHandler::register_thread_to_protocol(Thread* thr) {
50 jint threads_in_evac = Atomic::load_acquire(&_threads_in_evac);
51
52 assert(!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current()), "TL oom-during-evac must not be set");
53 while (true) {
54 jint other = Atomic::cmpxchg(&_threads_in_evac, threads_in_evac, threads_in_evac + 1);
55 if (other == threads_in_evac) {
56 // Success: caller may safely enter evacuation
57 return;
58 } else {
59 // Failure:
60 // - if offender has OOM_MARKER_MASK, then loop until no more threads in evac
61 // - otherwise re-try CAS
62 if ((other & OOM_MARKER_MASK) != 0) {
63 wait_for_no_evac_threads();
64 return;
65 }
66 threads_in_evac = other;
67 }
68 }
69 }
70
71 void ShenandoahEvacOOMHandler::unregister_thread_to_protocol(Thread* thr) {
72 if (!ShenandoahThreadLocalData::is_oom_during_evac(thr)) {
73 assert((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) > 0, "sanity");
74 // NOTE: It's ok to simply decrement, even with mask set, because unmasked value is positive.
75 Atomic::dec(&_threads_in_evac);
76 } else {
77 // If we get here, the current thread has already gone through the
78 // OOM-during-evac protocol and has thus either never entered or successfully left
79 // the evacuation region. Simply flip its TL oom-during-evac flag back off.
80 ShenandoahThreadLocalData::set_oom_during_evac(thr, false);
81 }
82 assert(!ShenandoahThreadLocalData::is_oom_during_evac(thr), "TL oom-during-evac must be turned off");
83 }
84
85 void ShenandoahEvacOOMHandler::handle_out_of_memory_during_evacuation() {
86 assert(ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "sanity");
87 assert(!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current()), "TL oom-during-evac must not be set");
88
89 jint threads_in_evac = Atomic::load_acquire(&_threads_in_evac);
90 while (true) {
91 jint other = Atomic::cmpxchg(&_threads_in_evac, threads_in_evac, (threads_in_evac - 1) | OOM_MARKER_MASK);
92 if (other == threads_in_evac) {
93 // Success: wait for other threads to get out of the protocol and return.
94 wait_for_no_evac_threads();
95 return;
96 } else {
97 // Failure: try again with updated new value.
98 threads_in_evac = other;
99 }
100 }
101 }
102
103 void ShenandoahEvacOOMHandler::clear() {
104 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
105 assert((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) == 0, "sanity");
106 Atomic::release_store_fence(&_threads_in_evac, (jint)0);
107 }
|