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

Fri, 09 May 2008 08:55:13 -0700

author
dcubed
date
Fri, 09 May 2008 08:55:13 -0700
changeset 587
c70a245cad3a
parent 435
a61af66fc99e
permissions
-rw-r--r--

6670684: 4/5 SA command universe did not print out CMS space information
Summary: Forward port of Yumin's fix for 6670684 from HSX-11; Yumin verified the port was correct.
Reviewed-by: dcubed

duke@435 1 /*
duke@435 2 * Copyright 2001-2005 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // CopyrightVersion 1.2
duke@435 26
duke@435 27 # include "incls/_precompiled.incl"
duke@435 28 # include "incls/_concurrentGCThread.cpp.incl"
duke@435 29
duke@435 30 bool ConcurrentGCThread::_should_terminate = false;
duke@435 31 bool ConcurrentGCThread::_has_terminated = false;
duke@435 32 int ConcurrentGCThread::_CGC_flag = CGC_nil;
duke@435 33
duke@435 34 SuspendibleThreadSet ConcurrentGCThread::_sts;
duke@435 35
duke@435 36 ConcurrentGCThread::ConcurrentGCThread() {
duke@435 37 _sts.initialize();
duke@435 38 };
duke@435 39
duke@435 40 void ConcurrentGCThread::stopWorldAndDo(VoidClosure* op) {
duke@435 41 MutexLockerEx x(Heap_lock,
duke@435 42 Mutex::_no_safepoint_check_flag);
duke@435 43 // warning("CGC: about to try stopping world");
duke@435 44 SafepointSynchronize::begin();
duke@435 45 // warning("CGC: successfully stopped world");
duke@435 46 op->do_void();
duke@435 47 SafepointSynchronize::end();
duke@435 48 // warning("CGC: successfully restarted world");
duke@435 49 }
duke@435 50
duke@435 51 void ConcurrentGCThread::safepoint_synchronize() {
duke@435 52 _sts.suspend_all();
duke@435 53 }
duke@435 54
duke@435 55 void ConcurrentGCThread::safepoint_desynchronize() {
duke@435 56 _sts.resume_all();
duke@435 57 }
duke@435 58
duke@435 59 void ConcurrentGCThread::create_and_start() {
duke@435 60 if (os::create_thread(this, os::cgc_thread)) {
duke@435 61 // XXX: need to set this to low priority
duke@435 62 // unless "agressive mode" set; priority
duke@435 63 // should be just less than that of VMThread.
duke@435 64 os::set_priority(this, NearMaxPriority);
duke@435 65 if (!_should_terminate && !DisableStartThread) {
duke@435 66 os::start_thread(this);
duke@435 67 }
duke@435 68 }
duke@435 69 }
duke@435 70
duke@435 71 void ConcurrentGCThread::initialize_in_thread() {
duke@435 72 this->record_stack_base_and_size();
duke@435 73 this->initialize_thread_local_storage();
duke@435 74 this->set_active_handles(JNIHandleBlock::allocate_block());
duke@435 75 // From this time Thread::current() should be working.
duke@435 76 assert(this == Thread::current(), "just checking");
duke@435 77 }
duke@435 78
duke@435 79 void ConcurrentGCThread::wait_for_universe_init() {
duke@435 80 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
duke@435 81 while (!is_init_completed() && !_should_terminate) {
duke@435 82 CGC_lock->wait(Mutex::_no_safepoint_check_flag, 200);
duke@435 83 }
duke@435 84 }
duke@435 85
duke@435 86 void ConcurrentGCThread::terminate() {
duke@435 87 // Signal that it is terminated
duke@435 88 {
duke@435 89 MutexLockerEx mu(Terminator_lock,
duke@435 90 Mutex::_no_safepoint_check_flag);
duke@435 91 _has_terminated = true;
duke@435 92 Terminator_lock->notify();
duke@435 93 }
duke@435 94
duke@435 95 // Thread destructor usually does this..
duke@435 96 ThreadLocalStorage::set_thread(NULL);
duke@435 97 }
duke@435 98
duke@435 99
duke@435 100 void SuspendibleThreadSet::initialize_work() {
duke@435 101 MutexLocker x(STS_init_lock);
duke@435 102 if (!_initialized) {
duke@435 103 _m = new Monitor(Mutex::leaf,
duke@435 104 "SuspendibleThreadSetLock", true);
duke@435 105 _async = 0;
duke@435 106 _async_stop = false;
duke@435 107 _async_stopped = 0;
duke@435 108 _initialized = true;
duke@435 109 }
duke@435 110 }
duke@435 111
duke@435 112 void SuspendibleThreadSet::join() {
duke@435 113 initialize();
duke@435 114 MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
duke@435 115 while (_async_stop) _m->wait(Mutex::_no_safepoint_check_flag);
duke@435 116 _async++;
duke@435 117 assert(_async > 0, "Huh.");
duke@435 118 }
duke@435 119
duke@435 120 void SuspendibleThreadSet::leave() {
duke@435 121 assert(_initialized, "Must be initialized.");
duke@435 122 MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
duke@435 123 _async--;
duke@435 124 assert(_async >= 0, "Huh.");
duke@435 125 if (_async_stop) _m->notify_all();
duke@435 126 }
duke@435 127
duke@435 128 void SuspendibleThreadSet::yield(const char* id) {
duke@435 129 assert(_initialized, "Must be initialized.");
duke@435 130 if (_async_stop) {
duke@435 131 MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
duke@435 132 if (_async_stop) {
duke@435 133 _async_stopped++;
duke@435 134 assert(_async_stopped > 0, "Huh.");
duke@435 135 if (_async_stopped == _async) {
duke@435 136 if (ConcGCYieldTimeout > 0) {
duke@435 137 double now = os::elapsedTime();
duke@435 138 guarantee((now - _suspend_all_start) * 1000.0 <
duke@435 139 (double)ConcGCYieldTimeout,
duke@435 140 "Long delay; whodunit?");
duke@435 141 }
duke@435 142 }
duke@435 143 _m->notify_all();
duke@435 144 while (_async_stop) _m->wait(Mutex::_no_safepoint_check_flag);
duke@435 145 _async_stopped--;
duke@435 146 assert(_async >= 0, "Huh");
duke@435 147 _m->notify_all();
duke@435 148 }
duke@435 149 }
duke@435 150 }
duke@435 151
duke@435 152 void SuspendibleThreadSet::suspend_all() {
duke@435 153 initialize(); // If necessary.
duke@435 154 if (ConcGCYieldTimeout > 0) {
duke@435 155 _suspend_all_start = os::elapsedTime();
duke@435 156 }
duke@435 157 MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
duke@435 158 assert(!_async_stop, "Only one at a time.");
duke@435 159 _async_stop = true;
duke@435 160 while (_async_stopped < _async) _m->wait(Mutex::_no_safepoint_check_flag);
duke@435 161 }
duke@435 162
duke@435 163 void SuspendibleThreadSet::resume_all() {
duke@435 164 assert(_initialized, "Must be initialized.");
duke@435 165 MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
duke@435 166 assert(_async_stopped == _async, "Huh.");
duke@435 167 _async_stop = false;
duke@435 168 _m->notify_all();
duke@435 169 }
duke@435 170
duke@435 171 static void _sltLoop(JavaThread* thread, TRAPS) {
duke@435 172 SurrogateLockerThread* slt = (SurrogateLockerThread*)thread;
duke@435 173 slt->loop();
duke@435 174 }
duke@435 175
duke@435 176 SurrogateLockerThread::SurrogateLockerThread() :
duke@435 177 JavaThread(&_sltLoop),
duke@435 178 _monitor(Mutex::nonleaf, "SLTMonitor"),
duke@435 179 _buffer(empty)
duke@435 180 {}
duke@435 181
duke@435 182 SurrogateLockerThread* SurrogateLockerThread::make(TRAPS) {
duke@435 183 klassOop k =
duke@435 184 SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_Thread(),
duke@435 185 true, CHECK_NULL);
duke@435 186 instanceKlassHandle klass (THREAD, k);
duke@435 187 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_NULL);
duke@435 188
duke@435 189 const char thread_name[] = "Surrogate Locker Thread (CMS)";
duke@435 190 Handle string = java_lang_String::create_from_str(thread_name, CHECK_NULL);
duke@435 191
duke@435 192 // Initialize thread_oop to put it into the system threadGroup
duke@435 193 Handle thread_group (THREAD, Universe::system_thread_group());
duke@435 194 JavaValue result(T_VOID);
duke@435 195 JavaCalls::call_special(&result, thread_oop,
duke@435 196 klass,
duke@435 197 vmSymbolHandles::object_initializer_name(),
duke@435 198 vmSymbolHandles::threadgroup_string_void_signature(),
duke@435 199 thread_group,
duke@435 200 string,
duke@435 201 CHECK_NULL);
duke@435 202
duke@435 203 SurrogateLockerThread* res;
duke@435 204 {
duke@435 205 MutexLocker mu(Threads_lock);
duke@435 206 res = new SurrogateLockerThread();
duke@435 207
duke@435 208 // At this point it may be possible that no osthread was created for the
duke@435 209 // JavaThread due to lack of memory. We would have to throw an exception
duke@435 210 // in that case. However, since this must work and we do not allow
duke@435 211 // exceptions anyway, check and abort if this fails.
duke@435 212 if (res == NULL || res->osthread() == NULL) {
duke@435 213 vm_exit_during_initialization("java.lang.OutOfMemoryError",
duke@435 214 "unable to create new native thread");
duke@435 215 }
duke@435 216 java_lang_Thread::set_thread(thread_oop(), res);
duke@435 217 java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
duke@435 218 java_lang_Thread::set_daemon(thread_oop());
duke@435 219
duke@435 220 res->set_threadObj(thread_oop());
duke@435 221 Threads::add(res);
duke@435 222 Thread::start(res);
duke@435 223 }
duke@435 224 os::yield(); // This seems to help with initial start-up of SLT
duke@435 225 return res;
duke@435 226 }
duke@435 227
duke@435 228 void SurrogateLockerThread::manipulatePLL(SLT_msg_type msg) {
duke@435 229 MutexLockerEx x(&_monitor, Mutex::_no_safepoint_check_flag);
duke@435 230 assert(_buffer == empty, "Should be empty");
duke@435 231 assert(msg != empty, "empty message");
duke@435 232 _buffer = msg;
duke@435 233 while (_buffer != empty) {
duke@435 234 _monitor.notify();
duke@435 235 _monitor.wait(Mutex::_no_safepoint_check_flag);
duke@435 236 }
duke@435 237 }
duke@435 238
duke@435 239 // ======= Surrogate Locker Thread =============
duke@435 240
duke@435 241 void SurrogateLockerThread::loop() {
duke@435 242 BasicLock pll_basic_lock;
duke@435 243 SLT_msg_type msg;
duke@435 244 debug_only(unsigned int owned = 0;)
duke@435 245
duke@435 246 while (/* !isTerminated() */ 1) {
duke@435 247 {
duke@435 248 MutexLocker x(&_monitor);
duke@435 249 // Since we are a JavaThread, we can't be here at a safepoint.
duke@435 250 assert(!SafepointSynchronize::is_at_safepoint(),
duke@435 251 "SLT is a JavaThread");
duke@435 252 // wait for msg buffer to become non-empty
duke@435 253 while (_buffer == empty) {
duke@435 254 _monitor.notify();
duke@435 255 _monitor.wait();
duke@435 256 }
duke@435 257 msg = _buffer;
duke@435 258 }
duke@435 259 switch(msg) {
duke@435 260 case acquirePLL: {
duke@435 261 instanceRefKlass::acquire_pending_list_lock(&pll_basic_lock);
duke@435 262 debug_only(owned++;)
duke@435 263 break;
duke@435 264 }
duke@435 265 case releaseAndNotifyPLL: {
duke@435 266 assert(owned > 0, "Don't have PLL");
duke@435 267 instanceRefKlass::release_and_notify_pending_list_lock(&pll_basic_lock);
duke@435 268 debug_only(owned--;)
duke@435 269 break;
duke@435 270 }
duke@435 271 case empty:
duke@435 272 default: {
duke@435 273 guarantee(false,"Unexpected message in _buffer");
duke@435 274 break;
duke@435 275 }
duke@435 276 }
duke@435 277 {
duke@435 278 MutexLocker x(&_monitor);
duke@435 279 // Since we are a JavaThread, we can't be here at a safepoint.
duke@435 280 assert(!SafepointSynchronize::is_at_safepoint(),
duke@435 281 "SLT is a JavaThread");
duke@435 282 _buffer = empty;
duke@435 283 _monitor.notify();
duke@435 284 }
duke@435 285 }
duke@435 286 assert(!_monitor.owned_by_self(), "Should unlock before exit.");
duke@435 287 }
duke@435 288
duke@435 289
duke@435 290 // ===== STS Access From Outside CGCT =====
duke@435 291
duke@435 292 void ConcurrentGCThread::stsYield(const char* id) {
duke@435 293 assert( Thread::current()->is_ConcurrentGC_thread(),
duke@435 294 "only a conc GC thread can call this" );
duke@435 295 _sts.yield(id);
duke@435 296 }
duke@435 297
duke@435 298 bool ConcurrentGCThread::stsShouldYield() {
duke@435 299 assert( Thread::current()->is_ConcurrentGC_thread(),
duke@435 300 "only a conc GC thread can call this" );
duke@435 301 return _sts.should_yield();
duke@435 302 }
duke@435 303
duke@435 304 void ConcurrentGCThread::stsJoin() {
duke@435 305 assert( Thread::current()->is_ConcurrentGC_thread(),
duke@435 306 "only a conc GC thread can call this" );
duke@435 307 _sts.join();
duke@435 308 }
duke@435 309
duke@435 310 void ConcurrentGCThread::stsLeave() {
duke@435 311 assert( Thread::current()->is_ConcurrentGC_thread(),
duke@435 312 "only a conc GC thread can call this" );
duke@435 313 _sts.leave();
duke@435 314 }

mercurial