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

Print this page




  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 bool ConcurrentMarkSweepThread::_icms_enabled   = false;
  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     // XXX: need to set this to low priority
  79     // unless "agressive mode" set; priority
  80     // should be just less than that of VMThread.
  81     os::set_priority(this, NearMaxPriority);
  82     if (!DisableStartThread) {
  83       os::start_thread(this);
  84     }
  85   }
  86   _sltMonitor = SLT_lock;
  87   set_icms_enabled(CMSIncrementalMode);
  88 }
  89 
  90 void ConcurrentMarkSweepThread::run() {
  91   assert(this == cmst(), "just checking");
  92 
  93   this->record_stack_base_and_size();
  94   this->initialize_thread_local_storage();
  95   this->set_active_handles(JNIHandleBlock::allocate_block());
  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 %u", 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.


 324   MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
 325   trace_state("start_icms");
 326   _should_run = true;
 327   iCMS_lock->notify_all();
 328 }
 329 
 330 void ConcurrentMarkSweepThread::stop_icms() {
 331   assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
 332   MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
 333   if (!_should_stop) {
 334     trace_state("stop_icms");
 335     _should_stop = true;
 336     _should_run = false;
 337     asynchronous_yield_request();
 338     iCMS_lock->notify_all();
 339   }
 340 }
 341 
 342 void ConcurrentMarkSweepThread::icms_wait() {
 343   assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
 344   if (_should_stop && icms_enabled()) {
 345     MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
 346     trace_state("pause_icms");
 347     _collector->stats().stop_cms_timer();
 348     while(!_should_run && icms_enabled()) {
 349       iCMS_lock->wait(Mutex::_no_safepoint_check_flag);
 350     }
 351     _collector->stats().start_cms_timer();
 352     _should_stop = false;
 353     trace_state("pause_icms end");
 354   }
 355 }
 356 
 357 // Note: this method, although exported by the ConcurrentMarkSweepThread,
 358 // which is a non-JavaThread, can only be called by a JavaThread.
 359 // Currently this is done at vm creation time (post-vm-init) by the
 360 // main/Primordial (Java)Thread.
 361 // XXX Consider changing this in the future to allow the CMS thread
 362 // itself to create this thread?
 363 void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) {
 364   assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC");
 365   assert(_slt == NULL, "SLT already created");
 366   _slt = SurrogateLockerThread::make(THREAD);
 367 }


  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     // XXX: need to set this to low priority
  79     // unless "agressive mode" set; priority
  80     // should be just less than that of VMThread.
  81     os::set_priority(this, NearMaxPriority);
  82     if (!DisableStartThread) {
  83       os::start_thread(this);
  84     }
  85   }
  86   _sltMonitor = SLT_lock;
  87   assert(!CMSIncrementalMode || icms_is_enabled(), "Error");
  88 }
  89 
  90 void ConcurrentMarkSweepThread::run() {
  91   assert(this == cmst(), "just checking");
  92 
  93   this->record_stack_base_and_size();
  94   this->initialize_thread_local_storage();
  95   this->set_active_handles(JNIHandleBlock::allocate_block());
  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 %u", 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.


 324   MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
 325   trace_state("start_icms");
 326   _should_run = true;
 327   iCMS_lock->notify_all();
 328 }
 329 
 330 void ConcurrentMarkSweepThread::stop_icms() {
 331   assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
 332   MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
 333   if (!_should_stop) {
 334     trace_state("stop_icms");
 335     _should_stop = true;
 336     _should_run = false;
 337     asynchronous_yield_request();
 338     iCMS_lock->notify_all();
 339   }
 340 }
 341 
 342 void ConcurrentMarkSweepThread::icms_wait() {
 343   assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
 344   if (_should_stop && icms_is_enabled()) {
 345     MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
 346     trace_state("pause_icms");
 347     _collector->stats().stop_cms_timer();
 348     while(!_should_run && icms_is_enabled()) {
 349       iCMS_lock->wait(Mutex::_no_safepoint_check_flag);
 350     }
 351     _collector->stats().start_cms_timer();
 352     _should_stop = false;
 353     trace_state("pause_icms end");
 354   }
 355 }
 356 
 357 // Note: this method, although exported by the ConcurrentMarkSweepThread,
 358 // which is a non-JavaThread, can only be called by a JavaThread.
 359 // Currently this is done at vm creation time (post-vm-init) by the
 360 // main/Primordial (Java)Thread.
 361 // XXX Consider changing this in the future to allow the CMS thread
 362 // itself to create this thread?
 363 void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) {
 364   assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC");
 365   assert(_slt == NULL, "SLT already created");
 366   _slt = SurrogateLockerThread::make(THREAD);
 367 }