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