1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. 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 #include "gc/shared/collectedHeap.hpp"
  27 #include "gc/shared/gcLocker.inline.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "logging/log.hpp"
  30 #include "runtime/atomic.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 #include "runtime/threadSMR.hpp"
  33 
  34 volatile jint GCLocker::_jni_lock_count = 0;
  35 volatile bool GCLocker::_needs_gc       = false;
  36 volatile bool GCLocker::_doing_gc       = false;
  37 
  38 #ifdef ASSERT
  39 volatile jint GCLocker::_debug_jni_lock_count = 0;
  40 #endif
  41 
  42 
  43 #ifdef ASSERT
  44 void GCLocker::verify_critical_count() {
  45   if (SafepointSynchronize::is_at_safepoint()) {
  46     assert(!needs_gc() || _debug_jni_lock_count == _jni_lock_count, "must agree");
  47     int count = 0;
  48     // Count the number of threads with critical operations in progress
  49     JavaThreadIteratorWithHandle jtiwh;
  50     for (; JavaThread *thr = jtiwh.next(); ) {
  51       if (thr->in_critical()) {
  52         count++;
  53       }
  54     }
  55     if (_jni_lock_count != count) {
  56       log_error(gc, verify)("critical counts don't match: %d != %d", _jni_lock_count, count);
  57       jtiwh.rewind();
  58       for (; JavaThread *thr = jtiwh.next(); ) {
  59         if (thr->in_critical()) {
  60           log_error(gc, verify)(INTPTR_FORMAT " in_critical %d", p2i(thr), thr->in_critical());
  61         }
  62       }
  63     }
  64     assert(_jni_lock_count == count, "must be equal");
  65   }
  66 }
  67 
  68 // In debug mode track the locking state at all times
  69 void GCLocker::increment_debug_jni_lock_count() {
  70   assert(_debug_jni_lock_count >= 0, "bad value");
  71   Atomic::inc(&_debug_jni_lock_count);
  72 }
  73 
  74 void GCLocker::decrement_debug_jni_lock_count() {
  75   assert(_debug_jni_lock_count > 0, "bad value");
  76   Atomic::dec(&_debug_jni_lock_count);
  77 }
  78 #endif
  79 
  80 void GCLocker::log_debug_jni(const char* msg) {
  81   Log(gc, jni) log;
  82   if (log.is_debug()) {
  83     ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
  84     log.debug("%s Thread \"%s\" %d locked.", msg, Thread::current()->name(), _jni_lock_count);
  85   }
  86 }
  87 
  88 bool GCLocker::check_active_before_gc() {
  89   assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint");
  90   if (is_active() && !_needs_gc) {
  91     verify_critical_count();
  92     _needs_gc = true;
  93     log_debug_jni("Setting _needs_gc.");
  94   }
  95   return is_active();
  96 }
  97 
  98 void GCLocker::stall_until_clear() {
  99   assert(!JavaThread::current()->in_critical(), "Would deadlock");
 100   MutexLocker   ml(JNICritical_lock);
 101 
 102   if (needs_gc()) {
 103     log_debug_jni("Allocation failed. Thread stalled by JNI critical section.");
 104   }
 105 
 106   // Wait for _needs_gc  to be cleared
 107   while (needs_gc()) {
 108     JNICritical_lock->wait();
 109   }
 110 }
 111 
 112 void GCLocker::jni_lock(JavaThread* thread) {
 113   assert(!thread->in_critical(), "shouldn't currently be in a critical region");
 114   MutexLocker mu(JNICritical_lock);
 115   // Block entering threads if we know at least one thread is in a
 116   // JNI critical region and we need a GC.
 117   // We check that at least one thread is in a critical region before
 118   // blocking because blocked threads are woken up by a thread exiting
 119   // a JNI critical region.
 120   while (is_active_and_needs_gc() || _doing_gc) {
 121     JNICritical_lock->wait();
 122   }
 123   thread->enter_critical();
 124   _jni_lock_count++;
 125   increment_debug_jni_lock_count();
 126 }
 127 
 128 void GCLocker::jni_unlock(JavaThread* thread) {
 129   assert(thread->in_last_critical(), "should be exiting critical region");
 130   MutexLocker mu(JNICritical_lock);
 131   _jni_lock_count--;
 132   decrement_debug_jni_lock_count();
 133   thread->exit_critical();
 134   if (needs_gc() && !is_active_internal()) {
 135     // We're the last thread out. Cause a GC to occur.
 136     _doing_gc = true;
 137     {
 138       // Must give up the lock while at a safepoint
 139       MutexUnlocker munlock(JNICritical_lock);
 140       log_debug_jni("Performing GC after exiting critical section.");
 141       Universe::heap()->collect(GCCause::_gc_locker);
 142     }
 143     _doing_gc = false;
 144     _needs_gc = false;
 145     JNICritical_lock->notify_all();
 146   }
 147 }
 148 
 149 // Implementation of NoGCVerifier
 150 
 151 #ifdef ASSERT
 152 
 153 NoGCVerifier::NoGCVerifier(bool verifygc) {
 154   _verifygc = verifygc;
 155   if (_verifygc) {
 156     CollectedHeap* h = Universe::heap();
 157     assert(!h->is_gc_active(), "GC active during NoGCVerifier");
 158     _old_invocations = h->total_collections();
 159   }
 160 }
 161 
 162 
 163 NoGCVerifier::~NoGCVerifier() {
 164   if (_verifygc) {
 165     CollectedHeap* h = Universe::heap();
 166     assert(!h->is_gc_active(), "GC active during NoGCVerifier");
 167     if (_old_invocations != h->total_collections()) {
 168       fatal("collection in a NoGCVerifier secured function");
 169     }
 170   }
 171 }
 172 
 173 PauseNoGCVerifier::PauseNoGCVerifier(NoGCVerifier * ngcv) {
 174   _ngcv = ngcv;
 175   if (_ngcv->_verifygc) {
 176     // if we were verifying, then make sure that nothing is
 177     // wrong before we "pause" verification
 178     CollectedHeap* h = Universe::heap();
 179     assert(!h->is_gc_active(), "GC active during NoGCVerifier");
 180     if (_ngcv->_old_invocations != h->total_collections()) {
 181       fatal("collection in a NoGCVerifier secured function");
 182     }
 183   }
 184 }
 185 
 186 
 187 PauseNoGCVerifier::~PauseNoGCVerifier() {
 188   if (_ngcv->_verifygc) {
 189     // if we were verifying before, then reenable verification
 190     CollectedHeap* h = Universe::heap();
 191     assert(!h->is_gc_active(), "GC active during NoGCVerifier");
 192     _ngcv->_old_invocations = h->total_collections();
 193   }
 194 }
 195 
 196 
 197 // JRT_LEAF rules:
 198 // A JRT_LEAF method may not interfere with safepointing by
 199 //   1) acquiring or blocking on a Mutex or JavaLock - checked
 200 //   2) allocating heap memory - checked
 201 //   3) executing a VM operation - checked
 202 //   4) executing a system call (including malloc) that could block or grab a lock
 203 //   5) invoking GC
 204 //   6) reaching a safepoint
 205 //   7) running too long
 206 // Nor may any method it calls.
 207 JRTLeafVerifier::JRTLeafVerifier()
 208   : NoSafepointVerifier(true, JRTLeafVerifier::should_verify_GC())
 209 {
 210 }
 211 
 212 JRTLeafVerifier::~JRTLeafVerifier()
 213 {
 214 }
 215 
 216 bool JRTLeafVerifier::should_verify_GC() {
 217   switch (JavaThread::current()->thread_state()) {
 218   case _thread_in_Java:
 219     // is in a leaf routine, there must be no safepoint.
 220     return true;
 221   case _thread_in_native:
 222     // A native thread is not subject to safepoints.
 223     // Even while it is in a leaf routine, GC is ok
 224     return false;
 225   default:
 226     // Leaf routines cannot be called from other contexts.
 227     ShouldNotReachHere();
 228     return false;
 229   }
 230 }
 231 #endif