1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/sjavac/server/CompilerThread.java Fri Jan 18 00:16:21 2013 +0100 1.3 @@ -0,0 +1,419 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.sjavac.server; 1.30 + 1.31 +import java.io.BufferedReader; 1.32 +import java.io.File; 1.33 +import java.io.IOException; 1.34 +import java.io.InputStreamReader; 1.35 +import java.io.OutputStreamWriter; 1.36 +import java.io.PrintWriter; 1.37 +import java.io.StringWriter; 1.38 +import java.net.Socket; 1.39 +import java.net.URI; 1.40 +import java.net.URISyntaxException; 1.41 +import java.util.ArrayList; 1.42 +import java.util.Arrays; 1.43 +import java.util.HashSet; 1.44 +import java.util.List; 1.45 +import java.util.Set; 1.46 +import java.util.Map; 1.47 +import java.util.concurrent.Future; 1.48 +import javax.tools.JavaFileManager; 1.49 +import javax.tools.JavaFileObject; 1.50 +import javax.tools.StandardJavaFileManager; 1.51 + 1.52 +import com.sun.tools.javac.util.Context; 1.53 +import com.sun.tools.javac.util.Log; 1.54 +import com.sun.tools.javac.util.BaseFileManager; 1.55 +import com.sun.tools.sjavac.comp.Dependencies; 1.56 +import com.sun.tools.sjavac.comp.JavaCompilerWithDeps; 1.57 +import com.sun.tools.sjavac.comp.SmartFileManager; 1.58 +import com.sun.tools.sjavac.comp.ResolveWithDeps; 1.59 + 1.60 +/** 1.61 + * The compiler thread maintains a JavaCompiler instance and 1.62 + * can receive a request from the client, perform the compilation 1.63 + * requested and report back the results. 1.64 + * 1.65 + * * <p><b>This is NOT part of any supported API. 1.66 + * If you write code that depends on this, you do so at your own 1.67 + * risk. This code and its internal interfaces are subject to change 1.68 + * or deletion without notice.</b></p> 1.69 + */ 1.70 +public class CompilerThread implements Runnable { 1.71 + private JavacServer javacServer; 1.72 + private CompilerPool compilerPool; 1.73 + private List<Future<?>> subTasks; 1.74 + 1.75 + // Communicating over this socket. 1.76 + private Socket socket; 1.77 + 1.78 + // The necessary classes to do a compilation. 1.79 + private com.sun.tools.javac.api.JavacTool compiler; 1.80 + private StandardJavaFileManager fileManager; 1.81 + private BaseFileManager fileManagerBase; 1.82 + private SmartFileManager smartFileManager; 1.83 + private Context context; 1.84 + 1.85 + // If true, then this thread is serving a request. 1.86 + private boolean inUse = false; 1.87 + 1.88 + CompilerThread(CompilerPool cp) { 1.89 + compilerPool = cp; 1.90 + javacServer = cp.getJavacServer(); 1.91 + } 1.92 + 1.93 + /** 1.94 + * Execute a minor task, for example generating bytecodes and writing them to disk, 1.95 + * that belong to a major compiler thread task. 1.96 + */ 1.97 + public synchronized void executeSubtask(Runnable r) { 1.98 + subTasks.add(compilerPool.executeSubtask(this, r)); 1.99 + } 1.100 + 1.101 + /** 1.102 + * Count the number of active sub tasks. 1.103 + */ 1.104 + public synchronized int numActiveSubTasks() { 1.105 + int c = 0; 1.106 + for (Future<?> f : subTasks) { 1.107 + if (!f.isDone() && !f.isCancelled()) { 1.108 + c++; 1.109 + } 1.110 + } 1.111 + return c; 1.112 + } 1.113 + 1.114 + /** 1.115 + * Use this socket for the upcoming request. 1.116 + */ 1.117 + public void setSocket(Socket s) { 1.118 + socket = s; 1.119 + } 1.120 + 1.121 + /** 1.122 + * Prepare the compiler thread for use. It is not yet started. 1.123 + * It will be started by the executor service. 1.124 + */ 1.125 + public synchronized void use() { 1.126 + assert(!inUse); 1.127 + inUse = true; 1.128 + compiler = com.sun.tools.javac.api.JavacTool.create(); 1.129 + fileManager = compiler.getStandardFileManager(null, null, null); 1.130 + fileManagerBase = (BaseFileManager)fileManager; 1.131 + smartFileManager = new SmartFileManager(fileManager); 1.132 + context = new Context(); 1.133 + context.put(JavaFileManager.class, smartFileManager); 1.134 + ResolveWithDeps.preRegister(context); 1.135 + JavaCompilerWithDeps.preRegister(context, this); 1.136 + subTasks = new ArrayList<Future<?>>(); 1.137 + } 1.138 + 1.139 + /** 1.140 + * Prepare the compiler thread for idleness. 1.141 + */ 1.142 + public synchronized void unuse() { 1.143 + assert(inUse); 1.144 + inUse = false; 1.145 + compiler = null; 1.146 + fileManager = null; 1.147 + fileManagerBase = null; 1.148 + smartFileManager = null; 1.149 + context = null; 1.150 + subTasks = null; 1.151 + } 1.152 + 1.153 + /** 1.154 + * Expect this key on the next line read from the reader. 1.155 + */ 1.156 + private static boolean expect(BufferedReader in, String key) throws IOException { 1.157 + String s = in.readLine(); 1.158 + if (s != null && s.equals(key)) { 1.159 + return true; 1.160 + } 1.161 + return false; 1.162 + } 1.163 + 1.164 + // The request identifier, for example GENERATE_NEWBYTECODE 1.165 + String id = ""; 1.166 + 1.167 + public String currentRequestId() { 1.168 + return id; 1.169 + } 1.170 + 1.171 + PrintWriter stdout; 1.172 + PrintWriter stderr; 1.173 + int forcedExitCode = 0; 1.174 + 1.175 + public void logError(String msg) { 1.176 + stderr.println(msg); 1.177 + forcedExitCode = -1; 1.178 + } 1.179 + 1.180 + /** 1.181 + * Invoked by the executor service. 1.182 + */ 1.183 + public void run() { 1.184 + // Unique nr that identifies this request. 1.185 + int thisRequest = compilerPool.startRequest(); 1.186 + long start = System.currentTimeMillis(); 1.187 + int numClasses = 0; 1.188 + StringBuilder compiledPkgs = new StringBuilder(); 1.189 + use(); 1.190 + 1.191 + PrintWriter out = null; 1.192 + try { 1.193 + javacServer.log("<"+thisRequest+"> Connect from "+socket.getRemoteSocketAddress()+" activethreads="+compilerPool.numActiveRequests()); 1.194 + BufferedReader in = new BufferedReader(new InputStreamReader( 1.195 + socket.getInputStream())); 1.196 + out = new PrintWriter(new OutputStreamWriter( 1.197 + socket.getOutputStream())); 1.198 + if (!expect(in, JavacServer.PROTOCOL_COOKIE_VERSION)) { 1.199 + javacServer.log("<"+thisRequest+"> Bad protocol from ip "+socket.getRemoteSocketAddress()); 1.200 + return; 1.201 + } 1.202 + 1.203 + String cookie = in.readLine(); 1.204 + if (cookie == null || !cookie.equals(""+javacServer.getCookie())) { 1.205 + javacServer.log("<"+thisRequest+"> Bad cookie from ip "+socket.getRemoteSocketAddress()); 1.206 + return; 1.207 + } 1.208 + if (!expect(in, JavacServer.PROTOCOL_CWD)) { 1.209 + return; 1.210 + } 1.211 + String cwd = in.readLine(); 1.212 + if (cwd == null) 1.213 + return; 1.214 + if (!expect(in, JavacServer.PROTOCOL_ID)) { 1.215 + return; 1.216 + } 1.217 + id = in.readLine(); 1.218 + if (id == null) 1.219 + return; 1.220 + if (!expect(in, JavacServer.PROTOCOL_ARGS)) { 1.221 + return; 1.222 + } 1.223 + ArrayList<String> the_options = new ArrayList<String>(); 1.224 + ArrayList<File> the_classes = new ArrayList<File>(); 1.225 + Iterable<File> path = Arrays.<File> asList(new File(cwd)); 1.226 + 1.227 + for (;;) { 1.228 + String l = in.readLine(); 1.229 + if (l == null) 1.230 + return; 1.231 + if (l.equals(JavacServer.PROTOCOL_SOURCES_TO_COMPILE)) 1.232 + break; 1.233 + if (l.startsWith("--server:")) 1.234 + continue; 1.235 + if (!l.startsWith("-") && l.endsWith(".java")) { 1.236 + the_classes.add(new File(l)); 1.237 + numClasses++; 1.238 + } else { 1.239 + the_options.add(l); 1.240 + } 1.241 + continue; 1.242 + } 1.243 + 1.244 + // Load sources to compile 1.245 + Set<URI> sourcesToCompile = new HashSet<URI>(); 1.246 + for (;;) { 1.247 + String l = in.readLine(); 1.248 + if (l == null) 1.249 + return; 1.250 + if (l.equals(JavacServer.PROTOCOL_VISIBLE_SOURCES)) 1.251 + break; 1.252 + try { 1.253 + sourcesToCompile.add(new URI(l)); 1.254 + numClasses++; 1.255 + } catch (URISyntaxException e) { 1.256 + return; 1.257 + } 1.258 + } 1.259 + // Load visible sources 1.260 + Set<URI> visibleSources = new HashSet<URI>(); 1.261 + boolean fix_drive_letter_case = System.getProperty("os.name").toLowerCase().equals("windows"); 1.262 + for (;;) { 1.263 + String l = in.readLine(); 1.264 + if (l == null) 1.265 + return; 1.266 + if (l.equals(JavacServer.PROTOCOL_END)) 1.267 + break; 1.268 + try { 1.269 + URI u = new URI(l); 1.270 + if (fix_drive_letter_case) { 1.271 + // Make sure the driver letter is lower case. 1.272 + String s = u.toString(); 1.273 + if (s.startsWith("file:/") && 1.274 + Character.isUpperCase(s.charAt(6))) { 1.275 + u = new URI("file:/"+Character.toLowerCase(s.charAt(6))+s.substring(7)); 1.276 + } 1.277 + } 1.278 + visibleSources.add(u); 1.279 + } catch (URISyntaxException e) { 1.280 + return; 1.281 + } 1.282 + } 1.283 + 1.284 + // A completed request has been received. 1.285 + 1.286 + // Now setup the actual compilation.... 1.287 + // First deal with explicit source files on cmdline and in at file. 1.288 + com.sun.tools.javac.util.ListBuffer<JavaFileObject> compilationUnits = 1.289 + new com.sun.tools.javac.util.ListBuffer<JavaFileObject>(); 1.290 + for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(the_classes)) { 1.291 + compilationUnits.append(i); 1.292 + } 1.293 + // Now deal with sources supplied as source_to_compile. 1.294 + com.sun.tools.javac.util.ListBuffer<File> sourcesToCompileFiles = 1.295 + new com.sun.tools.javac.util.ListBuffer<File>(); 1.296 + for (URI u : sourcesToCompile) { 1.297 + sourcesToCompileFiles.append(new File(u)); 1.298 + } 1.299 + for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(sourcesToCompileFiles)) { 1.300 + compilationUnits.append(i); 1.301 + } 1.302 + // Log the options to be used. 1.303 + StringBuilder options = new StringBuilder(); 1.304 + for (String s : the_options) { 1.305 + options.append(">").append(s).append("< "); 1.306 + } 1.307 + javacServer.log(id+" <"+thisRequest+"> options "+options.toString()); 1.308 + 1.309 + forcedExitCode = 0; 1.310 + // Create a new logger. 1.311 + StringWriter stdoutLog = new StringWriter(); 1.312 + StringWriter stderrLog = new StringWriter(); 1.313 + stdout = new PrintWriter(stdoutLog); 1.314 + stderr = new PrintWriter(stderrLog); 1.315 + com.sun.tools.javac.main.Main.Result rc = com.sun.tools.javac.main.Main.Result.OK; 1.316 + try { 1.317 + if (compilationUnits.size() > 0) { 1.318 + // Bind the new logger to the existing context. 1.319 + context.put(Log.outKey, stderr); 1.320 + Log.instance(context).setWriter(Log.WriterKind.NOTICE, stdout); 1.321 + Log.instance(context).setWriter(Log.WriterKind.WARNING, stderr); 1.322 + Log.instance(context).setWriter(Log.WriterKind.ERROR, stderr); 1.323 + // Process the options. 1.324 + com.sun.tools.javac.api.JavacTool.processOptions(context, smartFileManager, the_options); 1.325 + fileManagerBase.setContext(context); 1.326 + smartFileManager.setVisibleSources(visibleSources); 1.327 + smartFileManager.cleanArtifacts(); 1.328 + smartFileManager.setLog(stdout); 1.329 + Dependencies.instance(context).reset(); 1.330 + 1.331 + com.sun.tools.javac.main.Main ccompiler = new com.sun.tools.javac.main.Main("javacTask", stderr); 1.332 + String[] aa = the_options.toArray(new String[0]); 1.333 + 1.334 + // Do the compilation! 1.335 + rc = ccompiler.compile(aa, context, compilationUnits.toList(), null); 1.336 + 1.337 + while (numActiveSubTasks()>0) { 1.338 + try { Thread.sleep(1000); } catch (InterruptedException e) { } 1.339 + } 1.340 + 1.341 + smartFileManager.flush(); 1.342 + } 1.343 + } catch (Exception e) { 1.344 + stderr.println(e.getMessage()); 1.345 + forcedExitCode = -1; 1.346 + } 1.347 + 1.348 + // Send the response.. 1.349 + out.println(JavacServer.PROTOCOL_STDOUT); 1.350 + out.print(stdoutLog); 1.351 + out.println(JavacServer.PROTOCOL_STDERR); 1.352 + out.print(stderrLog); 1.353 + // The compilation is complete! And errors will have already been printed on out! 1.354 + out.println(JavacServer.PROTOCOL_PACKAGE_ARTIFACTS); 1.355 + Map<String,Set<URI>> pa = smartFileManager.getPackageArtifacts(); 1.356 + for (String aPkgName : pa.keySet()) { 1.357 + out.println("+"+aPkgName); 1.358 + Set<URI> as = pa.get(aPkgName); 1.359 + for (URI a : as) { 1.360 + out.println(" "+a.toString()); 1.361 + } 1.362 + } 1.363 + Dependencies deps = Dependencies.instance(context); 1.364 + out.println(JavacServer.PROTOCOL_PACKAGE_DEPENDENCIES); 1.365 + Map<String,Set<String>> pd = deps.getDependencies(); 1.366 + for (String aPkgName : pd.keySet()) { 1.367 + out.println("+"+aPkgName); 1.368 + Set<String> ds = pd.get(aPkgName); 1.369 + // Everything depends on java.lang 1.370 + if (!ds.contains(":java.lang")) ds.add(":java.lang"); 1.371 + for (String d : ds) { 1.372 + out.println(" "+d); 1.373 + } 1.374 + } 1.375 + out.println(JavacServer.PROTOCOL_PACKAGE_PUBLIC_APIS); 1.376 + Map<String,String> pp = deps.getPubapis(); 1.377 + for (String aPkgName : pp.keySet()) { 1.378 + out.println("+"+aPkgName); 1.379 + String ps = pp.get(aPkgName); 1.380 + // getPubapis added a space to each line! 1.381 + out.println(ps); 1.382 + compiledPkgs.append(aPkgName+" "); 1.383 + } 1.384 + out.println(JavacServer.PROTOCOL_SYSINFO); 1.385 + out.println("num_cores=" + Runtime.getRuntime().availableProcessors()); 1.386 + out.println("max_memory=" + Runtime.getRuntime().maxMemory()); 1.387 + out.println(JavacServer.PROTOCOL_RETURN_CODE); 1.388 + 1.389 + // Errors from sjavac that affect compilation status! 1.390 + int rcv = rc.exitCode; 1.391 + if (rcv == 0 && forcedExitCode != 0) { 1.392 + rcv = forcedExitCode; 1.393 + } 1.394 + out.println("" + rcv); 1.395 + out.println(JavacServer.PROTOCOL_END); 1.396 + out.flush(); 1.397 + } catch (IOException e) { 1.398 + e.printStackTrace(); 1.399 + } finally { 1.400 + try { 1.401 + if (out != null) out.close(); 1.402 + if (!socket.isClosed()) { 1.403 + socket.close(); 1.404 + } 1.405 + socket = null; 1.406 + } catch (Exception e) { 1.407 + javacServer.log("ERROR "+e); 1.408 + e.printStackTrace(); 1.409 + } 1.410 + compilerPool.stopRequest(); 1.411 + long duration = System.currentTimeMillis()-start; 1.412 + javacServer.addBuildTime(duration); 1.413 + float classpersec = ((float)numClasses)*(((float)1000.0)/((float)duration)); 1.414 + javacServer.log(id+" <"+thisRequest+"> "+compiledPkgs+" duration " + duration+ " ms num_classes="+numClasses+ 1.415 + " classpersec="+classpersec+" subtasks="+subTasks.size()); 1.416 + javacServer.flushLog(); 1.417 + unuse(); 1.418 + compilerPool.returnCompilerThread(this); 1.419 + } 1.420 + } 1.421 +} 1.422 +