Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/shenandoah/shenandoahEvacOOMHandler.hpp
38920 views
/*1* Copyright (c) 2018, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223#ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHEVACOOMHANDLER_HPP24#define SHARE_VM_GC_SHENANDOAH_SHENANDOAHEVACOOMHANDLER_HPP2526#include "gc_implementation/shenandoah/shenandoahPadding.hpp"2728/**29* Provides safe handling of out-of-memory situations during evacuation.30*31* When a Java thread encounters out-of-memory while evacuating an object in a32* write-barrier (i.e. it cannot copy the object to to-space), it does not necessarily33* follow we can return immediately from the WB (and store to from-space).34*35* In very basic case, on such failure we may wait until the the evacuation is over,36* and then resolve the forwarded copy, and to the store there. This is possible37* because other threads might still have space in their GCLABs, and successfully38* evacuate the object.39*40* But, there is a race due to non-atomic evac_in_progress transition. Consider41* thread A is stuck waiting for the evacuation to be over -- it cannot leave with42* from-space copy yet. Control thread drops evacuation_in_progress preparing for43* next STW phase that has to recover from OOME. Thread B misses that update, and44* successfully evacuates the object, does the write to to-copy. But, before45* Thread B is able to install the fwdptr, thread A discovers evac_in_progress is46* down, exits from here, reads the fwdptr, discovers old from-copy, and stores there.47* Thread B then wakes up and installs to-copy. This breaks to-space invariant, and48* silently corrupts the heap: we accepted two writes to separate copies of the object.49*50* The way it is solved here is to maintain a counter of threads inside the51* 'evacuation path'. The 'evacuation path' is the part of evacuation that does the actual52* allocation, copying and CASing of the copy object, and is protected by this53* OOM-during-evac-handler. The handler allows multiple threads to enter and exit54* evacuation path, but on OOME it requires all threads that experienced OOME to wait55* for current threads to leave, and blocks other threads from entering.56*57* Detailed state change:58*59* Upon entry of the evac-path, entering thread will attempt to increase the counter,60* using a CAS. Depending on the result of the CAS:61* - success: carry on with evac62* - failure:63* - if offending value is a valid counter, then try again64* - if offending value is OOM-during-evac special value: loop until65* counter drops to 0, then exit with read-barrier66*67* Upon exit, exiting thread will decrease the counter using atomic dec.68*69* Upon OOM-during-evac, any thread will attempt to CAS OOM-during-evac70* special value into the counter. Depending on result:71* - success: busy-loop until counter drops to zero, then exit with RB72* - failure:73* - offender is valid counter update: try again74* - offender is OOM-during-evac: busy loop until counter drops to75* zero, then exit with RB76*/77class ShenandoahEvacOOMHandler {78private:79static const jint OOM_MARKER_MASK;8081shenandoah_padding(0);82volatile jint _threads_in_evac;83shenandoah_padding(1);8485void wait_for_no_evac_threads();8687public:88ShenandoahEvacOOMHandler();8990/**91* Attempt to enter the protected evacuation path.92*93* When this returns true, it is safe to continue with normal evacuation.94* When this method returns false, evacuation must not be entered, and caller95* may safely continue with a read-barrier (if Java thread).96*/97void enter_evacuation();9899/**100* Leave evacuation path.101*/102void leave_evacuation();103104/**105* Signal out-of-memory during evacuation. It will prevent any other threads106* from entering the evacuation path, then wait until all threads have left the107* evacuation path, and then return. It is then safe to continue with a read-barrier.108*/109void handle_out_of_memory_during_evacuation();110111void clear();112};113114class ShenandoahEvacOOMScope : public StackObj {115public:116ShenandoahEvacOOMScope();117~ShenandoahEvacOOMScope();118};119120#endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHEVACOOMHANDLER_HPP121122123