< prev index next >

src/share/vm/gc/cms/concurrentMarkSweepThread.cpp

Print this page
rev 10297 : [mq]: webrev.01
rev 10298 : [mq]: webrev.02
   1 /*
   2  * Copyright (c) 2001, 2015, 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  *


  25 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "gc/cms/concurrentMarkSweepGeneration.inline.hpp"
  28 #include "gc/cms/concurrentMarkSweepThread.hpp"
  29 #include "gc/shared/gcId.hpp"
  30 #include "gc/shared/genCollectedHeap.hpp"
  31 #include "oops/instanceRefKlass.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "runtime/init.hpp"
  34 #include "runtime/interfaceSupport.hpp"
  35 #include "runtime/java.hpp"
  36 #include "runtime/javaCalls.hpp"
  37 #include "runtime/mutexLocker.hpp"
  38 #include "runtime/os.hpp"
  39 #include "runtime/vmThread.hpp"
  40 
  41 // ======= Concurrent Mark Sweep Thread ========
  42 
  43 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::_cmst = NULL;
  44 CMSCollector* ConcurrentMarkSweepThread::_collector         = NULL;
  45 bool ConcurrentMarkSweepThread::_should_terminate           = false;
  46 int  ConcurrentMarkSweepThread::_CMS_flag                   = CMS_nil;
  47 
  48 volatile jint ConcurrentMarkSweepThread::_pending_yields    = 0;
  49 
  50 SurrogateLockerThread* ConcurrentMarkSweepThread::_slt      = NULL;
  51 SurrogateLockerThread::SLT_msg_type
  52      ConcurrentMarkSweepThread::_sltBuffer = SurrogateLockerThread::empty;
  53 Monitor* ConcurrentMarkSweepThread::_sltMonitor             = NULL;
  54 
  55 ConcurrentMarkSweepThread::ConcurrentMarkSweepThread(CMSCollector* collector)
  56   : ConcurrentGCThread() {
  57   assert(UseConcMarkSweepGC,  "UseConcMarkSweepGC should be set");
  58   assert(_cmst == NULL, "CMS thread already created");
  59   _cmst = this;
  60   assert(_collector == NULL, "Collector already set");
  61   _collector = collector;
  62 
  63   set_name("CMS Main Thread");
  64 
  65   if (os::create_thread(this, os::cgc_thread)) {
  66     // An old comment here said: "Priority should be just less
  67     // than that of VMThread".  Since the VMThread runs at
  68     // NearMaxPriority, the old comment was inaccurate, but
  69     // changing the default priority to NearMaxPriority-1
  70     // could change current behavior, so the default of
  71     // NearMaxPriority stays in place.
  72     //
  73     // Note that there's a possibility of the VMThread
  74     // starving if UseCriticalCMSThreadPriority is on.
  75     // That won't happen on Solaris for various reasons,
  76     // but may well happen on non-Solaris platforms.
  77     int native_prio;
  78     if (UseCriticalCMSThreadPriority) {
  79       native_prio = os::java_to_os_priority[CriticalPriority];
  80     } else {
  81       native_prio = os::java_to_os_priority[NearMaxPriority];
  82     }
  83     os::set_native_priority(this, native_prio);
  84 
  85     if (!DisableStartThread) {
  86       os::start_thread(this);
  87     }
  88   }
  89   _sltMonitor = SLT_lock;
  90 }
  91 
  92 void ConcurrentMarkSweepThread::run() {
  93   assert(this == cmst(), "just checking");
  94 
  95   initialize_in_thread();
  96   // From this time Thread::current() should be working.
  97   assert(this == Thread::current(), "just checking");
  98   if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) {
  99     warning("Couldn't bind CMS thread to processor " UINTX_FORMAT, CPUForCMSThread);
 100   }
 101   // Wait until Universe::is_fully_initialized()
 102   {
 103     CMSLoopCountWarn loopX("CMS::run", "waiting for "
 104                            "Universe::is_fully_initialized()", 2);
 105     MutexLockerEx x(CGC_lock, true);
 106     set_CMS_flag(CMS_cms_wants_token);
 107     // Wait until Universe is initialized and all initialization is completed.
 108     while (!is_init_completed() && !Universe::is_fully_initialized() &&
 109            !_should_terminate) {
 110       CGC_lock->wait(true, 200);
 111       loopX.tick();
 112     }
 113     // Wait until the surrogate locker thread that will do
 114     // pending list locking on our behalf has been created.
 115     // We cannot start the SLT thread ourselves since we need
 116     // to be a JavaThread to do so.
 117     CMSLoopCountWarn loopY("CMS::run", "waiting for SLT installation", 2);
 118     while (_slt == NULL && !_should_terminate) {
 119       CGC_lock->wait(true, 200);
 120       loopY.tick();
 121     }
 122     clear_CMS_flag(CMS_cms_wants_token);
 123   }
 124 
 125   while (!_should_terminate) {
 126     sleepBeforeNextCycle();
 127     if (_should_terminate) break;
 128     GCIdMark gc_id_mark;
 129     GCCause::Cause cause = _collector->_full_gc_requested ?
 130       _collector->_full_gc_cause : GCCause::_cms_concurrent_mark;
 131     _collector->collect_in_background(cause);
 132   }
 133   assert(_should_terminate, "just checking");
 134   // Check that the state of any protocol for synchronization
 135   // between background (CMS) and foreground collector is "clean"
 136   // (i.e. will not potentially block the foreground collector,
 137   // requiring action by us).
 138   verify_ok_to_terminate();
 139   // Signal that it is terminated
 140   {
 141     MutexLockerEx mu(Terminator_lock,
 142                      Mutex::_no_safepoint_check_flag);
 143     assert(_cmst == this, "Weird!");
 144     _cmst = NULL;
 145     Terminator_lock->notify();
 146   }
 147 }
 148 
 149 #ifndef PRODUCT
 150 void ConcurrentMarkSweepThread::verify_ok_to_terminate() const {
 151   assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() ||
 152            cms_thread_wants_cms_token()),
 153          "Must renounce all worldly possessions and desires for nirvana");
 154   _collector->verify_ok_to_terminate();
 155 }
 156 #endif
 157 
 158 // create and start a new ConcurrentMarkSweep Thread for given CMS generation
 159 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) {
 160   if (!_should_terminate) {
 161     assert(cmst() == NULL, "start() called twice?");
 162     ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector);
 163     assert(cmst() == th, "Where did the just-created CMS thread go?");
 164     return th;
 165   }
 166   return NULL;
 167 }
 168 
 169 void ConcurrentMarkSweepThread::stop() {
 170   // it is ok to take late safepoints here, if needed
 171   {
 172     MutexLockerEx x(Terminator_lock);
 173     _should_terminate = true;
 174   }
 175   { // Now post a notify on CGC_lock so as to nudge
 176     // CMS thread(s) that might be slumbering in
 177     // sleepBeforeNextCycle.
 178     MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
 179     CGC_lock->notify_all();
 180   }
 181   { // Now wait until (all) CMS thread(s) have exited
 182     MutexLockerEx x(Terminator_lock);
 183     while(cmst() != NULL) {
 184       Terminator_lock->wait();
 185     }
 186   }
 187 }
 188 
 189 void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) {
 190   assert(tc != NULL, "Null ThreadClosure");
 191   if (_cmst != NULL) {
 192     tc->do_thread(_cmst);
 193   }
 194   assert(Universe::is_fully_initialized(),
 195          "Called too early, make sure heap is fully initialized");
 196   if (_collector != NULL) {
 197     AbstractWorkGang* gang = _collector->conc_workers();
 198     if (gang != NULL) {
 199       gang->threads_do(tc);
 200     }
 201   }
 202 }
 203 
 204 void ConcurrentMarkSweepThread::print_all_on(outputStream* st) {
 205   if (_cmst != NULL) {
 206     _cmst->print_on(st);
 207     st->cr();
 208   }
 209   if (_collector != NULL) {
 210     AbstractWorkGang* gang = _collector->conc_workers();
 211     if (gang != NULL) {
 212       gang->print_worker_threads_on(st);
 213     }
 214   }
 215 }
 216 
 217 void ConcurrentMarkSweepThread::synchronize(bool is_cms_thread) {
 218   assert(UseConcMarkSweepGC, "just checking");
 219 
 220   MutexLockerEx x(CGC_lock,
 221                   Mutex::_no_safepoint_check_flag);
 222   if (!is_cms_thread) {
 223     assert(Thread::current()->is_VM_thread(), "Not a VM thread");
 224     CMSSynchronousYieldRequest yr;
 225     while (CMS_flag_is_set(CMS_cms_has_token)) {
 226       // indicate that we want to get the token


   1 /*
   2  * Copyright (c) 2001, 2016, 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  *


  25 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "gc/cms/concurrentMarkSweepGeneration.inline.hpp"
  28 #include "gc/cms/concurrentMarkSweepThread.hpp"
  29 #include "gc/shared/gcId.hpp"
  30 #include "gc/shared/genCollectedHeap.hpp"
  31 #include "oops/instanceRefKlass.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "runtime/init.hpp"
  34 #include "runtime/interfaceSupport.hpp"
  35 #include "runtime/java.hpp"
  36 #include "runtime/javaCalls.hpp"
  37 #include "runtime/mutexLocker.hpp"
  38 #include "runtime/os.hpp"
  39 #include "runtime/vmThread.hpp"
  40 
  41 // ======= Concurrent Mark Sweep Thread ========
  42 
  43 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::_cmst = NULL;
  44 CMSCollector* ConcurrentMarkSweepThread::_collector         = NULL;

  45 int  ConcurrentMarkSweepThread::_CMS_flag                   = CMS_nil;
  46 
  47 volatile jint ConcurrentMarkSweepThread::_pending_yields    = 0;
  48 
  49 SurrogateLockerThread* ConcurrentMarkSweepThread::_slt      = NULL;
  50 SurrogateLockerThread::SLT_msg_type
  51      ConcurrentMarkSweepThread::_sltBuffer = SurrogateLockerThread::empty;
  52 Monitor* ConcurrentMarkSweepThread::_sltMonitor             = NULL;
  53 
  54 ConcurrentMarkSweepThread::ConcurrentMarkSweepThread(CMSCollector* collector)
  55   : ConcurrentGCThread() {
  56   assert(UseConcMarkSweepGC,  "UseConcMarkSweepGC should be set");
  57   assert(_cmst == NULL, "CMS thread already created");
  58   _cmst = this;
  59   assert(_collector == NULL, "Collector already set");
  60   _collector = collector;
  61 
  62   set_name("CMS Main Thread");
  63 

  64   // An old comment here said: "Priority should be just less
  65   // than that of VMThread".  Since the VMThread runs at
  66   // NearMaxPriority, the old comment was inaccurate, but
  67   // changing the default priority to NearMaxPriority-1
  68   // could change current behavior, so the default of
  69   // NearMaxPriority stays in place.
  70   //
  71   // Note that there's a possibility of the VMThread
  72   // starving if UseCriticalCMSThreadPriority is on.
  73   // That won't happen on Solaris for various reasons,
  74   // but may well happen on non-Solaris platforms.
  75   create_and_start(UseCriticalCMSThreadPriority ? CriticalPriority : NearMaxPriority);






  76 




  77   _sltMonitor = SLT_lock;
  78 }
  79 
  80 void ConcurrentMarkSweepThread::run_service() {
  81   assert(this == cmst(), "just checking");
  82 



  83   if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) {
  84     warning("Couldn't bind CMS thread to processor " UINTX_FORMAT, CPUForCMSThread);
  85   }
  86 
  87   {


  88     MutexLockerEx x(CGC_lock, true);
  89     set_CMS_flag(CMS_cms_wants_token);
  90     assert(is_init_completed() && Universe::is_fully_initialized(), "ConcurrentGCThread::run() should have waited for this.");
  91 




  92     // Wait until the surrogate locker thread that will do
  93     // pending list locking on our behalf has been created.
  94     // We cannot start the SLT thread ourselves since we need
  95     // to be a JavaThread to do so.
  96     CMSLoopCountWarn loopY("CMS::run", "waiting for SLT installation", 2);
  97     while (_slt == NULL && !_should_terminate) {
  98       CGC_lock->wait(true, 200);
  99       loopY.tick();
 100     }
 101     clear_CMS_flag(CMS_cms_wants_token);
 102   }
 103 
 104   while (!_should_terminate) {
 105     sleepBeforeNextCycle();
 106     if (_should_terminate) break;
 107     GCIdMark gc_id_mark;
 108     GCCause::Cause cause = _collector->_full_gc_requested ?
 109       _collector->_full_gc_cause : GCCause::_cms_concurrent_mark;
 110     _collector->collect_in_background(cause);
 111   }
 112 
 113   // Check that the state of any protocol for synchronization
 114   // between background (CMS) and foreground collector is "clean"
 115   // (i.e. will not potentially block the foreground collector,
 116   // requiring action by us).
 117   verify_ok_to_terminate();








 118 }
 119 
 120 #ifndef PRODUCT
 121 void ConcurrentMarkSweepThread::verify_ok_to_terminate() const {
 122   assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() ||
 123            cms_thread_wants_cms_token()),
 124          "Must renounce all worldly possessions and desires for nirvana");
 125   _collector->verify_ok_to_terminate();
 126 }
 127 #endif
 128 
 129 // create and start a new ConcurrentMarkSweep Thread for given CMS generation
 130 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) {
 131   guarantee(_cmst == NULL, "start() called twice!");

 132   ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector);
 133   assert(_cmst == th, "Where did the just-created CMS thread go?");
 134   return th;


 135 }
 136 
 137 void ConcurrentMarkSweepThread::stop_all() {
 138   assert(_cmst != NULL, "stop_all should be called after initialization");
 139   _cmst->stop();
 140 }
 141 
 142 void ConcurrentMarkSweepThread::stop_service() {
 143   // Now post a notify on CGC_lock so as to nudge
 144   // CMS thread(s) that might be slumbering in
 145   // sleepBeforeNextCycle.
 146   MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
 147   CGC_lock->notify_all();







 148 }
 149 
 150 void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) {
 151   assert(tc != NULL, "Null ThreadClosure");
 152   if (cmst() != NULL) {
 153     tc->do_thread(cmst());
 154   }
 155   assert(Universe::is_fully_initialized(),
 156          "Called too early, make sure heap is fully initialized");
 157   if (_collector != NULL) {
 158     AbstractWorkGang* gang = _collector->conc_workers();
 159     if (gang != NULL) {
 160       gang->threads_do(tc);
 161     }
 162   }
 163 }
 164 
 165 void ConcurrentMarkSweepThread::print_all_on(outputStream* st) {
 166   if (cmst() != NULL) {
 167     cmst()->print_on(st);
 168     st->cr();
 169   }
 170   if (_collector != NULL) {
 171     AbstractWorkGang* gang = _collector->conc_workers();
 172     if (gang != NULL) {
 173       gang->print_worker_threads_on(st);
 174     }
 175   }
 176 }
 177 
 178 void ConcurrentMarkSweepThread::synchronize(bool is_cms_thread) {
 179   assert(UseConcMarkSweepGC, "just checking");
 180 
 181   MutexLockerEx x(CGC_lock,
 182                   Mutex::_no_safepoint_check_flag);
 183   if (!is_cms_thread) {
 184     assert(Thread::current()->is_VM_thread(), "Not a VM thread");
 185     CMSSynchronousYieldRequest yr;
 186     while (CMS_flag_is_set(CMS_cms_has_token)) {
 187       // indicate that we want to get the token


< prev index next >