1 /* 2 * Copyright (c) 2001, 2023, 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 if (VM.getVM().isLP64()) { 58 klassShift = db.lookupLongConstant("markWord::klass_shift").longValue(); 59 } 60 lockMask = db.lookupLongConstant("markWord::lock_mask").longValue(); 61 lockMaskInPlace = db.lookupLongConstant("markWord::lock_mask_in_place").longValue(); 62 ageMask = db.lookupLongConstant("markWord::age_mask").longValue(); 63 ageMaskInPlace = db.lookupLongConstant("markWord::age_mask_in_place").longValue(); 64 hashMask = db.lookupLongConstant("markWord::hash_mask").longValue(); 65 hashMaskInPlace = db.lookupLongConstant("markWord::hash_mask_in_place").longValue(); 66 lockedValue = db.lookupLongConstant("markWord::locked_value").longValue(); 67 unlockedValue = db.lookupLongConstant("markWord::unlocked_value").longValue(); 68 monitorValue = db.lookupLongConstant("markWord::monitor_value").longValue(); 69 markedValue = db.lookupLongConstant("markWord::marked_value").longValue(); 70 noHash = db.lookupLongConstant("markWord::no_hash").longValue(); 71 noHashInPlace = db.lookupLongConstant("markWord::no_hash_in_place").longValue(); 72 noLockInPlace = db.lookupLongConstant("markWord::no_lock_in_place").longValue(); 73 maxAge = db.lookupLongConstant("markWord::max_age").longValue(); 74 } 75 76 // Field accessors 77 private static CIntegerField markField; 78 79 // Constants -- read from VM 80 private static long ageBits; 81 private static long lockBits; 82 private static long maxHashBits; 83 private static long hashBits; 84 85 private static long lockShift; 86 private static long ageShift; 87 private static long hashShift; 88 private static long klassShift; 89 90 private static long lockMask; 91 private static long lockMaskInPlace; 92 private static long ageMask; 93 private static long ageMaskInPlace; 94 private static long hashMask; 95 private static long hashMaskInPlace; 96 97 private static long lockedValue; 98 private static long unlockedValue; 99 private static long monitorValue; 100 private static long markedValue; 101 102 private static long noHash; 103 104 private static long noHashInPlace; 105 private static long noLockInPlace; 106 107 private static long maxAge; 108 109 public static long getKlassShift() { 110 return klassShift; 111 } 112 113 public Mark(Address addr) { 114 super(addr); 115 } 116 117 public long value() { 118 return markField.getValue(addr); 119 } 120 121 public Address valueAsAddress() { 122 return addr.getAddressAt(markField.getOffset()); 123 } 124 125 // lock accessors (note that these assume lock_shift == 0) 126 public boolean isLocked() { 127 return (Bits.maskBitsLong(value(), lockMaskInPlace) != unlockedValue); 128 } 129 public boolean isUnlocked() { 130 return (Bits.maskBitsLong(value(), lockMaskInPlace) == unlockedValue); 131 } 132 public boolean isMarked() { 133 return (Bits.maskBitsLong(value(), lockMaskInPlace) == markedValue); 134 } 135 136 // Special temporary state of the markWord while being inflated. 137 // Code that looks at mark outside a lock need to take this into account. 138 public boolean isBeingInflated() { 139 return (value() == 0); 140 } 141 142 // Should this header be preserved during GC? 143 public boolean mustBePreserved() { 144 return (!isUnlocked() || !hasNoHash()); 145 } 146 147 // WARNING: The following routines are used EXCLUSIVELY by 148 // synchronization functions. They are not really gc safe. 149 // They must get updated if markWord layout get changed. 150 151 public boolean hasLocker() { 152 return ((value() & lockMaskInPlace) == lockedValue); 153 } 154 public BasicLock locker() { 155 if (Assert.ASSERTS_ENABLED) { 156 Assert.that(hasLocker(), "check"); 157 } 158 return new BasicLock(valueAsAddress()); 159 } 160 public boolean hasMonitor() { 161 return ((value() & monitorValue) != 0); 162 } 163 public ObjectMonitor monitor() { 164 if (Assert.ASSERTS_ENABLED) { 165 Assert.that(hasMonitor(), "check"); 166 } 167 if (VM.getVM().getCommandLineFlag("UseObjectMonitorTable").getBool()) { 168 Iterator it = ObjectSynchronizer.objectMonitorIterator(); 169 while (it != null && it.hasNext()) { 170 ObjectMonitor mon = (ObjectMonitor)it.next(); 171 if (getAddress().equals(mon.object())) { 172 return mon; 173 } 174 } 175 return null; 176 } 177 // Use xor instead of &~ to provide one extra tag-bit check. 178 Address monAddr = valueAsAddress().xorWithMask(monitorValue); 179 return new ObjectMonitor(monAddr); 180 } 181 public boolean hasDisplacedMarkHelper() { 182 return ((value() & unlockedValue) == 0); 183 } 184 public Mark displacedMarkHelper() { 185 if (Assert.ASSERTS_ENABLED) { 186 Assert.that(hasDisplacedMarkHelper(), "check"); 187 } 188 Address addr = valueAsAddress().andWithMask(~monitorValue); 189 return new Mark(addr.getAddressAt(0)); 190 } 191 public int age() { return (int) Bits.maskBitsLong(value() >> ageShift, ageMask); } 192 193 // hash operations 194 public long hash() { 195 return Bits.maskBitsLong(value() >> hashShift, hashMask); 196 } 197 198 public boolean hasNoHash() { 199 return hash() == noHash; 200 } 201 202 public Klass getKlass() { 203 assert(VM.getVM().isCompactObjectHeadersEnabled()); 204 return (Klass)Metadata.instantiateWrapperFor(addr.getCompKlassAddressAt(0)); 205 } 206 207 // Debugging 208 public void printOn(PrintStream tty) { 209 if (isLocked()) { 210 tty.print("locked(0x" + 211 Long.toHexString(value()) + ")->"); 212 displacedMarkHelper().printOn(tty); 213 } else { 214 if (Assert.ASSERTS_ENABLED) { 215 Assert.that(isUnlocked(), "just checking"); 216 } 217 tty.print("mark("); 218 tty.print("hash " + Long.toHexString(hash()) + ","); 219 tty.print("age " + age() + ")"); 220 } 221 } 222 223 public long getSize() { return (long)value(); } 224 }