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 public static final PrintStream OUT = System.out; 39 public 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 System.setOut(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 // in case when a static constructor creates and runs a new thread 93 // we force it to exit 94 System.exit(0); 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 void await(ExecutorService executor) { 114 executor.shutdown(); 115 while (!executor.isTerminated()) { 116 try { 117 executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); 118 } catch (InterruptedException ie) { 119 Thread.currentThread().interrupt(); 120 break; 121 } 122 } 123 } 124 125 private static class CurrentThreadExecutor extends AbstractExecutorService { 126 private boolean isShutdown; 127 128 @Override 129 public void shutdown() { 130 this.isShutdown = true; 131 } 132 133 @Override 134 public List<Runnable> shutdownNow() { 135 return null; 136 } 137 138 @Override 139 public boolean isShutdown() { 140 return isShutdown; 141 } 142 143 @Override 144 public boolean isTerminated() { 145 return isShutdown; 146 } 147 148 @Override 149 public boolean awaitTermination(long timeout, TimeUnit unit) 150 throws InterruptedException { 151 return isShutdown; 152 } 153 154 @Override 155 public void execute(Runnable command) { 156 command.run(); 157 } 158 } 159 } 160