diff options
author | ruperts <ruperts@google.com> | 2017-12-05 21:53:50 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2017-12-05 21:55:51 -0800 |
commit | bec2fe85bdf07d944d0cb650b59732ea8d3e804c (patch) | |
tree | 06c6498199be667869d6885ea1f0ec9bfbf8f963 /src/main/java/com/google/devtools/build/lib/shell | |
parent | 7964b1aa0d6c681799633f8a20d5ade78aec8289 (diff) |
Make ProcessWrapperUtil aware of the execution statistics file, and add new ExecutionStatisticsProvider.
RELNOTES: None.
PiperOrigin-RevId: 178056182
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/shell')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/shell/BUILD | 6 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/shell/ExecutionStatistics.java | 66 |
2 files changed, 71 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/shell/BUILD b/src/main/java/com/google/devtools/build/lib/shell/BUILD index 2432831365..8b8d9301a5 100644 --- a/src/main/java/com/google/devtools/build/lib/shell/BUILD +++ b/src/main/java/com/google/devtools/build/lib/shell/BUILD @@ -14,6 +14,7 @@ java_library( name = "shell", srcs = glob(["*.java"]), deps = [ + "//src/main/protobuf:execution_statistics_java_proto", "//third_party:auto_value", "//third_party:guava", ], @@ -25,7 +26,10 @@ load("//tools/build_rules:java_rules_skylark.bzl", "bootstrap_java_library") bootstrap_java_library( name = "shell-skylark", - srcs = glob(["*.java"]), + srcs = glob( + ["*.java"], + exclude = ["ExecutionStatistics.java"], + ), jars = [ "//third_party:auto_value-jars", "//third_party:bootstrap_guava_and_error_prone-jars", diff --git a/src/main/java/com/google/devtools/build/lib/shell/ExecutionStatistics.java b/src/main/java/com/google/devtools/build/lib/shell/ExecutionStatistics.java new file mode 100644 index 0000000000..f1972ac847 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/shell/ExecutionStatistics.java @@ -0,0 +1,66 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.shell; + +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.time.Duration; +import java.util.Optional; + +/** Provides execution statistics (e.g. resource usage) for external commands. */ +public final class ExecutionStatistics { + private final com.google.devtools.build.lib.shell.Protos.ExecutionStatistics + executionStatisticsProto; + + /** + * Provides execution statistics based on a {@code execution_statistics.proto} file. + * + * @param executionStatisticsProtoPath path to a materialized ExecutionStatistics proto + */ + public ExecutionStatistics(String executionStatisticsProtoPath) throws IOException { + try (InputStream protoInputStream = + new BufferedInputStream(new FileInputStream(executionStatisticsProtoPath))) { + executionStatisticsProto = + com.google.devtools.build.lib.shell.Protos.ExecutionStatistics.parseFrom( + protoInputStream); + } + } + + /** Returns the user time for command execution, if available. */ + public Optional<Duration> getUserExecutionTime() { + if (executionStatisticsProto.hasResourceUsage()) { + return Optional.of( + Duration.ofSeconds( + executionStatisticsProto.getResourceUsage().getUtimeSec(), + executionStatisticsProto.getResourceUsage().getUtimeUsec() * 1000)); + } else { + return Optional.empty(); + } + } + + /** Returns the system time for command execution, if available. */ + public Optional<Duration> getSystemExecutionTime() { + if (executionStatisticsProto.hasResourceUsage()) { + return Optional.of( + Duration.ofSeconds( + executionStatisticsProto.getResourceUsage().getStimeSec(), + executionStatisticsProto.getResourceUsage().getStimeUsec() * 1000)); + } else { + return Optional.empty(); + } + } +} |