aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_OOPS_MARKOOP_HPP aoqi@0: #define SHARE_VM_OOPS_MARKOOP_HPP aoqi@0: aoqi@0: #include "oops/oop.hpp" aoqi@0: aoqi@0: // The markOop describes the header of an object. aoqi@0: // aoqi@0: // Note that the mark is not a real oop but just a word. aoqi@0: // It is placed in the oop hierarchy for historical reasons. aoqi@0: // aoqi@0: // Bit-format of an object header (most significant first, big endian layout below): aoqi@0: // aoqi@0: // 32 bits: aoqi@0: // -------- aoqi@0: // hash:25 ------------>| age:4 biased_lock:1 lock:2 (normal object) aoqi@0: // JavaThread*:23 epoch:2 age:4 biased_lock:1 lock:2 (biased object) aoqi@0: // size:32 ------------------------------------------>| (CMS free block) aoqi@0: // PromotedObject*:29 ---------->| promo_bits:3 ----->| (CMS promoted object) aoqi@0: // aoqi@0: // 64 bits: aoqi@0: // -------- aoqi@0: // unused:25 hash:31 -->| unused:1 age:4 biased_lock:1 lock:2 (normal object) aoqi@0: // JavaThread*:54 epoch:2 unused:1 age:4 biased_lock:1 lock:2 (biased object) aoqi@0: // PromotedObject*:61 --------------------->| promo_bits:3 ----->| (CMS promoted object) aoqi@0: // size:64 ----------------------------------------------------->| (CMS free block) aoqi@0: // aoqi@0: // unused:25 hash:31 -->| cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && normal object) aoqi@0: // JavaThread*:54 epoch:2 cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && biased object) aoqi@0: // narrowOop:32 unused:24 cms_free:1 unused:4 promo_bits:3 ----->| (COOPs && CMS promoted object) aoqi@0: // unused:21 size:35 -->| cms_free:1 unused:7 ------------------>| (COOPs && CMS free block) aoqi@0: // aoqi@0: // - hash contains the identity hash value: largest value is aoqi@0: // 31 bits, see os::random(). Also, 64-bit vm's require aoqi@0: // a hash value no bigger than 32 bits because they will not aoqi@0: // properly generate a mask larger than that: see library_call.cpp aoqi@0: // and c1_CodePatterns_sparc.cpp. aoqi@0: // aoqi@0: // - the biased lock pattern is used to bias a lock toward a given aoqi@0: // thread. When this pattern is set in the low three bits, the lock aoqi@0: // is either biased toward a given thread or "anonymously" biased, aoqi@0: // indicating that it is possible for it to be biased. When the aoqi@0: // lock is biased toward a given thread, locking and unlocking can aoqi@0: // be performed by that thread without using atomic operations. aoqi@0: // When a lock's bias is revoked, it reverts back to the normal aoqi@0: // locking scheme described below. aoqi@0: // aoqi@0: // Note that we are overloading the meaning of the "unlocked" state aoqi@0: // of the header. Because we steal a bit from the age we can aoqi@0: // guarantee that the bias pattern will never be seen for a truly aoqi@0: // unlocked object. aoqi@0: // aoqi@0: // Note also that the biased state contains the age bits normally aoqi@0: // contained in the object header. Large increases in scavenge aoqi@0: // times were seen when these bits were absent and an arbitrary age aoqi@0: // assigned to all biased objects, because they tended to consume a aoqi@0: // significant fraction of the eden semispaces and were not aoqi@0: // promoted promptly, causing an increase in the amount of copying aoqi@0: // performed. The runtime system aligns all JavaThread* pointers to aoqi@0: // a very large value (currently 128 bytes (32bVM) or 256 bytes (64bVM)) aoqi@0: // to make room for the age bits & the epoch bits (used in support of aoqi@0: // biased locking), and for the CMS "freeness" bit in the 64bVM (+COOPs). aoqi@0: // aoqi@0: // [JavaThread* | epoch | age | 1 | 01] lock is biased toward given thread aoqi@0: // [0 | epoch | age | 1 | 01] lock is anonymously biased aoqi@0: // aoqi@0: // - the two lock bits are used to describe three states: locked/unlocked and monitor. aoqi@0: // aoqi@0: // [ptr | 00] locked ptr points to real header on stack aoqi@0: // [header | 0 | 01] unlocked regular object header aoqi@0: // [ptr | 10] monitor inflated lock (header is wapped out) aoqi@0: // [ptr | 11] marked used by markSweep to mark an object aoqi@0: // not valid at any other time aoqi@0: // aoqi@0: // We assume that stack/thread pointers have the lowest two bits cleared. aoqi@0: aoqi@0: class BasicLock; aoqi@0: class ObjectMonitor; aoqi@0: class JavaThread; aoqi@0: aoqi@0: class markOopDesc: public oopDesc { aoqi@0: private: aoqi@0: // Conversion aoqi@0: uintptr_t value() const { return (uintptr_t) this; } aoqi@0: aoqi@0: public: aoqi@0: // Constants aoqi@0: enum { age_bits = 4, aoqi@0: lock_bits = 2, aoqi@0: biased_lock_bits = 1, aoqi@0: max_hash_bits = BitsPerWord - age_bits - lock_bits - biased_lock_bits, aoqi@0: hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits, aoqi@0: cms_bits = LP64_ONLY(1) NOT_LP64(0), aoqi@0: epoch_bits = 2 aoqi@0: }; aoqi@0: aoqi@0: // The biased locking code currently requires that the age bits be aoqi@0: // contiguous to the lock bits. aoqi@0: enum { lock_shift = 0, aoqi@0: biased_lock_shift = lock_bits, aoqi@0: age_shift = lock_bits + biased_lock_bits, aoqi@0: cms_shift = age_shift + age_bits, aoqi@0: hash_shift = cms_shift + cms_bits, aoqi@0: epoch_shift = hash_shift aoqi@0: }; aoqi@0: aoqi@0: enum { lock_mask = right_n_bits(lock_bits), aoqi@0: lock_mask_in_place = lock_mask << lock_shift, aoqi@0: biased_lock_mask = right_n_bits(lock_bits + biased_lock_bits), aoqi@0: biased_lock_mask_in_place= biased_lock_mask << lock_shift, aoqi@0: biased_lock_bit_in_place = 1 << biased_lock_shift, aoqi@0: age_mask = right_n_bits(age_bits), aoqi@0: age_mask_in_place = age_mask << age_shift, aoqi@0: epoch_mask = right_n_bits(epoch_bits), aoqi@0: epoch_mask_in_place = epoch_mask << epoch_shift, aoqi@0: cms_mask = right_n_bits(cms_bits), aoqi@0: cms_mask_in_place = cms_mask << cms_shift aoqi@0: #ifndef _WIN64 aoqi@0: ,hash_mask = right_n_bits(hash_bits), aoqi@0: hash_mask_in_place = (address_word)hash_mask << hash_shift aoqi@0: #endif aoqi@0: }; aoqi@0: aoqi@0: // Alignment of JavaThread pointers encoded in object header required by biased locking aoqi@0: enum { biased_lock_alignment = 2 << (epoch_shift + epoch_bits) aoqi@0: }; aoqi@0: aoqi@0: #ifdef _WIN64 aoqi@0: // These values are too big for Win64 aoqi@0: const static uintptr_t hash_mask = right_n_bits(hash_bits); aoqi@0: const static uintptr_t hash_mask_in_place = aoqi@0: (address_word)hash_mask << hash_shift; aoqi@0: #endif aoqi@0: aoqi@0: enum { locked_value = 0, aoqi@0: unlocked_value = 1, aoqi@0: monitor_value = 2, aoqi@0: marked_value = 3, aoqi@0: biased_lock_pattern = 5 aoqi@0: }; aoqi@0: aoqi@0: enum { no_hash = 0 }; // no hash value assigned aoqi@0: aoqi@0: enum { no_hash_in_place = (address_word)no_hash << hash_shift, aoqi@0: no_lock_in_place = unlocked_value aoqi@0: }; aoqi@0: aoqi@0: enum { max_age = age_mask }; aoqi@0: aoqi@0: enum { max_bias_epoch = epoch_mask }; aoqi@0: aoqi@0: // Biased Locking accessors. aoqi@0: // These must be checked by all code which calls into the aoqi@0: // ObjectSynchronizer and other code. The biasing is not understood aoqi@0: // by the lower-level CAS-based locking code, although the runtime aoqi@0: // fixes up biased locks to be compatible with it when a bias is aoqi@0: // revoked. aoqi@0: bool has_bias_pattern() const { aoqi@0: return (mask_bits(value(), biased_lock_mask_in_place) == biased_lock_pattern); aoqi@0: } aoqi@0: JavaThread* biased_locker() const { aoqi@0: assert(has_bias_pattern(), "should not call this otherwise"); aoqi@0: return (JavaThread*) ((intptr_t) (mask_bits(value(), ~(biased_lock_mask_in_place | age_mask_in_place | epoch_mask_in_place)))); aoqi@0: } aoqi@0: // Indicates that the mark has the bias bit set but that it has not aoqi@0: // yet been biased toward a particular thread aoqi@0: bool is_biased_anonymously() const { aoqi@0: return (has_bias_pattern() && (biased_locker() == NULL)); aoqi@0: } aoqi@0: // Indicates epoch in which this bias was acquired. If the epoch aoqi@0: // changes due to too many bias revocations occurring, the biases aoqi@0: // from the previous epochs are all considered invalid. aoqi@0: int bias_epoch() const { aoqi@0: assert(has_bias_pattern(), "should not call this otherwise"); aoqi@0: return (mask_bits(value(), epoch_mask_in_place) >> epoch_shift); aoqi@0: } aoqi@0: markOop set_bias_epoch(int epoch) { aoqi@0: assert(has_bias_pattern(), "should not call this otherwise"); aoqi@0: assert((epoch & (~epoch_mask)) == 0, "epoch overflow"); aoqi@0: return markOop(mask_bits(value(), ~epoch_mask_in_place) | (epoch << epoch_shift)); aoqi@0: } aoqi@0: markOop incr_bias_epoch() { aoqi@0: return set_bias_epoch((1 + bias_epoch()) & epoch_mask); aoqi@0: } aoqi@0: // Prototype mark for initialization aoqi@0: static markOop biased_locking_prototype() { aoqi@0: return markOop( biased_lock_pattern ); aoqi@0: } aoqi@0: aoqi@0: // lock accessors (note that these assume lock_shift == 0) aoqi@0: bool is_locked() const { aoqi@0: return (mask_bits(value(), lock_mask_in_place) != unlocked_value); aoqi@0: } aoqi@0: bool is_unlocked() const { aoqi@0: return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value); aoqi@0: } aoqi@0: bool is_marked() const { aoqi@0: return (mask_bits(value(), lock_mask_in_place) == marked_value); aoqi@0: } aoqi@0: bool is_neutral() const { return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value); } aoqi@0: aoqi@0: // Special temporary state of the markOop while being inflated. aoqi@0: // Code that looks at mark outside a lock need to take this into account. aoqi@0: bool is_being_inflated() const { return (value() == 0); } aoqi@0: aoqi@0: // Distinguished markword value - used when inflating over aoqi@0: // an existing stacklock. 0 indicates the markword is "BUSY". aoqi@0: // Lockword mutators that use a LD...CAS idiom should always aoqi@0: // check for and avoid overwriting a 0 value installed by some aoqi@0: // other thread. (They should spin or block instead. The 0 value aoqi@0: // is transient and *should* be short-lived). aoqi@0: static markOop INFLATING() { return (markOop) 0; } // inflate-in-progress aoqi@0: aoqi@0: // Should this header be preserved during GC? aoqi@0: inline bool must_be_preserved(oop obj_containing_mark) const; aoqi@0: inline bool must_be_preserved_with_bias(oop obj_containing_mark) const; aoqi@0: aoqi@0: // Should this header (including its age bits) be preserved in the aoqi@0: // case of a promotion failure during scavenge? aoqi@0: // Note that we special case this situation. We want to avoid aoqi@0: // calling BiasedLocking::preserve_marks()/restore_marks() (which aoqi@0: // decrease the number of mark words that need to be preserved aoqi@0: // during GC) during each scavenge. During scavenges in which there aoqi@0: // is no promotion failure, we actually don't need to call the above aoqi@0: // routines at all, since we don't mutate and re-initialize the aoqi@0: // marks of promoted objects using init_mark(). However, during aoqi@0: // scavenges which result in promotion failure, we do re-initialize aoqi@0: // the mark words of objects, meaning that we should have called aoqi@0: // these mark word preservation routines. Currently there's no good aoqi@0: // place in which to call them in any of the scavengers (although aoqi@0: // guarded by appropriate locks we could make one), but the aoqi@0: // observation is that promotion failures are quite rare and aoqi@0: // reducing the number of mark words preserved during them isn't a aoqi@0: // high priority. aoqi@0: inline bool must_be_preserved_for_promotion_failure(oop obj_containing_mark) const; aoqi@0: inline bool must_be_preserved_with_bias_for_promotion_failure(oop obj_containing_mark) const; aoqi@0: aoqi@0: // Should this header be preserved during a scavenge where CMS is aoqi@0: // the old generation? aoqi@0: // (This is basically the same body as must_be_preserved_for_promotion_failure(), aoqi@0: // but takes the Klass* as argument instead) aoqi@0: inline bool must_be_preserved_for_cms_scavenge(Klass* klass_of_obj_containing_mark) const; aoqi@0: inline bool must_be_preserved_with_bias_for_cms_scavenge(Klass* klass_of_obj_containing_mark) const; aoqi@0: aoqi@0: // WARNING: The following routines are used EXCLUSIVELY by aoqi@0: // synchronization functions. They are not really gc safe. aoqi@0: // They must get updated if markOop layout get changed. aoqi@0: markOop set_unlocked() const { aoqi@0: return markOop(value() | unlocked_value); aoqi@0: } aoqi@0: bool has_locker() const { aoqi@0: return ((value() & lock_mask_in_place) == locked_value); aoqi@0: } aoqi@0: BasicLock* locker() const { aoqi@0: assert(has_locker(), "check"); aoqi@0: return (BasicLock*) value(); aoqi@0: } aoqi@0: bool has_monitor() const { aoqi@0: return ((value() & monitor_value) != 0); aoqi@0: } aoqi@0: ObjectMonitor* monitor() const { aoqi@0: assert(has_monitor(), "check"); aoqi@0: // Use xor instead of &~ to provide one extra tag-bit check. aoqi@0: return (ObjectMonitor*) (value() ^ monitor_value); aoqi@0: } aoqi@0: bool has_displaced_mark_helper() const { aoqi@0: return ((value() & unlocked_value) == 0); aoqi@0: } aoqi@0: markOop displaced_mark_helper() const { aoqi@0: assert(has_displaced_mark_helper(), "check"); aoqi@0: intptr_t ptr = (value() & ~monitor_value); aoqi@0: return *(markOop*)ptr; aoqi@0: } aoqi@0: void set_displaced_mark_helper(markOop m) const { aoqi@0: assert(has_displaced_mark_helper(), "check"); aoqi@0: intptr_t ptr = (value() & ~monitor_value); aoqi@0: *(markOop*)ptr = m; aoqi@0: } aoqi@0: markOop copy_set_hash(intptr_t hash) const { aoqi@0: intptr_t tmp = value() & (~hash_mask_in_place); aoqi@0: tmp |= ((hash & hash_mask) << hash_shift); aoqi@0: return (markOop)tmp; aoqi@0: } aoqi@0: // it is only used to be stored into BasicLock as the aoqi@0: // indicator that the lock is using heavyweight monitor aoqi@0: static markOop unused_mark() { aoqi@0: return (markOop) marked_value; aoqi@0: } aoqi@0: // the following two functions create the markOop to be aoqi@0: // stored into object header, it encodes monitor info aoqi@0: static markOop encode(BasicLock* lock) { aoqi@0: return (markOop) lock; aoqi@0: } aoqi@0: static markOop encode(ObjectMonitor* monitor) { aoqi@0: intptr_t tmp = (intptr_t) monitor; aoqi@0: return (markOop) (tmp | monitor_value); aoqi@0: } aoqi@0: static markOop encode(JavaThread* thread, uint age, int bias_epoch) { aoqi@0: intptr_t tmp = (intptr_t) thread; aoqi@0: assert(UseBiasedLocking && ((tmp & (epoch_mask_in_place | age_mask_in_place | biased_lock_mask_in_place)) == 0), "misaligned JavaThread pointer"); aoqi@0: assert(age <= max_age, "age too large"); aoqi@0: assert(bias_epoch <= max_bias_epoch, "bias epoch too large"); aoqi@0: return (markOop) (tmp | (bias_epoch << epoch_shift) | (age << age_shift) | biased_lock_pattern); aoqi@0: } aoqi@0: aoqi@0: // used to encode pointers during GC aoqi@0: markOop clear_lock_bits() { return markOop(value() & ~lock_mask_in_place); } aoqi@0: aoqi@0: // age operations aoqi@0: markOop set_marked() { return markOop((value() & ~lock_mask_in_place) | marked_value); } aoqi@0: markOop set_unmarked() { return markOop((value() & ~lock_mask_in_place) | unlocked_value); } aoqi@0: aoqi@0: uint age() const { return mask_bits(value() >> age_shift, age_mask); } aoqi@0: markOop set_age(uint v) const { aoqi@0: assert((v & ~age_mask) == 0, "shouldn't overflow age field"); aoqi@0: return markOop((value() & ~age_mask_in_place) | (((uintptr_t)v & age_mask) << age_shift)); aoqi@0: } aoqi@0: markOop incr_age() const { return age() == max_age ? markOop(this) : set_age(age() + 1); } aoqi@0: aoqi@0: // hash operations aoqi@0: intptr_t hash() const { aoqi@0: return mask_bits(value() >> hash_shift, hash_mask); aoqi@0: } aoqi@0: aoqi@0: bool has_no_hash() const { aoqi@0: return hash() == no_hash; aoqi@0: } aoqi@0: aoqi@0: // Prototype mark for initialization aoqi@0: static markOop prototype() { aoqi@0: return markOop( no_hash_in_place | no_lock_in_place ); aoqi@0: } aoqi@0: aoqi@0: // Helper function for restoration of unmarked mark oops during GC aoqi@0: static inline markOop prototype_for_object(oop obj); aoqi@0: aoqi@0: // Debugging aoqi@0: void print_on(outputStream* st) const; aoqi@0: aoqi@0: // Prepare address of oop for placement into mark aoqi@0: inline static markOop encode_pointer_as_mark(void* p) { return markOop(p)->set_marked(); } aoqi@0: aoqi@0: // Recover address of oop from encoded form used in mark aoqi@0: inline void* decode_pointer() { if (UseBiasedLocking && has_bias_pattern()) return NULL; return clear_lock_bits(); } aoqi@0: aoqi@0: // These markOops indicate cms free chunk blocks and not objects. aoqi@0: // In 64 bit, the markOop is set to distinguish them from oops. aoqi@0: // These are defined in 32 bit mode for vmStructs. aoqi@0: const static uintptr_t cms_free_chunk_pattern = 0x1; aoqi@0: aoqi@0: // Constants for the size field. aoqi@0: enum { size_shift = cms_shift + cms_bits, aoqi@0: size_bits = 35 // need for compressed oops 32G aoqi@0: }; aoqi@0: // These values are too big for Win64 aoqi@0: const static uintptr_t size_mask = LP64_ONLY(right_n_bits(size_bits)) aoqi@0: NOT_LP64(0); aoqi@0: const static uintptr_t size_mask_in_place = aoqi@0: (address_word)size_mask << size_shift; aoqi@0: aoqi@0: #ifdef _LP64 aoqi@0: static markOop cms_free_prototype() { aoqi@0: return markOop(((intptr_t)prototype() & ~cms_mask_in_place) | aoqi@0: ((cms_free_chunk_pattern & cms_mask) << cms_shift)); aoqi@0: } aoqi@0: uintptr_t cms_encoding() const { aoqi@0: return mask_bits(value() >> cms_shift, cms_mask); aoqi@0: } aoqi@0: bool is_cms_free_chunk() const { aoqi@0: return is_neutral() && aoqi@0: (cms_encoding() & cms_free_chunk_pattern) == cms_free_chunk_pattern; aoqi@0: } aoqi@0: aoqi@0: size_t get_size() const { return (size_t)(value() >> size_shift); } aoqi@0: static markOop set_size_and_free(size_t size) { aoqi@0: assert((size & ~size_mask) == 0, "shouldn't overflow size field"); aoqi@0: return markOop(((intptr_t)cms_free_prototype() & ~size_mask_in_place) | aoqi@0: (((intptr_t)size & size_mask) << size_shift)); aoqi@0: } aoqi@0: #endif // _LP64 aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_OOPS_MARKOOP_HPP