author | sangheki |
Tue, 20 Feb 2018 12:54:26 -0800 | |
changeset 49028 | c7ba033fc07a |
parent 48861 | 47f19ff9903c |
child 49451 | e06f9607f370 |
permissions | -rw-r--r-- |
46393 | 1 |
/* |
49028
c7ba033fc07a
8193909: Obsolete(remove) Co-operative Memory Management (CMM)
sangheki
parents:
48861
diff
changeset
|
2 |
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. |
46393 | 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 org.graalvm.compiler.hotspot.test; |
|
24 |
||
25 |
import static java.util.Collections.singletonList; |
|
46680 | 26 |
import static org.graalvm.compiler.core.CompilationWrapper.ExceptionAction.Print; |
27 |
import static org.graalvm.compiler.core.GraalCompilerOptions.CompilationBailoutAction; |
|
28 |
import static org.graalvm.compiler.core.GraalCompilerOptions.CompilationFailureAction; |
|
46393 | 29 |
import static org.graalvm.compiler.core.test.ReflectionOptionDescriptors.extractEntries; |
46640 | 30 |
import static org.graalvm.compiler.debug.MemUseTrackerKey.getCurrentThreadAllocatedBytes; |
46393 | 31 |
import static org.graalvm.compiler.hotspot.test.CompileTheWorld.Options.DESCRIPTORS; |
46459 | 32 |
import static org.graalvm.compiler.serviceprovider.JDK9Method.Java8OrEarlier; |
46393 | 33 |
|
34 |
import java.io.Closeable; |
|
35 |
import java.io.File; |
|
36 |
import java.io.IOException; |
|
37 |
import java.lang.annotation.Annotation; |
|
38 |
import java.lang.reflect.Constructor; |
|
39 |
import java.lang.reflect.Method; |
|
40 |
import java.lang.reflect.Modifier; |
|
41 |
import java.net.URI; |
|
42 |
import java.net.URL; |
|
43 |
import java.net.URLClassLoader; |
|
44 |
import java.nio.file.FileSystem; |
|
45 |
import java.nio.file.FileSystems; |
|
46 |
import java.nio.file.FileVisitResult; |
|
47 |
import java.nio.file.Files; |
|
48 |
import java.nio.file.Path; |
|
49 |
import java.nio.file.SimpleFileVisitor; |
|
50 |
import java.nio.file.attribute.BasicFileAttributes; |
|
51 |
import java.util.ArrayList; |
|
52 |
import java.util.Arrays; |
|
53 |
import java.util.Collections; |
|
54 |
import java.util.Enumeration; |
|
47667 | 55 |
import java.util.HashMap; |
46393 | 56 |
import java.util.HashSet; |
57 |
import java.util.List; |
|
47667 | 58 |
import java.util.Map; |
46393 | 59 |
import java.util.ServiceLoader; |
60 |
import java.util.Set; |
|
61 |
import java.util.concurrent.ExecutionException; |
|
62 |
import java.util.concurrent.Future; |
|
63 |
import java.util.concurrent.LinkedBlockingQueue; |
|
64 |
import java.util.concurrent.ThreadPoolExecutor; |
|
65 |
import java.util.concurrent.TimeUnit; |
|
66 |
import java.util.concurrent.atomic.AtomicLong; |
|
67 |
import java.util.jar.JarEntry; |
|
68 |
import java.util.jar.JarFile; |
|
69 |
import java.util.stream.Collectors; |
|
70 |
||
48861 | 71 |
import org.graalvm.collections.EconomicMap; |
72 |
import org.graalvm.collections.UnmodifiableEconomicMap; |
|
46393 | 73 |
import org.graalvm.compiler.api.replacements.Snippet; |
74 |
import org.graalvm.compiler.bytecode.Bytecodes; |
|
75 |
import org.graalvm.compiler.core.CompilerThreadFactory; |
|
76 |
import org.graalvm.compiler.core.test.ReflectionOptionDescriptors; |
|
46640 | 77 |
import org.graalvm.compiler.debug.DebugOptions; |
46393 | 78 |
import org.graalvm.compiler.debug.GraalError; |
79 |
import org.graalvm.compiler.debug.MethodFilter; |
|
80 |
import org.graalvm.compiler.debug.TTY; |
|
81 |
import org.graalvm.compiler.hotspot.CompilationTask; |
|
82 |
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; |
|
83 |
import org.graalvm.compiler.hotspot.HotSpotGraalCompiler; |
|
84 |
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider; |
|
85 |
import org.graalvm.compiler.options.OptionDescriptors; |
|
86 |
import org.graalvm.compiler.options.OptionKey; |
|
87 |
import org.graalvm.compiler.options.OptionValues; |
|
88 |
import org.graalvm.compiler.options.OptionsParser; |
|
46459 | 89 |
import org.graalvm.compiler.serviceprovider.JDK9Method; |
46393 | 90 |
|
91 |
import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider; |
|
92 |
import jdk.vm.ci.hotspot.HotSpotCompilationRequest; |
|
93 |
import jdk.vm.ci.hotspot.HotSpotInstalledCode; |
|
94 |
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; |
|
95 |
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider; |
|
96 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; |
|
97 |
import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; |
|
98 |
import jdk.vm.ci.meta.ConstantPool; |
|
99 |
import jdk.vm.ci.meta.MetaAccessProvider; |
|
100 |
import jdk.vm.ci.runtime.JVMCI; |
|
101 |
import jdk.vm.ci.runtime.JVMCICompiler; |
|
102 |
||
103 |
/** |
|
104 |
* This class implements compile-the-world functionality with JVMCI. |
|
105 |
*/ |
|
106 |
public final class CompileTheWorld { |
|
107 |
||
108 |
/** |
|
46459 | 109 |
* Magic token to denote that JDK classes are to be compiled. If |
110 |
* {@link JDK9Method#Java8OrEarlier}, then the classes in {@code rt.jar} are compiled. Otherwise |
|
111 |
* the classes in the Java runtime image are compiled. |
|
46393 | 112 |
*/ |
113 |
public static final String SUN_BOOT_CLASS_PATH = "sun.boot.class.path"; |
|
114 |
||
115 |
/** |
|
116 |
* Magic token to denote the classes in the Java runtime image (i.e. in the {@code jrt:/} file |
|
117 |
* system). |
|
118 |
*/ |
|
119 |
public static final String JRT_CLASS_PATH_ENTRY = "<jrt>"; |
|
120 |
||
121 |
/** |
|
122 |
* @param options a space separated set of option value settings with each option setting in a |
|
123 |
* {@code -Dgraal.<name>=<value>} format but without the leading {@code -Dgraal.}. |
|
124 |
* Ignored if null. |
|
125 |
*/ |
|
126 |
public static EconomicMap<OptionKey<?>, Object> parseOptions(String options) { |
|
127 |
if (options != null) { |
|
128 |
EconomicMap<String, String> optionSettings = EconomicMap.create(); |
|
129 |
for (String optionSetting : options.split("\\s+|#")) { |
|
130 |
OptionsParser.parseOptionSettingTo(optionSetting, optionSettings); |
|
131 |
} |
|
132 |
EconomicMap<OptionKey<?>, Object> values = OptionValues.newOptionMap(); |
|
133 |
ServiceLoader<OptionDescriptors> loader = ServiceLoader.load(OptionDescriptors.class, OptionDescriptors.class.getClassLoader()); |
|
134 |
OptionsParser.parseOptions(optionSettings, values, loader); |
|
135 |
return values; |
|
136 |
} |
|
137 |
return EconomicMap.create(); |
|
138 |
} |
|
139 |
||
140 |
private final HotSpotJVMCIRuntimeProvider jvmciRuntime; |
|
141 |
||
142 |
private final HotSpotGraalCompiler compiler; |
|
143 |
||
144 |
/** |
|
145 |
* Class path denoting classes to compile. |
|
146 |
* |
|
147 |
* @see Options#Classpath |
|
148 |
*/ |
|
149 |
private final String inputClassPath; |
|
150 |
||
151 |
/** |
|
152 |
* Class index to start compilation at. |
|
153 |
* |
|
154 |
* @see Options#StartAt |
|
155 |
*/ |
|
156 |
private final int startAt; |
|
157 |
||
158 |
/** |
|
159 |
* Class index to stop compilation at. |
|
160 |
* |
|
161 |
* @see Options#StopAt |
|
162 |
*/ |
|
163 |
private final int stopAt; |
|
164 |
||
165 |
/** Only compile methods matching one of the filters in this array if the array is non-null. */ |
|
166 |
private final MethodFilter[] methodFilters; |
|
167 |
||
168 |
/** Exclude methods matching one of the filters in this array if the array is non-null. */ |
|
169 |
private final MethodFilter[] excludeMethodFilters; |
|
170 |
||
171 |
// Counters |
|
172 |
private int classFileCounter = 0; |
|
173 |
private AtomicLong compiledMethodsCounter = new AtomicLong(); |
|
174 |
private AtomicLong compileTime = new AtomicLong(); |
|
175 |
private AtomicLong memoryUsed = new AtomicLong(); |
|
176 |
||
177 |
private boolean verbose; |
|
178 |
||
179 |
/** |
|
180 |
* Signal that the threads should start compiling in multithreaded mode. |
|
181 |
*/ |
|
182 |
private boolean running; |
|
183 |
||
184 |
private ThreadPoolExecutor threadPool; |
|
185 |
||
186 |
private OptionValues currentOptions; |
|
46459 | 187 |
private final UnmodifiableEconomicMap<OptionKey<?>, Object> compilationOptions; |
46393 | 188 |
|
189 |
/** |
|
190 |
* Creates a compile-the-world instance. |
|
191 |
* |
|
192 |
* @param files {@link File#pathSeparator} separated list of Zip/Jar files to compile |
|
193 |
* @param startAt index of the class file to start compilation at |
|
194 |
* @param stopAt index of the class file to stop compilation at |
|
195 |
* @param methodFilters |
|
196 |
* @param excludeMethodFilters |
|
197 |
*/ |
|
198 |
public CompileTheWorld(HotSpotJVMCIRuntimeProvider jvmciRuntime, HotSpotGraalCompiler compiler, String files, int startAt, int stopAt, String methodFilters, String excludeMethodFilters, |
|
199 |
boolean verbose, OptionValues initialOptions, EconomicMap<OptionKey<?>, Object> compilationOptions) { |
|
200 |
this.jvmciRuntime = jvmciRuntime; |
|
201 |
this.compiler = compiler; |
|
202 |
this.inputClassPath = files; |
|
203 |
this.startAt = startAt; |
|
204 |
this.stopAt = stopAt; |
|
205 |
this.methodFilters = methodFilters == null || methodFilters.isEmpty() ? null : MethodFilter.parse(methodFilters); |
|
206 |
this.excludeMethodFilters = excludeMethodFilters == null || excludeMethodFilters.isEmpty() ? null : MethodFilter.parse(excludeMethodFilters); |
|
207 |
this.verbose = verbose; |
|
208 |
this.currentOptions = initialOptions; |
|
209 |
||
46459 | 210 |
// Copy the initial options and add in any extra options |
211 |
EconomicMap<OptionKey<?>, Object> compilationOptionsCopy = EconomicMap.create(initialOptions.getMap()); |
|
212 |
compilationOptionsCopy.putAll(compilationOptions); |
|
213 |
||
46680 | 214 |
// We want to see stack traces when a method fails to compile |
215 |
CompilationBailoutAction.putIfAbsent(compilationOptionsCopy, Print); |
|
216 |
CompilationFailureAction.putIfAbsent(compilationOptionsCopy, Print); |
|
46393 | 217 |
|
218 |
// By default only report statistics for the CTW threads themselves |
|
46640 | 219 |
DebugOptions.MetricsThreadFilter.putIfAbsent(compilationOptionsCopy, "^CompileTheWorld"); |
46459 | 220 |
this.compilationOptions = compilationOptionsCopy; |
46393 | 221 |
} |
222 |
||
223 |
public CompileTheWorld(HotSpotJVMCIRuntimeProvider jvmciRuntime, HotSpotGraalCompiler compiler, OptionValues options) { |
|
224 |
this(jvmciRuntime, compiler, Options.Classpath.getValue(options), |
|
225 |
Options.StartAt.getValue(options), |
|
226 |
Options.StopAt.getValue(options), |
|
227 |
Options.MethodFilter.getValue(options), |
|
228 |
Options.ExcludeMethodFilter.getValue(options), |
|
229 |
Options.Verbose.getValue(options), |
|
230 |
options, |
|
231 |
parseOptions(Options.Config.getValue(options))); |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* Compiles all methods in all classes in {@link #inputClassPath}. If {@link #inputClassPath} |
|
236 |
* equals {@link #SUN_BOOT_CLASS_PATH} the boot classes are used. |
|
237 |
*/ |
|
238 |
public void compile() throws Throwable { |
|
239 |
if (SUN_BOOT_CLASS_PATH.equals(inputClassPath)) { |
|
240 |
String bcpEntry = null; |
|
241 |
if (Java8OrEarlier) { |
|
242 |
final String[] entries = System.getProperty(SUN_BOOT_CLASS_PATH).split(File.pathSeparator); |
|
243 |
for (int i = 0; i < entries.length && bcpEntry == null; i++) { |
|
244 |
String entry = entries[i]; |
|
245 |
File entryFile = new File(entry); |
|
246 |
if (entryFile.getName().endsWith("rt.jar") && entryFile.isFile()) { |
|
247 |
bcpEntry = entry; |
|
248 |
} |
|
249 |
} |
|
250 |
if (bcpEntry == null) { |
|
251 |
throw new GraalError("Could not find rt.jar on boot class path %s", System.getProperty(SUN_BOOT_CLASS_PATH)); |
|
252 |
} |
|
253 |
} else { |
|
254 |
bcpEntry = JRT_CLASS_PATH_ENTRY; |
|
255 |
} |
|
256 |
compile(bcpEntry); |
|
257 |
} else { |
|
258 |
compile(inputClassPath); |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
public void println() { |
|
263 |
println(""); |
|
264 |
} |
|
265 |
||
266 |
public void println(String format, Object... args) { |
|
267 |
println(String.format(format, args)); |
|
268 |
} |
|
269 |
||
270 |
public void println(String s) { |
|
271 |
println(verbose, s); |
|
272 |
} |
|
273 |
||
274 |
public static void println(boolean cond, String s) { |
|
275 |
if (cond) { |
|
276 |
TTY.println(s); |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
public void printStackTrace(Throwable t) { |
|
281 |
if (verbose) { |
|
282 |
t.printStackTrace(TTY.out); |
|
283 |
} |
|
284 |
} |
|
285 |
||
286 |
@SuppressWarnings("unused") |
|
287 |
private static void dummy() { |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* Abstraction over different types of class path entries. |
|
292 |
*/ |
|
293 |
abstract static class ClassPathEntry implements Closeable { |
|
294 |
final String name; |
|
295 |
||
296 |
ClassPathEntry(String name) { |
|
297 |
this.name = name; |
|
298 |
} |
|
299 |
||
300 |
/** |
|
301 |
* Creates a {@link ClassLoader} for loading classes from this entry. |
|
302 |
*/ |
|
303 |
public abstract ClassLoader createClassLoader() throws IOException; |
|
304 |
||
305 |
/** |
|
306 |
* Gets the list of classes available under this entry. |
|
307 |
*/ |
|
308 |
public abstract List<String> getClassNames() throws IOException; |
|
309 |
||
310 |
@Override |
|
311 |
public String toString() { |
|
312 |
return name; |
|
313 |
} |
|
314 |
||
315 |
@Override |
|
316 |
public void close() throws IOException { |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
/** |
|
321 |
* A class path entry that is a normal file system directory. |
|
322 |
*/ |
|
323 |
static class DirClassPathEntry extends ClassPathEntry { |
|
324 |
||
325 |
private final File dir; |
|
326 |
||
327 |
DirClassPathEntry(String name) { |
|
328 |
super(name); |
|
329 |
dir = new File(name); |
|
330 |
assert dir.isDirectory(); |
|
331 |
} |
|
332 |
||
333 |
@Override |
|
334 |
public ClassLoader createClassLoader() throws IOException { |
|
335 |
URL url = dir.toURI().toURL(); |
|
336 |
return new URLClassLoader(new URL[]{url}); |
|
337 |
} |
|
338 |
||
339 |
@Override |
|
340 |
public List<String> getClassNames() throws IOException { |
|
341 |
List<String> classNames = new ArrayList<>(); |
|
342 |
String root = dir.getPath(); |
|
343 |
SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { |
|
344 |
@Override |
|
345 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
|
346 |
if (attrs.isRegularFile()) { |
|
347 |
File path = file.toFile(); |
|
348 |
if (path.getName().endsWith(".class")) { |
|
349 |
String pathString = path.getPath(); |
|
350 |
assert pathString.startsWith(root); |
|
351 |
String classFile = pathString.substring(root.length() + 1); |
|
352 |
String className = classFile.replace(File.separatorChar, '.'); |
|
353 |
classNames.add(className.replace('/', '.').substring(0, className.length() - ".class".length())); |
|
354 |
} |
|
355 |
} |
|
356 |
return super.visitFile(file, attrs); |
|
357 |
} |
|
358 |
}; |
|
359 |
Files.walkFileTree(dir.toPath(), visitor); |
|
360 |
return classNames; |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* A class path entry that is a jar or zip file. |
|
366 |
*/ |
|
367 |
static class JarClassPathEntry extends ClassPathEntry { |
|
368 |
||
369 |
private final JarFile jarFile; |
|
370 |
||
371 |
JarClassPathEntry(String name) throws IOException { |
|
372 |
super(name); |
|
373 |
jarFile = new JarFile(name); |
|
374 |
} |
|
375 |
||
376 |
@Override |
|
377 |
public ClassLoader createClassLoader() throws IOException { |
|
378 |
URL url = new URL("jar", "", "file:" + name + "!/"); |
|
379 |
return new URLClassLoader(new URL[]{url}); |
|
380 |
} |
|
381 |
||
382 |
@Override |
|
383 |
public List<String> getClassNames() throws IOException { |
|
384 |
Enumeration<JarEntry> e = jarFile.entries(); |
|
385 |
List<String> classNames = new ArrayList<>(jarFile.size()); |
|
386 |
while (e.hasMoreElements()) { |
|
387 |
JarEntry je = e.nextElement(); |
|
388 |
if (je.isDirectory() || !je.getName().endsWith(".class")) { |
|
389 |
continue; |
|
390 |
} |
|
391 |
String className = je.getName().substring(0, je.getName().length() - ".class".length()); |
|
392 |
classNames.add(className.replace('/', '.')); |
|
393 |
} |
|
394 |
return classNames; |
|
395 |
} |
|
396 |
||
397 |
@Override |
|
398 |
public void close() throws IOException { |
|
399 |
jarFile.close(); |
|
400 |
} |
|
401 |
} |
|
402 |
||
403 |
/** |
|
404 |
* A class path entry representing the {@code jrt:/} file system. |
|
405 |
*/ |
|
406 |
static class JRTClassPathEntry extends ClassPathEntry { |
|
407 |
||
408 |
private final String limitModules; |
|
409 |
||
410 |
JRTClassPathEntry(String name, String limitModules) { |
|
411 |
super(name); |
|
412 |
this.limitModules = limitModules; |
|
413 |
} |
|
414 |
||
415 |
@Override |
|
416 |
public ClassLoader createClassLoader() throws IOException { |
|
417 |
URL url = URI.create("jrt:/").toURL(); |
|
418 |
return new URLClassLoader(new URL[]{url}); |
|
419 |
} |
|
420 |
||
421 |
@Override |
|
422 |
public List<String> getClassNames() throws IOException { |
|
423 |
Set<String> negative = new HashSet<>(); |
|
424 |
Set<String> positive = new HashSet<>(); |
|
425 |
if (limitModules != null && !limitModules.isEmpty()) { |
|
426 |
for (String s : limitModules.split(",")) { |
|
427 |
if (s.startsWith("~")) { |
|
428 |
negative.add(s.substring(1)); |
|
429 |
} else { |
|
430 |
positive.add(s); |
|
431 |
} |
|
432 |
} |
|
433 |
} |
|
434 |
List<String> classNames = new ArrayList<>(); |
|
435 |
FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), Collections.emptyMap()); |
|
436 |
Path top = fs.getPath("/modules/"); |
|
437 |
Files.find(top, Integer.MAX_VALUE, |
|
438 |
(path, attrs) -> attrs.isRegularFile()).forEach(p -> { |
|
439 |
int nameCount = p.getNameCount(); |
|
440 |
if (nameCount > 2) { |
|
441 |
String base = p.getName(nameCount - 1).toString(); |
|
442 |
if (base.endsWith(".class") && !base.equals("module-info.class")) { |
|
443 |
String module = p.getName(1).toString(); |
|
444 |
if (positive.isEmpty() || positive.contains(module)) { |
|
445 |
if (negative.isEmpty() || !negative.contains(module)) { |
|
446 |
// Strip module prefix and convert to dotted form |
|
447 |
String className = p.subpath(2, nameCount).toString().replace('/', '.'); |
|
448 |
// Strip ".class" suffix |
|
449 |
className = className.replace('/', '.').substring(0, className.length() - ".class".length()); |
|
450 |
classNames.add(className); |
|
451 |
} |
|
452 |
} |
|
453 |
} |
|
454 |
} |
|
455 |
}); |
|
456 |
return classNames; |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
private boolean isClassIncluded(String className) { |
|
461 |
if (methodFilters != null && !MethodFilter.matchesClassName(methodFilters, className)) { |
|
462 |
return false; |
|
463 |
} |
|
464 |
if (excludeMethodFilters != null && MethodFilter.matchesClassName(excludeMethodFilters, className)) { |
|
465 |
return false; |
|
466 |
} |
|
467 |
return true; |
|
468 |
} |
|
469 |
||
470 |
/** |
|
471 |
* Compiles all methods in all classes in a given class path. |
|
472 |
* |
|
473 |
* @param classPath class path denoting classes to compile |
|
474 |
* @throws IOException |
|
475 |
*/ |
|
476 |
@SuppressWarnings("try") |
|
477 |
private void compile(String classPath) throws IOException { |
|
478 |
final String[] entries = classPath.split(File.pathSeparator); |
|
479 |
long start = System.currentTimeMillis(); |
|
47667 | 480 |
Map<Thread, StackTraceElement[]> initialThreads = Thread.getAllStackTraces(); |
46393 | 481 |
|
482 |
try { |
|
483 |
// compile dummy method to get compiler initialized outside of the |
|
484 |
// config debug override. |
|
485 |
HotSpotResolvedJavaMethod dummyMethod = (HotSpotResolvedJavaMethod) JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess().lookupJavaMethod( |
|
486 |
CompileTheWorld.class.getDeclaredMethod("dummy")); |
|
487 |
int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI; |
|
488 |
boolean useProfilingInfo = false; |
|
489 |
boolean installAsDefault = false; |
|
490 |
CompilationTask task = new CompilationTask(jvmciRuntime, compiler, new HotSpotCompilationRequest(dummyMethod, entryBCI, 0L), useProfilingInfo, installAsDefault, currentOptions); |
|
491 |
task.runCompilation(); |
|
492 |
} catch (NoSuchMethodException | SecurityException e1) { |
|
493 |
printStackTrace(e1); |
|
494 |
} |
|
495 |
||
496 |
/* |
|
497 |
* Always use a thread pool, even for single threaded mode since it simplifies the use of |
|
498 |
* DebugValueThreadFilter to filter on the thread names. |
|
499 |
*/ |
|
500 |
int threadCount = 1; |
|
501 |
if (Options.MultiThreaded.getValue(currentOptions)) { |
|
502 |
threadCount = Options.Threads.getValue(currentOptions); |
|
503 |
if (threadCount == 0) { |
|
504 |
threadCount = Runtime.getRuntime().availableProcessors(); |
|
505 |
} |
|
506 |
} else { |
|
507 |
running = true; |
|
508 |
} |
|
509 |
||
510 |
OptionValues savedOptions = currentOptions; |
|
46459 | 511 |
currentOptions = new OptionValues(compilationOptions); |
46640 | 512 |
threadPool = new ThreadPoolExecutor(threadCount, threadCount, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new CompilerThreadFactory("CompileTheWorld")); |
46393 | 513 |
|
514 |
try { |
|
515 |
for (int i = 0; i < entries.length; i++) { |
|
516 |
final String entry = entries[i]; |
|
517 |
||
518 |
ClassPathEntry cpe; |
|
519 |
if (entry.endsWith(".zip") || entry.endsWith(".jar")) { |
|
520 |
cpe = new JarClassPathEntry(entry); |
|
521 |
} else if (entry.equals(JRT_CLASS_PATH_ENTRY)) { |
|
522 |
cpe = new JRTClassPathEntry(entry, Options.LimitModules.getValue(currentOptions)); |
|
523 |
} else { |
|
524 |
if (!new File(entry).isDirectory()) { |
|
525 |
println("CompileTheWorld : Skipped classes in " + entry); |
|
526 |
println(); |
|
527 |
continue; |
|
528 |
} |
|
529 |
cpe = new DirClassPathEntry(entry); |
|
530 |
} |
|
531 |
||
532 |
if (methodFilters == null || methodFilters.length == 0) { |
|
533 |
println("CompileTheWorld : Compiling all classes in " + entry); |
|
534 |
} else { |
|
535 |
String include = Arrays.asList(methodFilters).stream().map(MethodFilter::toString).collect(Collectors.joining(", ")); |
|
536 |
println("CompileTheWorld : Compiling all methods in " + entry + " matching one of the following filters: " + include); |
|
537 |
} |
|
538 |
if (excludeMethodFilters != null && excludeMethodFilters.length > 0) { |
|
539 |
String exclude = Arrays.asList(excludeMethodFilters).stream().map(MethodFilter::toString).collect(Collectors.joining(", ")); |
|
540 |
println("CompileTheWorld : Excluding all methods matching one of the following filters: " + exclude); |
|
541 |
} |
|
542 |
println(); |
|
543 |
||
544 |
ClassLoader loader = cpe.createClassLoader(); |
|
545 |
||
546 |
for (String className : cpe.getClassNames()) { |
|
547 |
||
548 |
// Are we done? |
|
549 |
if (classFileCounter >= stopAt) { |
|
550 |
break; |
|
551 |
} |
|
552 |
||
553 |
classFileCounter++; |
|
554 |
||
47667 | 555 |
if (className.startsWith("jdk.management.") || |
556 |
// GR-5881: The class initializer for |
|
557 |
// sun.tools.jconsole.OutputViewer |
|
558 |
// spawns non-daemon threads for redirecting sysout and syserr. |
|
559 |
// These threads tend to cause deadlock at VM exit |
|
560 |
className.startsWith("sun.tools.jconsole.")) { |
|
46393 | 561 |
continue; |
562 |
} |
|
563 |
||
564 |
try { |
|
565 |
// Load and initialize class |
|
566 |
Class<?> javaClass = Class.forName(className, true, loader); |
|
567 |
||
568 |
// Pre-load all classes in the constant pool. |
|
569 |
try { |
|
570 |
HotSpotResolvedObjectType objectType = HotSpotResolvedObjectType.fromObjectClass(javaClass); |
|
571 |
ConstantPool constantPool = objectType.getConstantPool(); |
|
572 |
for (int cpi = 1; cpi < constantPool.length(); cpi++) { |
|
573 |
constantPool.loadReferencedType(cpi, Bytecodes.LDC); |
|
574 |
} |
|
575 |
} catch (Throwable t) { |
|
576 |
// If something went wrong during pre-loading we just ignore it. |
|
577 |
if (isClassIncluded(className)) { |
|
578 |
println("Preloading failed for (%d) %s: %s", classFileCounter, className, t); |
|
579 |
} |
|
580 |
continue; |
|
581 |
} |
|
582 |
||
583 |
/* |
|
584 |
* Only check filters after class loading and resolution to mitigate impact |
|
585 |
* on reproducibility. |
|
586 |
*/ |
|
587 |
if (!isClassIncluded(className)) { |
|
588 |
continue; |
|
589 |
} |
|
590 |
||
591 |
// Are we compiling this class? |
|
592 |
MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess(); |
|
593 |
if (classFileCounter >= startAt) { |
|
594 |
println("CompileTheWorld (%d) : %s", classFileCounter, className); |
|
595 |
||
596 |
// Compile each constructor/method in the class. |
|
597 |
for (Constructor<?> constructor : javaClass.getDeclaredConstructors()) { |
|
598 |
HotSpotResolvedJavaMethod javaMethod = (HotSpotResolvedJavaMethod) metaAccess.lookupJavaMethod(constructor); |
|
599 |
if (canBeCompiled(javaMethod, constructor.getModifiers())) { |
|
600 |
compileMethod(javaMethod); |
|
601 |
} |
|
602 |
} |
|
603 |
for (Method method : javaClass.getDeclaredMethods()) { |
|
604 |
HotSpotResolvedJavaMethod javaMethod = (HotSpotResolvedJavaMethod) metaAccess.lookupJavaMethod(method); |
|
605 |
if (canBeCompiled(javaMethod, method.getModifiers())) { |
|
606 |
compileMethod(javaMethod); |
|
607 |
} |
|
608 |
} |
|
609 |
||
610 |
// Also compile the class initializer if it exists |
|
611 |
HotSpotResolvedJavaMethod clinit = (HotSpotResolvedJavaMethod) metaAccess.lookupJavaType(javaClass).getClassInitializer(); |
|
612 |
if (clinit != null && canBeCompiled(clinit, clinit.getModifiers())) { |
|
613 |
compileMethod(clinit); |
|
614 |
} |
|
615 |
} |
|
616 |
} catch (Throwable t) { |
|
617 |
if (isClassIncluded(className)) { |
|
618 |
println("CompileTheWorld (%d) : Skipping %s %s", classFileCounter, className, t.toString()); |
|
619 |
printStackTrace(t); |
|
620 |
} |
|
621 |
} |
|
622 |
} |
|
623 |
cpe.close(); |
|
624 |
} |
|
625 |
} finally { |
|
626 |
currentOptions = savedOptions; |
|
627 |
} |
|
628 |
||
629 |
if (!running) { |
|
630 |
startThreads(); |
|
631 |
} |
|
632 |
int wakeups = 0; |
|
633 |
while (threadPool.getCompletedTaskCount() != threadPool.getTaskCount()) { |
|
634 |
if (wakeups % 15 == 0) { |
|
635 |
TTY.println("CompileTheWorld : Waiting for " + (threadPool.getTaskCount() - threadPool.getCompletedTaskCount()) + " compiles"); |
|
636 |
} |
|
637 |
try { |
|
638 |
threadPool.awaitTermination(1, TimeUnit.SECONDS); |
|
639 |
wakeups++; |
|
640 |
} catch (InterruptedException e) { |
|
641 |
} |
|
642 |
} |
|
643 |
threadPool = null; |
|
644 |
||
645 |
long elapsedTime = System.currentTimeMillis() - start; |
|
646 |
||
647 |
println(); |
|
648 |
if (Options.MultiThreaded.getValue(currentOptions)) { |
|
649 |
TTY.println("CompileTheWorld : Done (%d classes, %d methods, %d ms elapsed, %d ms compile time, %d bytes of memory used)", classFileCounter, compiledMethodsCounter.get(), elapsedTime, |
|
650 |
compileTime.get(), memoryUsed.get()); |
|
651 |
} else { |
|
652 |
TTY.println("CompileTheWorld : Done (%d classes, %d methods, %d ms, %d bytes of memory used)", classFileCounter, compiledMethodsCounter.get(), compileTime.get(), memoryUsed.get()); |
|
653 |
} |
|
47667 | 654 |
|
655 |
// Apart from the main thread, there should be only be daemon threads |
|
656 |
// alive now. If not, then a class initializer has probably started |
|
657 |
// a thread that could cause a deadlock while trying to exit the VM. |
|
658 |
// One known example of this is sun.tools.jconsole.OutputViewer which |
|
659 |
// spawns threads to redirect sysout and syserr. To help debug such |
|
660 |
// scenarios, the stacks of potentially problematic threads are dumped. |
|
661 |
Map<Thread, StackTraceElement[]> suspiciousThreads = new HashMap<>(); |
|
662 |
for (Map.Entry<Thread, StackTraceElement[]> e : Thread.getAllStackTraces().entrySet()) { |
|
663 |
Thread thread = e.getKey(); |
|
664 |
if (thread != Thread.currentThread() && !initialThreads.containsKey(thread) && !thread.isDaemon() && thread.isAlive()) { |
|
665 |
suspiciousThreads.put(thread, e.getValue()); |
|
666 |
} |
|
667 |
} |
|
668 |
if (!suspiciousThreads.isEmpty()) { |
|
669 |
TTY.println("--- Non-daemon threads started during CTW ---"); |
|
670 |
for (Map.Entry<Thread, StackTraceElement[]> e : suspiciousThreads.entrySet()) { |
|
671 |
Thread thread = e.getKey(); |
|
672 |
if (thread.isAlive()) { |
|
673 |
TTY.println(thread.toString() + " " + thread.getState()); |
|
674 |
for (StackTraceElement ste : e.getValue()) { |
|
675 |
TTY.println("\tat " + ste); |
|
676 |
} |
|
677 |
} |
|
678 |
} |
|
679 |
TTY.println("---------------------------------------------"); |
|
680 |
} |
|
46393 | 681 |
} |
682 |
||
683 |
private synchronized void startThreads() { |
|
684 |
running = true; |
|
685 |
// Wake up any waiting threads |
|
686 |
notifyAll(); |
|
687 |
} |
|
688 |
||
689 |
private synchronized void waitToRun() { |
|
690 |
while (!running) { |
|
691 |
try { |
|
692 |
wait(); |
|
693 |
} catch (InterruptedException e) { |
|
694 |
} |
|
695 |
} |
|
696 |
} |
|
697 |
||
698 |
@SuppressWarnings("try") |
|
699 |
private void compileMethod(HotSpotResolvedJavaMethod method) throws InterruptedException, ExecutionException { |
|
700 |
if (methodFilters != null && !MethodFilter.matches(methodFilters, method)) { |
|
701 |
return; |
|
702 |
} |
|
703 |
if (excludeMethodFilters != null && MethodFilter.matches(excludeMethodFilters, method)) { |
|
704 |
return; |
|
705 |
} |
|
706 |
Future<?> task = threadPool.submit(new Runnable() { |
|
707 |
@Override |
|
708 |
public void run() { |
|
709 |
waitToRun(); |
|
710 |
OptionValues savedOptions = currentOptions; |
|
46459 | 711 |
currentOptions = new OptionValues(compilationOptions); |
46393 | 712 |
try { |
713 |
compileMethod(method, classFileCounter); |
|
714 |
} finally { |
|
715 |
currentOptions = savedOptions; |
|
716 |
} |
|
717 |
} |
|
718 |
}); |
|
719 |
if (threadPool.getCorePoolSize() == 1) { |
|
720 |
task.get(); |
|
721 |
} |
|
722 |
} |
|
723 |
||
724 |
/** |
|
725 |
* Compiles a method and gathers some statistics. |
|
726 |
*/ |
|
727 |
private void compileMethod(HotSpotResolvedJavaMethod method, int counter) { |
|
728 |
try { |
|
729 |
long start = System.currentTimeMillis(); |
|
46640 | 730 |
long allocatedAtStart = getCurrentThreadAllocatedBytes(); |
46393 | 731 |
int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI; |
732 |
HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, entryBCI, 0L); |
|
733 |
// For more stable CTW execution, disable use of profiling information |
|
734 |
boolean useProfilingInfo = false; |
|
735 |
boolean installAsDefault = false; |
|
736 |
CompilationTask task = new CompilationTask(jvmciRuntime, compiler, request, useProfilingInfo, installAsDefault, currentOptions); |
|
737 |
task.runCompilation(); |
|
738 |
||
739 |
// Invalidate the generated code so the code cache doesn't fill up |
|
740 |
HotSpotInstalledCode installedCode = task.getInstalledCode(); |
|
741 |
if (installedCode != null) { |
|
742 |
installedCode.invalidate(); |
|
743 |
} |
|
744 |
||
46640 | 745 |
memoryUsed.getAndAdd(getCurrentThreadAllocatedBytes() - allocatedAtStart); |
46393 | 746 |
compileTime.getAndAdd(System.currentTimeMillis() - start); |
747 |
compiledMethodsCounter.incrementAndGet(); |
|
748 |
} catch (Throwable t) { |
|
749 |
// Catch everything and print a message |
|
750 |
println("CompileTheWorld (%d) : Error compiling method: %s", counter, method.format("%H.%n(%p):%r")); |
|
751 |
printStackTrace(t); |
|
752 |
} |
|
753 |
} |
|
754 |
||
755 |
/** |
|
756 |
* Determines if a method should be compiled (Cf. CompilationPolicy::can_be_compiled). |
|
757 |
* |
|
758 |
* @return true if it can be compiled, false otherwise |
|
759 |
*/ |
|
760 |
private boolean canBeCompiled(HotSpotResolvedJavaMethod javaMethod, int modifiers) { |
|
761 |
if (Modifier.isAbstract(modifiers) || Modifier.isNative(modifiers)) { |
|
762 |
return false; |
|
763 |
} |
|
764 |
GraalHotSpotVMConfig c = compiler.getGraalRuntime().getVMConfig(); |
|
765 |
if (c.dontCompileHugeMethods && javaMethod.getCodeSize() > c.hugeMethodLimit) { |
|
766 |
println(verbose || methodFilters != null, |
|
767 |
String.format("CompileTheWorld (%d) : Skipping huge method %s (use -XX:-DontCompileHugeMethods or -XX:HugeMethodLimit=%d to include it)", classFileCounter, |
|
768 |
javaMethod.format("%H.%n(%p):%r"), |
|
769 |
javaMethod.getCodeSize())); |
|
770 |
return false; |
|
771 |
} |
|
772 |
// Allow use of -XX:CompileCommand=dontinline to exclude problematic methods |
|
773 |
if (!javaMethod.canBeInlined()) { |
|
774 |
return false; |
|
775 |
} |
|
776 |
// Skip @Snippets for now |
|
777 |
for (Annotation annotation : javaMethod.getAnnotations()) { |
|
778 |
if (annotation.annotationType().equals(Snippet.class)) { |
|
779 |
return false; |
|
780 |
} |
|
781 |
} |
|
782 |
return true; |
|
783 |
} |
|
784 |
||
785 |
static class Options { |
|
786 |
// @formatter:off |
|
787 |
public static final OptionKey<Boolean> Help = new OptionKey<>(false); |
|
788 |
public static final OptionKey<String> Classpath = new OptionKey<>(CompileTheWorld.SUN_BOOT_CLASS_PATH); |
|
789 |
public static final OptionKey<Boolean> Verbose = new OptionKey<>(true); |
|
790 |
/** |
|
791 |
* Ignore Graal classes by default to avoid problems associated with compiling |
|
792 |
* snippets and method substitutions. |
|
793 |
*/ |
|
794 |
public static final OptionKey<String> LimitModules = new OptionKey<>("~jdk.internal.vm.compiler"); |
|
795 |
public static final OptionKey<Integer> Iterations = new OptionKey<>(1); |
|
796 |
public static final OptionKey<String> MethodFilter = new OptionKey<>(null); |
|
797 |
public static final OptionKey<String> ExcludeMethodFilter = new OptionKey<>(null); |
|
798 |
public static final OptionKey<Integer> StartAt = new OptionKey<>(1); |
|
799 |
public static final OptionKey<Integer> StopAt = new OptionKey<>(Integer.MAX_VALUE); |
|
800 |
public static final OptionKey<String> Config = new OptionKey<>(null); |
|
801 |
public static final OptionKey<Boolean> MultiThreaded = new OptionKey<>(false); |
|
802 |
public static final OptionKey<Integer> Threads = new OptionKey<>(0); |
|
803 |
||
804 |
static final ReflectionOptionDescriptors DESCRIPTORS = new ReflectionOptionDescriptors(Options.class, |
|
805 |
"Help", "List options and their help messages and then exit.", |
|
806 |
"Classpath", "Class path denoting methods to compile. Default is to compile boot classes.", |
|
807 |
"Verbose", "Verbose operation.", |
|
808 |
"LimitModules", "Comma separated list of module names to which compilation should be limited. " + |
|
809 |
"Module names can be prefixed with \"~\" to exclude the named module.", |
|
810 |
"Iterations", "The number of iterations to perform.", |
|
811 |
"MethodFilter", "Only compile methods matching this filter.", |
|
812 |
"ExcludeMethodFilter", "Exclude methods matching this filter from compilation.", |
|
813 |
"StartAt", "First class to consider for compilation.", |
|
814 |
"StopAt", "Last class to consider for compilation.", |
|
815 |
"Config", "Option value overrides to use during compile the world. For example, " + |
|
816 |
"to disable inlining and partial escape analysis specify 'PartialEscapeAnalysis=false Inline=false'. " + |
|
817 |
"The format for each option is the same as on the command line just without the '-Dgraal.' prefix.", |
|
818 |
"MultiThreaded", "Run using multiple threads for compilation.", |
|
819 |
"Threads", "Number of threads to use for multithreaded execution. Defaults to Runtime.getRuntime().availableProcessors()."); |
|
820 |
// @formatter:on |
|
821 |
} |
|
822 |
||
823 |
public static OptionValues loadOptions(OptionValues initialValues) { |
|
824 |
EconomicMap<OptionKey<?>, Object> values = OptionValues.newOptionMap(); |
|
825 |
List<OptionDescriptors> loader = singletonList(DESCRIPTORS); |
|
826 |
OptionsParser.parseOptions(extractEntries(System.getProperties(), "CompileTheWorld.", true), values, loader); |
|
827 |
OptionValues options = new OptionValues(initialValues, values); |
|
828 |
if (Options.Help.getValue(options)) { |
|
829 |
options.printHelp(loader, System.out, "CompileTheWorld."); |
|
830 |
System.exit(0); |
|
831 |
} |
|
832 |
return options; |
|
833 |
} |
|
834 |
||
835 |
public static void main(String[] args) throws Throwable { |
|
836 |
HotSpotJVMCIRuntime jvmciRuntime = HotSpotJVMCIRuntime.runtime(); |
|
837 |
HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) jvmciRuntime.getCompiler(); |
|
838 |
HotSpotGraalRuntimeProvider graalRuntime = compiler.getGraalRuntime(); |
|
839 |
HotSpotCodeCacheProvider codeCache = graalRuntime.getHostProviders().getCodeCache(); |
|
840 |
OptionValues options = loadOptions(graalRuntime.getOptions()); |
|
841 |
||
842 |
int iterations = Options.Iterations.getValue(options); |
|
843 |
for (int i = 0; i < iterations; i++) { |
|
844 |
codeCache.resetCompilationStatistics(); |
|
845 |
TTY.println("CompileTheWorld : iteration " + i); |
|
846 |
||
847 |
CompileTheWorld ctw = new CompileTheWorld(jvmciRuntime, compiler, options); |
|
848 |
ctw.compile(); |
|
849 |
} |
|
850 |
// This is required as non-daemon threads can be started by class initializers |
|
851 |
System.exit(0); |
|
852 |
} |
|
853 |
} |