< prev index next >

src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/gc/z/ZPage.java

Print this page




   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 sun.jvm.hotspot.debugger.Address;





  28 import sun.jvm.hotspot.runtime.VM;
  29 import sun.jvm.hotspot.runtime.VMObject;
  30 import sun.jvm.hotspot.runtime.VMObjectFactory;

  31 import sun.jvm.hotspot.types.CIntegerField;
  32 import sun.jvm.hotspot.types.Type;
  33 import sun.jvm.hotspot.types.TypeDataBase;
  34 
  35 public class ZPage extends VMObject {
  36     private static CIntegerField typeField;

  37     private static long virtualFieldOffset;


  38     private static long forwardingFieldOffset;
  39 
  40     static {
  41         VM.registerVMInitializedObserver((o, d) -> initialize(VM.getVM().getTypeDataBase()));
  42     }
  43 
  44     static private synchronized void initialize(TypeDataBase db) {
  45         Type type = db.lookupType("ZPage");
  46 
  47         typeField = type.getCIntegerField("_type");

  48         virtualFieldOffset = type.getField("_virtual").getOffset();


  49         forwardingFieldOffset = type.getField("_forwarding").getOffset();
  50     }
  51 
  52     public ZPage(Address addr) {
  53         super(addr);
  54     }
  55 
  56     private byte type() {
  57         return typeField.getJByte(addr);
  58     }
  59 




  60     private ZVirtualMemory virtual() {
  61         return (ZVirtualMemory)VMObjectFactory.newObject(ZVirtualMemory.class, addr.addOffsetTo(virtualFieldOffset));









  62     }
  63 
  64     private ZForwardingTable forwarding() {
  65         return (ZForwardingTable)VMObjectFactory.newObject(ZForwardingTable.class, addr.addOffsetTo(forwardingFieldOffset));















  66     }
  67 
  68     long start() {
  69         return virtual().start();
  70     }
  71 
  72     long size() {
  73         return virtual().end() - virtual().start();
  74     }
  75 
  76     Address forward_object(Address from) {
  77         // Lookup address in forwarding table
  78         long from_offset = ZAddress.offset(from);
  79         long from_index = (from_offset - start()) >> object_alignment_shift();
  80         ZForwardingTableEntry entry = forwarding().find(from_index);
  81         assert(!entry.is_empty());
  82         assert(entry.from_index() == from_index);
  83 
  84         return ZAddress.good(entry.to_offset());
  85     }
  86 
  87     Address relocate_object(Address from) {
  88         // Lookup address in forwarding table
  89         long from_offset = ZAddress.offset(from);
  90         long from_index = (from_offset - start()) >> object_alignment_shift();
  91         ZForwardingTableEntry entry = forwarding().find(from_index);
  92         if (!entry.is_empty() && entry.from_index() == from_index) {
  93           return ZAddress.good(entry.to_offset());
  94         }
  95 
  96         // There's no relocate operation in the SA.
  97         // Mimic object pinning and return the good view of the from object.
  98         return ZAddress.good(from);
  99     }
 100 
 101 
 102     long object_alignment_shift() {
 103         if (type() == ZGlobals.ZPageTypeSmall) {
 104             return ZGlobals.ZObjectAlignmentSmallShift();
 105         } else if (type() == ZGlobals.ZPageTypeMedium) {
 106             return ZGlobals.ZObjectAlignmentMediumShift;
 107         } else {
 108             assert(type() == ZGlobals.ZPageTypeLarge);
 109             return ZGlobals.ZObjectAlignmentLargeShift;
 110         }


























































































 111     }
 112 }


   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 }
< prev index next >