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