< prev index next >

src/hotspot/share/runtime/synchronizer.cpp

Print this page

  46 #include "runtime/objectMonitor.inline.hpp"
  47 #include "runtime/objectMonitorTable.hpp"
  48 #include "runtime/os.inline.hpp"
  49 #include "runtime/osThread.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/timerTrace.hpp"
  58 #include "runtime/trimNativeHeap.hpp"
  59 #include "runtime/vframe.hpp"
  60 #include "runtime/vmThread.hpp"
  61 #include "utilities/align.hpp"
  62 #include "utilities/concurrentHashTable.inline.hpp"
  63 #include "utilities/concurrentHashTableTasks.inline.hpp"
  64 #include "utilities/dtrace.hpp"
  65 #include "utilities/events.hpp"

  66 #include "utilities/globalCounter.inline.hpp"
  67 #include "utilities/globalDefinitions.hpp"
  68 #include "utilities/linkedlist.hpp"
  69 #include "utilities/preserveException.hpp"
  70 
  71 class ObjectMonitorDeflationLogging;
  72 
  73 void MonitorList::add(ObjectMonitor* m) {
  74   ObjectMonitor* head;
  75   do {
  76     head = AtomicAccess::load(&_head);
  77     m->set_next_om(head);
  78   } while (AtomicAccess::cmpxchg(&_head, head, m) != head);
  79 
  80   size_t count = AtomicAccess::add(&_count, 1u, memory_order_relaxed);
  81   size_t old_max;
  82   do {
  83     old_max = AtomicAccess::load(&_max);
  84     if (count <= old_max) {
  85       break;

 582 
 583 static SharedGlobals GVars;
 584 
 585 // hashCode() generation :
 586 //
 587 // Possibilities:
 588 // * MD5Digest of {obj,stw_random}
 589 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 590 // * A DES- or AES-style SBox[] mechanism
 591 // * One of the Phi-based schemes, such as:
 592 //   2654435761 = 2^32 * Phi (golden ratio)
 593 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 594 // * A variation of Marsaglia's shift-xor RNG scheme.
 595 // * (obj ^ stw_random) is appealing, but can result
 596 //   in undesirable regularity in the hashCode values of adjacent objects
 597 //   (objects allocated back-to-back, in particular).  This could potentially
 598 //   result in hashtable collisions and reduced hashtable efficiency.
 599 //   There are simple ways to "diffuse" the middle address bits over the
 600 //   generated hashCode values:
 601 
 602 static intptr_t get_next_hash(Thread* current, oop obj) {
 603   intptr_t value = 0;
 604   if (hashCode == 0) {
 605     // This form uses global Park-Miller RNG.
 606     // On MP system we'll have lots of RW access to a global, so the
 607     // mechanism induces lots of coherency traffic.
 608     value = os::random();
 609   } else if (hashCode == 1) {
 610     // This variation has the property of being stable (idempotent)
 611     // between STW operations.  This can be useful in some of the 1-0
 612     // synchronization schemes.
 613     intptr_t addr_bits = cast_from_oop<intptr_t>(obj) >> 3;
 614     value = addr_bits ^ (addr_bits >> 5) ^ GVars.stw_random;
 615   } else if (hashCode == 2) {
 616     value = 1;            // for sensitivity testing
 617   } else if (hashCode == 3) {
 618     value = ++GVars.hc_sequence;
 619   } else if (hashCode == 4) {
 620     value = cast_from_oop<intptr_t>(obj);
 621   } else {
 622     // Marsaglia's xor-shift scheme with thread-specific state
 623     // This is probably the best overall implementation -- we'll
 624     // likely make this the default in future releases.
 625     unsigned t = current->_hashStateX;
 626     t ^= (t << 11);
 627     current->_hashStateX = current->_hashStateY;
 628     current->_hashStateY = current->_hashStateZ;
 629     current->_hashStateZ = current->_hashStateW;
 630     unsigned v = current->_hashStateW;
 631     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8));
 632     current->_hashStateW = v;
 633     value = v;










 634   }
 635 
 636   value &= markWord::hash_mask;
 637   if (value == 0) value = 0xBAD;
 638   assert(value != markWord::no_hash, "invariant");
 639   return value;
 640 }
 641 
 642 intptr_t ObjectSynchronizer::FastHashCode(Thread* current, oop obj) {
 643   while (true) {
 644     ObjectMonitor* monitor = nullptr;
 645     markWord temp, test;
 646     intptr_t hash;
 647     markWord mark = obj->mark_acquire();
 648     // If UseObjectMonitorTable is set the hash can simply be installed in the
 649     // object header, since the monitor isn't in the object header.
 650     if (UseObjectMonitorTable || !mark.has_monitor()) {



















 651       hash = mark.hash();
 652       if (hash != 0) {                     // if it has a hash, just return it
 653         return hash;
 654       }
 655       hash = get_next_hash(current, obj);  // get a new hash
 656       temp = mark.copy_set_hash(hash);     // merge the hash into header
 657                                            // try to install the hash
 658       test = obj->cas_set_mark(temp, mark);
 659       if (test == mark) {                  // if the hash was installed, return it
 660         return hash;
 661       }
 662       // CAS failed, retry
 663       continue;
 664 
 665       // Failed to install the hash. It could be that another thread
 666       // installed the hash just before our attempt or inflation has
 667       // occurred or... so we fall thru to inflate the monitor for
 668       // stability and then install the hash.
 669     } else {
 670       assert(!mark.is_unlocked() && !mark.is_fast_locked(), "invariant");

 719         // If we add any new usages of the header/dmw field, this code
 720         // will need to be updated.
 721         hash = test.hash();
 722         assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
 723         assert(hash != 0, "should only have lost the race to a thread that set a non-zero hash");
 724       }
 725       if (monitor->is_being_async_deflated() && !UseObjectMonitorTable) {
 726         // If we detect that async deflation has occurred, then we
 727         // attempt to restore the header/dmw to the object's header
 728         // so that we only retry once if the deflater thread happens
 729         // to be slow.
 730         monitor->install_displaced_markword_in_object(obj);
 731         continue;
 732       }
 733     }
 734     // We finally get the hash.
 735     return hash;
 736   }
 737 }
 738 



















 739 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* current,
 740                                                    Handle h_obj) {
 741   assert(current == JavaThread::current(), "Can only be called on current thread");
 742   oop obj = h_obj();
 743 
 744   markWord mark = obj->mark_acquire();
 745 
 746   if (mark.is_fast_locked()) {
 747     // fast-locking case, see if lock is in current's lock stack
 748     return current->lock_stack().contains(h_obj());
 749   }
 750 
 751   while (mark.has_monitor()) {
 752     ObjectMonitor* monitor = read_monitor(obj, mark);
 753     if (monitor != nullptr) {
 754       return monitor->is_entered(current) != 0;
 755     }
 756     // Racing with inflation/deflation, retry
 757     mark = obj->mark_acquire();
 758 

1529   ObjectMonitor* monitor = get_or_insert_monitor_from_table(object, &inserted);
1530 
1531   if (inserted) {
1532     log_inflate(current, object, cause);
1533     if (event.should_commit()) {
1534       post_monitor_inflate_event(&event, object, cause);
1535     }
1536 
1537     // The monitor has an anonymous owner so it is safe from async deflation.
1538     ObjectSynchronizer::_in_use_list.add(monitor);
1539   }
1540 
1541   return monitor;
1542 }
1543 
1544 // Add the hashcode to the monitor to match the object and put it in the hashtable.
1545 ObjectMonitor* ObjectSynchronizer::add_monitor(ObjectMonitor* monitor, oop obj) {
1546   assert(UseObjectMonitorTable, "must be");
1547   assert(obj == monitor->object(), "must be");
1548 
1549   intptr_t hash = obj->mark().hash();






1550   assert(hash != 0, "must be set when claiming the object monitor");
1551   monitor->set_hash(hash);
1552 
1553   return ObjectMonitorTable::monitor_put_get(monitor, obj);
1554 }
1555 
1556 void ObjectSynchronizer::remove_monitor(ObjectMonitor* monitor, oop obj) {
1557   assert(UseObjectMonitorTable, "must be");
1558   assert(monitor->object_peek() == obj, "must be, cleared objects are removed by is_dead");
1559 
1560   ObjectMonitorTable::remove_monitor_entry(monitor);
1561 }
1562 
1563 void ObjectSynchronizer::deflate_mark_word(oop obj) {
1564   assert(UseObjectMonitorTable, "must be");
1565 
1566   markWord mark = obj->mark_acquire();
1567   assert(!mark.has_no_hash(), "obj with inflated monitor must have had a hash");
1568 
1569   while (mark.has_monitor()) {

  46 #include "runtime/objectMonitor.inline.hpp"
  47 #include "runtime/objectMonitorTable.hpp"
  48 #include "runtime/os.inline.hpp"
  49 #include "runtime/osThread.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/timerTrace.hpp"
  58 #include "runtime/trimNativeHeap.hpp"
  59 #include "runtime/vframe.hpp"
  60 #include "runtime/vmThread.hpp"
  61 #include "utilities/align.hpp"
  62 #include "utilities/concurrentHashTable.inline.hpp"
  63 #include "utilities/concurrentHashTableTasks.inline.hpp"
  64 #include "utilities/dtrace.hpp"
  65 #include "utilities/events.hpp"
  66 #include "utilities/fastHash.hpp"
  67 #include "utilities/globalCounter.inline.hpp"
  68 #include "utilities/globalDefinitions.hpp"
  69 #include "utilities/linkedlist.hpp"
  70 #include "utilities/preserveException.hpp"
  71 
  72 class ObjectMonitorDeflationLogging;
  73 
  74 void MonitorList::add(ObjectMonitor* m) {
  75   ObjectMonitor* head;
  76   do {
  77     head = AtomicAccess::load(&_head);
  78     m->set_next_om(head);
  79   } while (AtomicAccess::cmpxchg(&_head, head, m) != head);
  80 
  81   size_t count = AtomicAccess::add(&_count, 1u, memory_order_relaxed);
  82   size_t old_max;
  83   do {
  84     old_max = AtomicAccess::load(&_max);
  85     if (count <= old_max) {
  86       break;

 583 
 584 static SharedGlobals GVars;
 585 
 586 // hashCode() generation :
 587 //
 588 // Possibilities:
 589 // * MD5Digest of {obj,stw_random}
 590 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 591 // * A DES- or AES-style SBox[] mechanism
 592 // * One of the Phi-based schemes, such as:
 593 //   2654435761 = 2^32 * Phi (golden ratio)
 594 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 595 // * A variation of Marsaglia's shift-xor RNG scheme.
 596 // * (obj ^ stw_random) is appealing, but can result
 597 //   in undesirable regularity in the hashCode values of adjacent objects
 598 //   (objects allocated back-to-back, in particular).  This could potentially
 599 //   result in hashtable collisions and reduced hashtable efficiency.
 600 //   There are simple ways to "diffuse" the middle address bits over the
 601 //   generated hashCode values:
 602 
 603 intptr_t ObjectSynchronizer::get_next_hash(Thread* current, oop obj) {
 604   intptr_t value = 0;
 605   if (hashCode == 0) {
 606     // This form uses global Park-Miller RNG.
 607     // On MP system we'll have lots of RW access to a global, so the
 608     // mechanism induces lots of coherency traffic.
 609     value = os::random();
 610   } else if (hashCode == 1) {
 611     // This variation has the property of being stable (idempotent)
 612     // between STW operations.  This can be useful in some of the 1-0
 613     // synchronization schemes.
 614     intptr_t addr_bits = cast_from_oop<intptr_t>(obj) >> 3;
 615     value = addr_bits ^ (addr_bits >> 5) ^ GVars.stw_random;
 616   } else if (hashCode == 2) {
 617     value = 1;            // for sensitivity testing
 618   } else if (hashCode == 3) {
 619     value = ++GVars.hc_sequence;
 620   } else if (hashCode == 4) {
 621     value = cast_from_oop<intptr_t>(obj);
 622   } else if (hashCode == 5) {
 623     // Marsaglia's xor-shift scheme with thread-specific state
 624     // This is probably the best overall implementation -- we'll
 625     // likely make this the default in future releases.
 626     unsigned t = current->_hashStateX;
 627     t ^= (t << 11);
 628     current->_hashStateX = current->_hashStateY;
 629     current->_hashStateY = current->_hashStateZ;
 630     current->_hashStateZ = current->_hashStateW;
 631     unsigned v = current->_hashStateW;
 632     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8));
 633     current->_hashStateW = v;
 634     value = v;
 635   } else {
 636     assert(UseCompactObjectHeaders, "Only with compact i-hash");
 637 #ifdef _LP64
 638     uint64_t val = cast_from_oop<uint64_t>(obj);
 639     uint32_t hash = FastHash::get_hash32((uint32_t)val, (uint32_t)(val >> 32));
 640 #else
 641     uint32_t val = cast_from_oop<uint32_t>(obj);
 642     uint32_t hash = FastHash::get_hash32(val, UCONST64(0xAAAAAAAA));
 643 #endif
 644     value= static_cast<intptr_t>(hash);
 645   }
 646 
 647   value &= markWord::hash_mask;
 648   if (hashCode != 6 && value == 0) value = 0xBAD;
 649   assert(value != markWord::no_hash || hashCode == 6, "invariant");
 650   return value;
 651 }
 652 
 653 intptr_t ObjectSynchronizer::FastHashCode(Thread* current, oop obj) {
 654   while (true) {
 655     ObjectMonitor* monitor = nullptr;
 656     markWord temp, test;
 657     intptr_t hash;
 658     markWord mark = obj->mark_acquire();
 659     if (UseCompactObjectHeaders) {
 660       if (mark.is_hashed()) {
 661         return get_hash(mark, obj);
 662       }
 663       intptr_t hash = get_next_hash(current, obj);  // get a new hash
 664       markWord new_mark;
 665       if (mark.is_not_hashed_expanded()) {
 666         new_mark = mark.set_hashed_expanded();
 667         int offset = mark.klass()->hash_offset_in_bytes(obj, mark);
 668         obj->int_field_put(offset, (jint) hash);
 669       } else {
 670         new_mark = mark.set_hashed_not_expanded();
 671       }
 672       markWord old_mark = obj->cas_set_mark(new_mark, mark);
 673       if (old_mark == mark) {
 674         return hash;
 675       }
 676       // CAS failed, retry.
 677       continue;
 678     } else if (UseObjectMonitorTable || !mark.has_monitor()) {
 679       // If UseObjectMonitorTable is set the hash can simply be installed in the
 680       // object header, since the monitor isn't in the object header.
 681       hash = mark.hash();
 682       if (hash != 0) {                     // if it has a hash, just return it
 683         return hash;
 684       }
 685       hash = get_next_hash(current, obj);  // get a new hash
 686       temp = mark.copy_set_hash(hash);     // merge the hash into header
 687                                            // try to install the hash
 688       test = obj->cas_set_mark(temp, mark);
 689       if (test == mark) {                  // if the hash was installed, return it
 690         return hash;
 691       }
 692       // CAS failed, retry
 693       continue;
 694 
 695       // Failed to install the hash. It could be that another thread
 696       // installed the hash just before our attempt or inflation has
 697       // occurred or... so we fall thru to inflate the monitor for
 698       // stability and then install the hash.
 699     } else {
 700       assert(!mark.is_unlocked() && !mark.is_fast_locked(), "invariant");

 749         // If we add any new usages of the header/dmw field, this code
 750         // will need to be updated.
 751         hash = test.hash();
 752         assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
 753         assert(hash != 0, "should only have lost the race to a thread that set a non-zero hash");
 754       }
 755       if (monitor->is_being_async_deflated() && !UseObjectMonitorTable) {
 756         // If we detect that async deflation has occurred, then we
 757         // attempt to restore the header/dmw to the object's header
 758         // so that we only retry once if the deflater thread happens
 759         // to be slow.
 760         monitor->install_displaced_markword_in_object(obj);
 761         continue;
 762       }
 763     }
 764     // We finally get the hash.
 765     return hash;
 766   }
 767 }
 768 
 769 
 770 uint32_t ObjectSynchronizer::get_hash(markWord mark, oop obj, Klass* klass) {
 771   assert(UseCompactObjectHeaders, "Only with compact i-hash");
 772   //assert(mark.is_neutral() | mark.is_fast_locked(), "only from neutral or fast-locked mark: " INTPTR_FORMAT, mark.value());
 773   assert(mark.is_hashed(), "only from hashed or copied object");
 774   if (mark.is_hashed_expanded()) {
 775     return obj->int_field(klass->hash_offset_in_bytes(obj, mark));
 776   } else {
 777     assert(mark.is_hashed_not_expanded(), "must be hashed");
 778     assert(hashCode == 6 || hashCode == 2, "must have idempotent hashCode");
 779     // Already marked as hashed, but not yet copied. Recompute hash and return it.
 780     return ObjectSynchronizer::get_next_hash(nullptr, obj); // recompute hash
 781   }
 782 }
 783 
 784 uint32_t ObjectSynchronizer::get_hash(markWord mark, oop obj) {
 785   return get_hash(mark, obj, mark.klass());
 786 }
 787 
 788 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* current,
 789                                                    Handle h_obj) {
 790   assert(current == JavaThread::current(), "Can only be called on current thread");
 791   oop obj = h_obj();
 792 
 793   markWord mark = obj->mark_acquire();
 794 
 795   if (mark.is_fast_locked()) {
 796     // fast-locking case, see if lock is in current's lock stack
 797     return current->lock_stack().contains(h_obj());
 798   }
 799 
 800   while (mark.has_monitor()) {
 801     ObjectMonitor* monitor = read_monitor(obj, mark);
 802     if (monitor != nullptr) {
 803       return monitor->is_entered(current) != 0;
 804     }
 805     // Racing with inflation/deflation, retry
 806     mark = obj->mark_acquire();
 807 

1578   ObjectMonitor* monitor = get_or_insert_monitor_from_table(object, &inserted);
1579 
1580   if (inserted) {
1581     log_inflate(current, object, cause);
1582     if (event.should_commit()) {
1583       post_monitor_inflate_event(&event, object, cause);
1584     }
1585 
1586     // The monitor has an anonymous owner so it is safe from async deflation.
1587     ObjectSynchronizer::_in_use_list.add(monitor);
1588   }
1589 
1590   return monitor;
1591 }
1592 
1593 // Add the hashcode to the monitor to match the object and put it in the hashtable.
1594 ObjectMonitor* ObjectSynchronizer::add_monitor(ObjectMonitor* monitor, oop obj) {
1595   assert(UseObjectMonitorTable, "must be");
1596   assert(obj == monitor->object(), "must be");
1597 
1598   markWord mark = obj->mark();
1599   intptr_t hash;
1600   if (UseCompactObjectHeaders) {
1601     hash = static_cast<intptr_t>(get_hash(mark, obj));
1602   } else {
1603     hash = mark.hash();
1604   }
1605   assert(hash != 0, "must be set when claiming the object monitor");
1606   monitor->set_hash(hash);
1607 
1608   return ObjectMonitorTable::monitor_put_get(monitor, obj);
1609 }
1610 
1611 void ObjectSynchronizer::remove_monitor(ObjectMonitor* monitor, oop obj) {
1612   assert(UseObjectMonitorTable, "must be");
1613   assert(monitor->object_peek() == obj, "must be, cleared objects are removed by is_dead");
1614 
1615   ObjectMonitorTable::remove_monitor_entry(monitor);
1616 }
1617 
1618 void ObjectSynchronizer::deflate_mark_word(oop obj) {
1619   assert(UseObjectMonitorTable, "must be");
1620 
1621   markWord mark = obj->mark_acquire();
1622   assert(!mark.has_no_hash(), "obj with inflated monitor must have had a hash");
1623 
1624   while (mark.has_monitor()) {
< prev index next >