< prev index next >

src/hotspot/share/runtime/synchronizer.cpp

Print this page

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

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

 908   }
 909 }
 910 
 911 // hashCode() generation :
 912 //
 913 // Possibilities:
 914 // * MD5Digest of {obj,stw_random}
 915 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 916 // * A DES- or AES-style SBox[] mechanism
 917 // * One of the Phi-based schemes, such as:
 918 //   2654435761 = 2^32 * Phi (golden ratio)
 919 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 920 // * A variation of Marsaglia's shift-xor RNG scheme.
 921 // * (obj ^ stw_random) is appealing, but can result
 922 //   in undesirable regularity in the hashCode values of adjacent objects
 923 //   (objects allocated back-to-back, in particular).  This could potentially
 924 //   result in hashtable collisions and reduced hashtable efficiency.
 925 //   There are simple ways to "diffuse" the middle address bits over the
 926 //   generated hashCode values:
 927 
 928 static intptr_t get_next_hash(Thread* current, oop obj) {
 929   intptr_t value = 0;
 930   if (hashCode == 0) {
 931     // This form uses global Park-Miller RNG.
 932     // On MP system we'll have lots of RW access to a global, so the
 933     // mechanism induces lots of coherency traffic.
 934     value = os::random();
 935   } else if (hashCode == 1) {
 936     // This variation has the property of being stable (idempotent)
 937     // between STW operations.  This can be useful in some of the 1-0
 938     // synchronization schemes.
 939     intptr_t addr_bits = cast_from_oop<intptr_t>(obj) >> 3;
 940     value = addr_bits ^ (addr_bits >> 5) ^ GVars.stw_random;
 941   } else if (hashCode == 2) {
 942     value = 1;            // for sensitivity testing
 943   } else if (hashCode == 3) {
 944     value = ++GVars.hc_sequence;
 945   } else if (hashCode == 4) {
 946     value = cast_from_oop<intptr_t>(obj);
 947   } else {
 948     // Marsaglia's xor-shift scheme with thread-specific state
 949     // This is probably the best overall implementation -- we'll
 950     // likely make this the default in future releases.
 951     unsigned t = current->_hashStateX;
 952     t ^= (t << 11);
 953     current->_hashStateX = current->_hashStateY;
 954     current->_hashStateY = current->_hashStateZ;
 955     current->_hashStateZ = current->_hashStateW;
 956     unsigned v = current->_hashStateW;
 957     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8));
 958     current->_hashStateW = v;
 959     value = v;










 960   }
 961 
 962   value &= markWord::hash_mask;
 963   if (value == 0) value = 0xBAD;
 964   assert(value != markWord::no_hash, "invariant");
 965   return value;
 966 }
 967 
 968 static intptr_t install_hash_code(Thread* current, oop obj) {
 969   assert(UseObjectMonitorTable && LockingMode == LM_LIGHTWEIGHT, "must be");
 970 
 971   markWord mark = obj->mark_acquire();
 972   for (;;) {
 973     intptr_t hash = mark.hash();
 974     if (hash != 0) {
 975       return hash;
 976     }



















 977 
 978     hash = get_next_hash(current, obj);
 979     const markWord old_mark = mark;
 980     const markWord new_mark = old_mark.copy_set_hash(hash);
 981 
 982     mark = obj->cas_set_mark(new_mark, old_mark);
 983     if (old_mark == mark) {
 984       return hash;

 985     }
 986   }
 987 }
 988 
 989 intptr_t ObjectSynchronizer::FastHashCode(Thread* current, oop obj) {
 990   if (UseObjectMonitorTable) {
 991     // Since the monitor isn't in the object header, the hash can simply be
 992     // installed in the object header.
 993     return install_hash_code(current, obj);
 994   }
 995 
 996   while (true) {
 997     ObjectMonitor* monitor = nullptr;
 998     markWord temp, test;
 999     intptr_t hash;
1000     markWord mark = read_stable_mark(obj);
1001     if (VerifyHeavyMonitors) {
1002       assert(LockingMode == LM_MONITOR, "+VerifyHeavyMonitors requires LockingMode == 0 (LM_MONITOR)");
1003       guarantee((obj->mark().value() & markWord::lock_mask_in_place) != markWord::locked_value, "must not be lightweight/stack-locked");
1004     }

  46 #include "runtime/mutexLocker.hpp"
  47 #include "runtime/objectMonitor.hpp"
  48 #include "runtime/objectMonitor.inline.hpp"
  49 #include "runtime/os.inline.hpp"
  50 #include "runtime/osThread.hpp"
  51 #include "runtime/safepointMechanism.inline.hpp"
  52 #include "runtime/safepointVerifiers.hpp"
  53 #include "runtime/sharedRuntime.hpp"
  54 #include "runtime/stubRoutines.hpp"
  55 #include "runtime/synchronizer.inline.hpp"
  56 #include "runtime/threads.hpp"
  57 #include "runtime/timer.hpp"
  58 #include "runtime/trimNativeHeap.hpp"
  59 #include "runtime/vframe.hpp"
  60 #include "runtime/vmThread.hpp"
  61 #include "utilities/align.hpp"
  62 #include "utilities/dtrace.hpp"
  63 #include "utilities/events.hpp"
  64 #include "utilities/globalCounter.inline.hpp"
  65 #include "utilities/globalDefinitions.hpp"
  66 #include "utilities/fastHash.hpp"
  67 #include "utilities/linkedlist.hpp"
  68 #include "utilities/preserveException.hpp"
  69 
  70 class ObjectMonitorDeflationLogging;
  71 
  72 void MonitorList::add(ObjectMonitor* m) {
  73   ObjectMonitor* head;
  74   do {
  75     head = Atomic::load(&_head);
  76     m->set_next_om(head);
  77   } while (Atomic::cmpxchg(&_head, head, m) != head);
  78 
  79   size_t count = Atomic::add(&_count, 1u, memory_order_relaxed);
  80   size_t old_max;
  81   do {
  82     old_max = Atomic::load(&_max);
  83     if (count <= old_max) {
  84       break;
  85     }
  86   } while (Atomic::cmpxchg(&_max, old_max, count, memory_order_relaxed) != old_max);

 909   }
 910 }
 911 
 912 // hashCode() generation :
 913 //
 914 // Possibilities:
 915 // * MD5Digest of {obj,stw_random}
 916 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 917 // * A DES- or AES-style SBox[] mechanism
 918 // * One of the Phi-based schemes, such as:
 919 //   2654435761 = 2^32 * Phi (golden ratio)
 920 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 921 // * A variation of Marsaglia's shift-xor RNG scheme.
 922 // * (obj ^ stw_random) is appealing, but can result
 923 //   in undesirable regularity in the hashCode values of adjacent objects
 924 //   (objects allocated back-to-back, in particular).  This could potentially
 925 //   result in hashtable collisions and reduced hashtable efficiency.
 926 //   There are simple ways to "diffuse" the middle address bits over the
 927 //   generated hashCode values:
 928 
 929 intptr_t ObjectSynchronizer::get_next_hash(Thread* current, oop obj) {
 930   intptr_t value = 0;
 931   if (hashCode == 0) {
 932     // This form uses global Park-Miller RNG.
 933     // On MP system we'll have lots of RW access to a global, so the
 934     // mechanism induces lots of coherency traffic.
 935     value = os::random();
 936   } else if (hashCode == 1) {
 937     // This variation has the property of being stable (idempotent)
 938     // between STW operations.  This can be useful in some of the 1-0
 939     // synchronization schemes.
 940     intptr_t addr_bits = cast_from_oop<intptr_t>(obj) >> 3;
 941     value = addr_bits ^ (addr_bits >> 5) ^ GVars.stw_random;
 942   } else if (hashCode == 2) {
 943     value = 1;            // for sensitivity testing
 944   } else if (hashCode == 3) {
 945     value = ++GVars.hc_sequence;
 946   } else if (hashCode == 4) {
 947     value = cast_from_oop<intptr_t>(obj);
 948   } else if (hashCode == 5) {
 949     // Marsaglia's xor-shift scheme with thread-specific state
 950     // This is probably the best overall implementation -- we'll
 951     // likely make this the default in future releases.
 952     unsigned t = current->_hashStateX;
 953     t ^= (t << 11);
 954     current->_hashStateX = current->_hashStateY;
 955     current->_hashStateY = current->_hashStateZ;
 956     current->_hashStateZ = current->_hashStateW;
 957     unsigned v = current->_hashStateW;
 958     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8));
 959     current->_hashStateW = v;
 960     value = v;
 961   } else {
 962     assert(UseCompactObjectHeaders, "Only with compact i-hash");
 963 #ifdef _LP64
 964     uint64_t val = cast_from_oop<uint64_t>(obj);
 965     uint32_t hash = FastHash::get_hash32((uint32_t)val, (uint32_t)(val >> 32));
 966 #else
 967     uint32_t val = cast_from_oop<uint32_t>(obj);
 968     uint32_t hash = FastHash::get_hash32(val, UCONST64(0xAAAAAAAA));
 969 #endif
 970     value= static_cast<intptr_t>(hash);
 971   }
 972 
 973   value &= markWord::hash_mask;
 974   if (hashCode != 6 && value == 0) value = 0xBAD;
 975   assert(value != markWord::no_hash || hashCode == 6, "invariant");
 976   return value;
 977 }
 978 
 979 static intptr_t install_hash_code(Thread* current, oop obj) {
 980   assert(UseObjectMonitorTable && LockingMode == LM_LIGHTWEIGHT, "must be");
 981 
 982   markWord mark = obj->mark_acquire();
 983   for (;;) {
 984     if (UseCompactObjectHeaders) {
 985       if (mark.is_hashed()) {
 986         return LightweightSynchronizer::get_hash(mark, obj);
 987       }
 988       intptr_t hash = ObjectSynchronizer::get_next_hash(current, obj);  // get a new hash
 989       markWord new_mark;
 990       if (mark.is_not_hashed_expanded()) {
 991         new_mark = mark.set_hashed_expanded();
 992         int offset = mark.klass()->hash_offset_in_bytes(obj, mark);
 993         obj->int_field_put(offset, (jint) hash);
 994       } else {
 995         new_mark = mark.set_hashed_not_expanded();
 996       }
 997       markWord old_mark = obj->cas_set_mark(new_mark, mark);
 998       if (old_mark == mark) {
 999         return hash;
1000       }
1001       mark = old_mark;
1002     } else {
1003       intptr_t hash = mark.hash();
1004       if (hash != 0) {
1005         return hash;
1006       }
1007 
1008       hash = ObjectSynchronizer::get_next_hash(current, obj);
1009       const markWord old_mark = mark;
1010       const markWord new_mark = old_mark.copy_set_hash(hash);
1011 
1012       mark = obj->cas_set_mark(new_mark, old_mark);
1013       if (old_mark == mark) {
1014         return hash;
1015       }
1016     }
1017   }
1018 }
1019 
1020 intptr_t ObjectSynchronizer::FastHashCode(Thread* current, oop obj) {
1021   if (UseObjectMonitorTable) {
1022     // Since the monitor isn't in the object header, the hash can simply be
1023     // installed in the object header.
1024     return install_hash_code(current, obj);
1025   }
1026 
1027   while (true) {
1028     ObjectMonitor* monitor = nullptr;
1029     markWord temp, test;
1030     intptr_t hash;
1031     markWord mark = read_stable_mark(obj);
1032     if (VerifyHeavyMonitors) {
1033       assert(LockingMode == LM_MONITOR, "+VerifyHeavyMonitors requires LockingMode == 0 (LM_MONITOR)");
1034       guarantee((obj->mark().value() & markWord::lock_mask_in_place) != markWord::locked_value, "must not be lightweight/stack-locked");
1035     }
< prev index next >