< prev index next >

src/hotspot/share/runtime/synchronizer.cpp

Print this page
rev 54415 : 8222295: more baseline cleanups from Async Monitor Deflation project


 718   assert(!mark->has_bias_pattern(), "invariant");
 719 
 720   if (mark->is_neutral()) {
 721     hash = mark->hash();              // this is a normal header
 722     if (hash != 0) {                  // if it has hash, just return it
 723       return hash;
 724     }
 725     hash = get_next_hash(Self, obj);  // allocate a new hash code
 726     temp = mark->copy_set_hash(hash); // merge the hash code into header
 727     // use (machine word version) atomic operation to install the hash
 728     test = obj->cas_set_mark(temp, mark);
 729     if (test == mark) {
 730       return hash;
 731     }
 732     // If atomic operation failed, we must inflate the header
 733     // into heavy weight monitor. We could add more code here
 734     // for fast path, but it does not worth the complexity.
 735   } else if (mark->has_monitor()) {
 736     monitor = mark->monitor();
 737     temp = monitor->header();
 738     assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i((address)temp));
 739     hash = temp->hash();
 740     if (hash != 0) {
 741       return hash;
 742     }
 743     // Skip to the following code to reduce code size
 744   } else if (Self->is_lock_owned((address)mark->locker())) {
 745     temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned
 746     assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i((address)temp));
 747     hash = temp->hash();              // by current thread, check if the displaced
 748     if (hash != 0) {                  // header contains hash code
 749       return hash;
 750     }
 751     // WARNING:
 752     //   The displaced header is strictly immutable.
 753     // It can NOT be changed in ANY cases. So we have
 754     // to inflate the header into heavyweight monitor
 755     // even the current thread owns the lock. The reason
 756     // is the BasicLock (stack slot) will be asynchronously
 757     // read by other threads during the inflate() function.
 758     // Any change to stack may not propagate to other threads
 759     // correctly.
 760   }
 761 
 762   // Inflate the monitor to set hash code
 763   monitor = inflate(Self, obj, inflate_cause_hash_code);
 764   // Load displaced header and check it has hash code
 765   mark = monitor->header();
 766   assert(mark->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i((address)mark));
 767   hash = mark->hash();
 768   if (hash == 0) {
 769     hash = get_next_hash(Self, obj);
 770     temp = mark->copy_set_hash(hash); // merge hash code into header
 771     assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i((address)temp));
 772     test = Atomic::cmpxchg(temp, monitor->header_addr(), mark);
 773     if (test != mark) {
 774       // The only update to the header in the monitor (outside GC)
 775       // is install the hash code. If someone add new usage of
 776       // displaced header, please update this code
 777       hash = test->hash();
 778       assert(test->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i((address)test));
 779       assert(hash != 0, "Trivial unexpected object/monitor header usage.");
 780     }
 781   }
 782   // We finally get the hash
 783   return hash;
 784 }
 785 
 786 // Deprecated -- use FastHashCode() instead.
 787 
 788 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
 789   return FastHashCode(Thread::current(), obj());
 790 }
 791 
 792 
 793 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
 794                                                    Handle h_obj) {
 795   if (UseBiasedLocking) {
 796     BiasedLocking::revoke_and_rebias(h_obj, false, thread);
 797     assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 798   }


1317   assert(Universe::verify_in_progress() ||
1318          !SafepointSynchronize::is_at_safepoint(), "invariant");
1319 
1320   EventJavaMonitorInflate event;
1321 
1322   for (;;) {
1323     const markOop mark = object->mark();
1324     assert(!mark->has_bias_pattern(), "invariant");
1325 
1326     // The mark can be in one of the following states:
1327     // *  Inflated     - just return
1328     // *  Stack-locked - coerce it to inflated
1329     // *  INFLATING    - busy wait for conversion to complete
1330     // *  Neutral      - aggressively inflate the object.
1331     // *  BIASED       - Illegal.  We should never see this
1332 
1333     // CASE: inflated
1334     if (mark->has_monitor()) {
1335       ObjectMonitor * inf = mark->monitor();
1336       markOop dmw = inf->header();
1337       assert(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i((address)dmw));
1338       assert(oopDesc::equals((oop) inf->object(), object), "invariant");
1339       assert(ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1340       return inf;
1341     }
1342 
1343     // CASE: inflation in progress - inflating over a stack-lock.
1344     // Some other thread is converting from stack-locked to inflated.
1345     // Only that thread can complete inflation -- other threads must wait.
1346     // The INFLATING value is transient.
1347     // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
1348     // We could always eliminate polling by parking the thread on some auxiliary list.
1349     if (mark == markOopDesc::INFLATING()) {
1350       ReadStableMark(object);
1351       continue;
1352     }
1353 
1354     // CASE: stack-locked
1355     // Could be stack-locked either by this thread or by some other thread.
1356     //
1357     // Note that we allocate the objectmonitor speculatively, _before_ attempting


1381       m->_Responsible  = NULL;
1382       m->_recursions   = 0;
1383       m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;   // Consider: maintain by type/class
1384 
1385       markOop cmp = object->cas_set_mark(markOopDesc::INFLATING(), mark);
1386       if (cmp != mark) {
1387         omRelease(Self, m, true);
1388         continue;       // Interference -- just retry
1389       }
1390 
1391       // We've successfully installed INFLATING (0) into the mark-word.
1392       // This is the only case where 0 will appear in a mark-word.
1393       // Only the singular thread that successfully swings the mark-word
1394       // to 0 can perform (or more precisely, complete) inflation.
1395       //
1396       // Why do we CAS a 0 into the mark-word instead of just CASing the
1397       // mark-word from the stack-locked value directly to the new inflated state?
1398       // Consider what happens when a thread unlocks a stack-locked object.
1399       // It attempts to use CAS to swing the displaced header value from the
1400       // on-stack basiclock back into the object header.  Recall also that the
1401       // header value (hashcode, etc) can reside in (a) the object header, or
1402       // (b) a displaced header associated with the stack-lock, or (c) a displaced
1403       // header in an objectMonitor.  The inflate() routine must copy the header
1404       // value from the basiclock on the owner's stack to the objectMonitor, all
1405       // the while preserving the hashCode stability invariants.  If the owner
1406       // decides to release the lock while the value is 0, the unlock will fail
1407       // and control will eventually pass from slow_exit() to inflate.  The owner
1408       // will then spin, waiting for the 0 value to disappear.   Put another way,
1409       // the 0 causes the owner to stall if the owner happens to try to
1410       // drop the lock (restoring the header from the basiclock to the object)
1411       // while inflation is in-progress.  This protocol avoids races that might
1412       // would otherwise permit hashCode values to change or "flicker" for an object.
1413       // Critically, while object->mark is 0 mark->displaced_mark_helper() is stable.
1414       // 0 serves as a "BUSY" inflate-in-progress indicator.
1415 
1416 
1417       // fetch the displaced mark from the owner's stack.
1418       // The owner can't die or unwind past the lock while our INFLATING
1419       // object is in the mark.  Furthermore the owner can't complete
1420       // an unlock on the object, either.
1421       markOop dmw = mark->displaced_mark_helper();
1422       assert(dmw->is_neutral(), "invariant");


1423 
1424       // Setup monitor fields to proper values -- prepare the monitor
1425       m->set_header(dmw);
1426 
1427       // Optimization: if the mark->locker stack address is associated
1428       // with this thread we could simply set m->_owner = Self.
1429       // Note that a thread can inflate an object
1430       // that it has stack-locked -- as might happen in wait() -- directly
1431       // with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.
1432       m->set_owner(mark->locker());
1433       m->set_object(object);
1434       // TODO-FIXME: assert BasicLock->dhw != 0.
1435 
1436       // Must preserve store ordering. The monitor state must
1437       // be stable at the time of publishing the monitor address.
1438       guarantee(object->mark() == markOopDesc::INFLATING(), "invariant");
1439       object->release_set_mark(markOopDesc::encode(m));
1440 
1441       // Hopefully the performance counters are allocated on distinct cache lines
1442       // to avoid false sharing on MP systems ...


1446         lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark="
1447                      INTPTR_FORMAT ", type='%s'", p2i(object),
1448                      p2i(object->mark()), object->klass()->external_name());
1449       }
1450       if (event.should_commit()) {
1451         post_monitor_inflate_event(&event, object, cause);
1452       }
1453       return m;
1454     }
1455 
1456     // CASE: neutral
1457     // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1458     // If we know we're inflating for entry it's better to inflate by swinging a
1459     // pre-locked objectMonitor pointer into the object header.   A successful
1460     // CAS inflates the object *and* confers ownership to the inflating thread.
1461     // In the current implementation we use a 2-step mechanism where we CAS()
1462     // to inflate and then CAS() again to try to swing _owner from NULL to Self.
1463     // An inflateTry() method that we could call from fast_enter() and slow_enter()
1464     // would be useful.
1465 
1466     assert(mark->is_neutral(), "invariant");


