< 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);

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










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



















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

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

  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);

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