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