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.ArrayList; 27 import java.util.List; 28 import java.util.concurrent.PriorityBlockingQueue; 29 import java.util.concurrent.RejectedExecutionException; 30 import java.util.concurrent.ThreadPoolExecutor; 31 import java.util.concurrent.TimeUnit; 32 import java.util.concurrent.atomic.AtomicInteger; 33 34 import org.graalvm.compiler.options.OptionValues; 35 36 import jdk.vm.ci.meta.ResolvedJavaMethod; 37 38 public class AOTCompiler { 39 40 private final Main main; 41 42 private final OptionValues graalOptions; 43 44 private CompileQueue compileQueue; 45 46 private final AOTBackend backend; 47 48 /** 49 * Compile queue. 50 */ 51 private class CompileQueue extends ThreadPoolExecutor { 52 53 /** 54 * Time of the start of this queue. 55 */ 56 private final long startTime; 57 58 /** 59 * Method counter for successful compilations. 60 */ 61 private final AtomicInteger successfulMethodCount = new AtomicInteger(); 62 63 /** 64 * Method counter for failed compilations. 65 */ 66 private final AtomicInteger failedMethodCount = new AtomicInteger(); 67 68 /** 69 * Create a compile queue with the given number of threads. 70 */ 71 public CompileQueue(final int threads) { 72 super(threads, threads, 0L, TimeUnit.MILLISECONDS, new PriorityBlockingQueue<>()); 73 startTime = System.currentTimeMillis(); 74 } 75 76 @Override 77 protected void afterExecute(Runnable r, Throwable t) { 78 AOTCompilationTask task = (AOTCompilationTask) r; 79 if (task.getResult() != null) { 80 final int count = successfulMethodCount.incrementAndGet(); 81 if (count % 100 == 0) { 82 main.printInfo("."); 83 } 84 CompiledMethodInfo result = task.getResult(); 85 if (result != null) { 86 task.getHolder().addCompiledMethod(result); 87 } 88 } else { 89 failedMethodCount.incrementAndGet(); 90 main.printlnVerbose(""); 91 ResolvedJavaMethod method = task.getMethod(); 92 main.printlnVerbose(" failed " + method.getName() + method.getSignature().toMethodDescriptor()); 93 } 94 } 95 96 @Override 97 protected void terminated() { 98 final long endTime = System.currentTimeMillis(); 99 final int success = successfulMethodCount.get(); 100 final int failed = failedMethodCount.get(); 101 main.printlnInfo(""); 102 main.printlnInfo(success + " methods compiled, " + failed + " methods failed (" + (endTime - startTime) + " ms)"); 103 } 104 105 } 106 107 /** 108 * @param main 109 * @param graalOptions 110 * @param aotBackend 111 * @param threads number of compilation threads 112 */ 113 public AOTCompiler(Main main, OptionValues graalOptions, AOTBackend aotBackend, final int threads) { 114 this.main = main; 115 this.graalOptions = graalOptions; 116 this.compileQueue = new CompileQueue(threads); 117 this.backend = aotBackend; 118 } 119 120 /** 121 * Compile all methods in all classes passed. 122 * 123 * @param classes a list of class to compile 124 * @throws InterruptedException 125 */ 126 public List<AOTCompiledClass> compileClasses(List<AOTCompiledClass> classes) throws InterruptedException { 127 main.printlnInfo("Compiling with " + compileQueue.getCorePoolSize() + " threads"); 128 main.printInfo("."); // Compilation progress indication. 129 130 for (AOTCompiledClass c : classes) { 131 for (ResolvedJavaMethod m : c.getMethods()) { 132 enqueueMethod(c, m); 133 } 134 } 135 136 // Shutdown queue and wait for all tasks to complete. 137 compileQueue.shutdown(); 138 compileQueue.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); 139 140 List<AOTCompiledClass> compiledClasses = new ArrayList<>(); 141 for (AOTCompiledClass compiledClass : classes) { 142 if (compiledClass.hasCompiledMethods()) { 143 compiledClasses.add(compiledClass); 144 } 145 } 146 return compiledClasses; 147 } 148 149 /** 150 * Enqueue a method in the {@link #compileQueue}. 151 * 152 * @param method method to be enqueued 153 */ 154 private void enqueueMethod(AOTCompiledClass aotClass, ResolvedJavaMethod method) { 155 AOTCompilationTask task = new AOTCompilationTask(main, graalOptions, aotClass, method, backend); 156 try { 157 compileQueue.execute(task); 158 } catch (RejectedExecutionException e) { 159 e.printStackTrace(); 160 } 161 } 162 163 public static void logCompilation(String methodName, String message) { 164 Main.writeLog(message + " " + methodName); 165 } 166 167 }