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   JvmtiGCFullMarker jgcm;
 174   VM_GC_Operation::notify_gc_begin(true);
 175 
 176   GenCollectedHeap* gch = GenCollectedHeap::heap();
 177   if (_gc_count_before == gch->total_collections()) {
 178     // The "full" of do_full_collection call below "forces"
 179     // a collection; the second arg, 0, below ensures that
 180     // only the young gen is collected. XXX In the future,
 181     // we'll probably need to have something in this interface
 182     // to say do this only if we are sure we will not bail
 183     // out to a full collection in this attempt, but that's
 184     // for the future.
 185     assert(SafepointSynchronize::is_at_safepoint(),
 186       "We can only be executing this arm of if at a safepoint");
 187     GCCauseSetter gccs(gch, _gc_cause);
 188     gch->do_full_collection(gch->must_clear_all_soft_refs(),
 189                             0 /* collect only youngest gen */);
 190   } // Else no need for a foreground young gc
 191   assert((_gc_count_before < gch->total_collections()) ||
 192          (GC_locker::is_active() /* gc may have been skipped */
 193           && (_gc_count_before == gch->total_collections())),
 194          "total_collections() should be monotonically increasing");
 195 
 196   {
 197     MutexLockerEx x(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
 198     if (gch->total_full_collections() == _full_gc_count_before) {
 199       // Disable iCMS until the full collection is done.
 200       CMSCollector::disable_icms();
 201       // In case CMS thread was in icms_wait(), wake it up.
 202       CMSCollector::start_icms();
 203       // Nudge the CMS thread to start a concurrent collection.
 204       CMSCollector::request_full_gc(_full_gc_count_before);
 205     } else {
 206       FullGCCount_lock->notify_all(); // Inform the Java thread its work is done
 207     }
 208   }
 209   VM_GC_Operation::notify_gc_end();
 210 }
 211 
 212 bool VM_GenCollectFullConcurrent::evaluate_at_safepoint() const {
 213   Thread* thr = Thread::current();
 214   assert(thr != NULL, "Unexpected tid");
 215   if (!thr->is_Java_thread()) {
 216     assert(thr->is_VM_thread(), "Expected to be evaluated by VM thread");
 217     GenCollectedHeap* gch = GenCollectedHeap::heap();
 218     if (_gc_count_before != gch->total_collections()) {
 219       // No need to do a young gc, we'll just nudge the CMS thread
 220       // in the doit() method above, to be executed soon.
 221       assert(_gc_count_before < gch->total_collections(),
 222              "total_collections() should be monotnically increasing");
 223       return false;  // no need for foreground young gc
 224     }
 225   }
 226   return true;       // may still need foreground young gc
 227 }
 228 
 229 
 230 void VM_GenCollectFullConcurrent::doit_epilogue() {
 231   Thread* thr = Thread::current();
 232   assert(thr->is_Java_thread(), "just checking");
 233   JavaThread* jt = (JavaThread*)thr;
 234   // Release the Heap_lock first.
 235   Heap_lock->unlock();
 236   release_and_notify_pending_list_lock();
 237 
 238   // It is fine to test whether completed collections has
 239   // exceeded our request count without locking because
 240   // the completion count is monotonically increasing;
 241   // this will break for very long-running apps when the
 242   // count overflows and wraps around. XXX fix me !!!
 243   // e.g. at the rate of 1 full gc per ms, this could
 244   // overflow in about 1000 years.
 245   GenCollectedHeap* gch = GenCollectedHeap::heap();
 246   if (_gc_cause != GCCause::_gc_locker &&
 247       gch->total_full_collections_completed() <= _full_gc_count_before) {
 248     // maybe we should change the condition to test _gc_cause ==
 249     // GCCause::_java_lang_system_gc, instead of
 250     // _gc_cause != GCCause::_gc_locker
 251     assert(_gc_cause == GCCause::_java_lang_system_gc,
 252            "the only way to get here if this was a System.gc()-induced GC");
 253     assert(ExplicitGCInvokesConcurrent, "Error");
 254     // Now, wait for witnessing concurrent gc cycle to complete,
 255     // but do so in native mode, because we want to lock the
 256     // FullGCEvent_lock, which may be needed by the VM thread
 257     // or by the CMS thread, so we do not want to be suspended
 258     // while holding that lock.
 259     ThreadToNativeFromVM native(jt);
 260     MutexLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
 261     // Either a concurrent or a stop-world full gc is sufficient
 262     // witness to our request.
 263     while (gch->total_full_collections_completed() <= _full_gc_count_before) {
 264       FullGCCount_lock->wait(Mutex::_no_safepoint_check_flag);
 265     }
 266   }
 267   // Enable iCMS back.
 268   CMSCollector::enable_icms();
 269 }