Path: blob/master/src/hotspot/share/gc/shenandoah/shenandoahEvacOOMHandler.cpp
40961 views
/*1* Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"2526#include "gc/shenandoah/shenandoahEvacOOMHandler.inline.hpp"27#include "gc/shenandoah/shenandoahUtils.hpp"28#include "runtime/os.hpp"29#include "runtime/thread.hpp"3031const jint ShenandoahEvacOOMHandler::OOM_MARKER_MASK = 0x80000000;3233ShenandoahEvacOOMHandler::ShenandoahEvacOOMHandler() :34_threads_in_evac(0) {35}3637void ShenandoahEvacOOMHandler::wait_for_no_evac_threads() {38while ((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) != 0) {39os::naked_short_sleep(1);40}41// At this point we are sure that no threads can evacuate anything. Raise42// the thread-local oom_during_evac flag to indicate that any attempt43// to evacuate should simply return the forwarding pointer instead (which is safe now).44ShenandoahThreadLocalData::set_oom_during_evac(Thread::current(), true);45}4647void ShenandoahEvacOOMHandler::register_thread(Thread* thr) {48jint threads_in_evac = Atomic::load_acquire(&_threads_in_evac);4950assert(!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current()), "TL oom-during-evac must not be set");51while (true) {52// Check for OOM.53// If offender has OOM_MARKER_MASK, then loop until no more threads in evac54if ((threads_in_evac & OOM_MARKER_MASK) != 0) {55wait_for_no_evac_threads();56return;57}5859jint other = Atomic::cmpxchg(&_threads_in_evac, threads_in_evac, threads_in_evac + 1);60if (other == threads_in_evac) {61// Success: caller may safely enter evacuation62return;63} else {64threads_in_evac = other;65}66}67}6869void ShenandoahEvacOOMHandler::unregister_thread(Thread* thr) {70if (!ShenandoahThreadLocalData::is_oom_during_evac(thr)) {71assert((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) > 0, "sanity");72// NOTE: It's ok to simply decrement, even with mask set, because unmasked value is positive.73Atomic::dec(&_threads_in_evac);74} else {75// If we get here, the current thread has already gone through the76// OOM-during-evac protocol and has thus either never entered or successfully left77// the evacuation region. Simply flip its TL oom-during-evac flag back off.78ShenandoahThreadLocalData::set_oom_during_evac(thr, false);79}80assert(!ShenandoahThreadLocalData::is_oom_during_evac(thr), "TL oom-during-evac must be turned off");81}8283void ShenandoahEvacOOMHandler::handle_out_of_memory_during_evacuation() {84assert(ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "sanity");85assert(!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current()), "TL oom-during-evac must not be set");8687jint threads_in_evac = Atomic::load_acquire(&_threads_in_evac);88while (true) {89jint other = Atomic::cmpxchg(&_threads_in_evac, threads_in_evac, (threads_in_evac - 1) | OOM_MARKER_MASK);90if (other == threads_in_evac) {91// Success: wait for other threads to get out of the protocol and return.92wait_for_no_evac_threads();93return;94} else {95// Failure: try again with updated new value.96threads_in_evac = other;97}98}99}100101void ShenandoahEvacOOMHandler::clear() {102assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");103assert((Atomic::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) == 0, "sanity");104Atomic::release_store_fence(&_threads_in_evac, (jint)0);105}106107108