From d83f3ffb9ddde866f217aab838adc25aa184402c Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Thu, 10 Sep 2020 14:33:45 +0200 Subject: [PATCH] Replace stats_update_size with incrementing counters directly --- src/Result.cpp | 14 ++++++++------ src/ccache.cpp | 21 ++++++++++++--------- src/compress.cpp | 7 ++++--- src/stats.cpp | 13 ------------- src/stats.hpp | 1 - 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/Result.cpp b/src/Result.cpp index a95c67d03..ca3c06689 100644 --- a/src/Result.cpp +++ b/src/Result.cpp @@ -399,18 +399,20 @@ void Result::Writer::write_raw_file_entry(const std::string& path, uint32_t entry_number) { - auto raw_file = get_raw_file_path(m_result_path, entry_number); - auto old_stat = Stat::stat(raw_file); + const auto raw_file = get_raw_file_path(m_result_path, entry_number); + const auto old_stat = Stat::stat(raw_file); try { Util::clone_hard_link_or_copy_file(m_ctx, path, raw_file, true); } catch (Error& e) { throw Error( "Failed to store {} as raw file {}: {}", path, raw_file, e.what()); } - auto new_stat = Stat::stat(raw_file); - stats_update_size(m_ctx.counter_updates, - new_stat.size_on_disk() - old_stat.size_on_disk(), - (new_stat ? 1 : 0) - (old_stat ? 1 : 0)); + 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[Statistic::cache_size_kibibyte] += size_delta; + m_ctx.counter_updates[Statistic::files_in_cache] += files_delta; } } // namespace Result diff --git a/src/ccache.cpp b/src/ccache.cpp index d6370504e..4ef4a1c7d 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -750,16 +750,19 @@ update_manifest_file(Context& ctx) save_timestamp)) { log("Failed to add result name to {}", ctx.manifest_path()); } else { - auto st = Stat::stat(ctx.manifest_path(), Stat::OnError::log); + const auto st = Stat::stat(ctx.manifest_path(), Stat::OnError::log); - int64_t size_delta = st.size_on_disk() - old_st.size_on_disk(); - int nof_files_delta = !old_st && st ? 1 : 0; + const int64_t size_delta = + (st.size_on_disk() - old_st.size_on_disk()) / 1024; + const int64_t files_delta = !old_st && st ? 1 : 0; if (ctx.stats_file() == ctx.manifest_stats_file()) { - stats_update_size(ctx.counter_updates, size_delta, nof_files_delta); + ctx.counter_updates[Statistic::cache_size_kibibyte] += size_delta; + ctx.counter_updates[Statistic::files_in_cache] += files_delta; } else { Counters counters; - stats_update_size(counters, size_delta, nof_files_delta); + counters[Statistic::cache_size_kibibyte] += size_delta; + counters[Statistic::files_in_cache] += files_delta; stats_flush_to_file(ctx.config, ctx.manifest_stats_file(), counters); } } @@ -972,10 +975,10 @@ to_cache(Context& ctx, if (!new_dest_stat) { throw Failure(Statistic::internal_error); } - stats_update_size(ctx.counter_updates, - new_dest_stat.size_on_disk() - - orig_dest_stat.size_on_disk(), - orig_dest_stat ? 0 : 1); + const auto size_delta = + (new_dest_stat.size_on_disk() - orig_dest_stat.size_on_disk()) / 1024; + ctx.counter_updates[Statistic::cache_size_kibibyte] += size_delta; + ctx.counter_updates[Statistic::files_in_cache] += orig_dest_stat ? 0 : 1; MTR_END("file", "file_put"); diff --git a/src/compress.cpp b/src/compress.cpp index 3f3fd28f6..5ed62aee2 100644 --- a/src/compress.cpp +++ b/src/compress.cpp @@ -198,12 +198,13 @@ recompress_file(Context& ctx, atomic_new_file.commit(); auto new_stat = Stat::stat(cache_file.path(), Stat::OnError::log); - size_t size_on_disk_delta = new_stat.size_on_disk() - old_stat.size_on_disk(); + const size_t size_delta = + (new_stat.size_on_disk() - old_stat.size_on_disk()) / 1024; if (ctx.stats_file() == stats_file) { - stats_update_size(ctx.counter_updates, size_on_disk_delta, 0); + ctx.counter_updates[Statistic::cache_size_kibibyte] += size_delta; } else { Counters counters; - stats_update_size(counters, size_on_disk_delta, 0); + counters[Statistic::cache_size_kibibyte] += size_delta; stats_flush_to_file(ctx.config, stats_file, counters); } diff --git a/src/stats.cpp b/src/stats.cpp index c25ebf934..53aa5438c 100644 --- a/src/stats.cpp +++ b/src/stats.cpp @@ -226,19 +226,6 @@ stats_collect(const Config& config, Counters& counters, time_t* last_updated) counters[Statistic::stats_zeroed_timestamp] = zero_timestamp; } -// Record that a number of bytes and files have been added to the cache. Size -// is in bytes. -void -stats_update_size(Counters& counters, int64_t size, int files) -{ - if (size == 0 && files == 0) { - return; - } - - counters[Statistic::cache_size_kibibyte] += size / 1024; - counters[Statistic::files_in_cache] += files; -} - // Read in the stats from one directory and add to the counters. void stats_read(const std::string& path, Counters& counters) diff --git a/src/stats.hpp b/src/stats.hpp index b970440a9..67948db52 100644 --- a/src/stats.hpp +++ b/src/stats.hpp @@ -74,7 +74,6 @@ 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 std::string& dir, uint64_t* maxfiles, uint64_t* maxsize); -- 2.47.3