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

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

mercurial