1 /*
   2  * Copyright (c) 2017, 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.io.PrintStream;
  28 import java.util.Iterator;
  29 
  30 import sun.jvm.hotspot.debugger.Address;
  31 import sun.jvm.hotspot.debugger.OopHandle;
  32 import sun.jvm.hotspot.gc.shared.CollectedHeap;
  33 import sun.jvm.hotspot.gc.shared.CollectedHeapName;
  34 import sun.jvm.hotspot.gc.shared.LiveRegionsClosure;
  35 import sun.jvm.hotspot.runtime.VM;
  36 import sun.jvm.hotspot.runtime.VMObjectFactory;
  37 import sun.jvm.hotspot.types.Type;
  38 import sun.jvm.hotspot.types.TypeDataBase;
  39 import sun.jvm.hotspot.utilities.BitMapInterface;
  40 
  41 // Mirror class for ZCollectedHeap.
  42 
  43 public class ZCollectedHeap extends CollectedHeap {
  44     private static long zHeapFieldOffset;
  45 
  46     static {
  47         VM.registerVMInitializedObserver((o, d) -> initialize(VM.getVM().getTypeDataBase()));
  48     }
  49 
  50     private static synchronized void initialize(TypeDataBase db) {
  51         Type type = db.lookupType("ZCollectedHeap");
  52 
  53         zHeapFieldOffset = type.getAddressField("_heap").getOffset();
  54     }
  55 
  56     public ZHeap heap() {
  57         Address heapAddr = addr.addOffsetTo(zHeapFieldOffset);
  58         return (ZHeap)VMObjectFactory.newObject(ZHeap.class, heapAddr);
  59     }
  60 
  61     @Override
  62     public CollectedHeapName kind() {
  63         return CollectedHeapName.Z;
  64     }
  65 
  66     @Override
  67     public void printOn(PrintStream tty) {
  68         heap().printOn(tty);
  69     }
  70 
  71     public ZCollectedHeap(Address addr) {
  72         super(addr);
  73     }
  74 
  75     @Override
  76     public long capacity() {
  77         return heap().capacity();
  78     }
  79 
  80     @Override
  81     public long used() {
  82         return heap().used();
  83     }
  84 
  85 
  86 
  87     private OopHandle oop_load_barrier(Address oopAddress) {
  88         oopAddress = ZBarrier.weak_barrier(oopAddress);
  89         if (oopAddress == null) {
  90             return null;
  91         }
  92 
  93         return oopAddress.addOffsetToAsOopHandle(0);
  94     }
  95 
  96     @Override
  97     public OopHandle oop_load_at(OopHandle handle, long offset) {
  98         assert(!VM.getVM().isCompressedOopsEnabled());
  99 
 100         Address oopAddress = handle.getAddressAt(offset);
 101 
 102         return oop_load_barrier(oopAddress);
 103     }
 104 
 105     // addr can be either in heap or in native
 106     @Override
 107     public OopHandle oop_load_in_native(Address addr) {
 108         Address oopAddress = addr.getAddressAt(0);
 109 
 110         return oop_load_barrier(oopAddress);
 111     }
 112 
 113     public String oopAddressDescription(OopHandle handle) {
 114         Address origOop = ZOop.to_address(handle);
 115         Address loadBarrieredOop = ZBarrier.weak_barrier(origOop);
 116         if (!origOop.equals(loadBarrieredOop)) {
 117             return origOop + " (" + loadBarrieredOop.toString() + ")";
 118         } else {
 119             return handle.toString();
 120         }
 121     }
 122 
 123     @Override
 124     public void liveRegionsIterate(LiveRegionsClosure closure) {
 125         Iterator<ZPage> iter = heap().pageTable().activePagesIterator();
 126         while (iter.hasNext()) {
 127             ZPage page = iter.next();
 128             closure.doLiveRegions(page);
 129         }
 130     }
 131 
 132     @Override
 133     public BitMapInterface createBitMap(long size) {
 134         // Ignores the size
 135         return new ZExternalBitMap(this);
 136     }
 137 }