From: Joel Rosdahl Date: Sun, 26 Jul 2020 14:38:57 +0000 (+0200) Subject: C++-ify stats functions X-Git-Tag: v4.0~290 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=00844179367b9f68fda9f7260b30094ee225b896;p=thirdparty%2Fccache.git C++-ify stats functions --- diff --git a/src/Context.cpp b/src/Context.cpp index 578997269..5ee6367d5 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -43,7 +43,7 @@ Context::Context() Context::~Context() { - stats_flush(this); + stats_flush(*this); unlink_pending_tmp_files(); // Dump log buffer last to not lose any logs. diff --git a/src/ccache.cpp b/src/ccache.cpp index 884b1830d..4e8df4fb6 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -1775,7 +1775,7 @@ create_initial_config_file(Config& config) uint64_t max_size; std::string stats_dir = fmt::format("{}/0", config.cache_dir()); if (Stat::stat(stats_dir)) { - stats_get_obsolete_limits(stats_dir.c_str(), &max_files, &max_size); + stats_get_obsolete_limits(stats_dir, &max_files, &max_size); // STATS_MAXFILES and STATS_MAXSIZE was stored for each top directory. max_files *= 16; max_size *= 16; diff --git a/src/cleanup.cpp b/src/cleanup.cpp index 1e41cde11..4b83ae1e6 100644 --- a/src/cleanup.cpp +++ b/src/cleanup.cpp @@ -147,10 +147,10 @@ clean_up_dir(const std::string& subdir, if (cleaned) { cc_log("Cleaned up cache directory %s", subdir.c_str()); - stats_add_cleanup(subdir.c_str(), 1); + stats_add_cleanup(subdir, 1); } - stats_set_sizes(subdir.c_str(), files_in_cache, cache_size); + stats_set_sizes(subdir, files_in_cache, cache_size); } // Clean up all cache subdirectories. @@ -189,10 +189,10 @@ wipe_dir(const std::string& subdir, if (!files.empty()) { cc_log("Cleared out cache directory %s", subdir.c_str()); - stats_add_cleanup(subdir.c_str(), 1); + stats_add_cleanup(subdir, 1); } - stats_set_sizes(subdir.c_str(), 0, 0); + stats_set_sizes(subdir, 0, 0); } // Wipe all cached files in all subdirectories. diff --git a/src/stats.cpp b/src/stats.cpp index 7fad4f164..06cca020d 100644 --- a/src/stats.cpp +++ b/src/stats.cpp @@ -40,10 +40,10 @@ // Returns a formatted version of a statistics value, or NULL if the statistics // line shouldn't be printed. Caller frees. -using format_fn = char* (*)(uint64_t value); +using format_fn = std::string (*)(uint64_t value); -static char* format_size_times_1024(uint64_t size); -static char* format_timestamp(uint64_t timestamp); +static std::string format_size_times_1024(uint64_t size); +static std::string format_timestamp(uint64_t timestamp); // Statistics fields in display order. static struct @@ -51,7 +51,7 @@ static struct enum stats stat; const char* id; // for --print-stats const char* message; // for --show-stats - format_fn format; // NULL -> use plain integer format + format_fn format; // nullptr -> use plain integer format unsigned flags; } stats_info[] = { {STATS_ZEROTIMESTAMP, @@ -176,21 +176,19 @@ static struct FLAG_NOZERO | FLAG_NEVER}, {STATS_NONE, nullptr, nullptr, nullptr, 0}}; -static char* +static std::string format_size(uint64_t size) { - std::string result = - fmt::format("{:>11}", Util::format_human_readable_size(size)); - return x_strdup(result.c_str()); + return fmt::format("{:>11}", Util::format_human_readable_size(size)); } -static char* +static std::string format_size_times_1024(uint64_t size) { return format_size(size * 1024); } -static char* +static std::string format_timestamp(uint64_t timestamp) { if (timestamp > 0) { @@ -200,16 +198,16 @@ format_timestamp(uint64_t timestamp) strftime(buffer, sizeof(buffer), "%c", &tm); return format(" %s", buffer); } else { - return nullptr; + return {}; } } // Parse a stats file from a buffer, adding to the counters. static void -parse_stats(Counters& counters, const char* buf) +parse_stats(Counters& counters, const std::string& buf) { size_t i = 0; - const char* p = buf; + const char* p = buf.c_str(); while (true) { char* p2; long val = strtol(p, &p2, 10); @@ -259,12 +257,12 @@ stats_collect(const Config& config, Counters& counters, time_t* last_updated) // Add up the stats in each directory. for (int dir = -1; dir <= 0xF; dir++) { - char* fname; + std::string fname; if (dir == -1) { - fname = format("%s/stats", config.cache_dir().c_str()); + fname = config.cache_dir() + "/stats"; } else { - fname = format("%s/%1x/stats", config.cache_dir().c_str(), dir); + fname = fmt::format("{}/{:x}/stats", config.cache_dir(), dir); } counters[STATS_ZEROTIMESTAMP] = 0; // Don't add @@ -274,7 +272,6 @@ stats_collect(const Config& config, Counters& counters, time_t* last_updated) if (st && st.mtime() > *last_updated) { *last_updated = st.mtime(); } - free(fname); } counters[STATS_ZEROTIMESTAMP] = zero_timestamp; @@ -299,7 +296,7 @@ stats_read(const std::string& path, Counters& counters) { try { std::string data = Util::read_file(path); - parse_stats(counters, data.c_str()); + parse_stats(counters, data); } catch (Error&) { // Ignore. } @@ -308,7 +305,7 @@ stats_read(const std::string& path, Counters& counters) // Write counter updates in updates to sfile. void stats_flush_to_file(const Config& config, - std::string sfile, + const std::string& sfile, const Counters& updates) { if (updates.all_zero()) { @@ -380,9 +377,8 @@ stats_flush_to_file(const Config& config, // Write counter updates in counter_updates to disk. void -stats_flush(void* context) +stats_flush(Context& ctx) { - const Context& ctx = *static_cast(context); stats_flush_to_file(ctx.config, ctx.stats_file(), ctx.counter_updates); } @@ -413,7 +409,7 @@ stats_summary(const Context& ctx) localtime_r(&last_updated, &tm); char timestamp[100]; strftime(timestamp, sizeof(timestamp), "%c", &tm); - printf("stats updated %s\n", timestamp); + fmt::print("stats updated {}\n", timestamp); } // ...and display them. @@ -427,30 +423,29 @@ stats_summary(const Context& ctx) continue; } - char* value; + std::string value; if (stats_info[i].format) { value = stats_info[i].format(counters[stat]); } else { - value = format("%8u", counters[stat]); + value = fmt::format("{:8}", counters[stat]); } - if (value) { - printf("%-31s %s\n", stats_info[i].message, value); - free(value); + if (!value.empty()) { + fmt::print("{:31} {}\n", stats_info[i].message, value); } if (stat == STATS_CACHEMISS) { double percent = stats_hit_rate(counters); - printf("cache hit rate %6.2f %%\n", percent); + fmt::print("cache hit rate {:6.2f} %\n", percent); } } if (ctx.config.max_files() != 0) { - printf("max files %8u\n", ctx.config.max_files()); + fmt::print("max files {:8}\n", + ctx.config.max_files()); } if (ctx.config.max_size() != 0) { - char* value = format_size(ctx.config.max_size()); - printf("max cache size %s\n", value); - free(value); + fmt::print("max cache size {}\n", + format_size(ctx.config.max_size())); } } @@ -462,11 +457,11 @@ stats_print(const Config& config) time_t last_updated; stats_collect(config, counters, &last_updated); - printf("stats_updated_timestamp\t%llu\n", (unsigned long long)last_updated); + fmt::print("stats_updated_timestamp\t{}\n", last_updated); for (int i = 0; stats_info[i].message; i++) { if (!(stats_info[i].flags & FLAG_NEVER)) { - printf("%s\t%u\n", stats_info[i].id, counters[stats_info[i].stat]); + fmt::print("{}\t{}\n", stats_info[i].id, counters[stats_info[i].stat]); } } } @@ -475,18 +470,16 @@ stats_print(const Config& config) void stats_zero(const Context& ctx) { - char* fname = format("%s/stats", ctx.config.cache_dir().c_str()); - Util::unlink_safe(fname); - free(fname); + // Remove old legacy stats file at cache top directory. + Util::unlink_safe(ctx.config.cache_dir() + "/stats"); time_t timestamp = time(nullptr); for (int dir = 0; dir <= 0xF; dir++) { Counters counters; - fname = format("%s/%1x/stats", ctx.config.cache_dir().c_str(), dir); + auto fname = fmt::format("{}/{:x}/stats", ctx.config.cache_dir(), dir); if (!Stat::stat(fname)) { // No point in trying to reset the stats file if it doesn't exist. - free(fname); continue; } Lockfile lock(fname); @@ -500,30 +493,31 @@ stats_zero(const Context& ctx) counters[STATS_ZEROTIMESTAMP] = timestamp; stats_write(fname, counters); } - free(fname); } } // Get the per-directory limits. void -stats_get_obsolete_limits(const char* dir, +stats_get_obsolete_limits(const std::string& dir, unsigned* maxfiles, uint64_t* maxsize) { + assert(maxfiles); + assert(maxsize); + Counters counters; - char* sname = format("%s/stats", dir); + std::string sname = dir + "/stats"; stats_read(sname, counters); *maxfiles = counters[STATS_OBSOLETE_MAXFILES]; - *maxsize = (uint64_t)counters[STATS_OBSOLETE_MAXSIZE] * 1024; - free(sname); + *maxsize = static_cast(counters[STATS_OBSOLETE_MAXSIZE]) * 1024; } // Set the per-directory sizes. void -stats_set_sizes(const char* dir, unsigned num_files, uint64_t total_size) +stats_set_sizes(const std::string& dir, unsigned num_files, uint64_t total_size) { Counters counters; - char* statsfile = format("%s/stats", dir); + std::string statsfile = dir + "/stats"; Lockfile lock(statsfile); if (lock.acquired()) { stats_read(statsfile, counters); @@ -531,20 +525,18 @@ stats_set_sizes(const char* dir, unsigned num_files, uint64_t total_size) counters[STATS_TOTALSIZE] = total_size / 1024; stats_write(statsfile, counters); } - free(statsfile); } // Count directory cleanup run. void -stats_add_cleanup(const char* dir, unsigned count) +stats_add_cleanup(const std::string& dir, unsigned count) { Counters counters; - char* statsfile = format("%s/stats", dir); + std::string statsfile = dir + "/stats"; Lockfile lock(statsfile); if (lock.acquired()) { stats_read(statsfile, counters); counters[STATS_NUMCLEANUPS] += count; stats_write(statsfile, counters); } - free(statsfile); } diff --git a/src/stats.hpp b/src/stats.hpp index b67d20a65..b0d5b37b3 100644 --- a/src/stats.hpp +++ b/src/stats.hpp @@ -67,19 +67,21 @@ enum stats { }; void stats_update(Context& ctx, enum stats stat); -void stats_flush(void* context); +void stats_flush(Context& ctx); void stats_flush_to_file(const Config& config, - std::string sfile, + const std::string& sfile, const Counters& updates); void stats_zero(const Context& ctx); void stats_summary(const Context& ctx); void stats_print(const Config& config); void stats_update_size(Counters& counters, int64_t size, int files); -void stats_get_obsolete_limits(const char* dir, +void stats_get_obsolete_limits(const std::string& 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_set_sizes(const std::string& dir, + unsigned num_files, + uint64_t total_size); +void stats_add_cleanup(const std::string& dir, unsigned count); void stats_read(const std::string& path, Counters& counters); void stats_write(const std::string& path, const Counters& counters);