22 * |
22 * |
23 */ |
23 */ |
24 |
24 |
25 #include "precompiled.hpp" |
25 #include "precompiled.hpp" |
26 #include "classfile/vmSymbols.hpp" |
26 #include "classfile/vmSymbols.hpp" |
|
27 #include "memory/padded.hpp" |
27 #include "memory/resourceArea.hpp" |
28 #include "memory/resourceArea.hpp" |
28 #include "oops/markOop.hpp" |
29 #include "oops/markOop.hpp" |
29 #include "oops/oop.inline.hpp" |
30 #include "oops/oop.inline.hpp" |
30 #include "runtime/atomic.inline.hpp" |
31 #include "runtime/atomic.inline.hpp" |
31 #include "runtime/biasedLocking.hpp" |
32 #include "runtime/biasedLocking.hpp" |
108 } |
109 } |
109 |
110 |
110 #define NINFLATIONLOCKS 256 |
111 #define NINFLATIONLOCKS 256 |
111 static volatile intptr_t InflationLocks[NINFLATIONLOCKS]; |
112 static volatile intptr_t InflationLocks[NINFLATIONLOCKS]; |
112 |
113 |
|
114 // gBlockList is really PaddedEnd<ObjectMonitor> *, but we don't |
|
115 // want to expose the PaddedEnd template more than necessary. |
113 ObjectMonitor * ObjectSynchronizer::gBlockList = NULL; |
116 ObjectMonitor * ObjectSynchronizer::gBlockList = NULL; |
114 ObjectMonitor * volatile ObjectSynchronizer::gFreeList = NULL; |
117 ObjectMonitor * volatile ObjectSynchronizer::gFreeList = NULL; |
115 ObjectMonitor * volatile ObjectSynchronizer::gOmInUseList = NULL; |
118 ObjectMonitor * volatile ObjectSynchronizer::gOmInUseList = NULL; |
116 int ObjectSynchronizer::gOmInUseCount = 0; |
119 int ObjectSynchronizer::gOmInUseCount = 0; |
117 static volatile intptr_t ListLock = 0; // protects global monitor free-list cache |
120 static volatile intptr_t ListLock = 0; // protects global monitor free-list cache |
408 // As a general policy we use "volatile" to control compiler-based reordering |
411 // As a general policy we use "volatile" to control compiler-based reordering |
409 // and explicit fences (barriers) to control for architectural reordering |
412 // and explicit fences (barriers) to control for architectural reordering |
410 // performed by the CPU(s) or platform. |
413 // performed by the CPU(s) or platform. |
411 |
414 |
412 struct SharedGlobals { |
415 struct SharedGlobals { |
|
416 char _pad_prefix[DEFAULT_CACHE_LINE_SIZE]; |
413 // These are highly shared mostly-read variables. |
417 // These are highly shared mostly-read variables. |
414 // To avoid false-sharing they need to be the sole occupants of a $ line. |
418 // To avoid false-sharing they need to be the sole occupants of a cache line. |
415 double padPrefix[8]; |
|
416 volatile int stwRandom; |
419 volatile int stwRandom; |
417 volatile int stwCycle; |
420 volatile int stwCycle; |
418 |
421 DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile int) * 2); |
419 // Hot RW variables -- Sequester to avoid false-sharing |
422 // Hot RW variable -- Sequester to avoid false-sharing |
420 double padSuffix[16]; |
|
421 volatile int hcSequence; |
423 volatile int hcSequence; |
422 double padFinal[8]; |
424 DEFINE_PAD_MINUS_SIZE(2, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile int)); |
423 }; |
425 }; |
424 |
426 |
425 static SharedGlobals GVars; |
427 static SharedGlobals GVars; |
426 static int MonitorScavengeThreshold = 1000000; |
428 static int MonitorScavengeThreshold = 1000000; |
427 static volatile int ForceMonitorScavenge = 0; // Scavenge required and pending |
429 static volatile int ForceMonitorScavenge = 0; // Scavenge required and pending |
778 return NULL; |
780 return NULL; |
779 } |
781 } |
780 // Visitors ... |
782 // Visitors ... |
781 |
783 |
782 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) { |
784 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) { |
783 ObjectMonitor* block = gBlockList; |
785 PaddedEnd<ObjectMonitor> * block = (PaddedEnd<ObjectMonitor> *)gBlockList; |
784 ObjectMonitor* mid; |
786 ObjectMonitor* mid; |
785 while (block) { |
787 while (block) { |
786 assert(block->object() == CHAINMARKER, "must be a block header"); |
788 assert(block->object() == CHAINMARKER, "must be a block header"); |
787 for (int i = _BLOCKSIZE - 1; i > 0; i--) { |
789 for (int i = _BLOCKSIZE - 1; i > 0; i--) { |
788 mid = block + i; |
790 mid = (ObjectMonitor *)(block + i); |
789 oop object = (oop) mid->object(); |
791 oop object = (oop) mid->object(); |
790 if (object != NULL) { |
792 if (object != NULL) { |
791 closure->do_monitor(mid); |
793 closure->do_monitor(mid); |
792 } |
794 } |
793 } |
795 } |
794 block = (ObjectMonitor*) block->FreeNext; |
796 block = (PaddedEnd<ObjectMonitor> *) block->FreeNext; |
795 } |
797 } |
796 } |
798 } |
797 |
799 |
798 // Get the next block in the block list. |
800 // Get the next block in the block list. |
799 static inline ObjectMonitor* next(ObjectMonitor* block) { |
801 static inline ObjectMonitor* next(ObjectMonitor* block) { |
804 } |
806 } |
805 |
807 |
806 |
808 |
807 void ObjectSynchronizer::oops_do(OopClosure* f) { |
809 void ObjectSynchronizer::oops_do(OopClosure* f) { |
808 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
810 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
809 for (ObjectMonitor* block = gBlockList; block != NULL; block = next(block)) { |
811 for (PaddedEnd<ObjectMonitor> * block = |
|
812 (PaddedEnd<ObjectMonitor> *)gBlockList; block != NULL; |
|
813 block = (PaddedEnd<ObjectMonitor> *)next(block)) { |
810 assert(block->object() == CHAINMARKER, "must be a block header"); |
814 assert(block->object() == CHAINMARKER, "must be a block header"); |
811 for (int i = 1; i < _BLOCKSIZE; i++) { |
815 for (int i = 1; i < _BLOCKSIZE; i++) { |
812 ObjectMonitor* mid = &block[i]; |
816 ObjectMonitor* mid = (ObjectMonitor *)&block[i]; |
813 if (mid->object() != NULL) { |
817 if (mid->object() != NULL) { |
814 f->do_oop((oop*)mid->object_addr()); |
818 f->do_oop((oop*)mid->object_addr()); |
815 } |
819 } |
816 } |
820 } |
817 } |
821 } |
964 } |
968 } |
965 |
969 |
966 // 3: allocate a block of new ObjectMonitors |
970 // 3: allocate a block of new ObjectMonitors |
967 // Both the local and global free lists are empty -- resort to malloc(). |
971 // Both the local and global free lists are empty -- resort to malloc(). |
968 // In the current implementation objectMonitors are TSM - immortal. |
972 // In the current implementation objectMonitors are TSM - immortal. |
|
973 // Ideally, we'd write "new ObjectMonitor[_BLOCKSIZE], but we want |
|
974 // each ObjectMonitor to start at the beginning of a cache line, |
|
975 // so we use align_size_up(). |
|
976 // A better solution would be to use C++ placement-new. |
|
977 // BEWARE: As it stands currently, we don't run the ctors! |
969 assert(_BLOCKSIZE > 1, "invariant"); |
978 assert(_BLOCKSIZE > 1, "invariant"); |
970 ObjectMonitor * temp = new ObjectMonitor[_BLOCKSIZE]; |
979 size_t neededsize = sizeof(PaddedEnd<ObjectMonitor>) * _BLOCKSIZE; |
|
980 PaddedEnd<ObjectMonitor> * temp; |
|
981 size_t aligned_size = neededsize + (DEFAULT_CACHE_LINE_SIZE - 1); |
|
982 void* real_malloc_addr = (void *)NEW_C_HEAP_ARRAY(char, aligned_size, |
|
983 mtInternal); |
|
984 temp = (PaddedEnd<ObjectMonitor> *) |
|
985 align_size_up((intptr_t)real_malloc_addr, |
|
986 DEFAULT_CACHE_LINE_SIZE); |
971 |
987 |
972 // NOTE: (almost) no way to recover if allocation failed. |
988 // NOTE: (almost) no way to recover if allocation failed. |
973 // We might be able to induce a STW safepoint and scavenge enough |
989 // We might be able to induce a STW safepoint and scavenge enough |
974 // objectMonitors to permit progress. |
990 // objectMonitors to permit progress. |
975 if (temp == NULL) { |
991 if (temp == NULL) { |
976 vm_exit_out_of_memory(sizeof (ObjectMonitor[_BLOCKSIZE]), OOM_MALLOC_ERROR, |
992 vm_exit_out_of_memory(neededsize, OOM_MALLOC_ERROR, |
977 "Allocate ObjectMonitors"); |
993 "Allocate ObjectMonitors"); |
978 } |
994 } |
|
995 (void)memset((void *) temp, 0, neededsize); |
979 |
996 |
980 // Format the block. |
997 // Format the block. |
981 // initialize the linked list, each monitor points to its next |
998 // initialize the linked list, each monitor points to its next |
982 // forming the single linked free list, the very first monitor |
999 // forming the single linked free list, the very first monitor |
983 // will points to next block, which forms the block list. |
1000 // will points to next block, which forms the block list. |
984 // The trick of using the 1st element in the block as gBlockList |
1001 // The trick of using the 1st element in the block as gBlockList |
985 // linkage should be reconsidered. A better implementation would |
1002 // linkage should be reconsidered. A better implementation would |
986 // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; } |
1003 // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; } |
987 |
1004 |
988 for (int i = 1; i < _BLOCKSIZE; i++) { |
1005 for (int i = 1; i < _BLOCKSIZE; i++) { |
989 temp[i].FreeNext = &temp[i+1]; |
1006 temp[i].FreeNext = (ObjectMonitor *)&temp[i+1]; |
990 } |
1007 } |
991 |
1008 |
992 // terminate the last monitor as the end of list |
1009 // terminate the last monitor as the end of list |
993 temp[_BLOCKSIZE - 1].FreeNext = NULL; |
1010 temp[_BLOCKSIZE - 1].FreeNext = NULL; |
994 |
1011 |
1139 } |
1156 } |
1140 return ObjectSynchronizer::inflate(Thread::current(), obj); |
1157 return ObjectSynchronizer::inflate(Thread::current(), obj); |
1141 } |
1158 } |
1142 |
1159 |
1143 |
1160 |
1144 // Note that we could encounter some performance loss through false-sharing as |
|
1145 // multiple locks occupy the same $ line. Padding might be appropriate. |
|
1146 |
|
1147 |
|
1148 ObjectMonitor * NOINLINE ObjectSynchronizer::inflate(Thread * Self, |
1161 ObjectMonitor * NOINLINE ObjectSynchronizer::inflate(Thread * Self, |
1149 oop object) { |
1162 oop object) { |
1150 // Inflate mutates the heap ... |
1163 // Inflate mutates the heap ... |
1151 // Relaxing assertion for bug 6320749. |
1164 // Relaxing assertion for bug 6320749. |
1152 assert(Universe::verify_in_progress() || |
1165 assert(Universe::verify_in_progress() || |
1208 // Optimistically prepare the objectmonitor - anticipate successful CAS |
1221 // Optimistically prepare the objectmonitor - anticipate successful CAS |
1209 // We do this before the CAS in order to minimize the length of time |
1222 // We do this before the CAS in order to minimize the length of time |
1210 // in which INFLATING appears in the mark. |
1223 // in which INFLATING appears in the mark. |
1211 m->Recycle(); |
1224 m->Recycle(); |
1212 m->_Responsible = NULL; |
1225 m->_Responsible = NULL; |
1213 m->OwnerIsThread = 0; |
|
1214 m->_recursions = 0; |
1226 m->_recursions = 0; |
1215 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit; // Consider: maintain by type/class |
1227 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit; // Consider: maintain by type/class |
1216 |
1228 |
1217 markOop cmp = (markOop) Atomic::cmpxchg_ptr(markOopDesc::INFLATING(), object->mark_addr(), mark); |
1229 markOop cmp = (markOop) Atomic::cmpxchg_ptr(markOopDesc::INFLATING(), object->mark_addr(), mark); |
1218 if (cmp != mark) { |
1230 if (cmp != mark) { |
1255 |
1267 |
1256 // Setup monitor fields to proper values -- prepare the monitor |
1268 // Setup monitor fields to proper values -- prepare the monitor |
1257 m->set_header(dmw); |
1269 m->set_header(dmw); |
1258 |
1270 |
1259 // Optimization: if the mark->locker stack address is associated |
1271 // Optimization: if the mark->locker stack address is associated |
1260 // with this thread we could simply set m->_owner = Self and |
1272 // with this thread we could simply set m->_owner = Self. |
1261 // m->OwnerIsThread = 1. Note that a thread can inflate an object |
1273 // Note that a thread can inflate an object |
1262 // that it has stack-locked -- as might happen in wait() -- directly |
1274 // that it has stack-locked -- as might happen in wait() -- directly |
1263 // with CAS. That is, we can avoid the xchg-NULL .... ST idiom. |
1275 // with CAS. That is, we can avoid the xchg-NULL .... ST idiom. |
1264 m->set_owner(mark->locker()); |
1276 m->set_owner(mark->locker()); |
1265 m->set_object(object); |
1277 m->set_object(object); |
1266 // TODO-FIXME: assert BasicLock->dhw != 0. |
1278 // TODO-FIXME: assert BasicLock->dhw != 0. |
1300 // prepare m for installation - set monitor to initial state |
1312 // prepare m for installation - set monitor to initial state |
1301 m->Recycle(); |
1313 m->Recycle(); |
1302 m->set_header(mark); |
1314 m->set_header(mark); |
1303 m->set_owner(NULL); |
1315 m->set_owner(NULL); |
1304 m->set_object(object); |
1316 m->set_object(object); |
1305 m->OwnerIsThread = 1; |
|
1306 m->_recursions = 0; |
1317 m->_recursions = 0; |
1307 m->_Responsible = NULL; |
1318 m->_Responsible = NULL; |
1308 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit; // consider: keep metastats by type/class |
1319 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit; // consider: keep metastats by type/class |
1309 |
1320 |
1310 if (Atomic::cmpxchg_ptr (markOopDesc::encode(m), object->mark_addr(), mark) != mark) { |
1321 if (Atomic::cmpxchg_ptr (markOopDesc::encode(m), object->mark_addr(), mark) != mark) { |
1311 m->set_object(NULL); |
1322 m->set_object(NULL); |
1312 m->set_owner(NULL); |
1323 m->set_owner(NULL); |
1313 m->OwnerIsThread = 0; |
|
1314 m->Recycle(); |
1324 m->Recycle(); |
1315 omRelease(Self, m, true); |
1325 omRelease(Self, m, true); |
1316 m = NULL; |
1326 m = NULL; |
1317 continue; |
1327 continue; |
1318 // interference - the markword changed - just retry. |
1328 // interference - the markword changed - just retry. |
1489 gOmInUseCount-= deflatedcount; |
1496 gOmInUseCount-= deflatedcount; |
1490 nScavenged += deflatedcount; |
1497 nScavenged += deflatedcount; |
1491 nInuse += gOmInUseCount; |
1498 nInuse += gOmInUseCount; |
1492 } |
1499 } |
1493 |
1500 |
1494 } else for (ObjectMonitor* block = gBlockList; block != NULL; block = next(block)) { |
1501 } else for (PaddedEnd<ObjectMonitor> * block = |
|
1502 (PaddedEnd<ObjectMonitor> *)gBlockList; block != NULL; |
|
1503 block = (PaddedEnd<ObjectMonitor> *)next(block)) { |
1495 // Iterate over all extant monitors - Scavenge all idle monitors. |
1504 // Iterate over all extant monitors - Scavenge all idle monitors. |
1496 assert(block->object() == CHAINMARKER, "must be a block header"); |
1505 assert(block->object() == CHAINMARKER, "must be a block header"); |
1497 nInCirculation += _BLOCKSIZE; |
1506 nInCirculation += _BLOCKSIZE; |
1498 for (int i = 1; i < _BLOCKSIZE; i++) { |
1507 for (int i = 1; i < _BLOCKSIZE; i++) { |
1499 ObjectMonitor* mid = &block[i]; |
1508 ObjectMonitor* mid = (ObjectMonitor*)&block[i]; |
1500 oop obj = (oop) mid->object(); |
1509 oop obj = (oop) mid->object(); |
1501 |
1510 |
1502 if (obj == NULL) { |
1511 if (obj == NULL) { |
1503 // The monitor is not associated with an object. |
1512 // The monitor is not associated with an object. |
1504 // The monitor should either be a thread-specific private |
1513 // The monitor should either be a thread-specific private |
1646 |
1655 |
1647 #ifndef PRODUCT |
1656 #ifndef PRODUCT |
1648 |
1657 |
1649 // Verify all monitors in the monitor cache, the verification is weak. |
1658 // Verify all monitors in the monitor cache, the verification is weak. |
1650 void ObjectSynchronizer::verify() { |
1659 void ObjectSynchronizer::verify() { |
1651 ObjectMonitor* block = gBlockList; |
1660 PaddedEnd<ObjectMonitor> * block = (PaddedEnd<ObjectMonitor> *)gBlockList; |
1652 ObjectMonitor* mid; |
1661 ObjectMonitor* mid; |
1653 while (block) { |
1662 while (block) { |
1654 assert(block->object() == CHAINMARKER, "must be a block header"); |
1663 assert(block->object() == CHAINMARKER, "must be a block header"); |
1655 for (int i = 1; i < _BLOCKSIZE; i++) { |
1664 for (int i = 1; i < _BLOCKSIZE; i++) { |
1656 mid = block + i; |
1665 mid = (ObjectMonitor *)(block + i); |
1657 oop object = (oop) mid->object(); |
1666 oop object = (oop) mid->object(); |
1658 if (object != NULL) { |
1667 if (object != NULL) { |
1659 mid->verify(); |
1668 mid->verify(); |
1660 } |
1669 } |
1661 } |
1670 } |
1662 block = (ObjectMonitor*) block->FreeNext; |
1671 block = (PaddedEnd<ObjectMonitor> *) block->FreeNext; |
1663 } |
1672 } |
1664 } |
1673 } |
1665 |
1674 |
1666 // Check if monitor belongs to the monitor cache |
1675 // Check if monitor belongs to the monitor cache |
1667 // The list is grow-only so it's *relatively* safe to traverse |
1676 // The list is grow-only so it's *relatively* safe to traverse |
1668 // the list of extant blocks without taking a lock. |
1677 // the list of extant blocks without taking a lock. |
1669 |
1678 |
1670 int ObjectSynchronizer::verify_objmon_isinpool(ObjectMonitor *monitor) { |
1679 int ObjectSynchronizer::verify_objmon_isinpool(ObjectMonitor *monitor) { |
1671 ObjectMonitor* block = gBlockList; |
1680 PaddedEnd<ObjectMonitor> * block = (PaddedEnd<ObjectMonitor> *)gBlockList; |
1672 |
1681 |
1673 while (block) { |
1682 while (block) { |
1674 assert(block->object() == CHAINMARKER, "must be a block header"); |
1683 assert(block->object() == CHAINMARKER, "must be a block header"); |
1675 if (monitor > &block[0] && monitor < &block[_BLOCKSIZE]) { |
1684 if (monitor > (ObjectMonitor *)&block[0] && |
|
1685 monitor < (ObjectMonitor *)&block[_BLOCKSIZE]) { |
1676 address mon = (address) monitor; |
1686 address mon = (address) monitor; |
1677 address blk = (address) block; |
1687 address blk = (address) block; |
1678 size_t diff = mon - blk; |
1688 size_t diff = mon - blk; |
1679 assert((diff % sizeof(ObjectMonitor)) == 0, "check"); |
1689 assert((diff % sizeof(PaddedEnd<ObjectMonitor>)) == 0, "check"); |
1680 return 1; |
1690 return 1; |
1681 } |
1691 } |
1682 block = (ObjectMonitor*) block->FreeNext; |
1692 block = (PaddedEnd<ObjectMonitor> *) block->FreeNext; |
1683 } |
1693 } |
1684 return 0; |
1694 return 0; |
1685 } |
1695 } |
1686 |
1696 |
1687 #endif |
1697 #endif |