annotate src/share/vm/oops/markOop.hpp @ 2560:9b0ca45cd756

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