1467     ObjectMonitor * m = omAlloc(Self);
1468     // prepare m for installation - set monitor to initial state
1469     m->Recycle();
1470     m->set_header(mark);
1471     m->set_owner(NULL);
1472     m->set_object(object);
1473     m->_recursions   = 0;
1474     m->_Responsible  = NULL;
1475     m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;       // consider: keep metastats by type/class
1476 
1477     if (object->cas_set_mark(markOopDesc::encode(m), mark) != mark) {
1478       m->set_header(NULL);
1479       m->set_object(NULL);
1480       m->Recycle();
1481       omRelease(Self, m, true);
1482       m = NULL;
1483       continue;
1484       // interference - the markword changed - just retry.
1485       // The state-transitions are one-way, so there's no chance of
1486       // live-lock -- "Inflated" is an absorbing state.
1487     }
1488 
1489     // Hopefully the performance counters are allocated on distinct
1490     // cache lines to avoid false sharing on MP systems ...
1491     OM_PERFDATA_OP(Inflations, inc());
1492     if (log_is_enabled(Trace, monitorinflation)) {
1493       ResourceMark rm(Self);
1494       lsh.print_cr("inflate(neutral): object=" INTPTR_FORMAT ", mark="
1495                    INTPTR_FORMAT ", type='%s'", p2i(object),
1496                    p2i(object->mark()), object->klass()->external_name());
1497     }
1498     if (event.should_commit()) {
1499       post_monitor_inflate_event(&event, object, cause);
1500     }
1501     return m;
1502   }
1503 }
1504 
1505 
1506 // We create a list of in-use monitors for each thread.
1507 //
1508 // deflate_thread_local_monitors() scans a single thread's in-use list, while
1509 // deflate_idle_monitors() scans only a global list of in-use monitors which
1510 // is populated only as a thread dies (see omFlush()).
1511 //
1512 // These operations are called at all safepoints, immediately after mutators
1513 // are stopped, but before any objects have moved. Collectively they traverse
1514 // the population of in-use monitors, deflating where possible. The scavenged
1515 // monitors are returned to the monitor free list.
1516 //
1517 // Beware that we scavenge at *every* stop-the-world point. Having a large
1518 // number of monitors in-use could negatively impact performance. We also want
1519 // to minimize the total # of monitors in circulation, as they incur a small
1520 // footprint penalty.
1521 //
1522 // Perversely, the heap size -- and thus the STW safepoint rate --
1523 // typically drives the scavenge rate.  Large heaps can mean infrequent GC,
1524 // which in turn can mean large(r) numbers of objectmonitors in circulation.
1525 // This is an unfortunate aspect of this design.
1526 
1527 // Deflate a single monitor if not in-use
1528 // Return true if deflated, false if in-use
1529 bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj,
1530                                          ObjectMonitor** freeHeadp,
1531                                          ObjectMonitor** freeTailp) {
1532   bool deflated;
1533   // Normal case ... The monitor is associated with obj.
1534   guarantee(obj->mark() == markOopDesc::encode(mid), "invariant");
1535   guarantee(mid == obj->mark()->monitor(), "invariant");
1536   guarantee(mid->header()->is_neutral(), "invariant");






1537 
1538   if (mid->is_busy()) {
1539     deflated = false;
1540   } else {
1541     // Deflate the monitor if it is no longer being used
1542     // It's idle - scavenge and return to the global free list
1543     // plain old deflation ...
1544     if (log_is_enabled(Trace, monitorinflation)) {
1545       ResourceMark rm;
1546       log_trace(monitorinflation)("deflate_monitor: "
1547                                   "object=" INTPTR_FORMAT ", mark=" INTPTR_FORMAT ", type='%s'",
1548                                   p2i(obj), p2i(obj->mark()),
1549                                   obj->klass()->external_name());
1550     }
1551 
1552     // Restore the header back to obj
1553     obj->release_set_mark(mid->header());
1554     mid->clear();
1555 
1556     assert(mid->object() == NULL, "invariant");

1557 
1558     // Move the object to the working free list defined by freeHeadp, freeTailp
1559     if (*freeHeadp == NULL) *freeHeadp = mid;
1560     if (*freeTailp != NULL) {
1561       ObjectMonitor * prevtail = *freeTailp;
1562       assert(prevtail->FreeNext == NULL, "cleaned up deflated?");
1563       prevtail->FreeNext = mid;
1564     }
1565     *freeTailp = mid;
1566     deflated = true;
1567   }
1568   return deflated;
1569 }
1570 
1571 // Walk a given monitor list, and deflate idle monitors
1572 // The given list could be a per-thread list or a global list
1573 // Caller acquires gListLock as needed.
1574 //
1575 // In the case of parallel processing of thread local monitor lists,
1576 // work is done by Threads::parallel_threads_do() which ensures that


