< prev index next >

src/hotspot/share/runtime/synchronizer.cpp

Print this page

  46 #include "runtime/objectMonitor.inline.hpp"
  47 #include "runtime/os.inline.hpp"
  48 #include "runtime/osThread.hpp"
  49 #include "runtime/perfData.hpp"
  50 #include "runtime/safepointMechanism.inline.hpp"
  51 #include "runtime/safepointVerifiers.hpp"
  52 #include "runtime/sharedRuntime.hpp"
  53 #include "runtime/stubRoutines.hpp"
  54 #include "runtime/synchronizer.hpp"
  55 #include "runtime/threads.hpp"
  56 #include "runtime/timer.hpp"
  57 #include "runtime/trimNativeHeap.hpp"
  58 #include "runtime/vframe.hpp"
  59 #include "runtime/vmThread.hpp"
  60 #include "utilities/align.hpp"
  61 #include "utilities/dtrace.hpp"
  62 #include "utilities/events.hpp"
  63 #include "utilities/linkedlist.hpp"
  64 #include "utilities/preserveException.hpp"
  65 
  66 class ObjectMonitorsHashtable::PtrList :
  67   public LinkedListImpl<ObjectMonitor*,
  68                         AnyObj::C_HEAP, mtThread,
  69                         AllocFailStrategy::RETURN_NULL> {};
  70 
  71 class CleanupObjectMonitorsHashtable: StackObj {
  72  public:
  73   bool do_entry(void*& key, ObjectMonitorsHashtable::PtrList*& list) {
  74     list->clear();  // clear the LinkListNodes
  75     delete list;    // then delete the LinkedList
  76     return true;
  77   }
  78 };
  79 
  80 ObjectMonitorsHashtable::~ObjectMonitorsHashtable() {
  81   CleanupObjectMonitorsHashtable cleanup;
  82   _ptrs->unlink(&cleanup);  // cleanup the LinkedLists
  83   delete _ptrs;             // then delete the hash table
  84 }
  85 
  86 void ObjectMonitorsHashtable::add_entry(void* key, ObjectMonitor* om) {
  87   ObjectMonitorsHashtable::PtrList* list = get_entry(key);
  88   if (list == nullptr) {
  89     // Create new list and add it to the hash table:
  90     list = new (mtThread) ObjectMonitorsHashtable::PtrList;
  91     add_entry(key, list);
  92   }
  93   list->add(om);  // Add the ObjectMonitor to the list.
  94   _om_count++;
  95 }
  96 
  97 bool ObjectMonitorsHashtable::has_entry(void* key, ObjectMonitor* om) {
  98   ObjectMonitorsHashtable::PtrList* list = get_entry(key);
  99   if (list == nullptr || list->find(om) == nullptr) {
 100     return false;
 101   }
 102   return true;
 103 }
 104 
 105 void MonitorList::add(ObjectMonitor* m) {
 106   ObjectMonitor* head;
 107   do {
 108     head = Atomic::load(&_head);
 109     m->set_next_om(head);
 110   } while (Atomic::cmpxchg(&_head, head, m) != head);
 111 
 112   size_t count = Atomic::add(&_count, 1u);
 113   if (count > max()) {
 114     Atomic::inc(&_max);
 115   }
 116 }
 117 
 118 size_t MonitorList::count() const {
 119   return Atomic::load(&_count);
 120 }
 121 
 122 size_t MonitorList::max() const {
 123   return Atomic::load(&_max);
 124 }
 125 
 126 // Walk the in-use list and unlink (at most MonitorDeflationMax) deflated
 127 // ObjectMonitors. Returns the number of unlinked ObjectMonitors.
 128 size_t MonitorList::unlink_deflated(Thread* current, LogStream* ls,
 129                                     elapsedTimer* timer_p,

 130                                     GrowableArray<ObjectMonitor*>* unlinked_list) {
 131   size_t unlinked_count = 0;
 132   ObjectMonitor* prev = nullptr;
 133   ObjectMonitor* head = Atomic::load_acquire(&_head);
 134   ObjectMonitor* m = head;
 135   // The in-use list head can be null during the final audit.
 136   while (m != nullptr) {
 137     if (m->is_being_async_deflated()) {
 138       // Find next live ObjectMonitor.


 139       ObjectMonitor* next = m;




 140       do {
 141         ObjectMonitor* next_next = next->next_om();
 142         unlinked_count++;
 143         unlinked_list->append(next);
 144         next = next_next;
 145         if (unlinked_count >= (size_t)MonitorDeflationMax) {
 146           // Reached the max so bail out on the gathering loop.






 147           break;
 148         }
 149       } while (next != nullptr && next->is_being_async_deflated());


 150       if (prev == nullptr) {
 151         ObjectMonitor* prev_head = Atomic::cmpxchg(&_head, head, next);
 152         if (prev_head != head) {
 153           // Find new prev ObjectMonitor that just got inserted.


 154           for (ObjectMonitor* n = prev_head; n != m; n = n->next_om()) {
 155             prev = n;
 156           }

 157           prev->set_next_om(next);
 158         }
 159       } else {



 160         prev->set_next_om(next);
 161       }
 162       if (unlinked_count >= (size_t)MonitorDeflationMax) {
 163         // Reached the max so bail out on the searching loop.



 164         break;
 165       }
 166       m = next;
 167     } else {
 168       prev = m;
 169       m = m->next_om();
 170     }
 171 
 172     if (current->is_Java_thread()) {
 173       // A JavaThread must check for a safepoint/handshake and honor it.
 174       ObjectSynchronizer::chk_for_block_req(JavaThread::cast(current), "unlinking",
 175                                             "unlinked_count", unlinked_count,
 176                                             ls, timer_p);
 177     }
 178   }














 179   Atomic::sub(&_count, unlinked_count);
 180   return unlinked_count;
 181 }
 182 
 183 MonitorList::Iterator MonitorList::iterator() const {
 184   return Iterator(Atomic::load_acquire(&_head));
 185 }
 186 
 187 ObjectMonitor* MonitorList::Iterator::next() {
 188   ObjectMonitor* current = _current;
 189   _current = current->next_om();
 190   return current;
 191 }
 192 
 193 // The "core" versions of monitor enter and exit reside in this file.
 194 // The interpreter and compilers contain specialized transliterated
 195 // variants of the enter-exit fast-path operations.  See c2_MacroAssembler_x86.cpp
 196 // fast_lock(...) for instance.  If you make changes here, make sure to modify the
 197 // interpreter, and both C1 and C2 fast-path inline locking code emission.
 198 //

