-// 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.
//
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)
{
m_entries_to_write.emplace_back(file_type, file_path);
}
-optional<std::string>
+nonstd::expected<FileSizeAndCountDiff, std::string>
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) {
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);
}
writer.finalize();
atomic_result_file.commit();
+
+ return file_size_and_count_diff;
}
void
}
}
-void
+FileSizeAndCountDiff
Result::Writer::write_raw_file_entry(const std::string& path,
uint32_t entry_number)
{
"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
-// 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.
//
#include "system.hpp"
+#include "third_party/nonstd/expected.hpp"
#include "third_party/nonstd/optional.hpp"
#include <map>
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
{
void write(FileType file_type, const std::string& file_path);
// Write registered files to the result. Returns an error message on error.
- nonstd::optional<std::string> finalize();
+ nonstd::expected<FileSizeAndCountDiff, std::string> finalize();
private:
Context& m_ctx;
const std::string m_result_path;
std::vector<std::pair<FileType, std::string>> 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
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);