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