2005     *error_cnt_p = *error_cnt_p + 1;
2006   }
2007   if (n->object() == NULL) {
2008     if (jt != NULL) {
2009       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2010                     ": in-use per-thread monitor must have non-NULL _object "
2011                     "field.", p2i(jt), p2i(n));
2012     } else {
2013       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global monitor "
2014                     "must have non-NULL _object field.", p2i(n));
2015     }
2016     *error_cnt_p = *error_cnt_p + 1;
2017   }
2018   const oop obj = (oop)n->object();
2019   const markOop mark = obj->mark();
2020   if (!mark->has_monitor()) {
2021     if (jt != NULL) {
2022       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2023                     ": in-use per-thread monitor's object does not think "
2024                     "it has a monitor: obj=" INTPTR_FORMAT ", mark="
2025                     INTPTR_FORMAT,  p2i(jt), p2i(n), p2i((address)obj),
2026                     p2i((address)mark));
2027     } else {
2028       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global "
2029                     "monitor's object does not think it has a monitor: obj="
2030                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT, p2i(n),
2031                     p2i((address)obj), p2i((address)mark));
2032     }
2033     *error_cnt_p = *error_cnt_p + 1;
2034   }
2035   ObjectMonitor * const obj_mon = mark->monitor();
2036   if (n != obj_mon) {
2037     if (jt != NULL) {
2038       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2039                     ": in-use per-thread monitor's object does not refer "
2040                     "to the same monitor: obj=" INTPTR_FORMAT ", mark="
2041                     INTPTR_FORMAT ", obj_mon=" INTPTR_FORMAT, p2i(jt),
2042                     p2i(n), p2i((address)obj), p2i((address)mark),
2043                     p2i((address)obj_mon));
2044     } else {
2045       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global "
2046                     "monitor's object does not refer to the same monitor: obj="
2047                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT ", obj_mon="
2048                     INTPTR_FORMAT, p2i(n), p2i((address)obj),
2049                     p2i((address)mark), p2i((address)obj_mon));
2050     }
2051     *error_cnt_p = *error_cnt_p + 1;
2052   }
2053 }
2054 
2055 // Check the thread's free list and count; log the results of the checks.
2056 void ObjectSynchronizer::chk_per_thread_free_list_and_count(JavaThread *jt,
2057                                                             outputStream * out,
2058                                                             int *error_cnt_p) {
2059   int chkOmFreeCount = 0;
2060   for (ObjectMonitor * n = jt->omFreeList; n != NULL; n = n->FreeNext) {
2061     chk_free_entry(jt, n, out, error_cnt_p);
2062     chkOmFreeCount++;
2063   }
2064   if (jt->omFreeCount == chkOmFreeCount) {
2065     out->print_cr("jt=" INTPTR_FORMAT ": omFreeCount=%d equals "
2066                   "chkOmFreeCount=%d", p2i(jt), jt->omFreeCount, chkOmFreeCount);
2067   } else {
2068     out->print_cr("ERROR: jt=" INTPTR_FORMAT ": omFreeCount=%d is not "
2069                   "equal to chkOmFreeCount=%d", p2i(jt), jt->omFreeCount,


2088   } else {
2089     out->print_cr("ERROR: jt=" INTPTR_FORMAT ": omInUseCount=%d is not "
2090                   "equal to chkOmInUseCount=%d", p2i(jt), jt->omInUseCount,
2091                   chkOmInUseCount);
2092     *error_cnt_p = *error_cnt_p + 1;
2093   }
2094 }
2095 
2096 // Log details about ObjectMonitors on the in-use lists. The 'BHL'
2097 // flags indicate why the entry is in-use, 'object' and 'object type'
2098 // indicate the associated object and its type.
2099 void ObjectSynchronizer::log_in_use_monitor_details(outputStream * out,
2100                                                     bool on_exit) {
2101   if (!on_exit) {
2102     // Not at VM exit so grab the global list lock.
2103     Thread::muxAcquire(&gListLock, "log_in_use_monitor_details");
2104   }
2105 
2106   if (gOmInUseCount > 0) {
2107     out->print_cr("In-use global monitor info:");
2108     out->print_cr("(B -> is_busy, H -> has hashcode, L -> lock status)");
2109     out->print_cr("%18s  %s  %18s  %18s",
2110                   "monitor", "BHL", "object", "object type");
2111     out->print_cr("==================  ===  ==================  ==================");
2112     for (ObjectMonitor * n = gOmInUseList; n != NULL; n = n->FreeNext) {
2113       const oop obj = (oop) n->object();
2114       const markOop mark = n->header();
2115       ResourceMark rm;
2116       out->print_cr(INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT "  %s", p2i(n),
2117                     n->is_busy() != 0, mark->hash() != 0, n->owner() != NULL,
2118                     p2i(obj), obj->klass()->external_name());
2119     }
2120   }
2121 
2122   if (!on_exit) {
2123     Thread::muxRelease(&gListLock);
2124   }
2125 
2126   out->print_cr("In-use per-thread monitor info:");
2127   out->print_cr("(B -> is_busy, H -> has hashcode, L -> lock status)");
2128   out->print_cr("%18s  %18s  %s  %18s  %18s",
2129                 "jt", "monitor", "BHL", "object", "object type");
2130   out->print_cr("==================  ==================  ===  ==================  ==================");
2131   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2132     for (ObjectMonitor * n = jt->omInUseList; n != NULL; n = n->FreeNext) {
2133       const oop obj = (oop) n->object();
2134       const markOop mark = n->header();
2135       ResourceMark rm;
2136       out->print_cr(INTPTR_FORMAT "  " INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT
2137                     "  %s", p2i(jt), p2i(n), n->is_busy() != 0,
2138                     mark->hash() != 0, n->owner() != NULL, p2i(obj),
2139                     obj->klass()->external_name());
2140     }
2141   }
2142 
2143   out->flush();
2144 }
2145 
2146 // Log counts for the global and per-thread monitor lists and return
2147 // the population count.




 718   assert(!mark->has_bias_pattern(), "invariant");
 719 
 720   if (mark->is_neutral()) {
 721     hash = mark->hash();              // this is a normal header
 722     if (hash != 0) {                  // if it has hash, just return it
 723       return hash;
 724     }
 725     hash = get_next_hash(Self, obj);  // allocate a new hash code
 726     temp = mark->copy_set_hash(hash); // merge the hash code into header
 727     // use (machine word version) atomic operation to install the hash
 728     test = obj->cas_set_mark(temp, mark);
 729     if (test == mark) {
 730       return hash;
 731     }
 732     // If atomic operation failed, we must inflate the header
 733     // into heavy weight monitor. We could add more code here
 734     // for fast path, but it does not worth the complexity.
 735   } else if (mark->has_monitor()) {
 736     monitor = mark->monitor();
 737     temp = monitor->header();
 738     assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp));
 739     hash = temp->hash();
 740     if (hash != 0) {
 741       return hash;
 742     }
 743     // Skip to the following code to reduce code size
 744   } else if (Self->is_lock_owned((address)mark->locker())) {
 745     temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned
 746     assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp));
 747     hash = temp->hash();              // by current thread, check if the displaced
 748     if (hash != 0) {                  // header contains hash code
 749       return hash;
 750     }
 751     // WARNING:
 752     // The displaced header in the BasicLock on a thread's stack
 753     // is strictly immutable. It CANNOT be changed in ANY cases.
 754     // So we have to inflate the stack lock into an ObjectMonitor
 755     // even if the current thread owns the lock. The BasicLock on
 756     // a thread's stack can be asynchronously read by other threads
 757     // during an inflate() call so any change to that stack memory
 758     // may not propagate to other threads correctly.

 759   }
 760 
 761   // Inflate the monitor to set hash code
 762   monitor = inflate(Self, obj, inflate_cause_hash_code);
 763   // Load displaced header and check it has hash code
 764   mark = monitor->header();
 765   assert(mark->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(mark));
 766   hash = mark->hash();
 767   if (hash == 0) {
 768     hash = get_next_hash(Self, obj);
 769     temp = mark->copy_set_hash(hash); // merge hash code into header
 770     assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp));
 771     test = Atomic::cmpxchg(temp, monitor->header_addr(), mark);
 772     if (test != mark) {
 773       // The only update to the ObjectMonitor's header/dmw field
 774       // is to merge in the hash code. If someone adds a new usage
 775       // of the header/dmw field, please update this code.
 776       hash = test->hash();
 777       assert(test->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(test));
 778       assert(hash != 0, "Trivial unexpected object/monitor header usage.");
 779     }
 780   }
 781   // We finally get the hash
 782   return hash;
 783 }
 784 
 785 // Deprecated -- use FastHashCode() instead.
 786 
 787 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
 788   return FastHashCode(Thread::current(), obj());
 789 }
 790 
 791 
 792 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
 793                                                    Handle h_obj) {
 794   if (UseBiasedLocking) {
 795     BiasedLocking::revoke_and_rebias(h_obj, false, thread);
 796     assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 797   }


1316   assert(Universe::verify_in_progress() ||
1317          !SafepointSynchronize::is_at_safepoint(), "invariant");
1318 
1319   EventJavaMonitorInflate event;
1320 
1321   for (;;) {
1322     const markOop mark = object->mark();
1323     assert(!mark->has_bias_pattern(), "invariant");
1324 
1325     // The mark can be in one of the following states:
1326     // *  Inflated     - just return
1327     // *  Stack-locked - coerce it to inflated
1328     // *  INFLATING    - busy wait for conversion to complete
1329     // *  Neutral      - aggressively inflate the object.
1330     // *  BIASED       - Illegal.  We should never see this
1331 
1332     // CASE: inflated
1333     if (mark->has_monitor()) {
1334       ObjectMonitor * inf = mark->monitor();
1335       markOop dmw = inf->header();
1336       assert(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw));
1337       assert(oopDesc::equals((oop) inf->object(), object), "invariant");
1338       assert(ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1339       return inf;
1340     }
1341 
1342     // CASE: inflation in progress - inflating over a stack-lock.
1343     // Some other thread is converting from stack-locked to inflated.
1344     // Only that thread can complete inflation -- other threads must wait.
1345     // The INFLATING value is transient.
1346     // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
1347     // We could always eliminate polling by parking the thread on some auxiliary list.
1348     if (mark == markOopDesc::INFLATING()) {
1349       ReadStableMark(object);
1350       continue;
1351     }
1352 
1353     // CASE: stack-locked
1354     // Could be stack-locked either by this thread or by some other thread.
1355     //
1356     // Note that we allocate the objectmonitor speculatively, _before_ attempting


1380       m->_Responsible  = NULL;
1381       m->_recursions   = 0;
1382       m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;   // Consider: maintain by type/class
1383 
1384       markOop cmp = object->cas_set_mark(markOopDesc::INFLATING(), mark);
1385       if (cmp != mark) {
1386         omRelease(Self, m, true);
1387         continue;       // Interference -- just retry
1388       }
1389 
1390       // We've successfully installed INFLATING (0) into the mark-word.
1391       // This is the only case where 0 will appear in a mark-word.
1392       // Only the singular thread that successfully swings the mark-word
1393       // to 0 can perform (or more precisely, complete) inflation.
1394       //
1395       // Why do we CAS a 0 into the mark-word instead of just CASing the
1396       // mark-word from the stack-locked value directly to the new inflated state?
1397       // Consider what happens when a thread unlocks a stack-locked object.
1398       // It attempts to use CAS to swing the displaced header value from the
1399       // on-stack basiclock back into the object header.  Recall also that the
1400       // header value (hash code, etc) can reside in (a) the object header, or
1401       // (b) a displaced header associated with the stack-lock, or (c) a displaced
1402       // header in an objectMonitor.  The inflate() routine must copy the header
1403       // value from the basiclock on the owner's stack to the objectMonitor, all
1404       // the while preserving the hashCode stability invariants.  If the owner
1405       // decides to release the lock while the value is 0, the unlock will fail
1406       // and control will eventually pass from slow_exit() to inflate.  The owner
1407       // will then spin, waiting for the 0 value to disappear.   Put another way,
1408       // the 0 causes the owner to stall if the owner happens to try to
1409       // drop the lock (restoring the header from the basiclock to the object)
1410       // while inflation is in-progress.  This protocol avoids races that might
1411       // would otherwise permit hashCode values to change or "flicker" for an object.
1412       // Critically, while object->mark is 0 mark->displaced_mark_helper() is stable.
1413       // 0 serves as a "BUSY" inflate-in-progress indicator.
1414 
1415 
1416       // fetch the displaced mark from the owner's stack.
1417       // The owner can't die or unwind past the lock while our INFLATING
1418       // object is in the mark.  Furthermore the owner can't complete
1419       // an unlock on the object, either.
1420       markOop dmw = mark->displaced_mark_helper();
1421       // Catch if the object's header is not neutral (not locked and
1422       // not marked is what we care about here).
1423       assert(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw));
1424 
1425       // Setup monitor fields to proper values -- prepare the monitor
1426       m->set_header(dmw);
1427 
1428       // Optimization: if the mark->locker stack address is associated
1429       // with this thread we could simply set m->_owner = Self.
1430       // Note that a thread can inflate an object
1431       // that it has stack-locked -- as might happen in wait() -- directly
1432       // with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.
1433       m->set_owner(mark->locker());
1434       m->set_object(object);
1435       // TODO-FIXME: assert BasicLock->dhw != 0.
1436 
1437       // Must preserve store ordering. The monitor state must
1438       // be stable at the time of publishing the monitor address.
1439       guarantee(object->mark() == markOopDesc::INFLATING(), "invariant");
1440       object->release_set_mark(markOopDesc::encode(m));
1441 
1442       // Hopefully the performance counters are allocated on distinct cache lines
1443       // to avoid false sharing on MP systems ...


1447         lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark="
1448                      INTPTR_FORMAT ", type='%s'", p2i(object),
1449                      p2i(object->mark()), object->klass()->external_name());
1450       }
1451       if (event.should_commit()) {
1452         post_monitor_inflate_event(&event, object, cause);
1453       }
1454       return m;
1455     }
1456 
1457     // CASE: neutral
1458     // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1459     // If we know we're inflating for entry it's better to inflate by swinging a
1460     // pre-locked objectMonitor pointer into the object header.   A successful
1461     // CAS inflates the object *and* confers ownership to the inflating thread.
1462     // In the current implementation we use a 2-step mechanism where we CAS()
1463     // to inflate and then CAS() again to try to swing _owner from NULL to Self.
1464     // An inflateTry() method that we could call from fast_enter() and slow_enter()
1465     // would be useful.
1466 
1467     // Catch if the object's header is not neutral (not locked and
1468     // not marked is what we care about here).
1469     assert(mark->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(mark));
1470     ObjectMonitor * m = omAlloc(Self);
1471     // prepare m for installation - set monitor to initial state
1472     m->Recycle();
1473     m->set_header(mark);
1474     m->set_owner(NULL);
1475     m->set_object(object);
1476     m->_recursions   = 0;
1477     m->_Responsible  = NULL;
1478     m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;       // consider: keep metastats by type/class
1479 
1480     if (object->cas_set_mark(markOopDesc::encode(m), mark) != mark) {
1481       m->set_header(NULL);
1482       m->set_object(NULL);
1483       m->Recycle();
1484       omRelease(Self, m, true);
1485       m = NULL;
1486       continue;
1487       // interference - the markword changed - just retry.
1488       // The state-transitions are one-way, so there's no chance of
1489       // live-lock -- "Inflated" is an absorbing state.
1490     }
1491 
1492     // Hopefully the performance counters are allocated on distinct
1493     // cache lines to avoid false sharing on MP systems ...
1494     OM_PERFDATA_OP(Inflations, inc());
1495     if (log_is_enabled(Trace, monitorinflation)) {
1496       ResourceMark rm(Self);
1497       lsh.print_cr("inflate(neutral): object=" INTPTR_FORMAT ", mark="
1498                    INTPTR_FORMAT ", type='%s'", p2i(object),
1499                    p2i(object->mark()), object->klass()->external_name());
1500     }
1501     if (event.should_commit()) {
1502       post_monitor_inflate_event(&event, object, cause);
1503     }
1504     return m;
1505   }
1506 }
1507 
1508 
1509 // We maintain a list of in-use monitors for each thread.
1510 //
1511 // deflate_thread_local_monitors() scans a single thread's in-use list, while
1512 // deflate_idle_monitors() scans only a global list of in-use monitors which
1513 // is populated only as a thread dies (see omFlush()).
1514 //
1515 // These operations are called at all safepoints, immediately after mutators
1516 // are stopped, but before any objects have moved. Collectively they traverse
1517 // the population of in-use monitors, deflating where possible. The scavenged
1518 // monitors are returned to the global monitor free list.
1519 //
1520 // Beware that we scavenge at *every* stop-the-world point. Having a large
1521 // number of monitors in-use could negatively impact performance. We also want
1522 // to minimize the total # of monitors in circulation, as they incur a small
1523 // footprint penalty.
1524 //
1525 // Perversely, the heap size -- and thus the STW safepoint rate --
1526 // typically drives the scavenge rate.  Large heaps can mean infrequent GC,
1527 // which in turn can mean large(r) numbers of ObjectMonitors in circulation.
1528 // This is an unfortunate aspect of this design.
1529 
1530 // Deflate a single monitor if not in-use
1531 // Return true if deflated, false if in-use
1532 bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj,
1533                                          ObjectMonitor** freeHeadp,
1534                                          ObjectMonitor** freeTailp) {
1535   bool deflated;
1536   // Normal case ... The monitor is associated with obj.
1537   const markOop mark = obj->mark();
1538   guarantee(mark == markOopDesc::encode(mid), "should match: mark="
1539             INTPTR_FORMAT ", encoded mid=" INTPTR_FORMAT, p2i(mark),
1540             p2i(markOopDesc::encode(mid)));
1541   // Make sure that mark->monitor() and markOopDesc::encode() agree:
1542   guarantee(mark->monitor() == mid, "should match: monitor()=" INTPTR_FORMAT
1543             ", mid=" INTPTR_FORMAT, p2i(mark->monitor()), p2i(mid));
1544   const markOop dmw = mid->header();
1545   guarantee(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw));
1546 
1547   if (mid->is_busy()) {
1548     deflated = false;
1549   } else {
1550     // Deflate the monitor if it is no longer being used
1551     // It's idle - scavenge and return to the global free list
1552     // plain old deflation ...
1553     if (log_is_enabled(Trace, monitorinflation)) {
1554       ResourceMark rm;
1555       log_trace(monitorinflation)("deflate_monitor: "
1556                                   "object=" INTPTR_FORMAT ", mark="
1557                                   INTPTR_FORMAT ", type='%s'", p2i(obj),
1558                                   p2i(mark), obj->klass()->external_name());
1559     }
1560 
1561     // Restore the header back to obj
1562     obj->release_set_mark(dmw);
1563     mid->clear();
1564 
1565     assert(mid->object() == NULL, "invariant: object=" INTPTR_FORMAT,
1566            p2i(mid->object()));
1567 
1568     // Move the object to the working free list defined by freeHeadp, freeTailp
1569     if (*freeHeadp == NULL) *freeHeadp = mid;
1570     if (*freeTailp != NULL) {
1571       ObjectMonitor * prevtail = *freeTailp;
1572       assert(prevtail->FreeNext == NULL, "cleaned up deflated?");
1573       prevtail->FreeNext = mid;
1574     }
1575     *freeTailp = mid;
1576     deflated = true;
1577   }
1578   return deflated;
1579 }
1580 
1581 // Walk a given monitor list, and deflate idle monitors
1582 // The given list could be a per-thread list or a global list
1583 // Caller acquires gListLock as needed.
1584 //
1585 // In the case of parallel processing of thread local monitor lists,
1586 // work is done by Threads::parallel_threads_do() which ensures that


