Limit the number of forks getting Java versions (#41386)

To reduce configuration time, we fork some threads to compute the Java
version for the various configured Javas. However, as the number of
JAVA${N}_HOME variable increases, the current implementation creates as
many threads as there are such variables, which could be more than the
number of physical cores on the machine. It is not likely that we would
see benefits to trying to execute all of these once beyond the number of
physical cores (maybe simultaneous multi-threading helps though, who
knows. Therefore, this commit limits the parallelization here to the
number number of physical cores.
This commit is contained in:
Jason Tedor 2019-04-19 17:14:49 -04:00
parent c0164cbb63
commit 38b43c4a56
No known key found for this signature in database
GPG Key ID: FA89F05560F16BC5
1 changed files with 9 additions and 5 deletions

View File

@ -197,9 +197,10 @@ class BuildPlugin implements Plugin<Project> {
}
}
final int numberOfPhysicalCores = numberOfPhysicalCores(project.rootProject)
if (javaVersions.isEmpty() == false) {
ExecutorService exec = Executors.newFixedThreadPool(javaVersions.size())
ExecutorService exec = Executors.newFixedThreadPool(numberOfPhysicalCores)
Set<Future<Void>> results = new HashSet<>()
javaVersions.entrySet().stream()
@ -247,7 +248,7 @@ class BuildPlugin implements Plugin<Project> {
project.rootProject.ext.inFipsJvm = inFipsJvm
project.rootProject.ext.gradleJavaVersion = JavaVersion.toVersion(gradleJavaVersion)
project.rootProject.ext.java9Home = "${-> findJavaHome("9")}"
project.rootProject.ext.defaultParallel = findDefaultParallel(project.rootProject)
project.rootProject.ext.defaultParallel = numberOfPhysicalCores
}
project.targetCompatibility = project.rootProject.ext.minimumRuntimeVersion
@ -1024,7 +1025,7 @@ class BuildPlugin implements Plugin<Project> {
}
}
private static int findDefaultParallel(Project project) {
private static int numberOfPhysicalCores(Project project) {
if (project.file("/proc/cpuinfo").exists()) {
// Count physical cores on any Linux distro ( don't count hyper-threading )
Map<String, Integer> socketToCore = [:]
@ -1037,7 +1038,7 @@ class BuildPlugin implements Plugin<Project> {
if (name == "physical id") {
currentID = value
}
// Number of cores not including hyper-threading
// number of cores not including hyper-threading
if (name == "cpu cores") {
assert currentID.isEmpty() == false
socketToCore[currentID] = Integer.valueOf(value)
@ -1055,8 +1056,11 @@ class BuildPlugin implements Plugin<Project> {
standardOutput = stdout
}
return Integer.parseInt(stdout.toString('UTF-8').trim())
} else {
// guess that it is half the number of processors (which is wrong on systems that do not have simultaneous multi-threading)
// TODO: implement this on Windows
return Runtime.getRuntime().availableProcessors() / 2
}
return Runtime.getRuntime().availableProcessors() / 2
}
private static configurePrecommit(Project project) {