< prev index next >

src/hotspot/share/runtime/synchronizer.cpp

Print this page

  47 #include "runtime/mutexLocker.hpp"
  48 #include "runtime/objectMonitor.hpp"
  49 #include "runtime/objectMonitor.inline.hpp"
  50 #include "runtime/os.inline.hpp"
  51 #include "runtime/osThread.hpp"
  52 #include "runtime/perfData.hpp"
  53 #include "runtime/safepointMechanism.inline.hpp"
  54 #include "runtime/safepointVerifiers.hpp"
  55 #include "runtime/sharedRuntime.hpp"
  56 #include "runtime/stubRoutines.hpp"
  57 #include "runtime/synchronizer.inline.hpp"
  58 #include "runtime/threads.hpp"
  59 #include "runtime/timer.hpp"
  60 #include "runtime/trimNativeHeap.hpp"
  61 #include "runtime/vframe.hpp"
  62 #include "runtime/vmThread.hpp"
  63 #include "utilities/align.hpp"
  64 #include "utilities/dtrace.hpp"
  65 #include "utilities/events.hpp"
  66 #include "utilities/globalDefinitions.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);
  80   if (count > max()) {
  81     Atomic::inc(&_max);
  82   }
  83 }
  84 
  85 size_t MonitorList::count() const {
  86   return Atomic::load(&_count);

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










 967   }
 968 
 969   value &= markWord::hash_mask;
 970   if (value == 0) value = 0xBAD;
 971   assert(value != markWord::no_hash, "invariant");
 972   return value;
 973 }
 974 
 975 static intptr_t install_hash_code(Thread* current, oop obj) {
 976   assert(UseObjectMonitorTable && LockingMode == LM_LIGHTWEIGHT, "must be");
 977 
 978   markWord mark = obj->mark_acquire();
 979   for (;;) {
 980     intptr_t hash = mark.hash();
 981     if (hash != 0) {
 982       return hash;
 983     }



















 984 
 985     hash = get_next_hash(current, obj);
 986     const markWord old_mark = mark;
 987     const markWord new_mark = old_mark.copy_set_hash(hash);
 988 
 989     mark = obj->cas_set_mark(new_mark, old_mark);
 990     if (old_mark == mark) {
 991       return hash;

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

  47 #include "runtime/mutexLocker.hpp"
  48 #include "runtime/objectMonitor.hpp"
  49 #include "runtime/objectMonitor.inline.hpp"
  50 #include "runtime/os.inline.hpp"
  51 #include "runtime/osThread.hpp"
  52 #include "runtime/perfData.hpp"
  53 #include "runtime/safepointMechanism.inline.hpp"
  54 #include "runtime/safepointVerifiers.hpp"
  55 #include "runtime/sharedRuntime.hpp"
  56 #include "runtime/stubRoutines.hpp"
  57 #include "runtime/synchronizer.inline.hpp"
  58 #include "runtime/threads.hpp"
  59 #include "runtime/timer.hpp"
  60 #include "runtime/trimNativeHeap.hpp"
  61 #include "runtime/vframe.hpp"
  62 #include "runtime/vmThread.hpp"
  63 #include "utilities/align.hpp"
  64 #include "utilities/dtrace.hpp"
  65 #include "utilities/events.hpp"
  66 #include "utilities/globalDefinitions.hpp"
  67 #include "utilities/fastHash.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 = Atomic::load(&_head);
  77     m->set_next_om(head);
  78   } while (Atomic::cmpxchg(&_head, head, m) != head);
  79 
  80   size_t count = Atomic::add(&_count, 1u);
  81   if (count > max()) {
  82     Atomic::inc(&_max);
  83   }
  84 }
  85 
  86 size_t MonitorList::count() const {
  87   return Atomic::load(&_count);

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