comparison agent/src/share/classes/sun/jvm/hotspot/code/CodeCache.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 148e5441d916
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2000-2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 package sun.jvm.hotspot.code;
26
27 import java.util.*;
28 import sun.jvm.hotspot.debugger.*;
29 import sun.jvm.hotspot.memory.*;
30 import sun.jvm.hotspot.runtime.*;
31 import sun.jvm.hotspot.types.*;
32 import sun.jvm.hotspot.utilities.*;
33
34 public class CodeCache {
35 private static AddressField heapField;
36 private static VirtualConstructor virtualConstructor;
37
38 private CodeHeap heap;
39
40 static {
41 VM.registerVMInitializedObserver(new Observer() {
42 public void update(Observable o, Object data) {
43 initialize(VM.getVM().getTypeDataBase());
44 }
45 });
46 }
47
48 private static synchronized void initialize(TypeDataBase db) {
49 Type type = db.lookupType("CodeCache");
50
51 heapField = type.getAddressField("_heap");
52
53 virtualConstructor = new VirtualConstructor(db);
54 // Add mappings for all possible CodeBlob subclasses
55 virtualConstructor.addMapping("BufferBlob", BufferBlob.class);
56 virtualConstructor.addMapping("nmethod", NMethod.class);
57 virtualConstructor.addMapping("RuntimeStub", RuntimeStub.class);
58 virtualConstructor.addMapping("SafepointBlob", SafepointBlob.class);
59 virtualConstructor.addMapping("DeoptimizationBlob", DeoptimizationBlob.class);
60 if (VM.getVM().isServerCompiler()) {
61 virtualConstructor.addMapping("ExceptionBlob", ExceptionBlob.class);
62 virtualConstructor.addMapping("UncommonTrapBlob", UncommonTrapBlob.class);
63 }
64 }
65
66 public CodeCache() {
67 heap = (CodeHeap) VMObjectFactory.newObject(CodeHeap.class, heapField.getValue());
68 }
69
70 public boolean contains(Address p) {
71 return getHeap().contains(p);
72 }
73
74 /** When VM.getVM().isDebugging() returns true, this behaves like
75 findBlobUnsafe */
76 public CodeBlob findBlob(Address start) {
77 CodeBlob result = findBlobUnsafe(start);
78 if (result == null) return null;
79 if (VM.getVM().isDebugging()) {
80 return result;
81 }
82 // We could potientially look up non_entrant methods
83 // NOTE: this is effectively a "guarantee", and is slightly different from the one in the VM
84 if (Assert.ASSERTS_ENABLED) {
85 Assert.that(!(result.isZombie() || result.isLockedByVM()), "unsafe access to zombie method");
86 }
87 return result;
88 }
89
90 public CodeBlob findBlobUnsafe(Address start) {
91 CodeBlob result = null;
92
93 try {
94 result = (CodeBlob) virtualConstructor.instantiateWrapperFor(getHeap().findStart(start));
95 }
96 catch (WrongTypeException wte) {
97 Address cbAddr = null;
98 try {
99 cbAddr = getHeap().findStart(start);
100 }
101 catch (Exception findEx) {
102 findEx.printStackTrace();
103 }
104
105 String message = "Couldn't deduce type of CodeBlob ";
106 if (cbAddr != null) {
107 message = message + "@" + cbAddr + " ";
108 }
109 message = message + "for PC=" + start;
110
111 throw new RuntimeException(message, wte);
112 }
113 if (result == null) return null;
114 if (Assert.ASSERTS_ENABLED) {
115 // The HeapBlock that contains this blob is outside of the blob
116 // but it shouldn't be an error to find a blob based on the
117 // pointer to the HeapBlock.
118 Assert.that(result.blobContains(start) || result.blobContains(start.addOffsetTo(8)),
119 "found wrong CodeBlob");
120 }
121 return result;
122 }
123
124 public NMethod findNMethod(Address start) {
125 CodeBlob cb = findBlob(start);
126 if (Assert.ASSERTS_ENABLED) {
127 Assert.that(cb == null || cb.isNMethod(), "did not find an nmethod");
128 }
129 return (NMethod) cb;
130 }
131
132 public NMethod findNMethodUnsafe(Address start) {
133 CodeBlob cb = findBlobUnsafe(start);
134 if (Assert.ASSERTS_ENABLED) {
135 Assert.that(cb == null || cb.isNMethod(), "did not find an nmethod");
136 }
137 return (NMethod) cb;
138 }
139
140 /** Routine for instantiating appropriately-typed wrapper for a
141 CodeBlob. Used by CodeCache, Runtime1, etc. */
142 public CodeBlob createCodeBlobWrapper(Address codeBlobAddr) {
143 try {
144 return (CodeBlob) virtualConstructor.instantiateWrapperFor(codeBlobAddr);
145 }
146 catch (Exception e) {
147 String message = "Unable to deduce type of CodeBlob from address " + codeBlobAddr +
148 " (expected type nmethod, RuntimeStub, ";
149 if (VM.getVM().isClientCompiler()) {
150 message = message + " or ";
151 }
152 message = message + "SafepointBlob";
153 if (VM.getVM().isServerCompiler()) {
154 message = message + ", DeoptimizationBlob, or ExceptionBlob";
155 }
156 message = message + ")";
157 throw new RuntimeException(message);
158 }
159 }
160
161 public void iterate(CodeCacheVisitor visitor) {
162 CodeHeap heap = getHeap();
163 Address ptr = heap.begin();
164 Address end = heap.end();
165
166 visitor.prologue(ptr, end);
167 CodeBlob lastBlob = null;
168 while (ptr != null && ptr.lessThan(end)) {
169 try {
170 CodeBlob blob = findBlobUnsafe(ptr);
171 if (blob != null) {
172 visitor.visit(blob);
173 if (blob == lastBlob) {
174 throw new InternalError("saw same blob twice");
175 }
176 lastBlob = blob;
177 }
178 } catch (RuntimeException e) {
179 e.printStackTrace();
180 }
181 Address next = heap.nextBlock(ptr);
182 if (next != null && next.lessThan(ptr)) {
183 throw new InternalError("pointer moved backwards");
184 }
185 ptr = next;
186 }
187 visitor.epilogue();
188 }
189
190 //--------------------------------------------------------------------------------
191 // Internals only below this point
192 //
193
194 private CodeHeap getHeap() {
195 return heap;
196 }
197 }