1 /* 2 * Copyright (c) 2001, 2011, 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 "classfile/systemDictionary.hpp" 27 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp" 28 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp" 29 #include "memory/genCollectedHeap.hpp" 30 #include "oops/instanceRefKlass.hpp" 31 #include "oops/oop.inline.hpp" 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 // Priority should be just less than that of VMThread. 79 int native_prio; 80 if (UseCriticalCMSThreadPriority) { 81 native_prio = os::java_to_os_priority[CriticalPriority]; 82 } else { 83 native_prio = os::java_to_os_priority[NearMaxPriority]; 84 } 85 os::set_native_priority(this, native_prio); 86 87 if (!DisableStartThread) { 88 os::start_thread(this); 89 } 90 } 91 _sltMonitor = SLT_lock; 92 assert(!CMSIncrementalMode || icms_is_enabled(), "Error"); 93 } 94 95 void ConcurrentMarkSweepThread::run() { 96 assert(this == cmst(), "just checking"); 97 98 this->record_stack_base_and_size(); 99 this->initialize_thread_local_storage(); 100 this->set_active_handles(JNIHandleBlock::allocate_block()); 101 // From this time Thread::current() should be working. 102 assert(this == Thread::current(), "just checking"); 103 if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) { 104 warning("Couldn't bind CMS thread to processor %u", CPUForCMSThread); 105 } 106 // Wait until Universe::is_fully_initialized() 107 { 108 CMSLoopCountWarn loopX("CMS::run", "waiting for " 109 "Universe::is_fully_initialized()", 2); 110 MutexLockerEx x(CGC_lock, true); 111 set_CMS_flag(CMS_cms_wants_token); 112 // Wait until Universe is initialized and all initialization is completed. 113 while (!is_init_completed() && !Universe::is_fully_initialized() && 114 !_should_terminate) { 115 CGC_lock->wait(true, 200); 116 loopX.tick(); 117 } 118 // Wait until the surrogate locker thread that will do 119 // pending list locking on our behalf has been created. 120 // We cannot start the SLT thread ourselves since we need 121 // to be a JavaThread to do so. 122 CMSLoopCountWarn loopY("CMS::run", "waiting for SLT installation", 2); 123 while (_slt == NULL && !_should_terminate) { 124 CGC_lock->wait(true, 200); 125 loopY.tick(); 126 } 127 clear_CMS_flag(CMS_cms_wants_token); 128 } 129 130 while (!_should_terminate) { 131 sleepBeforeNextCycle(); 132 if (_should_terminate) break; 133 _collector->collect_in_background(false); // !clear_all_soft_refs 134 } 135 assert(_should_terminate, "just checking"); 136 // Check that the state of any protocol for synchronization 137 // between background (CMS) and foreground collector is "clean" 138 // (i.e. will not potentially block the foreground collector, 139 // requiring action by us). 140 verify_ok_to_terminate(); 141 // Signal that it is terminated 142 { 143 MutexLockerEx mu(Terminator_lock, 144 Mutex::_no_safepoint_check_flag); 145 assert(_cmst == this, "Weird!"); 146 _cmst = NULL; 147 Terminator_lock->notify(); 148 } 149 150 // Thread destructor usually does this.. 151 ThreadLocalStorage::set_thread(NULL); 152 } 153 154 #ifndef PRODUCT 155 void ConcurrentMarkSweepThread::verify_ok_to_terminate() const { 156 assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() || 157 cms_thread_wants_cms_token()), 158 "Must renounce all worldly possessions and desires for nirvana"); 159 _collector->verify_ok_to_terminate(); 160 } 161 #endif 162 163 // create and start a new ConcurrentMarkSweep Thread for given CMS generation 164 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) { 165 if (!_should_terminate) { 166 assert(cmst() == NULL, "start() called twice?"); 167 ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector); 168 assert(cmst() == th, "Where did the just-created CMS thread go?"); 169 return th; 170 } 171 return NULL; 172 } 173 174 void ConcurrentMarkSweepThread::stop() { 175 if (CMSIncrementalMode) { 176 // Disable incremental mode and wake up the thread so it notices the change. 177 disable_icms(); 178 start_icms(); 179 } 180 // it is ok to take late safepoints here, if needed 181 { 182 MutexLockerEx x(Terminator_lock); 183 _should_terminate = true; 184 } 185 { // Now post a notify on CGC_lock so as to nudge 186 // CMS thread(s) that might be slumbering in 187 // sleepBeforeNextCycle. 188 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag); 189 CGC_lock->notify_all(); 190 } 191 { // Now wait until (all) CMS thread(s) have exited 192 MutexLockerEx x(Terminator_lock); 193 while(cmst() != NULL) { 194 Terminator_lock->wait(); 195 } 196 } 197 } 198 199 void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) { 200 assert(tc != NULL, "Null ThreadClosure"); 201 if (_cmst != NULL) { 202 tc->do_thread(_cmst); 203 } 204 assert(Universe::is_fully_initialized(), 205 "Called too early, make sure heap is fully initialized"); 206 if (_collector != NULL) { 207 AbstractWorkGang* gang = _collector->conc_workers(); 208 if (gang != NULL) { 209 gang->threads_do(tc); 210 } 211 } 212 } 213 214 void ConcurrentMarkSweepThread::print_on(outputStream* st) const { 215 st->print("\"%s\" ", name()); 216 Thread::print_on(st); 217 st->cr(); 218 } 219 220 void ConcurrentMarkSweepThread::print_all_on(outputStream* st) { 221 if (_cmst != NULL) { 222 _cmst->print_on(st); 223 } 224 if (_collector != NULL) { 225 AbstractWorkGang* gang = _collector->conc_workers(); 226 if (gang != NULL) { 227 gang->print_worker_threads_on(st); 228 } 229 } 230 } 231 232 void ConcurrentMarkSweepThread::synchronize(bool is_cms_thread) { 233 assert(UseConcMarkSweepGC, "just checking"); 234 235 MutexLockerEx x(CGC_lock, 236 Mutex::_no_safepoint_check_flag); 237 if (!is_cms_thread) { 238 assert(Thread::current()->is_VM_thread(), "Not a VM thread"); 239 CMSSynchronousYieldRequest yr; 240 while (CMS_flag_is_set(CMS_cms_has_token)) { 241 // indicate that we want to get the token 242 set_CMS_flag(CMS_vm_wants_token); 243 CGC_lock->wait(true); 244 } 245 // claim the token and proceed 246 clear_CMS_flag(CMS_vm_wants_token); 247 set_CMS_flag(CMS_vm_has_token); 248 } else { 249 assert(Thread::current()->is_ConcurrentGC_thread(), 250 "Not a CMS thread"); 251 // The following barrier assumes there's only one CMS thread. 252 // This will need to be modified is there are more CMS threads than one. 253 while (CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token)) { 254 set_CMS_flag(CMS_cms_wants_token); 255 CGC_lock->wait(true); 256 } 257 // claim the token 258 clear_CMS_flag(CMS_cms_wants_token); 259 set_CMS_flag(CMS_cms_has_token); 260 } 261 } 262 263 void ConcurrentMarkSweepThread::desynchronize(bool is_cms_thread) { 264 assert(UseConcMarkSweepGC, "just checking"); 265 266 MutexLockerEx x(CGC_lock, 267 Mutex::_no_safepoint_check_flag); 268 if (!is_cms_thread) { 269 assert(Thread::current()->is_VM_thread(), "Not a VM thread"); 270 assert(CMS_flag_is_set(CMS_vm_has_token), "just checking"); 271 clear_CMS_flag(CMS_vm_has_token); 272 if (CMS_flag_is_set(CMS_cms_wants_token)) { 273 // wake-up a waiting CMS thread 274 CGC_lock->notify(); 275 } 276 assert(!CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token), 277 "Should have been cleared"); 278 } else { 279 assert(Thread::current()->is_ConcurrentGC_thread(), 280 "Not a CMS thread"); 281 assert(CMS_flag_is_set(CMS_cms_has_token), "just checking"); 282 clear_CMS_flag(CMS_cms_has_token); 283 if (CMS_flag_is_set(CMS_vm_wants_token)) { 284 // wake-up a waiting VM thread 285 CGC_lock->notify(); 286 } 287 assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token), 288 "Should have been cleared"); 289 } 290 } 291 292 // Wait until the next synchronous GC, a concurrent full gc request, 293 // or a timeout, whichever is earlier. 294 void ConcurrentMarkSweepThread::wait_on_cms_lock(long t_millis) { 295 MutexLockerEx x(CGC_lock, 296 Mutex::_no_safepoint_check_flag); 297 if (_should_terminate || _collector->_full_gc_requested) { 298 return; 299 } 300 set_CMS_flag(CMS_cms_wants_token); // to provoke notifies 301 CGC_lock->wait(Mutex::_no_safepoint_check_flag, t_millis); 302 clear_CMS_flag(CMS_cms_wants_token); 303 assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token), 304 "Should not be set"); 305 } 306 307 void ConcurrentMarkSweepThread::sleepBeforeNextCycle() { 308 while (!_should_terminate) { 309 if (CMSIncrementalMode) { 310 icms_wait(); 311 return; 312 } else { 313 // Wait until the next synchronous GC, a concurrent full gc 314 // request or a timeout, whichever is earlier. 315 wait_on_cms_lock(CMSWaitDuration); 316 } 317 // Check if we should start a CMS collection cycle 318 if (_collector->shouldConcurrentCollect()) { 319 return; 320 } 321 // .. collection criterion not yet met, let's go back 322 // and wait some more 323 } 324 } 325 326 // Incremental CMS 327 void ConcurrentMarkSweepThread::start_icms() { 328 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking"); 329 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag); 330 trace_state("start_icms"); 331 _should_run = true; 332 iCMS_lock->notify_all(); 333 } 334 335 void ConcurrentMarkSweepThread::stop_icms() { 336 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking"); 337 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag); 338 if (!_should_stop) { 339 trace_state("stop_icms"); 340 _should_stop = true; 341 _should_run = false; 342 asynchronous_yield_request(); 343 iCMS_lock->notify_all(); 344 } 345 } 346 347 void ConcurrentMarkSweepThread::icms_wait() { 348 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking"); 349 if (_should_stop && icms_is_enabled()) { 350 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag); 351 trace_state("pause_icms"); 352 _collector->stats().stop_cms_timer(); 353 while(!_should_run && icms_is_enabled()) { 354 iCMS_lock->wait(Mutex::_no_safepoint_check_flag); 355 } 356 _collector->stats().start_cms_timer(); 357 _should_stop = false; 358 trace_state("pause_icms end"); 359 } 360 } 361 362 // Note: this method, although exported by the ConcurrentMarkSweepThread, 363 // which is a non-JavaThread, can only be called by a JavaThread. 364 // Currently this is done at vm creation time (post-vm-init) by the 365 // main/Primordial (Java)Thread. 366 // XXX Consider changing this in the future to allow the CMS thread 367 // itself to create this thread? 368 void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) { 369 assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC"); 370 assert(_slt == NULL, "SLT already created"); 371 _slt = SurrogateLockerThread::make(THREAD); 372 }