author | hseigel |
Fri, 27 Feb 2015 12:48:06 -0500 | |
changeset 29319 | 54c7d4e5261f |
parent 20293 | 1dd93ae4304d |
child 40033 | dbd5f9838049 |
permissions | -rw-r--r-- |
19767 | 1 |
/* |
29319
54c7d4e5261f
8073908: Replace hotspot/testlibrary use of sun.management with public API
hseigel
parents:
20293
diff
changeset
|
2 |
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. |
19767 | 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 |
||
29319
54c7d4e5261f
8073908: Replace hotspot/testlibrary use of sun.management with public API
hseigel
parents:
20293
diff
changeset
|
26 |
import java.lang.management.ManagementFactory; |
19767 | 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 |
/** |
|
37 |
* Entry point. Compiles classes in {@code args}, or all classes in |
|
38 |
* boot-classpath if args is empty |
|
39 |
* |
|
40 |
* @param args paths to jar/zip, dir contains classes, or to .lst file |
|
41 |
* contains list of classes to compile |
|
42 |
*/ |
|
43 |
public static void main(String[] args) { |
|
44 |
String logfile = Utils.LOG_FILE; |
|
45 |
PrintStream os = null; |
|
46 |
if (logfile != null) { |
|
47 |
try { |
|
48 |
os = new PrintStream(Files.newOutputStream(Paths.get(logfile))); |
|
49 |
} catch (IOException io) { |
|
50 |
} |
|
51 |
} |
|
52 |
if (os != null) { |
|
53 |
System.setOut(os); |
|
54 |
} |
|
55 |
||
56 |
try { |
|
57 |
try { |
|
29319
54c7d4e5261f
8073908: Replace hotspot/testlibrary use of sun.management with public API
hseigel
parents:
20293
diff
changeset
|
58 |
if (ManagementFactory.getCompilationMXBean() == null) { |
19767 | 59 |
throw new RuntimeException( |
60 |
"CTW can not work in interpreted mode"); |
|
61 |
} |
|
62 |
} catch (java.lang.NoClassDefFoundError e) { |
|
63 |
// compact1, compact2 support |
|
64 |
} |
|
65 |
String[] paths = args; |
|
66 |
boolean skipRtJar = false; |
|
67 |
if (args.length == 0) { |
|
68 |
paths = getDefaultPaths(); |
|
69 |
skipRtJar = true; |
|
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 |
if (skipRtJar && i > 0 && isRtJar(path)) { |
|
79 |
// rt.jar is not first, so skip it |
|
80 |
continue; |
|
81 |
} |
|
82 |
PathHandler.create(path, executor).process(); |
|
83 |
} |
|
84 |
} finally { |
|
85 |
await(executor); |
|
86 |
} |
|
87 |
System.out.printf("Done (%d classes, %d methods, %d ms)%n", |
|
88 |
Compiler.getClassCount(), |
|
89 |
Compiler.getMethodCount(), |
|
90 |
System.currentTimeMillis() - start); |
|
91 |
} finally { |
|
92 |
if (os != null) { |
|
93 |
os.close(); |
|
94 |
} |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
private static ExecutorService createExecutor() { |
|
99 |
final int threadsCount = Math.min( |
|
100 |
Runtime.getRuntime().availableProcessors(), |
|
101 |
Utils.CI_COMPILER_COUNT); |
|
102 |
ExecutorService result; |
|
103 |
if (threadsCount > 1) { |
|
104 |
result = new ThreadPoolExecutor(threadsCount, threadsCount, |
|
105 |
/* keepAliveTime */ 0L, TimeUnit.MILLISECONDS, |
|
106 |
new ArrayBlockingQueue<>(threadsCount), |
|
107 |
new ThreadPoolExecutor.CallerRunsPolicy()); |
|
108 |
} else { |
|
109 |
result = new CurrentThreadExecutor(); |
|
110 |
} |
|
111 |
return result; |
|
112 |
} |
|
113 |
||
114 |
private static String[] getDefaultPaths() { |
|
115 |
String property = System.getProperty("sun.boot.class.path"); |
|
116 |
System.out.println( |
|
117 |
"# use 'sun.boot.class.path' as args: " + property); |
|
118 |
return Utils.PATH_SEPARATOR.split(property); |
|
119 |
} |
|
120 |
||
121 |
private static void await(ExecutorService executor) { |
|
122 |
executor.shutdown(); |
|
123 |
while (!executor.isTerminated()) { |
|
124 |
try { |
|
125 |
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
|
126 |
} catch (InterruptedException ie) { |
|
127 |
Thread.currentThread().interrupt(); |
|
128 |
break; |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
private static boolean isRtJar(String path) { |
|
134 |
return Utils.endsWithIgnoreCase(path, File.separator + "rt.jar"); |
|
135 |
} |
|
136 |
||
137 |
private static class CurrentThreadExecutor extends AbstractExecutorService { |
|
138 |
private boolean isShutdown; |
|
139 |
||
140 |
@Override |
|
141 |
public void shutdown() { |
|
142 |
this.isShutdown = true; |
|
143 |
} |
|
144 |
||
145 |
@Override |
|
146 |
public List<Runnable> shutdownNow() { |
|
147 |
return null; |
|
148 |
} |
|
149 |
||
150 |
@Override |
|
151 |
public boolean isShutdown() { |
|
152 |
return isShutdown; |
|
153 |
} |
|
154 |
||
155 |
@Override |
|
156 |
public boolean isTerminated() { |
|
157 |
return isShutdown; |
|
158 |
} |
|
159 |
||
160 |
@Override |
|
161 |
public boolean awaitTermination(long timeout, TimeUnit unit) |
|
162 |
throws InterruptedException { |
|
163 |
return isShutdown; |
|
164 |
} |
|
165 |
||
166 |
@Override |
|
167 |
public void execute(Runnable command) { |
|
168 |
command.run(); |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |