diff --git a/src/main/java/com/google/devtools/build/lib/actions/cache/ActionCache.java b/src/main/java/com/google/devtools/build/lib/actions/cache/ActionCache.java index 6b020a472d85cc..ec278bfe11dabe 100644 --- a/src/main/java/com/google/devtools/build/lib/actions/cache/ActionCache.java +++ b/src/main/java/com/google/devtools/build/lib/actions/cache/ActionCache.java @@ -37,6 +37,7 @@ import com.google.devtools.build.lib.vfs.PathFragment; import java.io.IOException; import java.io.PrintStream; +import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -434,4 +435,10 @@ public String toString() { /** Resets the current statistics to zero. */ void resetStatistics(); + + /** Duration it took to load the action cache. Might be null if not loaded in this invocation. */ + @Nullable + default Duration getLoadTime() { + return null; + } } diff --git a/src/main/java/com/google/devtools/build/lib/actions/cache/CompactPersistentActionCache.java b/src/main/java/com/google/devtools/build/lib/actions/cache/CompactPersistentActionCache.java index d906498770e072..f540d5713131cc 100644 --- a/src/main/java/com/google/devtools/build/lib/actions/cache/CompactPersistentActionCache.java +++ b/src/main/java/com/google/devtools/build/lib/actions/cache/CompactPersistentActionCache.java @@ -47,6 +47,7 @@ import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; import java.time.Duration; +import java.time.Instant; import java.util.Collection; import java.util.EnumMap; import java.util.Map; @@ -171,6 +172,7 @@ protected void writeValue(byte[] value, DataOutputStream out) throws IOException private final PersistentMap map; private final ImmutableMap misses; private final AtomicInteger hits = new AtomicInteger(); + private Duration loadTime; private CompactPersistentActionCache( PersistentStringIndexer indexer, @@ -184,8 +186,14 @@ private CompactPersistentActionCache( public static CompactPersistentActionCache create( Path cacheRoot, Clock clock, EventHandler reporterForInitializationErrors) throws IOException { - return create( - cacheRoot, clock, reporterForInitializationErrors, /*alreadyFoundCorruption=*/ false); + Instant before = clock.now(); + CompactPersistentActionCache compactPersistentActionCache = + create( + cacheRoot, clock, reporterForInitializationErrors, /* alreadyFoundCorruption= */ false); + Instant after = clock.now(); + compactPersistentActionCache.loadTime = Duration.between(before, after); + + return compactPersistentActionCache; } private static CompactPersistentActionCache create( @@ -759,4 +767,14 @@ public void resetStatistics() { entry.getValue().set(0); } } + + @Override + @Nullable + public Duration getLoadTime() { + Duration ret = loadTime; + // As a side effect, reset the load time, so it is only reported for the actual invocation that + // loaded the action cache. + loadTime = null; + return ret; + } } 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 3b4e6a638d6765..a82da29bc1fe27 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 @@ -1011,6 +1011,10 @@ private void saveActionCache(@Nullable ActionCache actionCache) { if (actionCache != null) { actionCache.mergeIntoActionCacheStatistics(builder); + Duration duration = actionCache.getLoadTime(); + if (duration != null) { + builder.setLoadTimeInMs(duration.toMillis()); + } AutoProfiler p = GoogleAutoProfilerUtils.profiledAndLogged("Saving action cache", ProfilerTask.INFO); diff --git a/src/main/protobuf/action_cache.proto b/src/main/protobuf/action_cache.proto index 5694ebe34aeb0a..a31cba599ed60f 100644 --- a/src/main/protobuf/action_cache.proto +++ b/src/main/protobuf/action_cache.proto @@ -55,5 +55,9 @@ message ActionCacheStatistics { // Breakdown of the cache misses based on the reasons behind them. repeated MissDetail miss_details = 5; - // NEXT TAG: 6 + // Time it took to load the action cache from disk. Reported as 0 if the + // action cache has not been loaded in this invocation. + uint64 load_time_in_ms = 6; + + // NEXT TAG: 7 }