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