From: Joel Rosdahl Date: Tue, 8 Jun 2010 21:12:24 +0000 (+0200) Subject: Count actual cache size and number of files again X-Git-Tag: v3.0~41 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7acf0db7ab1644c5cde2574b0e2562b3104f2804;p=thirdparty%2Fccache.git Count actual cache size and number of files again --- diff --git a/NEWS.txt b/NEWS.txt index 43b975968..a45be604e 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -19,21 +19,10 @@ Upgrade notes ~~~~~~~~~~~~~ - The way the hashes are calculated has changed, so you won't get cache - hits for compilation results stored by older ccache versions. - - - The statistics counters ``files in cache'' and ``cache size'' now only - count object files. (Previously, files containing cached standard error - output were counted as well.) The existing values of the two counters - will be erroneous at first after the upgrade, but will correct themselves - eventually when enough cleanups have been made, or when you run +ccache - --cleanup+. - - - The ``max file'' and ``max cache size'' settings now specify thresholds - for object files count and size. - - - Because of the changes mentioned above, you might as well clear the old - cache directory with `ccache --cleanup` if you want, unless you plan to - keep using an older ccache version. + hits for compilation results stored by older ccache versions. Because of + this, you might as well clear the old cache directory with `ccache + --cleanup` if you want, unless you plan to keep using an older ccache + version. New features and improvements diff --git a/ccache.c b/ccache.c index d92fcadad..db9b40d16 100644 --- a/ccache.c +++ b/ccache.c @@ -584,6 +584,8 @@ static void to_cache(ARGS *args) char *tmp_stdout, *tmp_stderr, *tmp_obj; struct stat st; int status; + size_t added_bytes = 0; + unsigned added_files = 0; x_asprintf(&tmp_stdout, "%s.tmp.stdout.%s", cached_obj, tmp_string()); x_asprintf(&tmp_stderr, "%s.tmp.stderr.%s", cached_obj, tmp_string()); @@ -710,6 +712,11 @@ static void to_cache(ARGS *args) failed(); } cc_log("Stored in cache: %s", cached_stderr); + if (enable_compression) { + stat(cached_stderr, &st); + } + added_bytes += file_size(&st); + added_files += 1; } else { unlink(tmp_stderr); } @@ -719,6 +726,9 @@ static void to_cache(ARGS *args) failed(); } else { cc_log("Stored in cache: %s", cached_obj); + stat(cached_obj, &st); + added_bytes += file_size(&st); + added_files += 1; } /* @@ -731,7 +741,7 @@ static void to_cache(ARGS *args) failed(); } - stats_tocache(file_size(&st)); + stats_update_size(STATS_TOCACHE, added_bytes / 1024, added_files); free(tmp_obj); free(tmp_stderr); @@ -1180,6 +1190,8 @@ static void from_cache(enum fromcache_call_mode mode, int put_object_in_manifest /* Continue despite the error. */ } else { cc_log("Stored in cache: %s", cached_dep); + stat(cached_dep, &st); + stats_update_size(STATS_NONE, file_size(&st) / 1024, 1); } } @@ -1211,13 +1223,21 @@ static void from_cache(enum fromcache_call_mode mode, int put_object_in_manifest && put_object_in_manifest && included_files && !getenv("CCACHE_READONLY")) { + struct stat st; + size_t old_size = 0; /* in bytes */ + if (stat(manifest_path, &st) == 0) { + old_size = file_size(&st); + } if (manifest_put(manifest_path, cached_obj_hash, included_files)) { - cc_log("Added object file hash to %s", - manifest_path); + cc_log("Added object file hash to %s", manifest_path); update_mtime(manifest_path); + stat(manifest_path, &st); + stats_update_size( + STATS_NONE, + (file_size(&st) - old_size) / 1024, + old_size == 0 ? 1 : 0); } else { - cc_log("Failed to add object file hash to %s", - manifest_path); + cc_log("Failed to add object file hash to %s", manifest_path); } } @@ -1234,6 +1254,7 @@ static void from_cache(enum fromcache_call_mode mode, int put_object_in_manifest break; case FROMCACHE_COMPILED_MODE: + // Stats already updated in to_cache(). break; } diff --git a/ccache.h b/ccache.h index b426f7ad8..389af2158 100644 --- a/ccache.h +++ b/ccache.h @@ -112,7 +112,7 @@ void update_mtime(const char *path); void stats_update(enum stats stat); void stats_zero(void); void stats_summary(void); -void stats_tocache(size_t size); +void stats_update_size(enum stats stat, size_t size, unsigned files); void stats_read(const char *stats_file, unsigned counters[STATS_END]); int stats_set_limits(long maxfiles, long maxsize); size_t value_units(const char *s); diff --git a/manual.txt b/manual.txt index e3cc0808f..46a5f4010 100644 --- a/manual.txt +++ b/manual.txt @@ -104,10 +104,10 @@ refer to the compiler's documentation. *-F, --max-files*='N':: - Set the maximum number of object files allowed in the cache. The value is - stored inside the cache directory and applies to all future compilations. - Due to the way the value is stored the actual value used is always rounded - down to the nearest multiple of 16. + Set the maximum number of files allowed in the cache. The value is stored + inside the cache directory and applies to all future compilations. Due to + the way the value is stored the actual value used is always rounded down to + the nearest multiple of 16. *-h, --help*:: @@ -115,10 +115,10 @@ refer to the compiler's documentation. *-M, --max-size*='SIZE':: - Set the maximum size of the object files stored in the cache. You can - specify a value in gigabytes, megabytes or kilobytes by appending a G, M or - K to the value. The default is gigabytes. The actual value stored is - rounded down to the nearest multiple of 16 kilobytes. + Set the maximum size of the files stored in the cache. You can specify a + value in gigabytes, megabytes or kilobytes by appending a G, M or K to the + value. The default is gigabytes. The actual value stored is rounded down to + the nearest multiple of 16 kilobytes. *-s, --show-stats*:: @@ -343,11 +343,11 @@ cases you won't need any of these as the defaults will be fine. CACHE SIZE MANAGEMENT --------------------- -By default ccache has a one gigabyte limit on the total size of object files in -the cache and no maximum number of object files. You can set different limits -using the *-M*/*--max-size* and *-F*/*--max-files* options. Use *ccache -s* to -see the cache size and the currently configured limits (in addition to other -various statistics). +By default ccache has a one gigabyte limit on the total size of files in the +cache and no maximum number of files. You can set different limits using the +*-M*/*--max-size* and *-F*/*--max-files* options. Use *ccache -s* to see the +cache size and the currently configured limits (in addition to other various +statistics). CACHE COMPRESSION diff --git a/stats.c b/stats.c index ae9db27ec..d25a4c5a7 100644 --- a/stats.c +++ b/stats.c @@ -140,8 +140,11 @@ static void stats_read_fd(int fd, unsigned counters[STATS_END]) parse_stats(counters, buf); } -/* update the stats counter for this compile */ -static void stats_update_size(enum stats stat, size_t size) +/* + * Update a statistics counter (unless it's STATS_NONE) and also record that a + * number of bytes and files have been added to the cache. Size is in KiB. + */ +void stats_update_size(enum stats stat, size_t size, unsigned files) { int fd; unsigned counters[STATS_END]; @@ -159,33 +162,22 @@ static void stats_update_size(enum stats stat, size_t size) x_asprintf(&stats_file, "%s/stats", cache_dir); } - /* open safely to try to prevent symlink races */ fd = safe_open(stats_file); - - /* still can't get it? don't bother ... */ if (fd == -1) return; - - memset(counters, 0, sizeof(counters)); - if (write_lock_fd(fd) != 0) return; - /* read in the old stats */ + memset(counters, 0, sizeof(counters)); stats_read_fd(fd, counters); - /* update them */ - counters[stat]++; - - /* on a cache miss we up the file count and size */ - if (stat == STATS_TOCACHE) { - counters[STATS_NUMFILES] += 1; - counters[STATS_TOTALSIZE] += size; + if (stat != STATS_NONE) { + counters[stat]++; } + counters[STATS_NUMFILES] += files; + counters[STATS_TOTALSIZE] += size; - /* and write them out */ write_stats(fd, counters); close(fd); - /* we might need to cleanup if the cache has now got too big */ if (counters[STATS_MAXFILES] != 0 && counters[STATS_NUMFILES] > counters[STATS_MAXFILES]) { need_cleanup = 1; @@ -202,19 +194,10 @@ static void stats_update_size(enum stats stat, size_t size) } } -/* record a cache miss */ -void stats_tocache(size_t size) -{ - /* convert size to kilobytes */ - size = size / 1024; - - stats_update_size(STATS_TOCACHE, size); -} - /* update a normal stat */ void stats_update(enum stats stat) { - stats_update_size(stat, 0); + stats_update_size(stat, 0, 0); } /* read in the stats from one dir and add to the counters */ diff --git a/test.sh b/test.sh index 5adaec5ca..b1ee28afb 100755 --- a/test.sh +++ b/test.sh @@ -360,16 +360,19 @@ int stderr(void) /* Trigger warning by having no return statement. */ } EOF + checkstat 'files in cache' 3 $CCACHE_COMPILE -Wall -W -c stderr.c 2>/dev/null num=`find $CCACHE_DIR -name '*.stderr' | wc -l` if [ $num -ne 1 ]; then test_failed "$num stderr files found, expected 1" fi + checkstat 'files in cache' 5 testname="zero-stats" $CCACHE -z > /dev/null checkstat 'cache hit (preprocessed)' 0 checkstat 'cache miss' 0 + checkstat 'files in cache' 5 testname="clear" $CCACHE -C > /dev/null @@ -525,13 +528,13 @@ EOF ################################################################## # Check calculation of dependency file names. - $CCACHE -z >/dev/null + $CCACHE -Cz >/dev/null + checkstat 'files in cache' 0 mkdir test.dir - # Make sure the dependency file is in the cache: - $CCACHE $COMPILER -MD -c test.c for ext in .obj "" . .foo.bar; do testname="dependency file calculation from object file 'test$ext'" dep_file=test.dir/`echo test$ext | sed 's/\.[^.]*\$//'`.d + $CCACHE $COMPILER -MD -c test.c -o test.dir/test$ext rm -f $dep_file $CCACHE $COMPILER -MD -c test.c -o test.dir/test$ext if [ ! -f $dep_file ]; then @@ -539,6 +542,7 @@ EOF fi done rm -rf test.dir + checkstat 'files in cache' 12 ################################################################## # Check that -Wp,-MD,file.d works.