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 * 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/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 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::_cmst = NULL; 43 CMSCollector* ConcurrentMarkSweepThread::_collector = NULL; 44 bool ConcurrentMarkSweepThread::_should_terminate = false; 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 if (os::create_thread(this, os::cgc_thread)) { 65 // An old comment here said: "Priority should be just less 66 // than that of VMThread". Since the VMThread runs at 67 // NearMaxPriority, the old comment was inaccurate, but 68 // changing the default priority to NearMaxPriority-1 69 // could change current behavior, so the default of 70 // NearMaxPriority stays in place. 71 // 72 // Note that there's a possibility of the VMThread 73 // starving if UseCriticalCMSThreadPriority is on. 74 // That won't happen on Solaris for various reasons, 75 // but may well happen on non-Solaris platforms. 76 int native_prio; 77 if (UseCriticalCMSThreadPriority) { 78 native_prio = os::java_to_os_priority[CriticalPriority]; 79 } else { 80 native_prio = os::java_to_os_priority[NearMaxPriority]; 81 } 82 os::set_native_priority(this, native_prio); 83 84 if (!DisableStartThread) { 85 os::start_thread(this); 86 } 87 } 88 _sltMonitor = SLT_lock; 89 } 90 91 void ConcurrentMarkSweepThread::run() { 92 assert(this == cmst(), "just checking"); 93 94 initialize_in_thread(); 95 // From this time Thread::current() should be working. 96 assert(this == Thread::current(), "just checking"); 97 if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) { 98 warning("Couldn't bind CMS thread to processor " UINTX_FORMAT, CPUForCMSThread); 99 } 100 // Wait until Universe::is_fully_initialized() 101 { 102 CMSLoopCountWarn loopX("CMS::run", "waiting for " 103 "Universe::is_fully_initialized()", 2); 104 MutexLockerEx x(CGC_lock, true); 105 set_CMS_flag(CMS_cms_wants_token); 106 // Wait until Universe is initialized and all initialization is completed. 107 while (!is_init_completed() && !Universe::is_fully_initialized() && 108 !_should_terminate) { 109 CGC_lock->wait(true, 200); 110 loopX.tick(); 111 } 112 // Wait until the surrogate locker thread that will do 113 // pending list locking on our behalf has been created. 114 // We cannot start the SLT thread ourselves since we need 115 // to be a JavaThread to do so. 116 CMSLoopCountWarn loopY("CMS::run", "waiting for SLT installation", 2); 117 while (_slt == NULL && !_should_terminate) { 118 CGC_lock->wait(true, 200); 119 loopY.tick(); 120 } 121 clear_CMS_flag(CMS_cms_wants_token); 122 } 123 124 while (!_should_terminate) { 125 sleepBeforeNextCycle(); 126 if (_should_terminate) break; 127 GCCause::Cause cause = _collector->_full_gc_requested ? 128 _collector->_full_gc_cause : GCCause::_cms_concurrent_mark; 129 _collector->collect_in_background(cause); 130 } 131 assert(_should_terminate, "just checking"); 132 // Check that the state of any protocol for synchronization 133 // between background (CMS) and foreground collector is "clean" 134 // (i.e. will not potentially block the foreground collector, 135 // requiring action by us). 136 verify_ok_to_terminate(); 137 // Signal that it is terminated 138 { 139 MutexLockerEx mu(Terminator_lock, 140 Mutex::_no_safepoint_check_flag); 141 assert(_cmst == this, "Weird!"); 142 _cmst = NULL; 143 Terminator_lock->notify(); 144 } 145 146 // Thread destructor usually does this.. 147 ThreadLocalStorage::set_thread(NULL); 148 } 149 150 #ifndef PRODUCT 151 void ConcurrentMarkSweepThread::verify_ok_to_terminate() const { 152 assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() || 153 cms_thread_wants_cms_token()), 154 "Must renounce all worldly possessions and desires for nirvana"); 155 _collector->verify_ok_to_terminate(); 156 } 157 #endif 158 159 // create and start a new ConcurrentMarkSweep Thread for given CMS generation 160 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) { 161 if (!_should_terminate) { 162 assert(cmst() == NULL, "start() called twice?"); 163 ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector); 164 assert(cmst() == th, "Where did the just-created CMS thread go?"); 165 return th; 166 } 167 return NULL; 168 } 169 170 void ConcurrentMarkSweepThread::stop() { 171 // it is ok to take late safepoints here, if needed 172 { 173 MutexLockerEx x(Terminator_lock); 174 _should_terminate = true; 175 } 176 { // Now post a notify on CGC_lock so as to nudge 177 // CMS thread(s) that might be slumbering in 178 // sleepBeforeNextCycle. 179 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag); 180 CGC_lock->notify_all(); 181 } 182 { // Now wait until (all) CMS thread(s) have exited 183 MutexLockerEx x(Terminator_lock); 184 while(cmst() != NULL) { 185 Terminator_lock->wait(); 186 } 187 } 188 } 189 190 void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) { 191 assert(tc != NULL, "Null ThreadClosure"); 192 if (_cmst != NULL) { 193 tc->do_thread(_cmst); 194 } 195 assert(Universe::is_fully_initialized(), 196 "Called too early, make sure heap is fully initialized"); 197 if (_collector != NULL) { 198 AbstractWorkGang* gang = _collector->conc_workers(); 199 if (gang != NULL) { 200 gang->threads_do(tc); 201 } 202 } 203 } 204 205 void ConcurrentMarkSweepThread::print_all_on(outputStream* st) { 206 if (_cmst != NULL) { 207 _cmst->print_on(st); 208 st->cr(); 209 } 210 if (_collector != NULL) { 211 AbstractWorkGang* gang = _collector->conc_workers(); 212 if (gang != NULL) { 213 gang->print_worker_threads_on(st); 214 } 215 } 216 } 217 218 void ConcurrentMarkSweepThread::synchronize(bool is_cms_thread) { 219 assert(UseConcMarkSweepGC, "just checking"); 220 221 MutexLockerEx x(CGC_lock, 222 Mutex::_no_safepoint_check_flag); 223 if (!is_cms_thread) { 224 assert(Thread::current()->is_VM_thread(), "Not a VM thread"); 225 CMSSynchronousYieldRequest yr; 226 while (CMS_flag_is_set(CMS_cms_has_token)) { 227 // indicate that we want to get the token 228 set_CMS_flag(CMS_vm_wants_token); 229 CGC_lock->wait(true); 230 } 231 // claim the token and proceed 232 clear_CMS_flag(CMS_vm_wants_token); 233 set_CMS_flag(CMS_vm_has_token); 234 } else { 235 assert(Thread::current()->is_ConcurrentGC_thread(), 236 "Not a CMS thread"); 237 // The following barrier assumes there's only one CMS thread. 238 // This will need to be modified is there are more CMS threads than one. 239 while (CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token)) { 240 set_CMS_flag(CMS_cms_wants_token); 241 CGC_lock->wait(true); 242 } 243 // claim the token 244 clear_CMS_flag(CMS_cms_wants_token); 245 set_CMS_flag(CMS_cms_has_token); 246 } 247 } 248 249 void ConcurrentMarkSweepThread::desynchronize(bool is_cms_thread) { 250 assert(UseConcMarkSweepGC, "just checking"); 251 252 MutexLockerEx x(CGC_lock, 253 Mutex::_no_safepoint_check_flag); 254 if (!is_cms_thread) { 255 assert(Thread::current()->is_VM_thread(), "Not a VM thread"); 256 assert(CMS_flag_is_set(CMS_vm_has_token), "just checking"); 257 clear_CMS_flag(CMS_vm_has_token); 258 if (CMS_flag_is_set(CMS_cms_wants_token)) { 259 // wake-up a waiting CMS thread 260 CGC_lock->notify(); 261 } 262 assert(!CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token), 263 "Should have been cleared"); 264 } else { 265 assert(Thread::current()->is_ConcurrentGC_thread(), 266 "Not a CMS thread"); 267 assert(CMS_flag_is_set(CMS_cms_has_token), "just checking"); 268 clear_CMS_flag(CMS_cms_has_token); 269 if (CMS_flag_is_set(CMS_vm_wants_token)) { 270 // wake-up a waiting VM thread 271 CGC_lock->notify(); 272 } 273 assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token), 274 "Should have been cleared"); 275 } 276 } 277 278 // Wait until any cms_lock event 279 void ConcurrentMarkSweepThread::wait_on_cms_lock(long t_millis) { 280 MutexLockerEx x(CGC_lock, 281 Mutex::_no_safepoint_check_flag); 282 if (_should_terminate || _collector->_full_gc_requested) { 283 return; 284 } 285 set_CMS_flag(CMS_cms_wants_token); // to provoke notifies 286 CGC_lock->wait(Mutex::_no_safepoint_check_flag, t_millis); 287 clear_CMS_flag(CMS_cms_wants_token); 288 assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token), 289 "Should not be set"); 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_for_scavenge(long t_millis) { 295 // Wait time in millis or 0 value representing infinite wait for a scavenge 296 assert(t_millis >= 0, "Wait time for scavenge should be 0 or positive"); 297 298 GenCollectedHeap* gch = GenCollectedHeap::heap(); 299 double start_time_secs = os::elapsedTime(); 300 double end_time_secs = start_time_secs + (t_millis / ((double) MILLIUNITS)); 301 302 // Total collections count before waiting loop 303 unsigned int before_count; 304 { 305 MutexLockerEx hl(Heap_lock, Mutex::_no_safepoint_check_flag); 306 before_count = gch->total_collections(); 307 } 308 309 unsigned int loop_count = 0; 310 311 while(!_should_terminate) { 312 double now_time = os::elapsedTime(); 313 long wait_time_millis; 314 315 if(t_millis != 0) { 316 // New wait limit 317 wait_time_millis = (long) ((end_time_secs - now_time) * MILLIUNITS); 318 if(wait_time_millis <= 0) { 319 // Wait time is over 320 break; 321 } 322 } else { 323 // No wait limit, wait if necessary forever 324 wait_time_millis = 0; 325 } 326 327 // Wait until the next event or the remaining timeout 328 { 329 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag); 330 331 if (_should_terminate || _collector->_full_gc_requested) { 332 return; 333 } 334 set_CMS_flag(CMS_cms_wants_token); // to provoke notifies 335 assert(t_millis == 0 || wait_time_millis > 0, "Sanity"); 336 CGC_lock->wait(Mutex::_no_safepoint_check_flag, wait_time_millis); 337 clear_CMS_flag(CMS_cms_wants_token); 338 assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token), 339 "Should not be set"); 340 } 341 342 // Extra wait time check before entering the heap lock to get the collection count 343 if(t_millis != 0 && os::elapsedTime() >= end_time_secs) { 344 // Wait time is over 345 break; 346 } 347 348 // Total collections count after the event 349 unsigned int after_count; 350 { 351 MutexLockerEx hl(Heap_lock, Mutex::_no_safepoint_check_flag); 352 after_count = gch->total_collections(); 353 } 354 355 if(before_count != after_count) { 356 // There was a collection - success 357 break; 358 } 359 360 // Too many loops warning 361 if(++loop_count == 0) { 362 warning("wait_on_cms_lock_for_scavenge() has looped %u times", loop_count - 1); 363 } 364 } 365 } 366 367 void ConcurrentMarkSweepThread::sleepBeforeNextCycle() { 368 while (!_should_terminate) { 369 if(CMSWaitDuration >= 0) { 370 // Wait until the next synchronous GC, a concurrent full gc 371 // request or a timeout, whichever is earlier. 372 wait_on_cms_lock_for_scavenge(CMSWaitDuration); 373 } else { 374 // Wait until any cms_lock event or check interval not to call shouldConcurrentCollect permanently 375 wait_on_cms_lock(CMSCheckInterval); 376 } 377 // Check if we should start a CMS collection cycle 378 if (_collector->shouldConcurrentCollect()) { 379 return; 380 } 381 // .. collection criterion not yet met, let's go back 382 // and wait some more 383 } 384 } 385 386 // Note: this method, although exported by the ConcurrentMarkSweepThread, 387 // which is a non-JavaThread, can only be called by a JavaThread. 388 // Currently this is done at vm creation time (post-vm-init) by the 389 // main/Primordial (Java)Thread. 390 // XXX Consider changing this in the future to allow the CMS thread 391 // itself to create this thread? 392 void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) { 393 assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC"); 394 assert(_slt == NULL, "SLT already created"); 395 _slt = SurrogateLockerThread::make(THREAD); 396 }