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 "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 log_warning(gc)("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(); 319 } 320 } 321 322 void ConcurrentMarkSweepThread::sleepBeforeNextCycle() { 323 while (!should_terminate()) { 324 if(CMSWaitDuration >= 0) { 325 // Wait until the next synchronous GC, a concurrent full gc 326 // request or a timeout, whichever is earlier. 327 wait_on_cms_lock_for_scavenge(CMSWaitDuration); 328 } else { 329 // Wait until any cms_lock event or check interval not to call shouldConcurrentCollect permanently 330 wait_on_cms_lock(CMSCheckInterval); 331 } 332 // Check if we should start a CMS collection cycle 333 if (_collector->shouldConcurrentCollect()) { 334 return; 335 } 336 // .. collection criterion not yet met, let's go back 337 // and wait some more 338 } 339 } 340 341 // Note: this method, although exported by the ConcurrentMarkSweepThread, 342 // which is a non-JavaThread, can only be called by a JavaThread. 343 // Currently this is done at vm creation time (post-vm-init) by the 344 // main/Primordial (Java)Thread. 345 // XXX Consider changing this in the future to allow the CMS thread 346 // itself to create this thread? 347 void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) { 348 assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC"); 349 assert(_slt == NULL, "SLT already created"); 350 _slt = SurrogateLockerThread::make(THREAD); 351 } | 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 "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 "gc/shared/referencePendingListLocker.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 ConcurrentMarkSweepThread::ConcurrentMarkSweepThread(CMSCollector* collector) 50 : ConcurrentGCThread() { 51 assert(UseConcMarkSweepGC, "UseConcMarkSweepGC should be set"); 52 assert(_cmst == NULL, "CMS thread already created"); 53 _cmst = this; 54 assert(_collector == NULL, "Collector already set"); 55 _collector = collector; 56 57 set_name("CMS Main Thread"); 58 59 // An old comment here said: "Priority should be just less 60 // than that of VMThread". Since the VMThread runs at 61 // NearMaxPriority, the old comment was inaccurate, but 62 // changing the default priority to NearMaxPriority-1 63 // could change current behavior, so the default of 64 // NearMaxPriority stays in place. 65 // 66 // Note that there's a possibility of the VMThread 67 // starving if UseCriticalCMSThreadPriority is on. 68 // That won't happen on Solaris for various reasons, 69 // but may well happen on non-Solaris platforms. 70 create_and_start(UseCriticalCMSThreadPriority ? CriticalPriority : NearMaxPriority); 71 } 72 73 void ConcurrentMarkSweepThread::run_service() { 74 assert(this == cmst(), "just checking"); 75 76 if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) { 77 log_warning(gc)("Couldn't bind CMS thread to processor " UINTX_FORMAT, CPUForCMSThread); 78 } 79 80 { 81 MutexLockerEx x(CGC_lock, true); 82 set_CMS_flag(CMS_cms_wants_token); 83 assert(is_init_completed() && Universe::is_fully_initialized(), "ConcurrentGCThread::run() should have waited for this."); 84 85 // Wait until the surrogate locker thread that will do 86 // pending list locking on our behalf has been created. 87 // We cannot start the SLT thread ourselves since we need 88 // to be a JavaThread to do so. 89 CMSLoopCountWarn loopY("CMS::run", "waiting for SLT installation", 2); 90 while (!ReferencePendingListLocker::is_initialized() && !should_terminate()) { 91 CGC_lock->wait(true, 200); 92 loopY.tick(); 93 } 94 clear_CMS_flag(CMS_cms_wants_token); 95 } 96 97 while (!should_terminate()) { 98 sleepBeforeNextCycle(); 99 if (should_terminate()) break; 100 GCIdMark gc_id_mark; 101 GCCause::Cause cause = _collector->_full_gc_requested ? 102 _collector->_full_gc_cause : GCCause::_cms_concurrent_mark; 103 _collector->collect_in_background(cause); 104 } 105 106 // Check that the state of any protocol for synchronization 107 // between background (CMS) and foreground collector is "clean" 108 // (i.e. will not potentially block the foreground collector, 109 // requiring action by us). 110 verify_ok_to_terminate(); 312 } 313 } 314 315 void ConcurrentMarkSweepThread::sleepBeforeNextCycle() { 316 while (!should_terminate()) { 317 if(CMSWaitDuration >= 0) { 318 // Wait until the next synchronous GC, a concurrent full gc 319 // request or a timeout, whichever is earlier. 320 wait_on_cms_lock_for_scavenge(CMSWaitDuration); 321 } else { 322 // Wait until any cms_lock event or check interval not to call shouldConcurrentCollect permanently 323 wait_on_cms_lock(CMSCheckInterval); 324 } 325 // Check if we should start a CMS collection cycle 326 if (_collector->shouldConcurrentCollect()) { 327 return; 328 } 329 // .. collection criterion not yet met, let's go back 330 // and wait some more 331 } 332 } |