]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
feat: Add statistics for direct/preprocessed cache miss
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 8 Aug 2021 15:35:18 +0000 (17:35 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 10 Aug 2021 13:38:26 +0000 (15:38 +0200)
src/ccache.cpp
src/core/Statistic.hpp
src/core/Statistics.cpp
test/suites/base.bash
test/suites/depend.bash
test/suites/direct.bash
test/suites/readonly_direct.bash
test/suites/stats_log.bash

index 98a425c59837a92de6553b43f88db02676308df3..1d629b3c6c5654f91b8c4e2a758f0e9bc0c728cb 100644 (file)
@@ -1713,14 +1713,14 @@ calculate_result_and_manifest_key(Context& ctx,
 enum class FromCacheCallMode { direct, cpp };
 
 // Try to return the compile result from cache.
-static optional<Statistic>
+static bool
 from_cache(Context& ctx, FromCacheCallMode mode, const Digest& result_key)
 {
   UmaskScope umask_scope(ctx.original_umask);
 
   // The user might be disabling cache hits.
   if (ctx.config.recache()) {
-    return nullopt;
+    return false;
   }
 
   // If we're using Clang, we can't trust a precompiled header object based on
@@ -1735,7 +1735,7 @@ from_cache(Context& ctx, FromCacheCallMode mode, const Digest& result_key)
       && ctx.args_info.output_is_precompiled_header
       && mode == FromCacheCallMode::cpp) {
     LOG_RAW("Not considering cached precompiled header in preprocessor mode");
-    return nullopt;
+    return false;
   }
 
   MTR_BEGIN("cache", "from_cache");
@@ -1744,7 +1744,7 @@ from_cache(Context& ctx, FromCacheCallMode mode, const Digest& result_key)
   const auto result_path =
     ctx.storage.get(result_key, core::CacheEntryType::result);
   if (!result_path) {
-    return nullopt;
+    return false;
   }
 
   Result::Reader result_reader(*result_path);
@@ -1755,13 +1755,11 @@ from_cache(Context& ctx, FromCacheCallMode mode, const Digest& result_key)
   MTR_END("cache", "from_cache");
   if (error) {
     LOG("Failed to get result from cache: {}", *error);
-    return nullopt;
+    return false;
   }
 
   LOG_RAW("Succeeded getting cached result");
-
-  return mode == FromCacheCallMode::direct ? Statistic::direct_cache_hit
-                                           : Statistic::preprocessed_cache_hit;
+  return true;
 }
 
 // Find the real compiler and put it into ctx.orig_args[0]. We just search the
@@ -2098,9 +2096,10 @@ do_cache_compilation(Context& ctx, const char* const* argv)
     MTR_END("hash", "direct_hash");
     if (result_key) {
       // If we can return from cache at this point then do so.
-      auto result = from_cache(ctx, FromCacheCallMode::direct, *result_key);
-      if (result) {
-        return *result;
+      const bool found =
+        from_cache(ctx, FromCacheCallMode::direct, *result_key);
+      if (found) {
+        return Statistic::direct_cache_hit;
       }
 
       // Wasn't able to return from cache at this point. However, the result
@@ -2112,6 +2111,8 @@ do_cache_compilation(Context& ctx, const char* const* argv)
       // Add result to manifest later.
       put_result_in_manifest = true;
     }
+
+    ctx.storage.primary.increment_statistic(Statistic::direct_cache_miss);
   }
 
   if (ctx.config.read_only_direct()) {
@@ -2160,13 +2161,15 @@ do_cache_compilation(Context& ctx, const char* const* argv)
     }
 
     // If we can return from cache at this point then do.
-    const auto result = from_cache(ctx, FromCacheCallMode::cpp, *result_key);
-    if (result) {
+    const auto found = from_cache(ctx, FromCacheCallMode::cpp, *result_key);
+    if (found) {
       if (manifest_key && put_result_in_manifest) {
         update_manifest_file(ctx, *manifest_key, *result_key);
       }
-      return *result;
+      return Statistic::preprocessed_cache_hit;
     }
+
+    ctx.storage.primary.increment_statistic(Statistic::preprocessed_cache_miss);
   }
 
   if (ctx.config.read_only()) {
index 2ad3cd7b0b031e4c3c3803e823dc36eeb0f3e7cc..43cc94f166cb3c990b9b0c32471045b5f4ec1604 100644 (file)
@@ -55,6 +55,8 @@ enum class Statistic {
   unsupported_code_directive = 30,
   stats_zeroed_timestamp = 31,
   could_not_use_modules = 32,
+  direct_cache_miss = 33,
+  preprocessed_cache_miss = 34,
 
   END
 };
index 6ff89afccd8fde274bbcc779cfefd96eb5ad8eae..ce3a24b18d503a62f742736b4db0a12d984a0ade 100644 (file)
@@ -79,6 +79,8 @@ const StatisticsField k_statistics_fields[] = {
   STATISTICS_FIELD(
     preprocessed_cache_hit, "cache hit (preprocessed)", FLAG_ALWAYS),
   STATISTICS_FIELD(cache_miss, "cache miss", FLAG_ALWAYS),
+  STATISTICS_FIELD(direct_cache_miss, "cache miss (direct)"),
+  STATISTICS_FIELD(preprocessed_cache_miss, "cache miss (preprocessed)"),
   STATISTICS_FIELD(called_for_link, "called for link"),
   STATISTICS_FIELD(called_for_preprocessing, "called for preprocessing"),
   STATISTICS_FIELD(multiple_source_files, "multiple source files"),
index 7251c20e981ddb42edf0e3e17e41ac7bebca2b72..84f51a48c380cc304c64b190ec5d1f0305eebe00 100644 (file)
@@ -7,12 +7,16 @@ base_tests() {
     $CCACHE_COMPILE -c test1.c
     expect_stat preprocessed_cache_hit 0
     expect_stat cache_miss 1
+    expect_stat direct_cache_miss 0
+    expect_stat preprocessed_cache_miss 1
     expect_stat files_in_cache 1
     expect_equal_object_files reference_test1.o test1.o
 
     $CCACHE_COMPILE -c test1.c
     expect_stat preprocessed_cache_hit 1
     expect_stat cache_miss 1
+    expect_stat direct_cache_miss 0
+    expect_stat preprocessed_cache_miss 1
     expect_stat files_in_cache 1
     expect_equal_object_files reference_test1.o test1.o
 
index 66df696c142c18acbeda5b2c9632a8c511c1abb1..24944acbc141a8330688dfd85a25b3f7421f6fe0 100644 (file)
@@ -99,6 +99,8 @@ SUITE_depend() {
     expect_stat direct_cache_hit 0
     expect_stat preprocessed_cache_hit 0
     expect_stat cache_miss 1
+    expect_stat direct_cache_miss 1
+    expect_stat preprocessed_cache_miss 0
     expect_stat files_in_cache 2 # result + manifest
 
     CCACHE_DEPEND=1 $CCACHE_COMPILE $DEPSFLAGS_CCACHE -c test.c
@@ -106,6 +108,8 @@ SUITE_depend() {
     expect_stat direct_cache_hit 1
     expect_stat preprocessed_cache_hit 0
     expect_stat cache_miss 1
+    expect_stat direct_cache_miss 1
+    expect_stat preprocessed_cache_miss 0
     expect_stat files_in_cache 2
 
     # -------------------------------------------------------------------------
index 616c03362a5c8aa0dffb0b0e8d682b69a04f3b4d..660ee07b654469d7022df3544a51fc4a07179624 100644 (file)
@@ -33,6 +33,8 @@ SUITE_direct() {
     expect_stat direct_cache_hit 0
     expect_stat preprocessed_cache_hit 0
     expect_stat cache_miss 1
+    expect_stat direct_cache_miss 1
+    expect_stat preprocessed_cache_miss 1
     expect_stat files_in_cache 2 # result + manifest
     expect_equal_object_files reference_test.o test.o
 
@@ -43,6 +45,8 @@ SUITE_direct() {
     expect_stat direct_cache_hit 1
     expect_stat preprocessed_cache_hit 0
     expect_stat cache_miss 1
+    expect_stat direct_cache_miss 1
+    expect_stat preprocessed_cache_miss 1
     expect_stat files_in_cache 2
     expect_equal_object_files reference_test.o test.o
     expect_newer_than $manifest_file test.c
index 68476723296b60bdf57c5c8e15ae0add32be7f72..738c2bf7c555648f0024d18bf0042358048e617c 100644 (file)
@@ -21,11 +21,15 @@ SUITE_readonly_direct() {
     expect_stat direct_cache_hit 0
     expect_stat preprocessed_cache_hit 0
     expect_stat cache_miss 1
+    expect_stat direct_cache_miss 1
+    expect_stat preprocessed_cache_miss 1
 
     CCACHE_READONLY_DIRECT=1 $CCACHE_COMPILE -c test.c -o test.o
     expect_stat direct_cache_hit 1
     expect_stat preprocessed_cache_hit 0
     expect_stat cache_miss 1
+    expect_stat direct_cache_miss 1
+    expect_stat preprocessed_cache_miss 1
 
     # -------------------------------------------------------------------------
     TEST "Direct miss doesn't lead to preprocessed hit"
@@ -34,9 +38,13 @@ SUITE_readonly_direct() {
     expect_stat direct_cache_hit 0
     expect_stat preprocessed_cache_hit 0
     expect_stat cache_miss 1
+    expect_stat direct_cache_miss 1
+    expect_stat preprocessed_cache_miss 1
 
     CCACHE_READONLY_DIRECT=1 $CCACHE_COMPILE -DFOO -c test.c -o test.o
     expect_stat direct_cache_hit 0
     expect_stat preprocessed_cache_hit 0
     expect_stat cache_miss 2
+    expect_stat direct_cache_miss 2
+    expect_stat preprocessed_cache_miss 1
 }
index 54a09a6c699261f156369d28eeaff358263767bf..a55de6e06d8c67f4a78e2b0f6137c9da8895af2f 100644 (file)
@@ -18,6 +18,8 @@ SUITE_stats_log() {
 
     expect_content stats.log "# test.c
 cache_miss
+direct_cache_miss
+preprocessed_cache_miss
 # test.c
 direct_cache_hit"
 }