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