View Javadoc
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    static {
60      VM.registerVMInitializedObserver(new Observer() {
61          public void update(Observable o, Object data) {
62            initialize(VM.getVM().getTypeDataBase());
63          }
64        });
65    }
66  
67    private static synchronized void initialize(TypeDataBase db) {
68      Type type = db.lookupType("Universe");
69  
70      collectedHeapField = type.getAddressField("_collectedHeap");
71  
72      heapConstructor = new VirtualConstructor(db);
73      heapConstructor.addMapping("GenCollectedHeap", GenCollectedHeap.class);
74      heapConstructor.addMapping("ParallelScavengeHeap", ParallelScavengeHeap.class);
75      heapConstructor.addMapping("G1CollectedHeap", G1CollectedHeap.class);
76  
77      mainThreadGroupField   = type.getOopField("_main_thread_group");
78      systemThreadGroupField = type.getOopField("_system_thread_group");
79  
80      boolArrayKlassField      = type.getAddressField("_boolArrayKlassObj");
81      byteArrayKlassField      = type.getAddressField("_byteArrayKlassObj");
82      charArrayKlassField      = type.getAddressField("_charArrayKlassObj");
83      intArrayKlassField       = type.getAddressField("_intArrayKlassObj");
84      shortArrayKlassField     = type.getAddressField("_shortArrayKlassObj");
85      longArrayKlassField      = type.getAddressField("_longArrayKlassObj");
86      singleArrayKlassField    = type.getAddressField("_singleArrayKlassObj");
87      doubleArrayKlassField    = type.getAddressField("_doubleArrayKlassObj");
88  
89      narrowOopBaseField = type.getAddressField("_narrow_oop._base");
90      narrowOopShiftField = type.getCIntegerField("_narrow_oop._shift");
91      narrowKlassBaseField = type.getAddressField("_narrow_klass._base");
92      narrowKlassShiftField = type.getCIntegerField("_narrow_klass._shift");
93    }
94  
95    public Universe() {
96    }
97  
98    public CollectedHeap heap() {
99      try {
100       return (CollectedHeap) heapConstructor.instantiateWrapperFor(collectedHeapField.getValue());
101     } catch (WrongTypeException e) {
102       return new CollectedHeap(collectedHeapField.getValue());
103     }
104   }
105 
106   public static long getNarrowOopBase() {
107     if (narrowOopBaseField.getValue() == null) {
108       return 0;
109     } else {
110       return narrowOopBaseField.getValue().minus(null);
111     }
112   }
113 
114   public static int getNarrowOopShift() {
115     return (int)narrowOopShiftField.getValue();
116   }
117 
118   public static long getNarrowKlassBase() {
119     if (narrowKlassBaseField.getValue() == null) {
120       return 0;
121     } else {
122       return narrowKlassBaseField.getValue().minus(null);
123     }
124   }
125 
126   public static int getNarrowKlassShift() {
127     return (int)narrowKlassShiftField.getValue();
128   }
129 
130 
131   /** Returns "TRUE" iff "p" points into the allocated area of the heap. */
132   public boolean isIn(Address p) {
133     return heap().isIn(p);
134   }
135 
136   /** Returns "TRUE" iff "p" points into the reserved area of the heap. */
137   public boolean isInReserved(Address p) {
138     return heap().isInReserved(p);
139   }
140 
141   private Oop newOop(OopHandle handle) {
142     return VM.getVM().getObjectHeap().newOop(handle);
143   }
144 
145   public Oop mainThreadGroup() {
146     return newOop(mainThreadGroupField.getValue());
147   }
148 
149   public Oop systemThreadGroup() {
150     return newOop(systemThreadGroupField.getValue());
151   }
152 
153   // iterate through the single dimensional primitive array klasses
154   // refer to basic_type_classes_do(void f(Klass*)) in universe.cpp
155   public void basicTypeClassesDo(SystemDictionary.ClassVisitor visitor) {
156     visitor.visit(new TypeArrayKlass(boolArrayKlassField.getValue()));
157     visitor.visit(new TypeArrayKlass(byteArrayKlassField.getValue()));
158     visitor.visit(new TypeArrayKlass(charArrayKlassField.getValue()));
159     visitor.visit(new TypeArrayKlass(intArrayKlassField.getValue()));
160     visitor.visit(new TypeArrayKlass(shortArrayKlassField.getValue()));
161     visitor.visit(new TypeArrayKlass(longArrayKlassField.getValue()));
162     visitor.visit(new TypeArrayKlass(singleArrayKlassField.getValue()));
163     visitor.visit(new TypeArrayKlass(doubleArrayKlassField.getValue()));
164   }
165 
166   public void print() { printOn(System.out); }
167   public void printOn(PrintStream tty) {
168     heap().printOn(tty);
169   }
170 
171   // Check whether an element of a typeArrayOop with the given type must be
172   // aligned 0 mod 8.  The typeArrayOop itself must be aligned at least this
173   // strongly.
174   public static boolean elementTypeShouldBeAligned(BasicType type) {
175     return type == BasicType.T_DOUBLE || type == BasicType.T_LONG;
176   }
177 
178   // Check whether an object field (static/non-static) of the given type must be
179   // aligned 0 mod 8.
180   public static boolean fieldTypeShouldBeAligned(BasicType type) {
181     return type == BasicType.T_DOUBLE || type == BasicType.T_LONG;
182   }
183 }