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