#include "CacheFile.hpp"
#include "Config.hpp"
#include "Context.hpp"
+#include "Lockfile.hpp"
#include "Logging.hpp"
#include "Util.hpp"
#include "stats.hpp"
}
}
+static void
+update_counters(const std::string& dir,
+ uint64_t files_in_cache,
+ uint64_t cache_size,
+ bool cleanup_performed)
+{
+ const std::string stats_file = dir + "/stats";
+ Lockfile lock(stats_file);
+ if (lock.acquired()) {
+ Counters counters = Statistics::read(stats_file);
+ if (cleanup_performed) {
+ counters.increment(Statistic::cleanups_performed);
+ }
+ counters.set(Statistic::files_in_cache, files_in_cache);
+ counters.set(Statistic::cache_size_kibibyte, cache_size / 1024);
+ Statistics::write(stats_file, counters);
+ }
+}
+
void
clean_old(const Context& ctx,
const Util::ProgressReceiver& progress_receiver,
if (cleaned) {
log("Cleaned up cache directory {}", subdir);
- stats_add_cleanup(subdir, 1);
}
- stats_set_sizes(subdir, files_in_cache, cache_size);
+ update_counters(subdir, files_in_cache, cache_size, cleaned);
}
// Clean up all cache subdirectories.
progress_receiver(0.5 + 0.5 * i / files.size());
}
- if (!files.empty()) {
+ const bool cleared = !files.empty();
+ if (cleared) {
log("Cleared out cache directory {}", subdir);
- stats_add_cleanup(subdir, 1);
}
-
- stats_set_sizes(subdir, 0, 0);
+ update_counters(subdir, 0, 0, cleared);
}
// Wipe all cached files in all subdirectories.
}
}
-// Set the per-directory sizes.
-void
-stats_set_sizes(const std::string& dir, uint64_t num_files, uint64_t total_size)
-{
- std::string statsfile = dir + "/stats";
- Lockfile lock(statsfile);
- if (lock.acquired()) {
- Counters counters = Statistics::read(statsfile);
- counters.set(Statistic::files_in_cache, num_files);
- counters.set(Statistic::cache_size_kibibyte, total_size / 1024);
- Statistics::write(statsfile, counters);
- }
-}
-
-// Count directory cleanup run.
-void
-stats_add_cleanup(const std::string& dir, uint64_t count)
-{
- std::string statsfile = dir + "/stats";
- Counters updates;
- updates.increment(Statistic::cleanups_performed, count);
- Statistics::increment(statsfile, updates);
-}
-
optional<std::string>
stats_get_result(const Counters& counters)
{
void stats_summary(const Context& ctx);
void stats_print(const Config& config);
-void stats_set_sizes(const std::string& dir,
- uint64_t num_files,
- uint64_t total_size);
-void stats_add_cleanup(const std::string& dir, uint64_t count);
-
nonstd::optional<std::string> stats_get_result(const Counters& counters);