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     private static AddressField bottomField;
  49     static private AddressField topField;
  50     private static AddressField endField;
  51 
  52     static private CIntegerField grainBytesField;
  53     private static long typeFieldOffset;
  54     private static long pointerSize;
  55 
  56     private HeapRegionType type;
  57 
  58     static {
  59         VM.registerVMInitializedObserver(new Observer() {
  60                 public void update(Observable o, Object data) {
  61                     initialize(VM.getVM().getTypeDataBase());
  62                 }
  63             });
  64     }
  65 
  66     static private synchronized void initialize(TypeDataBase db) {
  67         Type type = db.lookupType("HeapRegion");
  68 
  69         bottomField = type.getAddressField("_bottom");
  70         topField = type.getAddressField("_top");
  71         endField = type.getAddressField("_end");
  72 
  73         grainBytesField = type.getCIntegerField("GrainBytes");
  74         typeFieldOffset = type.getField("_type").getOffset();
  75 
  76         pointerSize = db.lookupType("HeapRegion*").getSize();
  77     }
  78 
  79     static public long grainBytes() {
  80         return grainBytesField.getValue();
  81     }
  82 
  83     public HeapRegion(Address addr) {
  84         super(addr);
  85         Address typeAddr = (addr instanceof OopHandle) ? addr.addOffsetToAsOopHandle(typeFieldOffset)
  86                                                        : addr.addOffsetTo(typeFieldOffset);
  87         type = (HeapRegionType)VMObjectFactory.newObject(HeapRegionType.class, typeAddr);
  88     }
  89 
  90     public Address bottom()        { return bottomField.getValue(addr); }
  91     public Address top()           { return topField.getValue(addr); }
  92     public Address end()           { return endField.getValue(addr); }
  93 
  94     @Override
  95     public List<MemRegion> getLiveRegions() {
  96         List<MemRegion> res = new ArrayList<>();
  97         res.add(new MemRegion(bottom(), top()));
  98         return res;
  99     }
 100 
 101     /** Returns a subregion of the space containing all the objects in
 102         the space. */
 103     public MemRegion usedRegion() {
 104         return new MemRegion(bottom(), end());
 105     }
 106 
 107     public long used() {
 108         return top().minus(bottom());
 109     }
 110 
 111     public long free() {
 112         return end().minus(top());
 113     }
 114 
 115     public boolean isFree() {
 116         return type.isFree();
 117     }
 118 
 119     public boolean isYoung() {
 120         return type.isYoung();
 121     }
 122 
 123     public boolean isHumongous() {
 124         return type.isHumongous();
 125     }
 126 
 127     public boolean isPinned() {
 128         return type.isPinned();
 129     }
 130 
 131     public boolean isOld() {
 132         return type.isOld();
 133     }
 134 
 135     public static long getPointerSize() {
 136         return pointerSize;
 137     }
 138 
 139     public void printOn(PrintStream tty) {
 140         tty.print("Region: " + bottom() + "," + top() + "," + end());
 141         tty.println(":" + type.typeAnnotation());
 142     }
 143 }