Thu, 12 Oct 2017 21:27:07 +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. |
kbarrett@7360 | 53 | SurrogateLockerThread* slt = ConcurrentMarkSweepThread::slt(); |
kbarrett@7360 | 54 | if (slt != NULL) { |
kbarrett@7360 | 55 | slt->manipulatePLL(SurrogateLockerThread::acquirePLL); |
kbarrett@7360 | 56 | } else { |
kbarrett@7360 | 57 | SurrogateLockerThread::report_missing_slt(); |
kbarrett@7360 | 58 | } |
aoqi@0 | 59 | } |
aoqi@0 | 60 | |
aoqi@0 | 61 | void VM_CMS_Operation::release_and_notify_pending_list_lock() { |
aoqi@0 | 62 | // The caller may block while communicating |
aoqi@0 | 63 | // with the SLT thread in order to acquire/release the PLL. |
aoqi@0 | 64 | ConcurrentMarkSweepThread::slt()-> |
aoqi@0 | 65 | manipulatePLL(SurrogateLockerThread::releaseAndNotifyPLL); |
aoqi@0 | 66 | } |
aoqi@0 | 67 | |
aoqi@0 | 68 | void VM_CMS_Operation::verify_before_gc() { |
aoqi@0 | 69 | if (VerifyBeforeGC && |
aoqi@0 | 70 | GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) { |
brutisso@6904 | 71 | GCTraceTime tm("Verify Before", false, false, _collector->_gc_timer_cm, _collector->_gc_tracer_cm->gc_id()); |
aoqi@0 | 72 | HandleMark hm; |
aoqi@0 | 73 | FreelistLocker x(_collector); |
aoqi@0 | 74 | MutexLockerEx y(_collector->bitMapLock(), Mutex::_no_safepoint_check_flag); |
aoqi@0 | 75 | Universe::heap()->prepare_for_verify(); |
aoqi@0 | 76 | Universe::verify(); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | void VM_CMS_Operation::verify_after_gc() { |
aoqi@0 | 81 | if (VerifyAfterGC && |
aoqi@0 | 82 | GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) { |
brutisso@6904 | 83 | GCTraceTime tm("Verify After", false, false, _collector->_gc_timer_cm, _collector->_gc_tracer_cm->gc_id()); |
aoqi@0 | 84 | HandleMark hm; |
aoqi@0 | 85 | FreelistLocker x(_collector); |
aoqi@0 | 86 | MutexLockerEx y(_collector->bitMapLock(), Mutex::_no_safepoint_check_flag); |
aoqi@0 | 87 | Universe::verify(); |
aoqi@0 | 88 | } |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | bool VM_CMS_Operation::lost_race() const { |
aoqi@0 | 92 | if (CMSCollector::abstract_state() == CMSCollector::Idling) { |
aoqi@0 | 93 | // We lost a race to a foreground collection |
aoqi@0 | 94 | // -- there's nothing to do |
aoqi@0 | 95 | return true; |
aoqi@0 | 96 | } |
aoqi@0 | 97 | assert(CMSCollector::abstract_state() == legal_state(), |
aoqi@0 | 98 | "Inconsistent collector state?"); |
aoqi@0 | 99 | return false; |
aoqi@0 | 100 | } |
aoqi@0 | 101 | |
aoqi@0 | 102 | bool VM_CMS_Operation::doit_prologue() { |
aoqi@0 | 103 | assert(Thread::current()->is_ConcurrentGC_thread(), "just checking"); |
aoqi@0 | 104 | assert(!CMSCollector::foregroundGCShouldWait(), "Possible deadlock"); |
aoqi@0 | 105 | assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(), |
aoqi@0 | 106 | "Possible deadlock"); |
aoqi@0 | 107 | |
aoqi@0 | 108 | if (needs_pll()) { |
aoqi@0 | 109 | acquire_pending_list_lock(); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | // Get the Heap_lock after the pending_list_lock. |
aoqi@0 | 112 | Heap_lock->lock(); |
aoqi@0 | 113 | if (lost_race()) { |
aoqi@0 | 114 | assert(_prologue_succeeded == false, "Initialized in c'tor"); |
aoqi@0 | 115 | Heap_lock->unlock(); |
aoqi@0 | 116 | if (needs_pll()) { |
aoqi@0 | 117 | release_and_notify_pending_list_lock(); |
aoqi@0 | 118 | } |
aoqi@0 | 119 | } else { |
aoqi@0 | 120 | _prologue_succeeded = true; |
aoqi@0 | 121 | } |
aoqi@0 | 122 | return _prologue_succeeded; |
aoqi@0 | 123 | } |
aoqi@0 | 124 | |
aoqi@0 | 125 | void VM_CMS_Operation::doit_epilogue() { |
aoqi@0 | 126 | assert(Thread::current()->is_ConcurrentGC_thread(), "just checking"); |
aoqi@0 | 127 | assert(!CMSCollector::foregroundGCShouldWait(), "Possible deadlock"); |
aoqi@0 | 128 | assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(), |
aoqi@0 | 129 | "Possible deadlock"); |
aoqi@0 | 130 | |
aoqi@0 | 131 | // Release the Heap_lock first. |
aoqi@0 | 132 | Heap_lock->unlock(); |
aoqi@0 | 133 | if (needs_pll()) { |
aoqi@0 | 134 | release_and_notify_pending_list_lock(); |
aoqi@0 | 135 | } |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | ////////////////////////////////////////////////////////// |
aoqi@0 | 139 | // Methods in class VM_CMS_Initial_Mark |
aoqi@0 | 140 | ////////////////////////////////////////////////////////// |
aoqi@0 | 141 | void VM_CMS_Initial_Mark::doit() { |
aoqi@0 | 142 | if (lost_race()) { |
aoqi@0 | 143 | // Nothing to do. |
aoqi@0 | 144 | return; |
aoqi@0 | 145 | } |
aoqi@0 | 146 | #ifndef USDT2 |
aoqi@0 | 147 | HS_DTRACE_PROBE(hs_private, cms__initmark__begin); |
aoqi@0 | 148 | #else /* USDT2 */ |
aoqi@0 | 149 | HS_PRIVATE_CMS_INITMARK_BEGIN( |
aoqi@0 | 150 | ); |
aoqi@0 | 151 | #endif /* USDT2 */ |
aoqi@0 | 152 | |
aoqi@0 | 153 | _collector->_gc_timer_cm->register_gc_pause_start("Initial Mark"); |
aoqi@0 | 154 | |
aoqi@0 | 155 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 156 | GCCauseSetter gccs(gch, GCCause::_cms_initial_mark); |
aoqi@0 | 157 | |
aoqi@0 | 158 | VM_CMS_Operation::verify_before_gc(); |
aoqi@0 | 159 | |
aoqi@0 | 160 | IsGCActiveMark x; // stop-world GC active |
aoqi@0 | 161 | _collector->do_CMS_operation(CMSCollector::CMS_op_checkpointRootsInitial, gch->gc_cause()); |
aoqi@0 | 162 | |
aoqi@0 | 163 | VM_CMS_Operation::verify_after_gc(); |
aoqi@0 | 164 | |
aoqi@0 | 165 | _collector->_gc_timer_cm->register_gc_pause_end(); |
aoqi@0 | 166 | |
aoqi@0 | 167 | #ifndef USDT2 |
aoqi@0 | 168 | HS_DTRACE_PROBE(hs_private, cms__initmark__end); |
aoqi@0 | 169 | #else /* USDT2 */ |
aoqi@0 | 170 | HS_PRIVATE_CMS_INITMARK_END( |
aoqi@0 | 171 | ); |
aoqi@0 | 172 | #endif /* USDT2 */ |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | ////////////////////////////////////////////////////////// |
aoqi@0 | 176 | // Methods in class VM_CMS_Final_Remark_Operation |
aoqi@0 | 177 | ////////////////////////////////////////////////////////// |
aoqi@0 | 178 | void VM_CMS_Final_Remark::doit() { |
aoqi@0 | 179 | if (lost_race()) { |
aoqi@0 | 180 | // Nothing to do. |
aoqi@0 | 181 | return; |
aoqi@0 | 182 | } |
aoqi@0 | 183 | #ifndef USDT2 |
aoqi@0 | 184 | HS_DTRACE_PROBE(hs_private, cms__remark__begin); |
aoqi@0 | 185 | #else /* USDT2 */ |
aoqi@0 | 186 | HS_PRIVATE_CMS_REMARK_BEGIN( |
aoqi@0 | 187 | ); |
aoqi@0 | 188 | #endif /* USDT2 */ |
aoqi@0 | 189 | |
aoqi@0 | 190 | _collector->_gc_timer_cm->register_gc_pause_start("Final Mark"); |
aoqi@0 | 191 | |
aoqi@0 | 192 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 193 | GCCauseSetter gccs(gch, GCCause::_cms_final_remark); |
aoqi@0 | 194 | |
aoqi@0 | 195 | VM_CMS_Operation::verify_before_gc(); |
aoqi@0 | 196 | |
aoqi@0 | 197 | IsGCActiveMark x; // stop-world GC active |
aoqi@0 | 198 | _collector->do_CMS_operation(CMSCollector::CMS_op_checkpointRootsFinal, gch->gc_cause()); |
aoqi@0 | 199 | |
aoqi@0 | 200 | VM_CMS_Operation::verify_after_gc(); |
aoqi@0 | 201 | |
aoqi@0 | 202 | _collector->save_heap_summary(); |
aoqi@0 | 203 | _collector->_gc_timer_cm->register_gc_pause_end(); |
aoqi@0 | 204 | |
aoqi@0 | 205 | #ifndef USDT2 |
aoqi@0 | 206 | HS_DTRACE_PROBE(hs_private, cms__remark__end); |
aoqi@0 | 207 | #else /* USDT2 */ |
aoqi@0 | 208 | HS_PRIVATE_CMS_REMARK_END( |
aoqi@0 | 209 | ); |
aoqi@0 | 210 | #endif /* USDT2 */ |
aoqi@0 | 211 | } |
aoqi@0 | 212 | |
aoqi@0 | 213 | // VM operation to invoke a concurrent collection of a |
aoqi@0 | 214 | // GenCollectedHeap heap. |
aoqi@0 | 215 | void VM_GenCollectFullConcurrent::doit() { |
aoqi@0 | 216 | assert(Thread::current()->is_VM_thread(), "Should be VM thread"); |
aoqi@0 | 217 | assert(GCLockerInvokesConcurrent || ExplicitGCInvokesConcurrent, "Unexpected"); |
aoqi@0 | 218 | |
aoqi@0 | 219 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 220 | if (_gc_count_before == gch->total_collections()) { |
aoqi@0 | 221 | // The "full" of do_full_collection call below "forces" |
aoqi@0 | 222 | // a collection; the second arg, 0, below ensures that |
aoqi@0 | 223 | // only the young gen is collected. XXX In the future, |
aoqi@0 | 224 | // we'll probably need to have something in this interface |
aoqi@0 | 225 | // to say do this only if we are sure we will not bail |
aoqi@0 | 226 | // out to a full collection in this attempt, but that's |
aoqi@0 | 227 | // for the future. |
aoqi@0 | 228 | assert(SafepointSynchronize::is_at_safepoint(), |
aoqi@0 | 229 | "We can only be executing this arm of if at a safepoint"); |
aoqi@0 | 230 | GCCauseSetter gccs(gch, _gc_cause); |
aoqi@0 | 231 | gch->do_full_collection(gch->must_clear_all_soft_refs(), |
aoqi@0 | 232 | 0 /* collect only youngest gen */); |
aoqi@0 | 233 | } // Else no need for a foreground young gc |
aoqi@0 | 234 | assert((_gc_count_before < gch->total_collections()) || |
aoqi@0 | 235 | (GC_locker::is_active() /* gc may have been skipped */ |
aoqi@0 | 236 | && (_gc_count_before == gch->total_collections())), |
aoqi@0 | 237 | "total_collections() should be monotonically increasing"); |
aoqi@0 | 238 | |
aoqi@0 | 239 | MutexLockerEx x(FullGCCount_lock, Mutex::_no_safepoint_check_flag); |
aoqi@0 | 240 | assert(_full_gc_count_before <= gch->total_full_collections(), "Error"); |
aoqi@0 | 241 | if (gch->total_full_collections() == _full_gc_count_before) { |
aoqi@0 | 242 | // Disable iCMS until the full collection is done, and |
aoqi@0 | 243 | // remember that we did so. |
aoqi@0 | 244 | CMSCollector::disable_icms(); |
aoqi@0 | 245 | _disabled_icms = true; |
aoqi@0 | 246 | // In case CMS thread was in icms_wait(), wake it up. |
aoqi@0 | 247 | CMSCollector::start_icms(); |
aoqi@0 | 248 | // Nudge the CMS thread to start a concurrent collection. |
aoqi@0 | 249 | CMSCollector::request_full_gc(_full_gc_count_before, _gc_cause); |
aoqi@0 | 250 | } else { |
aoqi@0 | 251 | assert(_full_gc_count_before < gch->total_full_collections(), "Error"); |
aoqi@0 | 252 | FullGCCount_lock->notify_all(); // Inform the Java thread its work is done |
aoqi@0 | 253 | } |
aoqi@0 | 254 | } |
aoqi@0 | 255 | |
aoqi@0 | 256 | bool VM_GenCollectFullConcurrent::evaluate_at_safepoint() const { |
aoqi@0 | 257 | Thread* thr = Thread::current(); |
aoqi@0 | 258 | assert(thr != NULL, "Unexpected tid"); |
aoqi@0 | 259 | if (!thr->is_Java_thread()) { |
aoqi@0 | 260 | assert(thr->is_VM_thread(), "Expected to be evaluated by VM thread"); |
aoqi@0 | 261 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 262 | if (_gc_count_before != gch->total_collections()) { |
aoqi@0 | 263 | // No need to do a young gc, we'll just nudge the CMS thread |
aoqi@0 | 264 | // in the doit() method above, to be executed soon. |
aoqi@0 | 265 | assert(_gc_count_before < gch->total_collections(), |
aoqi@0 | 266 | "total_collections() should be monotnically increasing"); |
aoqi@0 | 267 | return false; // no need for foreground young gc |
aoqi@0 | 268 | } |
aoqi@0 | 269 | } |
aoqi@0 | 270 | return true; // may still need foreground young gc |
aoqi@0 | 271 | } |
aoqi@0 | 272 | |
aoqi@0 | 273 | |
aoqi@0 | 274 | void VM_GenCollectFullConcurrent::doit_epilogue() { |
aoqi@0 | 275 | Thread* thr = Thread::current(); |
aoqi@0 | 276 | assert(thr->is_Java_thread(), "just checking"); |
aoqi@0 | 277 | JavaThread* jt = (JavaThread*)thr; |
aoqi@0 | 278 | // Release the Heap_lock first. |
aoqi@0 | 279 | Heap_lock->unlock(); |
aoqi@0 | 280 | release_and_notify_pending_list_lock(); |
aoqi@0 | 281 | |
aoqi@0 | 282 | // It is fine to test whether completed collections has |
aoqi@0 | 283 | // exceeded our request count without locking because |
aoqi@0 | 284 | // the completion count is monotonically increasing; |
aoqi@0 | 285 | // this will break for very long-running apps when the |
aoqi@0 | 286 | // count overflows and wraps around. XXX fix me !!! |
aoqi@0 | 287 | // e.g. at the rate of 1 full gc per ms, this could |
aoqi@0 | 288 | // overflow in about 1000 years. |
aoqi@0 | 289 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
aoqi@0 | 290 | if (_gc_cause != GCCause::_gc_locker && |
aoqi@0 | 291 | gch->total_full_collections_completed() <= _full_gc_count_before) { |
aoqi@0 | 292 | // maybe we should change the condition to test _gc_cause == |
aoqi@0 | 293 | // GCCause::_java_lang_system_gc, instead of |
aoqi@0 | 294 | // _gc_cause != GCCause::_gc_locker |
aoqi@0 | 295 | assert(_gc_cause == GCCause::_java_lang_system_gc, |
aoqi@0 | 296 | "the only way to get here if this was a System.gc()-induced GC"); |
aoqi@0 | 297 | assert(ExplicitGCInvokesConcurrent, "Error"); |
aoqi@0 | 298 | // Now, wait for witnessing concurrent gc cycle to complete, |
aoqi@0 | 299 | // but do so in native mode, because we want to lock the |
aoqi@0 | 300 | // FullGCEvent_lock, which may be needed by the VM thread |
aoqi@0 | 301 | // or by the CMS thread, so we do not want to be suspended |
aoqi@0 | 302 | // while holding that lock. |
aoqi@0 | 303 | ThreadToNativeFromVM native(jt); |
aoqi@0 | 304 | MutexLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag); |
aoqi@0 | 305 | // Either a concurrent or a stop-world full gc is sufficient |
aoqi@0 | 306 | // witness to our request. |
aoqi@0 | 307 | while (gch->total_full_collections_completed() <= _full_gc_count_before) { |
aoqi@0 | 308 | FullGCCount_lock->wait(Mutex::_no_safepoint_check_flag); |
aoqi@0 | 309 | } |
aoqi@0 | 310 | } |
aoqi@0 | 311 | // Enable iCMS back if we disabled it earlier. |
aoqi@0 | 312 | if (_disabled_icms) { |
aoqi@0 | 313 | CMSCollector::enable_icms(); |
aoqi@0 | 314 | } |
aoqi@0 | 315 | } |