]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Only move cache file to another level when we know the actual file count
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 5 Oct 2020 13:14:05 +0000 (15:14 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 5 Oct 2020 13:14:05 +0000 (15:14 +0200)
src/ccache.cpp
test/suites/cache_levels.bash

index fddef7c2995fdbfebd8142b9705fff4bb005c6ca..264297c26dbdcf67dda7409fdbb23d3757bb8d26 100644 (file)
@@ -1991,11 +1991,11 @@ update_stats_and_maybe_move_cache_file(const Context& ctx,
   // Use stats file in the level one subdirectory for cache bookkeeping counters
   // since cleanup is performed on level one. Use stats file in the level two
   // subdirectory for other counters to reduce lock contention.
-  const bool updated_file_size_or_count =
+  const bool use_stats_on_level_1 =
     counter_updates.get(Statistic::cache_size_kibibyte) != 0
     || counter_updates.get(Statistic::files_in_cache) != 0;
   std::string level_string = fmt::format("{:x}", name.bytes()[0] >> 4);
-  if (!updated_file_size_or_count) {
+  if (!use_stats_on_level_1) {
     level_string += fmt::format("/{:x}", name.bytes()[0] & 0xF);
   }
   const auto stats_file =
@@ -2009,18 +2009,23 @@ update_stats_and_maybe_move_cache_file(const Context& ctx,
     return nullopt;
   }
 
-  const auto wanted_level =
-    calculate_wanted_cache_level(counters->get(Statistic::files_in_cache));
-  const auto wanted_path = Util::get_path_in_cache(
-    ctx.config.cache_dir(), wanted_level, name.to_string() + file_suffix);
-  if (current_path != wanted_path) {
-    Util::ensure_dir_exists(Util::dir_name(wanted_path));
-    log("Moving {} to {}", current_path, wanted_path);
-    try {
-      Util::rename(current_path, wanted_path);
-    } catch (const Error&) {
-      // Two ccache processes may move the file at the same time, so failure to
-      // rename is OK.
+  if (use_stats_on_level_1) {
+    // Only consider moving the cache file to another level when we have read
+    // the level 1 stats file since it's only then we know the proper
+    // files_in_cache value.
+    const auto wanted_level =
+      calculate_wanted_cache_level(counters->get(Statistic::files_in_cache));
+    const auto wanted_path = Util::get_path_in_cache(
+      ctx.config.cache_dir(), wanted_level, name.to_string() + file_suffix);
+    if (current_path != wanted_path) {
+      Util::ensure_dir_exists(Util::dir_name(wanted_path));
+      log("Moving {} to {}", current_path, wanted_path);
+      try {
+        Util::rename(current_path, wanted_path);
+      } catch (const Error&) {
+        // Two ccache processes may move the file at the same time, so failure
+        // to rename is OK.
+      }
     }
   }
   return counters;
index c82eee0a0bc4fec56aab870ae42e3b74b1e3e2c2..776508bf8a8d2bc8e745dee3fe257424c5c15c39 100644 (file)
@@ -55,6 +55,13 @@ SUITE_cache_levels() {
     expect_on_level R 2
     expect_on_level M 2
 
+    $CCACHE_COMPILE -c test1.c
+    expect_stat 'cache hit (direct)' 1
+    expect_stat 'cache miss' 1
+    expect_stat 'files in cache' $((files + 2))
+    expect_on_level R 2
+    expect_on_level M 2
+
     # -------------------------------------------------------------------------
     TEST "Level 3"
 
@@ -68,6 +75,13 @@ SUITE_cache_levels() {
     expect_on_level R 3
     expect_on_level M 3
 
+    $CCACHE_COMPILE -c test1.c
+    expect_stat 'cache hit (direct)' 1
+    expect_stat 'cache miss' 1
+    expect_stat 'files in cache' $((files + 2))
+    expect_on_level R 3
+    expect_on_level M 3
+
     # -------------------------------------------------------------------------
     TEST "Level 4"
 
@@ -81,6 +95,13 @@ SUITE_cache_levels() {
     expect_on_level R 4
     expect_on_level M 4
 
+    $CCACHE_COMPILE -c test1.c
+    expect_stat 'cache hit (direct)' 1
+    expect_stat 'cache miss' 1
+    expect_stat 'files in cache' $((files + 2))
+    expect_on_level R 4
+    expect_on_level M 4
+
     # -------------------------------------------------------------------------
     TEST "No deeper than 4 levels"
 
@@ -93,4 +114,11 @@ SUITE_cache_levels() {
     expect_stat 'files in cache' $((files + 2))
     expect_on_level R 4
     expect_on_level M 4
+
+    $CCACHE_COMPILE -c test1.c
+    expect_stat 'cache hit (direct)' 1
+    expect_stat 'cache miss' 1
+    expect_stat 'files in cache' $((files + 2))
+    expect_on_level R 4
+    expect_on_level M 4
 }