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