// Update a normal stat. void stats_update(enum stats stat) { assert(stat > STATS_NONE && stat < STATS_END); init_counter_updates(); counter_updates->data[stat]++; }
// Record that a number of bytes and files have been added to the cache. Size // is in bytes. void stats_update_size(int64_t size, int files) { init_counter_updates(); counter_updates->data[STATS_NUMFILES] += files; counter_updates->data[STATS_TOTALSIZE] += size / 1024; }
/* * 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) { init_counter_updates(); if (stat != STATS_NONE) { counter_updates->data[stat]++; } counter_updates->data[STATS_NUMFILES] += files; counter_updates->data[STATS_TOTALSIZE] += size; }
// Record that a number of bytes and files have been added to the cache. Size // is in bytes. void stats_update_size(const char *sfile, int64_t size, int files) { struct counters *updates; if (sfile == stats_file) { init_counter_updates(); updates = counter_updates; } else { updates = counters_init(STATS_END); } updates->data[STATS_NUMFILES] += files; updates->data[STATS_TOTALSIZE] += size / 1024; if (sfile != stats_file) { stats_flush_to_file(sfile, updates); counters_free(updates); } }
// Get the pending update of a counter value. unsigned stats_get_pending(enum stats stat) { init_counter_updates(); return counter_updates->data[stat]; }
/* * Write counter updates in counter_updates to disk. */ void stats_flush(void) { struct counters *counters; bool need_cleanup = false; bool should_flush = false; int i; extern char *cache_logfile; if (getenv("CCACHE_NOSTATS")) return; init_counter_updates(); for (i = 0; i < STATS_END; ++i) { if (counter_updates->data[i] > 0) { should_flush = true; break; } } if (!should_flush) return; if (!stats_file) { char *stats_dir; /* * A NULL stats_file means that we didn't get past calculate_object_hash(), * so we just choose one of stats files in the 16 subdirectories. */ if (!cache_dir) return; stats_dir = format("%s/%x", cache_dir, hash_from_int(getpid()) % 16); stats_file = format("%s/stats", stats_dir); create_dir(stats_dir); free(stats_dir); } if (!lockfile_acquire(stats_file, lock_staleness_limit)) { return; } counters = counters_init(STATS_END); stats_read(stats_file, counters); for (i = 0; i < STATS_END; ++i) { counters->data[i] += counter_updates->data[i]; } stats_write(stats_file, counters); lockfile_release(stats_file); if (cache_logfile) { for (i = 0; i < STATS_END; ++i) { if (counter_updates->data[stats_info[i].stat] != 0 && !(stats_info[i].flags & FLAG_NOZERO)) { cc_log("Result: %s", stats_info[i].message); } } } if (counters->data[STATS_MAXFILES] != 0 && counters->data[STATS_NUMFILES] > counters->data[STATS_MAXFILES]) { need_cleanup = true; } if (counters->data[STATS_MAXSIZE] != 0 && counters->data[STATS_TOTALSIZE] > counters->data[STATS_MAXSIZE]) { need_cleanup = true; } if (need_cleanup) { char *p = dirname(stats_file); cleanup_dir(p, counters->data[STATS_MAXFILES], counters->data[STATS_MAXSIZE]); free(p); } }