1 /*
   2  * Copyright (c) 1997, 2019, 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.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "memory/universe.hpp"
  30 #include "logging/log.hpp"
  31 #include "runtime/atomic.hpp"
  32 #include "runtime/mutexLocker.inline.hpp"
  33 #include "runtime/safepoint.hpp"
  34 #include "runtime/thread.inline.hpp"
  35 #include "runtime/threadSMR.hpp"
  36 
  37 volatile jint GCLocker::_jni_lock_count = 0;
  38 volatile bool GCLocker::_needs_gc       = false;
  39 volatile bool GCLocker::_doing_gc       = false;
  40 unsigned int GCLocker::_total_collections = 0;
  41 
  42 #ifdef ASSERT
  43 volatile jint GCLocker::_debug_jni_lock_count = 0;
  44 #endif
  45 
  46 
  47 #ifdef ASSERT
  48 void GCLocker::verify_critical_count() {
  49   if (SafepointSynchronize::is_at_safepoint()) {
  50     assert(!needs_gc() || _debug_jni_lock_count == _jni_lock_count, "must agree");
  51     int count = 0;
  52     // Count the number of threads with critical operations in progress
  53     JavaThreadIteratorWithHandle jtiwh;
  54     for (; JavaThread *thr = jtiwh.next(); ) {
  55       if (thr->in_critical()) {
  56         count++;
  57       }
  58     }
  59     if (_jni_lock_count != count) {
  60       log_error(gc, verify)("critical counts don't match: %d != %d", _jni_lock_count, count);
  61       jtiwh.rewind();
  62       for (; JavaThread *thr = jtiwh.next(); ) {
  63         if (thr->in_critical()) {
  64           log_error(gc, verify)(INTPTR_FORMAT " in_critical %d", p2i(thr), thr->in_critical());
  65         }
  66       }
  67     }
  68     assert(_jni_lock_count == count, "must be equal");
  69   }
  70 }
  71 
  72 // In debug mode track the locking state at all times
  73 void GCLocker::increment_debug_jni_lock_count() {
  74   assert(_debug_jni_lock_count >= 0, "bad value");
  75   Atomic::inc(&_debug_jni_lock_count);
  76 }
  77 
  78 void GCLocker::decrement_debug_jni_lock_count() {
  79   assert(_debug_jni_lock_count > 0, "bad value");
  80   Atomic::dec(&_debug_jni_lock_count);
  81 }
  82 #endif
  83 
  84 void GCLocker::log_debug_jni(const char* msg) {
  85   Log(gc, jni) log;
  86   if (log.is_debug()) {
  87     ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
  88     log.debug("%s Thread \"%s\" %d locked.", msg, Thread::current()->name(), _jni_lock_count);
  89   }
  90 }
  91 
  92 bool GCLocker::is_at_safepoint() {
  93   return SafepointSynchronize::is_at_safepoint();
  94 }
  95 
  96 bool GCLocker::check_active_before_gc() {
  97   assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint");
  98   if (is_active() && !_needs_gc) {
  99     verify_critical_count();
 100     _needs_gc = true;
 101     log_debug_jni("Setting _needs_gc.");
 102   }
 103   return is_active();
 104 }
 105 
 106 void GCLocker::stall_until_clear() {
 107   assert(!JavaThread::current()->in_critical(), "Would deadlock");
 108   MonitorLocker ml(JNICritical_lock);
 109 
 110   if (needs_gc()) {
 111     log_debug_jni("Allocation failed. Thread stalled by JNI critical section.");
 112   }
 113 
 114   // Wait for _needs_gc  to be cleared
 115   while (needs_gc()) {
 116     ml.wait();
 117   }
 118 }
 119 
 120 bool GCLocker::should_discard(GCCause::Cause cause, uint total_collections) {
 121   return (cause == GCCause::_gc_locker) &&
 122          (_total_collections != total_collections);
 123 }
 124 
 125 void GCLocker::jni_lock(JavaThread* thread) {
 126   assert(!thread->in_critical(), "shouldn't currently be in a critical region");
 127   MonitorLocker ml(JNICritical_lock);
 128   // Block entering threads if we know at least one thread is in a
 129   // JNI critical region and we need a GC.
 130   // We check that at least one thread is in a critical region before
 131   // blocking because blocked threads are woken up by a thread exiting
 132   // a JNI critical region.
 133   while (is_active_and_needs_gc() || _doing_gc) {
 134     ml.wait();
 135   }
 136   thread->enter_critical();
 137   _jni_lock_count++;
 138   increment_debug_jni_lock_count();
 139 }
 140 
 141 void GCLocker::jni_unlock(JavaThread* thread) {
 142   assert(thread->in_last_critical(), "should be exiting critical region");
 143   MutexLocker mu(JNICritical_lock);
 144   _jni_lock_count--;
 145   decrement_debug_jni_lock_count();
 146   thread->exit_critical();
 147   if (needs_gc() && !is_active_internal()) {
 148     // We're the last thread out. Request a GC.
 149     // Capture the current total collections, to allow detection of
 150     // other collections that make this one unnecessary.  The value of
 151     // total_collections() is only changed at a safepoint, so there
 152     // must not be a safepoint between the lock becoming inactive and
 153     // getting the count, else there may be unnecessary GCLocker GCs.
 154     _total_collections = Universe::heap()->total_collections();
 155     _doing_gc = true;
 156     {
 157       // Must give up the lock while at a safepoint
 158       MutexUnlocker munlock(JNICritical_lock);
 159       log_debug_jni("Performing GC after exiting critical section.");
 160       Universe::heap()->collect(GCCause::_gc_locker);
 161     }
 162     _doing_gc = false;
 163     _needs_gc = false;
 164     JNICritical_lock->notify_all();
 165   }
 166 }