1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 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 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 
  26 #include "gc/shenandoah/shenandoahHeap.hpp"
  27 #include "gc/shenandoah/shenandoahUtils.hpp"
  28 #include "gc/shenandoah/shenandoahEvacOOMHandler.hpp"
  29 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
  30 #include "runtime/orderAccess.hpp"
  31 #include "runtime/os.hpp"
  32 #include "runtime/thread.hpp"
  33 
  34 const jint ShenandoahEvacOOMHandler::OOM_MARKER_MASK = 0x80000000;
  35 
  36 ShenandoahEvacOOMHandler::ShenandoahEvacOOMHandler() :
  37   _threads_in_evac(0) {
  38 }
  39 
  40 void ShenandoahEvacOOMHandler::wait_for_no_evac_threads() {
  41   while ((OrderAccess::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) != 0) {
  42     os::naked_short_sleep(1);
  43   }
  44   // At this point we are sure that no threads can evacuate anything. Raise
  45   // the thread-local oom_during_evac flag to indicate that any attempt
  46   // to evacuate should simply return the forwarding pointer instead (which is safe now).
  47   ShenandoahThreadLocalData::set_oom_during_evac(Thread::current(), true);
  48 }
  49 
  50 void ShenandoahEvacOOMHandler::enter_evacuation() {
  51   jint threads_in_evac = OrderAccess::load_acquire(&_threads_in_evac);
  52 
  53   assert(!ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "sanity");
  54   assert(!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current()), "TL oom-during-evac must not be set");
  55 
  56   if ((threads_in_evac & OOM_MARKER_MASK) != 0) {
  57     wait_for_no_evac_threads();
  58     return;
  59   }
  60 
  61   while (true) {
  62     jint other = Atomic::cmpxchg(threads_in_evac + 1, &_threads_in_evac, threads_in_evac);
  63     if (other == threads_in_evac) {
  64       // Success: caller may safely enter evacuation
  65       DEBUG_ONLY(ShenandoahThreadLocalData::set_evac_allowed(Thread::current(), true));
  66       return;
  67     } else {
  68       // Failure:
  69       //  - if offender has OOM_MARKER_MASK, then loop until no more threads in evac
  70       //  - otherwise re-try CAS
  71       if ((other & OOM_MARKER_MASK) != 0) {
  72         wait_for_no_evac_threads();
  73         return;
  74       }
  75       threads_in_evac = other;
  76     }
  77   }
  78 }
  79 
  80 void ShenandoahEvacOOMHandler::leave_evacuation() {
  81   if (!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current())) {
  82     assert((OrderAccess::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) > 0, "sanity");
  83     // NOTE: It's ok to simply decrement, even with mask set, because unmasked value is positive.
  84     Atomic::dec(&_threads_in_evac);
  85   } else {
  86     // If we get here, the current thread has already gone through the
  87     // OOM-during-evac protocol and has thus either never entered or successfully left
  88     // the evacuation region. Simply flip its TL oom-during-evac flag back off.
  89     ShenandoahThreadLocalData::set_oom_during_evac(Thread::current(), false);
  90   }
  91   DEBUG_ONLY(ShenandoahThreadLocalData::set_evac_allowed(Thread::current(), false));
  92   assert(!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current()), "TL oom-during-evac must be turned off");
  93 }
  94 
  95 void ShenandoahEvacOOMHandler::handle_out_of_memory_during_evacuation() {
  96   assert(ShenandoahThreadLocalData::is_evac_allowed(Thread::current()), "sanity");
  97   assert(!ShenandoahThreadLocalData::is_oom_during_evac(Thread::current()), "TL oom-during-evac must not be set");
  98 
  99   jint threads_in_evac = OrderAccess::load_acquire(&_threads_in_evac);
 100   while (true) {
 101     jint other = Atomic::cmpxchg((threads_in_evac - 1) | OOM_MARKER_MASK,
 102                                   &_threads_in_evac, threads_in_evac);
 103     if (other == threads_in_evac) {
 104       // Success: wait for other threads to get out of the protocol and return.
 105       wait_for_no_evac_threads();
 106       return;
 107     } else {
 108       // Failure: try again with updated new value.
 109       threads_in_evac = other;
 110     }
 111   }
 112 }
 113 
 114 void ShenandoahEvacOOMHandler::clear() {
 115   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
 116   assert((OrderAccess::load_acquire(&_threads_in_evac) & ~OOM_MARKER_MASK) == 0, "sanity");
 117   OrderAccess::release_store_fence<jint>(&_threads_in_evac, 0);
 118 }
 119 
 120 ShenandoahEvacOOMScope::ShenandoahEvacOOMScope() {
 121   ShenandoahHeap::heap()->enter_evacuation();
 122 }
 123 
 124 ShenandoahEvacOOMScope::~ShenandoahEvacOOMScope() {
 125   ShenandoahHeap::heap()->leave_evacuation();
 126 }