1 /*
   2  * Copyright (c) 2000, 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.memory;
  26 
  27 import java.io.PrintStream;
  28 import java.util.Observable;
  29 import java.util.Observer;
  30 
  31 import sun.jvm.hotspot.debugger.Address;
  32 import sun.jvm.hotspot.debugger.OopHandle;
  33 import sun.jvm.hotspot.gc.cms.CMSHeap;
  34 import sun.jvm.hotspot.gc.epsilon.EpsilonHeap;
  35 import sun.jvm.hotspot.gc.g1.G1CollectedHeap;
  36 import sun.jvm.hotspot.gc.parallel.ParallelScavengeHeap;
  37 import sun.jvm.hotspot.gc.serial.SerialHeap;
  38 import sun.jvm.hotspot.gc.shared.CollectedHeap;
  39 import sun.jvm.hotspot.gc.shenandoah.ShenandoahHeap;
  40 import sun.jvm.hotspot.gc.z.ZCollectedHeap;
  41 import sun.jvm.hotspot.oops.Oop;
  42 import sun.jvm.hotspot.runtime.BasicType;
  43 import sun.jvm.hotspot.runtime.VM;
  44 import sun.jvm.hotspot.runtime.VirtualConstructor;
  45 import sun.jvm.hotspot.types.AddressField;
  46 import sun.jvm.hotspot.types.CIntegerField;
  47 import sun.jvm.hotspot.types.Type;
  48 import sun.jvm.hotspot.types.TypeDataBase;
  49 
  50 
  51 public class Universe {
  52   private static AddressField collectedHeapField;
  53   private static VirtualConstructor heapConstructor;
  54   private static sun.jvm.hotspot.types.OopField mainThreadGroupField;
  55   private static sun.jvm.hotspot.types.OopField systemThreadGroupField;
  56 
  57   private static AddressField narrowOopBaseField;
  58   private static CIntegerField narrowOopShiftField;
  59   private static AddressField narrowKlassBaseField;
  60   private static CIntegerField narrowKlassShiftField;
  61 
  62   public enum NARROW_OOP_MODE {
  63     UnscaledNarrowOop,
  64     ZeroBasedNarrowOop,
  65     HeapBasedNarrowOop
  66   }
  67 
  68   static {
  69     VM.registerVMInitializedObserver(new Observer() {
  70         public void update(Observable o, Object data) {
  71           initialize(VM.getVM().getTypeDataBase());
  72         }
  73       });
  74   }
  75 
  76   private static boolean typeExists(TypeDataBase db, String type) {
  77       try {
  78           db.lookupType(type);
  79       } catch (RuntimeException e) {
  80           return false;
  81       }
  82       return true;
  83   }
  84 
  85   private static void addHeapTypeIfInDB(TypeDataBase db, Class heapClass) {
  86       String heapName = heapClass.getSimpleName();
  87       if (typeExists(db, heapName)) {
  88           heapConstructor.addMapping(heapName, heapClass);
  89       }
  90   }
  91 
  92   private static synchronized void initialize(TypeDataBase db) {
  93     Type type = db.lookupType("Universe");
  94 
  95     collectedHeapField = type.getAddressField("_collectedHeap");
  96 
  97     heapConstructor = new VirtualConstructor(db);
  98     addHeapTypeIfInDB(db, CMSHeap.class);
  99     addHeapTypeIfInDB(db, SerialHeap.class);
 100     addHeapTypeIfInDB(db, ParallelScavengeHeap.class);
 101     addHeapTypeIfInDB(db, G1CollectedHeap.class);
 102     addHeapTypeIfInDB(db, EpsilonHeap.class);
 103     addHeapTypeIfInDB(db, ZCollectedHeap.class);
 104     addHeapTypeIfInDB(db, ShenandoahHeap.class);
 105 
 106     mainThreadGroupField   = type.getOopField("_main_thread_group");
 107     systemThreadGroupField = type.getOopField("_system_thread_group");
 108 
 109     narrowOopBaseField = type.getAddressField("_narrow_oop._base");
 110     narrowOopShiftField = type.getCIntegerField("_narrow_oop._shift");
 111     narrowKlassBaseField = type.getAddressField("_narrow_klass._base");
 112     narrowKlassShiftField = type.getCIntegerField("_narrow_klass._shift");
 113 
 114     UniverseExt.initialize(heapConstructor);
 115   }
 116 
 117   public Universe() {
 118   }
 119   public static String narrowOopModeToString(NARROW_OOP_MODE mode) {
 120     switch (mode) {
 121     case UnscaledNarrowOop:
 122       return "32-bits Oops";
 123     case ZeroBasedNarrowOop:
 124       return "zero based Compressed Oops";
 125     case HeapBasedNarrowOop:
 126       return "Compressed Oops with base";
 127     }
 128     return "";
 129   }
 130   public CollectedHeap heap() {
 131     return (CollectedHeap) heapConstructor.instantiateWrapperFor(collectedHeapField.getValue());
 132   }
 133 
 134   public static long getNarrowOopBase() {
 135     if (narrowOopBaseField.getValue() == null) {
 136       return 0;
 137     } else {
 138       return narrowOopBaseField.getValue().minus(null);
 139     }
 140   }
 141 
 142   public static int getNarrowOopShift() {
 143     return (int)narrowOopShiftField.getValue();
 144   }
 145 
 146   public static long getNarrowKlassBase() {
 147     if (narrowKlassBaseField.getValue() == null) {
 148       return 0;
 149     } else {
 150       return narrowKlassBaseField.getValue().minus(null);
 151     }
 152   }
 153 
 154   public static int getNarrowKlassShift() {
 155     return (int)narrowKlassShiftField.getValue();
 156   }
 157 
 158 
 159   /** Returns "TRUE" iff "p" points into the allocated area of the heap. */
 160   public boolean isIn(Address p) {
 161     return heap().isIn(p);
 162   }
 163 
 164   /** Returns "TRUE" iff "p" points into the reserved area of the heap. */
 165   public boolean isInReserved(Address p) {
 166     return heap().isInReserved(p);
 167   }
 168 
 169   private Oop newOop(OopHandle handle) {
 170     return VM.getVM().getObjectHeap().newOop(handle);
 171   }
 172 
 173   public Oop mainThreadGroup() {
 174     return newOop(mainThreadGroupField.getValue());
 175   }
 176 
 177   public Oop systemThreadGroup() {
 178     return newOop(systemThreadGroupField.getValue());
 179   }
 180 
 181 
 182   public void print() { printOn(System.out); }
 183   public void printOn(PrintStream tty) {
 184     heap().printOn(tty);
 185   }
 186 
 187   // Check whether an element of a typeArrayOop with the given type must be
 188   // aligned 0 mod 8.  The typeArrayOop itself must be aligned at least this
 189   // strongly.
 190   public static boolean elementTypeShouldBeAligned(BasicType type) {
 191     return type == BasicType.T_DOUBLE || type == BasicType.T_LONG;
 192   }
 193 
 194   // Check whether an object field (static/non-static) of the given type must be
 195   // aligned 0 mod 8.
 196   public static boolean fieldTypeShouldBeAligned(BasicType type) {
 197     return type == BasicType.T_DOUBLE || type == BasicType.T_LONG;
 198   }
 199 }