1 /* 2 * Copyright (c) 1998, 2019, 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/vmSymbols.hpp" 27 #include "logging/log.hpp" 28 #include "logging/logStream.hpp" 29 #include "jfr/jfrEvents.hpp" 30 #include "memory/allocation.inline.hpp" 31 #include "memory/metaspaceShared.hpp" 32 #include "memory/padded.hpp" 33 #include "memory/resourceArea.hpp" 34 #include "memory/universe.hpp" 35 #include "oops/markWord.hpp" 36 #include "oops/oop.inline.hpp" 37 #include "runtime/atomic.hpp" 38 #include "runtime/biasedLocking.hpp" 39 #include "runtime/handles.inline.hpp" 40 #include "runtime/handshake.hpp" 41 #include "runtime/interfaceSupport.inline.hpp" 42 #include "runtime/mutexLocker.hpp" 43 #include "runtime/objectMonitor.hpp" 44 #include "runtime/objectMonitor.inline.hpp" 45 #include "runtime/osThread.hpp" 46 #include "runtime/safepointVerifiers.hpp" 47 #include "runtime/sharedRuntime.hpp" 48 #include "runtime/stubRoutines.hpp" 49 #include "runtime/synchronizer.hpp" 50 #include "runtime/thread.inline.hpp" 51 #include "runtime/timer.hpp" 52 #include "runtime/vframe.hpp" 53 #include "runtime/vmThread.hpp" 54 #include "utilities/align.hpp" 55 #include "utilities/dtrace.hpp" 56 #include "utilities/events.hpp" 57 #include "utilities/preserveException.hpp" 58 59 // The "core" versions of monitor enter and exit reside in this file. 60 // The interpreter and compilers contain specialized transliterated 61 // variants of the enter-exit fast-path operations. See i486.ad fast_lock(), 62 // for instance. If you make changes here, make sure to modify the 63 // interpreter, and both C1 and C2 fast-path inline locking code emission. 64 // 65 // ----------------------------------------------------------------------------- 66 67 #ifdef DTRACE_ENABLED 68 69 // Only bother with this argument setup if dtrace is available 70 // TODO-FIXME: probes should not fire when caller is _blocked. assert() accordingly. 71 72 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread) \ 73 char* bytes = NULL; \ 74 int len = 0; \ 75 jlong jtid = SharedRuntime::get_java_tid(thread); \ 76 Symbol* klassname = ((oop)(obj))->klass()->name(); \ 77 if (klassname != NULL) { \ 78 bytes = (char*)klassname->bytes(); \ 79 len = klassname->utf8_length(); \ 80 } 81 82 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis) \ 83 { \ 84 if (DTraceMonitorProbes) { \ 85 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ 86 HOTSPOT_MONITOR_WAIT(jtid, \ 87 (uintptr_t)(monitor), bytes, len, (millis)); \ 88 } \ 89 } 90 91 #define HOTSPOT_MONITOR_PROBE_notify HOTSPOT_MONITOR_NOTIFY 92 #define HOTSPOT_MONITOR_PROBE_notifyAll HOTSPOT_MONITOR_NOTIFYALL 93 #define HOTSPOT_MONITOR_PROBE_waited HOTSPOT_MONITOR_WAITED 94 95 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread) \ 96 { \ 97 if (DTraceMonitorProbes) { \ 98 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ 99 HOTSPOT_MONITOR_PROBE_##probe(jtid, /* probe = waited */ \ 100 (uintptr_t)(monitor), bytes, len); \ 101 } \ 102 } 103 104 #else // ndef DTRACE_ENABLED 105 106 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon) {;} 107 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon) {;} 108 109 #endif // ndef DTRACE_ENABLED 110 111 // This exists only as a workaround of dtrace bug 6254741 112 int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) { 113 DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr); 114 return 0; 115 } 116 117 #define NINFLATIONLOCKS 256 118 static volatile intptr_t gInflationLocks[NINFLATIONLOCKS]; 119 120 // global list of blocks of monitors 121 PaddedObjectMonitor* volatile ObjectSynchronizer::g_block_list = NULL; 122 bool volatile ObjectSynchronizer::_is_async_deflation_requested = false; 123 bool volatile ObjectSynchronizer::_is_special_deflation_requested = false; 124 jlong ObjectSynchronizer::_last_async_deflation_time_ns = 0; 125 126 // Global ObjectMonitor free list. Newly allocated and deflated 127 // ObjectMonitors are prepended here. 128 static ObjectMonitor* volatile g_free_list = NULL; 129 // Global ObjectMonitor in-use list. When a JavaThread is exiting, 130 // ObjectMonitors on its per-thread in-use list are prepended here. 131 static ObjectMonitor* volatile g_om_in_use_list = NULL; 132 // Global ObjectMonitor wait list. If HandshakeAfterDeflateIdleMonitors 133 // is true, deflated ObjectMonitors wait on this list until after a 134 // handshake or a safepoint for platforms that don't support handshakes. 135 // After the handshake or safepoint, the deflated ObjectMonitors are 136 // prepended to g_free_list. 137 static ObjectMonitor* volatile g_wait_list = NULL; 138 139 static volatile int g_om_free_count = 0; // # on g_free_list 140 static volatile int g_om_in_use_count = 0; // # on g_om_in_use_list 141 static volatile int g_om_population = 0; // # Extant -- in circulation 142 static volatile int g_om_wait_count = 0; // # on g_wait_list 143 144 #define CHAINMARKER (cast_to_oop<intptr_t>(-1)) 145 146 147 // =====================> List Management functions 148 149 // Return true if the ObjectMonitor's next field is marked. 150 // Otherwise returns false. 151 static bool is_next_marked(ObjectMonitor* om) { 152 // Use load_acquire() since _next_om fields are updated with a 153 // release_store(). 154 return ((intptr_t)OrderAccess::load_acquire(&om->_next_om) & 0x1) != 0; 155 } 156 157 // Mark an ObjectMonitor* and return it. Note: the om parameter 158 // may or may not have been marked originally. 159 static ObjectMonitor* mark_om_ptr(ObjectMonitor* om) { 160 return (ObjectMonitor*)((intptr_t)om | 0x1); 161 } 162 163 // Mark the next field in an ObjectMonitor. If marking was successful, 164 // then the unmarked next field is returned via parameter and true is 165 // returned. Otherwise false is returned. 166 static bool mark_next(ObjectMonitor* om, ObjectMonitor** next_p) { 167 // Get current next field without any marking value. 168 ObjectMonitor* next = (ObjectMonitor*)((intptr_t)om->_next_om & ~0x1); 169 if (Atomic::cmpxchg(mark_om_ptr(next), &om->_next_om, next) != next) { 170 return false; // Could not mark the next field or it was already marked. 171 } 172 *next_p = next; 173 return true; 174 } 175 176 // Loop until we mark the next field in an ObjectMonitor. The unmarked 177 // next field is returned. 178 static ObjectMonitor* mark_next_loop(ObjectMonitor* om) { 179 ObjectMonitor* next; 180 while (true) { 181 if (mark_next(om, &next)) { 182 // Marked om's next field so return the unmarked value. 183 return next; 184 } 185 } 186 } 187 188 // Set the next field in an ObjectMonitor to the specified value. 189 // The caller of set_next() must be the same thread that marked the 190 // ObjectMonitor. 191 static void set_next(ObjectMonitor* om, ObjectMonitor* value) { 192 OrderAccess::release_store(&om->_next_om, value); 193 } 194 195 // Mark the next field in the list head ObjectMonitor. If marking was 196 // successful, then the mid and the unmarked next field are returned 197 // via parameter and true is returned. Otherwise false is returned. 198 static bool mark_list_head(ObjectMonitor* volatile * list_p, 199 ObjectMonitor** mid_p, ObjectMonitor** next_p) { 200 while (true) { 201 ObjectMonitor* mid = *list_p; 202 if (mid == NULL) { 203 return false; // The list is empty so nothing to mark. 204 } 205 if (mark_next(mid, next_p)) { 206 if (*list_p != mid) { 207 // The list head changed so we have to retry. 208 set_next(mid, *next_p); // unmark mid 209 continue; 210 } 211 // We marked next field to guard against races. 212 *mid_p = mid; 213 return true; 214 } 215 } 216 } 217 218 // Return the unmarked next field in an ObjectMonitor. Note: the next 219 // field may or may not have been marked originally. 220 static ObjectMonitor* unmarked_next(ObjectMonitor* om) { 221 // Use load_acquire() since _next_om fields are updated with a 222 // release_store(). 223 return (ObjectMonitor*)((intptr_t)OrderAccess::load_acquire(&om->_next_om) & ~0x1); 224 } 225 226 // Mark the next ObjectMonitor for traversal. The current ObjectMonitor 227 // is unmarked after the next ObjectMonitor is marked. *cur_p and *next_p 228 // are updated to their next values in the list traversal. *cur_p is set 229 // to NULL when the end of the list is reached. 230 static void mark_next_for_traversal(ObjectMonitor** cur_p, ObjectMonitor** next_p) { 231 ObjectMonitor* prev = *cur_p; // Save current for unmarking. 232 if (*next_p == NULL) { // Reached the end of the list. 233 set_next(prev, NULL); // Unmark previous. 234 *cur_p = NULL; // Tell the caller we are done. 235 return; 236 } 237 (void)mark_next_loop(*next_p); // Mark next. 238 set_next(prev, *next_p); // Unmark previous. 239 *cur_p = *next_p; // Update current. 240 *next_p = unmarked_next(*cur_p); // Update next. 241 } 242 243 // Prepend a list of ObjectMonitors to the specified *list_p. 'tail' is 244 // the last ObjectMonitor in the list and there are 'count' on the list. 245 // Also updates the specified *count_p. 246 static void prepend_list_to_common(ObjectMonitor* list, ObjectMonitor* tail, 247 int count, ObjectMonitor* volatile* list_p, 248 volatile int* count_p) { 249 while (true) { 250 ObjectMonitor* cur = *list_p; 251 // Prepend list to *list_p. 252 ObjectMonitor* next = NULL; 253 if (!mark_next(tail, &next)) { 254 continue; // failed to mark next field so try it all again 255 } 256 set_next(tail, cur); // tail now points to cur (and unmarks tail) 257 if (cur == NULL) { 258 // No potential race with takers or other prependers since 259 // *list_p is empty. 260 if (Atomic::cmpxchg(list, list_p, cur) == cur) { 261 // Successfully switched *list_p to the list value. 262 Atomic::add(count, count_p); 263 break; 264 } 265 // Implied else: try it all again 266 } else { 267 // Try to mark next field to guard against races: 268 if (!mark_next(cur, &next)) { 269 continue; // failed to mark next field so try it all again 270 } 271 // We marked the next field so try to switch *list_p to the list value. 272 if (Atomic::cmpxchg(list, list_p, cur) != cur) { 273 // The list head has changed so unmark the next field and try again: 274 set_next(cur, next); 275 continue; 276 } 277 Atomic::add(count, count_p); 278 set_next(cur, next); // unmark next field 279 break; 280 } 281 } 282 } 283 284 // Prepend a newly allocated block of ObjectMonitors to g_block_list and 285 // g_free_list. Also updates g_om_population and g_om_free_count. 286 void ObjectSynchronizer::prepend_block_to_lists(PaddedObjectMonitor* new_blk) { 287 // First we handle g_block_list: 288 while (true) { 289 PaddedObjectMonitor* cur = g_block_list; 290 // Prepend new_blk to g_block_list. The first ObjectMonitor in 291 // a block is reserved for use as linkage to the next block. 292 new_blk[0]._next_om = cur; 293 if (Atomic::cmpxchg(new_blk, &g_block_list, cur) == cur) { 294 // Successfully switched g_block_list to the new_blk value. 295 Atomic::add(_BLOCKSIZE - 1, &g_om_population); 296 break; 297 } 298 // Implied else: try it all again 299 } 300 301 // Second we handle g_free_list: 302 prepend_list_to_common(new_blk + 1, &new_blk[_BLOCKSIZE - 1], _BLOCKSIZE - 1, 303 &g_free_list, &g_om_free_count); 304 } 305 306 // Prepend a list of ObjectMonitors to g_free_list. 'tail' is the last 307 // ObjectMonitor in the list and there are 'count' on the list. Also 308 // updates g_om_free_count. 309 static void prepend_list_to_g_free_list(ObjectMonitor* list, 310 ObjectMonitor* tail, int count) { 311 prepend_list_to_common(list, tail, count, &g_free_list, &g_om_free_count); 312 } 313 314 // Prepend a list of ObjectMonitors to g_wait_list. 'tail' is the last 315 // ObjectMonitor in the list and there are 'count' on the list. Also 316 // updates g_om_wait_count. 317 static void prepend_list_to_g_wait_list(ObjectMonitor* list, 318 ObjectMonitor* tail, int count) { 319 assert(HandshakeAfterDeflateIdleMonitors, "sanity check"); 320 prepend_list_to_common(list, tail, count, &g_wait_list, &g_om_wait_count); 321 } 322 323 // Prepend a list of ObjectMonitors to g_om_in_use_list. 'tail' is the last 324 // ObjectMonitor in the list and there are 'count' on the list. Also 325 // updates g_om_in_use_list. 326 static void prepend_list_to_g_om_in_use_list(ObjectMonitor* list, 327 ObjectMonitor* tail, int count) { 328 prepend_list_to_common(list, tail, count, &g_om_in_use_list, &g_om_in_use_count); 329 } 330 331 // Prepend an ObjectMonitor to the specified list. Also updates 332 // the specified counter. 333 static void prepend_to_common(ObjectMonitor* m, ObjectMonitor* volatile * list_p, 334 int volatile * count_p) { 335 while (true) { 336 (void)mark_next_loop(m); // mark m so we can safely update its next field 337 ObjectMonitor* cur = NULL; 338 ObjectMonitor* next = NULL; 339 // Mark the list head to guard against A-B-A race: 340 if (mark_list_head(list_p, &cur, &next)) { 341 // List head is now marked so we can safely switch it. 342 set_next(m, cur); // m now points to cur (and unmarks m) 343 *list_p = m; // Switch list head to unmarked m. 344 // mark_list_head() used cmpxchg() above, switching list head can be lazier: 345 OrderAccess::storestore(); 346 set_next(cur, next); // Unmark the previous list head. 347 break; 348 } 349 // The list is empty so try to set the list head. 350 assert(cur == NULL, "cur must be NULL: cur=" INTPTR_FORMAT, p2i(cur)); 351 set_next(m, cur); // m now points to NULL (and unmarks m) 352 if (Atomic::cmpxchg(m, list_p, cur) == cur) { 353 // List head is now unmarked m. 354 break; 355 } 356 // Implied else: try it all again 357 } 358 Atomic::inc(count_p); 359 } 360 361 // Prepend an ObjectMonitor to a per-thread om_free_list. 362 // Also updates the per-thread om_free_count. 363 static void prepend_to_om_free_list(Thread* self, ObjectMonitor* m) { 364 prepend_to_common(m, &self->om_free_list, &self->om_free_count); 365 } 366 367 // Prepend an ObjectMonitor to a per-thread om_in_use_list. 368 // Also updates the per-thread om_in_use_count. 369 static void prepend_to_om_in_use_list(Thread* self, ObjectMonitor* m) { 370 prepend_to_common(m, &self->om_in_use_list, &self->om_in_use_count); 371 } 372 373 // Take an ObjectMonitor from the start of the specified list. Also 374 // decrements the specified counter. Returns NULL if none are available. 375 static ObjectMonitor* take_from_start_of_common(ObjectMonitor* volatile * list_p, 376 int volatile * count_p) { 377 ObjectMonitor* next = NULL; 378 ObjectMonitor* take = NULL; 379 // Mark the list head to guard against A-B-A race: 380 if (!mark_list_head(list_p, &take, &next)) { 381 return NULL; // None are available. 382 } 383 // Switch marked list head to next (which unmarks the list head, but 384 // leaves take marked): 385 *list_p = next; 386 Atomic::dec(count_p); 387 // mark_list_head() used cmpxchg() above, switching list head can be lazier: 388 OrderAccess::storestore(); 389 // Unmark take, but leave the next value for any lagging list 390 // walkers. It will get cleaned up when take is prepended to 391 // the in-use list: 392 set_next(take, next); 393 return take; 394 } 395 396 // Take an ObjectMonitor from the start of the global free-list. Also 397 // updates g_om_free_count. Returns NULL if none are available. 398 static ObjectMonitor* take_from_start_of_g_free_list() { 399 return take_from_start_of_common(&g_free_list, &g_om_free_count); 400 } 401 402 // Take an ObjectMonitor from the start of a per-thread free-list. 403 // Also updates om_free_count. Returns NULL if none are available. 404 static ObjectMonitor* take_from_start_of_om_free_list(Thread* self) { 405 return take_from_start_of_common(&self->om_free_list, &self->om_free_count); 406 } 407 408 409 // =====================> Quick functions 410 411 // The quick_* forms are special fast-path variants used to improve 412 // performance. In the simplest case, a "quick_*" implementation could 413 // simply return false, in which case the caller will perform the necessary 414 // state transitions and call the slow-path form. 415 // The fast-path is designed to handle frequently arising cases in an efficient 416 // manner and is just a degenerate "optimistic" variant of the slow-path. 417 // returns true -- to indicate the call was satisfied. 418 // returns false -- to indicate the call needs the services of the slow-path. 419 // A no-loitering ordinance is in effect for code in the quick_* family 420 // operators: safepoints or indefinite blocking (blocking that might span a 421 // safepoint) are forbidden. Generally the thread_state() is _in_Java upon 422 // entry. 423 // 424 // Consider: An interesting optimization is to have the JIT recognize the 425 // following common idiom: 426 // synchronized (someobj) { .... ; notify(); } 427 // That is, we find a notify() or notifyAll() call that immediately precedes 428 // the monitorexit operation. In that case the JIT could fuse the operations 429 // into a single notifyAndExit() runtime primitive. 430 431 bool ObjectSynchronizer::quick_notify(oopDesc* obj, Thread* self, bool all) { 432 assert(!SafepointSynchronize::is_at_safepoint(), "invariant"); 433 assert(self->is_Java_thread(), "invariant"); 434 assert(((JavaThread *) self)->thread_state() == _thread_in_Java, "invariant"); 435 NoSafepointVerifier nsv; 436 if (obj == NULL) return false; // slow-path for invalid obj 437 const markWord mark = obj->mark(); 438 439 if (mark.has_locker() && self->is_lock_owned((address)mark.locker())) { 440 // Degenerate notify 441 // stack-locked by caller so by definition the implied waitset is empty. 442 return true; 443 } 444 445 if (mark.has_monitor()) { 446 ObjectMonitor* const mon = mark.monitor(); 447 assert(mon->object() == obj, "invariant"); 448 if (mon->owner() != self) return false; // slow-path for IMS exception 449 450 if (mon->first_waiter() != NULL) { 451 // We have one or more waiters. Since this is an inflated monitor 452 // that we own, we can transfer one or more threads from the waitset 453 // to the entrylist here and now, avoiding the slow-path. 454 if (all) { 455 DTRACE_MONITOR_PROBE(notifyAll, mon, obj, self); 456 } else { 457 DTRACE_MONITOR_PROBE(notify, mon, obj, self); 458 } 459 int free_count = 0; 460 do { 461 mon->INotify(self); 462 ++free_count; 463 } while (mon->first_waiter() != NULL && all); 464 OM_PERFDATA_OP(Notifications, inc(free_count)); 465 } 466 return true; 467 } 468 469 // biased locking and any other IMS exception states take the slow-path 470 return false; 471 } 472 473 474 // The LockNode emitted directly at the synchronization site would have 475 // been too big if it were to have included support for the cases of inflated 476 // recursive enter and exit, so they go here instead. 477 // Note that we can't safely call AsyncPrintJavaStack() from within 478 // quick_enter() as our thread state remains _in_Java. 479 480 bool ObjectSynchronizer::quick_enter(oop obj, Thread* self, 481 BasicLock * lock) { 482 assert(!SafepointSynchronize::is_at_safepoint(), "invariant"); 483 assert(self->is_Java_thread(), "invariant"); 484 assert(((JavaThread *) self)->thread_state() == _thread_in_Java, "invariant"); 485 NoSafepointVerifier nsv; 486 if (obj == NULL) return false; // Need to throw NPE 487 488 while (true) { 489 const markWord mark = obj->mark(); 490 491 if (mark.has_monitor()) { 492 ObjectMonitorHandle omh; 493 if (!omh.save_om_ptr(obj, mark)) { 494 // Lost a race with async deflation so try again. 495 assert(AsyncDeflateIdleMonitors, "sanity check"); 496 continue; 497 } 498 ObjectMonitor* const m = omh.om_ptr(); 499 assert(m->object() == obj, "invariant"); 500 Thread* const owner = (Thread *) m->_owner; 501 502 // Lock contention and Transactional Lock Elision (TLE) diagnostics 503 // and observability 504 // Case: light contention possibly amenable to TLE 505 // Case: TLE inimical operations such as nested/recursive synchronization 506 507 if (owner == self) { 508 m->_recursions++; 509 return true; 510 } 511 512 // This Java Monitor is inflated so obj's header will never be 513 // displaced to this thread's BasicLock. Make the displaced header 514 // non-NULL so this BasicLock is not seen as recursive nor as 515 // being locked. We do this unconditionally so that this thread's 516 // BasicLock cannot be mis-interpreted by any stack walkers. For 517 // performance reasons, stack walkers generally first check for 518 // Biased Locking in the object's header, the second check is for 519 // stack-locking in the object's header, the third check is for 520 // recursive stack-locking in the displaced header in the BasicLock, 521 // and last are the inflated Java Monitor (ObjectMonitor) checks. 522 lock->set_displaced_header(markWord::unused_mark()); 523 524 if (owner == NULL && m->try_set_owner_from(self, NULL) == NULL) { 525 assert(m->_recursions == 0, "invariant"); 526 return true; 527 } 528 529 if (AsyncDeflateIdleMonitors && 530 m->try_set_owner_from(self, DEFLATER_MARKER) == DEFLATER_MARKER) { 531 // The deflation protocol finished the first part (setting owner), 532 // but it failed the second part (making ref_count negative) and 533 // bailed. Or the ObjectMonitor was async deflated and reused. 534 // Acquired the monitor. 535 assert(m->_recursions == 0, "invariant"); 536 return true; 537 } 538 } 539 break; 540 } 541 542 // Note that we could inflate in quick_enter. 543 // This is likely a useful optimization 544 // Critically, in quick_enter() we must not: 545 // -- perform bias revocation, or 546 // -- block indefinitely, or 547 // -- reach a safepoint 548 549 return false; // revert to slow-path 550 } 551 552 // ----------------------------------------------------------------------------- 553 // Monitor Enter/Exit 554 // The interpreter and compiler assembly code tries to lock using the fast path 555 // of this algorithm. Make sure to update that code if the following function is 556 // changed. The implementation is extremely sensitive to race condition. Be careful. 557 558 void ObjectSynchronizer::enter(Handle obj, BasicLock* lock, TRAPS) { 559 if (UseBiasedLocking) { 560 if (!SafepointSynchronize::is_at_safepoint()) { 561 BiasedLocking::revoke(obj, THREAD); 562 } else { 563 BiasedLocking::revoke_at_safepoint(obj); 564 } 565 } 566 567 markWord mark = obj->mark(); 568 assert(!mark.has_bias_pattern(), "should not see bias pattern here"); 569 570 if (mark.is_neutral()) { 571 // Anticipate successful CAS -- the ST of the displaced mark must 572 // be visible <= the ST performed by the CAS. 573 lock->set_displaced_header(mark); 574 if (mark == obj()->cas_set_mark(markWord::from_pointer(lock), mark)) { 575 return; 576 } 577 // Fall through to inflate() ... 578 } else if (mark.has_locker() && 579 THREAD->is_lock_owned((address)mark.locker())) { 580 assert(lock != mark.locker(), "must not re-lock the same lock"); 581 assert(lock != (BasicLock*)obj->mark().value(), "don't relock with same BasicLock"); 582 lock->set_displaced_header(markWord::from_pointer(NULL)); 583 return; 584 } 585 586 // The object header will never be displaced to this lock, 587 // so it does not matter what the value is, except that it 588 // must be non-zero to avoid looking like a re-entrant lock, 589 // and must not look locked either. 590 lock->set_displaced_header(markWord::unused_mark()); 591 ObjectMonitorHandle omh; 592 inflate(&omh, THREAD, obj(), inflate_cause_monitor_enter); 593 omh.om_ptr()->enter(THREAD); 594 } 595 596 void ObjectSynchronizer::exit(oop object, BasicLock* lock, TRAPS) { 597 markWord mark = object->mark(); 598 // We cannot check for Biased Locking if we are racing an inflation. 599 assert(mark == markWord::INFLATING() || 600 !mark.has_bias_pattern(), "should not see bias pattern here"); 601 602 markWord dhw = lock->displaced_header(); 603 if (dhw.value() == 0) { 604 // If the displaced header is NULL, then this exit matches up with 605 // a recursive enter. No real work to do here except for diagnostics. 606 #ifndef PRODUCT 607 if (mark != markWord::INFLATING()) { 608 // Only do diagnostics if we are not racing an inflation. Simply 609 // exiting a recursive enter of a Java Monitor that is being 610 // inflated is safe; see the has_monitor() comment below. 611 assert(!mark.is_neutral(), "invariant"); 612 assert(!mark.has_locker() || 613 THREAD->is_lock_owned((address)mark.locker()), "invariant"); 614 if (mark.has_monitor()) { 615 // The BasicLock's displaced_header is marked as a recursive 616 // enter and we have an inflated Java Monitor (ObjectMonitor). 617 // This is a special case where the Java Monitor was inflated 618 // after this thread entered the stack-lock recursively. When a 619 // Java Monitor is inflated, we cannot safely walk the Java 620 // Monitor owner's stack and update the BasicLocks because a 621 // Java Monitor can be asynchronously inflated by a thread that 622 // does not own the Java Monitor. 623 ObjectMonitor* m = mark.monitor(); 624 assert(((oop)(m->object()))->mark() == mark, "invariant"); 625 assert(m->is_entered(THREAD), "invariant"); 626 } 627 } 628 #endif 629 return; 630 } 631 632 if (mark == markWord::from_pointer(lock)) { 633 // If the object is stack-locked by the current thread, try to 634 // swing the displaced header from the BasicLock back to the mark. 635 assert(dhw.is_neutral(), "invariant"); 636 if (object->cas_set_mark(dhw, mark) == mark) { 637 return; 638 } 639 } 640 641 // We have to take the slow-path of possible inflation and then exit. 642 ObjectMonitorHandle omh; 643 inflate(&omh, THREAD, object, inflate_cause_vm_internal); 644 omh.om_ptr()->exit(true, THREAD); 645 } 646 647 // ----------------------------------------------------------------------------- 648 // Class Loader support to workaround deadlocks on the class loader lock objects 649 // Also used by GC 650 // complete_exit()/reenter() are used to wait on a nested lock 651 // i.e. to give up an outer lock completely and then re-enter 652 // Used when holding nested locks - lock acquisition order: lock1 then lock2 653 // 1) complete_exit lock1 - saving recursion count 654 // 2) wait on lock2 655 // 3) when notified on lock2, unlock lock2 656 // 4) reenter lock1 with original recursion count 657 // 5) lock lock2 658 // NOTE: must use heavy weight monitor to handle complete_exit/reenter() 659 intptr_t ObjectSynchronizer::complete_exit(Handle obj, TRAPS) { 660 if (UseBiasedLocking) { 661 BiasedLocking::revoke(obj, THREAD); 662 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now"); 663 } 664 665 ObjectMonitorHandle omh; 666 inflate(&omh, THREAD, obj(), inflate_cause_vm_internal); 667 intptr_t ret_code = omh.om_ptr()->complete_exit(THREAD); 668 return ret_code; 669 } 670 671 // NOTE: must use heavy weight monitor to handle complete_exit/reenter() 672 void ObjectSynchronizer::reenter(Handle obj, intptr_t recursion, TRAPS) { 673 if (UseBiasedLocking) { 674 BiasedLocking::revoke(obj, THREAD); 675 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now"); 676 } 677 678 ObjectMonitorHandle omh; 679 inflate(&omh, THREAD, obj(), inflate_cause_vm_internal); 680 omh.om_ptr()->reenter(recursion, THREAD); 681 } 682 // ----------------------------------------------------------------------------- 683 // JNI locks on java objects 684 // NOTE: must use heavy weight monitor to handle jni monitor enter 685 void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) { 686 // the current locking is from JNI instead of Java code 687 if (UseBiasedLocking) { 688 BiasedLocking::revoke(obj, THREAD); 689 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now"); 690 } 691 THREAD->set_current_pending_monitor_is_from_java(false); 692 ObjectMonitorHandle omh; 693 inflate(&omh, THREAD, obj(), inflate_cause_jni_enter); 694 omh.om_ptr()->enter(THREAD); 695 THREAD->set_current_pending_monitor_is_from_java(true); 696 } 697 698 // NOTE: must use heavy weight monitor to handle jni monitor exit 699 void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) { 700 if (UseBiasedLocking) { 701 Handle h_obj(THREAD, obj); 702 BiasedLocking::revoke(h_obj, THREAD); 703 obj = h_obj(); 704 } 705 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now"); 706 707 ObjectMonitorHandle omh; 708 inflate(&omh, THREAD, obj, inflate_cause_jni_exit); 709 ObjectMonitor* monitor = omh.om_ptr(); 710 // If this thread has locked the object, exit the monitor. We 711 // intentionally do not use CHECK here because we must exit the 712 // monitor even if an exception is pending. 713 if (monitor->check_owner(THREAD)) { 714 monitor->exit(true, THREAD); 715 } 716 } 717 718 // ----------------------------------------------------------------------------- 719 // Internal VM locks on java objects 720 // standard constructor, allows locking failures 721 ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool do_lock) { 722 _dolock = do_lock; 723 _thread = thread; 724 _thread->check_for_valid_safepoint_state(); 725 _obj = obj; 726 727 if (_dolock) { 728 ObjectSynchronizer::enter(_obj, &_lock, _thread); 729 } 730 } 731 732 ObjectLocker::~ObjectLocker() { 733 if (_dolock) { 734 ObjectSynchronizer::exit(_obj(), &_lock, _thread); 735 } 736 } 737 738 739 // ----------------------------------------------------------------------------- 740 // Wait/Notify/NotifyAll 741 // NOTE: must use heavy weight monitor to handle wait() 742 int ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) { 743 if (UseBiasedLocking) { 744 BiasedLocking::revoke(obj, THREAD); 745 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now"); 746 } 747 if (millis < 0) { 748 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative"); 749 } 750 ObjectMonitorHandle omh; 751 inflate(&omh, THREAD, obj(), inflate_cause_wait); 752 ObjectMonitor* monitor = omh.om_ptr(); 753 754 DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis); 755 monitor->wait(millis, true, THREAD); 756 757 // This dummy call is in place to get around dtrace bug 6254741. Once 758 // that's fixed we can uncomment the following line, remove the call 759 // and change this function back into a "void" func. 760 // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD); 761 int ret_code = dtrace_waited_probe(monitor, obj, THREAD); 762 return ret_code; 763 } 764 765 void ObjectSynchronizer::wait_uninterruptibly(Handle obj, jlong millis, TRAPS) { 766 if (UseBiasedLocking) { 767 BiasedLocking::revoke(obj, THREAD); 768 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now"); 769 } 770 if (millis < 0) { 771 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative"); 772 } 773 ObjectMonitorHandle omh; 774 inflate(&omh, THREAD, obj(), inflate_cause_wait); 775 omh.om_ptr()->wait(millis, false, THREAD); 776 } 777 778 void ObjectSynchronizer::notify(Handle obj, TRAPS) { 779 if (UseBiasedLocking) { 780 BiasedLocking::revoke(obj, THREAD); 781 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now"); 782 } 783 784 markWord mark = obj->mark(); 785 if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) { 786 return; 787 } 788 ObjectMonitorHandle omh; 789 inflate(&omh, THREAD, obj(), inflate_cause_notify); 790 omh.om_ptr()->notify(THREAD); 791 } 792 793 // NOTE: see comment of notify() 794 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) { 795 if (UseBiasedLocking) { 796 BiasedLocking::revoke(obj, THREAD); 797 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now"); 798 } 799 800 markWord mark = obj->mark(); 801 if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) { 802 return; 803 } 804 ObjectMonitorHandle omh; 805 inflate(&omh, THREAD, obj(), inflate_cause_notify); 806 omh.om_ptr()->notifyAll(THREAD); 807 } 808 809 // ----------------------------------------------------------------------------- 810 // Hash Code handling 811 // 812 // Performance concern: 813 // OrderAccess::storestore() calls release() which at one time stored 0 814 // into the global volatile OrderAccess::dummy variable. This store was 815 // unnecessary for correctness. Many threads storing into a common location 816 // causes considerable cache migration or "sloshing" on large SMP systems. 817 // As such, I avoided using OrderAccess::storestore(). In some cases 818 // OrderAccess::fence() -- which incurs local latency on the executing 819 // processor -- is a better choice as it scales on SMP systems. 820 // 821 // See http://blogs.oracle.com/dave/entry/biased_locking_in_hotspot for 822 // a discussion of coherency costs. Note that all our current reference 823 // platforms provide strong ST-ST order, so the issue is moot on IA32, 824 // x64, and SPARC. 825 // 826 // As a general policy we use "volatile" to control compiler-based reordering 827 // and explicit fences (barriers) to control for architectural reordering 828 // performed by the CPU(s) or platform. 829 830 struct SharedGlobals { 831 char _pad_prefix[OM_CACHE_LINE_SIZE]; 832 // These are highly shared mostly-read variables. 833 // To avoid false-sharing they need to be the sole occupants of a cache line. 834 volatile int stw_random; 835 volatile int stw_cycle; 836 DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(volatile int) * 2); 837 // Hot RW variable -- Sequester to avoid false-sharing 838 volatile int hc_sequence; 839 DEFINE_PAD_MINUS_SIZE(2, OM_CACHE_LINE_SIZE, sizeof(volatile int)); 840 }; 841 842 static SharedGlobals GVars; 843 static int MonitorScavengeThreshold = 1000000; 844 static volatile int ForceMonitorScavenge = 0; // Scavenge required and pending 845 846 static markWord read_stable_mark(oop obj) { 847 markWord mark = obj->mark(); 848 if (!mark.is_being_inflated()) { 849 return mark; // normal fast-path return 850 } 851 852 int its = 0; 853 for (;;) { 854 markWord mark = obj->mark(); 855 if (!mark.is_being_inflated()) { 856 return mark; // normal fast-path return 857 } 858 859 // The object is being inflated by some other thread. 860 // The caller of read_stable_mark() must wait for inflation to complete. 861 // Avoid live-lock 862 // TODO: consider calling SafepointSynchronize::do_call_back() while 863 // spinning to see if there's a safepoint pending. If so, immediately 864 // yielding or blocking would be appropriate. Avoid spinning while 865 // there is a safepoint pending. 866 // TODO: add inflation contention performance counters. 867 // TODO: restrict the aggregate number of spinners. 868 869 ++its; 870 if (its > 10000 || !os::is_MP()) { 871 if (its & 1) { 872 os::naked_yield(); 873 } else { 874 // Note that the following code attenuates the livelock problem but is not 875 // a complete remedy. A more complete solution would require that the inflating 876 // thread hold the associated inflation lock. The following code simply restricts 877 // the number of spinners to at most one. We'll have N-2 threads blocked 878 // on the inflationlock, 1 thread holding the inflation lock and using 879 // a yield/park strategy, and 1 thread in the midst of inflation. 880 // A more refined approach would be to change the encoding of INFLATING 881 // to allow encapsulation of a native thread pointer. Threads waiting for 882 // inflation to complete would use CAS to push themselves onto a singly linked 883 // list rooted at the markword. Once enqueued, they'd loop, checking a per-thread flag 884 // and calling park(). When inflation was complete the thread that accomplished inflation 885 // would detach the list and set the markword to inflated with a single CAS and 886 // then for each thread on the list, set the flag and unpark() the thread. 887 // This is conceptually similar to muxAcquire-muxRelease, except that muxRelease 888 // wakes at most one thread whereas we need to wake the entire list. 889 int ix = (cast_from_oop<intptr_t>(obj) >> 5) & (NINFLATIONLOCKS-1); 890 int YieldThenBlock = 0; 891 assert(ix >= 0 && ix < NINFLATIONLOCKS, "invariant"); 892 assert((NINFLATIONLOCKS & (NINFLATIONLOCKS-1)) == 0, "invariant"); 893 Thread::muxAcquire(gInflationLocks + ix, "gInflationLock"); 894 while (obj->mark() == markWord::INFLATING()) { 895 // Beware: NakedYield() is advisory and has almost no effect on some platforms 896 // so we periodically call self->_ParkEvent->park(1). 897 // We use a mixed spin/yield/block mechanism. 898 if ((YieldThenBlock++) >= 16) { 899 Thread::current()->_ParkEvent->park(1); 900 } else { 901 os::naked_yield(); 902 } 903 } 904 Thread::muxRelease(gInflationLocks + ix); 905 } 906 } else { 907 SpinPause(); // SMP-polite spinning 908 } 909 } 910 } 911 912 // hashCode() generation : 913 // 914 // Possibilities: 915 // * MD5Digest of {obj,stw_random} 916 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function. 917 // * A DES- or AES-style SBox[] mechanism 918 // * One of the Phi-based schemes, such as: 919 // 2654435761 = 2^32 * Phi (golden ratio) 920 // HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ; 921 // * A variation of Marsaglia's shift-xor RNG scheme. 922 // * (obj ^ stw_random) is appealing, but can result 923 // in undesirable regularity in the hashCode values of adjacent objects 924 // (objects allocated back-to-back, in particular). This could potentially 925 // result in hashtable collisions and reduced hashtable efficiency. 926 // There are simple ways to "diffuse" the middle address bits over the 927 // generated hashCode values: 928 929 static inline intptr_t get_next_hash(Thread* self, oop obj) { 930 intptr_t value = 0; 931 if (hashCode == 0) { 932 // This form uses global Park-Miller RNG. 933 // On MP system we'll have lots of RW access to a global, so the 934 // mechanism induces lots of coherency traffic. 935 value = os::random(); 936 } else if (hashCode == 1) { 937 // This variation has the property of being stable (idempotent) 938 // between STW operations. This can be useful in some of the 1-0 939 // synchronization schemes. 940 intptr_t addr_bits = cast_from_oop<intptr_t>(obj) >> 3; 941 value = addr_bits ^ (addr_bits >> 5) ^ GVars.stw_random; 942 } else if (hashCode == 2) { 943 value = 1; // for sensitivity testing 944 } else if (hashCode == 3) { 945 value = ++GVars.hc_sequence; 946 } else if (hashCode == 4) { 947 value = cast_from_oop<intptr_t>(obj); 948 } else { 949 // Marsaglia's xor-shift scheme with thread-specific state 950 // This is probably the best overall implementation -- we'll 951 // likely make this the default in future releases. 952 unsigned t = self->_hashStateX; 953 t ^= (t << 11); 954 self->_hashStateX = self->_hashStateY; 955 self->_hashStateY = self->_hashStateZ; 956 self->_hashStateZ = self->_hashStateW; 957 unsigned v = self->_hashStateW; 958 v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)); 959 self->_hashStateW = v; 960 value = v; 961 } 962 963 value &= markWord::hash_mask; 964 if (value == 0) value = 0xBAD; 965 assert(value != markWord::no_hash, "invariant"); 966 return value; 967 } 968 969 intptr_t ObjectSynchronizer::FastHashCode(Thread* self, oop obj) { 970 if (UseBiasedLocking) { 971 // NOTE: many places throughout the JVM do not expect a safepoint 972 // to be taken here, in particular most operations on perm gen 973 // objects. However, we only ever bias Java instances and all of 974 // the call sites of identity_hash that might revoke biases have 975 // been checked to make sure they can handle a safepoint. The 976 // added check of the bias pattern is to avoid useless calls to 977 // thread-local storage. 978 if (obj->mark().has_bias_pattern()) { 979 // Handle for oop obj in case of STW safepoint 980 Handle hobj(self, obj); 981 // Relaxing assertion for bug 6320749. 982 assert(Universe::verify_in_progress() || 983 !SafepointSynchronize::is_at_safepoint(), 984 "biases should not be seen by VM thread here"); 985 BiasedLocking::revoke(hobj, JavaThread::current()); 986 obj = hobj(); 987 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now"); 988 } 989 } 990 991 // hashCode() is a heap mutator ... 992 // Relaxing assertion for bug 6320749. 993 assert(Universe::verify_in_progress() || DumpSharedSpaces || 994 !SafepointSynchronize::is_at_safepoint(), "invariant"); 995 assert(Universe::verify_in_progress() || DumpSharedSpaces || 996 self->is_Java_thread() , "invariant"); 997 assert(Universe::verify_in_progress() || DumpSharedSpaces || 998 ((JavaThread *)self)->thread_state() != _thread_blocked, "invariant"); 999 1000 while (true) { 1001 ObjectMonitor* monitor = NULL; 1002 markWord temp, test; 1003 intptr_t hash; 1004 markWord mark = read_stable_mark(obj); 1005 1006 // object should remain ineligible for biased locking 1007 assert(!mark.has_bias_pattern(), "invariant"); 1008 1009 if (mark.is_neutral()) { 1010 hash = mark.hash(); // this is a normal header 1011 if (hash != 0) { // if it has hash, just return it 1012 return hash; 1013 } 1014 hash = get_next_hash(self, obj); // allocate a new hash code 1015 temp = mark.copy_set_hash(hash); // merge the hash code into header 1016 // use (machine word version) atomic operation to install the hash 1017 test = obj->cas_set_mark(temp, mark); 1018 if (test == mark) { 1019 return hash; 1020 } 1021 // If atomic operation failed, we must inflate the header 1022 // into heavy weight monitor. We could add more code here 1023 // for fast path, but it does not worth the complexity. 1024 } else if (mark.has_monitor()) { 1025 ObjectMonitorHandle omh; 1026 if (!omh.save_om_ptr(obj, mark)) { 1027 // Lost a race with async deflation so try again. 1028 assert(AsyncDeflateIdleMonitors, "sanity check"); 1029 continue; 1030 } 1031 monitor = omh.om_ptr(); 1032 temp = monitor->header(); 1033 assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value()); 1034 hash = temp.hash(); 1035 if (hash != 0) { 1036 return hash; 1037 } 1038 // Skip to the following code to reduce code size 1039 } else if (self->is_lock_owned((address)mark.locker())) { 1040 temp = mark.displaced_mark_helper(); // this is a lightweight monitor owned 1041 assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value()); 1042 hash = temp.hash(); // by current thread, check if the displaced 1043 if (hash != 0) { // header contains hash code 1044 return hash; 1045 } 1046 // WARNING: 1047 // The displaced header in the BasicLock on a thread's stack 1048 // is strictly immutable. It CANNOT be changed in ANY cases. 1049 // So we have to inflate the stack lock into an ObjectMonitor 1050 // even if the current thread owns the lock. The BasicLock on 1051 // a thread's stack can be asynchronously read by other threads 1052 // during an inflate() call so any change to that stack memory 1053 // may not propagate to other threads correctly. 1054 } 1055 1056 // Inflate the monitor to set hash code 1057 ObjectMonitorHandle omh; 1058 inflate(&omh, self, obj, inflate_cause_hash_code); 1059 monitor = omh.om_ptr(); 1060 // Load displaced header and check it has hash code 1061 mark = monitor->header(); 1062 assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value()); 1063 hash = mark.hash(); 1064 if (hash == 0) { 1065 hash = get_next_hash(self, obj); 1066 temp = mark.copy_set_hash(hash); // merge hash code into header 1067 assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value()); 1068 uintptr_t v = Atomic::cmpxchg(temp.value(), (volatile uintptr_t*)monitor->header_addr(), mark.value()); 1069 test = markWord(v); 1070 if (test != mark) { 1071 // The only non-deflation update to the ObjectMonitor's 1072 // header/dmw field is to merge in the hash code. If someone 1073 // adds a new usage of the header/dmw field, please update 1074 // this code. 1075 // ObjectMonitor::install_displaced_markword_in_object() 1076 // does mark the header/dmw field as part of async deflation, 1077 // but that protocol cannot happen now due to the 1078 // ObjectMonitorHandle above. 1079 hash = test.hash(); 1080 assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value()); 1081 assert(hash != 0, "Trivial unexpected object/monitor header usage."); 1082 } 1083 } 1084 // We finally get the hash 1085 return hash; 1086 } 1087 } 1088 1089 // Deprecated -- use FastHashCode() instead. 1090 1091 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) { 1092 return FastHashCode(Thread::current(), obj()); 1093 } 1094 1095 1096 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread, 1097 Handle h_obj) { 1098 if (UseBiasedLocking) { 1099 BiasedLocking::revoke(h_obj, thread); 1100 assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now"); 1101 } 1102 1103 assert(thread == JavaThread::current(), "Can only be called on current thread"); 1104 oop obj = h_obj(); 1105 1106 while (true) { 1107 markWord mark = read_stable_mark(obj); 1108 1109 // Uncontended case, header points to stack 1110 if (mark.has_locker()) { 1111 return thread->is_lock_owned((address)mark.locker()); 1112 } 1113 // Contended case, header points to ObjectMonitor (tagged pointer) 1114 if (mark.has_monitor()) { 1115 ObjectMonitorHandle omh; 1116 if (!omh.save_om_ptr(obj, mark)) { 1117 // Lost a race with async deflation so try again. 1118 assert(AsyncDeflateIdleMonitors, "sanity check"); 1119 continue; 1120 } 1121 bool ret_code = omh.om_ptr()->is_entered(thread) != 0; 1122 return ret_code; 1123 } 1124 // Unlocked case, header in place 1125 assert(mark.is_neutral(), "sanity check"); 1126 return false; 1127 } 1128 } 1129 1130 // Be aware of this method could revoke bias of the lock object. 1131 // This method queries the ownership of the lock handle specified by 'h_obj'. 1132 // If the current thread owns the lock, it returns owner_self. If no 1133 // thread owns the lock, it returns owner_none. Otherwise, it will return 1134 // owner_other. 1135 ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership 1136 (JavaThread *self, Handle h_obj) { 1137 // The caller must beware this method can revoke bias, and 1138 // revocation can result in a safepoint. 1139 assert(!SafepointSynchronize::is_at_safepoint(), "invariant"); 1140 assert(self->thread_state() != _thread_blocked, "invariant"); 1141 1142 // Possible mark states: neutral, biased, stack-locked, inflated 1143 1144 if (UseBiasedLocking && h_obj()->mark().has_bias_pattern()) { 1145 // CASE: biased 1146 BiasedLocking::revoke(h_obj, self); 1147 assert(!h_obj->mark().has_bias_pattern(), 1148 "biases should be revoked by now"); 1149 } 1150 1151 assert(self == JavaThread::current(), "Can only be called on current thread"); 1152 oop obj = h_obj(); 1153 1154 while (true) { 1155 markWord mark = read_stable_mark(obj); 1156 1157 // CASE: stack-locked. Mark points to a BasicLock on the owner's stack. 1158 if (mark.has_locker()) { 1159 return self->is_lock_owned((address)mark.locker()) ? 1160 owner_self : owner_other; 1161 } 1162 1163 // CASE: inflated. Mark (tagged pointer) points to an ObjectMonitor. 1164 // The Object:ObjectMonitor relationship is stable as long as we're 1165 // not at a safepoint and AsyncDeflateIdleMonitors is false. 1166 if (mark.has_monitor()) { 1167 ObjectMonitorHandle omh; 1168 if (!omh.save_om_ptr(obj, mark)) { 1169 // Lost a race with async deflation so try again. 1170 assert(AsyncDeflateIdleMonitors, "sanity check"); 1171 continue; 1172 } 1173 ObjectMonitor* monitor = omh.om_ptr(); 1174 void* owner = monitor->_owner; 1175 if (owner == NULL) return owner_none; 1176 return (owner == self || 1177 self->is_lock_owned((address)owner)) ? owner_self : owner_other; 1178 } 1179 1180 // CASE: neutral 1181 assert(mark.is_neutral(), "sanity check"); 1182 return owner_none; // it's unlocked 1183 } 1184 } 1185 1186 // FIXME: jvmti should call this 1187 JavaThread* ObjectSynchronizer::get_lock_owner(ThreadsList * t_list, Handle h_obj) { 1188 if (UseBiasedLocking) { 1189 if (SafepointSynchronize::is_at_safepoint()) { 1190 BiasedLocking::revoke_at_safepoint(h_obj); 1191 } else { 1192 BiasedLocking::revoke(h_obj, JavaThread::current()); 1193 } 1194 assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now"); 1195 } 1196 1197 oop obj = h_obj(); 1198 1199 while (true) { 1200 address owner = NULL; 1201 markWord mark = read_stable_mark(obj); 1202 1203 // Uncontended case, header points to stack 1204 if (mark.has_locker()) { 1205 owner = (address) mark.locker(); 1206 } 1207 1208 // Contended case, header points to ObjectMonitor (tagged pointer) 1209 else if (mark.has_monitor()) { 1210 ObjectMonitorHandle omh; 1211 if (!omh.save_om_ptr(obj, mark)) { 1212 // Lost a race with async deflation so try again. 1213 assert(AsyncDeflateIdleMonitors, "sanity check"); 1214 continue; 1215 } 1216 ObjectMonitor* monitor = omh.om_ptr(); 1217 assert(monitor != NULL, "monitor should be non-null"); 1218 owner = (address) monitor->owner(); 1219 } 1220 1221 if (owner != NULL) { 1222 // owning_thread_from_monitor_owner() may also return NULL here 1223 return Threads::owning_thread_from_monitor_owner(t_list, owner); 1224 } 1225 1226 // Unlocked case, header in place 1227 // Cannot have assertion since this object may have been 1228 // locked by another thread when reaching here. 1229 // assert(mark.is_neutral(), "sanity check"); 1230 1231 return NULL; 1232 } 1233 } 1234 1235 // Visitors ... 1236 1237 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) { 1238 PaddedObjectMonitor* block = g_block_list; 1239 while (block != NULL) { 1240 assert(block->object() == CHAINMARKER, "must be a block header"); 1241 for (int i = _BLOCKSIZE - 1; i > 0; i--) { 1242 ObjectMonitor* mid = (ObjectMonitor *)(block + i); 1243 ObjectMonitorHandle omh; 1244 if (!mid->is_free() && omh.set_om_ptr_if_safe(mid)) { 1245 // The ObjectMonitor* is not free and it has been made safe. 1246 if (mid->object() == NULL) { 1247 // Only process with closure if the object is set. 1248 continue; 1249 } 1250 closure->do_monitor(mid); 1251 } 1252 } 1253 // unmarked_next() is not needed with g_block_list (no next field 1254 // marking) and no load_acquire() needed because _next_om is 1255 // updated before g_block_list is changed with cmpxchg(). 1256 block = (PaddedObjectMonitor*)block->_next_om; 1257 } 1258 } 1259 1260 static bool monitors_used_above_threshold() { 1261 if (g_om_population == 0) { 1262 return false; 1263 } 1264 if (MonitorUsedDeflationThreshold > 0) { 1265 int monitors_used = g_om_population - g_om_free_count; 1266 if (HandshakeAfterDeflateIdleMonitors) { 1267 monitors_used -= g_om_wait_count; 1268 } 1269 int monitor_usage = (monitors_used * 100LL) / g_om_population; 1270 return monitor_usage > MonitorUsedDeflationThreshold; 1271 } 1272 return false; 1273 } 1274 1275 // Returns true if MonitorBound is set (> 0) and if the specified 1276 // cnt is > MonitorBound. Otherwise returns false. 1277 static bool is_MonitorBound_exceeded(const int cnt) { 1278 const int mx = MonitorBound; 1279 return mx > 0 && cnt > mx; 1280 } 1281 1282 bool ObjectSynchronizer::is_async_deflation_needed() { 1283 if (!AsyncDeflateIdleMonitors) { 1284 return false; 1285 } 1286 if (is_async_deflation_requested()) { 1287 // Async deflation request. 1288 return true; 1289 } 1290 if (AsyncDeflationInterval > 0 && 1291 time_since_last_async_deflation_ms() > AsyncDeflationInterval && 1292 monitors_used_above_threshold()) { 1293 // It's been longer than our specified deflate interval and there 1294 // are too many monitors in use. We don't deflate more frequently 1295 // than AsyncDeflationInterval (unless is_async_deflation_requested) 1296 // in order to not swamp the ServiceThread. 1297 _last_async_deflation_time_ns = os::javaTimeNanos(); 1298 return true; 1299 } 1300 int monitors_used = g_om_population - g_om_free_count; 1301 if (HandshakeAfterDeflateIdleMonitors) { 1302 monitors_used -= g_om_wait_count; 1303 } 1304 if (is_MonitorBound_exceeded(monitors_used)) { 1305 // Not enough ObjectMonitors on the global free list. 1306 return true; 1307 } 1308 return false; 1309 } 1310 1311 bool ObjectSynchronizer::is_safepoint_deflation_needed() { 1312 if (!AsyncDeflateIdleMonitors) { 1313 if (monitors_used_above_threshold()) { 1314 // Too many monitors in use. 1315 return true; 1316 } 1317 return false; 1318 } 1319 if (is_special_deflation_requested()) { 1320 // For AsyncDeflateIdleMonitors only do a safepoint deflation 1321 // if there is a special deflation request. 1322 return true; 1323 } 1324 return false; 1325 } 1326 1327 jlong ObjectSynchronizer::time_since_last_async_deflation_ms() { 1328 return (os::javaTimeNanos() - _last_async_deflation_time_ns) / (NANOUNITS / MILLIUNITS); 1329 } 1330 1331 void ObjectSynchronizer::oops_do(OopClosure* f) { 1332 // We only scan the global used list here (for moribund threads), and 1333 // the thread-local monitors in Thread::oops_do(). 1334 global_used_oops_do(f); 1335 } 1336 1337 void ObjectSynchronizer::global_used_oops_do(OopClosure* f) { 1338 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1339 list_oops_do(g_om_in_use_list, g_om_in_use_count, f); 1340 } 1341 1342 void ObjectSynchronizer::thread_local_used_oops_do(Thread* thread, OopClosure* f) { 1343 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1344 list_oops_do(thread->om_in_use_list, thread->om_in_use_count, f); 1345 } 1346 1347 void ObjectSynchronizer::list_oops_do(ObjectMonitor* list, int count, OopClosure* f) { 1348 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1349 // The oops_do() phase does not overlap with monitor deflation 1350 // so no need to update the ObjectMonitor's ref_count for this 1351 // ObjectMonitor* use and no need to mark ObjectMonitors for the 1352 // list traversal. 1353 for (ObjectMonitor* mid = list; mid != NULL; mid = unmarked_next(mid)) { 1354 if (mid->object() != NULL) { 1355 f->do_oop((oop*)mid->object_addr()); 1356 } 1357 } 1358 } 1359 1360 1361 // ----------------------------------------------------------------------------- 1362 // ObjectMonitor Lifecycle 1363 // ----------------------- 1364 // Inflation unlinks monitors from the global g_free_list and 1365 // associates them with objects. Deflation -- which occurs at 1366 // STW-time -- disassociates idle monitors from objects. Such 1367 // scavenged monitors are returned to the g_free_list. 1368 // 1369 // ObjectMonitors reside in type-stable memory (TSM) and are immortal. 1370 // 1371 // Lifecycle: 1372 // -- unassigned and on the global free list 1373 // -- unassigned and on a thread's private om_free_list 1374 // -- assigned to an object. The object is inflated and the mark refers 1375 // to the objectmonitor. 1376 1377 1378 // Constraining monitor pool growth via MonitorBound ... 1379 // 1380 // If MonitorBound is not set (<= 0), MonitorBound checks are disabled. 1381 // 1382 // When safepoint deflation is being used (!AsyncDeflateIdleMonitors): 1383 // The monitor pool is grow-only. We scavenge at STW safepoint-time, but the 1384 // the rate of scavenging is driven primarily by GC. As such, we can find 1385 // an inordinate number of monitors in circulation. 1386 // To avoid that scenario we can artificially induce a STW safepoint 1387 // if the pool appears to be growing past some reasonable bound. 1388 // Generally we favor time in space-time tradeoffs, but as there's no 1389 // natural back-pressure on the # of extant monitors we need to impose some 1390 // type of limit. Beware that if MonitorBound is set to too low a value 1391 // we could just loop. In addition, if MonitorBound is set to a low value 1392 // we'll incur more safepoints, which are harmful to performance. 1393 // See also: GuaranteedSafepointInterval 1394 // 1395 // The current implementation uses asynchronous VM operations. 1396 // 1397 // When safepoint deflation is being used and MonitorBound is set, the 1398 // boundry applies to 1399 // (g_om_population - g_om_free_count) 1400 // i.e., if there are not enough ObjectMonitors on the global free list, 1401 // then a safepoint deflation is induced. Picking a good MonitorBound value 1402 // is non-trivial. 1403 // 1404 // When async deflation is being used: 1405 // The monitor pool is still grow-only. Async deflation is requested 1406 // by a safepoint's cleanup phase or by the ServiceThread at periodic 1407 // intervals when is_async_deflation_needed() returns true. In 1408 // addition to other policies that are checked, if there are not 1409 // enough ObjectMonitors on the global free list, then 1410 // is_async_deflation_needed() will return true. The ServiceThread 1411 // calls deflate_global_idle_monitors_using_JT() and also calls 1412 // deflate_per_thread_idle_monitors_using_JT() as needed. 1413 1414 static void InduceScavenge(Thread* self, const char * Whence) { 1415 assert(!AsyncDeflateIdleMonitors, "is not used by async deflation"); 1416 1417 // Induce STW safepoint to trim monitors 1418 // Ultimately, this results in a call to deflate_idle_monitors() in the near future. 1419 // More precisely, trigger an asynchronous STW safepoint as the number 1420 // of active monitors passes the specified threshold. 1421 // TODO: assert thread state is reasonable 1422 1423 if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) { 1424 // Induce a 'null' safepoint to scavenge monitors 1425 // Must VM_Operation instance be heap allocated as the op will be enqueue and posted 1426 // to the VMthread and have a lifespan longer than that of this activation record. 1427 // The VMThread will delete the op when completed. 1428 VMThread::execute(new VM_ScavengeMonitors()); 1429 } 1430 } 1431 1432 ObjectMonitor* ObjectSynchronizer::om_alloc(Thread* self, 1433 const InflateCause cause) { 1434 // A large MAXPRIVATE value reduces both list lock contention 1435 // and list coherency traffic, but also tends to increase the 1436 // number of ObjectMonitors in circulation as well as the STW 1437 // scavenge costs. As usual, we lean toward time in space-time 1438 // tradeoffs. 1439 const int MAXPRIVATE = 1024; 1440 1441 stringStream ss; 1442 for (;;) { 1443 ObjectMonitor* m; 1444 1445 // 1: try to allocate from the thread's local om_free_list. 1446 // Threads will attempt to allocate first from their local list, then 1447 // from the global list, and only after those attempts fail will the 1448 // thread attempt to instantiate new monitors. Thread-local free lists 1449 // improve allocation latency, as well as reducing coherency traffic 1450 // on the shared global list. 1451 m = take_from_start_of_om_free_list(self); 1452 if (m != NULL) { 1453 guarantee(m->object() == NULL, "invariant"); 1454 m->set_allocation_state(ObjectMonitor::New); 1455 prepend_to_om_in_use_list(self, m); 1456 return m; 1457 } 1458 1459 // 2: try to allocate from the global g_free_list 1460 // CONSIDER: use muxTry() instead of muxAcquire(). 1461 // If the muxTry() fails then drop immediately into case 3. 1462 // If we're using thread-local free lists then try 1463 // to reprovision the caller's free list. 1464 if (g_free_list != NULL) { 1465 // Reprovision the thread's om_free_list. 1466 // Use bulk transfers to reduce the allocation rate and heat 1467 // on various locks. 1468 for (int i = self->om_free_provision; --i >= 0;) { 1469 ObjectMonitor* take = take_from_start_of_g_free_list(); 1470 if (take == NULL) { 1471 break; // No more are available. 1472 } 1473 guarantee(take->object() == NULL, "invariant"); 1474 if (AsyncDeflateIdleMonitors) { 1475 // We allowed 3 field values to linger during async deflation. 1476 // We clear header and restore ref_count here, but we leave 1477 // owner == DEFLATER_MARKER so the simple C2 ObjectMonitor 1478 // enter optimization can no longer race with async deflation 1479 // and reuse. 1480 take->set_header(markWord::zero()); 1481 if (take->ref_count() < 0) { 1482 // Add back max_jint to restore the ref_count field to its 1483 // proper value. 1484 Atomic::add(max_jint, &take->_ref_count); 1485 1486 DEBUG_ONLY(jint l_ref_count = take->ref_count();) 1487 assert(l_ref_count >= 0, "must not be negative: l_ref_count=%d, ref_count=%d", 1488 l_ref_count, take->ref_count()); 1489 } 1490 } 1491 take->Recycle(); 1492 // Since we're taking from the global free-list, take must be Free. 1493 // om_release() also sets the allocation state to Free because it 1494 // is called from other code paths. 1495 assert(take->is_free(), "invariant"); 1496 om_release(self, take, false); 1497 } 1498 self->om_free_provision += 1 + (self->om_free_provision / 2); 1499 if (self->om_free_provision > MAXPRIVATE) self->om_free_provision = MAXPRIVATE; 1500 1501 if (!AsyncDeflateIdleMonitors && 1502 is_MonitorBound_exceeded(g_om_population - g_om_free_count)) { 1503 // Not enough ObjectMonitors on the global free list. 1504 // We can't safely induce a STW safepoint from om_alloc() as our thread 1505 // state may not be appropriate for such activities and callers may hold 1506 // naked oops, so instead we defer the action. 1507 InduceScavenge(self, "om_alloc"); 1508 } 1509 continue; 1510 } 1511 1512 // 3: allocate a block of new ObjectMonitors 1513 // Both the local and global free lists are empty -- resort to malloc(). 1514 // In the current implementation ObjectMonitors are TSM - immortal. 1515 // Ideally, we'd write "new ObjectMonitor[_BLOCKSIZE], but we want 1516 // each ObjectMonitor to start at the beginning of a cache line, 1517 // so we use align_up(). 1518 // A better solution would be to use C++ placement-new. 1519 // BEWARE: As it stands currently, we don't run the ctors! 1520 assert(_BLOCKSIZE > 1, "invariant"); 1521 size_t neededsize = sizeof(PaddedObjectMonitor) * _BLOCKSIZE; 1522 PaddedObjectMonitor* temp; 1523 size_t aligned_size = neededsize + (OM_CACHE_LINE_SIZE - 1); 1524 void* real_malloc_addr = NEW_C_HEAP_ARRAY(char, aligned_size, mtInternal); 1525 temp = (PaddedObjectMonitor*)align_up(real_malloc_addr, OM_CACHE_LINE_SIZE); 1526 (void)memset((void *) temp, 0, neededsize); 1527 1528 // Format the block. 1529 // initialize the linked list, each monitor points to its next 1530 // forming the single linked free list, the very first monitor 1531 // will points to next block, which forms the block list. 1532 // The trick of using the 1st element in the block as g_block_list 1533 // linkage should be reconsidered. A better implementation would 1534 // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; } 1535 1536 for (int i = 1; i < _BLOCKSIZE; i++) { 1537 temp[i]._next_om = (ObjectMonitor*)&temp[i + 1]; 1538 assert(temp[i].is_free(), "invariant"); 1539 } 1540 1541 // terminate the last monitor as the end of list 1542 temp[_BLOCKSIZE - 1]._next_om = (ObjectMonitor*)NULL; 1543 1544 // Element [0] is reserved for global list linkage 1545 temp[0].set_object(CHAINMARKER); 1546 1547 // Consider carving out this thread's current request from the 1548 // block in hand. This avoids some lock traffic and redundant 1549 // list activity. 1550 1551 prepend_block_to_lists(temp); 1552 } 1553 } 1554 1555 // Place "m" on the caller's private per-thread om_free_list. 1556 // In practice there's no need to clamp or limit the number of 1557 // monitors on a thread's om_free_list as the only non-allocation time 1558 // we'll call om_release() is to return a monitor to the free list after 1559 // a CAS attempt failed. This doesn't allow unbounded #s of monitors to 1560 // accumulate on a thread's free list. 1561 // 1562 // Key constraint: all ObjectMonitors on a thread's free list and the global 1563 // free list must have their object field set to null. This prevents the 1564 // scavenger -- deflate_monitor_list() or deflate_monitor_list_using_JT() 1565 // -- from reclaiming them while we are trying to release them. 1566 1567 void ObjectSynchronizer::om_release(Thread* self, ObjectMonitor* m, 1568 bool from_per_thread_alloc) { 1569 guarantee(m->header().value() == 0, "invariant"); 1570 guarantee(m->object() == NULL, "invariant"); 1571 stringStream ss; 1572 guarantee((m->is_busy() | m->_recursions) == 0, "freeing in-use monitor: " 1573 "%s, recursions=" INTX_FORMAT, m->is_busy_to_string(&ss), 1574 m->_recursions); 1575 m->set_allocation_state(ObjectMonitor::Free); 1576 // _next_om is used for both per-thread in-use and free lists so 1577 // we have to remove 'm' from the in-use list first (as needed). 1578 if (from_per_thread_alloc) { 1579 // Need to remove 'm' from om_in_use_list. 1580 // We use the more complicated mark-cur_mid_in_use-and-mid-as-we-go 1581 // protocol because async deflation can do list deletions in parallel. 1582 ObjectMonitor* cur_mid_in_use = NULL; 1583 ObjectMonitor* mid = NULL; 1584 ObjectMonitor* next = NULL; 1585 bool extracted = false; 1586 1587 if (!mark_list_head(&self->om_in_use_list, &mid, &next)) { 1588 fatal("thread=" INTPTR_FORMAT " in-use list must not be empty.", p2i(self)); 1589 } 1590 while (true) { 1591 if (m == mid) { 1592 // We found 'm' on the per-thread in-use list so try to extract it. 1593 if (cur_mid_in_use == NULL) { 1594 // mid is the list head and it is marked. Switch the list head 1595 // to next which unmarks the list head, but leaves mid marked: 1596 self->om_in_use_list = next; 1597 // mark_list_head() used cmpxchg() above, switching list head can be lazier: 1598 OrderAccess::storestore(); 1599 } else { 1600 // mid and cur_mid_in_use are marked. Switch cur_mid_in_use's 1601 // next field to next which unmarks cur_mid_in_use, but leaves 1602 // mid marked: 1603 OrderAccess::release_store(&cur_mid_in_use->_next_om, next); 1604 } 1605 extracted = true; 1606 Atomic::dec(&self->om_in_use_count); 1607 // Unmark mid, but leave the next value for any lagging list 1608 // walkers. It will get cleaned up when mid is prepended to 1609 // the thread's free list: 1610 set_next(mid, next); 1611 break; 1612 } 1613 if (cur_mid_in_use != NULL) { 1614 set_next(cur_mid_in_use, mid); // umark cur_mid_in_use 1615 } 1616 // The next cur_mid_in_use keeps mid's marked next field so 1617 // that it is stable for a possible next field change. It 1618 // cannot be deflated while it is marked. 1619 cur_mid_in_use = mid; 1620 mid = next; 1621 if (mid == NULL) { 1622 // Reached end of the list and didn't find m so: 1623 fatal("must find m=" INTPTR_FORMAT "on om_in_use_list=" INTPTR_FORMAT, 1624 p2i(m), p2i(self->om_in_use_list)); 1625 } 1626 // Mark mid's next field so we can possibly extract it: 1627 next = mark_next_loop(mid); 1628 } 1629 } 1630 1631 prepend_to_om_free_list(self, m); 1632 guarantee(m->is_free(), "invariant"); 1633 } 1634 1635 // Return ObjectMonitors on a moribund thread's free and in-use 1636 // lists to the appropriate global lists. The ObjectMonitors on the 1637 // per-thread in-use list may still be in use by other threads. 1638 // 1639 // We currently call om_flush() from Threads::remove() before the 1640 // thread has been excised from the thread list and is no longer a 1641 // mutator. This means that om_flush() cannot run concurrently with 1642 // a safepoint and interleave with deflate_idle_monitors(). In 1643 // particular, this ensures that the thread's in-use monitors are 1644 // scanned by a GC safepoint, either via Thread::oops_do() (before 1645 // om_flush() is called) or via ObjectSynchronizer::oops_do() (after 1646 // om_flush() is called). 1647 // 1648 // With AsyncDeflateIdleMonitors, deflate_global_idle_monitors_using_JT() 1649 // and deflate_per_thread_idle_monitors_using_JT() (in another thread) can 1650 // run at the same time as om_flush() so we have to follow a careful 1651 // protocol to prevent list corruption. 1652 1653 void ObjectSynchronizer::om_flush(Thread* self) { 1654 // This function can race with an async deflater thread. Since 1655 // deflation has to process the per-thread in-use list before 1656 // prepending the deflated ObjectMonitors to the global free list, 1657 // we process the per-thread lists in the same order to prevent 1658 // ordering races. 1659 int in_use_count = 0; 1660 ObjectMonitor* in_use_list = NULL; 1661 ObjectMonitor* in_use_tail = NULL; 1662 ObjectMonitor* next = NULL; 1663 1664 // An async deflation thread checks to see if the target thread 1665 // is exiting, but if it has made it past that check before we 1666 // started exiting, then it is racing to get to the in-use list. 1667 if (mark_list_head(&self->om_in_use_list, &in_use_list, &next)) { 1668 // At this point, we have marked the in-use list head so an 1669 // async deflation thread cannot come in after us. If an async 1670 // deflation thread is ahead of us, then we'll detect that and 1671 // wait for it to finish its work. 1672 // 1673 // The thread is going away, however the ObjectMonitors on the 1674 // om_in_use_list may still be in-use by other threads. Link 1675 // them to in_use_tail, which will be linked into the global 1676 // in-use list g_om_in_use_list below. 1677 // 1678 // Account for the in-use list head before the loop since it is 1679 // already marked (by this thread): 1680 in_use_tail = in_use_list; 1681 in_use_count++; 1682 for (ObjectMonitor* cur_om = unmarked_next(in_use_list); cur_om != NULL;) { 1683 if (is_next_marked(cur_om)) { 1684 // This next field is marked so there must be an async deflater 1685 // thread ahead of us so we'll give it a chance to finish. 1686 while (is_next_marked(cur_om)) { 1687 os::naked_short_sleep(1); 1688 } 1689 // Refetch the possibly changed next field and try again. 1690 cur_om = unmarked_next(in_use_tail); 1691 continue; 1692 } 1693 if (cur_om->is_free()) { 1694 // cur_om was deflated and the allocation state was changed 1695 // to Free while it was marked. We happened to see it just 1696 // after it was unmarked (and added to the free list). 1697 // Refetch the possibly changed next field and try again. 1698 cur_om = unmarked_next(in_use_tail); 1699 continue; 1700 } 1701 in_use_tail = cur_om; 1702 in_use_count++; 1703 cur_om = unmarked_next(cur_om); 1704 } 1705 guarantee(in_use_tail != NULL, "invariant"); 1706 int l_om_in_use_count = self->om_in_use_count; 1707 ADIM_guarantee(l_om_in_use_count == in_use_count, "in-use counts don't " 1708 "match: l_om_in_use_count=%d, in_use_count=%d", 1709 l_om_in_use_count, in_use_count); 1710 self->om_in_use_count = 0; 1711 // Clear the in-use list head (which also unmarks it): 1712 self->om_in_use_list = (ObjectMonitor*)NULL; 1713 // mark_list_head() used cmpxchg() above, clearing the disconnected list head can be lazier: 1714 OrderAccess::storestore(); 1715 set_next(in_use_list, next); 1716 } 1717 1718 int free_count = 0; 1719 ObjectMonitor* free_list = self->om_free_list; 1720 ObjectMonitor* free_tail = NULL; 1721 if (free_list != NULL) { 1722 // The thread is going away. Set 'free_tail' to the last per-thread free 1723 // monitor which will be linked to g_free_list below. 1724 stringStream ss; 1725 for (ObjectMonitor* s = free_list; s != NULL; s = unmarked_next(s)) { 1726 free_count++; 1727 free_tail = s; 1728 guarantee(s->object() == NULL, "invariant"); 1729 guarantee(!s->is_busy(), "must be !is_busy: %s", s->is_busy_to_string(&ss)); 1730 } 1731 guarantee(free_tail != NULL, "invariant"); 1732 int l_om_free_count = self->om_free_count; 1733 ADIM_guarantee(l_om_free_count == free_count, "free counts don't match: " 1734 "l_om_free_count=%d, free_count=%d", l_om_free_count, 1735 free_count); 1736 self->om_free_count = 0; 1737 self->om_free_list = NULL; 1738 OrderAccess::storestore(); // Lazier memory is okay for list walkers. 1739 } 1740 1741 if (free_tail != NULL) { 1742 prepend_list_to_g_free_list(free_list, free_tail, free_count); 1743 } 1744 1745 if (in_use_tail != NULL) { 1746 prepend_list_to_g_om_in_use_list(in_use_list, in_use_tail, in_use_count); 1747 } 1748 1749 LogStreamHandle(Debug, monitorinflation) lsh_debug; 1750 LogStreamHandle(Info, monitorinflation) lsh_info; 1751 LogStream* ls = NULL; 1752 if (log_is_enabled(Debug, monitorinflation)) { 1753 ls = &lsh_debug; 1754 } else if ((free_count != 0 || in_use_count != 0) && 1755 log_is_enabled(Info, monitorinflation)) { 1756 ls = &lsh_info; 1757 } 1758 if (ls != NULL) { 1759 ls->print_cr("om_flush: jt=" INTPTR_FORMAT ", free_count=%d" 1760 ", in_use_count=%d" ", om_free_provision=%d", 1761 p2i(self), free_count, in_use_count, self->om_free_provision); 1762 } 1763 } 1764 1765 static void post_monitor_inflate_event(EventJavaMonitorInflate* event, 1766 const oop obj, 1767 ObjectSynchronizer::InflateCause cause) { 1768 assert(event != NULL, "invariant"); 1769 assert(event->should_commit(), "invariant"); 1770 event->set_monitorClass(obj->klass()); 1771 event->set_address((uintptr_t)(void*)obj); 1772 event->set_cause((u1)cause); 1773 event->commit(); 1774 } 1775 1776 // Fast path code shared by multiple functions 1777 void ObjectSynchronizer::inflate_helper(ObjectMonitorHandle* omh_p, oop obj) { 1778 while (true) { 1779 markWord mark = obj->mark(); 1780 if (mark.has_monitor()) { 1781 if (!omh_p->save_om_ptr(obj, mark)) { 1782 // Lost a race with async deflation so try again. 1783 assert(AsyncDeflateIdleMonitors, "sanity check"); 1784 continue; 1785 } 1786 ObjectMonitor* monitor = omh_p->om_ptr(); 1787 assert(ObjectSynchronizer::verify_objmon_isinpool(monitor), "monitor is invalid"); 1788 markWord dmw = monitor->header(); 1789 assert(dmw.is_neutral(), "sanity check: header=" INTPTR_FORMAT, dmw.value()); 1790 return; 1791 } 1792 inflate(omh_p, Thread::current(), obj, inflate_cause_vm_internal); 1793 return; 1794 } 1795 } 1796 1797 void ObjectSynchronizer::inflate(ObjectMonitorHandle* omh_p, Thread* self, 1798 oop object, const InflateCause cause) { 1799 // Inflate mutates the heap ... 1800 // Relaxing assertion for bug 6320749. 1801 assert(Universe::verify_in_progress() || 1802 !SafepointSynchronize::is_at_safepoint(), "invariant"); 1803 1804 EventJavaMonitorInflate event; 1805 1806 for (;;) { 1807 const markWord mark = object->mark(); 1808 assert(!mark.has_bias_pattern(), "invariant"); 1809 1810 // The mark can be in one of the following states: 1811 // * Inflated - just return 1812 // * Stack-locked - coerce it to inflated 1813 // * INFLATING - busy wait for conversion to complete 1814 // * Neutral - aggressively inflate the object. 1815 // * BIASED - Illegal. We should never see this 1816 1817 // CASE: inflated 1818 if (mark.has_monitor()) { 1819 if (!omh_p->save_om_ptr(object, mark)) { 1820 // Lost a race with async deflation so try again. 1821 assert(AsyncDeflateIdleMonitors, "sanity check"); 1822 continue; 1823 } 1824 ObjectMonitor* inf = omh_p->om_ptr(); 1825 markWord dmw = inf->header(); 1826 assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value()); 1827 assert(inf->object() == object, "invariant"); 1828 assert(ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid"); 1829 return; 1830 } 1831 1832 // CASE: inflation in progress - inflating over a stack-lock. 1833 // Some other thread is converting from stack-locked to inflated. 1834 // Only that thread can complete inflation -- other threads must wait. 1835 // The INFLATING value is transient. 1836 // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish. 1837 // We could always eliminate polling by parking the thread on some auxiliary list. 1838 if (mark == markWord::INFLATING()) { 1839 read_stable_mark(object); 1840 continue; 1841 } 1842 1843 // CASE: stack-locked 1844 // Could be stack-locked either by this thread or by some other thread. 1845 // 1846 // Note that we allocate the objectmonitor speculatively, _before_ attempting 1847 // to install INFLATING into the mark word. We originally installed INFLATING, 1848 // allocated the objectmonitor, and then finally STed the address of the 1849 // objectmonitor into the mark. This was correct, but artificially lengthened 1850 // the interval in which INFLATED appeared in the mark, thus increasing 1851 // the odds of inflation contention. 1852 // 1853 // We now use per-thread private objectmonitor free lists. 1854 // These list are reprovisioned from the global free list outside the 1855 // critical INFLATING...ST interval. A thread can transfer 1856 // multiple objectmonitors en-mass from the global free list to its local free list. 1857 // This reduces coherency traffic and lock contention on the global free list. 1858 // Using such local free lists, it doesn't matter if the om_alloc() call appears 1859 // before or after the CAS(INFLATING) operation. 1860 // See the comments in om_alloc(). 1861 1862 LogStreamHandle(Trace, monitorinflation) lsh; 1863 1864 if (mark.has_locker()) { 1865 ObjectMonitor* m = om_alloc(self, cause); 1866 // Optimistically prepare the objectmonitor - anticipate successful CAS 1867 // We do this before the CAS in order to minimize the length of time 1868 // in which INFLATING appears in the mark. 1869 m->Recycle(); 1870 m->_Responsible = NULL; 1871 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit; // Consider: maintain by type/class 1872 1873 markWord cmp = object->cas_set_mark(markWord::INFLATING(), mark); 1874 if (cmp != mark) { 1875 // om_release() will reset the allocation state from New to Free. 1876 om_release(self, m, true); 1877 continue; // Interference -- just retry 1878 } 1879 1880 // We've successfully installed INFLATING (0) into the mark-word. 1881 // This is the only case where 0 will appear in a mark-word. 1882 // Only the singular thread that successfully swings the mark-word 1883 // to 0 can perform (or more precisely, complete) inflation. 1884 // 1885 // Why do we CAS a 0 into the mark-word instead of just CASing the 1886 // mark-word from the stack-locked value directly to the new inflated state? 1887 // Consider what happens when a thread unlocks a stack-locked object. 1888 // It attempts to use CAS to swing the displaced header value from the 1889 // on-stack BasicLock back into the object header. Recall also that the 1890 // header value (hash code, etc) can reside in (a) the object header, or 1891 // (b) a displaced header associated with the stack-lock, or (c) a displaced 1892 // header in an ObjectMonitor. The inflate() routine must copy the header 1893 // value from the BasicLock on the owner's stack to the ObjectMonitor, all 1894 // the while preserving the hashCode stability invariants. If the owner 1895 // decides to release the lock while the value is 0, the unlock will fail 1896 // and control will eventually pass from slow_exit() to inflate. The owner 1897 // will then spin, waiting for the 0 value to disappear. Put another way, 1898 // the 0 causes the owner to stall if the owner happens to try to 1899 // drop the lock (restoring the header from the BasicLock to the object) 1900 // while inflation is in-progress. This protocol avoids races that might 1901 // would otherwise permit hashCode values to change or "flicker" for an object. 1902 // Critically, while object->mark is 0 mark.displaced_mark_helper() is stable. 1903 // 0 serves as a "BUSY" inflate-in-progress indicator. 1904 1905 1906 // fetch the displaced mark from the owner's stack. 1907 // The owner can't die or unwind past the lock while our INFLATING 1908 // object is in the mark. Furthermore the owner can't complete 1909 // an unlock on the object, either. 1910 markWord dmw = mark.displaced_mark_helper(); 1911 // Catch if the object's header is not neutral (not locked and 1912 // not marked is what we care about here). 1913 ADIM_guarantee(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value()); 1914 1915 // Setup monitor fields to proper values -- prepare the monitor 1916 m->set_header(dmw); 1917 1918 // Optimization: if the mark.locker stack address is associated 1919 // with this thread we could simply set m->_owner = self. 1920 // Note that a thread can inflate an object 1921 // that it has stack-locked -- as might happen in wait() -- directly 1922 // with CAS. That is, we can avoid the xchg-NULL .... ST idiom. 1923 if (AsyncDeflateIdleMonitors) { 1924 m->simply_set_owner_from(mark.locker(), NULL, DEFLATER_MARKER); 1925 } else { 1926 m->simply_set_owner_from(mark.locker(), NULL); 1927 } 1928 m->set_object(object); 1929 // TODO-FIXME: assert BasicLock->dhw != 0. 1930 1931 omh_p->set_om_ptr(m); 1932 1933 // Must preserve store ordering. The monitor state must 1934 // be stable at the time of publishing the monitor address. 1935 guarantee(object->mark() == markWord::INFLATING(), "invariant"); 1936 object->release_set_mark(markWord::encode(m)); 1937 1938 // Once ObjectMonitor is configured and the object is associated 1939 // with the ObjectMonitor, it is safe to allow async deflation: 1940 assert(m->is_new(), "freshly allocated monitor must be new"); 1941 m->set_allocation_state(ObjectMonitor::Old); 1942 1943 // Hopefully the performance counters are allocated on distinct cache lines 1944 // to avoid false sharing on MP systems ... 1945 OM_PERFDATA_OP(Inflations, inc()); 1946 if (log_is_enabled(Trace, monitorinflation)) { 1947 ResourceMark rm(self); 1948 lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark=" 1949 INTPTR_FORMAT ", type='%s'", p2i(object), 1950 object->mark().value(), object->klass()->external_name()); 1951 } 1952 if (event.should_commit()) { 1953 post_monitor_inflate_event(&event, object, cause); 1954 } 1955 ADIM_guarantee(!m->is_free(), "inflated monitor to be returned cannot be free"); 1956 return; 1957 } 1958 1959 // CASE: neutral 1960 // TODO-FIXME: for entry we currently inflate and then try to CAS _owner. 1961 // If we know we're inflating for entry it's better to inflate by swinging a 1962 // pre-locked ObjectMonitor pointer into the object header. A successful 1963 // CAS inflates the object *and* confers ownership to the inflating thread. 1964 // In the current implementation we use a 2-step mechanism where we CAS() 1965 // to inflate and then CAS() again to try to swing _owner from NULL to self. 1966 // An inflateTry() method that we could call from enter() would be useful. 1967 1968 // Catch if the object's header is not neutral (not locked and 1969 // not marked is what we care about here). 1970 ADIM_guarantee(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT,mark.value()); 1971 ObjectMonitor* m = om_alloc(self, cause); 1972 // prepare m for installation - set monitor to initial state 1973 m->Recycle(); 1974 m->set_header(mark); 1975 // If we leave _owner == DEFLATER_MARKER here, then the simple C2 1976 // ObjectMonitor enter optimization can no longer race with async 1977 // deflation and reuse. 1978 m->set_object(object); 1979 m->_Responsible = NULL; 1980 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit; // consider: keep metastats by type/class 1981 1982 omh_p->set_om_ptr(m); 1983 1984 if (object->cas_set_mark(markWord::encode(m), mark) != mark) { 1985 m->set_header(markWord::zero()); 1986 m->set_object(NULL); 1987 m->Recycle(); 1988 omh_p->set_om_ptr(NULL); 1989 // om_release() will reset the allocation state from New to Free. 1990 om_release(self, m, true); 1991 m = NULL; 1992 continue; 1993 // interference - the markword changed - just retry. 1994 // The state-transitions are one-way, so there's no chance of 1995 // live-lock -- "Inflated" is an absorbing state. 1996 } 1997 1998 // Once the ObjectMonitor is configured and object is associated 1999 // with the ObjectMonitor, it is safe to allow async deflation: 2000 assert(m->is_new(), "freshly allocated monitor must be new"); 2001 m->set_allocation_state(ObjectMonitor::Old); 2002 2003 // Hopefully the performance counters are allocated on distinct 2004 // cache lines to avoid false sharing on MP systems ... 2005 OM_PERFDATA_OP(Inflations, inc()); 2006 if (log_is_enabled(Trace, monitorinflation)) { 2007 ResourceMark rm(self); 2008 lsh.print_cr("inflate(neutral): object=" INTPTR_FORMAT ", mark=" 2009 INTPTR_FORMAT ", type='%s'", p2i(object), 2010 object->mark().value(), object->klass()->external_name()); 2011 } 2012 if (event.should_commit()) { 2013 post_monitor_inflate_event(&event, object, cause); 2014 } 2015 ADIM_guarantee(!m->is_free(), "inflated monitor to be returned cannot be free"); 2016 return; 2017 } 2018 } 2019 2020 2021 // We maintain a list of in-use monitors for each thread. 2022 // 2023 // For safepoint based deflation: 2024 // deflate_thread_local_monitors() scans a single thread's in-use list, while 2025 // deflate_idle_monitors() scans only a global list of in-use monitors which 2026 // is populated only as a thread dies (see om_flush()). 2027 // 2028 // These operations are called at all safepoints, immediately after mutators 2029 // are stopped, but before any objects have moved. Collectively they traverse 2030 // the population of in-use monitors, deflating where possible. The scavenged 2031 // monitors are returned to the global monitor free list. 2032 // 2033 // Beware that we scavenge at *every* stop-the-world point. Having a large 2034 // number of monitors in-use could negatively impact performance. We also want 2035 // to minimize the total # of monitors in circulation, as they incur a small 2036 // footprint penalty. 2037 // 2038 // Perversely, the heap size -- and thus the STW safepoint rate -- 2039 // typically drives the scavenge rate. Large heaps can mean infrequent GC, 2040 // which in turn can mean large(r) numbers of ObjectMonitors in circulation. 2041 // This is an unfortunate aspect of this design. 2042 // 2043 // For async deflation: 2044 // If a special deflation request is made, then the safepoint based 2045 // deflation mechanism is used. Otherwise, an async deflation request 2046 // is registered with the ServiceThread and it is notified. 2047 2048 void ObjectSynchronizer::do_safepoint_work(DeflateMonitorCounters* counters) { 2049 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 2050 2051 // The per-thread in-use lists are handled in 2052 // ParallelSPCleanupThreadClosure::do_thread(). 2053 2054 if (!AsyncDeflateIdleMonitors || is_special_deflation_requested()) { 2055 // Use the older mechanism for the global in-use list or if a 2056 // special deflation has been requested before the safepoint. 2057 ObjectSynchronizer::deflate_idle_monitors(counters); 2058 return; 2059 } 2060 2061 log_debug(monitorinflation)("requesting async deflation of idle monitors."); 2062 // Request deflation of idle monitors by the ServiceThread: 2063 set_is_async_deflation_requested(true); 2064 MonitorLocker ml(Service_lock, Mutex::_no_safepoint_check_flag); 2065 ml.notify_all(); 2066 2067 if (log_is_enabled(Debug, monitorinflation)) { 2068 // exit_globals()'s call to audit_and_print_stats() is done 2069 // at the Info level and not at a safepoint. 2070 // For safepoint based deflation, audit_and_print_stats() is called 2071 // in ObjectSynchronizer::finish_deflate_idle_monitors() at the 2072 // Debug level at a safepoint. 2073 ObjectSynchronizer::audit_and_print_stats(false /* on_exit */); 2074 } 2075 } 2076 2077 // Deflate a single monitor if not in-use 2078 // Return true if deflated, false if in-use 2079 bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj, 2080 ObjectMonitor** free_head_p, 2081 ObjectMonitor** free_tail_p) { 2082 bool deflated; 2083 // Normal case ... The monitor is associated with obj. 2084 const markWord mark = obj->mark(); 2085 guarantee(mark == markWord::encode(mid), "should match: mark=" 2086 INTPTR_FORMAT ", encoded mid=" INTPTR_FORMAT, mark.value(), 2087 markWord::encode(mid).value()); 2088 // Make sure that mark.monitor() and markWord::encode() agree: 2089 guarantee(mark.monitor() == mid, "should match: monitor()=" INTPTR_FORMAT 2090 ", mid=" INTPTR_FORMAT, p2i(mark.monitor()), p2i(mid)); 2091 const markWord dmw = mid->header(); 2092 guarantee(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value()); 2093 2094 if (mid->is_busy() || mid->ref_count() != 0) { 2095 // Easy checks are first - the ObjectMonitor is busy or ObjectMonitor* 2096 // is in use so no deflation. 2097 deflated = false; 2098 } else { 2099 // Deflate the monitor if it is no longer being used 2100 // It's idle - scavenge and return to the global free list 2101 // plain old deflation ... 2102 if (log_is_enabled(Trace, monitorinflation)) { 2103 ResourceMark rm; 2104 log_trace(monitorinflation)("deflate_monitor: " 2105 "object=" INTPTR_FORMAT ", mark=" 2106 INTPTR_FORMAT ", type='%s'", p2i(obj), 2107 mark.value(), obj->klass()->external_name()); 2108 } 2109 2110 // Restore the header back to obj 2111 obj->release_set_mark(dmw); 2112 if (AsyncDeflateIdleMonitors) { 2113 // clear() expects the owner field to be NULL and we won't race 2114 // with the simple C2 ObjectMonitor enter optimization since 2115 // we're at a safepoint. DEFLATER_MARKER is the only non-NULL 2116 // value we should see here. 2117 mid->try_set_owner_from(NULL, DEFLATER_MARKER); 2118 } 2119 mid->clear(); 2120 2121 assert(mid->object() == NULL, "invariant: object=" INTPTR_FORMAT, 2122 p2i(mid->object())); 2123 assert(mid->is_free(), "invariant"); 2124 2125 // Move the deflated ObjectMonitor to the working free list 2126 // defined by free_head_p and free_tail_p. No races on this list 2127 // so no need for load_acquire() or store_release(). 2128 if (*free_head_p == NULL) *free_head_p = mid; 2129 if (*free_tail_p != NULL) { 2130 // We append to the list so the caller can use mid->_next_om 2131 // to fix the linkages in its context. 2132 ObjectMonitor* prevtail = *free_tail_p; 2133 // Should have been cleaned up by the caller: 2134 // Note: Should not have to mark prevtail here since we're at a 2135 // safepoint and ObjectMonitors on the local free list should 2136 // not be accessed in parallel. 2137 assert(prevtail->_next_om == NULL, "must be NULL: _next_om=" 2138 INTPTR_FORMAT, p2i(prevtail->_next_om)); 2139 set_next(prevtail, mid); 2140 } 2141 *free_tail_p = mid; 2142 // At this point, mid->_next_om still refers to its current 2143 // value and another ObjectMonitor's _next_om field still 2144 // refers to this ObjectMonitor. Those linkages have to be 2145 // cleaned up by the caller who has the complete context. 2146 deflated = true; 2147 } 2148 return deflated; 2149 } 2150 2151 // Deflate the specified ObjectMonitor if not in-use using a JavaThread. 2152 // Returns true if it was deflated and false otherwise. 2153 // 2154 // The async deflation protocol sets owner to DEFLATER_MARKER and 2155 // makes ref_count negative as signals to contending threads that 2156 // an async deflation is in progress. There are a number of checks 2157 // as part of the protocol to make sure that the calling thread has 2158 // not lost the race to a contending thread or to a thread that just 2159 // wants to use the ObjectMonitor*. 2160 // 2161 // The ObjectMonitor has been successfully async deflated when: 2162 // (owner == DEFLATER_MARKER && ref_count < 0) 2163 // Contending threads or ObjectMonitor* using threads that see those 2164 // values know to retry their operation. 2165 // 2166 bool ObjectSynchronizer::deflate_monitor_using_JT(ObjectMonitor* mid, 2167 ObjectMonitor** free_head_p, 2168 ObjectMonitor** free_tail_p) { 2169 assert(AsyncDeflateIdleMonitors, "sanity check"); 2170 assert(Thread::current()->is_Java_thread(), "precondition"); 2171 // A newly allocated ObjectMonitor should not be seen here so we 2172 // avoid an endless inflate/deflate cycle. 2173 assert(mid->is_old(), "must be old: allocation_state=%d", 2174 (int) mid->allocation_state()); 2175 2176 if (mid->is_busy() || mid->ref_count() != 0) { 2177 // Easy checks are first - the ObjectMonitor is busy or ObjectMonitor* 2178 // is in use so no deflation. 2179 return false; 2180 } 2181 2182 if (mid->try_set_owner_from(DEFLATER_MARKER, NULL) == NULL) { 2183 // ObjectMonitor is not owned by another thread. Our setting 2184 // owner to DEFLATER_MARKER forces any contending thread through 2185 // the slow path. This is just the first part of the async 2186 // deflation dance. 2187 2188 if (mid->_contentions != 0 || mid->_waiters != 0) { 2189 // Another thread has raced to enter the ObjectMonitor after 2190 // mid->is_busy() above or has already entered and waited on 2191 // it which makes it busy so no deflation. Restore owner to 2192 // NULL if it is still DEFLATER_MARKER. 2193 mid->try_set_owner_from(NULL, DEFLATER_MARKER); 2194 return false; 2195 } 2196 2197 if (Atomic::cmpxchg(-max_jint, &mid->_ref_count, (jint)0) == 0) { 2198 // Make ref_count negative to force any contending threads or 2199 // ObjectMonitor* using threads to retry. This is the second 2200 // part of the async deflation dance. 2201 2202 if (mid->owner_is_DEFLATER_MARKER()) { 2203 // If owner is still DEFLATER_MARKER, then we have successfully 2204 // signaled any contending threads to retry. If it is not, then we 2205 // have lost the race to an entering thread and the ObjectMonitor 2206 // is now busy. This is the third and final part of the async 2207 // deflation dance. 2208 // Note: This owner check solves the ABA problem with ref_count 2209 // where another thread acquired the ObjectMonitor, finished 2210 // using it and restored the ref_count to zero. 2211 2212 // Sanity checks for the races: 2213 guarantee(mid->_contentions == 0, "must be 0: contentions=%d", 2214 mid->_contentions); 2215 guarantee(mid->_waiters == 0, "must be 0: waiters=%d", mid->_waiters); 2216 guarantee(mid->_cxq == NULL, "must be no contending threads: cxq=" 2217 INTPTR_FORMAT, p2i(mid->_cxq)); 2218 guarantee(mid->_EntryList == NULL, 2219 "must be no entering threads: EntryList=" INTPTR_FORMAT, 2220 p2i(mid->_EntryList)); 2221 2222 const oop obj = (oop) mid->object(); 2223 if (log_is_enabled(Trace, monitorinflation)) { 2224 ResourceMark rm; 2225 log_trace(monitorinflation)("deflate_monitor_using_JT: " 2226 "object=" INTPTR_FORMAT ", mark=" 2227 INTPTR_FORMAT ", type='%s'", 2228 p2i(obj), obj->mark().value(), 2229 obj->klass()->external_name()); 2230 } 2231 2232 // Install the old mark word if nobody else has already done it. 2233 mid->install_displaced_markword_in_object(obj); 2234 mid->clear_using_JT(); 2235 2236 assert(mid->object() == NULL, "must be NULL: object=" INTPTR_FORMAT, 2237 p2i(mid->object())); 2238 assert(mid->is_free(), "must be free: allocation_state=%d", 2239 (int) mid->allocation_state()); 2240 2241 // Move the deflated ObjectMonitor to the working free list 2242 // defined by free_head_p and free_tail_p. No races on this list 2243 // so no need for load_acquire() or store_release(). 2244 if (*free_head_p == NULL) { 2245 // First one on the list. 2246 *free_head_p = mid; 2247 } 2248 if (*free_tail_p != NULL) { 2249 // We append to the list so the caller can use mid->_next_om 2250 // to fix the linkages in its context. 2251 ObjectMonitor* prevtail = *free_tail_p; 2252 // Should have been cleaned up by the caller: 2253 ObjectMonitor* next = mark_next_loop(prevtail); 2254 assert(unmarked_next(prevtail) == NULL, "must be NULL: _next_om=" 2255 INTPTR_FORMAT, p2i(unmarked_next(prevtail))); 2256 set_next(prevtail, mid); // prevtail now points to mid (and is unmarked) 2257 } 2258 *free_tail_p = mid; 2259 2260 // At this point, mid->_next_om still refers to its current 2261 // value and another ObjectMonitor's _next_om field still 2262 // refers to this ObjectMonitor. Those linkages have to be 2263 // cleaned up by the caller who has the complete context. 2264 2265 // We leave owner == DEFLATER_MARKER and ref_count < 0 2266 // to force any racing threads to retry. 2267 return true; // Success, ObjectMonitor has been deflated. 2268 } 2269 2270 // The owner was changed from DEFLATER_MARKER so we lost the 2271 // race since the ObjectMonitor is now busy. 2272 2273 // Add back max_jint to restore the ref_count field to its 2274 // proper value (which may not be what we saw above): 2275 Atomic::add(max_jint, &mid->_ref_count); 2276 2277 DEBUG_ONLY(jint l_ref_count = mid->ref_count();) 2278 assert(l_ref_count >= 0, "must not be negative: l_ref_count=%d, ref_count=%d", 2279 l_ref_count, mid->ref_count()); 2280 return false; 2281 } 2282 2283 // The ref_count was no longer 0 so we lost the race since the 2284 // ObjectMonitor is now busy or the ObjectMonitor* is now is use. 2285 // Restore owner to NULL if it is still DEFLATER_MARKER: 2286 mid->try_set_owner_from(NULL, DEFLATER_MARKER); 2287 } 2288 2289 // The owner field is no longer NULL so we lost the race since the 2290 // ObjectMonitor is now busy. 2291 return false; 2292 } 2293 2294 // Walk a given monitor list, and deflate idle monitors. 2295 // The given list could be a per-thread list or a global list. 2296 // 2297 // In the case of parallel processing of thread local monitor lists, 2298 // work is done by Threads::parallel_threads_do() which ensures that 2299 // each Java thread is processed by exactly one worker thread, and 2300 // thus avoid conflicts that would arise when worker threads would 2301 // process the same monitor lists concurrently. 2302 // 2303 // See also ParallelSPCleanupTask and 2304 // SafepointSynchronize::do_cleanup_tasks() in safepoint.cpp and 2305 // Threads::parallel_java_threads_do() in thread.cpp. 2306 int ObjectSynchronizer::deflate_monitor_list(ObjectMonitor* volatile * list_p, 2307 int volatile * count_p, 2308 ObjectMonitor** free_head_p, 2309 ObjectMonitor** free_tail_p) { 2310 ObjectMonitor* cur_mid_in_use = NULL; 2311 ObjectMonitor* mid = NULL; 2312 ObjectMonitor* next = NULL; 2313 int deflated_count = 0; 2314 2315 // We use the simpler mark-mid-as-we-go protocol since there are no 2316 // parallel list deletions since we are at a safepoint. 2317 if (!mark_list_head(list_p, &mid, &next)) { 2318 return 0; // The list is empty so nothing to deflate. 2319 } 2320 2321 while (true) { 2322 oop obj = (oop) mid->object(); 2323 if (obj != NULL && deflate_monitor(mid, obj, free_head_p, free_tail_p)) { 2324 // Deflation succeeded and already updated free_head_p and 2325 // free_tail_p as needed. Finish the move to the local free list 2326 // by unlinking mid from the global or per-thread in-use list. 2327 if (cur_mid_in_use == NULL) { 2328 // mid is the list head and it is marked. Switch the list head 2329 // to next which unmarks the list head, but leaves mid marked: 2330 *list_p = next; 2331 // mark_list_head() used cmpxchg() above, switching list head can be lazier: 2332 OrderAccess::storestore(); 2333 } else { 2334 // mid is marked. Switch cur_mid_in_use's next field to next 2335 // which is safe because we have no parallel list deletions, 2336 // but we leave mid marked: 2337 OrderAccess::release_store(&cur_mid_in_use->_next_om, next); 2338 } 2339 // At this point mid is disconnected from the in-use list so 2340 // its marked next field no longer has any effects. 2341 deflated_count++; 2342 Atomic::dec(count_p); 2343 // mid is current tail in the free_head_p list so NULL terminate it 2344 // (which also unmarks it): 2345 set_next(mid, NULL); 2346 2347 // All the list management is done so move on to the next one: 2348 mid = next; 2349 } else { 2350 set_next(mid, next); // unmark next field 2351 2352 // All the list management is done so move on to the next one: 2353 cur_mid_in_use = mid; 2354 mid = next; 2355 } 2356 if (mid == NULL) { 2357 break; // Reached end of the list so nothing more to deflate. 2358 } 2359 // Mark mid's next field so we can possibly deflate it: 2360 next = mark_next_loop(mid); 2361 } 2362 return deflated_count; 2363 } 2364 2365 // Walk a given ObjectMonitor list and deflate idle ObjectMonitors using 2366 // a JavaThread. Returns the number of deflated ObjectMonitors. The given 2367 // list could be a per-thread in-use list or the global in-use list. 2368 // If a safepoint has started, then we save state via saved_mid_in_use_p 2369 // and return to the caller to honor the safepoint. 2370 // 2371 int ObjectSynchronizer::deflate_monitor_list_using_JT(ObjectMonitor* volatile * list_p, 2372 int volatile * count_p, 2373 ObjectMonitor** free_head_p, 2374 ObjectMonitor** free_tail_p, 2375 ObjectMonitor** saved_mid_in_use_p) { 2376 assert(AsyncDeflateIdleMonitors, "sanity check"); 2377 assert(Thread::current()->is_Java_thread(), "precondition"); 2378 2379 ObjectMonitor* cur_mid_in_use = NULL; 2380 ObjectMonitor* mid = NULL; 2381 ObjectMonitor* next = NULL; 2382 ObjectMonitor* next_next = NULL; 2383 int deflated_count = 0; 2384 2385 // We use the more complicated mark-cur_mid_in_use-and-mid-as-we-go 2386 // protocol because om_release() can do list deletions in parallel. 2387 // We also mark-next-next-as-we-go to prevent an om_flush() that is 2388 // behind this thread from passing us. 2389 if (*saved_mid_in_use_p == NULL) { 2390 // No saved state so start at the beginning. 2391 // Mark the list head's next field so we can possibly deflate it: 2392 if (!mark_list_head(list_p, &mid, &next)) { 2393 return 0; // The list is empty so nothing to deflate. 2394 } 2395 } else { 2396 // We're restarting after a safepoint so restore the necessary state 2397 // before we resume. 2398 cur_mid_in_use = *saved_mid_in_use_p; 2399 // Mark cur_mid_in_use's next field so we can possibly update its 2400 // next field to extract a deflated ObjectMonitor. 2401 mid = mark_next_loop(cur_mid_in_use); 2402 if (mid == NULL) { 2403 set_next(cur_mid_in_use, NULL); // unmark next field 2404 *saved_mid_in_use_p = NULL; 2405 return 0; // The remainder is empty so nothing more to deflate. 2406 } 2407 // Mark mid's next field so we can possibly deflate it: 2408 next = mark_next_loop(mid); 2409 } 2410 2411 while (true) { 2412 // The current mid's next field is marked at this point. If we have 2413 // a cur_mid_in_use, then its next field is also marked at this point. 2414 2415 if (next != NULL) { 2416 // We mark next's next field so that an om_flush() 2417 // thread that is behind us cannot pass us when we 2418 // unmark the current mid's next field. 2419 next_next = mark_next_loop(next); 2420 } 2421 2422 // Only try to deflate if there is an associated Java object and if 2423 // mid is old (is not newly allocated and is not newly freed). 2424 if (mid->object() != NULL && mid->is_old() && 2425 deflate_monitor_using_JT(mid, free_head_p, free_tail_p)) { 2426 // Deflation succeeded and already updated free_head_p and 2427 // free_tail_p as needed. Finish the move to the local free list 2428 // by unlinking mid from the global or per-thread in-use list. 2429 if (cur_mid_in_use == NULL) { 2430 // mid is the list head and it is marked. Switch the list head 2431 // to next which is also marked (if not NULL) and also leave 2432 // mid marked: 2433 *list_p = next; 2434 // mark_list_head() used cmpxchg() above, switching list head can be lazier: 2435 OrderAccess::storestore(); 2436 } else { 2437 ObjectMonitor* marked_next = mark_om_ptr(next); 2438 // mid and cur_mid_in_use are marked. Switch cur_mid_in_use's 2439 // next field to marked_next and also leave mid marked: 2440 OrderAccess::release_store(&cur_mid_in_use->_next_om, marked_next); 2441 } 2442 // At this point mid is disconnected from the in-use list so 2443 // its marked next field no longer has any effects. 2444 deflated_count++; 2445 Atomic::dec(count_p); 2446 // mid is current tail in the free_head_p list so NULL terminate it 2447 // (which also unmarks it): 2448 set_next(mid, NULL); 2449 2450 // All the list management is done so move on to the next one: 2451 mid = next; // mid keeps non-NULL next's marked next field 2452 next = next_next; 2453 } else { 2454 // mid is considered in-use if it does not have an associated 2455 // Java object or mid is not old or deflation did not succeed. 2456 // A mid->is_new() node can be seen here when it is freshly 2457 // returned by om_alloc() (and skips the deflation code path). 2458 // A mid->is_old() node can be seen here when deflation failed. 2459 // A mid->is_free() node can be seen here when a fresh node from 2460 // om_alloc() is released by om_release() due to losing the race 2461 // in inflate(). 2462 2463 // All the list management is done so move on to the next one: 2464 if (cur_mid_in_use != NULL) { 2465 set_next(cur_mid_in_use, mid); // umark cur_mid_in_use 2466 } 2467 // The next cur_mid_in_use keeps mid's marked next field so 2468 // that it is stable for a possible next field change. It 2469 // cannot be modified by om_release() while it is marked. 2470 cur_mid_in_use = mid; 2471 mid = next; // mid keeps non-NULL next's marked next field 2472 next = next_next; 2473 2474 if (SafepointSynchronize::is_synchronizing() && 2475 cur_mid_in_use != *list_p && cur_mid_in_use->is_old()) { 2476 // If a safepoint has started and cur_mid_in_use is not the list 2477 // head and is old, then it is safe to use as saved state. Return 2478 // to the caller before blocking. 2479 *saved_mid_in_use_p = cur_mid_in_use; 2480 set_next(cur_mid_in_use, mid); // umark cur_mid_in_use 2481 if (mid != NULL) { 2482 set_next(mid, next); // umark mid 2483 } 2484 return deflated_count; 2485 } 2486 } 2487 if (mid == NULL) { 2488 if (cur_mid_in_use != NULL) { 2489 set_next(cur_mid_in_use, mid); // umark cur_mid_in_use 2490 } 2491 break; // Reached end of the list so nothing more to deflate. 2492 } 2493 2494 // The current mid's next field is marked at this point. If we have 2495 // a cur_mid_in_use, then its next field is also marked at this point. 2496 } 2497 // We finished the list without a safepoint starting so there's 2498 // no need to save state. 2499 *saved_mid_in_use_p = NULL; 2500 return deflated_count; 2501 } 2502 2503 void ObjectSynchronizer::prepare_deflate_idle_monitors(DeflateMonitorCounters* counters) { 2504 counters->n_in_use = 0; // currently associated with objects 2505 counters->n_in_circulation = 0; // extant 2506 counters->n_scavenged = 0; // reclaimed (global and per-thread) 2507 counters->per_thread_scavenged = 0; // per-thread scavenge total 2508 counters->per_thread_times = 0.0; // per-thread scavenge times 2509 OrderAccess::storestore(); // flush inits for worker threads 2510 } 2511 2512 void ObjectSynchronizer::deflate_idle_monitors(DeflateMonitorCounters* counters) { 2513 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 2514 2515 if (AsyncDeflateIdleMonitors) { 2516 // Nothing to do when global idle ObjectMonitors are deflated using 2517 // a JavaThread unless a special deflation has been requested. 2518 if (!is_special_deflation_requested()) { 2519 return; 2520 } 2521 } 2522 2523 bool deflated = false; 2524 2525 ObjectMonitor* free_head_p = NULL; // Local SLL of scavenged monitors 2526 ObjectMonitor* free_tail_p = NULL; 2527 elapsedTimer timer; 2528 2529 if (log_is_enabled(Info, monitorinflation)) { 2530 timer.start(); 2531 } 2532 2533 // Note: the thread-local monitors lists get deflated in 2534 // a separate pass. See deflate_thread_local_monitors(). 2535 2536 // For moribund threads, scan g_om_in_use_list 2537 int deflated_count = 0; 2538 if (g_om_in_use_list != NULL) { 2539 // Update n_in_circulation before g_om_in_use_count is updated by deflation. 2540 Atomic::add(g_om_in_use_count, &counters->n_in_circulation); 2541 2542 deflated_count = deflate_monitor_list(&g_om_in_use_list, &g_om_in_use_count, &free_head_p, &free_tail_p); 2543 Atomic::add(g_om_in_use_count, &counters->n_in_use); 2544 } 2545 2546 if (free_head_p != NULL) { 2547 // Move the deflated ObjectMonitors back to the global free list. 2548 // No races on the working free list so no need for load_acquire(). 2549 guarantee(free_tail_p != NULL && deflated_count > 0, "invariant"); 2550 assert(free_tail_p->_next_om == NULL, "must be NULL: _next_om=" 2551 INTPTR_FORMAT, p2i(free_tail_p->_next_om)); 2552 prepend_list_to_g_free_list(free_head_p, free_tail_p, deflated_count); 2553 Atomic::add(deflated_count, &counters->n_scavenged); 2554 } 2555 timer.stop(); 2556 2557 LogStreamHandle(Debug, monitorinflation) lsh_debug; 2558 LogStreamHandle(Info, monitorinflation) lsh_info; 2559 LogStream* ls = NULL; 2560 if (log_is_enabled(Debug, monitorinflation)) { 2561 ls = &lsh_debug; 2562 } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) { 2563 ls = &lsh_info; 2564 } 2565 if (ls != NULL) { 2566 ls->print_cr("deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count); 2567 } 2568 } 2569 2570 class HandshakeForDeflation : public ThreadClosure { 2571 public: 2572 void do_thread(Thread* thread) { 2573 log_trace(monitorinflation)("HandshakeForDeflation::do_thread: thread=" 2574 INTPTR_FORMAT, p2i(thread)); 2575 } 2576 }; 2577 2578 void ObjectSynchronizer::deflate_idle_monitors_using_JT() { 2579 assert(AsyncDeflateIdleMonitors, "sanity check"); 2580 2581 // Deflate any global idle monitors. 2582 deflate_global_idle_monitors_using_JT(); 2583 2584 int count = 0; 2585 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { 2586 if (jt->om_in_use_count > 0 && !jt->is_exiting()) { 2587 // This JavaThread is using ObjectMonitors so deflate any that 2588 // are idle unless this JavaThread is exiting; do not race with 2589 // ObjectSynchronizer::om_flush(). 2590 deflate_per_thread_idle_monitors_using_JT(jt); 2591 count++; 2592 } 2593 } 2594 if (count > 0) { 2595 log_debug(monitorinflation)("did async deflation of idle monitors for %d thread(s).", count); 2596 } 2597 2598 log_info(monitorinflation)("async g_om_population=%d, g_om_in_use_count=%d, " 2599 "g_om_free_count=%d, g_om_wait_count=%d", 2600 g_om_population, g_om_in_use_count, 2601 g_om_free_count, g_om_wait_count); 2602 2603 // The ServiceThread's async deflation request has been processed. 2604 set_is_async_deflation_requested(false); 2605 2606 if (HandshakeAfterDeflateIdleMonitors && g_om_wait_count > 0) { 2607 // There are deflated ObjectMonitors waiting for a handshake 2608 // (or a safepoint) for safety. 2609 2610 // g_wait_list and g_om_wait_count are only updated by the calling 2611 // thread so no need for load_acquire() or release_store(). 2612 ObjectMonitor* list = g_wait_list; 2613 ADIM_guarantee(list != NULL, "g_wait_list must not be NULL"); 2614 int count = g_om_wait_count; 2615 g_om_wait_count = 0; 2616 g_wait_list = NULL; 2617 OrderAccess::storestore(); // Lazier memory sync is okay for list walkers. 2618 2619 // Find the tail for prepend_list_to_common(). No need to mark 2620 // ObjectMonitors for this list walk since only the deflater 2621 // thread manages the wait list. 2622 int l_count = 0; 2623 ObjectMonitor* tail = NULL; 2624 for (ObjectMonitor* n = list; n != NULL; n = unmarked_next(n)) { 2625 tail = n; 2626 l_count++; 2627 } 2628 ADIM_guarantee(count == l_count, "count=%d != l_count=%d", count, l_count); 2629 2630 // Will execute a safepoint if !ThreadLocalHandshakes: 2631 HandshakeForDeflation hfd_tc; 2632 Handshake::execute(&hfd_tc); 2633 2634 prepend_list_to_common(list, tail, count, &g_free_list, &g_om_free_count); 2635 2636 log_info(monitorinflation)("moved %d idle monitors from global waiting list to global free list", count); 2637 } 2638 } 2639 2640 // Deflate global idle ObjectMonitors using a JavaThread. 2641 // 2642 void ObjectSynchronizer::deflate_global_idle_monitors_using_JT() { 2643 assert(AsyncDeflateIdleMonitors, "sanity check"); 2644 assert(Thread::current()->is_Java_thread(), "precondition"); 2645 JavaThread* self = JavaThread::current(); 2646 2647 deflate_common_idle_monitors_using_JT(true /* is_global */, self); 2648 } 2649 2650 // Deflate the specified JavaThread's idle ObjectMonitors using a JavaThread. 2651 // 2652 void ObjectSynchronizer::deflate_per_thread_idle_monitors_using_JT(JavaThread* target) { 2653 assert(AsyncDeflateIdleMonitors, "sanity check"); 2654 assert(Thread::current()->is_Java_thread(), "precondition"); 2655 2656 deflate_common_idle_monitors_using_JT(false /* !is_global */, target); 2657 } 2658 2659 // Deflate global or per-thread idle ObjectMonitors using a JavaThread. 2660 // 2661 void ObjectSynchronizer::deflate_common_idle_monitors_using_JT(bool is_global, JavaThread* target) { 2662 JavaThread* self = JavaThread::current(); 2663 2664 int deflated_count = 0; 2665 ObjectMonitor* free_head_p = NULL; // Local SLL of scavenged ObjectMonitors 2666 ObjectMonitor* free_tail_p = NULL; 2667 ObjectMonitor* saved_mid_in_use_p = NULL; 2668 elapsedTimer timer; 2669 2670 if (log_is_enabled(Info, monitorinflation)) { 2671 timer.start(); 2672 } 2673 2674 if (is_global) { 2675 OM_PERFDATA_OP(MonExtant, set_value(g_om_in_use_count)); 2676 } else { 2677 OM_PERFDATA_OP(MonExtant, inc(target->om_in_use_count)); 2678 } 2679 2680 do { 2681 int local_deflated_count; 2682 if (is_global) { 2683 local_deflated_count = deflate_monitor_list_using_JT(&g_om_in_use_list, &g_om_in_use_count, &free_head_p, &free_tail_p, &saved_mid_in_use_p); 2684 } else { 2685 local_deflated_count = deflate_monitor_list_using_JT(&target->om_in_use_list, &target->om_in_use_count, &free_head_p, &free_tail_p, &saved_mid_in_use_p); 2686 } 2687 deflated_count += local_deflated_count; 2688 2689 if (free_head_p != NULL) { 2690 // Move the deflated ObjectMonitors to the global free list. 2691 // No races on the working list so no need for load_acquire(). 2692 guarantee(free_tail_p != NULL && local_deflated_count > 0, "free_tail_p=" INTPTR_FORMAT ", local_deflated_count=%d", p2i(free_tail_p), local_deflated_count); 2693 // Note: The target thread can be doing an om_alloc() that 2694 // is trying to prepend an ObjectMonitor on its in-use list 2695 // at the same time that we have deflated the current in-use 2696 // list head and put it on the local free list. prepend_to_common() 2697 // will detect the race and retry which avoids list corruption, 2698 // but the next field in free_tail_p can flicker to marked 2699 // and then unmarked while prepend_to_common() is sorting it 2700 // all out. 2701 assert(unmarked_next(free_tail_p) == NULL, "must be NULL: _next_om=" 2702 INTPTR_FORMAT, p2i(unmarked_next(free_tail_p))); 2703 2704 if (HandshakeAfterDeflateIdleMonitors) { 2705 prepend_list_to_g_wait_list(free_head_p, free_tail_p, local_deflated_count); 2706 } else { 2707 prepend_list_to_g_free_list(free_head_p, free_tail_p, local_deflated_count); 2708 } 2709 2710 OM_PERFDATA_OP(Deflations, inc(local_deflated_count)); 2711 } 2712 2713 if (saved_mid_in_use_p != NULL) { 2714 // deflate_monitor_list_using_JT() detected a safepoint starting. 2715 timer.stop(); 2716 { 2717 if (is_global) { 2718 log_debug(monitorinflation)("pausing deflation of global idle monitors for a safepoint."); 2719 } else { 2720 log_debug(monitorinflation)("jt=" INTPTR_FORMAT ": pausing deflation of per-thread idle monitors for a safepoint.", p2i(target)); 2721 } 2722 assert(SafepointSynchronize::is_synchronizing(), "sanity check"); 2723 ThreadBlockInVM blocker(self); 2724 } 2725 // Prepare for another loop after the safepoint. 2726 free_head_p = NULL; 2727 free_tail_p = NULL; 2728 if (log_is_enabled(Info, monitorinflation)) { 2729 timer.start(); 2730 } 2731 } 2732 } while (saved_mid_in_use_p != NULL); 2733 timer.stop(); 2734 2735 LogStreamHandle(Debug, monitorinflation) lsh_debug; 2736 LogStreamHandle(Info, monitorinflation) lsh_info; 2737 LogStream* ls = NULL; 2738 if (log_is_enabled(Debug, monitorinflation)) { 2739 ls = &lsh_debug; 2740 } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) { 2741 ls = &lsh_info; 2742 } 2743 if (ls != NULL) { 2744 if (is_global) { 2745 ls->print_cr("async-deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count); 2746 } else { 2747 ls->print_cr("jt=" INTPTR_FORMAT ": async-deflating per-thread idle monitors, %3.7f secs, %d monitors", p2i(target), timer.seconds(), deflated_count); 2748 } 2749 } 2750 } 2751 2752 void ObjectSynchronizer::finish_deflate_idle_monitors(DeflateMonitorCounters* counters) { 2753 // Report the cumulative time for deflating each thread's idle 2754 // monitors. Note: if the work is split among more than one 2755 // worker thread, then the reported time will likely be more 2756 // than a beginning to end measurement of the phase. 2757 log_info(safepoint, cleanup)("deflating per-thread idle monitors, %3.7f secs, monitors=%d", counters->per_thread_times, counters->per_thread_scavenged); 2758 2759 bool needs_special_deflation = is_special_deflation_requested(); 2760 if (AsyncDeflateIdleMonitors && !needs_special_deflation) { 2761 // Nothing to do when idle ObjectMonitors are deflated using 2762 // a JavaThread unless a special deflation has been requested. 2763 return; 2764 } 2765 2766 if (log_is_enabled(Debug, monitorinflation)) { 2767 // exit_globals()'s call to audit_and_print_stats() is done 2768 // at the Info level and not at a safepoint. 2769 // For async deflation, audit_and_print_stats() is called in 2770 // ObjectSynchronizer::do_safepoint_work() at the Debug level 2771 // at a safepoint. 2772 ObjectSynchronizer::audit_and_print_stats(false /* on_exit */); 2773 } else if (log_is_enabled(Info, monitorinflation)) { 2774 log_info(monitorinflation)("g_om_population=%d, g_om_in_use_count=%d, " 2775 "g_om_free_count=%d, g_om_wait_count=%d", 2776 g_om_population, g_om_in_use_count, 2777 g_om_free_count, g_om_wait_count); 2778 } 2779 2780 ForceMonitorScavenge = 0; // Reset 2781 2782 OM_PERFDATA_OP(Deflations, inc(counters->n_scavenged)); 2783 OM_PERFDATA_OP(MonExtant, set_value(counters->n_in_circulation)); 2784 2785 GVars.stw_random = os::random(); 2786 GVars.stw_cycle++; 2787 2788 if (needs_special_deflation) { 2789 set_is_special_deflation_requested(false); // special deflation is done 2790 } 2791 } 2792 2793 void ObjectSynchronizer::deflate_thread_local_monitors(Thread* thread, DeflateMonitorCounters* counters) { 2794 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 2795 2796 if (AsyncDeflateIdleMonitors && !is_special_deflation_requested()) { 2797 // Nothing to do if a special deflation has NOT been requested. 2798 return; 2799 } 2800 2801 ObjectMonitor* free_head_p = NULL; // Local SLL of scavenged monitors 2802 ObjectMonitor* free_tail_p = NULL; 2803 elapsedTimer timer; 2804 2805 if (log_is_enabled(Info, safepoint, cleanup) || 2806 log_is_enabled(Info, monitorinflation)) { 2807 timer.start(); 2808 } 2809 2810 // Update n_in_circulation before om_in_use_count is updated by deflation. 2811 Atomic::add(thread->om_in_use_count, &counters->n_in_circulation); 2812 2813 int deflated_count = deflate_monitor_list(&thread->om_in_use_list, &thread->om_in_use_count, &free_head_p, &free_tail_p); 2814 Atomic::add(thread->om_in_use_count, &counters->n_in_use); 2815 2816 if (free_head_p != NULL) { 2817 // Move the deflated ObjectMonitors back to the global free list. 2818 // No races on the working list so no need for load_acquire(). 2819 guarantee(free_tail_p != NULL && deflated_count > 0, "invariant"); 2820 assert(free_tail_p->_next_om == NULL, "must be NULL: _next_om=" 2821 INTPTR_FORMAT, p2i(free_tail_p->_next_om)); 2822 prepend_list_to_g_free_list(free_head_p, free_tail_p, deflated_count); 2823 Atomic::add(deflated_count, &counters->n_scavenged); 2824 Atomic::add(deflated_count, &counters->per_thread_scavenged); 2825 } 2826 2827 timer.stop(); 2828 // Safepoint logging cares about cumulative per_thread_times and 2829 // we'll capture most of the cost, but not the muxRelease() which 2830 // should be cheap. 2831 counters->per_thread_times += timer.seconds(); 2832 2833 LogStreamHandle(Debug, monitorinflation) lsh_debug; 2834 LogStreamHandle(Info, monitorinflation) lsh_info; 2835 LogStream* ls = NULL; 2836 if (log_is_enabled(Debug, monitorinflation)) { 2837 ls = &lsh_debug; 2838 } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) { 2839 ls = &lsh_info; 2840 } 2841 if (ls != NULL) { 2842 ls->print_cr("jt=" INTPTR_FORMAT ": deflating per-thread idle monitors, %3.7f secs, %d monitors", p2i(thread), timer.seconds(), deflated_count); 2843 } 2844 } 2845 2846 // Monitor cleanup on JavaThread::exit 2847 2848 // Iterate through monitor cache and attempt to release thread's monitors 2849 // Gives up on a particular monitor if an exception occurs, but continues 2850 // the overall iteration, swallowing the exception. 2851 class ReleaseJavaMonitorsClosure: public MonitorClosure { 2852 private: 2853 TRAPS; 2854 2855 public: 2856 ReleaseJavaMonitorsClosure(Thread* thread) : THREAD(thread) {} 2857 void do_monitor(ObjectMonitor* mid) { 2858 if (mid->owner() == THREAD) { 2859 (void)mid->complete_exit(CHECK); 2860 } 2861 } 2862 }; 2863 2864 // Release all inflated monitors owned by THREAD. Lightweight monitors are 2865 // ignored. This is meant to be called during JNI thread detach which assumes 2866 // all remaining monitors are heavyweight. All exceptions are swallowed. 2867 // Scanning the extant monitor list can be time consuming. 2868 // A simple optimization is to add a per-thread flag that indicates a thread 2869 // called jni_monitorenter() during its lifetime. 2870 // 2871 // Instead of No_Savepoint_Verifier it might be cheaper to 2872 // use an idiom of the form: 2873 // auto int tmp = SafepointSynchronize::_safepoint_counter ; 2874 // <code that must not run at safepoint> 2875 // guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ; 2876 // Since the tests are extremely cheap we could leave them enabled 2877 // for normal product builds. 2878 2879 void ObjectSynchronizer::release_monitors_owned_by_thread(TRAPS) { 2880 assert(THREAD == JavaThread::current(), "must be current Java thread"); 2881 NoSafepointVerifier nsv; 2882 ReleaseJavaMonitorsClosure rjmc(THREAD); 2883 ObjectSynchronizer::monitors_iterate(&rjmc); 2884 THREAD->clear_pending_exception(); 2885 } 2886 2887 const char* ObjectSynchronizer::inflate_cause_name(const InflateCause cause) { 2888 switch (cause) { 2889 case inflate_cause_vm_internal: return "VM Internal"; 2890 case inflate_cause_monitor_enter: return "Monitor Enter"; 2891 case inflate_cause_wait: return "Monitor Wait"; 2892 case inflate_cause_notify: return "Monitor Notify"; 2893 case inflate_cause_hash_code: return "Monitor Hash Code"; 2894 case inflate_cause_jni_enter: return "JNI Monitor Enter"; 2895 case inflate_cause_jni_exit: return "JNI Monitor Exit"; 2896 default: 2897 ShouldNotReachHere(); 2898 } 2899 return "Unknown"; 2900 } 2901 2902 //------------------------------------------------------------------------------ 2903 // Debugging code 2904 2905 u_char* ObjectSynchronizer::get_gvars_addr() { 2906 return (u_char*)&GVars; 2907 } 2908 2909 u_char* ObjectSynchronizer::get_gvars_hc_sequence_addr() { 2910 return (u_char*)&GVars.hc_sequence; 2911 } 2912 2913 size_t ObjectSynchronizer::get_gvars_size() { 2914 return sizeof(SharedGlobals); 2915 } 2916 2917 u_char* ObjectSynchronizer::get_gvars_stw_random_addr() { 2918 return (u_char*)&GVars.stw_random; 2919 } 2920 2921 void ObjectSynchronizer::audit_and_print_stats(bool on_exit) { 2922 assert(on_exit || SafepointSynchronize::is_at_safepoint(), "invariant"); 2923 2924 LogStreamHandle(Debug, monitorinflation) lsh_debug; 2925 LogStreamHandle(Info, monitorinflation) lsh_info; 2926 LogStreamHandle(Trace, monitorinflation) lsh_trace; 2927 LogStream* ls = NULL; 2928 if (log_is_enabled(Trace, monitorinflation)) { 2929 ls = &lsh_trace; 2930 } else if (log_is_enabled(Debug, monitorinflation)) { 2931 ls = &lsh_debug; 2932 } else if (log_is_enabled(Info, monitorinflation)) { 2933 ls = &lsh_info; 2934 } 2935 assert(ls != NULL, "sanity check"); 2936 2937 // Log counts for the global and per-thread monitor lists: 2938 int chk_om_population = log_monitor_list_counts(ls); 2939 int error_cnt = 0; 2940 2941 ls->print_cr("Checking global lists:"); 2942 2943 // Check g_om_population: 2944 if (g_om_population == chk_om_population) { 2945 ls->print_cr("g_om_population=%d equals chk_om_population=%d", 2946 g_om_population, chk_om_population); 2947 } else { 2948 // With lock free access to the monitor lists, it is possible for 2949 // log_monitor_list_counts() to return a value that doesn't match 2950 // g_om_population. So far a higher value has been seen in testing 2951 // so something is being double counted by log_monitor_list_counts(). 2952 ls->print_cr("WARNING: g_om_population=%d is not equal to " 2953 "chk_om_population=%d", g_om_population, chk_om_population); 2954 } 2955 2956 // Check g_om_in_use_list and g_om_in_use_count: 2957 chk_global_in_use_list_and_count(ls, &error_cnt); 2958 2959 // Check g_free_list and g_om_free_count: 2960 chk_global_free_list_and_count(ls, &error_cnt); 2961 2962 if (HandshakeAfterDeflateIdleMonitors) { 2963 // Check g_wait_list and g_om_wait_count: 2964 chk_global_wait_list_and_count(ls, &error_cnt); 2965 } 2966 2967 ls->print_cr("Checking per-thread lists:"); 2968 2969 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { 2970 // Check om_in_use_list and om_in_use_count: 2971 chk_per_thread_in_use_list_and_count(jt, ls, &error_cnt); 2972 2973 // Check om_free_list and om_free_count: 2974 chk_per_thread_free_list_and_count(jt, ls, &error_cnt); 2975 } 2976 2977 if (error_cnt == 0) { 2978 ls->print_cr("No errors found in monitor list checks."); 2979 } else { 2980 log_error(monitorinflation)("found monitor list errors: error_cnt=%d", error_cnt); 2981 } 2982 2983 if ((on_exit && log_is_enabled(Info, monitorinflation)) || 2984 (!on_exit && log_is_enabled(Trace, monitorinflation))) { 2985 // When exiting this log output is at the Info level. When called 2986 // at a safepoint, this log output is at the Trace level since 2987 // there can be a lot of it. 2988 log_in_use_monitor_details(ls); 2989 } 2990 2991 ls->flush(); 2992 2993 guarantee(error_cnt == 0, "ERROR: found monitor list errors: error_cnt=%d", error_cnt); 2994 } 2995 2996 // Check a free monitor entry; log any errors. 2997 void ObjectSynchronizer::chk_free_entry(JavaThread* jt, ObjectMonitor* n, 2998 outputStream * out, int *error_cnt_p) { 2999 stringStream ss; 3000 if (n->is_busy()) { 3001 if (jt != NULL) { 3002 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT 3003 ": free per-thread monitor must not be busy: %s", p2i(jt), 3004 p2i(n), n->is_busy_to_string(&ss)); 3005 } else { 3006 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor " 3007 "must not be busy: %s", p2i(n), n->is_busy_to_string(&ss)); 3008 } 3009 *error_cnt_p = *error_cnt_p + 1; 3010 } 3011 if (n->header().value() != 0) { 3012 if (jt != NULL) { 3013 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT 3014 ": free per-thread monitor must have NULL _header " 3015 "field: _header=" INTPTR_FORMAT, p2i(jt), p2i(n), 3016 n->header().value()); 3017 *error_cnt_p = *error_cnt_p + 1; 3018 } else if (!AsyncDeflateIdleMonitors) { 3019 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor " 3020 "must have NULL _header field: _header=" INTPTR_FORMAT, 3021 p2i(n), n->header().value()); 3022 *error_cnt_p = *error_cnt_p + 1; 3023 } 3024 } 3025 if (n->object() != NULL) { 3026 if (jt != NULL) { 3027 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT 3028 ": free per-thread monitor must have NULL _object " 3029 "field: _object=" INTPTR_FORMAT, p2i(jt), p2i(n), 3030 p2i(n->object())); 3031 } else { 3032 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor " 3033 "must have NULL _object field: _object=" INTPTR_FORMAT, 3034 p2i(n), p2i(n->object())); 3035 } 3036 *error_cnt_p = *error_cnt_p + 1; 3037 } 3038 } 3039 3040 // Check the global free list and count; log the results of the checks. 3041 void ObjectSynchronizer::chk_global_free_list_and_count(outputStream * out, 3042 int *error_cnt_p) { 3043 int chk_om_free_count = 0; 3044 ObjectMonitor* cur = NULL; 3045 ObjectMonitor* next = NULL; 3046 if (mark_list_head(&g_free_list, &cur, &next)) { 3047 // Marked the global free list head so process the list. 3048 while (true) { 3049 chk_free_entry(NULL /* jt */, cur, out, error_cnt_p); 3050 chk_om_free_count++; 3051 3052 mark_next_for_traversal(&cur, &next); 3053 if (cur == NULL) { 3054 break; 3055 } 3056 } 3057 } 3058 if (g_om_free_count == chk_om_free_count) { 3059 out->print_cr("g_om_free_count=%d equals chk_om_free_count=%d", 3060 g_om_free_count, chk_om_free_count); 3061 } else { 3062 // With lock free access to g_free_list, it is possible for an 3063 // ObjectMonitor to be prepended to g_free_list after we started 3064 // calculating chk_om_free_count so g_om_free_count may not 3065 // match anymore. 3066 out->print_cr("WARNING: g_om_free_count=%d is not equal to " 3067 "chk_om_free_count=%d", g_om_free_count, chk_om_free_count); 3068 } 3069 } 3070 3071 // Check the global wait list and count; log the results of the checks. 3072 void ObjectSynchronizer::chk_global_wait_list_and_count(outputStream * out, 3073 int *error_cnt_p) { 3074 int chk_om_wait_count = 0; 3075 ObjectMonitor* cur = NULL; 3076 ObjectMonitor* next = NULL; 3077 if (mark_list_head(&g_wait_list, &cur, &next)) { 3078 // Marked the global wait list head so process the list. 3079 while (true) { 3080 // Rules for g_wait_list are the same as of g_free_list: 3081 chk_free_entry(NULL /* jt */, cur, out, error_cnt_p); 3082 chk_om_wait_count++; 3083 3084 mark_next_for_traversal(&cur, &next); 3085 if (cur == NULL) { 3086 break; 3087 } 3088 } 3089 } 3090 if (g_om_wait_count == chk_om_wait_count) { 3091 out->print_cr("g_om_wait_count=%d equals chk_om_wait_count=%d", 3092 g_om_wait_count, chk_om_wait_count); 3093 } else { 3094 out->print_cr("ERROR: g_om_wait_count=%d is not equal to " 3095 "chk_om_wait_count=%d", g_om_wait_count, chk_om_wait_count); 3096 *error_cnt_p = *error_cnt_p + 1; 3097 } 3098 } 3099 3100 // Check the global in-use list and count; log the results of the checks. 3101 void ObjectSynchronizer::chk_global_in_use_list_and_count(outputStream * out, 3102 int *error_cnt_p) { 3103 int chk_om_in_use_count = 0; 3104 ObjectMonitor* cur = NULL; 3105 ObjectMonitor* next = NULL; 3106 if (mark_list_head(&g_om_in_use_list, &cur, &next)) { 3107 // Marked the global in-use list head so process the list. 3108 while (true) { 3109 chk_in_use_entry(NULL /* jt */, cur, out, error_cnt_p); 3110 chk_om_in_use_count++; 3111 3112 mark_next_for_traversal(&cur, &next); 3113 if (cur == NULL) { 3114 break; 3115 } 3116 } 3117 } 3118 if (g_om_in_use_count == chk_om_in_use_count) { 3119 out->print_cr("g_om_in_use_count=%d equals chk_om_in_use_count=%d", 3120 g_om_in_use_count, chk_om_in_use_count); 3121 } else { 3122 // With lock free access to the monitor lists, it is possible for 3123 // an exiting JavaThread to put its in-use ObjectMonitors on the 3124 // global in-use list after chk_om_in_use_count is calculated above. 3125 out->print_cr("WARNING: g_om_in_use_count=%d is not equal to chk_om_in_use_count=%d", 3126 g_om_in_use_count, chk_om_in_use_count); 3127 } 3128 } 3129 3130 // Check an in-use monitor entry; log any errors. 3131 void ObjectSynchronizer::chk_in_use_entry(JavaThread* jt, ObjectMonitor* n, 3132 outputStream * out, int *error_cnt_p) { 3133 if (n->header().value() == 0) { 3134 if (jt != NULL) { 3135 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT 3136 ": in-use per-thread monitor must have non-NULL _header " 3137 "field.", p2i(jt), p2i(n)); 3138 } else { 3139 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global monitor " 3140 "must have non-NULL _header field.", p2i(n)); 3141 } 3142 *error_cnt_p = *error_cnt_p + 1; 3143 } 3144 if (n->object() == NULL) { 3145 if (jt != NULL) { 3146 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT 3147 ": in-use per-thread monitor must have non-NULL _object " 3148 "field.", p2i(jt), p2i(n)); 3149 } else { 3150 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global monitor " 3151 "must have non-NULL _object field.", p2i(n)); 3152 } 3153 *error_cnt_p = *error_cnt_p + 1; 3154 } 3155 const oop obj = (oop)n->object(); 3156 const markWord mark = obj->mark(); 3157 if (!mark.has_monitor()) { 3158 if (jt != NULL) { 3159 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT 3160 ": in-use per-thread monitor's object does not think " 3161 "it has a monitor: obj=" INTPTR_FORMAT ", mark=" 3162 INTPTR_FORMAT, p2i(jt), p2i(n), p2i(obj), mark.value()); 3163 } else { 3164 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global " 3165 "monitor's object does not think it has a monitor: obj=" 3166 INTPTR_FORMAT ", mark=" INTPTR_FORMAT, p2i(n), 3167 p2i(obj), mark.value()); 3168 } 3169 *error_cnt_p = *error_cnt_p + 1; 3170 } 3171 ObjectMonitor* const obj_mon = mark.monitor(); 3172 if (n != obj_mon) { 3173 if (jt != NULL) { 3174 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT 3175 ": in-use per-thread monitor's object does not refer " 3176 "to the same monitor: obj=" INTPTR_FORMAT ", mark=" 3177 INTPTR_FORMAT ", obj_mon=" INTPTR_FORMAT, p2i(jt), 3178 p2i(n), p2i(obj), mark.value(), p2i(obj_mon)); 3179 } else { 3180 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global " 3181 "monitor's object does not refer to the same monitor: obj=" 3182 INTPTR_FORMAT ", mark=" INTPTR_FORMAT ", obj_mon=" 3183 INTPTR_FORMAT, p2i(n), p2i(obj), mark.value(), p2i(obj_mon)); 3184 } 3185 *error_cnt_p = *error_cnt_p + 1; 3186 } 3187 } 3188 3189 // Check the thread's free list and count; log the results of the checks. 3190 void ObjectSynchronizer::chk_per_thread_free_list_and_count(JavaThread *jt, 3191 outputStream * out, 3192 int *error_cnt_p) { 3193 int chk_om_free_count = 0; 3194 ObjectMonitor* cur = NULL; 3195 ObjectMonitor* next = NULL; 3196 if (mark_list_head(&jt->om_free_list, &cur, &next)) { 3197 // Marked the per-thread free list head so process the list. 3198 while (true) { 3199 chk_free_entry(jt, cur, out, error_cnt_p); 3200 chk_om_free_count++; 3201 3202 mark_next_for_traversal(&cur, &next); 3203 if (cur == NULL) { 3204 break; 3205 } 3206 } 3207 } 3208 if (jt->om_free_count == chk_om_free_count) { 3209 out->print_cr("jt=" INTPTR_FORMAT ": om_free_count=%d equals " 3210 "chk_om_free_count=%d", p2i(jt), jt->om_free_count, 3211 chk_om_free_count); 3212 } else { 3213 out->print_cr("ERROR: jt=" INTPTR_FORMAT ": om_free_count=%d is not " 3214 "equal to chk_om_free_count=%d", p2i(jt), jt->om_free_count, 3215 chk_om_free_count); 3216 *error_cnt_p = *error_cnt_p + 1; 3217 } 3218 } 3219 3220 // Check the thread's in-use list and count; log the results of the checks. 3221 void ObjectSynchronizer::chk_per_thread_in_use_list_and_count(JavaThread *jt, 3222 outputStream * out, 3223 int *error_cnt_p) { 3224 int chk_om_in_use_count = 0; 3225 ObjectMonitor* cur = NULL; 3226 ObjectMonitor* next = NULL; 3227 if (mark_list_head(&jt->om_in_use_list, &cur, &next)) { 3228 // Marked the per-thread in-use list head so process the list. 3229 while (true) { 3230 chk_in_use_entry(jt, cur, out, error_cnt_p); 3231 chk_om_in_use_count++; 3232 3233 mark_next_for_traversal(&cur, &next); 3234 if (cur == NULL) { 3235 break; 3236 } 3237 } 3238 } 3239 if (jt->om_in_use_count == chk_om_in_use_count) { 3240 out->print_cr("jt=" INTPTR_FORMAT ": om_in_use_count=%d equals " 3241 "chk_om_in_use_count=%d", p2i(jt), 3242 jt->om_in_use_count, chk_om_in_use_count); 3243 } else { 3244 out->print_cr("ERROR: jt=" INTPTR_FORMAT ": om_in_use_count=%d is not " 3245 "equal to chk_om_in_use_count=%d", p2i(jt), 3246 jt->om_in_use_count, chk_om_in_use_count); 3247 *error_cnt_p = *error_cnt_p + 1; 3248 } 3249 } 3250 3251 // Log details about ObjectMonitors on the in-use lists. The 'BHL' 3252 // flags indicate why the entry is in-use, 'object' and 'object type' 3253 // indicate the associated object and its type. 3254 void ObjectSynchronizer::log_in_use_monitor_details(outputStream * out) { 3255 stringStream ss; 3256 if (g_om_in_use_count > 0) { 3257 out->print_cr("In-use global monitor info:"); 3258 out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)"); 3259 out->print_cr("%18s %s %7s %18s %18s", 3260 "monitor", "BHL", "ref_cnt", "object", "object type"); 3261 out->print_cr("================== === ======= ================== =================="); 3262 ObjectMonitor* cur = NULL; 3263 ObjectMonitor* next = NULL; 3264 if (mark_list_head(&g_om_in_use_list, &cur, &next)) { 3265 // Marked the global in-use list head so process the list. 3266 while (true) { 3267 const oop obj = (oop) cur->object(); 3268 const markWord mark = cur->header(); 3269 ResourceMark rm; 3270 out->print(INTPTR_FORMAT " %d%d%d %7d " INTPTR_FORMAT " %s", 3271 p2i(cur), cur->is_busy() != 0, mark.hash() != 0, 3272 cur->owner() != NULL, (int)cur->ref_count(), p2i(obj), 3273 obj->klass()->external_name()); 3274 if (cur->is_busy() != 0) { 3275 out->print(" (%s)", cur->is_busy_to_string(&ss)); 3276 ss.reset(); 3277 } 3278 out->cr(); 3279 3280 mark_next_for_traversal(&cur, &next); 3281 if (cur == NULL) { 3282 break; 3283 } 3284 } 3285 } 3286 } 3287 3288 out->print_cr("In-use per-thread monitor info:"); 3289 out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)"); 3290 out->print_cr("%18s %18s %s %7s %18s %18s", 3291 "jt", "monitor", "BHL", "ref_cnt", "object", "object type"); 3292 out->print_cr("================== ================== === ======= ================== =================="); 3293 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { 3294 ObjectMonitor* cur = NULL; 3295 ObjectMonitor* next = NULL; 3296 if (mark_list_head(&jt->om_in_use_list, &cur, &next)) { 3297 // Marked the global in-use list head so process the list. 3298 while (true) { 3299 const oop obj = (oop) cur->object(); 3300 const markWord mark = cur->header(); 3301 ResourceMark rm; 3302 out->print(INTPTR_FORMAT " " INTPTR_FORMAT " %d%d%d %7d " 3303 INTPTR_FORMAT " %s", p2i(jt), p2i(cur), cur->is_busy() != 0, 3304 mark.hash() != 0, cur->owner() != NULL, (int)cur->ref_count(), 3305 p2i(obj), obj->klass()->external_name()); 3306 if (cur->is_busy() != 0) { 3307 out->print(" (%s)", cur->is_busy_to_string(&ss)); 3308 ss.reset(); 3309 } 3310 out->cr(); 3311 3312 mark_next_for_traversal(&cur, &next); 3313 if (cur == NULL) { 3314 break; 3315 } 3316 } 3317 } 3318 } 3319 3320 out->flush(); 3321 } 3322 3323 // Log counts for the global and per-thread monitor lists and return 3324 // the population count. 3325 int ObjectSynchronizer::log_monitor_list_counts(outputStream * out) { 3326 int pop_count = 0; 3327 out->print_cr("%18s %10s %10s %10s %10s", 3328 "Global Lists:", "InUse", "Free", "Wait", "Total"); 3329 out->print_cr("================== ========== ========== ========== =========="); 3330 out->print_cr("%18s %10d %10d %10d %10d", "", g_om_in_use_count, 3331 g_om_free_count, g_om_wait_count, g_om_population); 3332 pop_count += g_om_in_use_count + g_om_free_count; 3333 if (HandshakeAfterDeflateIdleMonitors) { 3334 pop_count += g_om_wait_count; 3335 } 3336 3337 out->print_cr("%18s %10s %10s %10s", 3338 "Per-Thread Lists:", "InUse", "Free", "Provision"); 3339 out->print_cr("================== ========== ========== =========="); 3340 3341 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { 3342 out->print_cr(INTPTR_FORMAT " %10d %10d %10d", p2i(jt), 3343 jt->om_in_use_count, jt->om_free_count, jt->om_free_provision); 3344 pop_count += jt->om_in_use_count + jt->om_free_count; 3345 } 3346 return pop_count; 3347 } 3348 3349 #ifndef PRODUCT 3350 3351 // Check if monitor belongs to the monitor cache 3352 // The list is grow-only so it's *relatively* safe to traverse 3353 // the list of extant blocks without taking a lock. 3354 3355 int ObjectSynchronizer::verify_objmon_isinpool(ObjectMonitor *monitor) { 3356 PaddedObjectMonitor* block = g_block_list; 3357 while (block != NULL) { 3358 assert(block->object() == CHAINMARKER, "must be a block header"); 3359 if (monitor > &block[0] && monitor < &block[_BLOCKSIZE]) { 3360 address mon = (address)monitor; 3361 address blk = (address)block; 3362 size_t diff = mon - blk; 3363 assert((diff % sizeof(PaddedObjectMonitor)) == 0, "must be aligned"); 3364 return 1; 3365 } 3366 // unmarked_next() is not needed with g_block_list (no next field 3367 // marking) and no load_acquire() needed because _next_om is 3368 // updated before g_block_list is changed with cmpxchg(). 3369 block = (PaddedObjectMonitor*)block->_next_om; 3370 } 3371 return 0; 3372 } 3373 3374 #endif