1082   if (mark.has_monitor()) {
1083     // Inflated monitor so header points to ObjectMonitor (tagged pointer).
1084     // The first stage of async deflation does not affect any field
1085     // used by this comparison so the ObjectMonitor* is usable here.
1086     ObjectMonitor* monitor = mark.monitor();
1087     assert(monitor != nullptr, "monitor should be non-null");
1088     // owning_thread_from_monitor() may also return null here:
1089     return Threads::owning_thread_from_monitor(t_list, monitor);
1090   }
1091 
1092   // Unlocked case, header in place
1093   // Cannot have assertion since this object may have been
1094   // locked by another thread when reaching here.
1095   // assert(mark.is_neutral(), "sanity check");
1096 
1097   return nullptr;
1098 }
1099 
1100 // Visitors ...
1101 
1102 // Iterate ObjectMonitors where the owner == thread; this does NOT include
1103 // ObjectMonitors where owner is set to a stack-lock address in thread.
1104 //
1105 // This version of monitors_iterate() works with the in-use monitor list.
1106 //
1107 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure, JavaThread* thread) {
1108   MonitorList::Iterator iter = _in_use_list.iterator();
1109   while (iter.has_next()) {
1110     ObjectMonitor* mid = iter.next();
1111     if (mid->owner() != thread) {
1112       // Not owned by the target thread and intentionally skips when owner
1113       // is set to a stack-lock address in the target thread.
1114       continue;
1115     }
1116     if (!mid->is_being_async_deflated() && mid->object_peek() != nullptr) {
1117       // Only process with closure if the object is set.
1118 
1119       // monitors_iterate() is only called at a safepoint or when the
1120       // target thread is suspended or when the target thread is
1121       // operating on itself. The current closures in use today are
1122       // only interested in an owned ObjectMonitor and ownership
1123       // cannot be dropped under the calling contexts so the
1124       // ObjectMonitor cannot be async deflated.
1125       closure->do_monitor(mid);
1126     }
1127   }
1128 }
1129 
1130 // This version of monitors_iterate() works with the specified linked list.
1131 //
1132 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure,
1133                                           ObjectMonitorsHashtable::PtrList* list,
1134                                           JavaThread* thread) {
1135   typedef LinkedListIterator<ObjectMonitor*> ObjectMonitorIterator;
1136   ObjectMonitorIterator iter(list->head());
1137   while (!iter.is_empty()) {
1138     ObjectMonitor* mid = *iter.next();
1139     // Owner set to a stack-lock address in thread should never be seen here:
1140     assert(mid->owner() == thread, "must be");
1141     if (!mid->is_being_async_deflated() && mid->object_peek() != nullptr) {
1142       // Only process with closure if the object is set.
1143 
1144       // monitors_iterate() is only called at a safepoint or when the
1145       // target thread is suspended or when the target thread is
1146       // operating on itself. The current closures in use today are
1147       // only interested in an owned ObjectMonitor and ownership
1148       // cannot be dropped under the calling contexts so the
1149       // ObjectMonitor cannot be async deflated.
1150       closure->do_monitor(mid);
1151     }
1152   }













1153 }
1154 
1155 static bool monitors_used_above_threshold(MonitorList* list) {
1156   if (MonitorUsedDeflationThreshold == 0) {  // disabled case is easy
1157     return false;
1158   }
1159   // Start with ceiling based on a per-thread estimate:
1160   size_t ceiling = ObjectSynchronizer::in_use_list_ceiling();
1161   size_t old_ceiling = ceiling;
1162   if (ceiling < list->max()) {
1163     // The max used by the system has exceeded the ceiling so use that:
1164     ceiling = list->max();
1165   }
1166   size_t monitors_used = list->count();
1167   if (monitors_used == 0) {  // empty list is easy
1168     return false;
1169   }
1170   if (NoAsyncDeflationProgressMax != 0 &&
1171       _no_progress_cnt >= NoAsyncDeflationProgressMax) {
1172     float remainder = (100.0 - MonitorUsedDeflationThreshold) / 100.0;

1239 
1240     // If this deflation has no progress, then it should not affect the no-progress
1241     // tracking, otherwise threshold heuristics would think it was triggered, experienced
1242     // no progress, and needs to backoff more aggressively. In this "no progress" case,
1243     // the generic code would bump the no-progress counter, and we compensate for that
1244     // by telling it to skip the update.
1245     //
1246     // If this deflation has progress, then it should let non-progress tracking
1247     // know about this, otherwise the threshold heuristics would kick in, potentially
1248     // experience no-progress due to aggressive cleanup by this deflation, and think
1249     // it is still in no-progress stride. In this "progress" case, the generic code would
1250     // zero the counter, and we allow it to happen.
1251     _no_progress_skip_increment = true;
1252 
1253     return true;
1254   }
1255 
1256   return false;
1257 }
1258 
1259 bool ObjectSynchronizer::request_deflate_idle_monitors() {






1260   JavaThread* current = JavaThread::current();
1261   bool ret_code = false;
1262 
1263   jlong last_time = last_async_deflation_time_ns();
1264   set_is_async_deflation_requested(true);
1265   {
1266     MonitorLocker ml(MonitorDeflation_lock, Mutex::_no_safepoint_check_flag);
1267     ml.notify_all();
1268   }
1269   const int N_CHECKS = 5;
1270   for (int i = 0; i < N_CHECKS; i++) {  // sleep for at most 5 seconds
1271     if (last_async_deflation_time_ns() > last_time) {
1272       log_info(monitorinflation)("Async Deflation happened after %d check(s).", i);
1273       ret_code = true;
1274       break;
1275     }
1276     {
1277       // JavaThread has to honor the blocking protocol.
1278       ThreadBlockInVM tbivm(current);
1279       os::naked_short_sleep(999);  // sleep for almost 1 second
1280     }
1281   }
1282   if (!ret_code) {
1283     log_info(monitorinflation)("Async Deflation DID NOT happen after %d checks.", N_CHECKS);
1284   }
1285 
1286   return ret_code;
1287 }
1288 

