< prev index next >

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

Print this page
rev 7209 : [mq]: inccms


  32 #include "runtime/init.hpp"
  33 #include "runtime/interfaceSupport.hpp"
  34 #include "runtime/java.hpp"
  35 #include "runtime/javaCalls.hpp"
  36 #include "runtime/mutexLocker.hpp"
  37 #include "runtime/os.hpp"
  38 #include "runtime/vmThread.hpp"
  39 
  40 // ======= Concurrent Mark Sweep Thread ========
  41 
  42 // The CMS thread is created when Concurrent Mark Sweep is used in the
  43 // older of two generations in a generational memory system.
  44 
  45 ConcurrentMarkSweepThread*
  46      ConcurrentMarkSweepThread::_cmst     = NULL;
  47 CMSCollector* ConcurrentMarkSweepThread::_collector = NULL;
  48 bool ConcurrentMarkSweepThread::_should_terminate = false;
  49 int  ConcurrentMarkSweepThread::_CMS_flag         = CMS_nil;
  50 
  51 volatile jint ConcurrentMarkSweepThread::_pending_yields      = 0;
  52 volatile jint ConcurrentMarkSweepThread::_pending_decrements  = 0;
  53 
  54 volatile jint ConcurrentMarkSweepThread::_icms_disabled   = 0;
  55 volatile bool ConcurrentMarkSweepThread::_should_run     = false;
  56 // When icms is enabled, the icms thread is stopped until explicitly
  57 // started.
  58 volatile bool ConcurrentMarkSweepThread::_should_stop    = true;
  59 
  60 SurrogateLockerThread*
  61      ConcurrentMarkSweepThread::_slt = NULL;
  62 SurrogateLockerThread::SLT_msg_type
  63      ConcurrentMarkSweepThread::_sltBuffer = SurrogateLockerThread::empty;
  64 Monitor*
  65      ConcurrentMarkSweepThread::_sltMonitor = NULL;
  66 
  67 ConcurrentMarkSweepThread::ConcurrentMarkSweepThread(CMSCollector* collector)
  68   : ConcurrentGCThread() {
  69   assert(UseConcMarkSweepGC,  "UseConcMarkSweepGC should be set");
  70   assert(_cmst == NULL, "CMS thread already created");
  71   _cmst = this;
  72   assert(_collector == NULL, "Collector already set");
  73   _collector = collector;
  74 
  75   set_name("Concurrent Mark-Sweep GC Thread");
  76 
  77   if (os::create_thread(this, os::cgc_thread)) {
  78     // An old comment here said: "Priority should be just less


  82     // could change current behavior, so the default of
  83     // NearMaxPriority stays in place.
  84     //
  85     // Note that there's a possibility of the VMThread
  86     // starving if UseCriticalCMSThreadPriority is on.
  87     // That won't happen on Solaris for various reasons,
  88     // but may well happen on non-Solaris platforms.
  89     int native_prio;
  90     if (UseCriticalCMSThreadPriority) {
  91       native_prio = os::java_to_os_priority[CriticalPriority];
  92     } else {
  93       native_prio = os::java_to_os_priority[NearMaxPriority];
  94     }
  95     os::set_native_priority(this, native_prio);
  96 
  97     if (!DisableStartThread) {
  98       os::start_thread(this);
  99     }
 100   }
 101   _sltMonitor = SLT_lock;
 102   assert(!CMSIncrementalMode || icms_is_enabled(), "Error");
 103 }
 104 
 105 void ConcurrentMarkSweepThread::run() {
 106   assert(this == cmst(), "just checking");
 107 
 108   this->record_stack_base_and_size();
 109   this->initialize_thread_local_storage();
 110   this->set_active_handles(JNIHandleBlock::allocate_block());
 111   // From this time Thread::current() should be working.
 112   assert(this == Thread::current(), "just checking");
 113   if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) {
 114     warning("Couldn't bind CMS thread to processor " UINTX_FORMAT, CPUForCMSThread);
 115   }
 116   // Wait until Universe::is_fully_initialized()
 117   {
 118     CMSLoopCountWarn loopX("CMS::run", "waiting for "
 119                            "Universe::is_fully_initialized()", 2);
 120     MutexLockerEx x(CGC_lock, true);
 121     set_CMS_flag(CMS_cms_wants_token);
 122     // Wait until Universe is initialized and all initialization is completed.


 167 void ConcurrentMarkSweepThread::verify_ok_to_terminate() const {
 168   assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() ||
 169            cms_thread_wants_cms_token()),
 170          "Must renounce all worldly possessions and desires for nirvana");
 171   _collector->verify_ok_to_terminate();
 172 }
 173 #endif
 174 
 175 // create and start a new ConcurrentMarkSweep Thread for given CMS generation
 176 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) {
 177   if (!_should_terminate) {
 178     assert(cmst() == NULL, "start() called twice?");
 179     ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector);
 180     assert(cmst() == th, "Where did the just-created CMS thread go?");
 181     return th;
 182   }
 183   return NULL;
 184 }
 185 
 186 void ConcurrentMarkSweepThread::stop() {
 187   if (CMSIncrementalMode) {
 188     // Disable incremental mode and wake up the thread so it notices the change.
 189     disable_icms();
 190     start_icms();
 191   }
 192   // it is ok to take late safepoints here, if needed
 193   {
 194     MutexLockerEx x(Terminator_lock);
 195     _should_terminate = true;
 196   }
 197   { // Now post a notify on CGC_lock so as to nudge
 198     // CMS thread(s) that might be slumbering in
 199     // sleepBeforeNextCycle.
 200     MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
 201     CGC_lock->notify_all();
 202   }
 203   { // Now wait until (all) CMS thread(s) have exited
 204     MutexLockerEx x(Terminator_lock);
 205     while(cmst() != NULL) {
 206       Terminator_lock->wait();
 207     }
 208   }
 209 }
 210 
 211 void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) {


 370     unsigned int after_count;
 371     {
 372       MutexLockerEx hl(Heap_lock, Mutex::_no_safepoint_check_flag);
 373       after_count = gch->total_collections();
 374     }
 375 
 376     if(before_count != after_count) {
 377       // There was a collection - success
 378       break;
 379     }
 380 
 381     // Too many loops warning
 382     if(++loop_count == 0) {
 383       warning("wait_on_cms_lock_for_scavenge() has looped %u times", loop_count - 1);
 384     }
 385   }
 386 }
 387 
 388 void ConcurrentMarkSweepThread::sleepBeforeNextCycle() {
 389   while (!_should_terminate) {
 390     if (CMSIncrementalMode) {
 391       icms_wait();
 392       if(CMSWaitDuration >= 0) {
 393         // Wait until the next synchronous GC, a concurrent full gc
 394         // request or a timeout, whichever is earlier.
 395         wait_on_cms_lock_for_scavenge(CMSWaitDuration);
 396       }
 397       return;
 398     } else {
 399       if(CMSWaitDuration >= 0) {
 400         // Wait until the next synchronous GC, a concurrent full gc
 401         // request or a timeout, whichever is earlier.
 402         wait_on_cms_lock_for_scavenge(CMSWaitDuration);
 403       } else {
 404         // Wait until any cms_lock event or check interval not to call shouldConcurrentCollect permanently
 405         wait_on_cms_lock(CMSCheckInterval);
 406       }
 407     }
 408     // Check if we should start a CMS collection cycle
 409     if (_collector->shouldConcurrentCollect()) {
 410       return;
 411     }
 412     // .. collection criterion not yet met, let's go back
 413     // and wait some more
 414   }
 415 }
 416 
 417 // Incremental CMS
 418 void ConcurrentMarkSweepThread::start_icms() {
 419   assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
 420   MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
 421   trace_state("start_icms");
 422   _should_run = true;
 423   iCMS_lock->notify_all();
 424 }
 425 
 426 void ConcurrentMarkSweepThread::stop_icms() {
 427   assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
 428   MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
 429   if (!_should_stop) {
 430     trace_state("stop_icms");
 431     _should_stop = true;
 432     _should_run = false;
 433     asynchronous_yield_request();
 434     iCMS_lock->notify_all();
 435   }
 436 }
 437 
 438 void ConcurrentMarkSweepThread::icms_wait() {
 439   assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
 440   if (_should_stop && icms_is_enabled()) {
 441     MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
 442     trace_state("pause_icms");
 443     _collector->stats().stop_cms_timer();
 444     while(!_should_run && icms_is_enabled()) {
 445       iCMS_lock->wait(Mutex::_no_safepoint_check_flag);
 446     }
 447     _collector->stats().start_cms_timer();
 448     _should_stop = false;
 449     trace_state("pause_icms end");
 450   }
 451 }
 452 
 453 // Note: this method, although exported by the ConcurrentMarkSweepThread,
 454 // which is a non-JavaThread, can only be called by a JavaThread.
 455 // Currently this is done at vm creation time (post-vm-init) by the
 456 // main/Primordial (Java)Thread.
 457 // XXX Consider changing this in the future to allow the CMS thread
 458 // itself to create this thread?
 459 void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) {
 460   assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC");
 461   assert(_slt == NULL, "SLT already created");
 462   _slt = SurrogateLockerThread::make(THREAD);
 463 }


  32 #include "runtime/init.hpp"
  33 #include "runtime/interfaceSupport.hpp"
  34 #include "runtime/java.hpp"
  35 #include "runtime/javaCalls.hpp"
  36 #include "runtime/mutexLocker.hpp"
  37 #include "runtime/os.hpp"
  38 #include "runtime/vmThread.hpp"
  39 
  40 // ======= Concurrent Mark Sweep Thread ========
  41 
  42 // The CMS thread is created when Concurrent Mark Sweep is used in the
  43 // older of two generations in a generational memory system.
  44 
  45 ConcurrentMarkSweepThread*
  46      ConcurrentMarkSweepThread::_cmst     = NULL;
  47 CMSCollector* ConcurrentMarkSweepThread::_collector = NULL;
  48 bool ConcurrentMarkSweepThread::_should_terminate = false;
  49 int  ConcurrentMarkSweepThread::_CMS_flag         = CMS_nil;
  50 
  51 volatile jint ConcurrentMarkSweepThread::_pending_yields      = 0;







  52 
  53 SurrogateLockerThread*
  54      ConcurrentMarkSweepThread::_slt = NULL;
  55 SurrogateLockerThread::SLT_msg_type
  56      ConcurrentMarkSweepThread::_sltBuffer = SurrogateLockerThread::empty;
  57 Monitor*
  58      ConcurrentMarkSweepThread::_sltMonitor = NULL;
  59 
  60 ConcurrentMarkSweepThread::ConcurrentMarkSweepThread(CMSCollector* collector)
  61   : ConcurrentGCThread() {
  62   assert(UseConcMarkSweepGC,  "UseConcMarkSweepGC should be set");
  63   assert(_cmst == NULL, "CMS thread already created");
  64   _cmst = this;
  65   assert(_collector == NULL, "Collector already set");
  66   _collector = collector;
  67 
  68   set_name("Concurrent Mark-Sweep GC Thread");
  69 
  70   if (os::create_thread(this, os::cgc_thread)) {
  71     // An old comment here said: "Priority should be just less


  75     // could change current behavior, so the default of
  76     // NearMaxPriority stays in place.
  77     //
  78     // Note that there's a possibility of the VMThread
  79     // starving if UseCriticalCMSThreadPriority is on.
  80     // That won't happen on Solaris for various reasons,
  81     // but may well happen on non-Solaris platforms.
  82     int native_prio;
  83     if (UseCriticalCMSThreadPriority) {
  84       native_prio = os::java_to_os_priority[CriticalPriority];
  85     } else {
  86       native_prio = os::java_to_os_priority[NearMaxPriority];
  87     }
  88     os::set_native_priority(this, native_prio);
  89 
  90     if (!DisableStartThread) {
  91       os::start_thread(this);
  92     }
  93   }
  94   _sltMonitor = SLT_lock;

  95 }
  96 
  97 void ConcurrentMarkSweepThread::run() {
  98   assert(this == cmst(), "just checking");
  99 
 100   this->record_stack_base_and_size();
 101   this->initialize_thread_local_storage();
 102   this->set_active_handles(JNIHandleBlock::allocate_block());
 103   // From this time Thread::current() should be working.
 104   assert(this == Thread::current(), "just checking");
 105   if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) {
 106     warning("Couldn't bind CMS thread to processor " UINTX_FORMAT, CPUForCMSThread);
 107   }
 108   // Wait until Universe::is_fully_initialized()
 109   {
 110     CMSLoopCountWarn loopX("CMS::run", "waiting for "
 111                            "Universe::is_fully_initialized()", 2);
 112     MutexLockerEx x(CGC_lock, true);
 113     set_CMS_flag(CMS_cms_wants_token);
 114     // Wait until Universe is initialized and all initialization is completed.


 159 void ConcurrentMarkSweepThread::verify_ok_to_terminate() const {
 160   assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() ||
 161            cms_thread_wants_cms_token()),
 162          "Must renounce all worldly possessions and desires for nirvana");
 163   _collector->verify_ok_to_terminate();
 164 }
 165 #endif
 166 
 167 // create and start a new ConcurrentMarkSweep Thread for given CMS generation
 168 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) {
 169   if (!_should_terminate) {
 170     assert(cmst() == NULL, "start() called twice?");
 171     ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector);
 172     assert(cmst() == th, "Where did the just-created CMS thread go?");
 173     return th;
 174   }
 175   return NULL;
 176 }
 177 
 178 void ConcurrentMarkSweepThread::stop() {





 179   // it is ok to take late safepoints here, if needed
 180   {
 181     MutexLockerEx x(Terminator_lock);
 182     _should_terminate = true;
 183   }
 184   { // Now post a notify on CGC_lock so as to nudge
 185     // CMS thread(s) that might be slumbering in
 186     // sleepBeforeNextCycle.
 187     MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
 188     CGC_lock->notify_all();
 189   }
 190   { // Now wait until (all) CMS thread(s) have exited
 191     MutexLockerEx x(Terminator_lock);
 192     while(cmst() != NULL) {
 193       Terminator_lock->wait();
 194     }
 195   }
 196 }
 197 
 198 void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) {


 357     unsigned int after_count;
 358     {
 359       MutexLockerEx hl(Heap_lock, Mutex::_no_safepoint_check_flag);
 360       after_count = gch->total_collections();
 361     }
 362 
 363     if(before_count != after_count) {
 364       // There was a collection - success
 365       break;
 366     }
 367 
 368     // Too many loops warning
 369     if(++loop_count == 0) {
 370       warning("wait_on_cms_lock_for_scavenge() has looped %u times", loop_count - 1);
 371     }
 372   }
 373 }
 374 
 375 void ConcurrentMarkSweepThread::sleepBeforeNextCycle() {
 376   while (!_should_terminate) {









 377     if(CMSWaitDuration >= 0) {
 378       // Wait until the next synchronous GC, a concurrent full gc
 379       // request or a timeout, whichever is earlier.
 380       wait_on_cms_lock_for_scavenge(CMSWaitDuration);
 381     } else {
 382       // Wait until any cms_lock event or check interval not to call shouldConcurrentCollect permanently
 383       wait_on_cms_lock(CMSCheckInterval);
 384     }

 385     // Check if we should start a CMS collection cycle
 386     if (_collector->shouldConcurrentCollect()) {
 387       return;
 388     }
 389     // .. collection criterion not yet met, let's go back
 390     // and wait some more




































 391   }
 392 }
 393 
 394 // Note: this method, although exported by the ConcurrentMarkSweepThread,
 395 // which is a non-JavaThread, can only be called by a JavaThread.
 396 // Currently this is done at vm creation time (post-vm-init) by the
 397 // main/Primordial (Java)Thread.
 398 // XXX Consider changing this in the future to allow the CMS thread
 399 // itself to create this thread?
 400 void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) {
 401   assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC");
 402   assert(_slt == NULL, "SLT already created");
 403   _slt = SurrogateLockerThread::make(THREAD);
 404 }
< prev index next >