2015     *error_cnt_p = *error_cnt_p + 1;
2016   }
2017   if (n->object() == NULL) {
2018     if (jt != NULL) {
2019       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2020                     ": in-use per-thread monitor must have non-NULL _object "
2021                     "field.", p2i(jt), p2i(n));
2022     } else {
2023       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global monitor "
2024                     "must have non-NULL _object field.", p2i(n));
2025     }
2026     *error_cnt_p = *error_cnt_p + 1;
2027   }
2028   const oop obj = (oop)n->object();
2029   const markOop mark = obj->mark();
2030   if (!mark->has_monitor()) {
2031     if (jt != NULL) {
2032       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2033                     ": in-use per-thread monitor's object does not think "
2034                     "it has a monitor: obj=" INTPTR_FORMAT ", mark="
2035                     INTPTR_FORMAT,  p2i(jt), p2i(n), p2i(obj), p2i(mark));

2036     } else {
2037       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global "
2038                     "monitor's object does not think it has a monitor: obj="
2039                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT, p2i(n),
2040                     p2i(obj), p2i(mark));
2041     }
2042     *error_cnt_p = *error_cnt_p + 1;
2043   }
2044   ObjectMonitor * const obj_mon = mark->monitor();
2045   if (n != obj_mon) {
2046     if (jt != NULL) {
2047       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2048                     ": in-use per-thread monitor's object does not refer "
2049                     "to the same monitor: obj=" INTPTR_FORMAT ", mark="
2050                     INTPTR_FORMAT ", obj_mon=" INTPTR_FORMAT, p2i(jt),
2051                     p2i(n), p2i(obj), p2i(mark), p2i(obj_mon));

2052     } else {
2053       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global "
2054                     "monitor's object does not refer to the same monitor: obj="
2055                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT ", obj_mon="
2056                     INTPTR_FORMAT, p2i(n), p2i(obj), p2i(mark), p2i(obj_mon));

