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

Thu, 26 Jan 2012 20:06:06 -0500

author
phh
date
Thu, 26 Jan 2012 20:06:06 -0500
changeset 3481
de268c8a8075
parent 2647
a181f3a124dd
child 4077
a7509aff1b06
permissions
-rw-r--r--

7082553: Interpret Thread.setPriority(Thread.MAX_PRIORITY) to mean FX60 on Solaris 10 and 11
Summary: Add CriticalPriority == MaxPriority+1 and enable scheduling class as well as thread priority to change on Solaris.
Reviewed-by: dholmes, dcubed

duke@435 1 /*
phh@3481 2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/systemDictionary.hpp"
stefank@2314 27 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp"
stefank@2314 28 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
stefank@2314 29 #include "memory/genCollectedHeap.hpp"
stefank@2314 30 #include "oops/instanceRefKlass.hpp"
stefank@2314 31 #include "oops/oop.inline.hpp"
stefank@2314 32 #include "runtime/init.hpp"
stefank@2314 33 #include "runtime/interfaceSupport.hpp"
stefank@2314 34 #include "runtime/java.hpp"
stefank@2314 35 #include "runtime/javaCalls.hpp"
stefank@2314 36 #include "runtime/mutexLocker.hpp"
stefank@2314 37 #include "runtime/os.hpp"
stefank@2314 38 #include "runtime/vmThread.hpp"
duke@435 39
duke@435 40 // ======= Concurrent Mark Sweep Thread ========
duke@435 41
duke@435 42 // The CMS thread is created when Concurrent Mark Sweep is used in the
duke@435 43 // older of two generations in a generational memory system.
duke@435 44
duke@435 45 ConcurrentMarkSweepThread*
duke@435 46 ConcurrentMarkSweepThread::_cmst = NULL;
duke@435 47 CMSCollector* ConcurrentMarkSweepThread::_collector = NULL;
duke@435 48 bool ConcurrentMarkSweepThread::_should_terminate = false;
duke@435 49 int ConcurrentMarkSweepThread::_CMS_flag = CMS_nil;
duke@435 50
duke@435 51 volatile jint ConcurrentMarkSweepThread::_pending_yields = 0;
duke@435 52 volatile jint ConcurrentMarkSweepThread::_pending_decrements = 0;
duke@435 53
ysr@2647 54 volatile jint ConcurrentMarkSweepThread::_icms_disabled = 0;
duke@435 55 volatile bool ConcurrentMarkSweepThread::_should_run = false;
duke@435 56 // When icms is enabled, the icms thread is stopped until explicitly
duke@435 57 // started.
duke@435 58 volatile bool ConcurrentMarkSweepThread::_should_stop = true;
duke@435 59
duke@435 60 SurrogateLockerThread*
duke@435 61 ConcurrentMarkSweepThread::_slt = NULL;
duke@435 62 SurrogateLockerThread::SLT_msg_type
duke@435 63 ConcurrentMarkSweepThread::_sltBuffer = SurrogateLockerThread::empty;
duke@435 64 Monitor*
duke@435 65 ConcurrentMarkSweepThread::_sltMonitor = NULL;
duke@435 66
duke@435 67 ConcurrentMarkSweepThread::ConcurrentMarkSweepThread(CMSCollector* collector)
duke@435 68 : ConcurrentGCThread() {
duke@435 69 assert(UseConcMarkSweepGC, "UseConcMarkSweepGC should be set");
duke@435 70 assert(_cmst == NULL, "CMS thread already created");
duke@435 71 _cmst = this;
duke@435 72 assert(_collector == NULL, "Collector already set");
duke@435 73 _collector = collector;
duke@435 74
duke@435 75 set_name("Concurrent Mark-Sweep GC Thread");
duke@435 76
duke@435 77 if (os::create_thread(this, os::cgc_thread)) {
phh@3481 78 // An old comment here said: "Priority should be just less
phh@3481 79 // than that of VMThread". Since the VMThread runs at
phh@3481 80 // NearMaxPriority, the old comment was inaccurate, but
phh@3481 81 // changing the default priority to NearMaxPriority-1
phh@3481 82 // could change current behavior, so the default of
phh@3481 83 // NearMaxPriority stays in place.
phh@3481 84 //
phh@3481 85 // Note that there's a possibility of the VMThread
phh@3481 86 // starving if UseCriticalCMSThreadPriority is on.
phh@3481 87 // That won't happen on Solaris for various reasons,
phh@3481 88 // but may well happen on non-Solaris platforms.
phh@3481 89 int native_prio;
phh@3481 90 if (UseCriticalCMSThreadPriority) {
phh@3481 91 native_prio = os::java_to_os_priority[CriticalPriority];
phh@3481 92 } else {
phh@3481 93 native_prio = os::java_to_os_priority[NearMaxPriority];
phh@3481 94 }
phh@3481 95 os::set_native_priority(this, native_prio);
phh@3481 96
duke@435 97 if (!DisableStartThread) {
duke@435 98 os::start_thread(this);
duke@435 99 }
duke@435 100 }
duke@435 101 _sltMonitor = SLT_lock;
ysr@2647 102 assert(!CMSIncrementalMode || icms_is_enabled(), "Error");
duke@435 103 }
duke@435 104
duke@435 105 void ConcurrentMarkSweepThread::run() {
duke@435 106 assert(this == cmst(), "just checking");
duke@435 107
duke@435 108 this->record_stack_base_and_size();
duke@435 109 this->initialize_thread_local_storage();
duke@435 110 this->set_active_handles(JNIHandleBlock::allocate_block());
duke@435 111 // From this time Thread::current() should be working.
duke@435 112 assert(this == Thread::current(), "just checking");
duke@435 113 if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) {
duke@435 114 warning("Couldn't bind CMS thread to processor %u", CPUForCMSThread);
duke@435 115 }
duke@435 116 // Wait until Universe::is_fully_initialized()
duke@435 117 {
duke@435 118 CMSLoopCountWarn loopX("CMS::run", "waiting for "
duke@435 119 "Universe::is_fully_initialized()", 2);
duke@435 120 MutexLockerEx x(CGC_lock, true);
duke@435 121 set_CMS_flag(CMS_cms_wants_token);
duke@435 122 // Wait until Universe is initialized and all initialization is completed.
duke@435 123 while (!is_init_completed() && !Universe::is_fully_initialized() &&
duke@435 124 !_should_terminate) {
duke@435 125 CGC_lock->wait(true, 200);
duke@435 126 loopX.tick();
duke@435 127 }
duke@435 128 // Wait until the surrogate locker thread that will do
duke@435 129 // pending list locking on our behalf has been created.
duke@435 130 // We cannot start the SLT thread ourselves since we need
duke@435 131 // to be a JavaThread to do so.
duke@435 132 CMSLoopCountWarn loopY("CMS::run", "waiting for SLT installation", 2);
duke@435 133 while (_slt == NULL && !_should_terminate) {
duke@435 134 CGC_lock->wait(true, 200);
duke@435 135 loopY.tick();
duke@435 136 }
duke@435 137 clear_CMS_flag(CMS_cms_wants_token);
duke@435 138 }
duke@435 139
duke@435 140 while (!_should_terminate) {
duke@435 141 sleepBeforeNextCycle();
duke@435 142 if (_should_terminate) break;
duke@435 143 _collector->collect_in_background(false); // !clear_all_soft_refs
duke@435 144 }
duke@435 145 assert(_should_terminate, "just checking");
duke@435 146 // Check that the state of any protocol for synchronization
duke@435 147 // between background (CMS) and foreground collector is "clean"
duke@435 148 // (i.e. will not potentially block the foreground collector,
duke@435 149 // requiring action by us).
duke@435 150 verify_ok_to_terminate();
duke@435 151 // Signal that it is terminated
duke@435 152 {
duke@435 153 MutexLockerEx mu(Terminator_lock,
duke@435 154 Mutex::_no_safepoint_check_flag);
duke@435 155 assert(_cmst == this, "Weird!");
duke@435 156 _cmst = NULL;
duke@435 157 Terminator_lock->notify();
duke@435 158 }
duke@435 159
duke@435 160 // Thread destructor usually does this..
duke@435 161 ThreadLocalStorage::set_thread(NULL);
duke@435 162 }
duke@435 163
duke@435 164 #ifndef PRODUCT
duke@435 165 void ConcurrentMarkSweepThread::verify_ok_to_terminate() const {
duke@435 166 assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() ||
duke@435 167 cms_thread_wants_cms_token()),
duke@435 168 "Must renounce all worldly possessions and desires for nirvana");
duke@435 169 _collector->verify_ok_to_terminate();
duke@435 170 }
duke@435 171 #endif
duke@435 172
duke@435 173 // create and start a new ConcurrentMarkSweep Thread for given CMS generation
duke@435 174 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) {
duke@435 175 if (!_should_terminate) {
duke@435 176 assert(cmst() == NULL, "start() called twice?");
duke@435 177 ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector);
duke@435 178 assert(cmst() == th, "Where did the just-created CMS thread go?");
duke@435 179 return th;
duke@435 180 }
duke@435 181 return NULL;
duke@435 182 }
duke@435 183
duke@435 184 void ConcurrentMarkSweepThread::stop() {
duke@435 185 if (CMSIncrementalMode) {
duke@435 186 // Disable incremental mode and wake up the thread so it notices the change.
duke@435 187 disable_icms();
duke@435 188 start_icms();
duke@435 189 }
duke@435 190 // it is ok to take late safepoints here, if needed
duke@435 191 {
duke@435 192 MutexLockerEx x(Terminator_lock);
duke@435 193 _should_terminate = true;
duke@435 194 }
duke@435 195 { // Now post a notify on CGC_lock so as to nudge
duke@435 196 // CMS thread(s) that might be slumbering in
duke@435 197 // sleepBeforeNextCycle.
duke@435 198 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
duke@435 199 CGC_lock->notify_all();
duke@435 200 }
duke@435 201 { // Now wait until (all) CMS thread(s) have exited
duke@435 202 MutexLockerEx x(Terminator_lock);
duke@435 203 while(cmst() != NULL) {
duke@435 204 Terminator_lock->wait();
duke@435 205 }
duke@435 206 }
duke@435 207 }
duke@435 208
duke@435 209 void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) {
duke@435 210 assert(tc != NULL, "Null ThreadClosure");
duke@435 211 if (_cmst != NULL) {
duke@435 212 tc->do_thread(_cmst);
duke@435 213 }
duke@435 214 assert(Universe::is_fully_initialized(),
duke@435 215 "Called too early, make sure heap is fully initialized");
duke@435 216 if (_collector != NULL) {
duke@435 217 AbstractWorkGang* gang = _collector->conc_workers();
duke@435 218 if (gang != NULL) {
duke@435 219 gang->threads_do(tc);
duke@435 220 }
duke@435 221 }
duke@435 222 }
duke@435 223
duke@435 224 void ConcurrentMarkSweepThread::print_on(outputStream* st) const {
duke@435 225 st->print("\"%s\" ", name());
duke@435 226 Thread::print_on(st);
duke@435 227 st->cr();
duke@435 228 }
duke@435 229
duke@435 230 void ConcurrentMarkSweepThread::print_all_on(outputStream* st) {
duke@435 231 if (_cmst != NULL) {
duke@435 232 _cmst->print_on(st);
duke@435 233 }
duke@435 234 if (_collector != NULL) {
duke@435 235 AbstractWorkGang* gang = _collector->conc_workers();
duke@435 236 if (gang != NULL) {
duke@435 237 gang->print_worker_threads_on(st);
duke@435 238 }
duke@435 239 }
duke@435 240 }
duke@435 241
duke@435 242 void ConcurrentMarkSweepThread::synchronize(bool is_cms_thread) {
duke@435 243 assert(UseConcMarkSweepGC, "just checking");
duke@435 244
duke@435 245 MutexLockerEx x(CGC_lock,
duke@435 246 Mutex::_no_safepoint_check_flag);
duke@435 247 if (!is_cms_thread) {
duke@435 248 assert(Thread::current()->is_VM_thread(), "Not a VM thread");
duke@435 249 CMSSynchronousYieldRequest yr;
duke@435 250 while (CMS_flag_is_set(CMS_cms_has_token)) {
duke@435 251 // indicate that we want to get the token
duke@435 252 set_CMS_flag(CMS_vm_wants_token);
duke@435 253 CGC_lock->wait(true);
duke@435 254 }
duke@435 255 // claim the token and proceed
duke@435 256 clear_CMS_flag(CMS_vm_wants_token);
duke@435 257 set_CMS_flag(CMS_vm_has_token);
duke@435 258 } else {
duke@435 259 assert(Thread::current()->is_ConcurrentGC_thread(),
duke@435 260 "Not a CMS thread");
duke@435 261 // The following barrier assumes there's only one CMS thread.
duke@435 262 // This will need to be modified is there are more CMS threads than one.
duke@435 263 while (CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token)) {
duke@435 264 set_CMS_flag(CMS_cms_wants_token);
duke@435 265 CGC_lock->wait(true);
duke@435 266 }
duke@435 267 // claim the token
duke@435 268 clear_CMS_flag(CMS_cms_wants_token);
duke@435 269 set_CMS_flag(CMS_cms_has_token);
duke@435 270 }
duke@435 271 }
duke@435 272
duke@435 273 void ConcurrentMarkSweepThread::desynchronize(bool is_cms_thread) {
duke@435 274 assert(UseConcMarkSweepGC, "just checking");
duke@435 275
duke@435 276 MutexLockerEx x(CGC_lock,
duke@435 277 Mutex::_no_safepoint_check_flag);
duke@435 278 if (!is_cms_thread) {
duke@435 279 assert(Thread::current()->is_VM_thread(), "Not a VM thread");
duke@435 280 assert(CMS_flag_is_set(CMS_vm_has_token), "just checking");
duke@435 281 clear_CMS_flag(CMS_vm_has_token);
duke@435 282 if (CMS_flag_is_set(CMS_cms_wants_token)) {
duke@435 283 // wake-up a waiting CMS thread
duke@435 284 CGC_lock->notify();
duke@435 285 }
duke@435 286 assert(!CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token),
duke@435 287 "Should have been cleared");
duke@435 288 } else {
duke@435 289 assert(Thread::current()->is_ConcurrentGC_thread(),
duke@435 290 "Not a CMS thread");
duke@435 291 assert(CMS_flag_is_set(CMS_cms_has_token), "just checking");
duke@435 292 clear_CMS_flag(CMS_cms_has_token);
duke@435 293 if (CMS_flag_is_set(CMS_vm_wants_token)) {
duke@435 294 // wake-up a waiting VM thread
duke@435 295 CGC_lock->notify();
duke@435 296 }
duke@435 297 assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token),
duke@435 298 "Should have been cleared");
duke@435 299 }
duke@435 300 }
duke@435 301
ysr@2242 302 // Wait until the next synchronous GC, a concurrent full gc request,
ysr@2242 303 // or a timeout, whichever is earlier.
ysr@2242 304 void ConcurrentMarkSweepThread::wait_on_cms_lock(long t_millis) {
duke@435 305 MutexLockerEx x(CGC_lock,
duke@435 306 Mutex::_no_safepoint_check_flag);
ysr@2242 307 if (_should_terminate || _collector->_full_gc_requested) {
ysr@2242 308 return;
ysr@2242 309 }
duke@435 310 set_CMS_flag(CMS_cms_wants_token); // to provoke notifies
ysr@2242 311 CGC_lock->wait(Mutex::_no_safepoint_check_flag, t_millis);
duke@435 312 clear_CMS_flag(CMS_cms_wants_token);
duke@435 313 assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token),
duke@435 314 "Should not be set");
duke@435 315 }
duke@435 316
duke@435 317 void ConcurrentMarkSweepThread::sleepBeforeNextCycle() {
duke@435 318 while (!_should_terminate) {
duke@435 319 if (CMSIncrementalMode) {
duke@435 320 icms_wait();
duke@435 321 return;
duke@435 322 } else {
ysr@2242 323 // Wait until the next synchronous GC, a concurrent full gc
ysr@2242 324 // request or a timeout, whichever is earlier.
duke@435 325 wait_on_cms_lock(CMSWaitDuration);
duke@435 326 }
duke@435 327 // Check if we should start a CMS collection cycle
duke@435 328 if (_collector->shouldConcurrentCollect()) {
duke@435 329 return;
duke@435 330 }
duke@435 331 // .. collection criterion not yet met, let's go back
duke@435 332 // and wait some more
duke@435 333 }
duke@435 334 }
duke@435 335
duke@435 336 // Incremental CMS
duke@435 337 void ConcurrentMarkSweepThread::start_icms() {
duke@435 338 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
duke@435 339 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
duke@435 340 trace_state("start_icms");
duke@435 341 _should_run = true;
duke@435 342 iCMS_lock->notify_all();
duke@435 343 }
duke@435 344
duke@435 345 void ConcurrentMarkSweepThread::stop_icms() {
duke@435 346 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
duke@435 347 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
duke@435 348 if (!_should_stop) {
duke@435 349 trace_state("stop_icms");
duke@435 350 _should_stop = true;
duke@435 351 _should_run = false;
duke@435 352 asynchronous_yield_request();
duke@435 353 iCMS_lock->notify_all();
duke@435 354 }
duke@435 355 }
duke@435 356
duke@435 357 void ConcurrentMarkSweepThread::icms_wait() {
duke@435 358 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
ysr@2647 359 if (_should_stop && icms_is_enabled()) {
duke@435 360 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
duke@435 361 trace_state("pause_icms");
duke@435 362 _collector->stats().stop_cms_timer();
ysr@2647 363 while(!_should_run && icms_is_enabled()) {
duke@435 364 iCMS_lock->wait(Mutex::_no_safepoint_check_flag);
duke@435 365 }
duke@435 366 _collector->stats().start_cms_timer();
duke@435 367 _should_stop = false;
duke@435 368 trace_state("pause_icms end");
duke@435 369 }
duke@435 370 }
duke@435 371
duke@435 372 // Note: this method, although exported by the ConcurrentMarkSweepThread,
duke@435 373 // which is a non-JavaThread, can only be called by a JavaThread.
duke@435 374 // Currently this is done at vm creation time (post-vm-init) by the
duke@435 375 // main/Primordial (Java)Thread.
duke@435 376 // XXX Consider changing this in the future to allow the CMS thread
duke@435 377 // itself to create this thread?
duke@435 378 void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) {
duke@435 379 assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC");
duke@435 380 assert(_slt == NULL, "SLT already created");
duke@435 381 _slt = SurrogateLockerThread::make(THREAD);
duke@435 382 }

mercurial