]> 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:02:21 +0000 (20:02 +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).

doc/MANUAL.adoc
src/ccache.c
src/ccache.h
src/stats.c
test/suites/readonly.bash
test/suites/readonly_direct.bash

index 1717c18e21f15e54cbd62caaefd97483a7618da8..409d56084b2dba655940e41b19b62bfdce5697d0 100644 (file)
@@ -508,9 +508,13 @@ might be incorrect.
 *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
-    *temporary_dir* as otherwise ccache will fail to create temporary files.
+    will not add new results to the cache. Statistics counters will still be
+    updated, though, unless the *stats* option is set to *false*.
++
+If you are using this because your ccache directory is read-only, you need to
+set *temporary_dir* since ccache will fail to create temporary files otherwise.
+You may also want to set *stats = false* to make ccache not even try to update
+stats files.
 
 *read_only_direct* (*CCACHE_READONLY_DIRECT* or *CCACHE_NOREADONLY_DIRECT*, see <<_boolean_values,Boolean values>> above)::
 
index 9978d3874ed3cd42649bd9b851c2bc42d2c7ce31..e617bf48893d33a266039b9b20e5b16a12f66c55 100644 (file)
@@ -595,6 +595,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();
                }
        }
@@ -1519,18 +1520,21 @@ to_cache(struct args *args, struct hash *depend_mode_hash)
                if (x_rename(tmp_stderr, tmp_stderr2)) {
                        cc_log("Failed to rename %s to %s: %s", 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();
                }
 
@@ -1538,6 +1542,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();
                }
 
@@ -1572,6 +1577,7 @@ to_cache(struct args *args, struct hash *depend_mode_hash)
                struct file_hash *object_hash =
                        object_hash_from_depfile(output_dep, depend_mode_hash);
                if (!object_hash) {
+                       stats_update(STATS_ERROR);
                        failed();
                }
                update_cached_result_globals(object_hash);
@@ -1634,7 +1640,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
@@ -2241,6 +2247,7 @@ calculate_object_hash(struct args *args, struct hash *hash, int direct_mode)
                hash_delimiter(hash, "sourcecode");
                int result = hash_source_code_file(conf, hash, input_file);
                if (result & HASH_SOURCE_CODE_ERROR) {
+                       stats_update(STATS_ERROR);
                        failed();
                }
                if (result & HASH_SOURCE_CODE_FOUND_TIME) {
@@ -2585,6 +2592,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;
                        }
@@ -3091,6 +3099,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) {
@@ -3885,6 +3894,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();
        }
 
@@ -3892,6 +3902,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();
        }
 }
@@ -3935,6 +3946,7 @@ ccache(int argc, char *argv[])
 
        if (conf->disable) {
                cc_log("ccache is disabled");
+               stats_update(STATS_CACHEMISS); // Dummy to trigger stats_flush.
                failed();
        }
 
@@ -3962,7 +3974,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");
 
@@ -4050,6 +4062,7 @@ ccache(int argc, char *argv[])
 
        if (conf->read_only_direct) {
                cc_log("Read-only direct mode; running real compiler");
+               stats_update(STATS_CACHEMISS);
                failed();
        }
 
@@ -4096,6 +4109,7 @@ ccache(int argc, char *argv[])
 
        if (conf->read_only) {
                cc_log("Read-only mode; running real compiler");
+               stats_update(STATS_CACHEMISS);
                failed();
        }
 
index cabc980d653f392d27fff3277962fd59c23d69df..0d6be609a01c4e111aea6b34fad7b268a19e2993 100644 (file)
@@ -1,5 +1,5 @@
 // Copyright (C) 2002-2007 Andrew Tridgell
-// Copyright (C) 2009-2019 Joel Rosdahl
+// Copyright (C) 2009-2020 Joel Rosdahl
 //
 // This program is free software; you can redistribute it and/or modify it
 // under the terms of the GNU General Public License as published by the Free
@@ -43,7 +43,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 214f3be500a72474f955dcb19a40f5c0b5f8d5f5..c06c44b4b940f205619aa03fa1636570e2f43c09 100644 (file)
@@ -1,5 +1,5 @@
 // Copyright (C) 2002-2004 Andrew Tridgell
-// Copyright (C) 2009-2019 Joel Rosdahl
+// Copyright (C) 2009-2020 Joel Rosdahl
 //
 // This program is free software; you can redistribute it and/or modify it
 // under the terms of the GNU General Public License as published by the Free
@@ -79,7 +79,7 @@ static struct {
                FLAG_ALWAYS
        },
        {
-               STATS_TOCACHE,
+               STATS_CACHEMISS,
                "cache_miss",
                "cache miss",
                NULL,
@@ -370,7 +370,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;
 }
@@ -442,11 +442,26 @@ stats_flush_to_file(const char *sfile, struct counters *updates)
 {
        assert(conf);
 
-       if (!conf->stats) {
+       if (!updates) {
                return;
        }
 
-       if (!updates) {
+       if (conf->disable) {
+               // Just log result, don't update statistics.
+               cc_log("Result: disabled");
+               return;
+       }
+
+       if (!str_eq(conf->log_file, "") || conf->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 (!conf->stats) {
                return;
        }
 
@@ -483,15 +498,6 @@ stats_flush_to_file(const char *sfile, struct counters *updates)
        stats_write(sfile, counters);
        lockfile_release(sfile);
 
-       if (!str_eq(conf->log_file, "") || conf->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 = dirname(sfile);
        bool need_cleanup = false;
 
@@ -591,7 +597,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
 }