1565                  op_name, cnt_name, cnt, in_use_list_ceiling(),
1566                  _in_use_list.count(), _in_use_list.max());
1567   }
1568 
1569   {
1570     // Honor block request.
1571     ThreadBlockInVM tbivm(current);
1572   }
1573 
1574   if (ls != nullptr) {
1575     ls->print_cr("resuming %s: in_use_list stats: ceiling=" SIZE_FORMAT
1576                  ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT, op_name,
1577                  in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1578     timer_p->start();
1579   }
1580 }
1581 
1582 // Walk the in-use list and deflate (at most MonitorDeflationMax) idle
1583 // ObjectMonitors. Returns the number of deflated ObjectMonitors.
1584 //
1585 // If table != nullptr, we gather owned ObjectMonitors indexed by the
1586 // owner in the table. Please note that ObjectMonitors where the owner
1587 // is set to a stack-lock address are NOT associated with the JavaThread
1588 // that holds that stack-lock. All of the current consumers of
1589 // ObjectMonitorsHashtable info only care about JNI locked monitors and
1590 // those do not have the owner set to a stack-lock address.
1591 //
1592 size_t ObjectSynchronizer::deflate_monitor_list(Thread* current, LogStream* ls,
1593                                                 elapsedTimer* timer_p,
1594                                                 ObjectMonitorsHashtable* table) {
1595   MonitorList::Iterator iter = _in_use_list.iterator();
1596   size_t deflated_count = 0;
1597 
1598   while (iter.has_next()) {
1599     if (deflated_count >= (size_t)MonitorDeflationMax) {
1600       break;
1601     }
1602     ObjectMonitor* mid = iter.next();
1603     if (mid->deflate_monitor()) {
1604       deflated_count++;
1605     } else if (table != nullptr) {
1606       // The caller is interested in the owned ObjectMonitors. This does
1607       // not include when owner is set to a stack-lock address in thread.
1608       // This also does not capture unowned ObjectMonitors that cannot be
1609       // deflated because of a waiter.
1610       void* key = mid->owner();
1611       // Since deflate_idle_monitors() and deflate_monitor_list() can be
1612       // called more than once, we have to make sure the entry has not
1613       // already been added.
1614       if (key != nullptr && !table->has_entry(key, mid)) {
1615         table->add_entry(key, mid);
1616       }
1617     }
1618 
1619     if (current->is_Java_thread()) {
1620       // A JavaThread must check for a safepoint/handshake and honor it.
1621       chk_for_block_req(JavaThread::cast(current), "deflation", "deflated_count",
1622                         deflated_count, ls, timer_p);
1623     }
1624   }
1625 
1626   return deflated_count;
1627 }
1628 
1629 class HandshakeForDeflation : public HandshakeClosure {
1630  public:
1631   HandshakeForDeflation() : HandshakeClosure("HandshakeForDeflation") {}
1632 
1633   void do_thread(Thread* thread) {
1634     log_trace(monitorinflation)("HandshakeForDeflation::do_thread: thread="
1635                                 INTPTR_FORMAT, p2i(thread));
1636   }

1646   };
1647 };
1648 
1649 static size_t delete_monitors(Thread* current, GrowableArray<ObjectMonitor*>* delete_list,
1650                               LogStream* ls, elapsedTimer* timer_p) {
1651   NativeHeapTrimmer::SuspendMark sm("monitor deletion");
1652   size_t deleted_count = 0;
1653   for (ObjectMonitor* monitor: *delete_list) {
1654     delete monitor;
1655     deleted_count++;
1656     if (current->is_Java_thread()) {
1657       // A JavaThread must check for a safepoint/handshake and honor it.
1658       ObjectSynchronizer::chk_for_block_req(JavaThread::cast(current), "deletion", "deleted_count",
1659                                             deleted_count, ls, timer_p);
1660     }
1661   }
1662   return deleted_count;
1663 }
1664 
1665 // This function is called by the MonitorDeflationThread to deflate
1666 // ObjectMonitors. It is also called via do_final_audit_and_print_stats()
1667 // and VM_ThreadDump::doit() by the VMThread.
1668 size_t ObjectSynchronizer::deflate_idle_monitors(ObjectMonitorsHashtable* table) {
1669   Thread* current = Thread::current();
1670   if (current->is_Java_thread()) {
1671     // The async deflation request has been processed.
1672     _last_async_deflation_time_ns = os::javaTimeNanos();
1673     set_is_async_deflation_requested(false);
1674   }
1675 
1676   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1677   LogStreamHandle(Info, monitorinflation) lsh_info;
1678   LogStream* ls = nullptr;
1679   if (log_is_enabled(Debug, monitorinflation)) {
1680     ls = &lsh_debug;
1681   } else if (log_is_enabled(Info, monitorinflation)) {
1682     ls = &lsh_info;
1683   }
1684 
1685   elapsedTimer timer;
1686   if (ls != nullptr) {
1687     ls->print_cr("begin deflating: in_use_list stats: ceiling=" SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1688                  in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1689     timer.start();
1690   }
1691 
1692   // Deflate some idle ObjectMonitors.
1693   size_t deflated_count = deflate_monitor_list(current, ls, &timer, table);
1694   size_t unlinked_count = 0;
1695   size_t deleted_count = 0;
1696   if (deflated_count > 0 || is_final_audit()) {
1697     // There are ObjectMonitors that have been deflated or this is the
1698     // final audit and all the remaining ObjectMonitors have been
1699     // deflated, BUT the MonitorDeflationThread blocked for the final
1700     // safepoint during unlinking.
1701 
1702     // Unlink deflated ObjectMonitors from the in-use list.
1703     ResourceMark rm;
1704     GrowableArray<ObjectMonitor*> delete_list((int)deflated_count);
1705     unlinked_count = _in_use_list.unlink_deflated(current, ls, &timer, &delete_list);
1706     if (current->is_monitor_deflation_thread()) {
1707       if (ls != nullptr) {
1708         timer.stop();
1709         ls->print_cr("before handshaking: unlinked_count=" SIZE_FORMAT
1710                      ", in_use_list stats: ceiling=" SIZE_FORMAT ", count="
1711                      SIZE_FORMAT ", max=" SIZE_FORMAT,
1712                      unlinked_count, in_use_list_ceiling(),
1713                      _in_use_list.count(), _in_use_list.max());
1714       }
1715 
1716       // A JavaThread needs to handshake in order to safely free the
1717       // ObjectMonitors that were deflated in this cycle.
1718       HandshakeForDeflation hfd_hc;
1719       Handshake::execute(&hfd_hc);
1720       // Also, we sync and desync GC threads around the handshake, so that they can
1721       // safely read the mark-word and look-through to the object-monitor, without
1722       // being afraid that the object-monitor is going away.
1723       VM_RendezvousGCThreads sync_gc;
1724       VMThread::execute(&sync_gc);
1725 

1732     } else {
1733       // This is not a monitor deflation thread.
1734       // No handshake or rendezvous is needed when we are already at safepoint.
1735       assert_at_safepoint();
1736     }
1737 
1738     // After the handshake, safely free the ObjectMonitors that were
1739     // deflated and unlinked in this cycle.
1740     deleted_count = delete_monitors(current, &delete_list, ls, &timer);
1741     assert(unlinked_count == deleted_count, "must be");
1742   }
1743 
1744   if (ls != nullptr) {
1745     timer.stop();
1746     if (deflated_count != 0 || unlinked_count != 0 || log_is_enabled(Debug, monitorinflation)) {
1747       ls->print_cr("deflated_count=" SIZE_FORMAT ", {unlinked,deleted}_count=" SIZE_FORMAT " monitors in %3.7f secs",
1748                    deflated_count, unlinked_count, timer.seconds());
1749     }
1750     ls->print_cr("end deflating: in_use_list stats: ceiling=" SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1751                  in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1752     if (table != nullptr) {
1753       ls->print_cr("ObjectMonitorsHashtable: key_count=" SIZE_FORMAT ", om_count=" SIZE_FORMAT,
1754                    table->key_count(), table->om_count());
1755     }
1756   }
1757 
1758   OM_PERFDATA_OP(MonExtant, set_value(_in_use_list.count()));
1759   OM_PERFDATA_OP(Deflations, inc(deflated_count));
1760 
1761   GVars.stw_random = os::random();
1762 
1763   if (deflated_count != 0) {
1764     _no_progress_cnt = 0;
1765   } else if (_no_progress_skip_increment) {
1766     _no_progress_skip_increment = false;
1767   } else {
1768     _no_progress_cnt++;
1769   }
1770 
1771   return deflated_count;
1772 }
1773 
1774 // Monitor cleanup on JavaThread::exit
1775 

1788 
1789 // Release all inflated monitors owned by current thread.  Lightweight monitors are
1790 // ignored.  This is meant to be called during JNI thread detach which assumes
1791 // all remaining monitors are heavyweight.  All exceptions are swallowed.
1792 // Scanning the extant monitor list can be time consuming.
1793 // A simple optimization is to add a per-thread flag that indicates a thread
1794 // called jni_monitorenter() during its lifetime.
1795 //
1796 // Instead of NoSafepointVerifier it might be cheaper to
1797 // use an idiom of the form:
1798 //   auto int tmp = SafepointSynchronize::_safepoint_counter ;
1799 //   <code that must not run at safepoint>
1800 //   guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
1801 // Since the tests are extremely cheap we could leave them enabled
1802 // for normal product builds.
1803 
1804 void ObjectSynchronizer::release_monitors_owned_by_thread(JavaThread* current) {
1805   assert(current == JavaThread::current(), "must be current Java thread");
1806   NoSafepointVerifier nsv;
1807   ReleaseJavaMonitorsClosure rjmc(current);
1808   ObjectSynchronizer::monitors_iterate(&rjmc, current);
1809   assert(!current->has_pending_exception(), "Should not be possible");
1810   current->clear_pending_exception();
1811   assert(current->held_monitor_count() == 0, "Should not be possible");
1812   // All monitors (including entered via JNI) have been unlocked above, so we need to clear jni count.
1813   current->clear_jni_monitor_count();
1814 }
1815 
1816 const char* ObjectSynchronizer::inflate_cause_name(const InflateCause cause) {
1817   switch (cause) {
1818     case inflate_cause_vm_internal:    return "VM Internal";
1819     case inflate_cause_monitor_enter:  return "Monitor Enter";
1820     case inflate_cause_wait:           return "Monitor Wait";
1821     case inflate_cause_notify:         return "Monitor Notify";
1822     case inflate_cause_hash_code:      return "Monitor Hash Code";
1823     case inflate_cause_jni_enter:      return "JNI Monitor Enter";
1824     case inflate_cause_jni_exit:       return "JNI Monitor Exit";
1825     default:
1826       ShouldNotReachHere();
1827   }
1828   return "Unknown";

1842 size_t ObjectSynchronizer::get_gvars_size() {
1843   return sizeof(SharedGlobals);
1844 }
1845 
1846 u_char* ObjectSynchronizer::get_gvars_stw_random_addr() {
1847   return (u_char*)&GVars.stw_random;
1848 }
1849 
1850 // Do the final audit and print of ObjectMonitor stats; must be done
1851 // by the VMThread at VM exit time.
1852 void ObjectSynchronizer::do_final_audit_and_print_stats() {
1853   assert(Thread::current()->is_VM_thread(), "sanity check");
1854 
1855   if (is_final_audit()) {  // Only do the audit once.
1856     return;
1857   }
1858   set_is_final_audit();
1859   log_info(monitorinflation)("Starting the final audit.");
1860 
1861   if (log_is_enabled(Info, monitorinflation)) {
1862     // Do deflations in order to reduce the in-use monitor population
1863     // that is reported by ObjectSynchronizer::log_in_use_monitor_details()
1864     // which is called by ObjectSynchronizer::audit_and_print_stats().
1865     while (deflate_idle_monitors(/* ObjectMonitorsHashtable is not needed here */ nullptr) > 0) {
1866       ; // empty
1867     }
1868     // The other audit_and_print_stats() call is done at the Debug
1869     // level at a safepoint in SafepointSynchronize::do_cleanup_tasks.
1870     audit_and_print_stats(true /* on_exit */);
1871   }
1872 }
1873 
1874 // This function can be called at a safepoint or it can be called when
1875 // we are trying to exit the VM. When we are trying to exit the VM, the
1876 // list walker functions can run in parallel with the other list
1877 // operations so spin-locking is used for safety.
1878 //
1879 // Calls to this function can be added in various places as a debugging
1880 // aid; pass 'true' for the 'on_exit' parameter to have in-use monitor
1881 // details logged at the Info level and 'false' for the 'on_exit'
1882 // parameter to have in-use monitor details logged at the Trace level.
1883 //
1884 void ObjectSynchronizer::audit_and_print_stats(bool on_exit) {
1885   assert(on_exit || SafepointSynchronize::is_at_safepoint(), "invariant");
1886 
1887   LogStreamHandle(Debug, monitorinflation) lsh_debug;

1896     ls = &lsh_info;
1897   }
1898   assert(ls != nullptr, "sanity check");
1899 
1900   int error_cnt = 0;
1901 
1902   ls->print_cr("Checking in_use_list:");
1903   chk_in_use_list(ls, &error_cnt);
1904 
1905   if (error_cnt == 0) {
1906     ls->print_cr("No errors found in in_use_list checks.");
1907   } else {
1908     log_error(monitorinflation)("found in_use_list errors: error_cnt=%d", error_cnt);
1909   }
1910 
1911   if ((on_exit && log_is_enabled(Info, monitorinflation)) ||
1912       (!on_exit && log_is_enabled(Trace, monitorinflation))) {
1913     // When exiting this log output is at the Info level. When called
1914     // at a safepoint, this log output is at the Trace level since
1915     // there can be a lot of it.
1916     log_in_use_monitor_details(ls);
1917   }
1918 
1919   ls->flush();
1920 
1921   guarantee(error_cnt == 0, "ERROR: found monitor list errors: error_cnt=%d", error_cnt);
1922 }
1923 
1924 // Check the in_use_list; log the results of the checks.
1925 void ObjectSynchronizer::chk_in_use_list(outputStream* out, int *error_cnt_p) {
1926   size_t l_in_use_count = _in_use_list.count();
1927   size_t l_in_use_max = _in_use_list.max();
1928   out->print_cr("count=" SIZE_FORMAT ", max=" SIZE_FORMAT, l_in_use_count,
1929                 l_in_use_max);
1930 
1931   size_t ck_in_use_count = 0;
1932   MonitorList::Iterator iter = _in_use_list.iterator();
1933   while (iter.has_next()) {
1934     ObjectMonitor* mid = iter.next();
1935     chk_in_use_entry(mid, out, error_cnt_p);
1936     ck_in_use_count++;

1942   } else {
1943     out->print_cr("WARNING: in_use_count=" SIZE_FORMAT " is not equal to "
1944                   "ck_in_use_count=" SIZE_FORMAT, l_in_use_count,
1945                   ck_in_use_count);
1946   }
1947 
1948   size_t ck_in_use_max = _in_use_list.max();
1949   if (l_in_use_max == ck_in_use_max) {
1950     out->print_cr("in_use_max=" SIZE_FORMAT " equals ck_in_use_max="
1951                   SIZE_FORMAT, l_in_use_max, ck_in_use_max);
1952   } else {
1953     out->print_cr("WARNING: in_use_max=" SIZE_FORMAT " is not equal to "
1954                   "ck_in_use_max=" SIZE_FORMAT, l_in_use_max, ck_in_use_max);
1955   }
1956 }
1957 
1958 // Check an in-use monitor entry; log any errors.
1959 void ObjectSynchronizer::chk_in_use_entry(ObjectMonitor* n, outputStream* out,
1960                                           int* error_cnt_p) {
1961   if (n->owner_is_DEFLATER_MARKER()) {
1962     // This should not happen, but if it does, it is not fatal.
1963     out->print_cr("WARNING: monitor=" INTPTR_FORMAT ": in-use monitor is "
1964                   "deflated.", p2i(n));
1965     return;
1966   }

1967   if (n->header().value() == 0) {
1968     out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor must "
1969                   "have non-null _header field.", p2i(n));
1970     *error_cnt_p = *error_cnt_p + 1;
1971   }
1972   const oop obj = n->object_peek();
1973   if (obj != nullptr) {
1974     const markWord mark = obj->mark();
1975     if (!mark.has_monitor()) {
1976       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor's "
1977                     "object does not think it has a monitor: obj="
1978                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT, p2i(n),
1979                     p2i(obj), mark.value());
1980       *error_cnt_p = *error_cnt_p + 1;
1981     }
1982     ObjectMonitor* const obj_mon = mark.monitor();
1983     if (n != obj_mon) {
1984       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor's "
1985                     "object does not refer to the same monitor: obj="
1986                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT ", obj_mon="
1987                     INTPTR_FORMAT, p2i(n), p2i(obj), mark.value(), p2i(obj_mon));
1988       *error_cnt_p = *error_cnt_p + 1;
1989     }
1990   }
1991 }
1992 
1993 // Log details about ObjectMonitors on the in_use_list. The 'BHL'
1994 // flags indicate why the entry is in-use, 'object' and 'object type'
1995 // indicate the associated object and its type.
1996 void ObjectSynchronizer::log_in_use_monitor_details(outputStream* out) {
1997   stringStream ss;
1998   if (_in_use_list.count() > 0) {

1999     out->print_cr("In-use monitor info:");
2000     out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2001     out->print_cr("%18s  %s  %18s  %18s",
2002                   "monitor", "BHL", "object", "object type");
2003     out->print_cr("==================  ===  ==================  ==================");
2004     MonitorList::Iterator iter = _in_use_list.iterator();
2005     while (iter.has_next()) {
2006       ObjectMonitor* mid = iter.next();
2007       const oop obj = mid->object_peek();
2008       const markWord mark = mid->header();
2009       ResourceMark rm;
2010       out->print(INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT "  %s", p2i(mid),
2011                  mid->is_busy(), mark.hash() != 0, mid->owner() != nullptr,
2012                  p2i(obj), obj == nullptr ? "" : obj->klass()->external_name());
2013       if (mid->is_busy()) {
2014         out->print(" (%s)", mid->is_busy_to_string(&ss));
2015         ss.reset();






2016       }
2017       out->cr();
2018     }
2019   }
2020 
2021   out->flush();
2022 }

  46 #include "runtime/objectMonitor.inline.hpp"
  47 #include "runtime/os.inline.hpp"
  48 #include "runtime/osThread.hpp"
  49 #include "runtime/perfData.hpp"
  50 #include "runtime/safepointMechanism.inline.hpp"
  51 #include "runtime/safepointVerifiers.hpp"
  52 #include "runtime/sharedRuntime.hpp"
  53 #include "runtime/stubRoutines.hpp"
  54 #include "runtime/synchronizer.hpp"
  55 #include "runtime/threads.hpp"
  56 #include "runtime/timer.hpp"
  57 #include "runtime/trimNativeHeap.hpp"
  58 #include "runtime/vframe.hpp"
  59 #include "runtime/vmThread.hpp"
  60 #include "utilities/align.hpp"
  61 #include "utilities/dtrace.hpp"
  62 #include "utilities/events.hpp"
  63 #include "utilities/linkedlist.hpp"
  64 #include "utilities/preserveException.hpp"
  65 







































  66 void MonitorList::add(ObjectMonitor* m) {
  67   ObjectMonitor* head;
  68   do {
  69     head = Atomic::load(&_head);
  70     m->set_next_om(head);
  71   } while (Atomic::cmpxchg(&_head, head, m) != head);
  72 
  73   size_t count = Atomic::add(&_count, 1u);
  74   if (count > max()) {
  75     Atomic::inc(&_max);
  76   }
  77 }
  78 
  79 size_t MonitorList::count() const {
  80   return Atomic::load(&_count);
  81 }
  82 
  83 size_t MonitorList::max() const {
  84   return Atomic::load(&_max);
  85 }
  86 
  87 // Walk the in-use list and unlink deflated ObjectMonitors.
  88 // Returns the number of unlinked ObjectMonitors.
  89 size_t MonitorList::unlink_deflated(Thread* current, LogStream* ls,
  90                                     elapsedTimer* timer_p,
  91                                     size_t deflated_count,
  92                                     GrowableArray<ObjectMonitor*>* unlinked_list) {
  93   size_t unlinked_count = 0;
  94   ObjectMonitor* prev = nullptr;
  95   ObjectMonitor* m = Atomic::load_acquire(&_head);
  96 
  97   // The in-use list head can be null during the final audit.
  98   while (m != nullptr) {
  99     if (m->is_being_async_deflated()) {
 100       // Find next live ObjectMonitor. Batch up the unlinkable monitors, so we can
 101       // modify the list once per batch. The batch starts at "m".
 102       size_t unlinked_batch = 0;
 103       ObjectMonitor* next = m;
 104       // Look for at most MonitorUnlinkBatch monitors, or the number of
 105       // deflated and not unlinked monitors, whatever comes first.
 106       assert(deflated_count >= unlinked_count, "Sanity: underflow");
 107       size_t unlinked_batch_limit = MIN2<size_t>(deflated_count - unlinked_count, MonitorUnlinkBatch);
 108       do {
 109         ObjectMonitor* next_next = next->next_om();
 110         unlinked_batch++;
 111         unlinked_list->append(next);
 112         next = next_next;
 113         if (unlinked_batch >= unlinked_batch_limit) {
 114           // Reached the max batch, so bail out of the gathering loop.
 115           break;
 116         }
 117         if (prev == nullptr && Atomic::load(&_head) != m) {
 118           // Current batch used to be at head, but it is not at head anymore.
 119           // Bail out and figure out where we currently are. This avoids long
 120           // walks searching for new prev during unlink under heavy list inserts.
 121           break;
 122         }
 123       } while (next != nullptr && next->is_being_async_deflated());
 124 
 125       // Unlink the found batch.
 126       if (prev == nullptr) {
 127         // The current batch is the first batch, so there is a chance that it starts at head.
 128         // Optimistically assume no inserts happened, and try to unlink the entire batch from the head.
 129         ObjectMonitor* prev_head = Atomic::cmpxchg(&_head, m, next);
 130         if (prev_head != m) {
 131           // Something must have updated the head. Figure out the actual prev for this batch.
 132           for (ObjectMonitor* n = prev_head; n != m; n = n->next_om()) {
 133             prev = n;
 134           }
 135           assert(prev != nullptr, "Should have found the prev for the current batch");
 136           prev->set_next_om(next);
 137         }
 138       } else {
 139         // The current batch is preceded by another batch. This guarantees the current batch
 140         // does not start at head. Unlink the entire current batch without updating the head.
 141         assert(Atomic::load(&_head) != m, "Sanity");
 142         prev->set_next_om(next);
 143       }
 144 
 145       unlinked_count += unlinked_batch;
 146       if (unlinked_count >= deflated_count) {
 147         // Reached the max so bail out of the searching loop.
 148         // There should be no more deflated monitors left.
 149         break;
 150       }
 151       m = next;
 152     } else {
 153       prev = m;
 154       m = m->next_om();
 155     }
 156 
 157     if (current->is_Java_thread()) {
 158       // A JavaThread must check for a safepoint/handshake and honor it.
 159       ObjectSynchronizer::chk_for_block_req(JavaThread::cast(current), "unlinking",
 160                                             "unlinked_count", unlinked_count,
 161                                             ls, timer_p);
 162     }
 163   }
 164 
 165 #ifdef ASSERT
 166   // Invariant: the code above should unlink all deflated monitors.
 167   // The code that runs after this unlinking does not expect deflated monitors.
 168   // Notably, attempting to deflate the already deflated monitor would break.
 169   {
 170     ObjectMonitor* m = Atomic::load_acquire(&_head);
 171     while (m != nullptr) {
 172       assert(!m->is_being_async_deflated(), "All deflated monitors should be unlinked");
 173       m = m->next_om();
 174     }
 175   }
 176 #endif
 177 
 178   Atomic::sub(&_count, unlinked_count);
 179   return unlinked_count;
 180 }
 181 
 182 MonitorList::Iterator MonitorList::iterator() const {
 183   return Iterator(Atomic::load_acquire(&_head));
 184 }
 185 
 186 ObjectMonitor* MonitorList::Iterator::next() {
 187   ObjectMonitor* current = _current;
 188   _current = current->next_om();
 189   return current;
 190 }
 191 
 192 // The "core" versions of monitor enter and exit reside in this file.
 193 // The interpreter and compilers contain specialized transliterated
 194 // variants of the enter-exit fast-path operations.  See c2_MacroAssembler_x86.cpp
 195 // fast_lock(...) for instance.  If you make changes here, make sure to modify the
 196 // interpreter, and both C1 and C2 fast-path inline locking code emission.
 197 //

