ohrstrom@1504: /*
ohrstrom@1504: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
ohrstrom@1504: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@1504: *
ohrstrom@1504: * This code is free software; you can redistribute it and/or modify it
ohrstrom@1504: * under the terms of the GNU General Public License version 2 only, as
ohrstrom@1504: * published by the Free Software Foundation. Oracle designates this
ohrstrom@1504: * particular file as subject to the "Classpath" exception as provided
ohrstrom@1504: * by Oracle in the LICENSE file that accompanied this code.
ohrstrom@1504: *
ohrstrom@1504: * This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@1504: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@1504: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@1504: * version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@1504: * accompanied this code).
ohrstrom@1504: *
ohrstrom@1504: * You should have received a copy of the GNU General Public License version
ohrstrom@1504: * 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@1504: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@1504: *
ohrstrom@1504: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@1504: * or visit www.oracle.com if you need additional information or have any
ohrstrom@1504: * questions.
ohrstrom@1504: */
ohrstrom@1504:
ohrstrom@1504: package com.sun.tools.sjavac;
ohrstrom@1504:
ohrstrom@1504: import java.net.URI;
ohrstrom@1504: import java.util.Arrays;
ohrstrom@1504: import java.util.Random;
ohrstrom@1504: import java.util.Set;
ohrstrom@1504: import java.util.Map;
ohrstrom@1504:
ohrstrom@1504: import com.sun.tools.sjavac.server.JavacServer;
ohrstrom@1504: import com.sun.tools.sjavac.server.SysInfo;
ohrstrom@1504: import java.io.PrintStream;
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * This transform compiles a set of packages containing Java sources.
ohrstrom@1504: * The compile request is divided into separate sets of source files.
ohrstrom@1504: * For each set a separate request thread is dispatched to a javac server
ohrstrom@1504: * and the meta data is accumulated. The number of sets correspond more or
ohrstrom@1504: * less to the number of cores. Less so now, than it will in the future.
ohrstrom@1504: *
ohrstrom@1504: *
This is NOT part of any supported API.
ohrstrom@1504: * If you write code that depends on this, you do so at your own
ohrstrom@1504: * risk. This code and its internal interfaces are subject to change
ohrstrom@1504: * or deletion without notice.
ohrstrom@1504: */
ohrstrom@1504: public class CompileJavaPackages implements Transformer {
ohrstrom@1504:
ohrstrom@1504: // The current limited sharing of data between concurrent JavaCompilers
ohrstrom@1504: // in the server will not give speedups above 3 cores. Thus this limit.
ohrstrom@1504: // We hope to improve this in the future.
ohrstrom@1504: final static int limitOnConcurrency = 3;
ohrstrom@1504:
ohrstrom@1504: String serverSettings;
ohrstrom@1504: public void setExtra(String e) {
ohrstrom@1504: serverSettings = e;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: String[] args;
ohrstrom@1504: public void setExtra(String[] a) {
ohrstrom@1504: args = a;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: public boolean transform(Map> pkgSrcs,
ohrstrom@1504: Set visibleSources,
ohrstrom@1504: Map> visibleClasses,
ohrstrom@1504: Map> oldPackageDependents,
ohrstrom@1504: URI destRoot,
ohrstrom@1504: final Map> packageArtifacts,
ohrstrom@1504: final Map> packageDependencies,
ohrstrom@1504: final Map packagePubapis,
ohrstrom@1504: int debugLevel,
ohrstrom@1504: boolean incremental,
ohrstrom@1504: int numCores,
ohrstrom@1504: PrintStream out,
ohrstrom@1504: PrintStream err)
ohrstrom@1504: {
ohrstrom@1504: boolean rc = true;
ohrstrom@1504: boolean concurrentCompiles = true;
ohrstrom@1504:
ohrstrom@1504: // Fetch the id.
ohrstrom@1504: String id = Util.extractStringOption("id", serverSettings);
ohrstrom@1504: if (id == null || id.equals("")) {
ohrstrom@1504: // No explicit id set. Create a random id so that the requests can be
ohrstrom@1504: // grouped properly in the server.
ohrstrom@1504: id = "id"+(((new Random()).nextLong())&Long.MAX_VALUE);
ohrstrom@1504: }
ohrstrom@1504: // Only keep portfile and sjavac settings..
ohrstrom@1504: String psServerSettings = Util.cleanSubOptions("--server:", Util.set("portfile","sjavac","background","keepalive"), serverSettings);
ohrstrom@1504:
ohrstrom@1504: // Get maximum heap size from the server!
ohrstrom@1504: SysInfo sysinfo = JavacServer.connectGetSysInfo(psServerSettings, out, err);
ohrstrom@1504: if (sysinfo.numCores == -1) {
ohrstrom@1504: Log.error("Could not query server for sysinfo!");
ohrstrom@1504: return false;
ohrstrom@1504: }
ohrstrom@1504: int numMBytes = (int)(sysinfo.maxMemory / ((long)(1024*1024)));
ohrstrom@1504: Log.debug("Server reports "+numMBytes+"MiB of memory and "+sysinfo.numCores+" cores");
ohrstrom@1504:
ohrstrom@1504: if (numCores <= 0) {
ohrstrom@1504: // Set the requested number of cores to the number of cores on the server.
ohrstrom@1504: numCores = sysinfo.numCores;
ohrstrom@1504: Log.debug("Number of jobs not explicitly set, defaulting to "+sysinfo.numCores);
ohrstrom@1504: } else if (sysinfo.numCores < numCores) {
ohrstrom@1504: // Set the requested number of cores to the number of cores on the server.
ohrstrom@1504: Log.debug("Limiting jobs from explicitly set "+numCores+" to cores available on server: "+sysinfo.numCores);
ohrstrom@1504: numCores = sysinfo.numCores;
ohrstrom@1504: } else {
ohrstrom@1504: Log.debug("Number of jobs explicitly set to "+numCores);
ohrstrom@1504: }
ohrstrom@1504: // More than three concurrent cores does not currently give a speedup, at least for compiling the jdk
ohrstrom@1504: // in the OpenJDK. This will change in the future.
ohrstrom@1504: int numCompiles = numCores;
ohrstrom@1504: if (numCores > limitOnConcurrency) numCompiles = limitOnConcurrency;
ohrstrom@1504: // Split the work up in chunks to compiled.
ohrstrom@1504:
ohrstrom@1504: int numSources = 0;
ohrstrom@1504: for (String s : pkgSrcs.keySet()) {
ohrstrom@1504: Set ss = pkgSrcs.get(s);
ohrstrom@1504: numSources += ss.size();
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: int sourcesPerCompile = numSources / numCompiles;
ohrstrom@1504:
ohrstrom@1504: // For 64 bit Java, it seems we can compile the OpenJDK 8800 files with a 1500M of heap
ohrstrom@1504: // in a single chunk, with reasonable performance.
ohrstrom@1504: // For 32 bit java, it seems we need 1G of heap.
ohrstrom@1504: // Number experimentally determined when compiling the OpenJDK.
ohrstrom@1504: // Includes space for reasonably efficient garbage collection etc,
ohrstrom@1504: // Calculating backwards gives us a requirement of
ohrstrom@1504: // 1500M/8800 = 175 KiB for 64 bit platforms
ohrstrom@1504: // and 1G/8800 = 119 KiB for 32 bit platform
ohrstrom@1504: // for each compile.....
ohrstrom@1504: int kbPerFile = 175;
ohrstrom@1504: String osarch = System.getProperty("os.arch");
erikj@1861: String dataModel = System.getProperty("sun.arch.data.model");
erikj@1861: if ("32".equals(dataModel)) {
ohrstrom@1504: // For 32 bit platforms, assume it is slightly smaller
ohrstrom@1504: // because of smaller object headers and pointers.
ohrstrom@1504: kbPerFile = 119;
ohrstrom@1504: }
ohrstrom@1504: int numRequiredMBytes = (kbPerFile*numSources)/1024;
ohrstrom@1504: Log.debug("For os.arch "+osarch+" the empirically determined heap required per file is "+kbPerFile+"KiB");
ohrstrom@1504: Log.debug("Server has "+numMBytes+"MiB of heap.");
ohrstrom@1504: Log.debug("Heuristics say that we need "+numRequiredMBytes+"MiB of heap for all source files.");
ohrstrom@1504: // Perform heuristics to see how many cores we can use,
ohrstrom@1504: // or if we have to the work serially in smaller chunks.
ohrstrom@1504: if (numMBytes < numRequiredMBytes) {
ohrstrom@1504: // Ouch, cannot fit even a single compile into the heap.
ohrstrom@1504: // Split it up into several serial chunks.
ohrstrom@1504: concurrentCompiles = false;
ohrstrom@1504: // Limit the number of sources for each compile to 500.
ohrstrom@1504: if (numSources < 500) {
ohrstrom@1504: numCompiles = 1;
ohrstrom@1504: sourcesPerCompile = numSources;
ohrstrom@1504: Log.debug("Compiling as a single source code chunk to stay within heap size limitations!");
ohrstrom@1504: } else if (sourcesPerCompile > 500) {
ohrstrom@1504: // This number is very low, and tuned to dealing with the OpenJDK
ohrstrom@1504: // where the source is >very< circular! In normal application,
ohrstrom@1504: // with less circularity the number could perhaps be increased.
ohrstrom@1504: numCompiles = numSources / 500;
ohrstrom@1504: sourcesPerCompile = numSources/numCompiles;
ohrstrom@1504: Log.debug("Compiling source as "+numCompiles+" code chunks serially to stay within heap size limitations!");
ohrstrom@1504: }
ohrstrom@1504: } else {
ohrstrom@1504: if (numCompiles > 1) {
ohrstrom@1504: // Ok, we can fit at least one full compilation on the heap.
ohrstrom@1504: float usagePerCompile = (float)numRequiredMBytes / ((float)numCompiles * (float)0.7);
ohrstrom@1504: int usage = (int)(usagePerCompile * (float)numCompiles);
ohrstrom@1504: Log.debug("Heuristics say that for "+numCompiles+" concurrent compiles we need "+usage+"MiB");
ohrstrom@1504: if (usage > numMBytes) {
ohrstrom@1504: // Ouch it does not fit. Reduce to a single chunk.
ohrstrom@1504: numCompiles = 1;
ohrstrom@1504: sourcesPerCompile = numSources;
ohrstrom@1504: // What if the relationship betweem number of compile_chunks and num_required_mbytes
ohrstrom@1504: // is not linear? Then perhaps 2 chunks would fit where 3 does not. Well, this is
ohrstrom@1504: // something to experiment upon in the future.
ohrstrom@1504: Log.debug("Limiting compile to a single thread to stay within heap size limitations!");
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: Log.debug("Compiling sources in "+numCompiles+" chunk(s)");
ohrstrom@1504:
ohrstrom@1504: // Create the chunks to be compiled.
ohrstrom@1504: final CompileChunk[] compileChunks = createCompileChunks(pkgSrcs, oldPackageDependents,
ohrstrom@1504: numCompiles, sourcesPerCompile);
ohrstrom@1504:
ohrstrom@1504: if (Log.isDebugging()) {
ohrstrom@1504: int cn = 1;
ohrstrom@1504: for (CompileChunk cc : compileChunks) {
ohrstrom@1504: Log.debug("Chunk "+cn+" for "+id+" ---------------");
ohrstrom@1504: cn++;
ohrstrom@1504: for (URI u : cc.srcs) {
ohrstrom@1504: Log.debug(""+u);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: // The return values for each chunked compile.
ohrstrom@1504: final int[] rn = new int[numCompiles];
ohrstrom@1504: // The requets, might or might not run as a background thread.
ohrstrom@1504: final Thread[] requests = new Thread[numCompiles];
ohrstrom@1504:
ohrstrom@1504: final Set fvisible_sources = visibleSources;
ohrstrom@1504: final Map> fvisible_classes = visibleClasses;
ohrstrom@1504:
ohrstrom@1504: long start = System.currentTimeMillis();
ohrstrom@1504:
ohrstrom@1504: for (int i=0; i 0) {
ohrstrom@1504: String numdeps = "";
ohrstrom@1504: if (cc.numDependents > 0) numdeps = "(with "+cc.numDependents+" dependents) ";
ohrstrom@1504: if (!incremental || cc.numPackages > 16) {
ohrstrom@1504: String info = "("+cc.pkgFromTos+")";
ohrstrom@1504: if (info.equals("( to )")) {
ohrstrom@1504: info = "";
ohrstrom@1504: }
ohrstrom@1504: Log.info("Compiling "+cc.srcs.size()+" files "+numdeps+"in "+cc.numPackages+" packages "+info);
ohrstrom@1504: } else {
ohrstrom@1504: Log.info("Compiling "+cc.pkgNames+numdeps);
ohrstrom@1504: }
ohrstrom@1504: if (concurrentCompiles) {
ohrstrom@1504: requests[ii].start();
ohrstrom@1504: }
ohrstrom@1504: else {
ohrstrom@1504: requests[ii].run();
ohrstrom@1504: // If there was an error, then stop early when running single threaded.
ohrstrom@1504: if (rn[i] != 0) {
ohrstrom@1504: return false;
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: if (concurrentCompiles) {
ohrstrom@1504: // If there are background threads for the concurrent compiles, then join them.
ohrstrom@1504: for (int i=0; i 0) {
ohrstrom@1504: if (rn[i] != 0) {
ohrstrom@1504: rc = false;
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: long duration = System.currentTimeMillis() - start;
ohrstrom@1504: long minutes = duration/60000;
ohrstrom@1504: long seconds = (duration-minutes*60000)/1000;
ohrstrom@1504: Log.debug("Compilation of "+numSources+" source files took "+minutes+"m "+seconds+"s");
ohrstrom@1504:
ohrstrom@1504: return rc;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Split up the sources into compile chunks. If old package dependents information
ohrstrom@1504: * is available, sort the order of the chunks into the most dependent first!
ohrstrom@1504: * (Typically that chunk contains the java.lang package.) In the future
ohrstrom@1504: * we could perhaps improve the heuristics to put the sources into even more sensible chunks.
ohrstrom@1504: * Now the package are simple sorted in alphabetical order and chunked, then the chunks
ohrstrom@1504: * are sorted on how dependent they are.
ohrstrom@1504: *
ohrstrom@1504: * @param pkgSrcs The sources to compile.
ohrstrom@1504: * @param oldPackageDependents Old package dependents, if non-empty, used to sort the chunks.
ohrstrom@1504: * @param numCompiles The number of chunks.
ohrstrom@1504: * @param sourcesPerCompile The number of sources per chunk.
ohrstrom@1504: * @return
ohrstrom@1504: */
ohrstrom@1504: CompileChunk[] createCompileChunks(Map> pkgSrcs,
ohrstrom@1504: Map> oldPackageDependents,
ohrstrom@1504: int numCompiles,
ohrstrom@1504: int sourcesPerCompile) {
ohrstrom@1504:
ohrstrom@1504: CompileChunk[] compileChunks = new CompileChunk[numCompiles];
ohrstrom@1504: for (int i=0; i s = pkgSrcs.get(pkgName);
ohrstrom@1504: if (cc.srcs.size()+s.size() > sourcesPerCompile && ci < numCompiles-1) {
ohrstrom@1504: from = null;
ohrstrom@1504: ci++;
ohrstrom@1504: cc = compileChunks[ci];
ohrstrom@1504: }
ohrstrom@1504: cc.numPackages++;
ohrstrom@1504: cc.srcs.addAll(s);
ohrstrom@1504:
ohrstrom@1504: // Calculate nice package names to use as information when compiling.
ohrstrom@1504: String justPkgName = Util.justPackageName(pkgName);
ohrstrom@1504: // Fetch how many packages depend on this package from the old build state.
ohrstrom@1504: Set ss = oldPackageDependents.get(pkgName);
ohrstrom@1504: if (ss != null) {
ohrstrom@1504: // Accumulate this information onto this chunk.
ohrstrom@1504: cc.numDependents += ss.size();
ohrstrom@1504: }
ohrstrom@1504: if (from == null || from.trim().equals("")) from = justPkgName;
ohrstrom@1504: cc.pkgNames.append(justPkgName+"("+s.size()+") ");
ohrstrom@1504: cc.pkgFromTos = from+" to "+justPkgName;
ohrstrom@1504: }
ohrstrom@1504: // If we are compiling serially, sort the chunks, so that the chunk (with the most dependents) (usually the chunk
ohrstrom@1504: // containing java.lang.Object, is to be compiled first!
ohrstrom@1504: // For concurrent compilation, this does not matter.
ohrstrom@1504: Arrays.sort(compileChunks);
ohrstrom@1504: return compileChunks;
ohrstrom@1504: }
ohrstrom@1504: }