From: Joel Rosdahl Date: Sun, 13 Jun 2021 18:41:32 +0000 (+0200) Subject: Refactor away Result::Writer knowledge about counter_updates X-Git-Tag: v4.4~208 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=76e4435f5daa9d0197577a99ba4863925bde0ef3;p=thirdparty%2Fccache.git Refactor away Result::Writer knowledge about counter_updates --- diff --git a/src/Result.cpp b/src/Result.cpp index 8408d2a7b..7a6701409 100644 --- a/src/Result.cpp +++ b/src/Result.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2020 Joel Rosdahl and other contributors +// Copyright (C) 2019-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -196,6 +196,14 @@ gcno_file_in_unmangled_form(const Context& ctx) return Util::change_extension(ctx.args_info.output_obj, ".gcno"); } +FileSizeAndCountDiff& +FileSizeAndCountDiff::operator+=(const FileSizeAndCountDiff& other) +{ + size_kibibyte += other.size_kibibyte; + count += other.count; + return *this; +} + Result::Reader::Reader(const std::string& result_path) : m_result_path(result_path) { @@ -311,20 +319,20 @@ Writer::write(FileType file_type, const std::string& file_path) m_entries_to_write.emplace_back(file_type, file_path); } -optional +nonstd::expected Writer::finalize() { try { - do_finalize(); - return nullopt; + return do_finalize(); } catch (const Error& e) { - return e.what(); + return nonstd::make_unexpected(e.what()); } } -void +FileSizeAndCountDiff Writer::do_finalize() { + FileSizeAndCountDiff file_size_and_count_diff{0, 0}; uint64_t payload_size = 0; payload_size += 1; // n_entries for (const auto& pair : m_entries_to_write) { @@ -369,7 +377,7 @@ Writer::do_finalize() writer.write(file_size); if (store_raw) { - write_raw_file_entry(path, entry_number); + file_size_and_count_diff += write_raw_file_entry(path, entry_number); } else { write_embedded_file_entry(writer, path, file_size); } @@ -379,6 +387,8 @@ Writer::do_finalize() writer.finalize(); atomic_result_file.commit(); + + return file_size_and_count_diff; } void @@ -410,7 +420,7 @@ Result::Writer::write_embedded_file_entry(CacheEntryWriter& writer, } } -void +FileSizeAndCountDiff Result::Writer::write_raw_file_entry(const std::string& path, uint32_t entry_number) { @@ -423,11 +433,10 @@ 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); - 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)); + return { + Util::size_change_kibibyte(old_stat, new_stat), + (new_stat ? 1 : 0) - (old_stat ? 1 : 0), + }; } } // namespace Result diff --git a/src/Result.hpp b/src/Result.hpp index 01068c9e6..7fcfc95b6 100644 --- a/src/Result.hpp +++ b/src/Result.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2020 Joel Rosdahl and other contributors +// Copyright (C) 2019-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -20,6 +20,7 @@ #include "system.hpp" +#include "third_party/nonstd/expected.hpp" #include "third_party/nonstd/optional.hpp" #include @@ -79,6 +80,14 @@ const char* file_type_to_string(FileType type); std::string gcno_file_in_mangled_form(const Context& ctx); std::string gcno_file_in_unmangled_form(const Context& ctx); +struct FileSizeAndCountDiff +{ + int64_t size_kibibyte; + int64_t count; + + FileSizeAndCountDiff& operator+=(const FileSizeAndCountDiff& other); +}; + // This class knows how to read a result cache entry. class Reader { @@ -121,18 +130,19 @@ public: void write(FileType file_type, const std::string& file_path); // Write registered files to the result. Returns an error message on error. - nonstd::optional finalize(); + nonstd::expected finalize(); private: Context& m_ctx; const std::string m_result_path; std::vector> m_entries_to_write; - void do_finalize(); + FileSizeAndCountDiff do_finalize(); static void write_embedded_file_entry(CacheEntryWriter& writer, const std::string& path, uint64_t file_size); - void write_raw_file_entry(const std::string& path, uint32_t entry_number); + FileSizeAndCountDiff write_raw_file_entry(const std::string& path, + uint32_t entry_number); }; } // namespace Result diff --git a/src/ccache.cpp b/src/ccache.cpp index 9be789b9b..87e4f94e7 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -1134,11 +1134,15 @@ to_cache(Context& ctx, ctx.args_info.output_dwo); } - auto error = result_writer.finalize(); - if (error) { - LOG("Error: {}", *error); - } else { + const auto file_size_and_count_diff = result_writer.finalize(); + if (file_size_and_count_diff) { LOG("Stored in cache: {}", result_file.path); + ctx.counter_updates.increment(Statistic::cache_size_kibibyte, + file_size_and_count_diff->size_kibibyte); + ctx.counter_updates.increment(Statistic::files_in_cache, + file_size_and_count_diff->count); + } else { + LOG("Error: {}", file_size_and_count_diff.error()); } auto new_result_stat = Stat::stat(result_file.path, Stat::OnError::log);