1 /*
   2  * Copyright (c) 2013, 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 sun.hotspot.tools.ctw;
  25 
  26 import java.lang.management.ManagementFactory;
  27 
  28 import java.io.*;
  29 import java.nio.file.Files;
  30 import java.nio.file.Paths;
  31 
  32 import java.util.List;
  33 import java.util.concurrent.*;
  34 
  35 public class CompileTheWorld {
  36     // in case when a static constructor changes System::out and System::err
  37     // we hold these values of output streams
  38     static PrintStream OUT = System.out;
  39     static final PrintStream ERR = System.err;
  40     /**
  41      * Entry point. Compiles classes in {@code paths}
  42      *
  43      * @param paths paths to jar/zip, dir contains classes, or to .lst file
  44      *              contains list of classes to compile
  45      */
  46     public static void main(String[] paths) {
  47         if (paths.length == 0) {
  48             throw new IllegalArgumentException("Expect a path to a compile target.");
  49         }
  50         String logfile = Utils.LOG_FILE;
  51         PrintStream os = null;
  52         if (logfile != null) {
  53             try {
  54                 os = new PrintStream(Files.newOutputStream(Paths.get(logfile)));
  55             } catch (IOException io) {
  56             }
  57         }
  58         if (os != null) {
  59             OUT = os;
  60         }
  61 
  62         try {
  63             try {
  64                 if (ManagementFactory.getCompilationMXBean() == null) {
  65                     throw new RuntimeException(
  66                             "CTW can not work in interpreted mode");
  67                 }
  68             } catch (java.lang.NoClassDefFoundError e) {
  69                 // compact1, compact2 support
  70             }
  71             ExecutorService executor = createExecutor();
  72             long start = System.currentTimeMillis();
  73             try {
  74                 String path;
  75                 for (int i = 0, n = paths.length; i < n
  76                         && !PathHandler.isFinished(); ++i) {
  77                     path = paths[i];
  78                     PathHandler.create(path, executor).process();
  79                 }
  80             } finally {
  81                 await(executor);
  82             }
  83             CompileTheWorld.OUT.printf("Done (%d classes, %d methods, %d ms)%n",
  84                     PathHandler.getClassCount(),
  85                     Compiler.getMethodCount(),
  86                     System.currentTimeMillis() - start);
  87         } finally {
  88             if (os != null) {
  89                 os.close();
  90             }
  91         }
  92     }
  93 
  94     private static ExecutorService createExecutor() {
  95         final int threadsCount = Math.min(
  96                 Runtime.getRuntime().availableProcessors(),
  97                 Utils.CI_COMPILER_COUNT);
  98         ExecutorService result;
  99         if (threadsCount > 1) {
 100             result = new ThreadPoolExecutor(threadsCount, threadsCount,
 101                     /* keepAliveTime */ 0L, TimeUnit.MILLISECONDS,
 102                     new ArrayBlockingQueue<>(threadsCount),
 103                     new ThreadPoolExecutor.CallerRunsPolicy());
 104         } else {
 105             result = new CurrentThreadExecutor();
 106         }
 107         return result;
 108     }
 109 
 110     private static void await(ExecutorService executor) {
 111         executor.shutdown();
 112         while (!executor.isTerminated()) {
 113             try {
 114                 executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
 115             } catch (InterruptedException ie) {
 116                 Thread.currentThread().interrupt();
 117                 break;
 118             }
 119         }
 120     }
 121 
 122     private static class CurrentThreadExecutor extends AbstractExecutorService {
 123         private boolean isShutdown;
 124 
 125         @Override
 126         public void shutdown() {
 127             this.isShutdown = true;
 128         }
 129 
 130         @Override
 131         public List<Runnable> shutdownNow() {
 132             return null;
 133         }
 134 
 135         @Override
 136         public boolean isShutdown() {
 137             return isShutdown;
 138         }
 139 
 140         @Override
 141         public boolean isTerminated() {
 142             return isShutdown;
 143         }
 144 
 145         @Override
 146         public boolean awaitTermination(long timeout, TimeUnit unit)
 147                 throws InterruptedException {
 148             return isShutdown;
 149         }
 150 
 151         @Override
 152         public void execute(Runnable command) {
 153             command.run();
 154         }
 155     }
 156 }
 157