src/share/vm/oops/markOop.hpp

Thu, 13 Mar 2008 14:17:48 -0700

author
dcubed
date
Thu, 13 Mar 2008 14:17:48 -0700
changeset 487
75b0f3cb1943
parent 483
d8b3ef7ee3e5
child 548
ba764ed4b6f2
permissions
-rw-r--r--

Merge

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

mercurial