]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add and use Statistics::increment function
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 11 Sep 2020 14:50:26 +0000 (16:50 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 14 Sep 2020 17:40:06 +0000 (19:40 +0200)
src/Statistics.cpp
src/Statistics.hpp
src/ccache.cpp
src/compress.cpp
src/stats.cpp
unittest/test_Statistics.cpp

index 25de0ec02377882a472bcb60e0c046a3a78e68ea..6b268ffb251d8e1600b18b8cdc1a6c424b582360 100644 (file)
@@ -19,6 +19,7 @@
 #include "Statistics.hpp"
 
 #include "AtomicFile.hpp"
+#include "Lockfile.hpp"
 #include "Logging.hpp"
 #include "Util.hpp"
 #include "exceptions.hpp"
@@ -75,4 +76,17 @@ write(const std::string& path, const Counters& counters)
   }
 }
 
+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
index b517db120e9b65ea6e18f7c14881755017969b98..0008da0f10343bb82d01dfd5174ac585d5d7d445 100644 (file)
@@ -22,6 +22,8 @@
 
 #include "Counters.hpp"
 
+#include "third_party/nonstd/optional.hpp"
+
 #include <string>
 
 // Statistics fields in storage order.
@@ -71,4 +73,10 @@ Counters read(const std::string& path);
 // 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
index 2f6b2d7ced3999d82dbf435643e9119c4a875e8e..7d3b399966d9770f6399c3baf61ca69901e64662 100644 (file)
@@ -760,10 +760,10 @@ update_manifest_file(Context& ctx)
       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");
index 2d583631eb1bb8e129640f853b7b777c1a71fabb..5bed4affd64d851e84585a26f1a1c92c4d2938d1 100644 (file)
@@ -204,9 +204,9 @@ recompress_file(Context& ctx,
   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);
index 29513dad55582c5631f6b7ebbb50b46a788d7660..392653523043d4a794bedc3c944f6a7a91be3031 100644 (file)
@@ -411,10 +411,7 @@ void
 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);
 }
index a680ee20a8047660e2c97286a755a18f67ddde21..64cd1dca6e8bdecc9f44b196be75ac5fe571d0de 100644 (file)
@@ -97,4 +97,21 @@ TEST_CASE("Write")
   }
 }
 
+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();