src/share/vm/memory/gcLocker.cpp

Wed, 31 Jul 2019 14:28:51 -0400

author
kbarrett
date
Wed, 31 Jul 2019 14:28:51 -0400
changeset 9787
9f28a4cac6d9
parent 6911
ce8f6bb717c9
child 9806
758c07667682
permissions
-rw-r--r--

8048556: Unnecessary GCLocker-initiated young GCs
Summary: Fixed recognition of unnecessary GCLocker collections.
Reviewed-by: pliden, tschatzl
Contributed-by: johnc@azul.com

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

mercurial