Mon, 26 Jan 2009 12:47:21 -0800
6786503: Overflow list performance can be improved
Summary: Avoid overflow list walk in CMS & ParNew when it is unnecessary. Fix a couple of correctness issues, including a C-heap leak, in ParNew at the intersection of promotion failure, work queue overflow and object array chunking. Add stress testing option and related assertion checking.
Reviewed-by: jmasa
duke@435 | 1 | /* |
xdono@631 | 2 | * Copyright 1997-2008 Sun Microsystems, Inc. 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 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | # include "incls/_precompiled.incl" |
duke@435 | 26 | # include "incls/_gcLocker.cpp.incl" |
duke@435 | 27 | |
duke@435 | 28 | volatile jint GC_locker::_jni_lock_count = 0; |
duke@435 | 29 | volatile jint GC_locker::_lock_count = 0; |
duke@435 | 30 | volatile bool GC_locker::_needs_gc = false; |
duke@435 | 31 | volatile bool GC_locker::_doing_gc = false; |
duke@435 | 32 | |
duke@435 | 33 | void GC_locker::stall_until_clear() { |
duke@435 | 34 | assert(!JavaThread::current()->in_critical(), "Would deadlock"); |
apetrusenko@574 | 35 | if (PrintJNIGCStalls && PrintGCDetails) { |
apetrusenko@574 | 36 | ResourceMark rm; // JavaThread::name() allocates to convert to UTF8 |
apetrusenko@574 | 37 | gclog_or_tty->print_cr( |
apetrusenko@574 | 38 | "Allocation failed. Thread \"%s\" is stalled by JNI critical section.", |
apetrusenko@574 | 39 | JavaThread::current()->name()); |
apetrusenko@574 | 40 | } |
duke@435 | 41 | MutexLocker ml(JNICritical_lock); |
duke@435 | 42 | // Wait for _needs_gc to be cleared |
duke@435 | 43 | while (GC_locker::needs_gc()) { |
duke@435 | 44 | JNICritical_lock->wait(); |
duke@435 | 45 | } |
duke@435 | 46 | } |
duke@435 | 47 | |
duke@435 | 48 | void GC_locker::jni_lock_slow() { |
duke@435 | 49 | MutexLocker mu(JNICritical_lock); |
duke@435 | 50 | // Block entering threads if we know at least one thread is in a |
duke@435 | 51 | // JNI critical region and we need a GC. |
duke@435 | 52 | // We check that at least one thread is in a critical region before |
duke@435 | 53 | // blocking because blocked threads are woken up by a thread exiting |
duke@435 | 54 | // a JNI critical region. |
duke@435 | 55 | while ((is_jni_active() && needs_gc()) || _doing_gc) { |
duke@435 | 56 | JNICritical_lock->wait(); |
duke@435 | 57 | } |
duke@435 | 58 | jni_lock(); |
duke@435 | 59 | } |
duke@435 | 60 | |
duke@435 | 61 | void GC_locker::jni_unlock_slow() { |
duke@435 | 62 | MutexLocker mu(JNICritical_lock); |
duke@435 | 63 | jni_unlock(); |
duke@435 | 64 | if (needs_gc() && !is_jni_active()) { |
duke@435 | 65 | // We're the last thread out. Cause a GC to occur. |
duke@435 | 66 | // GC will also check is_active, so this check is not |
duke@435 | 67 | // strictly needed. It's added here to make it clear that |
duke@435 | 68 | // the GC will NOT be performed if any other caller |
duke@435 | 69 | // of GC_locker::lock() still needs GC locked. |
duke@435 | 70 | if (!is_active()) { |
duke@435 | 71 | _doing_gc = true; |
duke@435 | 72 | { |
duke@435 | 73 | // Must give up the lock while at a safepoint |
duke@435 | 74 | MutexUnlocker munlock(JNICritical_lock); |
duke@435 | 75 | Universe::heap()->collect(GCCause::_gc_locker); |
duke@435 | 76 | } |
duke@435 | 77 | _doing_gc = false; |
duke@435 | 78 | } |
duke@435 | 79 | clear_needs_gc(); |
duke@435 | 80 | JNICritical_lock->notify_all(); |
duke@435 | 81 | } |
duke@435 | 82 | } |
duke@435 | 83 | |
duke@435 | 84 | // Implementation of No_GC_Verifier |
duke@435 | 85 | |
duke@435 | 86 | #ifdef ASSERT |
duke@435 | 87 | |
duke@435 | 88 | No_GC_Verifier::No_GC_Verifier(bool verifygc) { |
duke@435 | 89 | _verifygc = verifygc; |
duke@435 | 90 | if (_verifygc) { |
duke@435 | 91 | CollectedHeap* h = Universe::heap(); |
duke@435 | 92 | assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); |
duke@435 | 93 | _old_invocations = h->total_collections(); |
duke@435 | 94 | } |
duke@435 | 95 | } |
duke@435 | 96 | |
duke@435 | 97 | |
duke@435 | 98 | No_GC_Verifier::~No_GC_Verifier() { |
duke@435 | 99 | if (_verifygc) { |
duke@435 | 100 | CollectedHeap* h = Universe::heap(); |
duke@435 | 101 | assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); |
duke@435 | 102 | if (_old_invocations != h->total_collections()) { |
duke@435 | 103 | fatal("collection in a No_GC_Verifier secured function"); |
duke@435 | 104 | } |
duke@435 | 105 | } |
duke@435 | 106 | } |
duke@435 | 107 | |
duke@435 | 108 | Pause_No_GC_Verifier::Pause_No_GC_Verifier(No_GC_Verifier * ngcv) { |
duke@435 | 109 | _ngcv = ngcv; |
duke@435 | 110 | if (_ngcv->_verifygc) { |
duke@435 | 111 | // if we were verifying, then make sure that nothing is |
duke@435 | 112 | // wrong before we "pause" verification |
duke@435 | 113 | CollectedHeap* h = Universe::heap(); |
duke@435 | 114 | assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); |
duke@435 | 115 | if (_ngcv->_old_invocations != h->total_collections()) { |
duke@435 | 116 | fatal("collection in a No_GC_Verifier secured function"); |
duke@435 | 117 | } |
duke@435 | 118 | } |
duke@435 | 119 | } |
duke@435 | 120 | |
duke@435 | 121 | |
duke@435 | 122 | Pause_No_GC_Verifier::~Pause_No_GC_Verifier() { |
duke@435 | 123 | if (_ngcv->_verifygc) { |
duke@435 | 124 | // if we were verifying before, then reenable verification |
duke@435 | 125 | CollectedHeap* h = Universe::heap(); |
duke@435 | 126 | assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); |
duke@435 | 127 | _ngcv->_old_invocations = h->total_collections(); |
duke@435 | 128 | } |
duke@435 | 129 | } |
duke@435 | 130 | |
duke@435 | 131 | |
duke@435 | 132 | // JRT_LEAF rules: |
duke@435 | 133 | // A JRT_LEAF method may not interfere with safepointing by |
duke@435 | 134 | // 1) acquiring or blocking on a Mutex or JavaLock - checked |
duke@435 | 135 | // 2) allocating heap memory - checked |
duke@435 | 136 | // 3) executing a VM operation - checked |
duke@435 | 137 | // 4) executing a system call (including malloc) that could block or grab a lock |
duke@435 | 138 | // 5) invoking GC |
duke@435 | 139 | // 6) reaching a safepoint |
duke@435 | 140 | // 7) running too long |
duke@435 | 141 | // Nor may any method it calls. |
duke@435 | 142 | JRT_Leaf_Verifier::JRT_Leaf_Verifier() |
duke@435 | 143 | : No_Safepoint_Verifier(true, JRT_Leaf_Verifier::should_verify_GC()) |
duke@435 | 144 | { |
duke@435 | 145 | } |
duke@435 | 146 | |
duke@435 | 147 | JRT_Leaf_Verifier::~JRT_Leaf_Verifier() |
duke@435 | 148 | { |
duke@435 | 149 | } |
duke@435 | 150 | |
duke@435 | 151 | bool JRT_Leaf_Verifier::should_verify_GC() { |
duke@435 | 152 | switch (JavaThread::current()->thread_state()) { |
duke@435 | 153 | case _thread_in_Java: |
duke@435 | 154 | // is in a leaf routine, there must be no safepoint. |
duke@435 | 155 | return true; |
duke@435 | 156 | case _thread_in_native: |
duke@435 | 157 | // A native thread is not subject to safepoints. |
duke@435 | 158 | // Even while it is in a leaf routine, GC is ok |
duke@435 | 159 | return false; |
duke@435 | 160 | default: |
duke@435 | 161 | // Leaf routines cannot be called from other contexts. |
duke@435 | 162 | ShouldNotReachHere(); |
duke@435 | 163 | return false; |
duke@435 | 164 | } |
duke@435 | 165 | } |
duke@435 | 166 | #endif |