1 /* 2 * Copyright (c) 2018, 2019, 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.gc.z; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 import sun.jvm.hotspot.debugger.Address; 31 import sun.jvm.hotspot.debugger.OopHandle; 32 import sun.jvm.hotspot.gc.shared.LiveRegionsProvider; 33 import sun.jvm.hotspot.memory.MemRegion; 34 import sun.jvm.hotspot.oops.Oop; 35 import sun.jvm.hotspot.oops.UnknownOopException; 36 import sun.jvm.hotspot.runtime.VM; 37 import sun.jvm.hotspot.runtime.VMObject; 38 import sun.jvm.hotspot.runtime.VMObjectFactory; 39 import sun.jvm.hotspot.types.AddressField; 40 import sun.jvm.hotspot.types.CIntegerField; 41 import sun.jvm.hotspot.types.Type; 42 import sun.jvm.hotspot.types.TypeDataBase; 43 44 public class ZPage extends VMObject implements LiveRegionsProvider { 45 private static CIntegerField typeField; 46 private static CIntegerField seqnumField; 47 private static long virtualFieldOffset; 48 private static AddressField topField; 49 private static CIntegerField refcountField; 50 private static long forwardingFieldOffset; 51 52 static { 53 VM.registerVMInitializedObserver((o, d) -> initialize(VM.getVM().getTypeDataBase())); 54 } 55 56 static private synchronized void initialize(TypeDataBase db) { 57 Type type = db.lookupType("ZPage"); 58 59 typeField = type.getCIntegerField("_type"); 60 seqnumField = type.getCIntegerField("_seqnum"); 61 virtualFieldOffset = type.getField("_virtual").getOffset(); 62 topField = type.getAddressField("_top"); 63 refcountField = type.getCIntegerField("_refcount"); 64 forwardingFieldOffset = type.getField("_forwarding").getOffset(); 65 } 66 67 public ZPage(Address addr) { 68 super(addr); 69 } 70 71 private byte type() { 72 return typeField.getJByte(addr); 73 } 74 75 private int seqnum() { 76 return seqnumField.getJInt(addr); 77 } 78 79 private ZVirtualMemory virtual() { 80 return VMObjectFactory.newObject(ZVirtualMemory.class, addr.addOffsetTo(virtualFieldOffset)); 81 } 82 83 private Address top() { 84 return topField.getValue(addr); 85 } 86 87 private int refcount() { 88 // refcount is uint32_t so need to be cautious when using this field. 89 return refcountField.getJInt(addr); 90 } 91 92 private ZForwardingTable forwarding() { 93 return VMObjectFactory.newObject(ZForwardingTable.class, addr.addOffsetTo(forwardingFieldOffset)); 94 } 95 96 private boolean is_forwarding() { 97 return forwarding().table() != null; 98 } 99 100 private boolean is_relocatable() { 101 return is_active() && seqnum() < ZGlobals.ZGlobalSeqNum(); 102 } 103 104 private boolean isPageRelocating() { 105 assert(is_active()); 106 // is_forwarding(): Has a (relocation) forwarding table 107 // is_relocatable(): Has not been freed yet 108 return is_forwarding() && is_relocatable(); 109 } 110 111 long start() { 112 return virtual().start(); 113 } 114 115 long size() { 116 return virtual().end() - virtual().start(); 117 } 118 119 Address forward_object(Address from) { 120 // Lookup address in forwarding table 121 long from_offset = ZAddress.offset(from); 122 long from_index = (from_offset - start()) >> object_alignment_shift(); 123 ZForwardingTableEntry entry = forwarding().find(from_index); 124 assert(!entry.is_empty()); 125 assert(entry.from_index() == from_index); 126 127 return ZAddress.good(entry.to_offset()); 128 } 129 130 Address relocate_object(Address from) { 131 // Lookup address in forwarding table 132 long from_offset = ZAddress.offset(from); 133 long from_index = (from_offset - start()) >> object_alignment_shift(); 134 ZForwardingTableEntry entry = forwarding().find(from_index); 135 if (!entry.is_empty() && entry.from_index() == from_index) { 136 return ZAddress.good(entry.to_offset()); 137 } 138 139 // There's no relocate operation in the SA. 140 // Mimic object pinning and return the good view of the from object. 141 return ZAddress.good(from); 142 } 143 144 long object_alignment_shift() { 145 if (type() == ZGlobals.ZPageTypeSmall) { 146 return ZGlobals.ZObjectAlignmentSmallShift(); 147 } else if (type() == ZGlobals.ZPageTypeMedium) { 148 return ZGlobals.ZObjectAlignmentMediumShift; 149 } else { 150 assert(type() == ZGlobals.ZPageTypeLarge); 151 return ZGlobals.ZObjectAlignmentLargeShift; 152 } 153 } 154 155 long objectAlignmentSize() { 156 return 1 << object_alignment_shift(); 157 } 158 159 public boolean is_active() { 160 return refcount() != 0; 161 } 162 163 private long getObjectSize(Address good) { 164 OopHandle handle = good.addOffsetToAsOopHandle(0); 165 Oop obj = null; 166 167 try { 168 obj = VM.getVM().getObjectHeap().newOop(handle); 169 } catch (UnknownOopException exp) { 170 throw new RuntimeException(" UnknownOopException " + exp); 171 } 172 173 return VM.getVM().alignUp(obj.getObjectSize(), objectAlignmentSize()); 174 } 175 176 private void addNotRelocatedRegions(List<MemRegion> regions) { 177 MemRegion mr = null; 178 179 // Some objects have already been forwarded to new locations. 180 long topValue = top().asLongValue(); 181 for (long offsetValue = start(); offsetValue < topValue;) { 182 Address from = ZAddress.good(ZUtils.longToAddress(offsetValue)); 183 184 Address to = relocate_object(from); 185 186 long byteSize; 187 try { 188 byteSize = getObjectSize(to); 189 } catch (Exception e) { 190 // Parsing the ZHeap is inherently unsafe 191 // when classes have been unloaded. Dead objects 192 // might have stale Klass pointers, and there's 193 // no way to get the size of the dead object. 194 // 195 // If possible, run with -XX:-ClassUnloading 196 // to ensure that all Klasses are kept alive. 197 System.err.println("Unparsable regions found. Skipping: " 198 + from 199 + " " 200 + from.addOffsetTo(topValue - offsetValue)); 201 202 // Can't proceed further. Just return the collected regions. 203 return; 204 } 205 206 if (from.equals(to)) { 207 // Not relocated - add region 208 if (mr == null) { 209 mr = new MemRegion(from, 0 /* wordSize */); 210 regions.add(mr); 211 } 212 213 long wordSize = byteSize / VM.getVM().getBytesPerWord(); 214 mr.setWordSize(mr.wordSize() + wordSize); 215 } else { 216 // Forwarded somewhere else, split region. 217 mr = null; 218 } 219 220 offsetValue += byteSize; 221 } 222 } 223 224 public List<MemRegion> getLiveRegions() { 225 List<MemRegion> res = new ArrayList<>(); 226 227 if (isPageRelocating()) { 228 addNotRelocatedRegions(res); 229 } else { 230 Address start = ZAddress.good(ZUtils.longToAddress(start())); 231 232 // Can't convert top() to a "good" address because it might 233 // be at the top of the "offset" range, and therefore also 234 // looks like one of the color bits. Instead use the "good" 235 // address and add the size. 236 long size = top().asLongValue() - start(); 237 Address end = start.addOffsetTo(size); 238 239 res.add(new MemRegion(start, end)); 240 } 241 242 return res; 243 } 244 }