2057     }
2058     *error_cnt_p = *error_cnt_p + 1;
2059   }
2060 }
2061 
2062 // Check the thread's free list and count; log the results of the checks.
2063 void ObjectSynchronizer::chk_per_thread_free_list_and_count(JavaThread *jt,
2064                                                             outputStream * out,
2065                                                             int *error_cnt_p) {
2066   int chkOmFreeCount = 0;
2067   for (ObjectMonitor * n = jt->omFreeList; n != NULL; n = n->FreeNext) {
2068     chk_free_entry(jt, n, out, error_cnt_p);
2069     chkOmFreeCount++;
2070   }
2071   if (jt->omFreeCount == chkOmFreeCount) {
2072     out->print_cr("jt=" INTPTR_FORMAT ": omFreeCount=%d equals "
2073                   "chkOmFreeCount=%d", p2i(jt), jt->omFreeCount, chkOmFreeCount);
2074   } else {
2075     out->print_cr("ERROR: jt=" INTPTR_FORMAT ": omFreeCount=%d is not "
2076                   "equal to chkOmFreeCount=%d", p2i(jt), jt->omFreeCount,


2095   } else {
2096     out->print_cr("ERROR: jt=" INTPTR_FORMAT ": omInUseCount=%d is not "
2097                   "equal to chkOmInUseCount=%d", p2i(jt), jt->omInUseCount,
2098                   chkOmInUseCount);
2099     *error_cnt_p = *error_cnt_p + 1;
2100   }
2101 }
2102 
2103 // Log details about ObjectMonitors on the in-use lists. The 'BHL'
2104 // flags indicate why the entry is in-use, 'object' and 'object type'
2105 // indicate the associated object and its type.
2106 void ObjectSynchronizer::log_in_use_monitor_details(outputStream * out,
2107                                                     bool on_exit) {
2108   if (!on_exit) {
2109     // Not at VM exit so grab the global list lock.
2110     Thread::muxAcquire(&gListLock, "log_in_use_monitor_details");
2111   }
2112 
2113   if (gOmInUseCount > 0) {
2114     out->print_cr("In-use global monitor info:");
2115     out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2116     out->print_cr("%18s  %s  %18s  %18s",
2117                   "monitor", "BHL", "object", "object type");
2118     out->print_cr("==================  ===  ==================  ==================");
2119     for (ObjectMonitor * n = gOmInUseList; n != NULL; n = n->FreeNext) {
2120       const oop obj = (oop) n->object();
2121       const markOop mark = n->header();
2122       ResourceMark rm;
2123       out->print_cr(INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT "  %s", p2i(n),
2124                     n->is_busy() != 0, mark->hash() != 0, n->owner() != NULL,
2125                     p2i(obj), obj->klass()->external_name());
2126     }
2127   }
2128 
2129   if (!on_exit) {
2130     Thread::muxRelease(&gListLock);
2131   }
2132 
2133   out->print_cr("In-use per-thread monitor info:");
2134   out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2135   out->print_cr("%18s  %18s  %s  %18s  %18s",
2136                 "jt", "monitor", "BHL", "object", "object type");
2137   out->print_cr("==================  ==================  ===  ==================  ==================");
2138   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2139     for (ObjectMonitor * n = jt->omInUseList; n != NULL; n = n->FreeNext) {
2140       const oop obj = (oop) n->object();
2141       const markOop mark = n->header();
2142       ResourceMark rm;
2143       out->print_cr(INTPTR_FORMAT "  " INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT
2144                     "  %s", p2i(jt), p2i(n), n->is_busy() != 0,
2145                     mark->hash() != 0, n->owner() != NULL, p2i(obj),
2146                     obj->klass()->external_name());
2147     }
2148   }
2149 
2150   out->flush();
2151 }
2152 
2153 // Log counts for the global and per-thread monitor lists and return
2154 // the population count.


< prev index next >