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         boolean passed = false;
  63 
  64         try {
  65             try {
  66                 if (ManagementFactory.getCompilationMXBean() == null) {
  67                     throw new RuntimeException(
  68                             "CTW can not work in interpreted mode");
  69                 }
  70             } catch (java.lang.NoClassDefFoundError e) {
  71                 // compact1, compact2 support
  72             }
  73             ExecutorService executor = createExecutor();
  74             long start = System.currentTimeMillis();
  75             try {
  76                 String path;
  77                 for (int i = 0, n = paths.length; i < n
  78                         && !PathHandler.isFinished(); ++i) {
  79                     path = paths[i];
  80                     PathHandler.create(path, executor).process();
  81                 }
  82             } finally {
  83                 await(executor);
  84             }
  85             CompileTheWorld.OUT.printf("Done (%d classes, %d methods, %d ms)%n",
  86                     PathHandler.getClassCount(),
  87                     Compiler.getMethodCount(),
  88                     System.currentTimeMillis() - start);
  89             passed = true;
  90         } finally {
  91             // <clinit> might have started new threads
  92             System.exit(passed ? 0 : 1);
  93         }
  94     }
  95 
  96     private static ExecutorService createExecutor() {
  97         final int threadsCount = Math.min(
  98                 Runtime.getRuntime().availableProcessors(),
  99                 Utils.CI_COMPILER_COUNT);
 100         ExecutorService result;
 101         if (threadsCount > 1) {
 102             result = new ThreadPoolExecutor(threadsCount, threadsCount,
 103                     /* keepAliveTime */ 0L, TimeUnit.MILLISECONDS,
 104                     new ArrayBlockingQueue<>(threadsCount),
 105                     new ThreadPoolExecutor.CallerRunsPolicy());
 106         } else {
 107             result = new CurrentThreadExecutor();
 108         }
 109         return result;
 110     }
 111 
 112     private static void await(ExecutorService executor) {
 113         executor.shutdown();
 114         while (!executor.isTerminated()) {
 115             try {
 116                 executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
 117             } catch (InterruptedException ie) {
 118                 Thread.currentThread().interrupt();
 119                 break;
 120             }
 121         }
 122     }
 123 
 124     private static class CurrentThreadExecutor extends AbstractExecutorService {
 125         private boolean isShutdown;
 126 
 127         @Override
 128         public void shutdown() {
 129             this.isShutdown = true;
 130         }
 131 
 132         @Override
 133         public List<Runnable> shutdownNow() {
 134             return null;
 135         }
 136 
 137         @Override
 138         public boolean isShutdown() {
 139             return isShutdown;
 140         }
 141 
 142         @Override
 143         public boolean isTerminated() {
 144             return isShutdown;
 145         }
 146 
 147         @Override
 148         public boolean awaitTermination(long timeout, TimeUnit unit)
 149                 throws InterruptedException {
 150             return isShutdown;
 151         }
 152 
 153         @Override
 154         public void execute(Runnable command) {
 155             command.run();
 156         }
 157     }
 158 }
 159