Skip to content

Commit 16850bc

Browse files
authored
Fix calculation of max parallel forks for test execution (#85360) (#85403)
1 parent fe97af9 commit 16850bc

File tree

2 files changed

+10
-60
lines changed

2 files changed

+10
-60
lines changed

build-conventions/src/main/java/org/elasticsearch/gradle/internal/conventions/info/ParallelDetector.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,25 @@ public static int findDefaultParallel(Project project) {
6262
} else if (isMac(project.getProviders())) {
6363
// Ask macOS to count physical CPUs for us
6464
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
65+
66+
// On Apple silicon, we only want to use the performance cores
67+
String query = project.getProviders().systemProperty("os.arch").getOrElse("").equals("aarch64")
68+
? "hw.perflevel0.physicalcpu"
69+
: "hw.physicalcpu";
70+
6571
project.exec(spec -> {
6672
spec.setExecutable("sysctl");
67-
spec.args("-n", "hw.physicalcpu");
73+
spec.args("-n", query);
6874
spec.setStandardOutput(stdout);
6975
});
7076

7177
_defaultParallel = Integer.parseInt(stdout.toString().trim());
78+
} else {
79+
_defaultParallel = Runtime.getRuntime().availableProcessors() / 2;
7280
}
73-
74-
_defaultParallel = Runtime.getRuntime().availableProcessors() / 2;
7581
}
7682

77-
return _defaultParallel;
83+
return Math.min(_defaultParallel, project.getGradle().getStartParameter().getMaxWorkerCount());
7884
}
7985

8086
private static boolean isMac(ProviderFactory providers) {

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/info/GlobalBuildInfoPlugin.java

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package org.elasticsearch.gradle.internal.info;
99

1010
import org.apache.commons.io.IOUtils;
11-
import org.elasticsearch.gradle.OS;
1211
import org.elasticsearch.gradle.internal.BwcVersions;
1312
import org.elasticsearch.gradle.internal.conventions.info.GitInfo;
1413
import org.elasticsearch.gradle.internal.conventions.info.ParallelDetector;
@@ -30,21 +29,17 @@
3029
import org.gradle.util.GradleVersion;
3130

3231
import java.io.BufferedReader;
33-
import java.io.ByteArrayOutputStream;
3432
import java.io.File;
3533
import java.io.FileInputStream;
36-
import java.io.FileReader;
3734
import java.io.IOException;
3835
import java.io.InputStreamReader;
3936
import java.io.UncheckedIOException;
4037
import java.nio.file.Files;
4138
import java.time.ZoneOffset;
4239
import java.time.ZonedDateTime;
4340
import java.util.Arrays;
44-
import java.util.HashMap;
4541
import java.util.List;
4642
import java.util.Locale;
47-
import java.util.Map;
4843
import java.util.Random;
4944
import java.util.concurrent.atomic.AtomicReference;
5045
import java.util.stream.Collectors;
@@ -55,7 +50,6 @@
5550
public class GlobalBuildInfoPlugin implements Plugin<Project> {
5651
private static final Logger LOGGER = Logging.getLogger(GlobalBuildInfoPlugin.class);
5752
private static final String DEFAULT_VERSION_JAVA_FILE_PATH = "server/src/main/java/org/elasticsearch/Version.java";
58-
private static Integer _defaultParallel = null;
5953

6054
private final JavaInstallationRegistry javaInstallationRegistry;
6155
private final JvmMetadataDetector metadataDetector;
@@ -305,56 +299,6 @@ private static String getJavaHomeEnvVarName(String version) {
305299
return "JAVA" + version + "_HOME";
306300
}
307301

308-
private static int findDefaultParallel(Project project) {
309-
// Since it costs IO to compute this, and is done at configuration time we want to cache this if possible
310-
// It's safe to store this in a static variable since it's just a primitive so leaking memory isn't an issue
311-
if (_defaultParallel == null) {
312-
File cpuInfoFile = new File("/proc/cpuinfo");
313-
if (cpuInfoFile.exists()) {
314-
// Count physical cores on any Linux distro ( don't count hyper-threading )
315-
Map<String, Integer> socketToCore = new HashMap<>();
316-
String currentID = "";
317-
318-
try (BufferedReader reader = new BufferedReader(new FileReader(cpuInfoFile))) {
319-
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
320-
if (line.contains(":")) {
321-
List<String> parts = Arrays.stream(line.split(":", 2)).map(String::trim).collect(Collectors.toList());
322-
String name = parts.get(0);
323-
String value = parts.get(1);
324-
// the ID of the CPU socket
325-
if (name.equals("physical id")) {
326-
currentID = value;
327-
}
328-
// Number of cores not including hyper-threading
329-
if (name.equals("cpu cores")) {
330-
assert currentID.isEmpty() == false;
331-
socketToCore.put("currentID", Integer.valueOf(value));
332-
currentID = "";
333-
}
334-
}
335-
}
336-
} catch (IOException e) {
337-
throw new UncheckedIOException(e);
338-
}
339-
_defaultParallel = socketToCore.values().stream().mapToInt(i -> i).sum();
340-
} else if (OS.current() == OS.MAC) {
341-
// Ask macOS to count physical CPUs for us
342-
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
343-
project.exec(spec -> {
344-
spec.setExecutable("sysctl");
345-
spec.args("-n", "hw.physicalcpu");
346-
spec.setStandardOutput(stdout);
347-
});
348-
349-
_defaultParallel = Integer.parseInt(stdout.toString().trim());
350-
}
351-
352-
_defaultParallel = Runtime.getRuntime().availableProcessors() / 2;
353-
}
354-
355-
return _defaultParallel;
356-
}
357-
358302
public static String getResourceContents(String resourcePath) {
359303
try (
360304
BufferedReader reader = new BufferedReader(new InputStreamReader(GlobalBuildInfoPlugin.class.getResourceAsStream(resourcePath)))

0 commit comments

Comments
 (0)