1 /*
   2  * Copyright (c) 2017, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 package sun.jvm.hotspot.gc_implementation.shenandoah;
  25 
  26 import sun.jvm.hotspot.gc_interface.CollectedHeap;
  27 import sun.jvm.hotspot.gc_interface.CollectedHeapName;
  28 import sun.jvm.hotspot.debugger.Address;
  29 import sun.jvm.hotspot.runtime.VM;
  30 import sun.jvm.hotspot.runtime.VMObjectFactory;
  31 import sun.jvm.hotspot.types.Type;
  32 import sun.jvm.hotspot.types.TypeDataBase;
  33 import sun.jvm.hotspot.memory.MemRegion;
  34 import sun.jvm.hotspot.memory.SpaceClosure;
  35 import sun.jvm.hotspot.types.AddressField;
  36 import sun.jvm.hotspot.types.CIntegerField;
  37 import sun.jvm.hotspot.types.JLongField;
  38 import java.io.PrintStream;
  39 import java.util.ArrayList;
  40 import java.util.List;
  41 import java.util.Observable;
  42 import java.util.Observer;
  43 
  44 public class ShenandoahHeap extends CollectedHeap {
  45     static private CIntegerField numRegions;
  46     static private JLongField    used;
  47     static private CIntegerField committed;
  48     static private AddressField  regions;
  49 
  50     static {
  51         VM.registerVMInitializedObserver(new Observer() {
  52             public void update(Observable o, Object data) {
  53                 initialize(VM.getVM().getTypeDataBase());
  54             }
  55         });
  56     }
  57 
  58     static private synchronized void initialize(TypeDataBase db) {
  59         Type type = db.lookupType("ShenandoahHeap");
  60         numRegions = type.getCIntegerField("_num_regions");
  61         used = type.getJLongField("_used");
  62         committed = type.getCIntegerField("_committed");
  63         regions = type.getAddressField("_regions");
  64     }
  65 
  66     @Override
  67     public CollectedHeapName kind() {
  68         return CollectedHeapName.SHENANDOAH_HEAP;
  69     }
  70 
  71     public long numOfRegions() {
  72         return numRegions.getValue(addr);
  73     }
  74 
  75     @Override
  76     public long used() {
  77         return used.getValue(addr);
  78     }
  79 
  80     public long committed() {
  81         return committed.getValue(addr);
  82     }
  83 
  84     @Override
  85     public void printOn(PrintStream tty) {
  86         MemRegion mr = reservedRegion();
  87         tty.print("Shenandoah heap");
  88         tty.print(" [" + mr.start() + ", " + mr.end() + "]");
  89         tty.println(" region size " + ShenandoahHeapRegion.regionSizeBytes() / 1024 + " K");
  90     }
  91 
  92     public ShenandoahHeap(Address addr) {
  93         super(addr);
  94     }
  95 
  96     private ShenandoahHeapRegion at(long index) {
  97         Address arrayAddr = regions.getValue(addr);
  98         // Offset of &_regions[index]
  99         long offset = index * VM.getVM().getAddressSize();
 100         Address regionAddr = arrayAddr.getAddressAt(offset);
 101         return (ShenandoahHeapRegion) VMObjectFactory.newObject(ShenandoahHeapRegion.class,
 102                 regionAddr);
 103     }
 104 
 105     public List/*<MemRegion>*/ getLiveRegions() {
 106         List res = new ArrayList();
 107         for (int i = 0; i < numOfRegions(); i++) {
 108             ShenandoahHeapRegion r = at(i);
 109             res.add(new MemRegion(r.bottom(), r.top()));
 110         }
 111         return res;
 112     }
 113 
 114 }