src/share/classes/com/sun/tools/sjavac/CompileJavaPackages.java

Mon, 11 Mar 2013 19:03:35 -0700

author
ohrstrom
date
Mon, 11 Mar 2013 19:03:35 -0700
changeset 1625
fbb6e470079d
parent 1504
22e417cdddee
child 1861
dcc6a52bf363
permissions
-rw-r--r--

8009843: sjavac should accept -cp as synonym for -classpath
Reviewed-by: jjg

ohrstrom@1504 1 /*
ohrstrom@1504 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
ohrstrom@1504 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@1504 4 *
ohrstrom@1504 5 * This code is free software; you can redistribute it and/or modify it
ohrstrom@1504 6 * under the terms of the GNU General Public License version 2 only, as
ohrstrom@1504 7 * published by the Free Software Foundation. Oracle designates this
ohrstrom@1504 8 * particular file as subject to the "Classpath" exception as provided
ohrstrom@1504 9 * by Oracle in the LICENSE file that accompanied this code.
ohrstrom@1504 10 *
ohrstrom@1504 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@1504 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@1504 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@1504 14 * version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@1504 15 * accompanied this code).
ohrstrom@1504 16 *
ohrstrom@1504 17 * You should have received a copy of the GNU General Public License version
ohrstrom@1504 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@1504 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@1504 20 *
ohrstrom@1504 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@1504 22 * or visit www.oracle.com if you need additional information or have any
ohrstrom@1504 23 * questions.
ohrstrom@1504 24 */
ohrstrom@1504 25
ohrstrom@1504 26 package com.sun.tools.sjavac;
ohrstrom@1504 27
ohrstrom@1504 28 import java.net.URI;
ohrstrom@1504 29 import java.util.Arrays;
ohrstrom@1504 30 import java.util.Random;
ohrstrom@1504 31 import java.util.Set;
ohrstrom@1504 32 import java.util.Map;
ohrstrom@1504 33
ohrstrom@1504 34 import com.sun.tools.sjavac.server.JavacServer;
ohrstrom@1504 35 import com.sun.tools.sjavac.server.SysInfo;
ohrstrom@1504 36 import java.io.PrintStream;
ohrstrom@1504 37
ohrstrom@1504 38 /**
ohrstrom@1504 39 * This transform compiles a set of packages containing Java sources.
ohrstrom@1504 40 * The compile request is divided into separate sets of source files.
ohrstrom@1504 41 * For each set a separate request thread is dispatched to a javac server
ohrstrom@1504 42 * and the meta data is accumulated. The number of sets correspond more or
ohrstrom@1504 43 * less to the number of cores. Less so now, than it will in the future.
ohrstrom@1504 44 *
ohrstrom@1504 45 * <p><b>This is NOT part of any supported API.
ohrstrom@1504 46 * If you write code that depends on this, you do so at your own
ohrstrom@1504 47 * risk. This code and its internal interfaces are subject to change
ohrstrom@1504 48 * or deletion without notice.</b></p>
ohrstrom@1504 49 */
ohrstrom@1504 50 public class CompileJavaPackages implements Transformer {
ohrstrom@1504 51
ohrstrom@1504 52 // The current limited sharing of data between concurrent JavaCompilers
ohrstrom@1504 53 // in the server will not give speedups above 3 cores. Thus this limit.
ohrstrom@1504 54 // We hope to improve this in the future.
ohrstrom@1504 55 final static int limitOnConcurrency = 3;
ohrstrom@1504 56
ohrstrom@1504 57 String serverSettings;
ohrstrom@1504 58 public void setExtra(String e) {
ohrstrom@1504 59 serverSettings = e;
ohrstrom@1504 60 }
ohrstrom@1504 61
ohrstrom@1504 62 String[] args;
ohrstrom@1504 63 public void setExtra(String[] a) {
ohrstrom@1504 64 args = a;
ohrstrom@1504 65 }
ohrstrom@1504 66
ohrstrom@1504 67 public boolean transform(Map<String,Set<URI>> pkgSrcs,
ohrstrom@1504 68 Set<URI> visibleSources,
ohrstrom@1504 69 Map<URI,Set<String>> visibleClasses,
ohrstrom@1504 70 Map<String,Set<String>> oldPackageDependents,
ohrstrom@1504 71 URI destRoot,
ohrstrom@1504 72 final Map<String,Set<URI>> packageArtifacts,
ohrstrom@1504 73 final Map<String,Set<String>> packageDependencies,
ohrstrom@1504 74 final Map<String,String> packagePubapis,
ohrstrom@1504 75 int debugLevel,
ohrstrom@1504 76 boolean incremental,
ohrstrom@1504 77 int numCores,
ohrstrom@1504 78 PrintStream out,
ohrstrom@1504 79 PrintStream err)
ohrstrom@1504 80 {
ohrstrom@1504 81 boolean rc = true;
ohrstrom@1504 82 boolean concurrentCompiles = true;
ohrstrom@1504 83
ohrstrom@1504 84 // Fetch the id.
ohrstrom@1504 85 String id = Util.extractStringOption("id", serverSettings);
ohrstrom@1504 86 if (id == null || id.equals("")) {
ohrstrom@1504 87 // No explicit id set. Create a random id so that the requests can be
ohrstrom@1504 88 // grouped properly in the server.
ohrstrom@1504 89 id = "id"+(((new Random()).nextLong())&Long.MAX_VALUE);
ohrstrom@1504 90 }
ohrstrom@1504 91 // Only keep portfile and sjavac settings..
ohrstrom@1504 92 String psServerSettings = Util.cleanSubOptions("--server:", Util.set("portfile","sjavac","background","keepalive"), serverSettings);
ohrstrom@1504 93
ohrstrom@1504 94 // Get maximum heap size from the server!
ohrstrom@1504 95 SysInfo sysinfo = JavacServer.connectGetSysInfo(psServerSettings, out, err);
ohrstrom@1504 96 if (sysinfo.numCores == -1) {
ohrstrom@1504 97 Log.error("Could not query server for sysinfo!");
ohrstrom@1504 98 return false;
ohrstrom@1504 99 }
ohrstrom@1504 100 int numMBytes = (int)(sysinfo.maxMemory / ((long)(1024*1024)));
ohrstrom@1504 101 Log.debug("Server reports "+numMBytes+"MiB of memory and "+sysinfo.numCores+" cores");
ohrstrom@1504 102
ohrstrom@1504 103 if (numCores <= 0) {
ohrstrom@1504 104 // Set the requested number of cores to the number of cores on the server.
ohrstrom@1504 105 numCores = sysinfo.numCores;
ohrstrom@1504 106 Log.debug("Number of jobs not explicitly set, defaulting to "+sysinfo.numCores);
ohrstrom@1504 107 } else if (sysinfo.numCores < numCores) {
ohrstrom@1504 108 // Set the requested number of cores to the number of cores on the server.
ohrstrom@1504 109 Log.debug("Limiting jobs from explicitly set "+numCores+" to cores available on server: "+sysinfo.numCores);
ohrstrom@1504 110 numCores = sysinfo.numCores;
ohrstrom@1504 111 } else {
ohrstrom@1504 112 Log.debug("Number of jobs explicitly set to "+numCores);
ohrstrom@1504 113 }
ohrstrom@1504 114 // More than three concurrent cores does not currently give a speedup, at least for compiling the jdk
ohrstrom@1504 115 // in the OpenJDK. This will change in the future.
ohrstrom@1504 116 int numCompiles = numCores;
ohrstrom@1504 117 if (numCores > limitOnConcurrency) numCompiles = limitOnConcurrency;
ohrstrom@1504 118 // Split the work up in chunks to compiled.
ohrstrom@1504 119
ohrstrom@1504 120 int numSources = 0;
ohrstrom@1504 121 for (String s : pkgSrcs.keySet()) {
ohrstrom@1504 122 Set<URI> ss = pkgSrcs.get(s);
ohrstrom@1504 123 numSources += ss.size();
ohrstrom@1504 124 }
ohrstrom@1504 125
ohrstrom@1504 126 int sourcesPerCompile = numSources / numCompiles;
ohrstrom@1504 127
ohrstrom@1504 128 // For 64 bit Java, it seems we can compile the OpenJDK 8800 files with a 1500M of heap
ohrstrom@1504 129 // in a single chunk, with reasonable performance.
ohrstrom@1504 130 // For 32 bit java, it seems we need 1G of heap.
ohrstrom@1504 131 // Number experimentally determined when compiling the OpenJDK.
ohrstrom@1504 132 // Includes space for reasonably efficient garbage collection etc,
ohrstrom@1504 133 // Calculating backwards gives us a requirement of
ohrstrom@1504 134 // 1500M/8800 = 175 KiB for 64 bit platforms
ohrstrom@1504 135 // and 1G/8800 = 119 KiB for 32 bit platform
ohrstrom@1504 136 // for each compile.....
ohrstrom@1504 137 int kbPerFile = 175;
ohrstrom@1504 138 String osarch = System.getProperty("os.arch");
ohrstrom@1504 139 if (osarch.equals("i386")) {
ohrstrom@1504 140 // For 32 bit platforms, assume it is slightly smaller
ohrstrom@1504 141 // because of smaller object headers and pointers.
ohrstrom@1504 142 kbPerFile = 119;
ohrstrom@1504 143 }
ohrstrom@1504 144 int numRequiredMBytes = (kbPerFile*numSources)/1024;
ohrstrom@1504 145 Log.debug("For os.arch "+osarch+" the empirically determined heap required per file is "+kbPerFile+"KiB");
ohrstrom@1504 146 Log.debug("Server has "+numMBytes+"MiB of heap.");
ohrstrom@1504 147 Log.debug("Heuristics say that we need "+numRequiredMBytes+"MiB of heap for all source files.");
ohrstrom@1504 148 // Perform heuristics to see how many cores we can use,
ohrstrom@1504 149 // or if we have to the work serially in smaller chunks.
ohrstrom@1504 150 if (numMBytes < numRequiredMBytes) {
ohrstrom@1504 151 // Ouch, cannot fit even a single compile into the heap.
ohrstrom@1504 152 // Split it up into several serial chunks.
ohrstrom@1504 153 concurrentCompiles = false;
ohrstrom@1504 154 // Limit the number of sources for each compile to 500.
ohrstrom@1504 155 if (numSources < 500) {
ohrstrom@1504 156 numCompiles = 1;
ohrstrom@1504 157 sourcesPerCompile = numSources;
ohrstrom@1504 158 Log.debug("Compiling as a single source code chunk to stay within heap size limitations!");
ohrstrom@1504 159 } else if (sourcesPerCompile > 500) {
ohrstrom@1504 160 // This number is very low, and tuned to dealing with the OpenJDK
ohrstrom@1504 161 // where the source is >very< circular! In normal application,
ohrstrom@1504 162 // with less circularity the number could perhaps be increased.
ohrstrom@1504 163 numCompiles = numSources / 500;
ohrstrom@1504 164 sourcesPerCompile = numSources/numCompiles;
ohrstrom@1504 165 Log.debug("Compiling source as "+numCompiles+" code chunks serially to stay within heap size limitations!");
ohrstrom@1504 166 }
ohrstrom@1504 167 } else {
ohrstrom@1504 168 if (numCompiles > 1) {
ohrstrom@1504 169 // Ok, we can fit at least one full compilation on the heap.
ohrstrom@1504 170 float usagePerCompile = (float)numRequiredMBytes / ((float)numCompiles * (float)0.7);
ohrstrom@1504 171 int usage = (int)(usagePerCompile * (float)numCompiles);
ohrstrom@1504 172 Log.debug("Heuristics say that for "+numCompiles+" concurrent compiles we need "+usage+"MiB");
ohrstrom@1504 173 if (usage > numMBytes) {
ohrstrom@1504 174 // Ouch it does not fit. Reduce to a single chunk.
ohrstrom@1504 175 numCompiles = 1;
ohrstrom@1504 176 sourcesPerCompile = numSources;
ohrstrom@1504 177 // What if the relationship betweem number of compile_chunks and num_required_mbytes
ohrstrom@1504 178 // is not linear? Then perhaps 2 chunks would fit where 3 does not. Well, this is
ohrstrom@1504 179 // something to experiment upon in the future.
ohrstrom@1504 180 Log.debug("Limiting compile to a single thread to stay within heap size limitations!");
ohrstrom@1504 181 }
ohrstrom@1504 182 }
ohrstrom@1504 183 }
ohrstrom@1504 184
ohrstrom@1504 185 Log.debug("Compiling sources in "+numCompiles+" chunk(s)");
ohrstrom@1504 186
ohrstrom@1504 187 // Create the chunks to be compiled.
ohrstrom@1504 188 final CompileChunk[] compileChunks = createCompileChunks(pkgSrcs, oldPackageDependents,
ohrstrom@1504 189 numCompiles, sourcesPerCompile);
ohrstrom@1504 190
ohrstrom@1504 191 if (Log.isDebugging()) {
ohrstrom@1504 192 int cn = 1;
ohrstrom@1504 193 for (CompileChunk cc : compileChunks) {
ohrstrom@1504 194 Log.debug("Chunk "+cn+" for "+id+" ---------------");
ohrstrom@1504 195 cn++;
ohrstrom@1504 196 for (URI u : cc.srcs) {
ohrstrom@1504 197 Log.debug(""+u);
ohrstrom@1504 198 }
ohrstrom@1504 199 }
ohrstrom@1504 200 }
ohrstrom@1504 201
ohrstrom@1504 202 // The return values for each chunked compile.
ohrstrom@1504 203 final int[] rn = new int[numCompiles];
ohrstrom@1504 204 // The requets, might or might not run as a background thread.
ohrstrom@1504 205 final Thread[] requests = new Thread[numCompiles];
ohrstrom@1504 206
ohrstrom@1504 207 final Set<URI> fvisible_sources = visibleSources;
ohrstrom@1504 208 final Map<URI,Set<String>> fvisible_classes = visibleClasses;
ohrstrom@1504 209
ohrstrom@1504 210 long start = System.currentTimeMillis();
ohrstrom@1504 211
ohrstrom@1504 212 for (int i=0; i<numCompiles; ++i) {
ohrstrom@1504 213 final int ii = i;
ohrstrom@1504 214 final CompileChunk cc = compileChunks[i];
ohrstrom@1504 215
ohrstrom@1504 216 // Pass the num_cores and the id (appended with the chunk number) to the server.
ohrstrom@1504 217 final String cleanedServerSettings = psServerSettings+",poolsize="+numCores+",id="+id+"-"+ii;
ohrstrom@1504 218 final PrintStream fout = out;
ohrstrom@1504 219 final PrintStream ferr = err;
ohrstrom@1504 220
ohrstrom@1504 221 requests[ii] = new Thread() {
ohrstrom@1504 222 @Override
ohrstrom@1504 223 public void run() {
ohrstrom@1504 224 rn[ii] = JavacServer.useServer(cleanedServerSettings,
ohrstrom@1504 225 Main.removeWrapperArgs(args),
ohrstrom@1504 226 cc.srcs,
ohrstrom@1504 227 fvisible_sources,
ohrstrom@1504 228 fvisible_classes,
ohrstrom@1504 229 packageArtifacts,
ohrstrom@1504 230 packageDependencies,
ohrstrom@1504 231 packagePubapis,
ohrstrom@1504 232 null,
ohrstrom@1504 233 fout, ferr);
ohrstrom@1504 234 }
ohrstrom@1504 235 };
ohrstrom@1504 236
ohrstrom@1504 237 if (cc.srcs.size() > 0) {
ohrstrom@1504 238 String numdeps = "";
ohrstrom@1504 239 if (cc.numDependents > 0) numdeps = "(with "+cc.numDependents+" dependents) ";
ohrstrom@1504 240 if (!incremental || cc.numPackages > 16) {
ohrstrom@1504 241 String info = "("+cc.pkgFromTos+")";
ohrstrom@1504 242 if (info.equals("( to )")) {
ohrstrom@1504 243 info = "";
ohrstrom@1504 244 }
ohrstrom@1504 245 Log.info("Compiling "+cc.srcs.size()+" files "+numdeps+"in "+cc.numPackages+" packages "+info);
ohrstrom@1504 246 } else {
ohrstrom@1504 247 Log.info("Compiling "+cc.pkgNames+numdeps);
ohrstrom@1504 248 }
ohrstrom@1504 249 if (concurrentCompiles) {
ohrstrom@1504 250 requests[ii].start();
ohrstrom@1504 251 }
ohrstrom@1504 252 else {
ohrstrom@1504 253 requests[ii].run();
ohrstrom@1504 254 // If there was an error, then stop early when running single threaded.
ohrstrom@1504 255 if (rn[i] != 0) {
ohrstrom@1504 256 return false;
ohrstrom@1504 257 }
ohrstrom@1504 258 }
ohrstrom@1504 259 }
ohrstrom@1504 260 }
ohrstrom@1504 261 if (concurrentCompiles) {
ohrstrom@1504 262 // If there are background threads for the concurrent compiles, then join them.
ohrstrom@1504 263 for (int i=0; i<numCompiles; ++i) {
ohrstrom@1504 264 try { requests[i].join(); } catch (InterruptedException e) { }
ohrstrom@1504 265 }
ohrstrom@1504 266 }
ohrstrom@1504 267
ohrstrom@1504 268 // Check the return values.
ohrstrom@1504 269 for (int i=0; i<numCompiles; ++i) {
ohrstrom@1504 270 if (compileChunks[i].srcs.size() > 0) {
ohrstrom@1504 271 if (rn[i] != 0) {
ohrstrom@1504 272 rc = false;
ohrstrom@1504 273 }
ohrstrom@1504 274 }
ohrstrom@1504 275 }
ohrstrom@1504 276 long duration = System.currentTimeMillis() - start;
ohrstrom@1504 277 long minutes = duration/60000;
ohrstrom@1504 278 long seconds = (duration-minutes*60000)/1000;
ohrstrom@1504 279 Log.debug("Compilation of "+numSources+" source files took "+minutes+"m "+seconds+"s");
ohrstrom@1504 280
ohrstrom@1504 281 return rc;
ohrstrom@1504 282 }
ohrstrom@1504 283
ohrstrom@1504 284
ohrstrom@1504 285 /**
ohrstrom@1504 286 * Split up the sources into compile chunks. If old package dependents information
ohrstrom@1504 287 * is available, sort the order of the chunks into the most dependent first!
ohrstrom@1504 288 * (Typically that chunk contains the java.lang package.) In the future
ohrstrom@1504 289 * we could perhaps improve the heuristics to put the sources into even more sensible chunks.
ohrstrom@1504 290 * Now the package are simple sorted in alphabetical order and chunked, then the chunks
ohrstrom@1504 291 * are sorted on how dependent they are.
ohrstrom@1504 292 *
ohrstrom@1504 293 * @param pkgSrcs The sources to compile.
ohrstrom@1504 294 * @param oldPackageDependents Old package dependents, if non-empty, used to sort the chunks.
ohrstrom@1504 295 * @param numCompiles The number of chunks.
ohrstrom@1504 296 * @param sourcesPerCompile The number of sources per chunk.
ohrstrom@1504 297 * @return
ohrstrom@1504 298 */
ohrstrom@1504 299 CompileChunk[] createCompileChunks(Map<String,Set<URI>> pkgSrcs,
ohrstrom@1504 300 Map<String,Set<String>> oldPackageDependents,
ohrstrom@1504 301 int numCompiles,
ohrstrom@1504 302 int sourcesPerCompile) {
ohrstrom@1504 303
ohrstrom@1504 304 CompileChunk[] compileChunks = new CompileChunk[numCompiles];
ohrstrom@1504 305 for (int i=0; i<compileChunks.length; ++i) {
ohrstrom@1504 306 compileChunks[i] = new CompileChunk();
ohrstrom@1504 307 }
ohrstrom@1504 308
ohrstrom@1504 309 // Now go through the packages and spread out the source on the different chunks.
ohrstrom@1504 310 int ci = 0;
ohrstrom@1504 311 // Sort the packages
ohrstrom@1504 312 String[] packageNames = pkgSrcs.keySet().toArray(new String[0]);
ohrstrom@1504 313 Arrays.sort(packageNames);
ohrstrom@1504 314 String from = null;
ohrstrom@1504 315 for (String pkgName : packageNames) {
ohrstrom@1504 316 CompileChunk cc = compileChunks[ci];
ohrstrom@1504 317 Set<URI> s = pkgSrcs.get(pkgName);
ohrstrom@1504 318 if (cc.srcs.size()+s.size() > sourcesPerCompile && ci < numCompiles-1) {
ohrstrom@1504 319 from = null;
ohrstrom@1504 320 ci++;
ohrstrom@1504 321 cc = compileChunks[ci];
ohrstrom@1504 322 }
ohrstrom@1504 323 cc.numPackages++;
ohrstrom@1504 324 cc.srcs.addAll(s);
ohrstrom@1504 325
ohrstrom@1504 326 // Calculate nice package names to use as information when compiling.
ohrstrom@1504 327 String justPkgName = Util.justPackageName(pkgName);
ohrstrom@1504 328 // Fetch how many packages depend on this package from the old build state.
ohrstrom@1504 329 Set<String> ss = oldPackageDependents.get(pkgName);
ohrstrom@1504 330 if (ss != null) {
ohrstrom@1504 331 // Accumulate this information onto this chunk.
ohrstrom@1504 332 cc.numDependents += ss.size();
ohrstrom@1504 333 }
ohrstrom@1504 334 if (from == null || from.trim().equals("")) from = justPkgName;
ohrstrom@1504 335 cc.pkgNames.append(justPkgName+"("+s.size()+") ");
ohrstrom@1504 336 cc.pkgFromTos = from+" to "+justPkgName;
ohrstrom@1504 337 }
ohrstrom@1504 338 // If we are compiling serially, sort the chunks, so that the chunk (with the most dependents) (usually the chunk
ohrstrom@1504 339 // containing java.lang.Object, is to be compiled first!
ohrstrom@1504 340 // For concurrent compilation, this does not matter.
ohrstrom@1504 341 Arrays.sort(compileChunks);
ohrstrom@1504 342 return compileChunks;
ohrstrom@1504 343 }
ohrstrom@1504 344 }

mercurial