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

Thu, 06 Jan 2011 23:50:02 -0800

author
ysr
date
Thu, 06 Jan 2011 23:50:02 -0800
changeset 2452
4947ee68d19c
parent 2314
f95d63e2154a
child 2647
a181f3a124dd
permissions
-rw-r--r--

7008136: CMS: assert((HeapWord*)nextChunk <= _limit) failed: sweep invariant
Summary: The recorded _sweep_limit may not necessarily remain a block boundary as the old generation expands during a concurrent cycle. Terminal actions inside the sweep closure need to be aware of this as they cross over the limit.
Reviewed-by: johnc, minqi

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

mercurial