src/share/classes/com/sun/tools/sjavac/server/CompilerThread.java

changeset 1570
f91144b7da75
parent 1504
22e417cdddee
child 1763
445b8b5ae9f4
equal deleted inserted replaced
1569:475eb15dfdad 1570:f91144b7da75
1 /*
2 * Copyright (c) 2012, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.sjavac.server;
27
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.io.OutputStreamWriter;
33 import java.io.PrintWriter;
34 import java.io.StringWriter;
35 import java.net.Socket;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Set;
43 import java.util.Map;
44 import java.util.concurrent.Future;
45 import javax.tools.JavaFileManager;
46 import javax.tools.JavaFileObject;
47 import javax.tools.StandardJavaFileManager;
48
49 import com.sun.tools.javac.util.Context;
50 import com.sun.tools.javac.util.Log;
51 import com.sun.tools.javac.util.BaseFileManager;
52 import com.sun.tools.sjavac.comp.Dependencies;
53 import com.sun.tools.sjavac.comp.JavaCompilerWithDeps;
54 import com.sun.tools.sjavac.comp.SmartFileManager;
55 import com.sun.tools.sjavac.comp.ResolveWithDeps;
56
57 /**
58 * The compiler thread maintains a JavaCompiler instance and
59 * can receive a request from the client, perform the compilation
60 * requested and report back the results.
61 *
62 * * <p><b>This is NOT part of any supported API.
63 * If you write code that depends on this, you do so at your own
64 * risk. This code and its internal interfaces are subject to change
65 * or deletion without notice.</b></p>
66 */
67 public class CompilerThread implements Runnable {
68 private JavacServer javacServer;
69 private CompilerPool compilerPool;
70 private List<Future<?>> subTasks;
71
72 // Communicating over this socket.
73 private Socket socket;
74
75 // The necessary classes to do a compilation.
76 private com.sun.tools.javac.api.JavacTool compiler;
77 private StandardJavaFileManager fileManager;
78 private BaseFileManager fileManagerBase;
79 private SmartFileManager smartFileManager;
80 private Context context;
81
82 // If true, then this thread is serving a request.
83 private boolean inUse = false;
84
85 CompilerThread(CompilerPool cp) {
86 compilerPool = cp;
87 javacServer = cp.getJavacServer();
88 }
89
90 /**
91 * Execute a minor task, for example generating bytecodes and writing them to disk,
92 * that belong to a major compiler thread task.
93 */
94 public synchronized void executeSubtask(Runnable r) {
95 subTasks.add(compilerPool.executeSubtask(this, r));
96 }
97
98 /**
99 * Count the number of active sub tasks.
100 */
101 public synchronized int numActiveSubTasks() {
102 int c = 0;
103 for (Future<?> f : subTasks) {
104 if (!f.isDone() && !f.isCancelled()) {
105 c++;
106 }
107 }
108 return c;
109 }
110
111 /**
112 * Use this socket for the upcoming request.
113 */
114 public void setSocket(Socket s) {
115 socket = s;
116 }
117
118 /**
119 * Prepare the compiler thread for use. It is not yet started.
120 * It will be started by the executor service.
121 */
122 public synchronized void use() {
123 assert(!inUse);
124 inUse = true;
125 compiler = com.sun.tools.javac.api.JavacTool.create();
126 fileManager = compiler.getStandardFileManager(null, null, null);
127 fileManagerBase = (BaseFileManager)fileManager;
128 smartFileManager = new SmartFileManager(fileManager);
129 context = new Context();
130 context.put(JavaFileManager.class, smartFileManager);
131 ResolveWithDeps.preRegister(context);
132 JavaCompilerWithDeps.preRegister(context, this);
133 subTasks = new ArrayList<Future<?>>();
134 }
135
136 /**
137 * Prepare the compiler thread for idleness.
138 */
139 public synchronized void unuse() {
140 assert(inUse);
141 inUse = false;
142 compiler = null;
143 fileManager = null;
144 fileManagerBase = null;
145 smartFileManager = null;
146 context = null;
147 subTasks = null;
148 }
149
150 /**
151 * Expect this key on the next line read from the reader.
152 */
153 private static boolean expect(BufferedReader in, String key) throws IOException {
154 String s = in.readLine();
155 if (s != null && s.equals(key)) {
156 return true;
157 }
158 return false;
159 }
160
161 // The request identifier, for example GENERATE_NEWBYTECODE
162 String id = "";
163
164 public String currentRequestId() {
165 return id;
166 }
167
168 PrintWriter stdout;
169 PrintWriter stderr;
170 int forcedExitCode = 0;
171
172 public void logError(String msg) {
173 stderr.println(msg);
174 forcedExitCode = -1;
175 }
176
177 /**
178 * Invoked by the executor service.
179 */
180 public void run() {
181 // Unique nr that identifies this request.
182 int thisRequest = compilerPool.startRequest();
183 long start = System.currentTimeMillis();
184 int numClasses = 0;
185 StringBuilder compiledPkgs = new StringBuilder();
186 use();
187
188 PrintWriter out = null;
189 try {
190 javacServer.log("<"+thisRequest+"> Connect from "+socket.getRemoteSocketAddress()+" activethreads="+compilerPool.numActiveRequests());
191 BufferedReader in = new BufferedReader(new InputStreamReader(
192 socket.getInputStream()));
193 out = new PrintWriter(new OutputStreamWriter(
194 socket.getOutputStream()));
195 if (!expect(in, JavacServer.PROTOCOL_COOKIE_VERSION)) {
196 javacServer.log("<"+thisRequest+"> Bad protocol from ip "+socket.getRemoteSocketAddress());
197 return;
198 }
199
200 String cookie = in.readLine();
201 if (cookie == null || !cookie.equals(""+javacServer.getCookie())) {
202 javacServer.log("<"+thisRequest+"> Bad cookie from ip "+socket.getRemoteSocketAddress());
203 return;
204 }
205 if (!expect(in, JavacServer.PROTOCOL_CWD)) {
206 return;
207 }
208 String cwd = in.readLine();
209 if (cwd == null)
210 return;
211 if (!expect(in, JavacServer.PROTOCOL_ID)) {
212 return;
213 }
214 id = in.readLine();
215 if (id == null)
216 return;
217 if (!expect(in, JavacServer.PROTOCOL_ARGS)) {
218 return;
219 }
220 ArrayList<String> the_options = new ArrayList<String>();
221 ArrayList<File> the_classes = new ArrayList<File>();
222 Iterable<File> path = Arrays.<File> asList(new File(cwd));
223
224 for (;;) {
225 String l = in.readLine();
226 if (l == null)
227 return;
228 if (l.equals(JavacServer.PROTOCOL_SOURCES_TO_COMPILE))
229 break;
230 if (l.startsWith("--server:"))
231 continue;
232 if (!l.startsWith("-") && l.endsWith(".java")) {
233 the_classes.add(new File(l));
234 numClasses++;
235 } else {
236 the_options.add(l);
237 }
238 continue;
239 }
240
241 // Load sources to compile
242 Set<URI> sourcesToCompile = new HashSet<URI>();
243 for (;;) {
244 String l = in.readLine();
245 if (l == null)
246 return;
247 if (l.equals(JavacServer.PROTOCOL_VISIBLE_SOURCES))
248 break;
249 try {
250 sourcesToCompile.add(new URI(l));
251 numClasses++;
252 } catch (URISyntaxException e) {
253 return;
254 }
255 }
256 // Load visible sources
257 Set<URI> visibleSources = new HashSet<URI>();
258 boolean fix_drive_letter_case = System.getProperty("os.name").toLowerCase().equals("windows");
259 for (;;) {
260 String l = in.readLine();
261 if (l == null)
262 return;
263 if (l.equals(JavacServer.PROTOCOL_END))
264 break;
265 try {
266 URI u = new URI(l);
267 if (fix_drive_letter_case) {
268 // Make sure the driver letter is lower case.
269 String s = u.toString();
270 if (s.startsWith("file:/") &&
271 Character.isUpperCase(s.charAt(6))) {
272 u = new URI("file:/"+Character.toLowerCase(s.charAt(6))+s.substring(7));
273 }
274 }
275 visibleSources.add(u);
276 } catch (URISyntaxException e) {
277 return;
278 }
279 }
280
281 // A completed request has been received.
282
283 // Now setup the actual compilation....
284 // First deal with explicit source files on cmdline and in at file.
285 com.sun.tools.javac.util.ListBuffer<JavaFileObject> compilationUnits =
286 new com.sun.tools.javac.util.ListBuffer<JavaFileObject>();
287 for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(the_classes)) {
288 compilationUnits.append(i);
289 }
290 // Now deal with sources supplied as source_to_compile.
291 com.sun.tools.javac.util.ListBuffer<File> sourcesToCompileFiles =
292 new com.sun.tools.javac.util.ListBuffer<File>();
293 for (URI u : sourcesToCompile) {
294 sourcesToCompileFiles.append(new File(u));
295 }
296 for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(sourcesToCompileFiles)) {
297 compilationUnits.append(i);
298 }
299 // Log the options to be used.
300 StringBuilder options = new StringBuilder();
301 for (String s : the_options) {
302 options.append(">").append(s).append("< ");
303 }
304 javacServer.log(id+" <"+thisRequest+"> options "+options.toString());
305
306 forcedExitCode = 0;
307 // Create a new logger.
308 StringWriter stdoutLog = new StringWriter();
309 StringWriter stderrLog = new StringWriter();
310 stdout = new PrintWriter(stdoutLog);
311 stderr = new PrintWriter(stderrLog);
312 com.sun.tools.javac.main.Main.Result rc = com.sun.tools.javac.main.Main.Result.OK;
313 try {
314 if (compilationUnits.size() > 0) {
315 // Bind the new logger to the existing context.
316 context.put(Log.outKey, stderr);
317 Log.instance(context).setWriter(Log.WriterKind.NOTICE, stdout);
318 Log.instance(context).setWriter(Log.WriterKind.WARNING, stderr);
319 Log.instance(context).setWriter(Log.WriterKind.ERROR, stderr);
320 // Process the options.
321 com.sun.tools.javac.api.JavacTool.processOptions(context, smartFileManager, the_options);
322 fileManagerBase.setContext(context);
323 smartFileManager.setVisibleSources(visibleSources);
324 smartFileManager.cleanArtifacts();
325 smartFileManager.setLog(stdout);
326 Dependencies.instance(context).reset();
327
328 com.sun.tools.javac.main.Main ccompiler = new com.sun.tools.javac.main.Main("javacTask", stderr);
329 String[] aa = the_options.toArray(new String[0]);
330
331 // Do the compilation!
332 rc = ccompiler.compile(aa, context, compilationUnits.toList(), null);
333
334 while (numActiveSubTasks()>0) {
335 try { Thread.sleep(1000); } catch (InterruptedException e) { }
336 }
337
338 smartFileManager.flush();
339 }
340 } catch (Exception e) {
341 stderr.println(e.getMessage());
342 forcedExitCode = -1;
343 }
344
345 // Send the response..
346 out.println(JavacServer.PROTOCOL_STDOUT);
347 out.print(stdoutLog);
348 out.println(JavacServer.PROTOCOL_STDERR);
349 out.print(stderrLog);
350 // The compilation is complete! And errors will have already been printed on out!
351 out.println(JavacServer.PROTOCOL_PACKAGE_ARTIFACTS);
352 Map<String,Set<URI>> pa = smartFileManager.getPackageArtifacts();
353 for (String aPkgName : pa.keySet()) {
354 out.println("+"+aPkgName);
355 Set<URI> as = pa.get(aPkgName);
356 for (URI a : as) {
357 out.println(" "+a.toString());
358 }
359 }
360 Dependencies deps = Dependencies.instance(context);
361 out.println(JavacServer.PROTOCOL_PACKAGE_DEPENDENCIES);
362 Map<String,Set<String>> pd = deps.getDependencies();
363 for (String aPkgName : pd.keySet()) {
364 out.println("+"+aPkgName);
365 Set<String> ds = pd.get(aPkgName);
366 // Everything depends on java.lang
367 if (!ds.contains(":java.lang")) ds.add(":java.lang");
368 for (String d : ds) {
369 out.println(" "+d);
370 }
371 }
372 out.println(JavacServer.PROTOCOL_PACKAGE_PUBLIC_APIS);
373 Map<String,String> pp = deps.getPubapis();
374 for (String aPkgName : pp.keySet()) {
375 out.println("+"+aPkgName);
376 String ps = pp.get(aPkgName);
377 // getPubapis added a space to each line!
378 out.println(ps);
379 compiledPkgs.append(aPkgName+" ");
380 }
381 out.println(JavacServer.PROTOCOL_SYSINFO);
382 out.println("num_cores=" + Runtime.getRuntime().availableProcessors());
383 out.println("max_memory=" + Runtime.getRuntime().maxMemory());
384 out.println(JavacServer.PROTOCOL_RETURN_CODE);
385
386 // Errors from sjavac that affect compilation status!
387 int rcv = rc.exitCode;
388 if (rcv == 0 && forcedExitCode != 0) {
389 rcv = forcedExitCode;
390 }
391 out.println("" + rcv);
392 out.println(JavacServer.PROTOCOL_END);
393 out.flush();
394 } catch (IOException e) {
395 e.printStackTrace();
396 } finally {
397 try {
398 if (out != null) out.close();
399 if (!socket.isClosed()) {
400 socket.close();
401 }
402 socket = null;
403 } catch (Exception e) {
404 javacServer.log("ERROR "+e);
405 e.printStackTrace();
406 }
407 compilerPool.stopRequest();
408 long duration = System.currentTimeMillis()-start;
409 javacServer.addBuildTime(duration);
410 float classpersec = ((float)numClasses)*(((float)1000.0)/((float)duration));
411 javacServer.log(id+" <"+thisRequest+"> "+compiledPkgs+" duration " + duration+ " ms num_classes="+numClasses+
412 " classpersec="+classpersec+" subtasks="+subTasks.size());
413 javacServer.flushLog();
414 unuse();
415 compilerPool.returnCompilerThread(this);
416 }
417 }
418 }
419

mercurial