1 /*
   2  * Copyright (c) 2011, 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.g1;
  26 
  27 import java.io.PrintStream;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import java.util.Observable;
  31 import java.util.Observer;
  32 import sun.jvm.hotspot.debugger.Address;
  33 import sun.jvm.hotspot.debugger.OopHandle;
  34 import sun.jvm.hotspot.gc.shared.CompactibleSpace;
  35 import sun.jvm.hotspot.gc.shared.LiveRegionsProvider;
  36 import sun.jvm.hotspot.memory.MemRegion;
  37 import sun.jvm.hotspot.runtime.VM;
  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 // Mirror class for HeapRegion. Currently we don't actually include
  45 // any of its fields but only iterate over it.
  46 
  47 public class HeapRegion extends CompactibleSpace implements LiveRegionsProvider {
  48     // static int GrainBytes;
  49     static private CIntegerField grainBytesField;
  50     static private AddressField topField;
  51     private static long typeFieldOffset;
  52     private static long pointerSize;
  53 
  54     private HeapRegionType type;
  55 
  56     static {
  57         VM.registerVMInitializedObserver(new Observer() {
  58                 public void update(Observable o, Object data) {
  59                     initialize(VM.getVM().getTypeDataBase());
  60                 }
  61             });
  62     }
  63 
  64     static private synchronized void initialize(TypeDataBase db) {
  65         Type type = db.lookupType("HeapRegion");
  66 
  67         grainBytesField = type.getCIntegerField("GrainBytes");
  68         topField = type.getAddressField("_top");
  69         typeFieldOffset = type.getField("_type").getOffset();
  70 
  71         pointerSize = db.lookupType("HeapRegion*").getSize();
  72     }
  73 
  74     static public long grainBytes() {
  75         return grainBytesField.getValue();
  76     }
  77 
  78     public HeapRegion(Address addr) {
  79         super(addr);
  80         Address typeAddr = (addr instanceof OopHandle) ? addr.addOffsetToAsOopHandle(typeFieldOffset)
  81                                                        : addr.addOffsetTo(typeFieldOffset);
  82         type = (HeapRegionType)VMObjectFactory.newObject(HeapRegionType.class, typeAddr);
  83     }
  84 
  85     public Address top() {
  86         return topField.getValue(addr);
  87     }
  88 
  89     @Override
  90     public List<MemRegion> getLiveRegions() {
  91         List<MemRegion> res = new ArrayList<>();
  92         res.add(new MemRegion(bottom(), top()));
  93         return res;
  94     }
  95 
  96     @Override
  97     public long used() {
  98         return top().minus(bottom());
  99     }
 100 
 101     @Override
 102     public long free() {
 103         return end().minus(top());
 104     }
 105 
 106     public boolean isFree() {
 107         return type.isFree();
 108     }
 109 
 110     public boolean isYoung() {
 111         return type.isYoung();
 112     }
 113 
 114     public boolean isHumongous() {
 115         return type.isHumongous();
 116     }
 117 
 118     public boolean isPinned() {
 119         return type.isPinned();
 120     }
 121 
 122     public boolean isOld() {
 123         return type.isOld();
 124     }
 125 
 126     public static long getPointerSize() {
 127         return pointerSize;
 128     }
 129 
 130     public void printOn(PrintStream tty) {
 131         tty.print("Region: " + bottom() + "," + top() + "," + end());
 132         tty.println(":" + type.typeAnnotation());
 133     }
 134 }