1 /* 2 * Copyright (c) 2001, 2021, 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 package sun.jvm.hotspot.oops; 26 27 import java.io.*; 28 import java.util.*; 29 30 import sun.jvm.hotspot.debugger.*; 31 import sun.jvm.hotspot.runtime.*; 32 import sun.jvm.hotspot.types.*; 33 import sun.jvm.hotspot.utilities.*; 34 import sun.jvm.hotspot.utilities.Observable; 35 import sun.jvm.hotspot.utilities.Observer; 36 37 public class Mark extends VMObject { 38 static { 39 VM.registerVMInitializedObserver(new Observer() { 40 public void update(Observable o, Object data) { 41 initialize(VM.getVM().getTypeDataBase()); 42 } 43 }); 44 } 45 46 private static synchronized void initialize(TypeDataBase db) throws WrongTypeException { 47 Type type = db.lookupType("oopDesc"); 48 markField = type.getCIntegerField("_mark"); 49 50 ageBits = db.lookupLongConstant("markWord::age_bits").longValue(); 51 lockBits = db.lookupLongConstant("markWord::lock_bits").longValue(); 52 maxHashBits = db.lookupLongConstant("markWord::max_hash_bits").longValue(); 53 hashBits = db.lookupLongConstant("markWord::hash_bits").longValue(); 54 lockShift = db.lookupLongConstant("markWord::lock_shift").longValue(); 55 ageShift = db.lookupLongConstant("markWord::age_shift").longValue(); 56 hashShift = db.lookupLongConstant("markWord::hash_shift").longValue(); 57 lockMask = db.lookupLongConstant("markWord::lock_mask").longValue(); 58 lockMaskInPlace = db.lookupLongConstant("markWord::lock_mask_in_place").longValue(); 59 ageMask = db.lookupLongConstant("markWord::age_mask").longValue(); 60 ageMaskInPlace = db.lookupLongConstant("markWord::age_mask_in_place").longValue(); 61 hashMask = db.lookupLongConstant("markWord::hash_mask").longValue(); 62 hashMaskInPlace = db.lookupLongConstant("markWord::hash_mask_in_place").longValue(); 63 lockedValue = db.lookupLongConstant("markWord::locked_value").longValue(); 64 unlockedValue = db.lookupLongConstant("markWord::unlocked_value").longValue(); 65 monitorValue = db.lookupLongConstant("markWord::monitor_value").longValue(); 66 markedValue = db.lookupLongConstant("markWord::marked_value").longValue(); 67 noHash = db.lookupLongConstant("markWord::no_hash").longValue(); 68 noHashInPlace = db.lookupLongConstant("markWord::no_hash_in_place").longValue(); 69 noLockInPlace = db.lookupLongConstant("markWord::no_lock_in_place").longValue(); 70 maxAge = db.lookupLongConstant("markWord::max_age").longValue(); 71 } 72 73 // Field accessors 74 private static CIntegerField markField; 75 76 // Constants -- read from VM 77 private static long ageBits; 78 private static long lockBits; 79 private static long maxHashBits; 80 private static long hashBits; 81 82 private static long lockShift; 83 private static long ageShift; 84 private static long hashShift; 85 86 private static long lockMask; 87 private static long lockMaskInPlace; 88 private static long ageMask; 89 private static long ageMaskInPlace; 90 private static long hashMask; 91 private static long hashMaskInPlace; 92 93 private static long lockedValue; 94 private static long unlockedValue; 95 private static long monitorValue; 96 private static long markedValue; 97 98 private static long noHash; 99 100 private static long noHashInPlace; 101 private static long noLockInPlace; 102 103 private static long maxAge; 104 105 /* Constants in markWord used by CMS. */ 106 private static long cmsShift; 107 private static long cmsMask; 108 private static long sizeShift; 109 110 public Mark(Address addr) { 111 super(addr); 112 } 113 114 public long value() { 115 return markField.getValue(addr); 116 } 117 118 public Address valueAsAddress() { 119 return addr.getAddressAt(markField.getOffset()); 120 } 121 122 // lock accessors (note that these assume lock_shift == 0) 123 public boolean isLocked() { 124 return (Bits.maskBitsLong(value(), lockMaskInPlace) != unlockedValue); 125 } 126 public boolean isUnlocked() { 127 return (Bits.maskBitsLong(value(), lockMaskInPlace) == unlockedValue); 128 } 129 public boolean isMarked() { 130 return (Bits.maskBitsLong(value(), lockMaskInPlace) == markedValue); 131 } 132 133 // Special temporary state of the markWord while being inflated. 134 // Code that looks at mark outside a lock need to take this into account. 135 public boolean isBeingInflated() { 136 return (value() == 0); 137 } 138 139 // Should this header be preserved during GC? 140 public boolean mustBePreserved() { 141 return (!isUnlocked() || !hasNoHash()); 142 } 143 144 // WARNING: The following routines are used EXCLUSIVELY by 145 // synchronization functions. They are not really gc safe. 146 // They must get updated if markWord layout get changed. 147 148 public boolean hasLocker() { 149 return ((value() & lockMaskInPlace) == lockedValue); 150 } 151 public BasicLock locker() { 152 if (Assert.ASSERTS_ENABLED) { 153 Assert.that(hasLocker(), "check"); 154 } 155 return new BasicLock(valueAsAddress()); 156 } 157 public boolean hasMonitor() { 158 return ((value() & monitorValue) != 0); 159 } 160 public ObjectMonitor monitor() { 161 if (Assert.ASSERTS_ENABLED) { 162 Assert.that(hasMonitor(), "check"); 163 } 164 // Use xor instead of &~ to provide one extra tag-bit check. 165 Address monAddr = valueAsAddress().xorWithMask(monitorValue); 166 return new ObjectMonitor(monAddr); 167 } 168 public boolean hasDisplacedMarkHelper() { 169 return ((value() & unlockedValue) == 0); 170 } 171 public Mark displacedMarkHelper() { 172 if (Assert.ASSERTS_ENABLED) { 173 Assert.that(hasDisplacedMarkHelper(), "check"); 174 } 175 Address addr = valueAsAddress().andWithMask(~monitorValue); 176 return new Mark(addr.getAddressAt(0)); 177 } 178 public int age() { return (int) Bits.maskBitsLong(value() >> ageShift, ageMask); } 179 180 // hash operations 181 public long hash() { 182 return Bits.maskBitsLong(value() >> hashShift, hashMask); 183 } 184 185 public boolean hasNoHash() { 186 return hash() == noHash; 187 } 188 189 // Debugging 190 public void printOn(PrintStream tty) { 191 if (isLocked()) { 192 tty.print("locked(0x" + 193 Long.toHexString(value()) + ")->"); 194 displacedMarkHelper().printOn(tty); 195 } else { 196 if (Assert.ASSERTS_ENABLED) { 197 Assert.that(isUnlocked(), "just checking"); 198 } 199 tty.print("mark("); 200 tty.print("hash " + Long.toHexString(hash()) + ","); 201 tty.print("age " + age() + ")"); 202 } 203 } 204 205 public long getSize() { return (long)(value() >> sizeShift); } 206 }