Build: Pick default test jvms for macOS (#35789)

In #35259 we switched the default number of VMs to fork for unit tests to
the number of physical CPU cores. But because we could only get an accurate
count on machines with a normal `/proc` filesystem, macOS machine did not
pick up the new default. Given that macOS is a huge portion of developer
machines, we'd like to get the right default there. This does that.

It also moves the default-finding process from happening once per testing
task to happening once at startup. This seems like a good choice in general,
but a very good choice for macOS because we have to run a command to list
the count.
This commit is contained in:
Nik Everett 2018-11-27 09:59:44 -05:00 committed by GitHub
parent 541869a96a
commit 38725bd3a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 24 deletions

View File

@ -215,6 +215,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.targetCompatibility = project.rootProject.ext.minimumRuntimeVersion
@ -773,32 +774,9 @@ class BuildPlugin implements Plugin<Project> {
}
static void applyCommonTestConfig(Project project) {
String defaultParallel = 'auto'
// Count physical cores on any Linux distro ( don't count hyper-threading )
if (project.file("/proc/cpuinfo").exists()) {
Map<String, Integer> socketToCore = [:]
String currentID = ""
project.file("/proc/cpuinfo").readLines().forEach({ line ->
if (line.contains(":")) {
List<String> parts = line.split(":", 2).collect({it.trim()})
String name = parts[0], value = parts[1]
// the ID of the CPU socket
if (name == "physical id") {
currentID = value
}
// Number of cores not including hyper-threading
if (name == "cpu cores") {
assert currentID.isEmpty() == false
socketToCore[currentID] = Integer.valueOf(value)
currentID = ""
}
}
})
defaultParallel = socketToCore.values().sum().toString();
}
project.tasks.withType(RandomizedTestingTask) {
jvm "${project.runtimeJavaHome}/bin/java"
parallelism System.getProperty('tests.jvms', defaultParallel)
parallelism System.getProperty('tests.jvms', project.rootProject.ext.defaultParallel)
ifNoTests System.getProperty('tests.ifNoTests', 'fail')
onNonEmptyWorkDirectory 'wipe'
leaveTemporary true
@ -902,6 +880,41 @@ class BuildPlugin implements Plugin<Project> {
}
}
private static String findDefaultParallel(Project project) {
if (project.file("/proc/cpuinfo").exists()) {
// Count physical cores on any Linux distro ( don't count hyper-threading )
Map<String, Integer> socketToCore = [:]
String currentID = ""
project.file("/proc/cpuinfo").readLines().forEach({ line ->
if (line.contains(":")) {
List<String> parts = line.split(":", 2).collect({it.trim()})
String name = parts[0], value = parts[1]
// the ID of the CPU socket
if (name == "physical id") {
currentID = value
}
// Number of cores not including hyper-threading
if (name == "cpu cores") {
assert currentID.isEmpty() == false
socketToCore[currentID] = Integer.valueOf(value)
currentID = ""
}
}
})
return socketToCore.values().sum().toString();
} else if ('Mac OS X'.equals(System.getProperty('os.name'))) {
// Ask macOS to count physical CPUs for us
ByteArrayOutputStream stdout = new ByteArrayOutputStream()
project.exec {
executable 'sysctl'
args '-n', 'hw.physicalcpu'
standardOutput = stdout
}
return stdout.toString('UTF-8')
}
return 'auto';
}
/** Configures the test task */
static Task configureTest(Project project) {
project.tasks.getByName('test') {