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