]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Show the time when stats were last zeroed out 140/head
authorAnders Björklund <anders@itension.se>
Wed, 20 Jul 2016 08:26:25 +0000 (10:26 +0200)
committerAnders Björklund <anders@psqr.se>
Sat, 22 Oct 2016 19:26:50 +0000 (21:26 +0200)
Closes #97.

ccache.h
stats.c

index 7b29bb8cd65db0193d420bfbd609e156b03198aa..ee1f1f445f0dd3ca0888b81d7d4ad11ec4e70e28 100644 (file)
--- 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 6eff3e1ec5992e74206ca2386c10c17cf11fac1b..110020029da5fe13b30414d221f722eafc329f29 100644 (file)
--- 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);
                }