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