-// Copyright (C) 2010-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2010-2023 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// Return true if all counters are zero, false otherwise.
bool all_zero() const;
+ bool operator==(const StatisticsCounters& other) const noexcept;
+ bool operator!=(const StatisticsCounters& other) const noexcept;
+
private:
std::vector<uint64_t> m_counters;
};
+inline bool
+StatisticsCounters::operator==(const StatisticsCounters& other) const noexcept
+{
+ return m_counters == other.m_counters;
+}
+
+inline bool
+StatisticsCounters::operator!=(const StatisticsCounters& other) const noexcept
+{
+ return !(*this == other);
+}
+
} // namespace core
std::optional<core::StatisticsCounters>
StatsFile::update(
- std::function<void(core::StatisticsCounters& counters)> function) const
+ std::function<void(core::StatisticsCounters& counters)> function,
+ OnlyIfChanged only_if_changed) const
{
util::LockFile lock(m_path);
if (!lock.acquire()) {
}
auto counters = read();
+ const auto orig_counters = counters;
function(counters);
-
- core::AtomicFile file(m_path, core::AtomicFile::Mode::text);
- for (size_t i = 0; i < counters.size(); ++i) {
- file.write(FMT("{}\n", counters.get_raw(i)));
- }
- try {
- file.commit();
- } catch (const core::Error& e) {
- // Make failure to write a stats file a soft error since it's not important
- // enough to fail whole the process and also because it is called in the
- // Context destructor.
- LOG("Error: {}", e.what());
+ if (only_if_changed == OnlyIfChanged::no || counters != orig_counters) {
+ core::AtomicFile file(m_path, core::AtomicFile::Mode::text);
+ for (size_t i = 0; i < counters.size(); ++i) {
+ file.write(FMT("{}\n", counters.get_raw(i)));
+ }
+ try {
+ file.commit();
+ } catch (const core::Error& e) {
+ // Make failure to write a stats file a soft error since it's not
+ // important enough to fail whole the process and also because it is
+ // called in the Context destructor.
+ LOG("Error: {}", e.what());
+ }
}
return counters;
-// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// counters will be zero.
core::StatisticsCounters read() const;
+ enum class OnlyIfChanged { no, yes };
+
// Acquire a lock, read counters, call `function` with the counters, write the
// counters and release the lock. Returns the resulting counters or nullopt on
// error (e.g. if the lock could not be acquired).
std::optional<core::StatisticsCounters>
- update(std::function<void(core::StatisticsCounters& counters)>) const;
+ update(std::function<void(core::StatisticsCounters& counters)>,
+ OnlyIfChanged only_if_changed = OnlyIfChanged::no) const;
private:
std::string m_path;