From: Anders Björklund Date: Fri, 25 Sep 2015 17:22:44 +0000 (+0200) Subject: Count the number of cleanups performed, in stats X-Git-Tag: v3.3~61 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0769ea3630efd6315303f7325fc16d73a0ef1a4d;p=thirdparty%2Fccache.git Count the number of cleanups performed, in stats 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). --- diff --git a/ccache.h b/ccache.h index 0b1792da6..ae0906f59 100644 --- 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); diff --git a/cleanup.c b/cleanup.c index dcb1a1570..515c8bcba 100644 --- 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 5f217657a..e9a8f09e8 100644 --- 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 29a0e1acf..9aeeee3cf 100755 --- 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