]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Count the number of cleanups performed, in stats
authorAnders Björklund <anders@itension.se>
Fri, 25 Sep 2015 17:22:44 +0000 (19:22 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 21 Jul 2016 19:40:07 +0000 (21:40 +0200)
When running a busy ccache, it is useful to keep track of the number
of cleanups performed to know when you need to increase the cache size.

The number shown is actually the number of subdirectories, so when
you do a full cleanup the counter usually increases by 16 (not just 1).

ccache.h
cleanup.c
stats.c
test.sh

index 0b1792da686ff4008775bacad77a62912f59c36c..ae0906f59da8cc1a50e8cb89288b04f59ba953ee 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -51,6 +51,7 @@ enum stats {
        STATS_COMPCHECK = 26,
        STATS_CANTUSEPCH = 27,
        STATS_PREPROCESSING = 28,
+       STATS_NUMCLEANUPS = 29,
 
        STATS_END
 };
@@ -192,6 +193,7 @@ void stats_update_size(uint64_t size, unsigned files);
 void stats_get_obsolete_limits(const char *dir, unsigned *maxfiles,
                                uint64_t *maxsize);
 void stats_set_sizes(const char *dir, unsigned num_files, uint64_t total_size);
+void stats_add_cleanup(const char *dir, unsigned count);
 void stats_read(const char *path, struct counters *counters);
 void stats_write(const char *path, struct counters *counters);
 
index dcb1a1570ff72d2de6415d1e921bff3315670142..515c8bcba2eac3123975f1e570a16ebc53ead42e 100644 (file)
--- a/cleanup.c
+++ b/cleanup.c
@@ -130,11 +130,12 @@ delete_sibling_file(const char *base, const char *extension)
 
 /* sort the files we've found and delete the oldest ones until we are
    below the thresholds */
-static void
+static bool
 sort_and_clean(void)
 {
        unsigned i;
        char *last_base = x_strdup("");
+       bool clean = false;
 
        if (num_files > 1) {
                /* Sort in ascending mtime order. */
@@ -181,8 +182,10 @@ sort_and_clean(void)
                        /* .manifest or unknown file. */
                        delete_file(files[i]->fname, files[i]->size);
                }
+               clean = true;
        }
        free(last_base);
+       return clean;
 }
 
 /* cleanup in one cache subdir */
@@ -190,6 +193,7 @@ void
 cleanup_dir(struct conf *conf, const char *dir)
 {
        unsigned i;
+       bool clean;
 
        cc_log("Cleaning up cache directory %s", dir);
 
@@ -204,7 +208,12 @@ cleanup_dir(struct conf *conf, const char *dir)
        traverse(dir, traverse_fn);
 
        /* clean the cache */
-       sort_and_clean();
+       clean = sort_and_clean();
+
+       if (clean) {
+               cc_log("Cleaned up cache directory %s", dir);
+               stats_add_cleanup(dir, 1);
+       }
 
        stats_set_sizes(dir, files_in_cache, cache_size);
 
@@ -253,9 +262,31 @@ static void wipe_fn(const char *fname, struct stat *st)
        }
        free(p);
 
+       files_in_cache++;
+
        x_unlink(fname);
 }
 
