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