1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/concurrentMarkSweep/vmCMSOperations.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,250 @@ 1.4 +/* 1.5 + * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 +# include "incls/_precompiled.incl" 1.28 +# include "incls/_vmCMSOperations.cpp.incl" 1.29 + 1.30 +HS_DTRACE_PROBE_DECL(hs_private, cms__initmark__begin); 1.31 +HS_DTRACE_PROBE_DECL(hs_private, cms__initmark__end); 1.32 + 1.33 +HS_DTRACE_PROBE_DECL(hs_private, cms__remark__begin); 1.34 +HS_DTRACE_PROBE_DECL(hs_private, cms__remark__end); 1.35 + 1.36 +////////////////////////////////////////////////////////// 1.37 +// Methods in abstract class VM_CMS_Operation 1.38 +////////////////////////////////////////////////////////// 1.39 +void VM_CMS_Operation::acquire_pending_list_lock() { 1.40 + // The caller may block while communicating 1.41 + // with the SLT thread in order to acquire/release the PLL. 1.42 + ConcurrentMarkSweepThread::slt()-> 1.43 + manipulatePLL(SurrogateLockerThread::acquirePLL); 1.44 +} 1.45 + 1.46 +void VM_CMS_Operation::release_and_notify_pending_list_lock() { 1.47 + // The caller may block while communicating 1.48 + // with the SLT thread in order to acquire/release the PLL. 1.49 + ConcurrentMarkSweepThread::slt()-> 1.50 + manipulatePLL(SurrogateLockerThread::releaseAndNotifyPLL); 1.51 +} 1.52 + 1.53 +void VM_CMS_Operation::verify_before_gc() { 1.54 + if (VerifyBeforeGC && 1.55 + GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) { 1.56 + HandleMark hm; 1.57 + FreelistLocker x(_collector); 1.58 + MutexLockerEx y(_collector->bitMapLock(), Mutex::_no_safepoint_check_flag); 1.59 + Universe::heap()->prepare_for_verify(); 1.60 + Universe::verify(true); 1.61 + } 1.62 +} 1.63 + 1.64 +void VM_CMS_Operation::verify_after_gc() { 1.65 + if (VerifyAfterGC && 1.66 + GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) { 1.67 + HandleMark hm; 1.68 + FreelistLocker x(_collector); 1.69 + MutexLockerEx y(_collector->bitMapLock(), Mutex::_no_safepoint_check_flag); 1.70 + Universe::verify(true); 1.71 + } 1.72 +} 1.73 + 1.74 +bool VM_CMS_Operation::lost_race() const { 1.75 + if (CMSCollector::abstract_state() == CMSCollector::Idling) { 1.76 + // We lost a race to a foreground collection 1.77 + // -- there's nothing to do 1.78 + return true; 1.79 + } 1.80 + assert(CMSCollector::abstract_state() == legal_state(), 1.81 + "Inconsistent collector state?"); 1.82 + return false; 1.83 +} 1.84 + 1.85 +bool VM_CMS_Operation::doit_prologue() { 1.86 + assert(Thread::current()->is_ConcurrentGC_thread(), "just checking"); 1.87 + assert(!CMSCollector::foregroundGCShouldWait(), "Possible deadlock"); 1.88 + assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(), 1.89 + "Possible deadlock"); 1.90 + 1.91 + if (needs_pll()) { 1.92 + acquire_pending_list_lock(); 1.93 + } 1.94 + // Get the Heap_lock after the pending_list_lock. 1.95 + Heap_lock->lock(); 1.96 + if (lost_race()) { 1.97 + assert(_prologue_succeeded == false, "Initialized in c'tor"); 1.98 + Heap_lock->unlock(); 1.99 + if (needs_pll()) { 1.100 + release_and_notify_pending_list_lock(); 1.101 + } 1.102 + } else { 1.103 + _prologue_succeeded = true; 1.104 + } 1.105 + return _prologue_succeeded; 1.106 +} 1.107 + 1.108 +void VM_CMS_Operation::doit_epilogue() { 1.109 + assert(Thread::current()->is_ConcurrentGC_thread(), "just checking"); 1.110 + assert(!CMSCollector::foregroundGCShouldWait(), "Possible deadlock"); 1.111 + assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(), 1.112 + "Possible deadlock"); 1.113 + 1.114 + // Release the Heap_lock first. 1.115 + Heap_lock->unlock(); 1.116 + if (needs_pll()) { 1.117 + release_and_notify_pending_list_lock(); 1.118 + } 1.119 +} 1.120 + 1.121 +////////////////////////////////////////////////////////// 1.122 +// Methods in class VM_CMS_Initial_Mark 1.123 +////////////////////////////////////////////////////////// 1.124 +void VM_CMS_Initial_Mark::doit() { 1.125 + if (lost_race()) { 1.126 + // Nothing to do. 1.127 + return; 1.128 + } 1.129 + HS_DTRACE_PROBE(hs_private, cms__initmark__begin); 1.130 + 1.131 + GenCollectedHeap* gch = GenCollectedHeap::heap(); 1.132 + GCCauseSetter gccs(gch, GCCause::_cms_initial_mark); 1.133 + 1.134 + VM_CMS_Operation::verify_before_gc(); 1.135 + 1.136 + IsGCActiveMark x; // stop-world GC active 1.137 + _collector->do_CMS_operation(CMSCollector::CMS_op_checkpointRootsInitial); 1.138 + 1.139 + VM_CMS_Operation::verify_after_gc(); 1.140 + HS_DTRACE_PROBE(hs_private, cms__initmark__end); 1.141 +} 1.142 + 1.143 +////////////////////////////////////////////////////////// 1.144 +// Methods in class VM_CMS_Final_Remark_Operation 1.145 +////////////////////////////////////////////////////////// 1.146 +void VM_CMS_Final_Remark::doit() { 1.147 + if (lost_race()) { 1.148 + // Nothing to do. 1.149 + return; 1.150 + } 1.151 + HS_DTRACE_PROBE(hs_private, cms__remark__begin); 1.152 + 1.153 + GenCollectedHeap* gch = GenCollectedHeap::heap(); 1.154 + GCCauseSetter gccs(gch, GCCause::_cms_final_remark); 1.155 + 1.156 + VM_CMS_Operation::verify_before_gc(); 1.157 + 1.158 + IsGCActiveMark x; // stop-world GC active 1.159 + _collector->do_CMS_operation(CMSCollector::CMS_op_checkpointRootsFinal); 1.160 + 1.161 + VM_CMS_Operation::verify_after_gc(); 1.162 + HS_DTRACE_PROBE(hs_private, cms__remark__end); 1.163 +} 1.164 + 1.165 +// VM operation to invoke a concurrent collection of a 1.166 +// GenCollectedHeap heap. 1.167 +void VM_GenCollectFullConcurrent::doit() { 1.168 + assert(Thread::current()->is_VM_thread(), "Should be VM thread"); 1.169 + 1.170 + GenCollectedHeap* gch = GenCollectedHeap::heap(); 1.171 + if (_gc_count_before == gch->total_collections()) { 1.172 + // The "full" of do_full_collection call below "forces" 1.173 + // a collection; the second arg, 0, below ensures that 1.174 + // only the young gen is collected. XXX In the future, 1.175 + // we'll probably need to have something in this interface 1.176 + // to say do this only if we are sure we will not bail 1.177 + // out to a full collection in this attempt, but that's 1.178 + // for the future. 1.179 + assert(SafepointSynchronize::is_at_safepoint(), 1.180 + "We can only be executing this arm of if at a safepoint"); 1.181 + GCCauseSetter gccs(gch, _gc_cause); 1.182 + gch->do_full_collection(gch->must_clear_all_soft_refs(), 1.183 + 0 /* collect only youngest gen */); 1.184 + } // Else no need for a foreground young gc 1.185 + assert((_gc_count_before < gch->total_collections()) || 1.186 + (GC_locker::is_active() /* gc may have been skipped */ 1.187 + && (_gc_count_before == gch->total_collections())), 1.188 + "total_collections() should be monotonically increasing"); 1.189 + 1.190 + MutexLockerEx x(FullGCCount_lock, Mutex::_no_safepoint_check_flag); 1.191 + if (gch->total_full_collections() == _full_gc_count_before) { 1.192 + // Disable iCMS until the full collection is done. 1.193 + CMSCollector::disable_icms(); 1.194 + // In case CMS thread was in icms_wait(), wake it up. 1.195 + CMSCollector::start_icms(); 1.196 + // Nudge the CMS thread to start a concurrent collection 1.197 + CMSCollector::request_full_gc(_full_gc_count_before); 1.198 + } else { 1.199 + FullGCCount_lock->notify_all(); // Inform the Java thread its work is done 1.200 + } 1.201 +} 1.202 + 1.203 +bool VM_GenCollectFullConcurrent::evaluate_at_safepoint() const { 1.204 + Thread* thr = Thread::current(); 1.205 + assert(thr != NULL, "Unexpected tid"); 1.206 + if (!thr->is_Java_thread()) { 1.207 + assert(thr->is_VM_thread(), "Expected to be evaluated by VM thread"); 1.208 + GenCollectedHeap* gch = GenCollectedHeap::heap(); 1.209 + if (_gc_count_before != gch->total_collections()) { 1.210 + // No need to do a young gc, we'll just nudge the CMS thread 1.211 + // in the doit() method above, to be executed soon. 1.212 + assert(_gc_count_before < gch->total_collections(), 1.213 + "total_collections() should be monotnically increasing"); 1.214 + return false; // no need for foreground young gc 1.215 + } 1.216 + } 1.217 + return true; // may still need foreground young gc 1.218 +} 1.219 + 1.220 + 1.221 +void VM_GenCollectFullConcurrent::doit_epilogue() { 1.222 + Thread* thr = Thread::current(); 1.223 + assert(thr->is_Java_thread(), "just checking"); 1.224 + JavaThread* jt = (JavaThread*)thr; 1.225 + // Release the Heap_lock first. 1.226 + Heap_lock->unlock(); 1.227 + release_and_notify_pending_list_lock(); 1.228 + 1.229 + // It is fine to test whether completed collections has 1.230 + // exceeded our request count without locking because 1.231 + // the completion count is monotonically increasing; 1.232 + // this will break for very long-running apps when the 1.233 + // count overflows and wraps around. XXX fix me !!! 1.234 + // e.g. at the rate of 1 full gc per ms, this could 1.235 + // overflow in about 1000 years. 1.236 + GenCollectedHeap* gch = GenCollectedHeap::heap(); 1.237 + if (gch->total_full_collections_completed() <= _full_gc_count_before) { 1.238 + // Now, wait for witnessing concurrent gc cycle to complete, 1.239 + // but do so in native mode, because we want to lock the 1.240 + // FullGCEvent_lock, which may be needed by the VM thread 1.241 + // or by the CMS thread, so we do not want to be suspended 1.242 + // while holding that lock. 1.243 + ThreadToNativeFromVM native(jt); 1.244 + MutexLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag); 1.245 + // Either a concurrent or a stop-world full gc is sufficient 1.246 + // witness to our request. 1.247 + while (gch->total_full_collections_completed() <= _full_gc_count_before) { 1.248 + FullGCCount_lock->wait(Mutex::_no_safepoint_check_flag); 1.249 + } 1.250 + } 1.251 + // Enable iCMS back. 1.252 + CMSCollector::enable_icms(); 1.253 +}