+/* wipe in one cache subdir */
+void
+wipe_dir(struct conf *conf, const char *dir)
+{
+       cc_log("Clearing out cache directory %s", dir);
+
+       files_in_cache_threshold = conf->max_files * LIMIT_MULTIPLE / 16;
+
+       files_in_cache = 0;
+
+       traverse(dir, wipe_fn);
+
+       if (files_in_cache > 0) {
+               cc_log("Cleared out cache directory %s", dir);
+               stats_add_cleanup(dir, 1);
+       }
+
+       files_in_cache = 0;
+}
+
 /* wipe all cached files in all subdirs */
 void wipe_all(struct conf *conf)
 {
@@ -263,7 +294,7 @@ void wipe_all(struct conf *conf)
 
        for (i = 0; i <= 0xF; i++) {
                char *dname = format("%s/%1x", conf->cache_dir, i);
-               traverse(dname, wipe_fn);
+               wipe_dir(conf, dname);
                free(dname);
        }
 
diff --git a/stats.c b/stats.c
index 5f217657a27a8ffc1fafbcc7fdf1a90c0a5e437d..e9a8f09e86d59eae2c71bb36acd6ae476d7ab24b 100644 (file)
--- a/stats.c
+++ b/stats.c
@@ -78,6 +78,7 @@ static struct {
        { STATS_DEVICE,       "output to a non-regular file   ", NULL, 0 },
        { STATS_NOINPUT,      "no input file                  ", NULL, 0 },
        { STATS_BADEXTRAFILE, "error hashing extra file       ", NULL, 0 },
+       { STATS_NUMCLEANUPS,  "cleanups performed             ", NULL, FLAG_ALWAYS },
        { STATS_NUMFILES,     "files in cache                 ", NULL,
                FLAG_NOZERO|FLAG_ALWAYS },
        { STATS_TOTALSIZE,    "cache size                     ",
@@ -407,3 +408,22 @@ stats_set_sizes(const char *dir, unsigned num_files, uint64_t total_size)
        free(statsfile);
        counters_free(counters);
 }
+
+/* count directory cleanup run */
+void
+stats_add_cleanup(const char *dir, unsigned count)
+{
+       struct counters *counters = counters_init(STATS_END);
+       char *statsfile;
+
+       statsfile = format("%s/stats", dir);
+
+       if (lockfile_acquire(statsfile, lock_staleness_limit)) {
+               stats_read(statsfile, counters);
+               counters->data[STATS_NUMCLEANUPS] += count;
+               stats_write(statsfile, counters);
+               lockfile_release(statsfile);
+       }
+       free(statsfile);
+       counters_free(counters);
+}
diff --git a/test.sh b/test.sh
index 29a0e1acf838e7d20e2e8b3da94ca45d9a493300..9aeeee3cf5fc258398f63571f1b6f12f6df7b2f7 100755 (executable)
--- a/test.sh
+++ b/test.sh
@@ -2030,6 +2030,7 @@ cleanup_suite() {
     checkfilecount 0 '*.d' $CCACHE_DIR
     checkfilecount 0 '*.stderr' $CCACHE_DIR
     checkstat 'files in cache' 0
+    checkstat 'cleanups performed' 1
 
     testname="forced cleanup, no limits"
     $CCACHE -C >/dev/null
@@ -2040,6 +2041,7 @@ cleanup_suite() {
     checkfilecount 10 '*.d' $CCACHE_DIR
     checkfilecount 10 '*.stderr' $CCACHE_DIR
     checkstat 'files in cache' 30
+    checkstat 'cleanups performed' 0
 
     testname="forced cleanup, file limit"
     $CCACHE -C >/dev/null
@@ -2052,6 +2054,7 @@ cleanup_suite() {
     checkfilecount 7 '*.d' $CCACHE_DIR
     checkfilecount 7 '*.stderr' $CCACHE_DIR
     checkstat 'files in cache' 21
+    checkstat 'cleanups performed' 1
     for i in 0 1 2 3 4 5 9; do
         file=$CCACHE_DIR/a/result$i-4017.o
         if [ ! -f $file ]; then
@@ -2081,6 +2084,7 @@ cleanup_suite() {
     checkfilecount 3 '*.d' $CCACHE_DIR
     checkfilecount 3 '*.stderr' $CCACHE_DIR
     checkstat 'files in cache' 9
+    checkstat 'cleanups performed' 1
     for i in 3 4 5; do
         file=$CCACHE_DIR/a/result$i-4017.o
         if [ ! -f $file ]; then
@@ -2112,6 +2116,7 @@ cleanup_suite() {
     checkfilecount 156 '*.d' $CCACHE_DIR
     checkfilecount 156 '*.stderr' $CCACHE_DIR
     checkstat 'files in cache' 469
+    checkstat 'cleanups performed' 1
 
     testname="sibling cleanup"
     $CCACHE -C >/dev/null