From: Joel Rosdahl Date: Wed, 16 Sep 2020 14:44:45 +0000 (+0200) Subject: Fix and simplify calculation of cache size change X-Git-Tag: v4.0~89 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9d2ee9b866c101a14137b91de0c0cff92f81dd2f;p=thirdparty%2Fccache.git Fix and simplify calculation of cache size change --- diff --git a/src/Result.cpp b/src/Result.cpp index cbad746e1..7c1af27f6 100644 --- a/src/Result.cpp +++ b/src/Result.cpp @@ -408,11 +408,11 @@ Result::Writer::write_raw_file_entry(const std::string& path, "Failed to store {} as raw file {}: {}", path, raw_file, e.what()); } const auto new_stat = Stat::stat(raw_file); - const auto size_delta = - (new_stat.size_on_disk() - old_stat.size_on_disk()) / 1024; - const auto files_delta = (new_stat ? 1 : 0) - (old_stat ? 1 : 0); - m_ctx.counter_updates.increment(Statistic::cache_size_kibibyte, size_delta); - m_ctx.counter_updates.increment(Statistic::files_in_cache, files_delta); + m_ctx.counter_updates.increment( + Statistic::cache_size_kibibyte, + Util::size_change_kibibyte(old_stat, new_stat)); + m_ctx.counter_updates.increment(Statistic::files_in_cache, + (new_stat ? 1 : 0) - (old_stat ? 1 : 0)); } } // namespace Result diff --git a/src/Util.hpp b/src/Util.hpp index f694f3f1a..e14e7f087 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -390,6 +390,15 @@ void set_cloexec_flag(int fd); // Set environment variable `name` to `value`. void setenv(const std::string& name, const std::string& value); +// Return size change in KiB between `old_stat` and `new_stat`. +inline int64_t +size_change_kibibyte(const Stat& old_stat, const Stat& new_stat) +{ + return (static_cast(new_stat.size_on_disk()) + - static_cast(old_stat.size_on_disk())) + / 1024; +} + // Split `input` into words at any of the characters listed in `separators`. // These words are a view into `input`; empty words are omitted. `separators` // must neither be the empty string nor a nullptr. diff --git a/src/ccache.cpp b/src/ccache.cpp index c66c9b50d..bc8778a6d 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -753,9 +753,7 @@ update_manifest_file(Context& ctx) log("Failed to add result name to {}", ctx.manifest_path()); } else { const auto st = Stat::stat(ctx.manifest_path(), Stat::OnError::log); - - const int64_t size_delta_kibibyte = - (st.size_on_disk() - old_st.size_on_disk()) / 1024; + const int64_t size_delta_kibibyte = Util::size_change_kibibyte(old_st, st); const int64_t files_delta = !old_st && st ? 1 : 0; if (ctx.stats_file() == ctx.manifest_stats_file()) { @@ -972,9 +970,9 @@ to_cache(Context& ctx, if (!new_dest_stat) { throw Failure(Statistic::internal_error); } - const auto size_delta = - (new_dest_stat.size_on_disk() - orig_dest_stat.size_on_disk()) / 1024; - ctx.counter_updates.increment(Statistic::cache_size_kibibyte, size_delta); + ctx.counter_updates.increment( + Statistic::cache_size_kibibyte, + Util::size_change_kibibyte(orig_dest_stat, new_dest_stat)); ctx.counter_updates.increment(Statistic::files_in_cache, orig_dest_stat ? 0 : 1); diff --git a/src/compress.cpp b/src/compress.cpp index b73dfae0c..bf7816619 100644 --- a/src/compress.cpp +++ b/src/compress.cpp @@ -197,10 +197,9 @@ recompress_file(RecompressionStatistics& statistics, atomic_new_file.commit(); auto new_stat = Stat::stat(cache_file.path(), Stat::OnError::log); - const uint64_t size_delta = - (new_stat.size_on_disk() - old_stat.size_on_disk()) / 1024; Statistics::update(stats_file, [=](Counters& cs) { - cs.increment(Statistic::cache_size_kibibyte, size_delta); + cs.increment(Statistic::cache_size_kibibyte, + Util::size_change_kibibyte(old_stat, new_stat)); }); statistics.update(content_size, old_stat.size(), new_stat.size(), 0);