aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/sandbox
diff options
context:
space:
mode:
authorGravatar twerth <twerth@google.com>2018-07-06 03:23:26 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-07-06 03:24:31 -0700
commitbc898cabe7cece0cf868447392f6863cb134d85c (patch)
tree2ffffdd93beac435ea0a8149414c4506fa50399d /src/main/java/com/google/devtools/build/lib/sandbox
parentaa0d5a73fff8a879953315013ee24dad8dcdcff5 (diff)
Don't check if sandbox is supported over and over again.
RELNOTES: None PiperOrigin-RevId: 203454741
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/sandbox')
-rw-r--r--src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedSpawnRunner.java11
-rw-r--r--src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedSpawnRunner.java17
-rw-r--r--src/main/java/com/google/devtools/build/lib/sandbox/SandboxModule.java2
3 files changed, 22 insertions, 8 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedSpawnRunner.java b/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedSpawnRunner.java
index bde1a99692..ced76adba9 100644
--- a/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedSpawnRunner.java
+++ b/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedSpawnRunner.java
@@ -65,9 +65,12 @@ final class DarwinSandboxedSpawnRunner extends AbstractSandboxSpawnRunner {
@VisibleForTesting
static String sandboxExecBinary = "/usr/bin/sandbox-exec";
+ // Since checking if sandbox is supported is expensive, we remember what we've checked.
+ private static Boolean isSupported = null;
+
/**
* Returns whether the darwin sandbox is supported on the local machine by running a small command
- * in it. This is expensive!
+ * in it.
*/
public static boolean isSupported(CommandEnvironment cmdEnv) {
if (OS.getCurrent() != OS.DARWIN) {
@@ -76,7 +79,13 @@ final class DarwinSandboxedSpawnRunner extends AbstractSandboxSpawnRunner {
if (!ProcessWrapperUtil.isSupported(cmdEnv)) {
return false;
}
+ if (isSupported == null) {
+ isSupported = computeIsSupported();
+ }
+ return isSupported;
+ }
+ private static boolean computeIsSupported() {
List<String> args = new ArrayList<>();
args.add(sandboxExecBinary);
args.add("-p");
diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedSpawnRunner.java b/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedSpawnRunner.java
index b0a7ed652a..5be9cd18f7 100644
--- a/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedSpawnRunner.java
+++ b/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedSpawnRunner.java
@@ -41,6 +41,7 @@ import com.google.devtools.build.lib.vfs.Symlinks;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
+import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import javax.annotation.Nullable;
@@ -48,22 +49,28 @@ import javax.annotation.Nullable;
/** Spawn runner that uses linux sandboxing APIs to execute a local subprocess. */
final class LinuxSandboxedSpawnRunner extends AbstractSandboxSpawnRunner {
+ // Since checking if sandbox is supported is expensive, we remember what we've checked.
+ private static final Map<Path, Boolean> isSupportedMap = new HashMap<>();
+
/**
* Returns whether the linux sandbox is supported on the local machine by running a small command
- * in it. This is expensive!
+ * in it.
*/
- public static boolean isSupported(CommandEnvironment cmdEnv) {
+ public static boolean isSupported(final CommandEnvironment cmdEnv) {
if (OS.getCurrent() != OS.LINUX) {
return false;
}
if (!LinuxSandboxUtil.isSupported(cmdEnv)) {
return false;
}
+ Path linuxSandbox = LinuxSandboxUtil.getLinuxSandbox(cmdEnv);
+ return isSupportedMap.computeIfAbsent(
+ linuxSandbox, linuxSandboxPath -> computeIsSupported(cmdEnv, linuxSandboxPath));
+ }
+ private static boolean computeIsSupported(CommandEnvironment cmdEnv, Path linuxSandbox) {
ImmutableList<String> linuxSandboxArgv =
- LinuxSandboxUtil.commandLineBuilder(
- LinuxSandboxUtil.getLinuxSandbox(cmdEnv), ImmutableList.of("/bin/true"))
- .build();
+ LinuxSandboxUtil.commandLineBuilder(linuxSandbox, ImmutableList.of("/bin/true")).build();
ImmutableMap<String, String> env = ImmutableMap.of();
Path execRoot = cmdEnv.getExecRoot();
File cwd = execRoot.getPathFile();
diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/SandboxModule.java b/src/main/java/com/google/devtools/build/lib/sandbox/SandboxModule.java
index a331f55b3d..a177598036 100644
--- a/src/main/java/com/google/devtools/build/lib/sandbox/SandboxModule.java
+++ b/src/main/java/com/google/devtools/build/lib/sandbox/SandboxModule.java
@@ -144,9 +144,7 @@ public final class SandboxModule extends BlazeModule {
cmdEnv.getOptions().getOptions(LocalExecutionOptions.class).getLocalSigkillGraceSeconds();
boolean processWrapperSupported = ProcessWrapperSandboxedSpawnRunner.isSupported(cmdEnv);
- // LinuxSandboxedSpawnRunner.isSupported is expensive! It runs the sandbox as a subprocess.
boolean linuxSandboxSupported = LinuxSandboxedSpawnRunner.isSupported(cmdEnv);
- // DarwinSandboxedSpawnRunner.isSupported is expensive! It runs the sandbox as a subprocess.
boolean darwinSandboxSupported = DarwinSandboxedSpawnRunner.isSupported(cmdEnv);
// This works on most platforms, but isn't the best choice, so we put it first and let later