Thu, 12 Oct 2017 21:27:07 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #include "precompiled.hpp" |
aoqi@0 | 26 | #include "memory/gcLocker.inline.hpp" |
aoqi@0 | 27 | #include "memory/resourceArea.hpp" |
aoqi@0 | 28 | #include "memory/sharedHeap.hpp" |
goetz@6911 | 29 | #include "runtime/thread.inline.hpp" |
aoqi@0 | 30 | |
aoqi@0 | 31 | volatile jint GC_locker::_jni_lock_count = 0; |
aoqi@0 | 32 | volatile bool GC_locker::_needs_gc = false; |
aoqi@0 | 33 | volatile bool GC_locker::_doing_gc = false; |
aoqi@0 | 34 | |
aoqi@0 | 35 | #ifdef ASSERT |
aoqi@0 | 36 | volatile jint GC_locker::_debug_jni_lock_count = 0; |
aoqi@0 | 37 | #endif |
aoqi@0 | 38 | |
aoqi@0 | 39 | |
aoqi@0 | 40 | #ifdef ASSERT |
aoqi@0 | 41 | void GC_locker::verify_critical_count() { |
aoqi@0 | 42 | if (SafepointSynchronize::is_at_safepoint()) { |
aoqi@0 | 43 | assert(!needs_gc() || _debug_jni_lock_count == _jni_lock_count, "must agree"); |
aoqi@0 | 44 | int count = 0; |
aoqi@0 | 45 | // Count the number of threads with critical operations in progress |
aoqi@0 | 46 | for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) { |
aoqi@0 | 47 | if (thr->in_critical()) { |
aoqi@0 | 48 | count++; |
aoqi@0 | 49 | } |
aoqi@0 | 50 | } |
aoqi@0 | 51 | if (_jni_lock_count != count) { |
aoqi@0 | 52 | tty->print_cr("critical counts don't match: %d != %d", _jni_lock_count, count); |
aoqi@0 | 53 | for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) { |
aoqi@0 | 54 | if (thr->in_critical()) { |
aoqi@0 | 55 | tty->print_cr(INTPTR_FORMAT " in_critical %d", p2i(thr), thr->in_critical()); |
aoqi@0 | 56 | } |
aoqi@0 | 57 | } |
aoqi@0 | 58 | } |
aoqi@0 | 59 | assert(_jni_lock_count == count, "must be equal"); |
aoqi@0 | 60 | } |
aoqi@0 | 61 | } |
aoqi@0 | 62 | #endif |
aoqi@0 | 63 | |
aoqi@0 | 64 | bool GC_locker::check_active_before_gc() { |
aoqi@0 | 65 | assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint"); |
aoqi@0 | 66 | if (is_active() && !_needs_gc) { |
aoqi@0 | 67 | verify_critical_count(); |
aoqi@0 | 68 | _needs_gc = true; |
aoqi@0 | 69 | if (PrintJNIGCStalls && PrintGCDetails) { |
aoqi@0 | 70 | ResourceMark rm; // JavaThread::name() allocates to convert to UTF8 |
aoqi@0 | 71 | gclog_or_tty->print_cr("%.3f: Setting _needs_gc. Thread \"%s\" %d locked.", |
aoqi@0 | 72 | gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count); |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | } |
aoqi@0 | 76 | return is_active(); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | |
aoqi@0 | 79 | void GC_locker::stall_until_clear() { |
aoqi@0 | 80 | assert(!JavaThread::current()->in_critical(), "Would deadlock"); |
aoqi@0 | 81 | MutexLocker ml(JNICritical_lock); |
aoqi@0 | 82 | |
aoqi@0 | 83 | if (needs_gc()) { |
aoqi@0 | 84 | if (PrintJNIGCStalls && PrintGCDetails) { |
aoqi@0 | 85 | ResourceMark rm; // JavaThread::name() allocates to convert to UTF8 |
aoqi@0 | 86 | gclog_or_tty->print_cr("%.3f: Allocation failed. Thread \"%s\" is stalled by JNI critical section, %d locked.", |
aoqi@0 | 87 | gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count); |
aoqi@0 | 88 | } |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | // Wait for _needs_gc to be cleared |
aoqi@0 | 92 | while (needs_gc()) { |
aoqi@0 | 93 | JNICritical_lock->wait(); |
aoqi@0 | 94 | } |
aoqi@0 | 95 | } |
aoqi@0 | 96 | |
aoqi@0 | 97 | void GC_locker::jni_lock(JavaThread* thread) { |
aoqi@0 | 98 | assert(!thread->in_critical(), "shouldn't currently be in a critical region"); |
aoqi@0 | 99 | MutexLocker mu(JNICritical_lock); |
aoqi@0 | 100 | // Block entering threads if we know at least one thread is in a |
aoqi@0 | 101 | // JNI critical region and we need a GC. |
aoqi@0 | 102 | // We check that at least one thread is in a critical region before |
aoqi@0 | 103 | // blocking because blocked threads are woken up by a thread exiting |
aoqi@0 | 104 | // a JNI critical region. |
aoqi@0 | 105 | while (is_active_and_needs_gc() || _doing_gc) { |
aoqi@0 | 106 | JNICritical_lock->wait(); |
aoqi@0 | 107 | } |
aoqi@0 | 108 | thread->enter_critical(); |
aoqi@0 | 109 | _jni_lock_count++; |
aoqi@0 | 110 | increment_debug_jni_lock_count(); |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | void GC_locker::jni_unlock(JavaThread* thread) { |
aoqi@0 | 114 | assert(thread->in_last_critical(), "should be exiting critical region"); |
aoqi@0 | 115 | MutexLocker mu(JNICritical_lock); |
aoqi@0 | 116 | _jni_lock_count--; |
aoqi@0 | 117 | decrement_debug_jni_lock_count(); |
aoqi@0 | 118 | thread->exit_critical(); |
aoqi@0 | 119 | if (needs_gc() && !is_active_internal()) { |
aoqi@0 | 120 | // We're the last thread out. Cause a GC to occur. |
aoqi@0 | 121 | _doing_gc = true; |
aoqi@0 | 122 | { |
aoqi@0 | 123 | // Must give up the lock while at a safepoint |
aoqi@0 | 124 | MutexUnlocker munlock(JNICritical_lock); |
aoqi@0 | 125 | if (PrintJNIGCStalls && PrintGCDetails) { |
aoqi@0 | 126 | ResourceMark rm; // JavaThread::name() allocates to convert to UTF8 |
aoqi@0 | 127 | gclog_or_tty->print_cr("%.3f: Thread \"%s\" is performing GC after exiting critical section, %d locked", |
aoqi@0 | 128 | gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count); |
aoqi@0 | 129 | } |
aoqi@0 | 130 | Universe::heap()->collect(GCCause::_gc_locker); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | _doing_gc = false; |
aoqi@0 | 133 | _needs_gc = false; |
aoqi@0 | 134 | JNICritical_lock->notify_all(); |
aoqi@0 | 135 | } |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | // Implementation of No_GC_Verifier |
aoqi@0 | 139 | |
aoqi@0 | 140 | #ifdef ASSERT |
aoqi@0 | 141 | |
aoqi@0 | 142 | No_GC_Verifier::No_GC_Verifier(bool verifygc) { |
aoqi@0 | 143 | _verifygc = verifygc; |
aoqi@0 | 144 | if (_verifygc) { |
aoqi@0 | 145 | CollectedHeap* h = Universe::heap(); |
aoqi@0 | 146 | assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); |
aoqi@0 | 147 | _old_invocations = h->total_collections(); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | } |
aoqi@0 | 150 | |
aoqi@0 | 151 | |
aoqi@0 | 152 | No_GC_Verifier::~No_GC_Verifier() { |
aoqi@0 | 153 | if (_verifygc) { |
aoqi@0 | 154 | CollectedHeap* h = Universe::heap(); |
aoqi@0 | 155 | assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); |
aoqi@0 | 156 | if (_old_invocations != h->total_collections()) { |
aoqi@0 | 157 | fatal("collection in a No_GC_Verifier secured function"); |
aoqi@0 | 158 | } |
aoqi@0 | 159 | } |
aoqi@0 | 160 | } |
aoqi@0 | 161 | |
aoqi@0 | 162 | Pause_No_GC_Verifier::Pause_No_GC_Verifier(No_GC_Verifier * ngcv) { |
aoqi@0 | 163 | _ngcv = ngcv; |
aoqi@0 | 164 | if (_ngcv->_verifygc) { |
aoqi@0 | 165 | // if we were verifying, then make sure that nothing is |
aoqi@0 | 166 | // wrong before we "pause" verification |
aoqi@0 | 167 | CollectedHeap* h = Universe::heap(); |
aoqi@0 | 168 | assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); |
aoqi@0 | 169 | if (_ngcv->_old_invocations != h->total_collections()) { |
aoqi@0 | 170 | fatal("collection in a No_GC_Verifier secured function"); |
aoqi@0 | 171 | } |
aoqi@0 | 172 | } |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | |
aoqi@0 | 176 | Pause_No_GC_Verifier::~Pause_No_GC_Verifier() { |
aoqi@0 | 177 | if (_ngcv->_verifygc) { |
aoqi@0 | 178 | // if we were verifying before, then reenable verification |
aoqi@0 | 179 | CollectedHeap* h = Universe::heap(); |
aoqi@0 | 180 | assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); |
aoqi@0 | 181 | _ngcv->_old_invocations = h->total_collections(); |
aoqi@0 | 182 | } |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | |
aoqi@0 | 186 | // JRT_LEAF rules: |
aoqi@0 | 187 | // A JRT_LEAF method may not interfere with safepointing by |
aoqi@0 | 188 | // 1) acquiring or blocking on a Mutex or JavaLock - checked |
aoqi@0 | 189 | // 2) allocating heap memory - checked |
aoqi@0 | 190 | // 3) executing a VM operation - checked |
aoqi@0 | 191 | // 4) executing a system call (including malloc) that could block or grab a lock |
aoqi@0 | 192 | // 5) invoking GC |
aoqi@0 | 193 | // 6) reaching a safepoint |
aoqi@0 | 194 | // 7) running too long |
aoqi@0 | 195 | // Nor may any method it calls. |
aoqi@0 | 196 | JRT_Leaf_Verifier::JRT_Leaf_Verifier() |
aoqi@0 | 197 | : No_Safepoint_Verifier(true, JRT_Leaf_Verifier::should_verify_GC()) |
aoqi@0 | 198 | { |
aoqi@0 | 199 | } |
aoqi@0 | 200 | |
aoqi@0 | 201 | JRT_Leaf_Verifier::~JRT_Leaf_Verifier() |
aoqi@0 | 202 | { |
aoqi@0 | 203 | } |
aoqi@0 | 204 | |
aoqi@0 | 205 | bool JRT_Leaf_Verifier::should_verify_GC() { |
aoqi@0 | 206 | switch (JavaThread::current()->thread_state()) { |
aoqi@0 | 207 | case _thread_in_Java: |
aoqi@0 | 208 | // is in a leaf routine, there must be no safepoint. |
aoqi@0 | 209 | return true; |
aoqi@0 | 210 | case _thread_in_native: |
aoqi@0 | 211 | // A native thread is not subject to safepoints. |
aoqi@0 | 212 | // Even while it is in a leaf routine, GC is ok |
aoqi@0 | 213 | return false; |
aoqi@0 | 214 | default: |
aoqi@0 | 215 | // Leaf routines cannot be called from other contexts. |
aoqi@0 | 216 | ShouldNotReachHere(); |
aoqi@0 | 217 | return false; |
aoqi@0 | 218 | } |
aoqi@0 | 219 | } |
aoqi@0 | 220 | #endif |