author | iveresov |
Thu, 22 Dec 2016 17:26:02 -0800 | |
changeset 43405 | 1fab8e6866d7 |
parent 42650 | 1f304d0c888b |
child 43464 | f38fde4a6b52 |
permissions | -rw-r--r-- |
42650 | 1 |
/* |
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
package jdk.tools.jaotc; |
|
25 |
||
26 |
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; |
|
27 |
import static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode; |
|
28 |
import static org.graalvm.compiler.hotspot.meta.HotSpotAOTProfilingPlugin.Options.TieredAOT; |
|
29 |
||
30 |
import java.io.BufferedReader; |
|
31 |
import java.io.File; |
|
32 |
import java.io.FileNotFoundException; |
|
33 |
import java.io.FileReader; |
|
34 |
import java.io.FileWriter; |
|
35 |
import java.io.IOException; |
|
36 |
import java.io.InputStream; |
|
37 |
import java.io.InputStreamReader; |
|
38 |
import java.io.PrintWriter; |
|
39 |
import java.lang.management.ManagementFactory; |
|
40 |
import java.lang.management.MemoryUsage; |
|
41 |
import java.nio.file.Path; |
|
42 |
import java.nio.file.Paths; |
|
43 |
import java.text.MessageFormat; |
|
44 |
import java.util.ArrayList; |
|
45 |
import java.util.Date; |
|
46 |
import java.util.LinkedList; |
|
47 |
import java.util.List; |
|
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
48 |
import java.util.ListIterator; |
42650 | 49 |
import java.util.Set; |
50 |
import java.util.stream.Stream; |
|
51 |
||
52 |
import jdk.tools.jaotc.binformat.BinaryContainer; |
|
53 |
import jdk.tools.jaotc.binformat.ByteContainer; |
|
54 |
import jdk.tools.jaotc.collect.ClassCollector; |
|
55 |
import jdk.tools.jaotc.utils.Timer; |
|
56 |
||
57 |
import org.graalvm.compiler.api.runtime.GraalJVMCICompiler; |
|
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
58 |
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; |
42650 | 59 |
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider; |
60 |
import org.graalvm.compiler.hotspot.HotSpotHostBackend; |
|
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
61 |
import org.graalvm.compiler.java.GraphBuilderPhase; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
62 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
63 |
import org.graalvm.compiler.phases.BasePhase; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
64 |
import org.graalvm.compiler.phases.PhaseSuite; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
65 |
import org.graalvm.compiler.phases.tiers.HighTierContext; |
42650 | 66 |
import org.graalvm.compiler.runtime.RuntimeProvider; |
67 |
||
68 |
import jdk.vm.ci.meta.MetaAccessProvider; |
|
69 |
import jdk.vm.ci.meta.ResolvedJavaMethod; |
|
70 |
import jdk.vm.ci.meta.ResolvedJavaType; |
|
71 |
import jdk.vm.ci.runtime.JVMCI; |
|
72 |
||
73 |
public class Main implements LogPrinter { |
|
74 |
static { |
|
75 |
GeneratePIC.setValue(true); |
|
76 |
ImmutableCode.setValue(true); |
|
77 |
} |
|
78 |
||
79 |
static class BadArgs extends Exception { |
|
80 |
private static final long serialVersionUID = 1L; |
|
81 |
final String key; |
|
82 |
final Object[] args; |
|
83 |
boolean showUsage; |
|
84 |
||
85 |
BadArgs(String key, Object... args) { |
|
86 |
super(MessageFormat.format(key, args)); |
|
87 |
this.key = key; |
|
88 |
this.args = args; |
|
89 |
} |
|
90 |
||
91 |
BadArgs showUsage(boolean b) { |
|
92 |
showUsage = b; |
|
93 |
return this; |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
abstract static class Option { |
|
98 |
final String help; |
|
99 |
final boolean hasArg; |
|
100 |
final String[] aliases; |
|
101 |
||
102 |
Option(String help, boolean hasArg, String... aliases) { |
|
103 |
this.help = help; |
|
104 |
this.hasArg = hasArg; |
|
105 |
this.aliases = aliases; |
|
106 |
} |
|
107 |
||
108 |
boolean isHidden() { |
|
109 |
return false; |
|
110 |
} |
|
111 |
||
112 |
boolean matches(String opt) { |
|
113 |
for (String a : aliases) { |
|
114 |
if (a.equals(opt)) { |
|
115 |
return true; |
|
116 |
} else if (opt.startsWith("--") && hasArg && opt.startsWith(a + "=")) { |
|
117 |
return true; |
|
118 |
} |
|
119 |
} |
|
120 |
return false; |
|
121 |
} |
|
122 |
||
123 |
boolean ignoreRest() { |
|
124 |
return false; |
|
125 |
} |
|
126 |
||
127 |
abstract void process(Main task, String opt, String arg) throws BadArgs; |
|
128 |
} |
|
129 |
||
130 |
static Option[] recognizedOptions = {new Option(" --module <name> Module to compile", true, "--module") { |
|
131 |
@Override |
|
132 |
void process(Main task, String opt, String arg) { |
|
133 |
task.options.module = arg; |
|
134 |
} |
|
135 |
}, new Option(" --module-path <path> Specify where to find module to compile", true, "--module-path") { |
|
136 |
@Override |
|
137 |
void process(Main task, String opt, String arg) { |
|
138 |
task.options.modulepath = arg; |
|
139 |
} |
|
140 |
}, new Option(" --output <file> Output file name", true, "--output") { |
|
141 |
@Override |
|
142 |
void process(Main task, String opt, String arg) { |
|
143 |
String name = arg; |
|
144 |
if (name.endsWith(".so")) { |
|
145 |
name = name.substring(0, name.length() - ".so".length()); |
|
146 |
} |
|
147 |
task.options.outputName = name; |
|
148 |
} |
|
149 |
}, new Option(" --compile-commands <file> Name of file with compile commands", true, "--compile-commands") { |
|
150 |
@Override |
|
151 |
void process(Main task, String opt, String arg) { |
|
152 |
task.options.methodList = arg; |
|
153 |
} |
|
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
154 |
}, new Option(" --compile-for-tiered Generate profiling code for tiered compilation", false, "--compile-for-tiered") { |
42650 | 155 |
@Override |
156 |
void process(Main task, String opt, String arg) { |
|
157 |
TieredAOT.setValue(true); |
|
158 |
} |
|
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
159 |
}, new Option(" --compile-with-assertions Compile assertions", false, "--compile-with-assertions") { |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
160 |
@Override |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
161 |
void process(Main task, String opt, String arg) { |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
162 |
task.options.compileWithAssertions = true; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
163 |
} |
42650 | 164 |
}, new Option(" --classpath <path> Specify where to find user class files", true, "--classpath", "--class-path") { |
165 |
@Override |
|
166 |
void process(Main task, String opt, String arg) { |
|
167 |
task.options.classpath = arg; |
|
168 |
} |
|
169 |
}, new Option(" --threads <number> Number of compilation threads to be used", true, "--threads") { |
|
170 |
@Override |
|
171 |
void process(Main task, String opt, String arg) { |
|
172 |
int threads = Integer.parseInt(arg); |
|
173 |
final int available = Runtime.getRuntime().availableProcessors(); |
|
174 |
if (threads <= 0) { |
|
175 |
task.warning("invalid number of threads specified: {0}, using: {1}", threads, available); |
|
176 |
threads = available; |
|
177 |
} |
|
178 |
if (threads > available) { |
|
179 |
task.warning("too many threads specified: {0}, limiting to: {1}", threads, available); |
|
180 |
} |
|
181 |
task.options.threads = Integer.min(threads, available); |
|
182 |
} |
|
183 |
}, new Option(" --ignore-errors Ignores all exceptions thrown during class loading", false, "--ignore-errors") { |
|
184 |
@Override |
|
185 |
void process(Main task, String opt, String arg) { |
|
186 |
task.options.ignoreClassLoadingErrors = true; |
|
187 |
} |
|
188 |
}, new Option(" --exit-on-error Exit on compilation errors", false, "--exit-on-error") { |
|
189 |
@Override |
|
190 |
void process(Main task, String opt, String arg) { |
|
191 |
task.options.exitOnError = true; |
|
192 |
} |
|
193 |
}, new Option(" --info Print information during compilation", false, "--info") { |
|
194 |
@Override |
|
195 |
void process(Main task, String opt, String arg) throws BadArgs { |
|
196 |
task.options.info = true; |
|
197 |
} |
|
198 |
}, new Option(" --verbose Print verbose information", false, "--verbose") { |
|
199 |
@Override |
|
200 |
void process(Main task, String opt, String arg) throws BadArgs { |
|
201 |
task.options.info = true; |
|
202 |
task.options.verbose = true; |
|
203 |
} |
|
204 |
}, new Option(" --debug Print debug information", false, "--debug") { |
|
205 |
@Override |
|
206 |
void process(Main task, String opt, String arg) throws BadArgs { |
|
207 |
task.options.info = true; |
|
208 |
task.options.verbose = true; |
|
209 |
task.options.debug = true; |
|
210 |
} |
|
211 |
}, new Option(" --help Print this usage message", false, "--help") { |
|
212 |
@Override |
|
213 |
void process(Main task, String opt, String arg) { |
|
214 |
task.options.help = true; |
|
215 |
} |
|
216 |
}, new Option(" --version Version information", false, "--version") { |
|
217 |
@Override |
|
218 |
void process(Main task, String opt, String arg) { |
|
219 |
task.options.version = true; |
|
220 |
} |
|
221 |
}, new Option(" -J<flag> Pass <flag> directly to the runtime system", false, "-J") { |
|
222 |
@Override |
|
223 |
void process(Main task, String opt, String arg) { |
|
224 |
} |
|
225 |
}}; |
|
226 |
||
227 |
public static class Options { |
|
228 |
public List<String> files = new LinkedList<>(); |
|
229 |
public String module = null; |
|
230 |
public String modulepath = "modules"; |
|
231 |
public String outputName = "unnamed"; |
|
232 |
public String methodList; |
|
233 |
public String classpath = "."; |
|
234 |
||
235 |
/** |
|
236 |
* We don't see scaling beyond 16 threads. |
|
237 |
*/ |
|
238 |
private static final int COMPILER_THREADS = 16; |
|
239 |
||
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
240 |
public int threads = Integer.min(COMPILER_THREADS, Runtime.getRuntime().availableProcessors()); |
42650 | 241 |
|
242 |
public boolean ignoreClassLoadingErrors; |
|
243 |
public boolean exitOnError; |
|
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
244 |
public boolean info; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
245 |
public boolean verbose; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
246 |
public boolean debug; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
247 |
public boolean help; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
248 |
public boolean version; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
249 |
public boolean compileWithAssertions; |
42650 | 250 |
} |
251 |
||
252 |
/* package */final Options options = new Options(); |
|
253 |
||
254 |
/** |
|
255 |
* Logfile. |
|
256 |
*/ |
|
257 |
private static FileWriter logFile = null; |
|
258 |
||
259 |
private static final int EXIT_OK = 0; // No errors. |
|
260 |
private static final int EXIT_CMDERR = 2; // Bad command-line arguments and/or switches. |
|
261 |
private static final int EXIT_ABNORMAL = 4; // Terminated abnormally. |
|
262 |
||
263 |
private static final String PROGNAME = "jaotc"; |
|
264 |
||
265 |
private static final String JVM_VERSION = System.getProperty("java.runtime.version"); |
|
266 |
||
267 |
public static void main(String[] args) throws Exception { |
|
268 |
Main t = new Main(); |
|
269 |
final int exitCode = t.run(args); |
|
270 |
System.exit(exitCode); |
|
271 |
} |
|
272 |
||
273 |
private int run(String[] args) { |
|
274 |
if (log == null) { |
|
275 |
log = new PrintWriter(System.out); |
|
276 |
} |
|
277 |
||
278 |
try { |
|
279 |
handleOptions(args); |
|
280 |
if (options.help) { |
|
281 |
showHelp(); |
|
282 |
return EXIT_OK; |
|
283 |
} |
|
284 |
if (options.version) { |
|
285 |
showVersion(); |
|
286 |
return EXIT_OK; |
|
287 |
} |
|
288 |
||
289 |
printlnInfo("Compiling " + options.outputName + "..."); |
|
290 |
final long start = System.currentTimeMillis(); |
|
291 |
run(); |
|
292 |
final long end = System.currentTimeMillis(); |
|
293 |
printlnInfo("Total time: " + (end - start) + " ms"); |
|
294 |
||
295 |
return EXIT_OK; |
|
296 |
} catch (BadArgs e) { |
|
297 |
reportError(e.key, e.args); |
|
298 |
if (e.showUsage) { |
|
299 |
showUsage(); |
|
300 |
} |
|
301 |
return EXIT_CMDERR; |
|
302 |
} catch (Exception e) { |
|
303 |
e.printStackTrace(); |
|
304 |
return EXIT_ABNORMAL; |
|
305 |
} finally { |
|
306 |
log.flush(); |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
private static String humanReadableByteCount(long bytes) { |
|
311 |
int unit = 1024; |
|
312 |
||
313 |
if (bytes < unit) { |
|
314 |
return bytes + " B"; |
|
315 |
} |
|
316 |
||
317 |
int exp = (int) (Math.log(bytes) / Math.log(unit)); |
|
318 |
char pre = "KMGTPE".charAt(exp - 1); |
|
319 |
return String.format("%.1f %cB", bytes / Math.pow(unit, exp), pre); |
|
320 |
} |
|
321 |
||
322 |
void printMemoryUsage() { |
|
323 |
if (options.verbose) { |
|
324 |
MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); |
|
325 |
float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted(); |
|
326 |
log.format(" [used: %-7s, comm: %-7s, freeRatio ~= %.1f%%]", |
|
327 |
humanReadableByteCount(memusage.getUsed()), |
|
328 |
humanReadableByteCount(memusage.getCommitted()), |
|
329 |
freeratio * 100); |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
@SuppressWarnings("try") |
|
334 |
private void run() throws Exception { |
|
335 |
openLog(); |
|
336 |
||
337 |
try { |
|
338 |
CompilationSpec compilationRestrictions = collectSpecifiedMethods(); |
|
339 |
||
340 |
Set<Class<?>> classesToCompile; |
|
341 |
||
342 |
try (Timer t = new Timer(this, "")) { |
|
343 |
ClassCollector collector = new ClassCollector(this.options, this); |
|
344 |
classesToCompile = collector.collectClassesToCompile(); |
|
345 |
printInfo(classesToCompile.size() + " classes found"); |
|
346 |
} |
|
347 |
||
348 |
GraalJVMCICompiler graalCompiler = (GraalJVMCICompiler) JVMCI.getRuntime().getCompiler(); |
|
349 |
HotSpotGraalRuntimeProvider runtime = (HotSpotGraalRuntimeProvider) graalCompiler.getGraalRuntime(); |
|
350 |
HotSpotHostBackend backend = (HotSpotHostBackend) runtime.getCapability(RuntimeProvider.class).getHostBackend(); |
|
351 |
MetaAccessProvider metaAccess = backend.getProviders().getMetaAccess(); |
|
352 |
GraalFilters filters = new GraalFilters(metaAccess); |
|
353 |
||
354 |
List<AOTCompiledClass> classes; |
|
355 |
||
356 |
try (Timer t = new Timer(this, "")) { |
|
357 |
classes = collectMethodsToCompile(classesToCompile, compilationRestrictions, filters, metaAccess); |
|
358 |
} |
|
359 |
||
360 |
// Free memory! |
|
361 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
362 |
printMemoryUsage(); |
|
363 |
compilationRestrictions = null; |
|
364 |
classesToCompile = null; |
|
365 |
System.gc(); |
|
366 |
} |
|
367 |
||
368 |
AOTBackend aotBackend = new AOTBackend(this, backend, filters); |
|
369 |
AOTCompiler compiler = new AOTCompiler(this, aotBackend, options.threads); |
|
370 |
classes = compiler.compileClasses(classes); |
|
371 |
||
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
372 |
GraalHotSpotVMConfig graalHotSpotVMConfig = runtime.getVMConfig(); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
373 |
PhaseSuite<HighTierContext> graphBuilderSuite = aotBackend.getGraphBuilderSuite(); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
374 |
ListIterator<BasePhase<? super HighTierContext>> iterator = graphBuilderSuite.findPhase(GraphBuilderPhase.class); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
375 |
GraphBuilderConfiguration graphBuilderConfig = ((GraphBuilderPhase) iterator.previous()).getGraphBuilderConfig(); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
376 |
|
42650 | 377 |
// Free memory! |
378 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
379 |
printMemoryUsage(); |
|
380 |
aotBackend = null; |
|
381 |
compiler = null; |
|
382 |
System.gc(); |
|
383 |
} |
|
384 |
||
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
385 |
BinaryContainer binaryContainer = new BinaryContainer(graalHotSpotVMConfig, graphBuilderConfig, JVM_VERSION); |
42650 | 386 |
DataBuilder dataBuilder = new DataBuilder(this, backend, classes, binaryContainer); |
387 |
dataBuilder.prepareData(); |
|
388 |
||
389 |
// Print information about section sizes |
|
390 |
printContainerInfo(binaryContainer.getHeaderContainer().getContainer()); |
|
391 |
printContainerInfo(binaryContainer.getConfigContainer()); |
|
392 |
printContainerInfo(binaryContainer.getKlassesOffsetsContainer()); |
|
393 |
printContainerInfo(binaryContainer.getMethodsOffsetsContainer()); |
|
394 |
printContainerInfo(binaryContainer.getKlassesDependenciesContainer()); |
|
395 |
printContainerInfo(binaryContainer.getStubsOffsetsContainer()); |
|
396 |
printContainerInfo(binaryContainer.getMethodMetadataContainer()); |
|
397 |
printContainerInfo(binaryContainer.getCodeContainer()); |
|
398 |
printContainerInfo(binaryContainer.getCodeSegmentsContainer()); |
|
399 |
printContainerInfo(binaryContainer.getConstantDataContainer()); |
|
400 |
printContainerInfo(binaryContainer.getMetaspaceGotContainer()); |
|
401 |
printContainerInfo(binaryContainer.getMetadataGotContainer()); |
|
402 |
printContainerInfo(binaryContainer.getMethodStateContainer()); |
|
403 |
printContainerInfo(binaryContainer.getOopGotContainer()); |
|
404 |
printContainerInfo(binaryContainer.getMetaspaceNamesContainer()); |
|
405 |
||
406 |
// Free memory! |
|
407 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
408 |
printMemoryUsage(); |
|
409 |
backend = null; |
|
410 |
for (AOTCompiledClass aotCompClass : classes) { |
|
411 |
aotCompClass.clear(); |
|
412 |
} |
|
413 |
classes.clear(); |
|
414 |
classes = null; |
|
415 |
dataBuilder = null; |
|
416 |
binaryContainer.freeMemory(); |
|
417 |
System.gc(); |
|
418 |
} |
|
419 |
||
420 |
String objectFileName = options.outputName + ".o"; |
|
421 |
String libraryFileName = options.outputName + ".so"; |
|
422 |
||
423 |
try (Timer t = new Timer(this, "Creating binary: " + objectFileName)) { |
|
424 |
binaryContainer.createBinary(objectFileName, JVM_VERSION); |
|
425 |
} |
|
426 |
||
427 |
// Free memory! |
|
428 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
429 |
printMemoryUsage(); |
|
430 |
binaryContainer = null; |
|
431 |
System.gc(); |
|
432 |
} |
|
433 |
||
434 |
try (Timer t = new Timer(this, "Creating shared library: " + libraryFileName)) { |
|
435 |
Process p = Runtime.getRuntime().exec("ld -shared -z noexecstack -o " + libraryFileName + " " + objectFileName); |
|
436 |
final int exitCode = p.waitFor(); |
|
437 |
if (exitCode != 0) { |
|
438 |
InputStream stderr = p.getErrorStream(); |
|
439 |
BufferedReader br = new BufferedReader(new InputStreamReader(stderr)); |
|
440 |
Stream<String> lines = br.lines(); |
|
441 |
StringBuilder sb = new StringBuilder(); |
|
442 |
lines.iterator().forEachRemaining(e -> sb.append(e)); |
|
443 |
throw new InternalError(sb.toString()); |
|
444 |
} |
|
445 |
File objFile = new File(objectFileName); |
|
446 |
if (objFile.exists()) { |
|
447 |
if (!objFile.delete()) { |
|
448 |
throw new InternalError("Failed to delete " + objectFileName + " file"); |
|
449 |
} |
|
450 |
} |
|
451 |
// Make non-executable for all. |
|
452 |
File libFile = new File(libraryFileName); |
|
453 |
if (libFile.exists()) { |
|
454 |
if (!libFile.setExecutable(false, false)) { |
|
455 |
throw new InternalError("Failed to change attribute for " + libraryFileName + " file"); |
|
456 |
} |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
printVerbose("Final memory "); |
|
461 |
printMemoryUsage(); |
|
462 |
printlnVerbose(""); |
|
463 |
||
464 |
} finally { |
|
465 |
closeLog(); |
|
466 |
} |
|
467 |
} |
|
468 |
||
469 |
private void addMethods(AOTCompiledClass aotClass, ResolvedJavaMethod[] methods, CompilationSpec compilationRestrictions, GraalFilters filters) { |
|
470 |
for (ResolvedJavaMethod m : methods) { |
|
471 |
addMethod(aotClass, m, compilationRestrictions, filters); |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
private void addMethod(AOTCompiledClass aotClass, ResolvedJavaMethod method, CompilationSpec compilationRestrictions, GraalFilters filters) { |
|
476 |
// Don't compile native or abstract methods. |
|
477 |
if (!method.hasBytecodes()) { |
|
478 |
return; |
|
479 |
} |
|
480 |
if (!compilationRestrictions.shouldCompileMethod(method)) { |
|
481 |
return; |
|
482 |
} |
|
483 |
if (!filters.shouldCompileMethod(method)) { |
|
484 |
return; |
|
485 |
} |
|
486 |
||
487 |
aotClass.addMethod(method); |
|
488 |
printlnVerbose(" added " + method.getName() + method.getSignature().toMethodDescriptor()); |
|
489 |
} |
|
490 |
||
491 |
private void printContainerInfo(ByteContainer container) { |
|
492 |
printlnVerbose(container.getContainerName() + ": " + container.getByteStreamSize() + " bytes"); |
|
493 |
} |
|
494 |
||
495 |
PrintWriter log; |
|
496 |
||
497 |
private void handleOptions(String[] args) throws BadArgs { |
|
498 |
if (args.length == 0) { |
|
499 |
options.help = true; |
|
500 |
return; |
|
501 |
} |
|
502 |
||
503 |
// Make checkstyle happy. |
|
504 |
int i = 0; |
|
505 |
for (; i < args.length; i++) { |
|
506 |
String arg = args[i]; |
|
507 |
||
508 |
if (arg.charAt(0) == '-') { |
|
509 |
Option option = getOption(arg); |
|
510 |
String param = null; |
|
511 |
||
512 |
if (option.hasArg) { |
|
513 |
if (arg.startsWith("--") && arg.indexOf('=') > 0) { |
|
514 |
param = arg.substring(arg.indexOf('=') + 1, arg.length()); |
|
515 |
} else if (i + 1 < args.length) { |
|
516 |
param = args[++i]; |
|
517 |
} |
|
518 |
||
519 |
if (param == null || param.isEmpty() || param.charAt(0) == '-') { |
|
520 |
throw new BadArgs("missing argument for option: {0}", arg).showUsage(true); |
|
521 |
} |
|
522 |
} |
|
523 |
||
524 |
option.process(this, arg, param); |
|
525 |
||
526 |
if (option.ignoreRest()) { |
|
527 |
break; |
|
528 |
} |
|
529 |
} else { |
|
530 |
options.files.add(arg); |
|
531 |
} |
|
532 |
} |
|
533 |
} |
|
534 |
||
535 |
private static Option getOption(String name) throws BadArgs { |
|
536 |
for (Option o : recognizedOptions) { |
|
537 |
if (o.matches(name)) { |
|
538 |
return o; |
|
539 |
} |
|
540 |
} |
|
541 |
throw new BadArgs("unknown option: {0}", name).showUsage(true); |
|
542 |
} |
|
543 |
||
544 |
public void printInfo(String message) { |
|
545 |
if (options.info) { |
|
546 |
log.print(message); |
|
547 |
log.flush(); |
|
548 |
} |
|
549 |
} |
|
550 |
||
551 |
public void printlnInfo(String message) { |
|
552 |
if (options.info) { |
|
553 |
log.println(message); |
|
554 |
log.flush(); |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
public void printVerbose(String message) { |
|
559 |
if (options.verbose) { |
|
560 |
log.print(message); |
|
561 |
log.flush(); |
|
562 |
} |
|
563 |
} |
|
564 |
||
565 |
public void printlnVerbose(String message) { |
|
566 |
if (options.verbose) { |
|
567 |
log.println(message); |
|
568 |
log.flush(); |
|
569 |
} |
|
570 |
} |
|
571 |
||
572 |
public void printDebug(String message) { |
|
573 |
if (options.debug) { |
|
574 |
log.print(message); |
|
575 |
log.flush(); |
|
576 |
} |
|
577 |
} |
|
578 |
||
579 |
public void printlnDebug(String message) { |
|
580 |
if (options.debug) { |
|
581 |
log.println(message); |
|
582 |
log.flush(); |
|
583 |
} |
|
584 |
} |
|
585 |
||
586 |
public void printError(String message) { |
|
587 |
log.println("Error: " + message); |
|
588 |
log.flush(); |
|
589 |
} |
|
590 |
||
591 |
private void reportError(String key, Object... args) { |
|
592 |
printError(MessageFormat.format(key, args)); |
|
593 |
} |
|
594 |
||
595 |
private void warning(String key, Object... args) { |
|
596 |
log.println("Warning: " + MessageFormat.format(key, args)); |
|
597 |
log.flush(); |
|
598 |
} |
|
599 |
||
600 |
private void showUsage() { |
|
601 |
log.println("Usage: " + PROGNAME + " <options> list..."); |
|
602 |
log.println("use --help for a list of possible options"); |
|
603 |
} |
|
604 |
||
605 |
private void showHelp() { |
|
606 |
log.println("Usage: " + PROGNAME + " <options> <--module name> | <list...>"); |
|
607 |
log.println(); |
|
608 |
log.println(" list A list of class files, jar files or directories which"); |
|
609 |
log.println(" contains class files."); |
|
610 |
log.println(); |
|
611 |
log.println("where possible options include:"); |
|
612 |
for (Option o : recognizedOptions) { |
|
613 |
String name = o.aliases[0].substring(1); // there must always be at least one name |
|
614 |
name = name.charAt(0) == '-' ? name.substring(1) : name; |
|
615 |
if (o.isHidden() || name.equals("h")) { |
|
616 |
continue; |
|
617 |
} |
|
618 |
log.println(o.help); |
|
619 |
} |
|
620 |
} |
|
621 |
||
622 |
private void showVersion() { |
|
623 |
log.println(PROGNAME + " " + JVM_VERSION); |
|
624 |
} |
|
625 |
||
626 |
/** |
|
627 |
* Collect all method we should compile. |
|
628 |
* |
|
629 |
* @return array list of AOT classes which have compiled methods. |
|
630 |
*/ |
|
631 |
private List<AOTCompiledClass> collectMethodsToCompile(Set<Class<?>> classesToCompile, CompilationSpec compilationRestrictions, GraalFilters filters, MetaAccessProvider metaAccess) { |
|
632 |
int total = 0; |
|
633 |
int count = 0; |
|
634 |
List<AOTCompiledClass> classes = new ArrayList<>(); |
|
635 |
||
636 |
for (Class<?> c : classesToCompile) { |
|
637 |
ResolvedJavaType resolvedJavaType = metaAccess.lookupJavaType(c); |
|
638 |
if (filters.shouldCompileAnyMethodInClass(resolvedJavaType)) { |
|
639 |
AOTCompiledClass aotClass = new AOTCompiledClass(resolvedJavaType); |
|
640 |
printlnVerbose(" Scanning " + c.getName()); |
|
641 |
||
642 |
// Constructors |
|
643 |
try { |
|
644 |
ResolvedJavaMethod[] ctors = resolvedJavaType.getDeclaredConstructors(); |
|
645 |
addMethods(aotClass, ctors, compilationRestrictions, filters); |
|
646 |
total += ctors.length; |
|
647 |
} catch (Throwable e) { |
|
648 |
// If we are running in JCK mode we ignore all exceptions. |
|
649 |
if (options.ignoreClassLoadingErrors) { |
|
650 |
printError(c.getName() + ": " + e); |
|
651 |
} else { |
|
652 |
throw new InternalError(e); |
|
653 |
} |
|
654 |
} |
|
655 |
||
656 |
// Methods |
|
657 |
try { |
|
658 |
ResolvedJavaMethod[] methods = resolvedJavaType.getDeclaredMethods(); |
|
659 |
addMethods(aotClass, methods, compilationRestrictions, filters); |
|
660 |
total += methods.length; |
|
661 |
} catch (Throwable e) { |
|
662 |
// If we are running in JCK mode we ignore all exceptions. |
|
663 |
if (options.ignoreClassLoadingErrors) { |
|
664 |
printError(c.getName() + ": " + e); |
|
665 |
} else { |
|
666 |
throw new InternalError(e); |
|
667 |
} |
|
668 |
} |
|
669 |
||
670 |
// Class initializer |
|
671 |
try { |
|
672 |
ResolvedJavaMethod clinit = resolvedJavaType.getClassInitializer(); |
|
673 |
if (clinit != null) { |
|
674 |
addMethod(aotClass, clinit, compilationRestrictions, filters); |
|
675 |
total++; |
|
676 |
} |
|
677 |
} catch (Throwable e) { |
|
678 |
// If we are running in JCK mode we ignore all exceptions. |
|
679 |
if (options.ignoreClassLoadingErrors) { |
|
680 |
printError(c.getName() + ": " + e); |
|
681 |
} else { |
|
682 |
throw new InternalError(e); |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
// Found any methods to compile? Add the class. |
|
687 |
if (aotClass.hasMethods()) { |
|
688 |
classes.add(aotClass); |
|
689 |
count += aotClass.getMethodCount(); |
|
690 |
} |
|
691 |
} |
|
692 |
} |
|
693 |
printInfo(total + " methods total, " + count + " methods to compile"); |
|
694 |
return classes; |
|
695 |
} |
|
696 |
||
697 |
/** |
|
698 |
* If a file with compilation limitations is specified using the java property |
|
699 |
* jdk.tools.jaotc.compile.method.list, read the file's contents and collect the restrictions. |
|
700 |
*/ |
|
701 |
private CompilationSpec collectSpecifiedMethods() { |
|
702 |
CompilationSpec compilationRestrictions = new CompilationSpec(); |
|
703 |
String methodListFileName = options.methodList; |
|
704 |
||
705 |
if (methodListFileName != null && !methodListFileName.equals("")) { |
|
706 |
try { |
|
707 |
FileReader methListFile = new FileReader(methodListFileName); |
|
708 |
BufferedReader readBuf = new BufferedReader(methListFile); |
|
709 |
String line = null; |
|
710 |
while ((line = readBuf.readLine()) != null) { |
|
711 |
String trimmedLine = line.trim(); |
|
712 |
if (!trimmedLine.startsWith("#")) { |
|
713 |
String[] components = trimmedLine.split(" "); |
|
714 |
if (components.length == 2) { |
|
715 |
String directive = components[0]; |
|
716 |
String pattern = components[1]; |
|
717 |
switch (directive) { |
|
718 |
case "compileOnly": |
|
719 |
compilationRestrictions.addCompileOnlyPattern(pattern); |
|
720 |
break; |
|
721 |
case "exclude": |
|
722 |
compilationRestrictions.addExcludePattern(pattern); |
|
723 |
break; |
|
724 |
default: |
|
725 |
System.out.println("Unrecognized command " + directive + ". Ignoring\n\t" + line + "\n encountered in " + methodListFileName); |
|
726 |
} |
|
727 |
} else { |
|
728 |
if (!trimmedLine.equals("")) { |
|
729 |
System.out.println("Ignoring malformed line:\n\t " + line + "\n"); |
|
730 |
} |
|
731 |
} |
|
732 |
} |
|
733 |
} |
|
734 |
readBuf.close(); |
|
735 |
} catch (FileNotFoundException e) { |
|
736 |
throw new InternalError("Unable to open method list file: " + methodListFileName, e); |
|
737 |
} catch (IOException e) { |
|
738 |
throw new InternalError("Unable to read method list file: " + methodListFileName, e); |
|
739 |
} |
|
740 |
} |
|
741 |
||
742 |
return compilationRestrictions; |
|
743 |
} |
|
744 |
||
745 |
private static void openLog() { |
|
746 |
int v = Integer.getInteger("jdk.tools.jaotc.logCompilation", 0); |
|
747 |
if (v == 0) { |
|
748 |
logFile = null; |
|
749 |
return; |
|
750 |
} |
|
751 |
// Create log file in current directory |
|
752 |
String fileName = "aot_compilation" + new Date().getTime() + ".log"; |
|
753 |
Path logFilePath = Paths.get("./", fileName); |
|
754 |
String logFileName = logFilePath.toString(); |
|
755 |
try { |
|
756 |
// Create file to which we do not append |
|
757 |
logFile = new FileWriter(logFileName, false); |
|
758 |
} catch (IOException e) { |
|
759 |
System.out.println("Unable to open logfile :" + logFileName + "\nNo logs will be created"); |
|
760 |
logFile = null; |
|
761 |
} |
|
762 |
} |
|
763 |
||
764 |
public static void writeLog(String str) { |
|
765 |
if (logFile != null) { |
|
766 |
try { |
|
767 |
logFile.write(str + "\n"); |
|
768 |
logFile.flush(); |
|
769 |
} catch (IOException e) { |
|
770 |
// Print to console |
|
771 |
System.out.println(str + "\n"); |
|
772 |
} |
|
773 |
} |
|
774 |
} |
|
775 |
||
776 |
public static void closeLog() { |
|
777 |
if (logFile != null) { |
|
778 |
try { |
|
779 |
logFile.close(); |
|
780 |
} catch (IOException e) { |
|
781 |
// Do nothing |
|
782 |
} |
|
783 |
} |
|
784 |
} |
|
785 |
} |