Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.2.1] Do not fail a sandboxed spawn for missing execution statistics #22790

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.exec.Protos.Digest;
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
import com.google.devtools.build.lib.shell.ExecutionStatistics;
import com.google.devtools.build.lib.shell.TerminationStatus;
import com.google.devtools.build.lib.util.DetailedExitCode;
import com.google.devtools.build.lib.util.ExitCode;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.vfs.Path;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.Locale;
Expand Down Expand Up @@ -671,6 +674,28 @@ public Builder setDigest(Digest digest) {
this.digest = digest;
return this;
}

/** Adds execution statistics based on a {@code execution_statistics.proto} file. */
@CanIgnoreReturnValue
public Builder setResourceUsageFromProto(Path statisticsPath) throws IOException {
ExecutionStatistics.getResourceUsage(statisticsPath)
.ifPresent(
resourceUsage -> {
setUserTimeInMs((int) resourceUsage.getUserExecutionTime().toMillis());
setSystemTimeInMs((int) resourceUsage.getSystemExecutionTime().toMillis());
setNumBlockOutputOperations(resourceUsage.getBlockOutputOperations());
setNumBlockInputOperations(resourceUsage.getBlockInputOperations());
setNumInvoluntaryContextSwitches(resourceUsage.getInvoluntaryContextSwitches());
// The memory usage of the largest child process. For Darwin maxrss returns size in
// bytes.
if (OS.getCurrent() == OS.DARWIN) {
setMemoryInKb(resourceUsage.getMaximumResidentSetSize() / 1000);
} else {
setMemoryInKb(resourceUsage.getMaximumResidentSetSize());
}
});
return this;
}
}

/** A {@link Spawn}'s metadata name and {@link Path}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@
import com.google.devtools.build.lib.server.FailureDetails;
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
import com.google.devtools.build.lib.server.FailureDetails.Spawn.Code;
import com.google.devtools.build.lib.shell.ExecutionStatistics;
import com.google.devtools.build.lib.shell.Subprocess;
import com.google.devtools.build.lib.shell.SubprocessBuilder;
import com.google.devtools.build.lib.shell.TerminationStatus;
import com.google.devtools.build.lib.util.NetUtil;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.util.io.FileOutErr;
import com.google.devtools.build.lib.vfs.Path;
import com.google.errorprone.annotations.FormatMethod;
Expand Down Expand Up @@ -481,29 +479,8 @@ private SpawnResult start()
if (status != Status.SUCCESS) {
spawnResultBuilder.setFailureDetail(makeFailureDetail(exitCode, status, actionType));
}
if (statisticsPath != null && statisticsPath.exists()) {
ExecutionStatistics.getResourceUsage(statisticsPath)
.ifPresent(
resourceUsage -> {
spawnResultBuilder.setUserTimeInMs(
(int) resourceUsage.getUserExecutionTime().toMillis());
spawnResultBuilder.setSystemTimeInMs(
(int) resourceUsage.getSystemExecutionTime().toMillis());
spawnResultBuilder.setNumBlockOutputOperations(
resourceUsage.getBlockOutputOperations());
spawnResultBuilder.setNumBlockInputOperations(
resourceUsage.getBlockInputOperations());
spawnResultBuilder.setNumInvoluntaryContextSwitches(
resourceUsage.getInvoluntaryContextSwitches());
// The memory usage of the largest child process. For Darwin maxrss returns size
// in bytes.
if (OS.getCurrent() == OS.DARWIN) {
spawnResultBuilder.setMemoryInKb(
resourceUsage.getMaximumResidentSetSize() / 1000);
} else {
spawnResultBuilder.setMemoryInKb(resourceUsage.getMaximumResidentSetSize());
}
});
if (statisticsPath != null) {
spawnResultBuilder.setResourceUsageFromProto(statisticsPath);
}
spawnMetrics.setTotalTimeInMs((int) totalTimeStopwatch.elapsed().toMillis());
spawnResultBuilder.setSpawnMetrics(spawnMetrics.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import com.google.devtools.build.lib.server.FailureDetails;
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
import com.google.devtools.build.lib.server.FailureDetails.Sandbox.Code;
import com.google.devtools.build.lib.shell.ExecutionStatistics;
import com.google.devtools.build.lib.shell.Subprocess;
import com.google.devtools.build.lib.shell.SubprocessBuilder;
import com.google.devtools.build.lib.shell.TerminationStatus;
Expand Down Expand Up @@ -309,28 +308,7 @@ private final SpawnResult run(

Path statisticsPath = sandbox.getStatisticsPath();
if (statisticsPath != null) {
ExecutionStatistics.getResourceUsage(statisticsPath)
.ifPresent(
resourceUsage -> {
spawnResultBuilder.setUserTimeInMs(
(int) resourceUsage.getUserExecutionTime().toMillis());
spawnResultBuilder.setSystemTimeInMs(
(int) resourceUsage.getSystemExecutionTime().toMillis());
spawnResultBuilder.setNumBlockOutputOperations(
resourceUsage.getBlockOutputOperations());
spawnResultBuilder.setNumBlockInputOperations(
resourceUsage.getBlockInputOperations());
spawnResultBuilder.setNumInvoluntaryContextSwitches(
resourceUsage.getInvoluntaryContextSwitches());
// The memory usage of the largest child process. For Darwin maxrss returns size in
// bytes.
if (OS.getCurrent() == OS.DARWIN) {
spawnResultBuilder.setMemoryInKb(
resourceUsage.getMaximumResidentSetSize() / 1000);
} else {
spawnResultBuilder.setMemoryInKb(resourceUsage.getMaximumResidentSetSize());
}
});
spawnResultBuilder.setResourceUsageFromProto(statisticsPath);
}

return spawnResultBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.devtools.build.lib.vfs.Path;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
Expand All @@ -40,6 +41,10 @@ public static Optional<ResourceUsage> getResourceUsage(Path executionStatisticsP
} else {
return Optional.empty();
}
} catch (FileNotFoundException e) {
// Collecting resource usage is best-effort and the file may be missing if the wrapper around
// the command terminated abnormally.
return Optional.empty();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ protected void chmod(PathFragment path, int mode) throws IOException {
/**
* Creates an InputStream accessing the file denoted by the path.
*
* @throws FileNotFoundException if the file does not exist
* @throws IOException if there was an error opening the file for reading
*/
protected abstract InputStream getInputStream(PathFragment path) throws IOException;
Expand Down
Loading