src/share/vm/memory/gcLocker.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial