]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Make sure to always log a “Result:” line
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 4 Jan 2020 18:19:01 +0000 (19:19 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 5 Jan 2020 19:36:22 +0000 (20:36 +0100)
Fixes #500.

A side effect of this is that read-only cache misses now will update the
“cache miss” statistics counter, which is consistent with the cache hit
cases (which already now update the counters).

(cherry picked from commit 1d2b76a71b12c2ffdd107f89a3d9caa6cd223d44)

doc/MANUAL.adoc
src/ccache.cpp
src/ccache.hpp
src/stats.cpp
test/suites/readonly.bash
test/suites/readonly_direct.bash

index b9820e0f140d96b578e8fc01ac27c3030eed76ee..d9db0a06f9583358c1773e52a1ad8a1b0440ebee 100644 (file)
@@ -593,11 +593,14 @@ file in `/etc/rsyslog.d`:
 
 [[config_read_only]] *read_only* (*CCACHE_READONLY* or *CCACHE_NOREADONLY*, see <<_boolean_values,Boolean values>> above)::
 
-    If true, ccache will attempt to use existing cached object files, but it
-    will not to try to add anything new to the cache. If you are using this
-    because your ccache directory is read-only, then you need to set
-    <<config_temporary_dir,*temporary_dir*>> as otherwise ccache will fail to
-    create temporary files.
+    If true, ccache will attempt to use existing cached results, but it will not
+    add new results to the cache. Statistics counters will still be updated,
+    though, unless the <<config_stats,*stats*>> option is set to *false*.
++
+If you are using this because your ccache directory is read-only, you need to
+set <<config_temporary_dir,*temporary_dir*>> since ccache will fail to create
+temporary files otherwise. You may also want to set <<config_stats,*stats*>> to
+*false* make ccache not even try to update stats files.
 
 [[config_read_only_direct]] *read_only_direct* (*CCACHE_READONLY_DIRECT* or *CCACHE_NOREADONLY_DIRECT*, see <<_boolean_values,Boolean values>> above)::
 
index ca63c0efb7980517dae9f7d69dcbcc370c748d3e..4b1f929d6bb49726fdd8e0c3180cb74545f9a74f 100644 (file)
@@ -592,6 +592,7 @@ get_current_working_dir(void)
     if (!current_working_dir) {
       cc_log("Unable to determine current working directory: %s",
              strerror(errno));
+      stats_update(STATS_ERROR);
       failed();
     }
   }
@@ -1394,18 +1395,21 @@ to_cache(struct args* args, struct hash* depend_mode_hash)
              tmp_stderr,
              tmp_stderr2,
              strerror(errno));
+      stats_update(STATS_ERROR);
       failed();
     }
 
     int fd_cpp_stderr = open(cpp_stderr, O_RDONLY | O_BINARY);
     if (fd_cpp_stderr == -1) {
       cc_log("Failed opening %s: %s", cpp_stderr, strerror(errno));
+      stats_update(STATS_ERROR);
       failed();
     }
 
     int fd_real_stderr = open(tmp_stderr2, O_RDONLY | O_BINARY);
     if (fd_real_stderr == -1) {
       cc_log("Failed opening %s: %s", tmp_stderr2, strerror(errno));
+      stats_update(STATS_ERROR);
       failed();
     }
 
@@ -1413,6 +1417,7 @@ to_cache(struct args* args, struct hash* depend_mode_hash)
       open(tmp_stderr, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
     if (fd_result == -1) {
       cc_log("Failed opening %s: %s", tmp_stderr, strerror(errno));
+      stats_update(STATS_ERROR);
       failed();
     }
 
@@ -1447,6 +1452,7 @@ to_cache(struct args* args, struct hash* depend_mode_hash)
     struct digest* result_name =
       result_name_from_depfile(output_dep, depend_mode_hash);
     if (!result_name) {
+      stats_update(STATS_ERROR);
       failed();
     }
     update_cached_result_globals(result_name);
@@ -1516,7 +1522,7 @@ to_cache(struct args* args, struct hash* depend_mode_hash)
 
   MTR_END("file", "file_put");
 
-  stats_update(STATS_TOCACHE);
+  stats_update(STATS_CACHEMISS);
 
   // Make sure we have a CACHEDIR.TAG in the cache part of cache_dir. This can
   // be done almost anywhere, but we might as well do it near the end as we
@@ -2115,6 +2121,7 @@ calculate_result_name(struct args* args, struct hash* hash, int direct_mode)
     hash_delimiter(hash, "sourcecode");
     int result = hash_source_code_file(g_config, hash, input_file);
     if (result & HASH_SOURCE_CODE_ERROR) {
+      stats_update(STATS_ERROR);
       failed();
     }
     if (result & HASH_SOURCE_CODE_FOUND_TIME) {
@@ -2439,6 +2446,7 @@ cc_process_args(struct args* args,
       i++;
       if (i == argc) {
         cc_log("--ccache-skip lacks an argument");
+        stats_update(STATS_ARGS);
         result = false;
         goto out;
       }
@@ -2969,6 +2977,7 @@ cc_process_args(struct args* args,
         // know what the user means, and what the compiler will do.
         if (arg_profile_dir && profile_dir) {
           cc_log("Profile directory already set; giving up");
+          stats_update(STATS_UNSUPPORTED_OPTION);
           result = false;
           goto out;
         } else if (arg_profile_dir) {
@@ -3734,6 +3743,7 @@ set_up_uncached_err(void)
   int uncached_fd = dup(2); // The file descriptor is intentionally leaked.
   if (uncached_fd == -1) {
     cc_log("dup(2) failed: %s", strerror(errno));
+    stats_update(STATS_ERROR);
     failed();
   }
 
@@ -3741,6 +3751,7 @@ set_up_uncached_err(void)
   char* buf = format("UNCACHED_ERR_FD=%d", uncached_fd);
   if (putenv(buf) == -1) {
     cc_log("putenv failed: %s", strerror(errno));
+    stats_update(STATS_ERROR);
     failed();
   }
 }
@@ -3794,6 +3805,7 @@ ccache(int argc, char* argv[])
 
   if (g_config.disable()) {
     cc_log("ccache is disabled");
+    stats_update(STATS_CACHEMISS); // Dummy to trigger stats_flush.
     failed();
   }
 
@@ -3822,7 +3834,7 @@ ccache(int argc, char* argv[])
   MTR_BEGIN("main", "process_args");
   if (!cc_process_args(
         orig_args, &preprocessor_args, &extra_args_to_hash, &compiler_args)) {
-    failed();
+    failed(); // stats_update is called in cc_process_args.
   }
   MTR_END("main", "process_args");
 
@@ -3909,6 +3921,7 @@ ccache(int argc, char* argv[])
 
   if (g_config.read_only_direct()) {
     cc_log("Read-only direct mode; running real compiler");
+    stats_update(STATS_CACHEMISS);
     failed();
   }
 
@@ -3955,6 +3968,7 @@ ccache(int argc, char* argv[])
 
   if (g_config.read_only()) {
     cc_log("Read-only mode; running real compiler");
+    stats_update(STATS_CACHEMISS);
     failed();
   }
 
index d8ea395d30ae78dc76157eef422664c55bfc4c2e..0c254703b24762b6774ea92dbd895e24f05caba1 100644 (file)
@@ -1,5 +1,5 @@
 // Copyright (C) 2002-2007 Andrew Tridgell
-// Copyright (C) 2009-2019 Joel Rosdahl and other contributors
+// Copyright (C) 2009-2020 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -49,7 +49,7 @@ enum stats {
   STATS_STDOUT = 1,
   STATS_STATUS = 2,
   STATS_ERROR = 3,
-  STATS_TOCACHE = 4,
+  STATS_CACHEMISS = 4,
   STATS_PREPROCESSOR = 5,
   STATS_COMPILER = 6,
   STATS_MISSING = 7,
index 07ae928033aea61b95000bcc029d87c5bf081854..9241151624a22bc75e8a6f589711506ad25fb270 100644 (file)
@@ -1,5 +1,5 @@
 // Copyright (C) 2002-2004 Andrew Tridgell
-// Copyright (C) 2009-2019 Joel Rosdahl and other contributors
+// Copyright (C) 2009-2020 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -77,7 +77,7 @@ static struct
    "cache hit (preprocessed)",
    NULL,
    FLAG_ALWAYS},
-  {STATS_TOCACHE, "cache_miss", "cache miss", NULL, FLAG_ALWAYS},
+  {STATS_CACHEMISS, "cache_miss", "cache miss", NULL, FLAG_ALWAYS},
   {STATS_LINK, "called_for_link", "called for link", NULL, 0},
   {STATS_PREPROCESSING,
    "called_for_preprocessing",
@@ -246,7 +246,7 @@ stats_hit_rate(struct counters* counters)
   unsigned direct = counters->data[STATS_CACHEHIT_DIR];
   unsigned preprocessed = counters->data[STATS_CACHEHIT_CPP];
   unsigned hit = direct + preprocessed;
-  unsigned miss = counters->data[STATS_TOCACHE];
+  unsigned miss = counters->data[STATS_CACHEMISS];
   unsigned total = hit + miss;
   return total > 0 ? (100.0 * hit) / total : 0.0;
 }
@@ -321,11 +321,26 @@ stats_read(const char* sfile, struct counters* counters)
 static void
 stats_flush_to_file(const char* sfile, struct counters* updates)
 {
-  if (!g_config.stats()) {
+  if (!updates) {
     return;
   }
 
-  if (!updates) {
+  if (g_config.disable()) {
+    // Just log result, don't update statistics.
+    cc_log("Result: disabled");
+    return;
+  }
+
+  if (!g_config.log_file().empty() || g_config.debug()) {
+    for (int i = 0; i < STATS_END; ++i) {
+      if (updates->data[stats_info[i].stat] != 0
+          && !(stats_info[i].flags & FLAG_NOZERO)) {
+        cc_log("Result: %s", stats_info[i].message);
+      }
+    }
+  }
+
+  if (!g_config.stats()) {
     return;
   }
 
@@ -363,15 +378,6 @@ stats_flush_to_file(const char* sfile, struct counters* updates)
   stats_write(sfile, counters);
   lockfile_release(sfile);
 
-  if (!g_config.log_file().empty() || g_config.debug()) {
-    for (int i = 0; i < STATS_END; ++i) {
-      if (updates->data[stats_info[i].stat] != 0
-          && !(stats_info[i].flags & FLAG_NOZERO)) {
-        cc_log("Result: %s", stats_info[i].message);
-      }
-    }
-  }
-
   char* subdir = x_dirname(sfile);
   bool need_cleanup = false;
 
@@ -473,7 +479,7 @@ stats_summary(void)
       free(value);
     }
 
-    if (stat == STATS_TOCACHE) {
+    if (stat == STATS_CACHEMISS) {
       double percent = stats_hit_rate(counters);
       printf("cache hit rate                    %6.2f %%\n", percent);
     }
index be98696687450db59eb0e870078c3d0c6434824d..5cdbc251d356df057053ffd5336b600e3e397601 100644 (file)
@@ -44,9 +44,9 @@ SUITE_readonly() {
     if [ $? -ne 0 ]; then
         test_failed "Failure when compiling test2.c read-only"
     fi
-    if [ -d $CCACHE_DIR ]; then
-        test_failed "ccache dir was created"
-    fi
+    expect_stat 'cache hit (preprocessed)' 0
+    expect_stat 'cache miss' 1
+    expect_stat 'files in cache' 0
 
     # -------------------------------------------------------------------------
     # Check that read-only mode and direct mode work together.
index b1ea4c6e8d78d6da835a8682d8a119a5043ff3d0..9a7636f41fcae686920f4ae6735f5e61b3398f64 100644 (file)
@@ -29,5 +29,5 @@ SUITE_readonly_direct() {
     CCACHE_READONLY_DIRECT=1 $CCACHE_COMPILE -DFOO -c test.c -o test.o
     expect_stat 'cache hit (direct)' 0
     expect_stat 'cache hit (preprocessed)' 0
-    expect_stat 'cache miss' 1
+    expect_stat 'cache miss' 2
 }