1033 // an inordinate number of monitors in circulation.
1034 // To avoid that scenario we can artificially induce a STW safepoint
1035 // if the pool appears to be growing past some reasonable bound.
1036 // Generally we favor time in space-time tradeoffs, but as there's no
1037 // natural back-pressure on the # of extant monitors we need to impose some
1038 // type of limit. Beware that if MonitorBound is set to too low a value
1039 // we could just loop. In addition, if MonitorBound is set to a low value
1040 // we'll incur more safepoints, which are harmful to performance.
1041 // See also: GuaranteedSafepointInterval
1042 //
1043 // The current implementation uses asynchronous VM operations.
1044
1045 static void InduceScavenge(Thread * Self, const char * Whence) {
1046 // Induce STW safepoint to trim monitors
1047 // Ultimately, this results in a call to deflate_idle_monitors() in the near future.
1048 // More precisely, trigger an asynchronous STW safepoint as the number
1049 // of active monitors passes the specified threshold.
1050 // TODO: assert thread state is reasonable
1051
1052 if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) {
1053 if (ObjectMonitor::Knob_Verbose) {
1054 tty->print_cr("INFO: Monitor scavenge - Induced STW @%s (%d)",
1055 Whence, ForceMonitorScavenge) ;
1056 tty->flush();
1057 }
1058 // Induce a 'null' safepoint to scavenge monitors
1059 // Must VM_Operation instance be heap allocated as the op will be enqueue and posted
1060 // to the VMthread and have a lifespan longer than that of this activation record.
1061 // The VMThread will delete the op when completed.
1062 VMThread::execute(new VM_ScavengeMonitors());
1063
1064 if (ObjectMonitor::Knob_Verbose) {
1065 tty->print_cr("INFO: Monitor scavenge - STW posted @%s (%d)",
1066 Whence, ForceMonitorScavenge) ;
1067 tty->flush();
1068 }
1069 }
1070 }
1071
1072 void ObjectSynchronizer::verifyInUse(Thread *Self) {
1073 ObjectMonitor* mid;
1074 int in_use_tally = 0;
1075 for (mid = Self->omInUseList; mid != NULL; mid = mid->FreeNext) {
1076 in_use_tally++;
1077 }
1078 assert(in_use_tally == Self->omInUseCount, "in-use count off");
1079
1080 int free_tally = 0;
1081 for (mid = Self->omFreeList; mid != NULL; mid = mid->FreeNext) {
1082 free_tally++;
1083 }
1084 assert(free_tally == Self->omFreeCount, "free count off");
1085 }
1086
1087 ObjectMonitor* ObjectSynchronizer::omAlloc(Thread * Self) {
1088 // A large MAXPRIVATE value reduces both list lock contention
1093 const int MAXPRIVATE = 1024;
1094 for (;;) {
1095 ObjectMonitor * m;
1096
1097 // 1: try to allocate from the thread's local omFreeList.
1098 // Threads will attempt to allocate first from their local list, then
1099 // from the global list, and only after those attempts fail will the thread
1100 // attempt to instantiate new monitors. Thread-local free lists take
1101 // heat off the gListLock and improve allocation latency, as well as reducing
1102 // coherency traffic on the shared global list.
1103 m = Self->omFreeList;
1104 if (m != NULL) {
1105 Self->omFreeList = m->FreeNext;
1106 Self->omFreeCount--;
1107 // CONSIDER: set m->FreeNext = BAD -- diagnostic hygiene
1108 guarantee(m->object() == NULL, "invariant");
1109 if (MonitorInUseLists) {
1110 m->FreeNext = Self->omInUseList;
1111 Self->omInUseList = m;
1112 Self->omInUseCount++;
1113 if (ObjectMonitor::Knob_VerifyInUse) {
1114 verifyInUse(Self);
1115 }
1116 } else {
1117 m->FreeNext = NULL;
1118 }
1119 return m;
1120 }
1121
1122 // 2: try to allocate from the global gFreeList
1123 // CONSIDER: use muxTry() instead of muxAcquire().
1124 // If the muxTry() fails then drop immediately into case 3.
1125 // If we're using thread-local free lists then try
1126 // to reprovision the caller's free list.
1127 if (gFreeList != NULL) {
1128 // Reprovision the thread's omFreeList.
1129 // Use bulk transfers to reduce the allocation rate and heat
1130 // on various locks.
1131 Thread::muxAcquire(&gListLock, "omAlloc");
1132 for (int i = Self->omFreeProvision; --i >= 0 && gFreeList != NULL;) {
1133 gMonitorFreeCount--;
1134 ObjectMonitor * take = gFreeList;
1135 gFreeList = take->FreeNext;
1233 // scavenger -- deflate_idle_monitors -- from reclaiming them.
1234
1235 void ObjectSynchronizer::omRelease(Thread * Self, ObjectMonitor * m,
1236 bool fromPerThreadAlloc) {
1237 guarantee(m->object() == NULL, "invariant");
1238 guarantee(((m->is_busy()|m->_recursions) == 0), "freeing in-use monitor");
1239 // Remove from omInUseList
1240 if (MonitorInUseLists && fromPerThreadAlloc) {
1241 ObjectMonitor* cur_mid_in_use = NULL;
1242 bool extracted = false;
1243 for (ObjectMonitor* mid = Self->omInUseList; mid != NULL; cur_mid_in_use = mid, mid = mid->FreeNext) {
1244 if (m == mid) {
1245 // extract from per-thread in-use list
1246 if (mid == Self->omInUseList) {
1247 Self->omInUseList = mid->FreeNext;
1248 } else if (cur_mid_in_use != NULL) {
1249 cur_mid_in_use->FreeNext = mid->FreeNext; // maintain the current thread in-use list
1250 }
1251 extracted = true;
1252 Self->omInUseCount--;
1253 if (ObjectMonitor::Knob_VerifyInUse) {
1254 verifyInUse(Self);
1255 }
1256 break;
1257 }
1258 }
1259 assert(extracted, "Should have extracted from in-use list");
1260 }
1261
1262 // FreeNext is used for both omInUseList and omFreeList, so clear old before setting new
1263 m->FreeNext = Self->omFreeList;
1264 Self->omFreeList = m;
1265 Self->omFreeCount++;
1266 }
1267
1268 // Return the monitors of a moribund thread's local free list to
1269 // the global free list. Typically a thread calls omFlush() when
1270 // it's dying. We could also consider having the VM thread steal
1271 // monitors from threads that have not run java code over a few
1272 // consecutive STW safepoints. Relatedly, we might decay
1273 // omFreeProvision at STW safepoints.
1274 //
1275 // Also return the monitors of a moribund thread's omInUseList to
1746 }
1747 }
1748
1749 // Move the scavenged monitors back to the global free list.
1750 if (freeHeadp != NULL) {
1751 guarantee(freeTailp != NULL && counters->nScavenged > 0, "invariant");
1752 assert(freeTailp->FreeNext == NULL, "invariant");
1753 // constant-time list splice - prepend scavenged segment to gFreeList
1754 freeTailp->FreeNext = gFreeList;
1755 gFreeList = freeHeadp;
1756 }
1757 Thread::muxRelease(&gListLock);
1758
1759 }
1760
1761 void ObjectSynchronizer::finish_deflate_idle_monitors(DeflateMonitorCounters* counters) {
1762 gMonitorFreeCount += counters->nScavenged;
1763
1764 // Consider: audit gFreeList to ensure that gMonitorFreeCount and list agree.
1765
1766 if (ObjectMonitor::Knob_Verbose) {
1767 tty->print_cr("INFO: Deflate: InCirc=%d InUse=%d Scavenged=%d "
1768 "ForceMonitorScavenge=%d : pop=%d free=%d",
1769 counters->nInCirculation, counters->nInuse, counters->nScavenged, ForceMonitorScavenge,
1770 gMonitorPopulation, gMonitorFreeCount);
1771 tty->flush();
1772 }
1773
1774 ForceMonitorScavenge = 0; // Reset
1775
1776 OM_PERFDATA_OP(Deflations, inc(counters->nScavenged));
1777 OM_PERFDATA_OP(MonExtant, set_value(counters->nInCirculation));
1778
1779 // TODO: Add objectMonitor leak detection.
1780 // Audit/inventory the objectMonitors -- make sure they're all accounted for.
1781 GVars.stwRandom = os::random();
1782 GVars.stwCycle++;
1783 }
1784
1785 void ObjectSynchronizer::deflate_thread_local_monitors(Thread* thread, DeflateMonitorCounters* counters) {
1786 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1787 if (!MonitorInUseLists) return;
1788
1789 ObjectMonitor * freeHeadp = NULL; // Local SLL of scavenged monitors
1790 ObjectMonitor * freeTailp = NULL;
1791
1792 int deflated_count = deflate_monitor_list(thread->omInUseList_addr(), &freeHeadp, &freeTailp);
1793
1794 Thread::muxAcquire(&gListLock, "scavenge - return");
1795
1796 // Adjust counters
1797 counters->nInCirculation += thread->omInUseCount;
1798 thread->omInUseCount -= deflated_count;
1799 if (ObjectMonitor::Knob_VerifyInUse) {
1800 verifyInUse(thread);
1801 }
1802 counters->nScavenged += deflated_count;
1803 counters->nInuse += thread->omInUseCount;
1804
1805 // Move the scavenged monitors back to the global free list.
1806 if (freeHeadp != NULL) {
1807 guarantee(freeTailp != NULL && deflated_count > 0, "invariant");
1808 assert(freeTailp->FreeNext == NULL, "invariant");
1809
1810 // constant-time list splice - prepend scavenged segment to gFreeList
1811 freeTailp->FreeNext = gFreeList;
1812 gFreeList = freeHeadp;
1813 }
1814 Thread::muxRelease(&gListLock);
1815 }
1816
1817 // Monitor cleanup on JavaThread::exit
1818
1819 // Iterate through monitor cache and attempt to release thread's monitors
1820 // Gives up on a particular monitor if an exception occurs, but continues
1821 // the overall iteration, swallowing the exception.
1822 class ReleaseJavaMonitorsClosure: public MonitorClosure {
1823 private:
1824 TRAPS;
1825
1826 public:
1827 ReleaseJavaMonitorsClosure(Thread* thread) : THREAD(thread) {}
1828 void do_monitor(ObjectMonitor* mid) {
1829 if (mid->owner() == THREAD) {
1830 if (ObjectMonitor::Knob_VerifyMatch != 0) {
1831 ResourceMark rm;
1832 Handle obj(THREAD, (oop) mid->object());
1833 tty->print("INFO: unexpected locked object:");
1834 javaVFrame::print_locked_object_class_name(tty, obj, "locked");
1835 fatal("exiting JavaThread=" INTPTR_FORMAT
1836 " unexpectedly owns ObjectMonitor=" INTPTR_FORMAT,
1837 p2i(THREAD), p2i(mid));
1838 }
1839 (void)mid->complete_exit(CHECK);
1840 }
1841 }
1842 };
1843
1844 // Release all inflated monitors owned by THREAD. Lightweight monitors are
1845 // ignored. This is meant to be called during JNI thread detach which assumes
1846 // all remaining monitors are heavyweight. All exceptions are swallowed.
1847 // Scanning the extant monitor list can be time consuming.
1848 // A simple optimization is to add a per-thread flag that indicates a thread
1849 // called jni_monitorenter() during its lifetime.
1850 //
1851 // Instead of No_Savepoint_Verifier it might be cheaper to
1852 // use an idiom of the form:
1853 // auto int tmp = SafepointSynchronize::_safepoint_counter ;
1854 // <code that must not run at safepoint>
1855 // guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
1856 // Since the tests are extremely cheap we could leave them enabled
1857 // for normal product builds.
1858
|
1033 // an inordinate number of monitors in circulation.
1034 // To avoid that scenario we can artificially induce a STW safepoint
1035 // if the pool appears to be growing past some reasonable bound.
1036 // Generally we favor time in space-time tradeoffs, but as there's no
1037 // natural back-pressure on the # of extant monitors we need to impose some
1038 // type of limit. Beware that if MonitorBound is set to too low a value
1039 // we could just loop. In addition, if MonitorBound is set to a low value
1040 // we'll incur more safepoints, which are harmful to performance.
1041 // See also: GuaranteedSafepointInterval
1042 //
1043 // The current implementation uses asynchronous VM operations.
1044
1045 static void InduceScavenge(Thread * Self, const char * Whence) {
1046 // Induce STW safepoint to trim monitors
1047 // Ultimately, this results in a call to deflate_idle_monitors() in the near future.
1048 // More precisely, trigger an asynchronous STW safepoint as the number
1049 // of active monitors passes the specified threshold.
1050 // TODO: assert thread state is reasonable
1051
1052 if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) {
1053 // Induce a 'null' safepoint to scavenge monitors
1054 // Must VM_Operation instance be heap allocated as the op will be enqueue and posted
1055 // to the VMthread and have a lifespan longer than that of this activation record.
1056 // The VMThread will delete the op when completed.
1057 VMThread::execute(new VM_ScavengeMonitors());
1058 }
1059 }
1060
1061 void ObjectSynchronizer::verifyInUse(Thread *Self) {
1062 ObjectMonitor* mid;
1063 int in_use_tally = 0;
1064 for (mid = Self->omInUseList; mid != NULL; mid = mid->FreeNext) {
1065 in_use_tally++;
1066 }
1067 assert(in_use_tally == Self->omInUseCount, "in-use count off");
1068
1069 int free_tally = 0;
1070 for (mid = Self->omFreeList; mid != NULL; mid = mid->FreeNext) {
1071 free_tally++;
1072 }
1073 assert(free_tally == Self->omFreeCount, "free count off");
1074 }
1075
1076 ObjectMonitor* ObjectSynchronizer::omAlloc(Thread * Self) {
1077 // A large MAXPRIVATE value reduces both list lock contention
1082 const int MAXPRIVATE = 1024;
1083 for (;;) {
1084 ObjectMonitor * m;
1085
1086 // 1: try to allocate from the thread's local omFreeList.
1087 // Threads will attempt to allocate first from their local list, then
1088 // from the global list, and only after those attempts fail will the thread
1089 // attempt to instantiate new monitors. Thread-local free lists take
1090 // heat off the gListLock and improve allocation latency, as well as reducing
1091 // coherency traffic on the shared global list.
1092 m = Self->omFreeList;
1093 if (m != NULL) {
1094 Self->omFreeList = m->FreeNext;
1095 Self->omFreeCount--;
1096 // CONSIDER: set m->FreeNext = BAD -- diagnostic hygiene
1097 guarantee(m->object() == NULL, "invariant");
1098 if (MonitorInUseLists) {
1099 m->FreeNext = Self->omInUseList;
1100 Self->omInUseList = m;
1101 Self->omInUseCount++;
1102 } else {
1103 m->FreeNext = NULL;
1104 }
1105 return m;
1106 }
1107
1108 // 2: try to allocate from the global gFreeList
1109 // CONSIDER: use muxTry() instead of muxAcquire().
1110 // If the muxTry() fails then drop immediately into case 3.
1111 // If we're using thread-local free lists then try
1112 // to reprovision the caller's free list.
1113 if (gFreeList != NULL) {
1114 // Reprovision the thread's omFreeList.
1115 // Use bulk transfers to reduce the allocation rate and heat
1116 // on various locks.
1117 Thread::muxAcquire(&gListLock, "omAlloc");
1118 for (int i = Self->omFreeProvision; --i >= 0 && gFreeList != NULL;) {
1119 gMonitorFreeCount--;
1120 ObjectMonitor * take = gFreeList;
1121 gFreeList = take->FreeNext;
1219 // scavenger -- deflate_idle_monitors -- from reclaiming them.
1220
1221 void ObjectSynchronizer::omRelease(Thread * Self, ObjectMonitor * m,
1222 bool fromPerThreadAlloc) {
1223 guarantee(m->object() == NULL, "invariant");
1224 guarantee(((m->is_busy()|m->_recursions) == 0), "freeing in-use monitor");
1225 // Remove from omInUseList
1226 if (MonitorInUseLists && fromPerThreadAlloc) {
1227 ObjectMonitor* cur_mid_in_use = NULL;
1228 bool extracted = false;
1229 for (ObjectMonitor* mid = Self->omInUseList; mid != NULL; cur_mid_in_use = mid, mid = mid->FreeNext) {
1230 if (m == mid) {
1231 // extract from per-thread in-use list
1232 if (mid == Self->omInUseList) {
1233 Self->omInUseList = mid->FreeNext;
1234 } else if (cur_mid_in_use != NULL) {
1235 cur_mid_in_use->FreeNext = mid->FreeNext; // maintain the current thread in-use list
1236 }
1237 extracted = true;
1238 Self->omInUseCount--;
1239 break;
1240 }
1241 }
1242 assert(extracted, "Should have extracted from in-use list");
1243 }
1244
1245 // FreeNext is used for both omInUseList and omFreeList, so clear old before setting new
1246 m->FreeNext = Self->omFreeList;
1247 Self->omFreeList = m;
1248 Self->omFreeCount++;
1249 }
1250
1251 // Return the monitors of a moribund thread's local free list to
1252 // the global free list. Typically a thread calls omFlush() when
1253 // it's dying. We could also consider having the VM thread steal
1254 // monitors from threads that have not run java code over a few
1255 // consecutive STW safepoints. Relatedly, we might decay
1256 // omFreeProvision at STW safepoints.
1257 //
1258 // Also return the monitors of a moribund thread's omInUseList to
1729 }
1730 }
1731
1732 // Move the scavenged monitors back to the global free list.
1733 if (freeHeadp != NULL) {
1734 guarantee(freeTailp != NULL && counters->nScavenged > 0, "invariant");
1735 assert(freeTailp->FreeNext == NULL, "invariant");
1736 // constant-time list splice - prepend scavenged segment to gFreeList
1737 freeTailp->FreeNext = gFreeList;
1738 gFreeList = freeHeadp;
1739 }
1740 Thread::muxRelease(&gListLock);
1741
1742 }
1743
1744 void ObjectSynchronizer::finish_deflate_idle_monitors(DeflateMonitorCounters* counters) {
1745 gMonitorFreeCount += counters->nScavenged;
1746
1747 // Consider: audit gFreeList to ensure that gMonitorFreeCount and list agree.
1748
1749 ForceMonitorScavenge = 0; // Reset
1750
1751 OM_PERFDATA_OP(Deflations, inc(counters->nScavenged));
1752 OM_PERFDATA_OP(MonExtant, set_value(counters->nInCirculation));
1753
1754 // TODO: Add objectMonitor leak detection.
1755 // Audit/inventory the objectMonitors -- make sure they're all accounted for.
1756 GVars.stwRandom = os::random();
1757 GVars.stwCycle++;
1758 }
1759
1760 void ObjectSynchronizer::deflate_thread_local_monitors(Thread* thread, DeflateMonitorCounters* counters) {
1761 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1762 if (!MonitorInUseLists) return;
1763
1764 ObjectMonitor * freeHeadp = NULL; // Local SLL of scavenged monitors
1765 ObjectMonitor * freeTailp = NULL;
1766
1767 int deflated_count = deflate_monitor_list(thread->omInUseList_addr(), &freeHeadp, &freeTailp);
1768
1769 Thread::muxAcquire(&gListLock, "scavenge - return");
1770
1771 // Adjust counters
1772 counters->nInCirculation += thread->omInUseCount;
1773 thread->omInUseCount -= deflated_count;
1774 counters->nScavenged += deflated_count;
1775 counters->nInuse += thread->omInUseCount;
1776
1777 // Move the scavenged monitors back to the global free list.
1778 if (freeHeadp != NULL) {
1779 guarantee(freeTailp != NULL && deflated_count > 0, "invariant");
1780 assert(freeTailp->FreeNext == NULL, "invariant");
1781
1782 // constant-time list splice - prepend scavenged segment to gFreeList
1783 freeTailp->FreeNext = gFreeList;
1784 gFreeList = freeHeadp;
1785 }
1786 Thread::muxRelease(&gListLock);
1787 }
1788
1789 // Monitor cleanup on JavaThread::exit
1790
1791 // Iterate through monitor cache and attempt to release thread's monitors
1792 // Gives up on a particular monitor if an exception occurs, but continues
1793 // the overall iteration, swallowing the exception.
1794 class ReleaseJavaMonitorsClosure: public MonitorClosure {
1795 private:
1796 TRAPS;
1797
1798 public:
1799 ReleaseJavaMonitorsClosure(Thread* thread) : THREAD(thread) {}
1800 void do_monitor(ObjectMonitor* mid) {
1801 if (mid->owner() == THREAD) {
1802 (void)mid->complete_exit(CHECK);
1803 }
1804 }
1805 };
1806
1807 // Release all inflated monitors owned by THREAD. Lightweight monitors are
1808 // ignored. This is meant to be called during JNI thread detach which assumes
1809 // all remaining monitors are heavyweight. All exceptions are swallowed.
1810 // Scanning the extant monitor list can be time consuming.
1811 // A simple optimization is to add a per-thread flag that indicates a thread
1812 // called jni_monitorenter() during its lifetime.
1813 //
1814 // Instead of No_Savepoint_Verifier it might be cheaper to
1815 // use an idiom of the form:
1816 // auto int tmp = SafepointSynchronize::_safepoint_counter ;
1817 // <code that must not run at safepoint>
1818 // guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
1819 // Since the tests are extremely cheap we could leave them enabled
1820 // for normal product builds.
1821
|