1081   if (mark.has_monitor()) {
1082     // Inflated monitor so header points to ObjectMonitor (tagged pointer).
1083     // The first stage of async deflation does not affect any field
1084     // used by this comparison so the ObjectMonitor* is usable here.
1085     ObjectMonitor* monitor = mark.monitor();
1086     assert(monitor != nullptr, "monitor should be non-null");
1087     // owning_thread_from_monitor() may also return null here:
1088     return Threads::owning_thread_from_monitor(t_list, monitor);
1089   }
1090 
1091   // Unlocked case, header in place
1092   // Cannot have assertion since this object may have been
1093   // locked by another thread when reaching here.
1094   // assert(mark.is_neutral(), "sanity check");
1095 
1096   return nullptr;
1097 }
1098 
1099 // Visitors ...
1100 
1101 // Iterate over all ObjectMonitors.
1102 template <typename Function>
1103 void ObjectSynchronizer::monitors_iterate(Function function) {



1104   MonitorList::Iterator iter = _in_use_list.iterator();
1105   while (iter.has_next()) {
1106     ObjectMonitor* monitor = iter.next();
1107     function(monitor);















1108   }
1109 }
1110 
1111 // Iterate ObjectMonitors owned by any thread and where the owner `filter`
1112 // returns true.
1113 template <typename OwnerFilter>
1114 void ObjectSynchronizer::owned_monitors_iterate_filtered(MonitorClosure* closure, OwnerFilter filter) {
1115   monitors_iterate([&](ObjectMonitor* monitor) {
1116     // This function is only called at a safepoint or when the
1117     // target thread is suspended or when the target thread is
1118     // operating on itself. The current closures in use today are
1119     // only interested in an owned ObjectMonitor and ownership
1120     // cannot be dropped under the calling contexts so the
1121     // ObjectMonitor cannot be async deflated.
1122     if (monitor->has_owner() && filter(monitor->owner_raw())) {
1123       assert(!monitor->is_being_async_deflated(), "Owned monitors should not be deflating");
1124 
1125       closure->do_monitor(monitor);






1126     }
1127   });
1128 }
1129 
1130 // Iterate ObjectMonitors where the owner == thread; this does NOT include
1131 // ObjectMonitors where owner is set to a stack-lock address in thread.
1132 void ObjectSynchronizer::owned_monitors_iterate(MonitorClosure* closure, JavaThread* thread) {
1133   auto thread_filter = [&](void* owner) { return owner == thread; };
1134   return owned_monitors_iterate_filtered(closure, thread_filter);
1135 }
1136 
1137 // Iterate ObjectMonitors owned by any thread.
1138 void ObjectSynchronizer::owned_monitors_iterate(MonitorClosure* closure) {
1139   auto all_filter = [&](void* owner) { return true; };
1140   return owned_monitors_iterate_filtered(closure, all_filter);
1141 }
1142 
1143 static bool monitors_used_above_threshold(MonitorList* list) {
1144   if (MonitorUsedDeflationThreshold == 0) {  // disabled case is easy
1145     return false;
1146   }
1147   // Start with ceiling based on a per-thread estimate:
1148   size_t ceiling = ObjectSynchronizer::in_use_list_ceiling();
1149   size_t old_ceiling = ceiling;
1150   if (ceiling < list->max()) {
1151     // The max used by the system has exceeded the ceiling so use that:
1152     ceiling = list->max();
1153   }
1154   size_t monitors_used = list->count();
1155   if (monitors_used == 0) {  // empty list is easy
1156     return false;
1157   }
1158   if (NoAsyncDeflationProgressMax != 0 &&
1159       _no_progress_cnt >= NoAsyncDeflationProgressMax) {
1160     float remainder = (100.0 - MonitorUsedDeflationThreshold) / 100.0;

1227 
1228     // If this deflation has no progress, then it should not affect the no-progress
1229     // tracking, otherwise threshold heuristics would think it was triggered, experienced
1230     // no progress, and needs to backoff more aggressively. In this "no progress" case,
1231     // the generic code would bump the no-progress counter, and we compensate for that
1232     // by telling it to skip the update.
1233     //
1234     // If this deflation has progress, then it should let non-progress tracking
1235     // know about this, otherwise the threshold heuristics would kick in, potentially
1236     // experience no-progress due to aggressive cleanup by this deflation, and think
1237     // it is still in no-progress stride. In this "progress" case, the generic code would
1238     // zero the counter, and we allow it to happen.
1239     _no_progress_skip_increment = true;
1240 
1241     return true;
1242   }
1243 
1244   return false;
1245 }
1246 
1247 void ObjectSynchronizer::request_deflate_idle_monitors() {
1248   MonitorLocker ml(MonitorDeflation_lock, Mutex::_no_safepoint_check_flag);
1249   set_is_async_deflation_requested(true);
1250   ml.notify_all();
1251 }
1252 
1253 bool ObjectSynchronizer::request_deflate_idle_monitors_from_wb() {
1254   JavaThread* current = JavaThread::current();
1255   bool ret_code = false;
1256 
1257   jlong last_time = last_async_deflation_time_ns();
1258 
1259   request_deflate_idle_monitors();
1260 


1261   const int N_CHECKS = 5;
1262   for (int i = 0; i < N_CHECKS; i++) {  // sleep for at most 5 seconds
1263     if (last_async_deflation_time_ns() > last_time) {
1264       log_info(monitorinflation)("Async Deflation happened after %d check(s).", i);
1265       ret_code = true;
1266       break;
1267     }
1268     {
1269       // JavaThread has to honor the blocking protocol.
1270       ThreadBlockInVM tbivm(current);
1271       os::naked_short_sleep(999);  // sleep for almost 1 second
1272     }
1273   }
1274   if (!ret_code) {
1275     log_info(monitorinflation)("Async Deflation DID NOT happen after %d checks.", N_CHECKS);
1276   }
1277 
1278   return ret_code;
1279 }
1280 

