1 /* 2 * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_OOPS_MARKWORD_HPP 26 #define SHARE_OOPS_MARKWORD_HPP 27 28 #include "metaprogramming/integralConstant.hpp" 29 #include "metaprogramming/primitiveConversions.hpp" 30 #include "oops/oopsHierarchy.hpp" 31 #include "runtime/globals.hpp" 32 33 // The markWord describes the header of an object. 34 // 35 // Bit-format of an object header (most significant first, big endian layout below): 36 // 37 // 32 bits: 38 // -------- 39 // hash:25 ------------>| age:4 biased_lock:1 lock:2 (normal object) 40 // JavaThread*:23 epoch:2 age:4 biased_lock:1 lock:2 (biased object) 41 // 42 // 64 bits: 43 // -------- 44 // nklass:32 hash:25 -->| unused_gap:1 age:4 biased_lock:1 lock:2 (normal object) 45 // JavaThread*:54 epoch:2 unused_gap:1 age:4 biased_lock:1 lock:2 (biased object) 46 // 47 // - hash contains the identity hash value: largest value is 48 // 31 bits, see os::random(). Also, 64-bit vm's require 49 // a hash value no bigger than 32 bits because they will not 50 // properly generate a mask larger than that: see library_call.cpp 51 // 52 // - the biased lock pattern is used to bias a lock toward a given 53 // thread. When this pattern is set in the low three bits, the lock 54 // is either biased toward a given thread or "anonymously" biased, 55 // indicating that it is possible for it to be biased. When the 56 // lock is biased toward a given thread, locking and unlocking can 57 // be performed by that thread without using atomic operations. 58 // When a lock's bias is revoked, it reverts back to the normal 59 // locking scheme described below. 60 // 61 // Note that we are overloading the meaning of the "unlocked" state 62 // of the header. Because we steal a bit from the age we can 63 // guarantee that the bias pattern will never be seen for a truly 64 // unlocked object. 65 // 66 // Note also that the biased state contains the age bits normally 67 // contained in the object header. Large increases in scavenge 68 // times were seen when these bits were absent and an arbitrary age 69 // assigned to all biased objects, because they tended to consume a 70 // significant fraction of the eden semispaces and were not 71 // promoted promptly, causing an increase in the amount of copying 72 // performed. The runtime system aligns all JavaThread* pointers to 73 // a very large value (currently 128 bytes (32bVM) or 256 bytes (64bVM)) 74 // to make room for the age bits & the epoch bits (used in support of 75 // biased locking). 76 // 77 // [JavaThread* | epoch | age | 1 | 01] lock is biased toward given thread 78 // [0 | epoch | age | 1 | 01] lock is anonymously biased 79 // 80 // - the two lock bits are used to describe three states: locked/unlocked and monitor. 81 // 82 // [ptr | 00] locked ptr points to real header on stack 83 // [header | 0 | 01] unlocked regular object header 84 // [ptr | 10] monitor inflated lock (header is wapped out) 85 // [ptr | 11] marked used to mark an object 86 // [0 ............ 0| 00] inflating inflation in progress 87 // 88 // We assume that stack/thread pointers have the lowest two bits cleared. 89 // 90 // - INFLATING() is a distinguished markword value of all zeros that is 91 // used when inflating an existing stack-lock into an ObjectMonitor. 92 // See below for is_being_inflated() and INFLATING(). 93 94 class BasicLock; 95 class ObjectMonitor; 96 class JavaThread; 97 class Klass; 98 class outputStream; 99 100 class markWord { 101 private: 102 uintptr_t _value; 103 104 public: 105 explicit markWord(uintptr_t value) : _value(value) {} 106 107 markWord() { /* uninitialized */} 108 109 // It is critical for performance that this class be trivially 110 // destructable, copyable, and assignable. 111 112 static markWord from_pointer(void* ptr) { 113 return markWord((uintptr_t)ptr); 114 } 115 void* to_pointer() const { 116 return (void*)_value; 117 } 118 119 bool operator==(const markWord& other) const { 120 return _value == other._value; 121 } 122 bool operator!=(const markWord& other) const { 123 return !operator==(other); 124 } 125 126 // Conversion 127 uintptr_t value() const { return _value; } 128 129 // Constants 130 static const int age_bits = 4; 131 static const int lock_bits = 2; 132 static const int biased_lock_bits = 1; 133 static const int self_forwarded_bits = 1; 134 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - self_forwarded_bits; 135 static const int hash_bits = max_hash_bits > 25 ? 25 : max_hash_bits; 136 #ifdef _LP64 137 static const int klass_bits = 32; 138 #endif 139 static const int epoch_bits = 2; 140 141 // The biased locking code currently requires that the age bits be 142 // contiguous to the lock bits. 143 static const int lock_shift = 0; 144 static const int biased_lock_shift = lock_bits; 145 static const int self_forwarded_shift = lock_shift + lock_bits; 146 static const int age_shift = self_forwarded_shift + self_forwarded_bits; 147 static const int hash_shift = age_shift + age_bits; 148 #ifdef _LP64 149 static const int klass_shift = hash_shift + hash_bits; 150 #endif 151 static const int epoch_shift = hash_shift; 152 153 static const uintptr_t lock_mask = right_n_bits(lock_bits); 154 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift; 155 static const uintptr_t biased_lock_mask = right_n_bits(lock_bits + biased_lock_bits); 156 static const uintptr_t biased_lock_mask_in_place= biased_lock_mask << lock_shift; 157 static const uintptr_t biased_lock_bit_in_place = 1 << biased_lock_shift; 158 static const uintptr_t self_forwarded_mask = right_n_bits(self_forwarded_bits); 159 static const uintptr_t self_forwarded_mask_in_place = self_forwarded_mask << self_forwarded_shift; 160 static const uintptr_t age_mask = right_n_bits(age_bits); 161 static const uintptr_t age_mask_in_place = age_mask << age_shift; 162 static const uintptr_t epoch_mask = right_n_bits(epoch_bits); 163 static const uintptr_t epoch_mask_in_place = epoch_mask << epoch_shift; 164 165 static const uintptr_t hash_mask = right_n_bits(hash_bits); 166 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift; 167 168 #ifdef _LP64 169 static const uintptr_t klass_mask = right_n_bits(klass_bits); 170 static const uintptr_t klass_mask_in_place = klass_mask << klass_shift; 171 #endif 172 173 // Alignment of JavaThread pointers encoded in object header required by biased locking 174 static const size_t biased_lock_alignment = 2 << (epoch_shift + epoch_bits); 175 176 static const uintptr_t locked_value = 0; 177 static const uintptr_t unlocked_value = 1; 178 static const uintptr_t monitor_value = 2; 179 static const uintptr_t marked_value = 3; 180 static const uintptr_t biased_lock_pattern = 5; 181 182 static const uintptr_t no_hash = 0 ; // no hash value assigned 183 static const uintptr_t no_hash_in_place = (address_word)no_hash << hash_shift; 184 static const uintptr_t no_lock_in_place = unlocked_value; 185 186 static const uint max_age = age_mask; 187 188 static const int max_bias_epoch = epoch_mask; 189 190 // Creates a markWord with all bits set to zero. 191 static markWord zero() { return markWord(uintptr_t(0)); } 192 193 // Biased Locking accessors. 194 // These must be checked by all code which calls into the 195 // ObjectSynchronizer and other code. The biasing is not understood 196 // by the lower-level CAS-based locking code, although the runtime 197 // fixes up biased locks to be compatible with it when a bias is 198 // revoked. 199 bool has_bias_pattern() const { 200 return (mask_bits(value(), biased_lock_mask_in_place) == biased_lock_pattern); 201 } 202 JavaThread* biased_locker() const { 203 assert(has_bias_pattern(), "should not call this otherwise"); 204 return (JavaThread*) mask_bits(value(), ~(biased_lock_mask_in_place | age_mask_in_place | epoch_mask_in_place)); 205 } 206 // Indicates that the mark has the bias bit set but that it has not 207 // yet been biased toward a particular thread 208 bool is_biased_anonymously() const { 209 return (has_bias_pattern() && (biased_locker() == NULL)); 210 } 211 // Indicates epoch in which this bias was acquired. If the epoch 212 // changes due to too many bias revocations occurring, the biases 213 // from the previous epochs are all considered invalid. 214 int bias_epoch() const { 215 assert(has_bias_pattern(), "should not call this otherwise"); 216 return (mask_bits(value(), epoch_mask_in_place) >> epoch_shift); 217 } 218 markWord set_bias_epoch(int epoch) { 219 assert(has_bias_pattern(), "should not call this otherwise"); 220 assert((epoch & (~epoch_mask)) == 0, "epoch overflow"); 221 return markWord(mask_bits(value(), ~epoch_mask_in_place) | (epoch << epoch_shift)); 222 } 223 markWord incr_bias_epoch() { 224 return set_bias_epoch((1 + bias_epoch()) & epoch_mask); 225 } 226 // Prototype mark for initialization 227 static markWord biased_locking_prototype() { 228 return markWord( biased_lock_pattern ); 229 } 230 231 // lock accessors (note that these assume lock_shift == 0) 232 bool is_locked() const { 233 return (mask_bits(value(), lock_mask_in_place) != unlocked_value); 234 } 235 bool is_unlocked() const { 236 return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value); 237 } 238 bool is_marked() const { 239 return (mask_bits(value(), lock_mask_in_place) == marked_value); 240 } 241 bool is_neutral() const { return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value); } 242 243 // Special temporary state of the markWord while being inflated. 244 // Code that looks at mark outside a lock need to take this into account. 245 bool is_being_inflated() const { return (value() == 0); } 246 247 // Distinguished markword value - used when inflating over 248 // an existing stack-lock. 0 indicates the markword is "BUSY". 249 // Lockword mutators that use a LD...CAS idiom should always 250 // check for and avoid overwriting a 0 value installed by some 251 // other thread. (They should spin or block instead. The 0 value 252 // is transient and *should* be short-lived). 253 static markWord INFLATING() { return zero(); } // inflate-in-progress 254 255 // Should this header be preserved during GC? 256 inline bool must_be_preserved(const oopDesc* obj) const; 257 258 // Should this header (including its age bits) be preserved in the 259 // case of a promotion failure during scavenge? 260 // Note that we special case this situation. We want to avoid 261 // calling BiasedLocking::preserve_marks()/restore_marks() (which 262 // decrease the number of mark words that need to be preserved 263 // during GC) during each scavenge. During scavenges in which there 264 // is no promotion failure, we actually don't need to call the above 265 // routines at all, since we don't mutate and re-initialize the 266 // marks of promoted objects using init_mark(). However, during 267 // scavenges which result in promotion failure, we do re-initialize 268 // the mark words of objects, meaning that we should have called 269 // these mark word preservation routines. Currently there's no good 270 // place in which to call them in any of the scavengers (although 271 // guarded by appropriate locks we could make one), but the 272 // observation is that promotion failures are quite rare and 273 // reducing the number of mark words preserved during them isn't a 274 // high priority. 275 inline bool must_be_preserved_for_promotion_failure(const oopDesc* obj) const; 276 277 // WARNING: The following routines are used EXCLUSIVELY by 278 // synchronization functions. They are not really gc safe. 279 // They must get updated if markWord layout get changed. 280 markWord set_unlocked() const { 281 return markWord(value() | unlocked_value); 282 } 283 bool has_locker() const { 284 return !UseFastLocking && ((value() & lock_mask_in_place) == locked_value); 285 } 286 BasicLock* locker() const { 287 assert(has_locker(), "check"); 288 return (BasicLock*) value(); 289 } 290 291 bool is_fast_locked() const { 292 return UseFastLocking && ((value() & lock_mask_in_place) == locked_value); 293 } 294 markWord set_fast_locked() const { 295 return markWord(value() & ~lock_mask_in_place); 296 } 297 298 bool has_monitor() const { 299 return ((value() & monitor_value) != 0); 300 } 301 ObjectMonitor* monitor() const { 302 assert(has_monitor(), "check"); 303 // Use xor instead of &~ to provide one extra tag-bit check. 304 return (ObjectMonitor*) (value() ^ monitor_value); 305 } 306 bool has_displaced_mark_helper() const { 307 intptr_t lockbits = value() & lock_mask_in_place; 308 return UseFastLocking ? lockbits == monitor_value // monitor? 309 : (lockbits & unlocked_value) == 0; // monitor | stack-locked? 310 } 311 markWord displaced_mark_helper() const; 312 void set_displaced_mark_helper(markWord m) const; 313 markWord copy_set_hash(intptr_t hash) const { 314 uintptr_t tmp = value() & (~hash_mask_in_place); 315 tmp |= ((hash & hash_mask) << hash_shift); 316 return markWord(tmp); 317 } 318 // it is only used to be stored into BasicLock as the 319 // indicator that the lock is using heavyweight monitor 320 static markWord unused_mark() { 321 return markWord(marked_value); 322 } 323 // the following two functions create the markWord to be 324 // stored into object header, it encodes monitor info 325 static markWord encode(BasicLock* lock) { 326 return from_pointer(lock); 327 } 328 static markWord encode(ObjectMonitor* monitor) { 329 uintptr_t tmp = (uintptr_t) monitor; 330 return markWord(tmp | monitor_value); 331 } 332 static markWord encode(JavaThread* thread, uint age, int bias_epoch) { 333 uintptr_t tmp = (uintptr_t) thread; 334 assert(UseBiasedLocking && ((tmp & (epoch_mask_in_place | age_mask_in_place | biased_lock_mask_in_place)) == 0), "misaligned JavaThread pointer"); 335 assert(age <= max_age, "age too large"); 336 assert(bias_epoch <= max_bias_epoch, "bias epoch too large"); 337 return markWord(tmp | (bias_epoch << epoch_shift) | (age << age_shift) | biased_lock_pattern); 338 } 339 340 // used to encode pointers during GC 341 markWord clear_lock_bits() { return markWord(value() & ~lock_mask_in_place); } 342 343 // age operations 344 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); } 345 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); } 346 347 uint age() const { return mask_bits(value() >> age_shift, age_mask); } 348 markWord set_age(uint v) const { 349 assert((v & ~age_mask) == 0, "shouldn't overflow age field"); 350 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift)); 351 } 352 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); } 353 354 // hash operations 355 intptr_t hash() const { 356 return mask_bits(value() >> hash_shift, hash_mask); 357 } 358 359 bool has_no_hash() const { 360 return hash() == no_hash; 361 } 362 363 #ifdef _LP64 364 inline Klass* klass() const; 365 inline Klass* klass_or_null() const; 366 inline Klass* safe_klass() const; 367 inline markWord set_klass(const Klass* klass) const; 368 inline narrowKlass narrow_klass() const; 369 inline markWord set_narrow_klass(const narrowKlass klass) const; 370 #endif 371 372 // Prototype mark for initialization 373 static markWord prototype() { 374 return markWord( no_hash_in_place | no_lock_in_place ); 375 } 376 377 // Helper function for restoration of unmarked mark oops during GC 378 static inline markWord prototype_for_klass(const Klass* klass); 379 380 // Debugging 381 void print_on(outputStream* st, bool print_monitor_info = true) const; 382 383 // Prepare address of oop for placement into mark 384 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); } 385 386 // Recover address of oop from encoded form used in mark 387 inline void* decode_pointer() { if (UseBiasedLocking && has_bias_pattern()) return NULL; return (void*)clear_lock_bits().value(); } 388 389 inline bool self_forwarded() const { 390 return mask_bits(value(), self_forwarded_mask_in_place) != 0; 391 } 392 393 inline markWord set_self_forwarded() const { 394 return markWord(value() | self_forwarded_mask_in_place | marked_value); 395 } 396 }; 397 398 // Support atomic operations. 399 template<> 400 struct PrimitiveConversions::Translate<markWord> : public TrueType { 401 typedef markWord Value; 402 typedef uintptr_t Decayed; 403 404 static Decayed decay(const Value& x) { return x.value(); } 405 static Value recover(Decayed x) { return Value(x); } 406 }; 407 408 #endif // SHARE_OOPS_MARKWORD_HPP