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 java.util.concurrent.TimeUnit; 27 import java.util.concurrent.atomic.AtomicInteger; 28 29 import org.graalvm.compiler.code.CompilationResult; 30 import org.graalvm.compiler.core.GraalCompilerOptions; 31 import org.graalvm.compiler.debug.Debug; 32 import org.graalvm.compiler.debug.DebugEnvironment; 33 import org.graalvm.compiler.debug.Management; 34 import org.graalvm.compiler.debug.TTY; 35 import org.graalvm.compiler.debug.internal.DebugScope; 36 import org.graalvm.compiler.options.OptionValues; 37 38 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 39 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 40 import jdk.vm.ci.meta.ResolvedJavaMethod; 41 import jdk.vm.ci.runtime.JVMCICompiler; 42 43 /** 44 * Represents a task in the compile queue. 45 * 46 * This class encapsulates all Graal-specific information that is used during offline AOT 47 * compilation of classes. It also defines methods that parse compilation result of Graal to create 48 * target-independent representation {@code BinaryContainer} of the intended target binary. 49 */ 50 public class AOTCompilationTask implements Runnable, Comparable<Object> { 51 52 private static final AtomicInteger ids = new AtomicInteger(); 53 54 private static final com.sun.management.ThreadMXBean threadMXBean = (com.sun.management.ThreadMXBean) Management.getThreadMXBean(); 55 56 private final Main main; 71 72 private final AOTBackend aotBackend; 73 74 /** 75 * The result of this compilation task. 76 */ 77 private CompiledMethodInfo result; 78 79 public AOTCompilationTask(Main main, OptionValues graalOptions, AOTCompiledClass holder, ResolvedJavaMethod method, AOTBackend aotBackend) { 80 this.main = main; 81 this.graalOptions = graalOptions; 82 this.id = ids.incrementAndGet(); 83 this.holder = holder; 84 this.method = method; 85 this.aotBackend = aotBackend; 86 } 87 88 /** 89 * Compile a method or a constructor. 90 */ 91 public void run() { 92 // Ensure a JVMCI runtime is initialized prior to Debug being initialized as the former 93 // may include processing command line options used by the latter. 94 HotSpotJVMCIRuntime.runtime(); 95 96 // Ensure a debug configuration for this thread is initialized 97 if (Debug.isEnabled() && DebugScope.getConfig() == null) { 98 DebugEnvironment.ensureInitialized(graalOptions); 99 } 100 AOTCompiler.logCompilation(MiscUtils.uniqueMethodName(method), "Compiling"); 101 102 final long threadId = Thread.currentThread().getId(); 103 104 final boolean printCompilation = GraalCompilerOptions.PrintCompilation.getValue(graalOptions) && !TTY.isSuppressed(); 105 final boolean printAfterCompilation = GraalCompilerOptions.PrintAfterCompilation.getValue(graalOptions) && !TTY.isSuppressed(); 106 if (printCompilation) { 107 TTY.println(getMethodDescription() + "..."); 108 } 109 110 final long start; 111 final long allocatedBytesBefore; 112 if (printAfterCompilation || printCompilation) { 113 start = System.currentTimeMillis(); 114 allocatedBytesBefore = printAfterCompilation || printCompilation ? threadMXBean.getThreadAllocatedBytes(threadId) : 0L; 115 } else { 116 start = 0L; 117 allocatedBytesBefore = 0L; 118 } 119 120 final long startTime = System.currentTimeMillis(); 121 CompilationResult compResult = aotBackend.compileMethod(method); 122 final long endTime = System.currentTimeMillis(); 123 124 if (printAfterCompilation || printCompilation) { 125 final long stop = System.currentTimeMillis(); 126 final int targetCodeSize = compResult != null ? compResult.getTargetCodeSize() : -1; 127 final long allocatedBytesAfter = threadMXBean.getThreadAllocatedBytes(threadId); 128 final long allocatedBytes = (allocatedBytesAfter - allocatedBytesBefore) / 1024; 129 130 TTY.println(getMethodDescription() + String.format(" | %4dms %5dB %5dkB", stop - start, targetCodeSize, allocatedBytes)); 131 } 132 133 if (compResult == null) { 134 result = null; 135 return; 136 } 137 138 // For now precision to the nearest second is sufficient. 139 Main.writeLog(" Compile Time: " + TimeUnit.MILLISECONDS.toSeconds(endTime - startTime) + "secs"); 140 if (main.options.debug) { 141 aotBackend.printCompiledMethod((HotSpotResolvedJavaMethod) method, compResult); | 1 /* 2 * Copyright (c) 2016, 2017, 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 java.util.concurrent.TimeUnit; 27 import java.util.concurrent.atomic.AtomicInteger; 28 29 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; 30 import org.graalvm.compiler.code.CompilationResult; 31 import org.graalvm.compiler.core.GraalCompilerOptions; 32 import org.graalvm.compiler.debug.DebugContext; 33 import org.graalvm.compiler.debug.Management; 34 import org.graalvm.compiler.debug.TTY; 35 import org.graalvm.compiler.debug.DebugContext.Activation; 36 import org.graalvm.compiler.options.OptionValues; 37 import org.graalvm.compiler.printer.GraalDebugHandlersFactory; 38 39 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 40 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 41 import jdk.vm.ci.meta.ResolvedJavaMethod; 42 import jdk.vm.ci.runtime.JVMCICompiler; 43 44 /** 45 * Represents a task in the compile queue. 46 * 47 * This class encapsulates all Graal-specific information that is used during offline AOT 48 * compilation of classes. It also defines methods that parse compilation result of Graal to create 49 * target-independent representation {@code BinaryContainer} of the intended target binary. 50 */ 51 public class AOTCompilationTask implements Runnable, Comparable<Object> { 52 53 private static final AtomicInteger ids = new AtomicInteger(); 54 55 private static final com.sun.management.ThreadMXBean threadMXBean = (com.sun.management.ThreadMXBean) Management.getThreadMXBean(); 56 57 private final Main main; 72 73 private final AOTBackend aotBackend; 74 75 /** 76 * The result of this compilation task. 77 */ 78 private CompiledMethodInfo result; 79 80 public AOTCompilationTask(Main main, OptionValues graalOptions, AOTCompiledClass holder, ResolvedJavaMethod method, AOTBackend aotBackend) { 81 this.main = main; 82 this.graalOptions = graalOptions; 83 this.id = ids.incrementAndGet(); 84 this.holder = holder; 85 this.method = method; 86 this.aotBackend = aotBackend; 87 } 88 89 /** 90 * Compile a method or a constructor. 91 */ 92 @SuppressWarnings("try") 93 public void run() { 94 // Ensure a JVMCI runtime is initialized prior to Debug being initialized as the former 95 // may include processing command line options used by the latter. 96 HotSpotJVMCIRuntime.runtime(); 97 98 AOTCompiler.logCompilation(MiscUtils.uniqueMethodName(method), "Compiling"); 99 100 final long threadId = Thread.currentThread().getId(); 101 102 final boolean printCompilation = GraalCompilerOptions.PrintCompilation.getValue(graalOptions) && !TTY.isSuppressed(); 103 final boolean printAfterCompilation = GraalCompilerOptions.PrintAfterCompilation.getValue(graalOptions) && !TTY.isSuppressed(); 104 if (printCompilation) { 105 TTY.println(getMethodDescription() + "..."); 106 } 107 108 final long start; 109 final long allocatedBytesBefore; 110 if (printAfterCompilation || printCompilation) { 111 start = System.currentTimeMillis(); 112 allocatedBytesBefore = printAfterCompilation || printCompilation ? threadMXBean.getThreadAllocatedBytes(threadId) : 0L; 113 } else { 114 start = 0L; 115 allocatedBytesBefore = 0L; 116 } 117 118 CompilationResult compResult = null; 119 final long startTime = System.currentTimeMillis(); 120 SnippetReflectionProvider snippetReflection = aotBackend.getProviders().getSnippetReflection(); 121 try (DebugContext debug = DebugContext.create(graalOptions, new GraalDebugHandlersFactory(snippetReflection)); Activation a = debug.activate()) { 122 compResult = aotBackend.compileMethod(method, debug); 123 } 124 final long endTime = System.currentTimeMillis(); 125 126 if (printAfterCompilation || printCompilation) { 127 final long stop = System.currentTimeMillis(); 128 final int targetCodeSize = compResult != null ? compResult.getTargetCodeSize() : -1; 129 final long allocatedBytesAfter = threadMXBean.getThreadAllocatedBytes(threadId); 130 final long allocatedBytes = (allocatedBytesAfter - allocatedBytesBefore) / 1024; 131 132 TTY.println(getMethodDescription() + String.format(" | %4dms %5dB %5dkB", stop - start, targetCodeSize, allocatedBytes)); 133 } 134 135 if (compResult == null) { 136 result = null; 137 return; 138 } 139 140 // For now precision to the nearest second is sufficient. 141 Main.writeLog(" Compile Time: " + TimeUnit.MILLISECONDS.toSeconds(endTime - startTime) + "secs"); 142 if (main.options.debug) { 143 aotBackend.printCompiledMethod((HotSpotResolvedJavaMethod) method, compResult); |