#include "Statistics.hpp"
#include "AtomicFile.hpp"
+#include "Lockfile.hpp"
#include "Logging.hpp"
#include "Util.hpp"
#include "exceptions.hpp"
}
}
+optional<Counters>
+increment(const std::string& path, const Counters& updates)
+{
+ Lockfile lock(path);
+ if (!lock.acquired()) {
+ return nullopt;
+ }
+ auto counters = Statistics::read(path);
+ counters.increment(updates);
+ Statistics::write(path, counters);
+ return counters;
+}
+
} // namespace Statistics
#include "Counters.hpp"
+#include "third_party/nonstd/optional.hpp"
+
#include <string>
// Statistics fields in storage order.
// Write `counters` to `path`. No lock is acquired.
void write(const std::string& path, const Counters& counters);
+// Acquire a lock, read counters from `path`, apply `updates`, write result to
+// `path` and release the lock. Returns the resulting counters or nullopt on
+// error (e.g. if the lock could not be acquired).
+nonstd::optional<Counters> increment(const std::string& path,
+ const Counters& updates);
+
} // namespace Statistics
ctx.counter_updates.increment(Statistic::cache_size_kibibyte, size_delta);
ctx.counter_updates.increment(Statistic::files_in_cache, files_delta);
} else {
- Counters counters;
- counters.increment(Statistic::cache_size_kibibyte, size_delta);
- counters.increment(Statistic::files_in_cache, files_delta);
- stats_flush_to_file(ctx.config, ctx.manifest_stats_file(), counters);
+ Counters updates;
+ updates.increment(Statistic::cache_size_kibibyte, size_delta);
+ updates.increment(Statistic::files_in_cache, files_delta);
+ Statistics::increment(ctx.manifest_stats_file(), updates);
}
}
MTR_END("manifest", "manifest_put");
if (ctx.stats_file() == stats_file) {
ctx.counter_updates.increment(Statistic::cache_size_kibibyte, size_delta);
} else {
- Counters counters;
- counters.increment(Statistic::cache_size_kibibyte, size_delta);
- stats_flush_to_file(ctx.config, stats_file, counters);
+ Counters updates;
+ updates.increment(Statistic::cache_size_kibibyte, size_delta);
+ Statistics::increment(stats_file, updates);
}
statistics.update(content_size, old_stat.size(), new_stat.size(), 0);
stats_add_cleanup(const std::string& dir, uint64_t count)
{
std::string statsfile = dir + "/stats";
- Lockfile lock(statsfile);
- if (lock.acquired()) {
- Counters counters = Statistics::read(statsfile);
- counters.increment(Statistic::cleanups_performed, count);
- Statistics::write(statsfile, counters);
- }
+ Counters updates;
+ updates.increment(Statistic::cleanups_performed, count);
+ Statistics::increment(statsfile, updates);
}
}
}
+TEST_CASE("Increment")
+{
+ TestContext test_context;
+
+ Util::write_file("test", "0 1 2 3 27 5\n");
+
+ Counters updates;
+ updates.set(Statistic::internal_error, 1);
+ updates.set(Statistic::cache_miss, 6);
+
+ Statistics::increment("test", updates);
+
+ Counters counters = Statistics::read("test");
+ CHECK(counters.get(Statistic::internal_error) == 4);
+ CHECK(counters.get(Statistic::cache_miss) == 33);
+}
+
TEST_SUITE_END();