src/share/vm/gc_implementation/concurrentMarkSweep/vmCMSOperations.cpp

Thu, 06 Jan 2011 23:50:02 -0800

author
ysr
date
Thu, 06 Jan 2011 23:50:02 -0800
changeset 2452
4947ee68d19c
parent 2314
f95d63e2154a
child 2647
a181f3a124dd
permissions
-rw-r--r--

7008136: CMS: assert((HeapWord*)nextChunk <= _limit) failed: sweep invariant
Summary: The recorded _sweep_limit may not necessarily remain a block boundary as the old generation expands during a concurrent cycle. Terminal actions inside the sweep closure need to be aware of this as they cross over the limit.
Reviewed-by: johnc, minqi

duke@435 1 /*
stefank@2314 2 * Copyright (c) 2005, 2010, 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 "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp"
stefank@2314 27 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
stefank@2314 28 #include "gc_implementation/concurrentMarkSweep/vmCMSOperations.hpp"
stefank@2314 29 #include "gc_implementation/shared/isGCActiveMark.hpp"
stefank@2314 30 #include "memory/gcLocker.inline.hpp"
stefank@2314 31 #include "runtime/interfaceSupport.hpp"
stefank@2314 32 #include "utilities/dtrace.hpp"
duke@435 33 HS_DTRACE_PROBE_DECL(hs_private, cms__initmark__begin);
duke@435 34 HS_DTRACE_PROBE_DECL(hs_private, cms__initmark__end);
duke@435 35
duke@435 36 HS_DTRACE_PROBE_DECL(hs_private, cms__remark__begin);
duke@435 37 HS_DTRACE_PROBE_DECL(hs_private, cms__remark__end);
duke@435 38
duke@435 39 //////////////////////////////////////////////////////////
duke@435 40 // Methods in abstract class VM_CMS_Operation
duke@435 41 //////////////////////////////////////////////////////////
duke@435 42 void VM_CMS_Operation::acquire_pending_list_lock() {
duke@435 43 // The caller may block while communicating
duke@435 44 // with the SLT thread in order to acquire/release the PLL.
duke@435 45 ConcurrentMarkSweepThread::slt()->
duke@435 46 manipulatePLL(SurrogateLockerThread::acquirePLL);
duke@435 47 }
duke@435 48
duke@435 49 void VM_CMS_Operation::release_and_notify_pending_list_lock() {
duke@435 50 // The caller may block while communicating
duke@435 51 // with the SLT thread in order to acquire/release the PLL.
duke@435 52 ConcurrentMarkSweepThread::slt()->
duke@435 53 manipulatePLL(SurrogateLockerThread::releaseAndNotifyPLL);
duke@435 54 }
duke@435 55
duke@435 56 void VM_CMS_Operation::verify_before_gc() {
duke@435 57 if (VerifyBeforeGC &&
duke@435 58 GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
duke@435 59 HandleMark hm;
duke@435 60 FreelistLocker x(_collector);
duke@435 61 MutexLockerEx y(_collector->bitMapLock(), Mutex::_no_safepoint_check_flag);
duke@435 62 Universe::heap()->prepare_for_verify();
duke@435 63 Universe::verify(true);
duke@435 64 }
duke@435 65 }
duke@435 66
duke@435 67 void VM_CMS_Operation::verify_after_gc() {
duke@435 68 if (VerifyAfterGC &&
duke@435 69 GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
duke@435 70 HandleMark hm;
duke@435 71 FreelistLocker x(_collector);
duke@435 72 MutexLockerEx y(_collector->bitMapLock(), Mutex::_no_safepoint_check_flag);
duke@435 73 Universe::verify(true);
duke@435 74 }
duke@435 75 }
duke@435 76
duke@435 77 bool VM_CMS_Operation::lost_race() const {
duke@435 78 if (CMSCollector::abstract_state() == CMSCollector::Idling) {
duke@435 79 // We lost a race to a foreground collection
duke@435 80 // -- there's nothing to do
duke@435 81 return true;
duke@435 82 }
duke@435 83 assert(CMSCollector::abstract_state() == legal_state(),
duke@435 84 "Inconsistent collector state?");
duke@435 85 return false;
duke@435 86 }
duke@435 87
duke@435 88 bool VM_CMS_Operation::doit_prologue() {
duke@435 89 assert(Thread::current()->is_ConcurrentGC_thread(), "just checking");
duke@435 90 assert(!CMSCollector::foregroundGCShouldWait(), "Possible deadlock");
duke@435 91 assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(),
duke@435 92 "Possible deadlock");
duke@435 93
duke@435 94 if (needs_pll()) {
duke@435 95 acquire_pending_list_lock();
duke@435 96 }
duke@435 97 // Get the Heap_lock after the pending_list_lock.
duke@435 98 Heap_lock->lock();
duke@435 99 if (lost_race()) {
duke@435 100 assert(_prologue_succeeded == false, "Initialized in c'tor");
duke@435 101 Heap_lock->unlock();
duke@435 102 if (needs_pll()) {
duke@435 103 release_and_notify_pending_list_lock();
duke@435 104 }
duke@435 105 } else {
duke@435 106 _prologue_succeeded = true;
duke@435 107 }
duke@435 108 return _prologue_succeeded;
duke@435 109 }
duke@435 110
duke@435 111 void VM_CMS_Operation::doit_epilogue() {
duke@435 112 assert(Thread::current()->is_ConcurrentGC_thread(), "just checking");
duke@435 113 assert(!CMSCollector::foregroundGCShouldWait(), "Possible deadlock");
duke@435 114 assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(),
duke@435 115 "Possible deadlock");
duke@435 116
duke@435 117 // Release the Heap_lock first.
duke@435 118 Heap_lock->unlock();
duke@435 119 if (needs_pll()) {
duke@435 120 release_and_notify_pending_list_lock();
duke@435 121 }
duke@435 122 }
duke@435 123
duke@435 124 //////////////////////////////////////////////////////////
duke@435 125 // Methods in class VM_CMS_Initial_Mark
duke@435 126 //////////////////////////////////////////////////////////
duke@435 127 void VM_CMS_Initial_Mark::doit() {
duke@435 128 if (lost_race()) {
duke@435 129 // Nothing to do.
duke@435 130 return;
duke@435 131 }
duke@435 132 HS_DTRACE_PROBE(hs_private, cms__initmark__begin);
duke@435 133
duke@435 134 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 135 GCCauseSetter gccs(gch, GCCause::_cms_initial_mark);
duke@435 136
duke@435 137 VM_CMS_Operation::verify_before_gc();
duke@435 138
duke@435 139 IsGCActiveMark x; // stop-world GC active
duke@435 140 _collector->do_CMS_operation(CMSCollector::CMS_op_checkpointRootsInitial);
duke@435 141
duke@435 142 VM_CMS_Operation::verify_after_gc();
duke@435 143 HS_DTRACE_PROBE(hs_private, cms__initmark__end);
duke@435 144 }
duke@435 145
duke@435 146 //////////////////////////////////////////////////////////
duke@435 147 // Methods in class VM_CMS_Final_Remark_Operation
duke@435 148 //////////////////////////////////////////////////////////
duke@435 149 void VM_CMS_Final_Remark::doit() {
duke@435 150 if (lost_race()) {
duke@435 151 // Nothing to do.
duke@435 152 return;
duke@435 153 }
duke@435 154 HS_DTRACE_PROBE(hs_private, cms__remark__begin);
duke@435 155
duke@435 156 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 157 GCCauseSetter gccs(gch, GCCause::_cms_final_remark);
duke@435 158
duke@435 159 VM_CMS_Operation::verify_before_gc();
duke@435 160
duke@435 161 IsGCActiveMark x; // stop-world GC active
duke@435 162 _collector->do_CMS_operation(CMSCollector::CMS_op_checkpointRootsFinal);
duke@435 163
duke@435 164 VM_CMS_Operation::verify_after_gc();
duke@435 165 HS_DTRACE_PROBE(hs_private, cms__remark__end);
duke@435 166 }
duke@435 167
duke@435 168 // VM operation to invoke a concurrent collection of a
duke@435 169 // GenCollectedHeap heap.
duke@435 170 void VM_GenCollectFullConcurrent::doit() {
duke@435 171 assert(Thread::current()->is_VM_thread(), "Should be VM thread");
ysr@1875 172 assert(GCLockerInvokesConcurrent || ExplicitGCInvokesConcurrent, "Unexpected");
duke@435 173
duke@435 174 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 175 if (_gc_count_before == gch->total_collections()) {
duke@435 176 // The "full" of do_full_collection call below "forces"
duke@435 177 // a collection; the second arg, 0, below ensures that
duke@435 178 // only the young gen is collected. XXX In the future,
duke@435 179 // we'll probably need to have something in this interface
duke@435 180 // to say do this only if we are sure we will not bail
duke@435 181 // out to a full collection in this attempt, but that's
duke@435 182 // for the future.
duke@435 183 assert(SafepointSynchronize::is_at_safepoint(),
duke@435 184 "We can only be executing this arm of if at a safepoint");
duke@435 185 GCCauseSetter gccs(gch, _gc_cause);
duke@435 186 gch->do_full_collection(gch->must_clear_all_soft_refs(),
duke@435 187 0 /* collect only youngest gen */);
duke@435 188 } // Else no need for a foreground young gc
duke@435 189 assert((_gc_count_before < gch->total_collections()) ||
duke@435 190 (GC_locker::is_active() /* gc may have been skipped */
duke@435 191 && (_gc_count_before == gch->total_collections())),
duke@435 192 "total_collections() should be monotonically increasing");
duke@435 193
duke@435 194 MutexLockerEx x(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
duke@435 195 if (gch->total_full_collections() == _full_gc_count_before) {
duke@435 196 // Disable iCMS until the full collection is done.
duke@435 197 CMSCollector::disable_icms();
duke@435 198 // In case CMS thread was in icms_wait(), wake it up.
duke@435 199 CMSCollector::start_icms();
ysr@1875 200 // Nudge the CMS thread to start a concurrent collection.
duke@435 201 CMSCollector::request_full_gc(_full_gc_count_before);
duke@435 202 } else {
duke@435 203 FullGCCount_lock->notify_all(); // Inform the Java thread its work is done
duke@435 204 }
duke@435 205 }
duke@435 206
duke@435 207 bool VM_GenCollectFullConcurrent::evaluate_at_safepoint() const {
duke@435 208 Thread* thr = Thread::current();
duke@435 209 assert(thr != NULL, "Unexpected tid");
duke@435 210 if (!thr->is_Java_thread()) {
duke@435 211 assert(thr->is_VM_thread(), "Expected to be evaluated by VM thread");
duke@435 212 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 213 if (_gc_count_before != gch->total_collections()) {
duke@435 214 // No need to do a young gc, we'll just nudge the CMS thread
duke@435 215 // in the doit() method above, to be executed soon.
duke@435 216 assert(_gc_count_before < gch->total_collections(),
duke@435 217 "total_collections() should be monotnically increasing");
duke@435 218 return false; // no need for foreground young gc
duke@435 219 }
duke@435 220 }
duke@435 221 return true; // may still need foreground young gc
duke@435 222 }
duke@435 223
duke@435 224
duke@435 225 void VM_GenCollectFullConcurrent::doit_epilogue() {
duke@435 226 Thread* thr = Thread::current();
duke@435 227 assert(thr->is_Java_thread(), "just checking");
duke@435 228 JavaThread* jt = (JavaThread*)thr;
duke@435 229 // Release the Heap_lock first.
duke@435 230 Heap_lock->unlock();
duke@435 231 release_and_notify_pending_list_lock();
duke@435 232
duke@435 233 // It is fine to test whether completed collections has
duke@435 234 // exceeded our request count without locking because
duke@435 235 // the completion count is monotonically increasing;
duke@435 236 // this will break for very long-running apps when the
duke@435 237 // count overflows and wraps around. XXX fix me !!!
duke@435 238 // e.g. at the rate of 1 full gc per ms, this could
duke@435 239 // overflow in about 1000 years.
duke@435 240 GenCollectedHeap* gch = GenCollectedHeap::heap();
ysr@1875 241 if (_gc_cause != GCCause::_gc_locker &&
ysr@1875 242 gch->total_full_collections_completed() <= _full_gc_count_before) {
tonyp@2011 243 // maybe we should change the condition to test _gc_cause ==
tonyp@2011 244 // GCCause::_java_lang_system_gc, instead of
tonyp@2011 245 // _gc_cause != GCCause::_gc_locker
tonyp@2011 246 assert(_gc_cause == GCCause::_java_lang_system_gc,
tonyp@2011 247 "the only way to get here if this was a System.gc()-induced GC");
ysr@1875 248 assert(ExplicitGCInvokesConcurrent, "Error");
duke@435 249 // Now, wait for witnessing concurrent gc cycle to complete,
duke@435 250 // but do so in native mode, because we want to lock the
duke@435 251 // FullGCEvent_lock, which may be needed by the VM thread
duke@435 252 // or by the CMS thread, so we do not want to be suspended
duke@435 253 // while holding that lock.
duke@435 254 ThreadToNativeFromVM native(jt);
duke@435 255 MutexLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
duke@435 256 // Either a concurrent or a stop-world full gc is sufficient
duke@435 257 // witness to our request.
duke@435 258 while (gch->total_full_collections_completed() <= _full_gc_count_before) {
duke@435 259 FullGCCount_lock->wait(Mutex::_no_safepoint_check_flag);
duke@435 260 }
duke@435 261 }
duke@435 262 // Enable iCMS back.
duke@435 263 CMSCollector::enable_icms();
duke@435 264 }

mercurial