Skip to content

Commit

Permalink
Add load time of action cache to statistics.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 583040794
Change-Id: I249afa1769edf76780373a1907a02a4fe5abe74f
  • Loading branch information
meisterT authored and copybara-github committed Nov 16, 2023
1 parent 8ed6837 commit 6fb551f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -171,6 +172,7 @@ protected void writeValue(byte[] value, DataOutputStream out) throws IOException
private final PersistentMap<Integer, byte[]> map;
private final ImmutableMap<MissReason, AtomicInteger> misses;
private final AtomicInteger hits = new AtomicInteger();
private Duration loadTime;

private CompactPersistentActionCache(
PersistentStringIndexer indexer,
Expand All @@ -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(
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/main/protobuf/action_cache.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 6fb551f

Please sign in to comment.