From: Anders Björklund Date: Wed, 20 Jul 2016 08:26:25 +0000 (+0200) Subject: Show the time when stats were last zeroed out X-Git-Tag: v3.4~78^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F140%2Fhead;p=thirdparty%2Fccache.git Show the time when stats were last zeroed out Closes #97. --- diff --git a/ccache.h b/ccache.h index 7b29bb8cd..ee1f1f445 100644 --- a/ccache.h +++ b/ccache.h @@ -52,6 +52,7 @@ enum stats { STATS_CANTUSEPCH = 27, STATS_PREPROCESSING = 28, STATS_NUMCLEANUPS = 29, + STATS_ZEROTIMESTAMP = 30, STATS_END }; @@ -197,6 +198,7 @@ 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_timestamp(time_t time, struct counters *counters); void stats_read(const char *path, struct counters *counters); void stats_write(const char *path, struct counters *counters); diff --git a/stats.c b/stats.c index 6eff3e1ec..110020029 100644 --- a/stats.c +++ b/stats.c @@ -75,6 +75,7 @@ static struct { { STATS_NOINPUT, "no input file ", NULL, 0 }, { STATS_BADEXTRAFILE, "error hashing extra file ", NULL, 0 }, { STATS_NUMCLEANUPS, "cleanups performed ", NULL, FLAG_ALWAYS }, + { STATS_ZEROTIMESTAMP,"stats zero timestamp ", NULL, FLAG_NEVER }, { STATS_NUMFILES, "files in cache ", NULL, FLAG_NOZERO|FLAG_ALWAYS }, { STATS_TOTALSIZE, "cache size ", @@ -125,6 +126,13 @@ parse_stats(struct counters *counters, const char *buf) void stats_write(const char *path, struct counters *counters) { + struct stat st; + if (stat(path, &st) != 0 && errno == ENOENT) { + /* new stats, update zero timestamp */ + time_t now; + time(&now); + stats_timestamp(now, counters); + } char *tmp_file = format("%s.tmp", path); FILE *f = create_tmp_file(&tmp_file, "wb"); for (size_t i = 0; i < counters->size; i++) { @@ -166,6 +174,13 @@ stats_read(const char *sfile, struct counters *counters) free(data); } +// Set the timestamp when the counters were last zeroed out. +void +stats_timestamp(time_t time, struct counters *counters) +{ + counters->data[STATS_ZEROTIMESTAMP] = (unsigned) time; +} + // Write counter updates in counter_updates to disk. void stats_flush(void) @@ -263,12 +278,15 @@ void stats_summary(struct conf *conf) { struct counters *counters = counters_init(STATS_END); + time_t oldest; assert(conf); + oldest = 0; // Add up the stats in each directory. for (int dir = -1; dir <= 0xF; dir++) { char *fname; + time_t current; if (dir == -1) { fname = format("%s/stats", conf->cache_dir); @@ -276,7 +294,12 @@ stats_summary(struct conf *conf) fname = format("%s/%1x/stats", conf->cache_dir, dir); } + counters->data[STATS_ZEROTIMESTAMP] = 0; /* don't add */ stats_read(fname, counters); + current = (time_t) counters->data[STATS_ZEROTIMESTAMP]; + if (current != 0 && (oldest == 0 || current < oldest)) { + oldest = current; + } free(fname); } @@ -285,6 +308,10 @@ stats_summary(struct conf *conf) primary_config_path ? primary_config_path : ""); printf("secondary config (readonly) %s\n", secondary_config_path ? secondary_config_path : ""); + if (oldest) { + struct tm *tm = localtime(&oldest); + printf("stats zero time %s", asctime(tm)); + } // ...and display them. for (int i = 0; stats_info[i].message; i++) { @@ -354,6 +381,7 @@ stats_zero(void) counters->data[stats_info[i].stat] = 0; } } + stats_timestamp(time(NULL), counters); stats_write(fname, counters); lockfile_release(fname); }