Mercurial > hg > truffle
annotate graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/CompileTheWorld.java @ 10672:e7d07c9bb779
Removed priority compilation queue.
Fixed another race condition in the compilation queue.
Removed Graal-specific compilation policy.
author | Christian Haeubl <haeubl@ssw.jku.at> |
---|---|
date | Tue, 09 Jul 2013 17:53:58 +0200 |
parents | 97caf20971ed |
children | 5124eeec1a7b |
rev | line source |
---|---|
9108 | 1 /* |
2 * Copyright (c) 2013, 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 package com.oracle.graal.hotspot; | |
24 | |
9289
261a43921c5e
rename: HotSpotGraalRuntime.getInstance() -> graalRuntime()
Doug Simon <doug.simon@oracle.com>
parents:
9108
diff
changeset
|
25 import static com.oracle.graal.hotspot.HotSpotGraalRuntime.*; |
9864
063a712fe8d8
converted remaining options in GraalOptions to new system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
9289
diff
changeset
|
26 import static com.oracle.graal.phases.GraalOptions.*; |
9289
261a43921c5e
rename: HotSpotGraalRuntime.getInstance() -> graalRuntime()
Doug Simon <doug.simon@oracle.com>
parents:
9108
diff
changeset
|
27 |
9108 | 28 import java.io.File; |
29 import java.lang.reflect.Constructor; | |
30 import java.lang.reflect.Method; | |
31 import java.lang.reflect.Modifier; | |
32 import java.net.*; | |
33 import java.util.Enumeration; | |
34 import java.util.jar.*; | |
35 | |
36 import com.oracle.graal.api.meta.*; | |
37 import com.oracle.graal.bytecode.Bytecodes; | |
38 import com.oracle.graal.debug.*; | |
39 import com.oracle.graal.hotspot.bridge.*; | |
40 import com.oracle.graal.hotspot.meta.*; | |
41 import com.oracle.graal.nodes.*; | |
42 import com.oracle.graal.phases.*; | |
43 import com.oracle.graal.replacements.*; | |
44 | |
45 /** | |
46 * This class implements compile-the-world functionality in Graal. | |
47 */ | |
48 public final class CompileTheWorld { | |
49 | |
50 /** | |
51 * This is our magic token to trigger reading files from the boot class path. | |
52 */ | |
53 public static final String SUN_BOOT_CLASS_PATH = "sun.boot.class.path"; | |
54 | |
55 // Some runtime instances we need. | |
9289
261a43921c5e
rename: HotSpotGraalRuntime.getInstance() -> graalRuntime()
Doug Simon <doug.simon@oracle.com>
parents:
9108
diff
changeset
|
56 private final HotSpotGraalRuntime graalRuntime = graalRuntime(); |
9108 | 57 private final VMToCompilerImpl vmToCompiler = (VMToCompilerImpl) graalRuntime.getVMToCompiler(); |
58 | |
59 /** List of Zip/Jar files to compile (see {@link GraalOptions#CompileTheWorld}. */ | |
60 private final String files; | |
61 | |
62 /** Class index to start compilation at (see {@link GraalOptions#CompileTheWorldStartAt}. */ | |
63 private final int startAt; | |
64 | |
65 /** Class index to stop compilation at (see {@link GraalOptions#CompileTheWorldStopAt}. */ | |
66 private final int stopAt; | |
67 | |
68 // Counters | |
69 private int classFileCounter = 0; | |
70 private int compiledMethodsCounter = 0; | |
71 private long compileTime = 0; | |
72 | |
73 /** | |
74 * Create a compile-the-world instance with default values from | |
75 * {@link GraalOptions#CompileTheWorld}, {@link GraalOptions#CompileTheWorldStartAt} and | |
76 * {@link GraalOptions#CompileTheWorldStopAt}. | |
77 */ | |
78 public CompileTheWorld() { | |
9864
063a712fe8d8
converted remaining options in GraalOptions to new system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
9289
diff
changeset
|
79 this(CompileTheWorld.getValue(), CompileTheWorldStartAt.getValue(), CompileTheWorldStopAt.getValue()); |
9108 | 80 } |
81 | |
82 /** | |
83 * Create a compile-the-world instance. | |
84 * | |
85 * @param files {@link File#pathSeparator} separated list of Zip/Jar files to compile | |
86 * @param startAt index of the class file to start compilation at | |
87 * @param stopAt index of the class file to stop compilation at | |
88 */ | |
89 public CompileTheWorld(String files, int startAt, int stopAt) { | |
90 this.files = files; | |
91 this.startAt = startAt; | |
92 this.stopAt = stopAt; | |
93 | |
94 // We don't want the VM to exit when a method fails to compile. | |
9864
063a712fe8d8
converted remaining options in GraalOptions to new system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents:
9289
diff
changeset
|
95 ExitVMOnException.setValue(false); |
9108 | 96 } |
97 | |
98 /** | |
99 * Compile all methods in all classes in the Zip/Jar files in | |
100 * {@link GraalOptions#CompileTheWorld}. If the GraalOptions.CompileTheWorld contains the magic | |
101 * token {@link CompileTheWorld#SUN_BOOT_CLASS_PATH} passed up from HotSpot we take the files | |
102 * from the boot class path. | |
103 * | |
104 * @throws Throwable | |
105 */ | |
106 public void compile() throws Throwable { | |
107 if (SUN_BOOT_CLASS_PATH.equals(files)) { | |
108 final String[] entries = System.getProperty(SUN_BOOT_CLASS_PATH).split(File.pathSeparator); | |
109 String bcpFiles = ""; | |
110 for (int i = 0; i < entries.length; i++) { | |
111 final String entry = entries[i]; | |
112 | |
113 // We stop at rt.jar, unless it is the first boot class path entry. | |
114 if (entry.endsWith("rt.jar") && (i > 0)) { | |
115 break; | |
116 } | |
117 if (i > 0) { | |
118 bcpFiles += File.pathSeparator; | |
119 } | |
120 bcpFiles += entry; | |
121 } | |
122 compile(bcpFiles); | |
123 } else { | |
124 compile(files); | |
125 } | |
126 } | |
127 | |
128 /** | |
129 * Compile all methods in all classes in the Zip/Jar files passed. | |
130 * | |
131 * @param fileList {@link File#pathSeparator} separated list of Zip/Jar files to compile | |
132 * @throws Throwable | |
133 */ | |
134 private void compile(String fileList) throws Throwable { | |
135 final String[] entries = fileList.split(File.pathSeparator); | |
136 | |
137 for (int i = 0; i < entries.length; i++) { | |
138 final String entry = entries[i]; | |
139 | |
140 // For now we only compile all methods in all classes in zip/jar files. | |
141 if (!entry.endsWith(".zip") && !entry.endsWith(".jar")) { | |
142 TTY.println("CompileTheWorld : Skipped classes in " + entry); | |
143 TTY.println(); | |
144 continue; | |
145 } | |
146 | |
147 TTY.println("CompileTheWorld : Compiling all classes in " + entry); | |
148 TTY.println(); | |
149 | |
150 URL url = new URL("jar", "", "file:" + entry + "!/"); | |
151 ClassLoader loader = new URLClassLoader(new URL[]{url}); | |
152 | |
153 JarFile jarFile = new JarFile(entry); | |
154 Enumeration<JarEntry> e = jarFile.entries(); | |
155 | |
156 while (e.hasMoreElements()) { | |
157 JarEntry je = e.nextElement(); | |
158 if (je.isDirectory() || !je.getName().endsWith(".class")) { | |
159 continue; | |
160 } | |
161 | |
162 // Are we done? | |
163 if (classFileCounter >= stopAt) { | |
164 break; | |
165 } | |
166 | |
167 String className = je.getName().substring(0, je.getName().length() - ".class".length()); | |
168 classFileCounter++; | |
169 | |
170 try { | |
171 // Load and initialize class | |
10543
97caf20971ed
CTW: adapt output messages, so that they match with the output of hotspot
Bernhard Urban <bernhard.urban@jku.at>
parents:
10461
diff
changeset
|
172 Class<?> javaClass = Class.forName(className.replace('/', '.'), true, loader); |
9108 | 173 |
174 // Pre-load all classes in the constant pool. | |
175 try { | |
176 HotSpotResolvedObjectType objectType = (HotSpotResolvedObjectType) HotSpotResolvedObjectType.fromClass(javaClass); | |
177 ConstantPool constantPool = objectType.constantPool(); | |
178 for (int cpi = 1; cpi < constantPool.length(); cpi++) { | |
179 constantPool.loadReferencedType(cpi, Bytecodes.LDC); | |
180 } | |
181 } catch (Throwable t) { | |
182 // If something went wrong during pre-loading we just ignore it. | |
10543
97caf20971ed
CTW: adapt output messages, so that they match with the output of hotspot
Bernhard Urban <bernhard.urban@jku.at>
parents:
10461
diff
changeset
|
183 TTY.println("Preloading failed for (%d) %s", classFileCounter, className); |
9108 | 184 } |
185 | |
186 // Are we compiling this class? | |
187 if (classFileCounter >= startAt) { | |
188 TTY.println("CompileTheWorld (%d) : %s", classFileCounter, className); | |
189 | |
190 // Enqueue each constructor/method in the class for compilation. | |
191 for (Constructor<?> constructor : javaClass.getDeclaredConstructors()) { | |
192 HotSpotResolvedJavaMethod javaMethod = (HotSpotResolvedJavaMethod) graalRuntime.getRuntime().lookupJavaConstructor(constructor); | |
193 if (canBeCompiled(javaMethod, constructor.getModifiers())) { | |
194 compileMethod(javaMethod); | |
195 } | |
196 } | |
197 for (Method method : javaClass.getDeclaredMethods()) { | |
198 HotSpotResolvedJavaMethod javaMethod = (HotSpotResolvedJavaMethod) graalRuntime.getRuntime().lookupJavaMethod(method); | |
199 if (canBeCompiled(javaMethod, method.getModifiers())) { | |
200 compileMethod(javaMethod); | |
201 } | |
202 } | |
203 } | |
204 } catch (Throwable t) { | |
205 TTY.println("CompileTheWorld (%d) : Skipping %s", classFileCounter, className); | |
206 } | |
207 } | |
208 jarFile.close(); | |
209 } | |
210 | |
211 TTY.println(); | |
212 TTY.println("CompileTheWorld : Done (%d classes, %d methods, %d ms)", classFileCounter, compiledMethodsCounter, compileTime); | |
213 } | |
214 | |
215 /** | |
216 * Helper method to schedule a method for compilation and gather some statistics. | |
217 */ | |
218 private void compileMethod(HotSpotResolvedJavaMethod method) { | |
219 try { | |
220 long start = System.currentTimeMillis(); | |
10672
e7d07c9bb779
Removed priority compilation queue.
Christian Haeubl <haeubl@ssw.jku.at>
parents:
10543
diff
changeset
|
221 vmToCompiler.compileMethod(method, StructuredGraph.INVOCATION_ENTRY_BCI, true); |
9108 | 222 compileTime += (System.currentTimeMillis() - start); |
223 compiledMethodsCounter++; | |
224 method.reprofile(); // makes the method also not-entrant | |
225 } catch (Throwable t) { | |
226 // Catch everything and print a message | |
227 TTY.println("CompileTheWorld (%d) : Error compiling method: %s", classFileCounter, MetaUtil.format("%H.%n(%p):%r", method)); | |
228 t.printStackTrace(TTY.cachedOut); | |
229 } | |
230 } | |
231 | |
232 /** | |
233 * Helper method for CompileTheWorld to determine if a method should be compiled (Cf. | |
234 * CompilationPolicy::can_be_compiled). | |
235 * | |
236 * @return true if it can be compiled, false otherwise | |
237 */ | |
238 private static boolean canBeCompiled(HotSpotResolvedJavaMethod javaMethod, int modifiers) { | |
239 if (Modifier.isAbstract(modifiers) || Modifier.isNative(modifiers)) { | |
240 return false; | |
241 } | |
242 // This number is from HotSpot: | |
243 final int hugeMethodLimit = 8000; | |
244 if (javaMethod.getCodeSize() > hugeMethodLimit) { | |
245 return false; | |
246 } | |
247 // Skip @Snippets for now | |
248 if (javaMethod.getAnnotation(Snippet.class) != null) { | |
249 return false; | |
250 } | |
251 return true; | |
252 } | |
253 | |
254 } |