Skip to content

Commit

Permalink
Clear the file digests cache on clean.
Browse files Browse the repository at this point in the history
Failure to do so compromises benchmarking results, as digest computations can still hit the cache despite the expectation of starting from a clean slate.

PiperOrigin-RevId: 606620638
Change-Id: I60b066da4b50992f57a6b5e2d37db3b04ea31e93
  • Loading branch information
tjgq authored and copybara-github committed Feb 13, 2024
1 parent e762184 commit 28ec988
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.devtools.build.lib.util.CommandBuilder;
import com.google.devtools.build.lib.util.InterruptedFailureDetails;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.vfs.DigestUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.common.options.Option;
import com.google.devtools.common.options.OptionDocumentationCategory;
Expand Down Expand Up @@ -213,18 +214,23 @@ private BlazeCommandResult actuallyClean(
CommandEnvironment env, Path outputBase, boolean expunge, boolean async, String symlinkPrefix)
throws CleanException, InterruptedException {
BlazeRuntime runtime = env.getRuntime();

if (env.getOutputService() != null) {
try {
env.getOutputService().clean();
} catch (ExecException e) {
throw new CleanException(Code.OUTPUT_SERVICE_CLEAN_FAILURE, e);
}
}

try {
env.getBlazeWorkspace().clearCaches();
} catch (IOException e) {
throw new CleanException(Code.ACTION_CACHE_CLEAN_FAILURE, e);
}

DigestUtils.clearCache();

if (expunge && !async) {
logger.atInfo().log("Expunging...");
runtime.prepareForAbruptShutdown();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/google/devtools/build/lib/vfs/DigestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ public static void configureCache(long maximumSize) {
}
}

/**
* Clears the cache contents without changing its size. No-op if the cache hasn't yet been
* initialized.
*/
public static void clearCache() {
if (globalCache != null) {
globalCache.invalidateAll();
}
}

/**
* Obtains cache statistics.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,21 @@ protected byte[] getDigest(PathFragment path) throws IOException {
Path file = tracingFileSystem.getPath("/file.txt");
FileSystemUtils.writeContentAsLatin1(file, "some contents");

byte[] digest1 = DigestUtils.getDigestWithManualFallback(file, SyscallCache.NO_CACHE);
byte[] digest = DigestUtils.getDigestWithManualFallback(file, SyscallCache.NO_CACHE);
assertThat(getFastDigestCounter.get()).isEqualTo(1);
assertThat(getDigestCounter.get()).isEqualTo(1);

assertThat(DigestUtils.getDigestWithManualFallback(file, SyscallCache.NO_CACHE))
.isEqualTo(digest1);
.isEqualTo(digest);
assertThat(getFastDigestCounter.get()).isEqualTo(2);
assertThat(getDigestCounter.get()).isEqualTo(1); // Cached.

DigestUtils.clearCache();

assertThat(DigestUtils.getDigestWithManualFallback(file, SyscallCache.NO_CACHE))
.isEqualTo(digest);
assertThat(getFastDigestCounter.get()).isEqualTo(3);
assertThat(getDigestCounter.get()).isEqualTo(2); // Not cached.
}

@Test
Expand Down

0 comments on commit 28ec988

Please sign in to comment.