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.parallel.*;
  35 import sun.jvm.hotspot.oops.*;
  36 import sun.jvm.hotspot.types.*;
  37 import sun.jvm.hotspot.runtime.*;
  38 
  39 
  40 public class Universe {
  41   private static AddressField collectedHeapField;
  42   private static VirtualConstructor heapConstructor;
  43   private static sun.jvm.hotspot.types.OopField mainThreadGroupField;
  44   private static sun.jvm.hotspot.types.OopField systemThreadGroupField;
  45 
  46   // single dimensional primitive array klasses
  47   private static sun.jvm.hotspot.types.AddressField boolArrayKlassField;
  48   private static sun.jvm.hotspot.types.AddressField byteArrayKlassField;
  49   private static sun.jvm.hotspot.types.AddressField charArrayKlassField;
  50   private static sun.jvm.hotspot.types.AddressField intArrayKlassField;
  51   private static sun.jvm.hotspot.types.AddressField shortArrayKlassField;
  52   private static sun.jvm.hotspot.types.AddressField longArrayKlassField;
  53   private static sun.jvm.hotspot.types.AddressField singleArrayKlassField;
  54   private static sun.jvm.hotspot.types.AddressField doubleArrayKlassField;
  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 synchronized void initialize(TypeDataBase db) {
  76     Type type = db.lookupType("Universe");
  77 
  78     collectedHeapField = type.getAddressField("_collectedHeap");
  79 
  80     heapConstructor = new VirtualConstructor(db);
  81     heapConstructor.addMapping("CMSHeap", CMSHeap.class);
  82     heapConstructor.addMapping("SerialHeap", SerialHeap.class);
  83     heapConstructor.addMapping("ParallelScavengeHeap", ParallelScavengeHeap.class);
  84     heapConstructor.addMapping("G1CollectedHeap", G1CollectedHeap.class);

  85 
  86     mainThreadGroupField   = type.getOopField("_main_thread_group");
  87     systemThreadGroupField = type.getOopField("_system_thread_group");
  88 
  89     boolArrayKlassField      = type.getAddressField("_boolArrayKlassObj");
  90     byteArrayKlassField      = type.getAddressField("_byteArrayKlassObj");
  91     charArrayKlassField      = type.getAddressField("_charArrayKlassObj");
  92     intArrayKlassField       = type.getAddressField("_intArrayKlassObj");
  93     shortArrayKlassField     = type.getAddressField("_shortArrayKlassObj");
  94     longArrayKlassField      = type.getAddressField("_longArrayKlassObj");
  95     singleArrayKlassField    = type.getAddressField("_singleArrayKlassObj");
  96     doubleArrayKlassField    = type.getAddressField("_doubleArrayKlassObj");
  97 
  98     narrowOopBaseField = type.getAddressField("_narrow_oop._base");
  99     narrowOopShiftField = type.getCIntegerField("_narrow_oop._shift");
 100     narrowKlassBaseField = type.getAddressField("_narrow_klass._base");
 101     narrowKlassShiftField = type.getCIntegerField("_narrow_klass._shift");
 102 
 103     UniverseExt.initialize(heapConstructor);
 104   }
 105 
 106   public Universe() {
 107   }
 108   public static String narrowOopModeToString(NARROW_OOP_MODE mode) {
 109     switch (mode) {
 110     case UnscaledNarrowOop:
 111       return "32-bits Oops";
 112     case ZeroBasedNarrowOop:
 113       return "zero based Compressed Oops";
 114     case HeapBasedNarrowOop:
 115       return "Compressed Oops with base";
 116     }
 117     return "";
 118   }
 119   public CollectedHeap heap() {
 120     return (CollectedHeap) heapConstructor.instantiateWrapperFor(collectedHeapField.getValue());
 121   }
 122 
 123   public static long getNarrowOopBase() {
 124     if (narrowOopBaseField.getValue() == null) {
 125       return 0;
 126     } else {
 127       return narrowOopBaseField.getValue().minus(null);
 128     }
 129   }
 130 
 131   public static int getNarrowOopShift() {
 132     return (int)narrowOopShiftField.getValue();
 133   }
 134 
 135   public static long getNarrowKlassBase() {
 136     if (narrowKlassBaseField.getValue() == null) {
 137       return 0;
 138     } else {
 139       return narrowKlassBaseField.getValue().minus(null);
 140     }
 141   }
 142 
 143   public static int getNarrowKlassShift() {
 144     return (int)narrowKlassShiftField.getValue();
 145   }
 146 
 147 
 148   /** Returns "TRUE" iff "p" points into the allocated area of the heap. */
 149   public boolean isIn(Address p) {
 150     return heap().isIn(p);
 151   }
 152 
 153   /** Returns "TRUE" iff "p" points into the reserved area of the heap. */
 154   public boolean isInReserved(Address p) {
 155     return heap().isInReserved(p);
 156   }
 157 
 158   private Oop newOop(OopHandle handle) {
 159     return VM.getVM().getObjectHeap().newOop(handle);
 160   }
 161 
 162   public Oop mainThreadGroup() {
 163     return newOop(mainThreadGroupField.getValue());
 164   }
 165 
 166   public Oop systemThreadGroup() {
 167     return newOop(systemThreadGroupField.getValue());
 168   }
 169 
 170 
 171   public void print() { printOn(System.out); }
 172   public void printOn(PrintStream tty) {
 173     heap().printOn(tty);
 174   }
 175 
 176   // Check whether an element of a typeArrayOop with the given type must be
 177   // aligned 0 mod 8.  The typeArrayOop itself must be aligned at least this
 178   // strongly.
 179   public static boolean elementTypeShouldBeAligned(BasicType type) {
 180     return type == BasicType.T_DOUBLE || type == BasicType.T_LONG;
 181   }
 182 
 183   // Check whether an object field (static/non-static) of the given type must be
 184   // aligned 0 mod 8.
 185   public static boolean fieldTypeShouldBeAligned(BasicType type) {
 186     return type == BasicType.T_DOUBLE || type == BasicType.T_LONG;
 187   }
 188 }
--- EOF ---