1557                  op_name, cnt_name, cnt, in_use_list_ceiling(),
1558                  _in_use_list.count(), _in_use_list.max());
1559   }
1560 
1561   {
1562     // Honor block request.
1563     ThreadBlockInVM tbivm(current);
1564   }
1565 
1566   if (ls != nullptr) {
1567     ls->print_cr("resuming %s: in_use_list stats: ceiling=" SIZE_FORMAT
1568                  ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT, op_name,
1569                  in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1570     timer_p->start();
1571   }
1572 }
1573 
1574 // Walk the in-use list and deflate (at most MonitorDeflationMax) idle
1575 // ObjectMonitors. Returns the number of deflated ObjectMonitors.
1576 //







1577 size_t ObjectSynchronizer::deflate_monitor_list(Thread* current, LogStream* ls,
1578                                                 elapsedTimer* timer_p) {

1579   MonitorList::Iterator iter = _in_use_list.iterator();
1580   size_t deflated_count = 0;
1581 
1582   while (iter.has_next()) {
1583     if (deflated_count >= (size_t)MonitorDeflationMax) {
1584       break;
1585     }
1586     ObjectMonitor* mid = iter.next();
1587     if (mid->deflate_monitor()) {
1588       deflated_count++;












1589     }
1590 
1591     if (current->is_Java_thread()) {
1592       // A JavaThread must check for a safepoint/handshake and honor it.
1593       chk_for_block_req(JavaThread::cast(current), "deflation", "deflated_count",
1594                         deflated_count, ls, timer_p);
1595     }
1596   }
1597 
1598   return deflated_count;
1599 }
1600 
1601 class HandshakeForDeflation : public HandshakeClosure {
1602  public:
1603   HandshakeForDeflation() : HandshakeClosure("HandshakeForDeflation") {}
1604 
1605   void do_thread(Thread* thread) {
1606     log_trace(monitorinflation)("HandshakeForDeflation::do_thread: thread="
1607                                 INTPTR_FORMAT, p2i(thread));
1608   }

1618   };
1619 };
1620 
1621 static size_t delete_monitors(Thread* current, GrowableArray<ObjectMonitor*>* delete_list,
1622                               LogStream* ls, elapsedTimer* timer_p) {
1623   NativeHeapTrimmer::SuspendMark sm("monitor deletion");
1624   size_t deleted_count = 0;
1625   for (ObjectMonitor* monitor: *delete_list) {
1626     delete monitor;
1627     deleted_count++;
1628     if (current->is_Java_thread()) {
1629       // A JavaThread must check for a safepoint/handshake and honor it.
1630       ObjectSynchronizer::chk_for_block_req(JavaThread::cast(current), "deletion", "deleted_count",
1631                                             deleted_count, ls, timer_p);
1632     }
1633   }
1634   return deleted_count;
1635 }
1636 
1637 // This function is called by the MonitorDeflationThread to deflate
1638 // ObjectMonitors.
1639 size_t ObjectSynchronizer::deflate_idle_monitors() {

1640   Thread* current = Thread::current();
1641   if (current->is_Java_thread()) {
1642     // The async deflation request has been processed.
1643     _last_async_deflation_time_ns = os::javaTimeNanos();
1644     set_is_async_deflation_requested(false);
1645   }
1646 
1647   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1648   LogStreamHandle(Info, monitorinflation) lsh_info;
1649   LogStream* ls = nullptr;
1650   if (log_is_enabled(Debug, monitorinflation)) {
1651     ls = &lsh_debug;
1652   } else if (log_is_enabled(Info, monitorinflation)) {
1653     ls = &lsh_info;
1654   }
1655 
1656   elapsedTimer timer;
1657   if (ls != nullptr) {
1658     ls->print_cr("begin deflating: in_use_list stats: ceiling=" SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1659                  in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1660     timer.start();
1661   }
1662 
1663   // Deflate some idle ObjectMonitors.
1664   size_t deflated_count = deflate_monitor_list(current, ls, &timer);
1665   size_t unlinked_count = 0;
1666   size_t deleted_count = 0;
1667   if (deflated_count > 0) {
1668     // There are ObjectMonitors that have been deflated.



1669 
1670     // Unlink deflated ObjectMonitors from the in-use list.
1671     ResourceMark rm;
1672     GrowableArray<ObjectMonitor*> delete_list((int)deflated_count);
1673     unlinked_count = _in_use_list.unlink_deflated(current, ls, &timer, deflated_count, &delete_list);
1674     if (current->is_monitor_deflation_thread()) {
1675       if (ls != nullptr) {
1676         timer.stop();
1677         ls->print_cr("before handshaking: unlinked_count=" SIZE_FORMAT
1678                      ", in_use_list stats: ceiling=" SIZE_FORMAT ", count="
1679                      SIZE_FORMAT ", max=" SIZE_FORMAT,
1680                      unlinked_count, in_use_list_ceiling(),
1681                      _in_use_list.count(), _in_use_list.max());
1682       }
1683 
1684       // A JavaThread needs to handshake in order to safely free the
1685       // ObjectMonitors that were deflated in this cycle.
1686       HandshakeForDeflation hfd_hc;
1687       Handshake::execute(&hfd_hc);
1688       // Also, we sync and desync GC threads around the handshake, so that they can
1689       // safely read the mark-word and look-through to the object-monitor, without
1690       // being afraid that the object-monitor is going away.
1691       VM_RendezvousGCThreads sync_gc;
1692       VMThread::execute(&sync_gc);
1693 

1700     } else {
1701       // This is not a monitor deflation thread.
1702       // No handshake or rendezvous is needed when we are already at safepoint.
1703       assert_at_safepoint();
1704     }
1705 
1706     // After the handshake, safely free the ObjectMonitors that were
1707     // deflated and unlinked in this cycle.
1708     deleted_count = delete_monitors(current, &delete_list, ls, &timer);
1709     assert(unlinked_count == deleted_count, "must be");
1710   }
1711 
1712   if (ls != nullptr) {
1713     timer.stop();
1714     if (deflated_count != 0 || unlinked_count != 0 || log_is_enabled(Debug, monitorinflation)) {
1715       ls->print_cr("deflated_count=" SIZE_FORMAT ", {unlinked,deleted}_count=" SIZE_FORMAT " monitors in %3.7f secs",
1716                    deflated_count, unlinked_count, timer.seconds());
1717     }
1718     ls->print_cr("end deflating: in_use_list stats: ceiling=" SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1719                  in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());




1720   }
1721 
1722   OM_PERFDATA_OP(MonExtant, set_value(_in_use_list.count()));
1723   OM_PERFDATA_OP(Deflations, inc(deflated_count));
1724 
1725   GVars.stw_random = os::random();
1726 
1727   if (deflated_count != 0) {
1728     _no_progress_cnt = 0;
1729   } else if (_no_progress_skip_increment) {
1730     _no_progress_skip_increment = false;
1731   } else {
1732     _no_progress_cnt++;
1733   }
1734 
1735   return deflated_count;
1736 }
1737 
1738 // Monitor cleanup on JavaThread::exit
1739 

1752 
1753 // Release all inflated monitors owned by current thread.  Lightweight monitors are
1754 // ignored.  This is meant to be called during JNI thread detach which assumes
1755 // all remaining monitors are heavyweight.  All exceptions are swallowed.
1756 // Scanning the extant monitor list can be time consuming.
1757 // A simple optimization is to add a per-thread flag that indicates a thread
1758 // called jni_monitorenter() during its lifetime.
1759 //
1760 // Instead of NoSafepointVerifier it might be cheaper to
1761 // use an idiom of the form:
1762 //   auto int tmp = SafepointSynchronize::_safepoint_counter ;
1763 //   <code that must not run at safepoint>
1764 //   guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
1765 // Since the tests are extremely cheap we could leave them enabled
1766 // for normal product builds.
1767 
1768 void ObjectSynchronizer::release_monitors_owned_by_thread(JavaThread* current) {
1769   assert(current == JavaThread::current(), "must be current Java thread");
1770   NoSafepointVerifier nsv;
1771   ReleaseJavaMonitorsClosure rjmc(current);
1772   ObjectSynchronizer::owned_monitors_iterate(&rjmc, current);
1773   assert(!current->has_pending_exception(), "Should not be possible");
1774   current->clear_pending_exception();
1775   assert(current->held_monitor_count() == 0, "Should not be possible");
1776   // All monitors (including entered via JNI) have been unlocked above, so we need to clear jni count.
1777   current->clear_jni_monitor_count();
1778 }
1779 
1780 const char* ObjectSynchronizer::inflate_cause_name(const InflateCause cause) {
1781   switch (cause) {
1782     case inflate_cause_vm_internal:    return "VM Internal";
1783     case inflate_cause_monitor_enter:  return "Monitor Enter";
1784     case inflate_cause_wait:           return "Monitor Wait";
1785     case inflate_cause_notify:         return "Monitor Notify";
1786     case inflate_cause_hash_code:      return "Monitor Hash Code";
1787     case inflate_cause_jni_enter:      return "JNI Monitor Enter";
1788     case inflate_cause_jni_exit:       return "JNI Monitor Exit";
1789     default:
1790       ShouldNotReachHere();
1791   }
1792   return "Unknown";

1806 size_t ObjectSynchronizer::get_gvars_size() {
1807   return sizeof(SharedGlobals);
1808 }
1809 
1810 u_char* ObjectSynchronizer::get_gvars_stw_random_addr() {
1811   return (u_char*)&GVars.stw_random;
1812 }
1813 
1814 // Do the final audit and print of ObjectMonitor stats; must be done
1815 // by the VMThread at VM exit time.
1816 void ObjectSynchronizer::do_final_audit_and_print_stats() {
1817   assert(Thread::current()->is_VM_thread(), "sanity check");
1818 
1819   if (is_final_audit()) {  // Only do the audit once.
1820     return;
1821   }
1822   set_is_final_audit();
1823   log_info(monitorinflation)("Starting the final audit.");
1824 
1825   if (log_is_enabled(Info, monitorinflation)) {






1826     // The other audit_and_print_stats() call is done at the Debug
1827     // level at a safepoint in SafepointSynchronize::do_cleanup_tasks.
1828     audit_and_print_stats(true /* on_exit */);
1829   }
1830 }
1831 
1832 // This function can be called at a safepoint or it can be called when
1833 // we are trying to exit the VM. When we are trying to exit the VM, the
1834 // list walker functions can run in parallel with the other list
1835 // operations so spin-locking is used for safety.
1836 //
1837 // Calls to this function can be added in various places as a debugging
1838 // aid; pass 'true' for the 'on_exit' parameter to have in-use monitor
1839 // details logged at the Info level and 'false' for the 'on_exit'
1840 // parameter to have in-use monitor details logged at the Trace level.
1841 //
1842 void ObjectSynchronizer::audit_and_print_stats(bool on_exit) {
1843   assert(on_exit || SafepointSynchronize::is_at_safepoint(), "invariant");
1844 
1845   LogStreamHandle(Debug, monitorinflation) lsh_debug;

1854     ls = &lsh_info;
1855   }
1856   assert(ls != nullptr, "sanity check");
1857 
1858   int error_cnt = 0;
1859 
1860   ls->print_cr("Checking in_use_list:");
1861   chk_in_use_list(ls, &error_cnt);
1862 
1863   if (error_cnt == 0) {
1864     ls->print_cr("No errors found in in_use_list checks.");
1865   } else {
1866     log_error(monitorinflation)("found in_use_list errors: error_cnt=%d", error_cnt);
1867   }
1868 
1869   if ((on_exit && log_is_enabled(Info, monitorinflation)) ||
1870       (!on_exit && log_is_enabled(Trace, monitorinflation))) {
1871     // When exiting this log output is at the Info level. When called
1872     // at a safepoint, this log output is at the Trace level since
1873     // there can be a lot of it.
1874     log_in_use_monitor_details(ls, !on_exit /* log_all */);
1875   }
1876 
1877   ls->flush();
1878 
1879   guarantee(error_cnt == 0, "ERROR: found monitor list errors: error_cnt=%d", error_cnt);
1880 }
1881 
1882 // Check the in_use_list; log the results of the checks.
1883 void ObjectSynchronizer::chk_in_use_list(outputStream* out, int *error_cnt_p) {
1884   size_t l_in_use_count = _in_use_list.count();
1885   size_t l_in_use_max = _in_use_list.max();
1886   out->print_cr("count=" SIZE_FORMAT ", max=" SIZE_FORMAT, l_in_use_count,
1887                 l_in_use_max);
1888 
1889   size_t ck_in_use_count = 0;
1890   MonitorList::Iterator iter = _in_use_list.iterator();
1891   while (iter.has_next()) {
1892     ObjectMonitor* mid = iter.next();
1893     chk_in_use_entry(mid, out, error_cnt_p);
1894     ck_in_use_count++;

1900   } else {
1901     out->print_cr("WARNING: in_use_count=" SIZE_FORMAT " is not equal to "
1902                   "ck_in_use_count=" SIZE_FORMAT, l_in_use_count,
1903                   ck_in_use_count);
1904   }
1905 
1906   size_t ck_in_use_max = _in_use_list.max();
1907   if (l_in_use_max == ck_in_use_max) {
1908     out->print_cr("in_use_max=" SIZE_FORMAT " equals ck_in_use_max="
1909                   SIZE_FORMAT, l_in_use_max, ck_in_use_max);
1910   } else {
1911     out->print_cr("WARNING: in_use_max=" SIZE_FORMAT " is not equal to "
1912                   "ck_in_use_max=" SIZE_FORMAT, l_in_use_max, ck_in_use_max);
1913   }
1914 }
1915 
1916 // Check an in-use monitor entry; log any errors.
1917 void ObjectSynchronizer::chk_in_use_entry(ObjectMonitor* n, outputStream* out,
1918                                           int* error_cnt_p) {
1919   if (n->owner_is_DEFLATER_MARKER()) {
1920     // This could happen when monitor deflation blocks for a safepoint.


1921     return;
1922   }
1923 
1924   if (n->header().value() == 0) {
1925     out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor must "
1926                   "have non-null _header field.", p2i(n));
1927     *error_cnt_p = *error_cnt_p + 1;
1928   }
1929   const oop obj = n->object_peek();
1930   if (obj != nullptr) {
1931     const markWord mark = obj->mark();
1932     if (!mark.has_monitor()) {
1933       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor's "
1934                     "object does not think it has a monitor: obj="
1935                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT, p2i(n),
1936                     p2i(obj), mark.value());
1937       *error_cnt_p = *error_cnt_p + 1;
1938     }
1939     ObjectMonitor* const obj_mon = mark.monitor();
1940     if (n != obj_mon) {
1941       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor's "
1942                     "object does not refer to the same monitor: obj="
1943                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT ", obj_mon="
1944                     INTPTR_FORMAT, p2i(n), p2i(obj), mark.value(), p2i(obj_mon));
1945       *error_cnt_p = *error_cnt_p + 1;
1946     }
1947   }
1948 }
1949 
1950 // Log details about ObjectMonitors on the in_use_list. The 'BHL'
1951 // flags indicate why the entry is in-use, 'object' and 'object type'
1952 // indicate the associated object and its type.
1953 void ObjectSynchronizer::log_in_use_monitor_details(outputStream* out, bool log_all) {

1954   if (_in_use_list.count() > 0) {
1955     stringStream ss;
1956     out->print_cr("In-use monitor info:");
1957     out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
1958     out->print_cr("%18s  %s  %18s  %18s",
1959                   "monitor", "BHL", "object", "object type");
1960     out->print_cr("==================  ===  ==================  ==================");
1961 
1962     auto is_interesting = [&](ObjectMonitor* monitor) {
1963       return log_all || monitor->has_owner() || monitor->is_busy();
1964     };
1965 
1966     monitors_iterate([&](ObjectMonitor* monitor) {
1967       if (is_interesting(monitor)) {
1968         const oop obj = monitor->object_peek();
1969         const markWord mark = monitor->header();
1970         ResourceMark rm;
1971         out->print(INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT "  %s", p2i(monitor),
1972                    monitor->is_busy(), mark.hash() != 0, monitor->owner() != nullptr,
1973                    p2i(obj), obj == nullptr ? "" : obj->klass()->external_name());
1974         if (monitor->is_busy()) {
1975           out->print(" (%s)", monitor->is_busy_to_string(&ss));
1976           ss.reset();
1977         }
1978         out->cr();
1979       }
1980     });

1981   }
1982 
1983   out->flush();
1984 }
< prev index next >