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

Tue, 15 May 2012 10:25:06 +0200

author
brutisso
date
Tue, 15 May 2012 10:25:06 +0200
changeset 3767
9d679effd28c
parent 3202
436b4a3231bf
child 4153
b9a9ed0f8eeb
child 4176
4202510ee0fe
permissions
-rw-r--r--

7166894: Add gc cause to GC logging for all collectors
Reviewed-by: mgerdin, johnc

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

mercurial