1 /*
   2  * Copyright (c) 2013, 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 sun.management.ManagementFactoryHelper;
  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 /**
  36  * @author igor.ignatyev@oracle.com
  37  */
  38 public class CompileTheWorld {
  39     /**
  40      * Entry point. Compiles classes in {@code args}, or all classes in
  41      * boot-classpath if args is empty
  42      *
  43      * @param args 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[] args) {
  47         String logfile = Utils.LOG_FILE;
  48         PrintStream os = null;
  49         if (logfile != null) {
  50             try {
  51                 os = new PrintStream(Files.newOutputStream(Paths.get(logfile)));
  52             } catch (IOException io) {
  53             }
  54         }
  55         if (os != null) {
  56             System.setOut(os);
  57         }
  58 
  59         try {
  60             if (ManagementFactoryHelper.getCompilationMXBean() == null) {
  61                 throw new RuntimeException(
  62                         "CTW can not work in interpreted mode");
  63             }
  64             String[] paths = args;
  65             boolean skipRtJar = false;
  66             if (args.length == 0) {
  67                 paths = getDefaultPaths();
  68                 skipRtJar = true;
  69             }
  70             ExecutorService executor = createExecutor();
  71             long start = System.currentTimeMillis();
  72             try {
  73                 String path;
  74                 for (int i = 0, n = paths.length; i < n
  75                         && !PathHandler.isFinished(); ++i) {
  76                     path = paths[i];
  77                     if (skipRtJar && i > 0 && isRtJar(path)) {
  78                         // rt.jar is not first, so skip it
  79                         continue;
  80                     }
  81                     PathHandler.create(path, executor).process();
  82                 }
  83             } finally {
  84                 await(executor);
  85             }
  86             System.out.printf("Done (%d classes, %d methods, %d ms)%n",
  87                     PathHandler.getClassCount(),
  88                     PathHandler.getMethodCount(),
  89                     System.currentTimeMillis() - start);
  90         } finally {
  91             if (os != null) {
  92                 os.close();
  93             }
  94         }
  95     }
  96 
  97     private static ExecutorService createExecutor() {
  98         final int threadsCount = Math.min(
  99                 Runtime.getRuntime().availableProcessors(),
 100                 Utils.CI_COMPILER_COUNT);
 101         ExecutorService result;
 102         if (threadsCount > 1) {
 103             result = new ThreadPoolExecutor(threadsCount, threadsCount,
 104                     /* keepAliveTime */ 0L, TimeUnit.MILLISECONDS,
 105                     new ArrayBlockingQueue<>(threadsCount),
 106                     new ThreadPoolExecutor.CallerRunsPolicy());
 107         } else {
 108             result = new CurrentThreadExecutor();
 109         }
 110         return result;
 111     }
 112 
 113     private static String[] getDefaultPaths() {
 114         String property = System.getProperty("sun.boot.class.path");
 115         System.out.println(
 116                 "# use 'sun.boot.class.path' as args: " + property);
 117         return Utils.PATH_SEPARATOR.split(property);
 118     }
 119 
 120     private static void await(ExecutorService executor) {
 121         executor.shutdown();
 122         while (!executor.isTerminated()) {
 123             try {
 124                 executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
 125             } catch (InterruptedException ie) {
 126                 Thread.currentThread().interrupt();
 127                 break;
 128             }
 129         }
 130     }
 131 
 132     private static boolean isRtJar(String path) {
 133         return Utils.endsWithIgnoreCase(path, File.separator + "rt.jar");
 134     }
 135 
 136     private static class CurrentThreadExecutor extends AbstractExecutorService {
 137         private boolean isShutdown;
 138 
 139         @Override
 140         public void shutdown() {
 141             this.isShutdown = true;
 142         }
 143 
 144         @Override
 145         public List<Runnable> shutdownNow() {
 146             return null;
 147         }
 148 
 149         @Override
 150         public boolean isShutdown() {
 151             return isShutdown;
 152         }
 153 
 154         @Override
 155         public boolean isTerminated() {
 156             return isShutdown;
 157         }
 158 
 159         @Override
 160         public boolean awaitTermination(long timeout, TimeUnit unit)
 161                 throws InterruptedException {
 162             return isShutdown;
 163         }
 164 
 165         @Override
 166         public void execute(Runnable command) {
 167             command.run();
 168         }
 169     }
 170 }
 171