Refactor collection of action cache statistics.
This change gets rid of the old CachesSavedEvent object (note, plural) which
used to capture details about various caches. As this was now only used for
a single cache, rename it to ActionCacheStatistics so that the data it
contains is more cohesive.
The reason for this change is to make room for the addition of other metrics
for the action cache (like hits/misses), which will come later. As of now,
this change is intended to be a no-op.
While doing this, use AutoValue to avoid mutability of the generated objects
and use better types for the contained data (e.g. Duration for timings).
RELNOTES: None.
PiperOrigin-RevId: 166742117
diff --git a/src/main/java/com/google/devtools/build/lib/BUILD b/src/main/java/com/google/devtools/build/lib/BUILD
index c3cb48c..742b982 100644
--- a/src/main/java/com/google/devtools/build/lib/BUILD
+++ b/src/main/java/com/google/devtools/build/lib/BUILD
@@ -1198,6 +1198,7 @@
"//src/main/protobuf:extra_actions_base_java_proto",
"//src/main/protobuf:invocation_policy_java_proto",
"//src/main/protobuf:test_status_java_proto",
+ "//third_party:auto_value",
"//third_party:guava",
"//third_party:joda_time",
"//third_party:jsr305",
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/ActionCacheStatistics.java b/src/main/java/com/google/devtools/build/lib/buildtool/ActionCacheStatistics.java
new file mode 100644
index 0000000..d1ccee6
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/ActionCacheStatistics.java
@@ -0,0 +1,44 @@
+// Copyright 2014 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.buildtool;
+
+import com.google.auto.value.AutoValue;
+import java.time.Duration;
+
+/** Statistics about the action cache during a single build. */
+@AutoValue
+public abstract class ActionCacheStatistics {
+ /** Gets the time it took to save the action cache to disk. */
+ public abstract Duration saveTime();
+
+ /** Gets the size of the action cache in bytes as persisted on disk. */
+ public abstract long sizeInBytes();
+
+ /** Returns a new builder. */
+ static Builder builder() {
+ return new AutoValue_ActionCacheStatistics.Builder();
+ }
+
+ @AutoValue.Builder
+ abstract static class Builder {
+ /** Sets the time it took to save the action cache to disk. */
+ abstract Builder setSaveTime(Duration duration);
+
+ /** Sets the size of the action cache in bytes as persisted to disk. */
+ abstract Builder setSizeInBytes(long sizeInBytes);
+
+ /** Constructs and returns the {@link ActionCacheStatistics}. */
+ abstract ActionCacheStatistics build();
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/CachesSavedEvent.java b/src/main/java/com/google/devtools/build/lib/buildtool/CachesSavedEvent.java
deleted file mode 100644
index 8f60892..0000000
--- a/src/main/java/com/google/devtools/build/lib/buildtool/CachesSavedEvent.java
+++ /dev/null
@@ -1,39 +0,0 @@
-// Copyright 2014 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.buildtool;
-
-/**
- * Event that is raised when the action and artifact metadata caches are saved at the end of the
- * build. Contains statistics.
- */
-public class CachesSavedEvent {
- /** Cache serialization statistics. */
- private final long actionCacheSaveTimeInMillis;
- private final long actionCacheSizeInBytes;
-
- public CachesSavedEvent(
- long actionCacheSaveTimeInMillis,
- long actionCacheSizeInBytes) {
- this.actionCacheSaveTimeInMillis = actionCacheSaveTimeInMillis;
- this.actionCacheSizeInBytes = actionCacheSizeInBytes;
- }
-
- public long getActionCacheSaveTimeInMillis() {
- return actionCacheSaveTimeInMillis;
- }
-
- public long getActionCacheSizeInBytes() {
- return actionCacheSizeInBytes;
- }
-}
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
index a5d38d4..dc81d34 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
@@ -14,7 +14,6 @@
package com.google.devtools.build.lib.buildtool;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
-import static java.util.concurrent.TimeUnit.NANOSECONDS;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
@@ -97,6 +96,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
+import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -469,7 +469,7 @@
Profiler.instance().markPhase(ProfilePhase.FINISH);
if (buildCompleted) {
- saveCaches(actionCache);
+ saveActionCache(actionCache);
}
try (AutoProfiler p = AutoProfiler.profiled("Show results", ProfilerTask.INFO)) {
@@ -716,24 +716,23 @@
}
/**
- * Writes the cache files to disk, reporting any errors that occurred during
- * writing.
+ * Writes the action cache files to disk, reporting any errors that occurred during writing and
+ * capturing statistics.
*/
- private void saveCaches(ActionCache actionCache) {
- long actionCacheSizeInBytes = 0;
- long actionCacheSaveTimeInMs;
+ private void saveActionCache(ActionCache actionCache) {
+ ActionCacheStatistics.Builder builder = ActionCacheStatistics.builder();
AutoProfiler p = AutoProfiler.profiledAndLogged("Saving action cache", ProfilerTask.INFO, log);
try {
- actionCacheSizeInBytes = actionCache.save();
+ builder.setSizeInBytes(actionCache.save());
} catch (IOException e) {
+ builder.setSizeInBytes(0);
getReporter().handle(Event.error("I/O error while writing action log: " + e.getMessage()));
} finally {
- actionCacheSaveTimeInMs =
- MILLISECONDS.convert(p.completeAndGetElapsedTimeNanos(), NANOSECONDS);
+ builder.setSaveTime(Duration.ofNanos(p.completeAndGetElapsedTimeNanos()));
}
- env.getEventBus().post(new CachesSavedEvent(
- actionCacheSaveTimeInMs, actionCacheSizeInBytes));
+
+ env.getEventBus().post(builder.build());
}
private Reporter getReporter() {