]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactored the stats code a little
authorAndrew Tridgell <tridge@samba.org>
Mon, 1 Apr 2002 03:08:48 +0000 (05:08 +0200)
committerAndrew Tridgell <tridge@samba.org>
Mon, 1 Apr 2002 03:08:48 +0000 (05:08 +0200)
ccache.h
stats.c

index 0a440baab80b4dae950650fca3e916849a609cc4..9afd9d27f2b9bf3dd9f3826353f1e2177ccda459 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -1,4 +1,4 @@
-#define CCACHE_VERSION "1.2"
+#define CCACHE_VERSION "1.3"
 
 #include "config.h"
 
diff --git a/stats.c b/stats.c
index 2334174e0cc83528efaefcb59dffa43d2f49d5f0..ea75fc158158ef4048e0ede9d08e13a90d429dab 100644 (file)
--- a/stats.c
+++ b/stats.c
@@ -57,18 +57,6 @@ static struct {
        { STATS_NONE, NULL, NULL, 0 }
 };
 
-/* return a string description of a statistic */
-static int stats_find(enum stats stat)
-{
-       int i;
-       for (i=0;stats_info[i].stat != STATS_NONE; i++) {
-               if (stats_info[i].stat == stat) {
-                       return i;
-               }
-       }
-       return -1;
-}
-
 /* parse a stats file from a buffer - adding to the counters */
 static void parse_stats(unsigned counters[STATS_END], char *buf)
 {
@@ -133,10 +121,12 @@ static void stats_update_size(enum stats stat, size_t size)
 
        /* still can't get it? don't bother ... */
        if (fd == -1) return;
+
+       memset(counters, 0, sizeof(counters));
+
        if (lock_fd(fd) != 0) return;
 
        /* read in the old stats */
-       memset(counters, 0, sizeof(counters));
        stats_read_fd(fd, counters);
 
        /* update them */
@@ -146,22 +136,22 @@ static void stats_update_size(enum stats stat, size_t size)
        if (stat == STATS_TOCACHE) {
                counters[STATS_NUMFILES] += 2;
                counters[STATS_TOTALSIZE] += size;
-
-               /* 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;
-               }
-               if (counters[STATS_MAXSIZE] != 0 &&
-                   counters[STATS_TOTALSIZE] > counters[STATS_MAXSIZE]) {
-                       need_cleanup = 1;
-               }
        }
 
        /* 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;
+       }
+       if (counters[STATS_MAXSIZE] != 0 &&
+           counters[STATS_TOTALSIZE] > counters[STATS_MAXSIZE]) {
+               need_cleanup = 1;
+       }
+
        if (need_cleanup) {
                char *p = dirname(stats_file);
                cleanup_dir(p, counters[STATS_MAXFILES], counters[STATS_MAXSIZE]);
@@ -253,13 +243,12 @@ void stats_zero(void)
                        free(fname);
                        continue;
                }
-               lock_fd(fd);
                memset(counters, 0, sizeof(counters));
+               lock_fd(fd);
                stats_read_fd(fd, counters);
-               for (i=0;i<=STATS_END;i++) {
-                       int n = stats_find(i);
-                       if (n != -1 && (!stats_info[n].flags & FLAG_NOZERO)) {
-                               counters[i] = 0;
+               for (i=0;stats_info[i].message;i++) {
+                       if (!(stats_info[i].flags & FLAG_NOZERO)) {
+                               counters[stats_info[i].stat] = 0;
                        }
                }
                write_stats(fd, counters);