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