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