aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "memory/gcLocker.inline.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "memory/sharedHeap.hpp" aoqi@0: aoqi@0: volatile jint GC_locker::_jni_lock_count = 0; aoqi@0: volatile bool GC_locker::_needs_gc = false; aoqi@0: volatile bool GC_locker::_doing_gc = false; aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: volatile jint GC_locker::_debug_jni_lock_count = 0; aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: void GC_locker::verify_critical_count() { aoqi@0: if (SafepointSynchronize::is_at_safepoint()) { aoqi@0: assert(!needs_gc() || _debug_jni_lock_count == _jni_lock_count, "must agree"); aoqi@0: int count = 0; aoqi@0: // Count the number of threads with critical operations in progress aoqi@0: for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) { aoqi@0: if (thr->in_critical()) { aoqi@0: count++; aoqi@0: } aoqi@0: } aoqi@0: if (_jni_lock_count != count) { aoqi@0: tty->print_cr("critical counts don't match: %d != %d", _jni_lock_count, count); aoqi@0: for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) { aoqi@0: if (thr->in_critical()) { aoqi@0: tty->print_cr(INTPTR_FORMAT " in_critical %d", p2i(thr), thr->in_critical()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: assert(_jni_lock_count == count, "must be equal"); aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: bool GC_locker::check_active_before_gc() { aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint"); aoqi@0: if (is_active() && !_needs_gc) { aoqi@0: verify_critical_count(); aoqi@0: _needs_gc = true; aoqi@0: if (PrintJNIGCStalls && PrintGCDetails) { aoqi@0: ResourceMark rm; // JavaThread::name() allocates to convert to UTF8 aoqi@0: gclog_or_tty->print_cr("%.3f: Setting _needs_gc. Thread \"%s\" %d locked.", aoqi@0: gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: return is_active(); aoqi@0: } aoqi@0: aoqi@0: void GC_locker::stall_until_clear() { aoqi@0: assert(!JavaThread::current()->in_critical(), "Would deadlock"); aoqi@0: MutexLocker ml(JNICritical_lock); aoqi@0: aoqi@0: if (needs_gc()) { aoqi@0: if (PrintJNIGCStalls && PrintGCDetails) { aoqi@0: ResourceMark rm; // JavaThread::name() allocates to convert to UTF8 aoqi@0: gclog_or_tty->print_cr("%.3f: Allocation failed. Thread \"%s\" is stalled by JNI critical section, %d locked.", aoqi@0: gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Wait for _needs_gc to be cleared aoqi@0: while (needs_gc()) { aoqi@0: JNICritical_lock->wait(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GC_locker::jni_lock(JavaThread* thread) { aoqi@0: assert(!thread->in_critical(), "shouldn't currently be in a critical region"); aoqi@0: MutexLocker mu(JNICritical_lock); aoqi@0: // Block entering threads if we know at least one thread is in a aoqi@0: // JNI critical region and we need a GC. aoqi@0: // We check that at least one thread is in a critical region before aoqi@0: // blocking because blocked threads are woken up by a thread exiting aoqi@0: // a JNI critical region. aoqi@0: while (is_active_and_needs_gc() || _doing_gc) { aoqi@0: JNICritical_lock->wait(); aoqi@0: } aoqi@0: thread->enter_critical(); aoqi@0: _jni_lock_count++; aoqi@0: increment_debug_jni_lock_count(); aoqi@0: } aoqi@0: aoqi@0: void GC_locker::jni_unlock(JavaThread* thread) { aoqi@0: assert(thread->in_last_critical(), "should be exiting critical region"); aoqi@0: MutexLocker mu(JNICritical_lock); aoqi@0: _jni_lock_count--; aoqi@0: decrement_debug_jni_lock_count(); aoqi@0: thread->exit_critical(); aoqi@0: if (needs_gc() && !is_active_internal()) { aoqi@0: // We're the last thread out. Cause a GC to occur. aoqi@0: _doing_gc = true; aoqi@0: { aoqi@0: // Must give up the lock while at a safepoint aoqi@0: MutexUnlocker munlock(JNICritical_lock); aoqi@0: if (PrintJNIGCStalls && PrintGCDetails) { aoqi@0: ResourceMark rm; // JavaThread::name() allocates to convert to UTF8 aoqi@0: gclog_or_tty->print_cr("%.3f: Thread \"%s\" is performing GC after exiting critical section, %d locked", aoqi@0: gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count); aoqi@0: } aoqi@0: Universe::heap()->collect(GCCause::_gc_locker); aoqi@0: } aoqi@0: _doing_gc = false; aoqi@0: _needs_gc = false; aoqi@0: JNICritical_lock->notify_all(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Implementation of No_GC_Verifier aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: aoqi@0: No_GC_Verifier::No_GC_Verifier(bool verifygc) { aoqi@0: _verifygc = verifygc; aoqi@0: if (_verifygc) { aoqi@0: CollectedHeap* h = Universe::heap(); aoqi@0: assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); aoqi@0: _old_invocations = h->total_collections(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: No_GC_Verifier::~No_GC_Verifier() { aoqi@0: if (_verifygc) { aoqi@0: CollectedHeap* h = Universe::heap(); aoqi@0: assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); aoqi@0: if (_old_invocations != h->total_collections()) { aoqi@0: fatal("collection in a No_GC_Verifier secured function"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: Pause_No_GC_Verifier::Pause_No_GC_Verifier(No_GC_Verifier * ngcv) { aoqi@0: _ngcv = ngcv; aoqi@0: if (_ngcv->_verifygc) { aoqi@0: // if we were verifying, then make sure that nothing is aoqi@0: // wrong before we "pause" verification aoqi@0: CollectedHeap* h = Universe::heap(); aoqi@0: assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); aoqi@0: if (_ngcv->_old_invocations != h->total_collections()) { aoqi@0: fatal("collection in a No_GC_Verifier secured function"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: Pause_No_GC_Verifier::~Pause_No_GC_Verifier() { aoqi@0: if (_ngcv->_verifygc) { aoqi@0: // if we were verifying before, then reenable verification aoqi@0: CollectedHeap* h = Universe::heap(); aoqi@0: assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); aoqi@0: _ngcv->_old_invocations = h->total_collections(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // JRT_LEAF rules: aoqi@0: // A JRT_LEAF method may not interfere with safepointing by aoqi@0: // 1) acquiring or blocking on a Mutex or JavaLock - checked aoqi@0: // 2) allocating heap memory - checked aoqi@0: // 3) executing a VM operation - checked aoqi@0: // 4) executing a system call (including malloc) that could block or grab a lock aoqi@0: // 5) invoking GC aoqi@0: // 6) reaching a safepoint aoqi@0: // 7) running too long aoqi@0: // Nor may any method it calls. aoqi@0: JRT_Leaf_Verifier::JRT_Leaf_Verifier() aoqi@0: : No_Safepoint_Verifier(true, JRT_Leaf_Verifier::should_verify_GC()) aoqi@0: { aoqi@0: } aoqi@0: aoqi@0: JRT_Leaf_Verifier::~JRT_Leaf_Verifier() aoqi@0: { aoqi@0: } aoqi@0: aoqi@0: bool JRT_Leaf_Verifier::should_verify_GC() { aoqi@0: switch (JavaThread::current()->thread_state()) { aoqi@0: case _thread_in_Java: aoqi@0: // is in a leaf routine, there must be no safepoint. aoqi@0: return true; aoqi@0: case _thread_in_native: aoqi@0: // A native thread is not subject to safepoints. aoqi@0: // Even while it is in a leaf routine, GC is ok aoqi@0: return false; aoqi@0: default: aoqi@0: // Leaf routines cannot be called from other contexts. aoqi@0: ShouldNotReachHere(); aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: #endif