1 /*
   2  * Copyright (c) 2000, 2017, 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.*;
  28 import java.util.*;
  29 import sun.jvm.hotspot.debugger.*;
  30 import sun.jvm.hotspot.gc.cms.CMSHeap;
  31 import sun.jvm.hotspot.gc.serial.SerialHeap;
  32 import sun.jvm.hotspot.gc.shared.*;
  33 import sun.jvm.hotspot.gc.g1.G1CollectedHeap;
  34 import sun.jvm.hotspot.gc.epsilon.EpsilonHeap;
  35 import sun.jvm.hotspot.gc.parallel.*;
  36 import sun.jvm.hotspot.oops.*;
  37 import sun.jvm.hotspot.types.*;
  38 import sun.jvm.hotspot.runtime.*;
  39 
  40 
  41 public class Universe {
  42   private static AddressField collectedHeapField;
  43   private static VirtualConstructor heapConstructor;
  44   private static sun.jvm.hotspot.types.OopField mainThreadGroupField;
  45   private static sun.jvm.hotspot.types.OopField systemThreadGroupField;
  46 
  47   // single dimensional primitive array klasses
  48   private static sun.jvm.hotspot.types.AddressField boolArrayKlassField;
  49   private static sun.jvm.hotspot.types.AddressField byteArrayKlassField;
  50   private static sun.jvm.hotspot.types.AddressField charArrayKlassField;
  51   private static sun.jvm.hotspot.types.AddressField intArrayKlassField;
  52   private static sun.jvm.hotspot.types.AddressField shortArrayKlassField;
  53   private static sun.jvm.hotspot.types.AddressField longArrayKlassField;
  54   private static sun.jvm.hotspot.types.AddressField singleArrayKlassField;
  55   private static sun.jvm.hotspot.types.AddressField doubleArrayKlassField;
  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 synchronized void initialize(TypeDataBase db) {
  77     Type type = db.lookupType("Universe");
  78 
  79     collectedHeapField = type.getAddressField("_collectedHeap");
  80 
  81     heapConstructor = new VirtualConstructor(db);
  82     heapConstructor.addMapping("CMSHeap", CMSHeap.class);
  83     heapConstructor.addMapping("SerialHeap", SerialHeap.class);
  84     heapConstructor.addMapping("ParallelScavengeHeap", ParallelScavengeHeap.class);
  85     heapConstructor.addMapping("G1CollectedHeap", G1CollectedHeap.class);
  86     heapConstructor.addMapping("EpsilonHeap", EpsilonHeap.class);
  87 
  88     mainThreadGroupField   = type.getOopField("_main_thread_group");
  89     systemThreadGroupField = type.getOopField("_system_thread_group");
  90 
  91     boolArrayKlassField      = type.getAddressField("_boolArrayKlassObj");
  92     byteArrayKlassField      = type.getAddressField("_byteArrayKlassObj");
  93     charArrayKlassField      = type.getAddressField("_charArrayKlassObj");
  94     intArrayKlassField       = type.getAddressField("_intArrayKlassObj");
  95     shortArrayKlassField     = type.getAddressField("_shortArrayKlassObj");
  96     longArrayKlassField      = type.getAddressField("_longArrayKlassObj");
  97     singleArrayKlassField    = type.getAddressField("_singleArrayKlassObj");
  98     doubleArrayKlassField    = type.getAddressField("_doubleArrayKlassObj");
  99 
 100     narrowOopBaseField = type.getAddressField("_narrow_oop._base");
 101     narrowOopShiftField = type.getCIntegerField("_narrow_oop._shift");
 102     narrowKlassBaseField = type.getAddressField("_narrow_klass._base");
 103     narrowKlassShiftField = type.getCIntegerField("_narrow_klass._shift");
 104 
 105     UniverseExt.initialize(heapConstructor);
 106   }
 107 
 108   public Universe() {
 109   }
 110   public static String narrowOopModeToString(NARROW_OOP_MODE mode) {
 111     switch (mode) {
 112     case UnscaledNarrowOop:
 113       return "32-bits Oops";
 114     case ZeroBasedNarrowOop:
 115       return "zero based Compressed Oops";
 116     case HeapBasedNarrowOop:
 117       return "Compressed Oops with base";
 118     }
 119     return "";
 120   }
 121   public CollectedHeap heap() {
 122     return (CollectedHeap) heapConstructor.instantiateWrapperFor(collectedHeapField.getValue());
 123   }
 124 
 125   public static long getNarrowOopBase() {
 126     if (narrowOopBaseField.getValue() == null) {
 127       return 0;
 128     } else {
 129       return narrowOopBaseField.getValue().minus(null);
 130     }
 131   }
 132 
 133   public static int getNarrowOopShift() {
 134     return (int)narrowOopShiftField.getValue();
 135   }
 136 
 137   public static long getNarrowKlassBase() {
 138     if (narrowKlassBaseField.getValue() == null) {
 139       return 0;
 140     } else {
 141       return narrowKlassBaseField.getValue().minus(null);
 142     }
 143   }
 144 
 145   public static int getNarrowKlassShift() {
 146     return (int)narrowKlassShiftField.getValue();
 147   }
 148 
 149 
 150   /** Returns "TRUE" iff "p" points into the allocated area of the heap. */
 151   public boolean isIn(Address p) {
 152     return heap().isIn(p);
 153   }
 154 
 155   /** Returns "TRUE" iff "p" points into the reserved area of the heap. */
 156   public boolean isInReserved(Address p) {
 157     return heap().isInReserved(p);
 158   }
 159 
 160   private Oop newOop(OopHandle handle) {
 161     return VM.getVM().getObjectHeap().newOop(handle);
 162   }
 163 
 164   public Oop mainThreadGroup() {
 165     return newOop(mainThreadGroupField.getValue());
 166   }
 167 
 168   public Oop systemThreadGroup() {
 169     return newOop(systemThreadGroupField.getValue());
 170   }
 171 
 172 
 173   public void print() { printOn(System.out); }
 174   public void printOn(PrintStream tty) {
 175     heap().printOn(tty);
 176   }
 177 
 178   // Check whether an element of a typeArrayOop with the given type must be
 179   // aligned 0 mod 8.  The typeArrayOop itself must be aligned at least this
 180   // strongly.
 181   public static boolean elementTypeShouldBeAligned(BasicType type) {
 182     return type == BasicType.T_DOUBLE || type == BasicType.T_LONG;
 183   }
 184 
 185   // Check whether an object field (static/non-static) of the given type must be
 186   // aligned 0 mod 8.
 187   public static boolean fieldTypeShouldBeAligned(BasicType type) {
 188     return type == BasicType.T_DOUBLE || type == BasicType.T_LONG;
 189   }
 190 }
--- EOF ---