]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Prepare for a ccache invocation having multiple results
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 8 Aug 2021 19:44:44 +0000 (21:44 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 10 Aug 2021 13:38:24 +0000 (15:38 +0200)
That is, “result” as in “increased statistics counter”.

src/ccache.cpp
src/core/Statistics.cpp
src/core/Statistics.hpp
src/core/StatsLog.cpp
src/core/StatsLog.hpp
unittest/test_core_Statistics.cpp
unittest/test_core_StatsLog.cpp

index 374a41e43f7cf6dde3969afab0b50c0fbc799252..98a425c59837a92de6553b43f88db02676308df3 100644 (file)
@@ -1858,9 +1858,8 @@ log_result_to_debug_log(Context& ctx)
   }
 
   core::Statistics statistics(ctx.storage.primary.get_statistics_updates());
-  const auto result_message = statistics.get_result_message();
-  if (result_message) {
-    LOG("Result: {}", *result_message);
+  for (const auto& message : statistics.get_statistics_messages()) {
+    LOG("Result: {}", message);
   }
 }
 
@@ -1872,13 +1871,13 @@ log_result_to_stats_log(Context& ctx)
   }
 
   core::Statistics statistics(ctx.storage.primary.get_statistics_updates());
-  const auto result_id = statistics.get_result_id();
-  if (!result_id) {
+  const auto ids = statistics.get_statistics_ids();
+  if (ids.empty()) {
     return;
   }
 
   core::StatsLog(ctx.config.stats_log())
-    .log_result(ctx.args_info.input_file, *result_id);
+    .log_result(ctx.args_info.input_file, ids);
 }
 
 static void
index 6c30fd3a240e0bb95ebfa38ef13c4c9782b67718..61014df6670bb7d500d77bb655d1943762199ba9 100644 (file)
@@ -160,35 +160,29 @@ Statistics::Statistics(const StatisticsCounters& counters)
 {
 }
 
-static const StatisticsField*
-get_result(const core::StatisticsCounters& counters)
+static std::vector<std::string>
+get_statistics_fields(const core::StatisticsCounters& counters, bool id)
 {
+  std::vector<std::string> result;
   for (const auto& field : k_statistics_fields) {
     if (counters.get(field.statistic) != 0 && !(field.flags & FLAG_NOZERO)) {
-      return &field;
+      result.emplace_back(id ? field.id : field.message);
     }
   }
-  return nullptr;
+  std::sort(result.begin(), result.end());
+  return result;
 }
 
-nonstd::optional<std::string>
-Statistics::get_result_id() const
+std::vector<std::string>
+Statistics::get_statistics_ids() const
 {
-  const auto result = get_result(m_counters);
-  if (result) {
-    return result->id;
-  }
-  return nonstd::nullopt;
+  return get_statistics_fields(m_counters, true);
 }
 
-nonstd::optional<std::string>
-Statistics::get_result_message() const
+std::vector<std::string>
+Statistics::get_statistics_messages() const
 {
-  const auto result = get_result(m_counters);
-  if (result) {
-    return result->message;
-  }
-  return nonstd::nullopt;
+  return get_statistics_fields(m_counters, false);
 }
 
 std::string
index 46aa93559082f4522f9e3dbba67c92862eeb288f..39ccbbe28bdbb84c3e9e91dc87b5bf05041486a5 100644 (file)
 
 #include <core/StatisticsCounters.hpp>
 
-#include <third_party/nonstd/optional.hpp>
-
 #include <string>
 #include <unordered_map>
+#include <vector>
 
 class Config;
 
@@ -34,13 +33,11 @@ class Statistics
 public:
   Statistics(const StatisticsCounters& counters);
 
-  // Return a machine-readable string representing the final ccache result, or
-  // nullopt if there was no result.
-  nonstd::optional<std::string> get_result_id() const;
+  // Return machine-readable strings representing the statistics counters.
+  std::vector<std::string> get_statistics_ids() const;
 
-  // Return a human-readable string representing the final ccache result, or
-  // nullopt if there was no result.
-  nonstd::optional<std::string> get_result_message() const;
+  // Return human-readable strings representing the statistics counters.
+  std::vector<std::string> get_statistics_messages() const;
 
   // Format config header in human-readable format.
   static std::string format_config_header(const Config& config);
index 723189053027456dcfe333aad289eedc82b1aed1..3202d2c6d3ec4ef7452a4909c159a50c46caf2b9 100644 (file)
@@ -54,14 +54,17 @@ StatsLog::read() const
 
 void
 StatsLog::log_result(const std::string& input_file,
-                     const std::string& result_id)
+                     const std::vector<std::string>& result_ids)
 {
   File file(m_path, "ab");
-  if (file) {
-    PRINT(*file, "# {}\n", input_file);
-    PRINT(*file, "{}\n", result_id);
-  } else {
+  if (!file) {
     LOG("Failed to open {}: {}", m_path, strerror(errno));
+    return;
+  }
+
+  PRINT(*file, "# {}\n", input_file);
+  for (const auto& id : result_ids) {
+    PRINT(*file, "{}\n", id);
   }
 }
 
index 4081b67330fac33f4e4dd01cc2a790d87fab2b4c..19284de293fcd78980ee5a614c3da73994a6387d 100644 (file)
@@ -21,6 +21,7 @@
 #include "StatisticsCounters.hpp"
 
 #include <string>
+#include <vector>
 
 namespace core {
 
@@ -30,7 +31,8 @@ public:
   StatsLog(const std::string& path);
 
   StatisticsCounters read() const;
-  void log_result(const std::string& input_file, const std::string& result_id);
+  void log_result(const std::string& input_file,
+                  const std::vector<std::string>& result_ids);
 
 private:
   const std::string m_path;
index 2546c6791bc19fb4bdcfb18c42c35fb44280b7c9..2a8620e98987d70476425b69131a7569e3f8e478 100644 (file)
@@ -32,24 +32,34 @@ using TestUtil::TestContext;
 
 TEST_SUITE_BEGIN("core::Statistics");
 
-TEST_CASE("get_result_id")
+TEST_CASE("get_statistics_ids")
 {
   TestContext test_context;
 
   StatisticsCounters counters;
+  counters.increment(Statistic::cache_size_kibibyte);
   counters.increment(Statistic::cache_miss);
+  counters.increment(Statistic::direct_cache_hit);
+  counters.increment(Statistic::autoconf_test);
 
-  CHECK(*Statistics(counters).get_result_id() == "cache_miss");
+  std::vector<std::string> expected = {
+    "autoconf_test", "cache_miss", "direct_cache_hit"};
+  CHECK(Statistics(counters).get_statistics_ids() == expected);
 }
 
-TEST_CASE("get_result_message")
+TEST_CASE("get_statistics_messages")
 {
   TestContext test_context;
 
   StatisticsCounters counters;
+  counters.increment(Statistic::cache_size_kibibyte);
   counters.increment(Statistic::cache_miss);
+  counters.increment(Statistic::direct_cache_hit);
+  counters.increment(Statistic::autoconf_test);
 
-  CHECK(*Statistics(counters).get_result_message() == "cache miss");
+  std::vector<std::string> expected = {
+    "autoconf compile/link", "cache hit (direct)", "cache miss"};
+  CHECK(Statistics(counters).get_statistics_messages() == expected);
 }
 
 TEST_SUITE_END();
index 175d9416f1531af4410217d1a93f8e865eac6d17..132288ade0d97cf86d9009feeee6ecf0e04f0b21 100644 (file)
@@ -45,8 +45,8 @@ TEST_CASE("log_result")
   TestContext test_context;
 
   StatsLog stats_log("stats.log");
-  stats_log.log_result("foo.c", "cache_miss");
-  stats_log.log_result("bar.c", "preprocessed_cache_hit");
+  stats_log.log_result("foo.c", {"cache_miss"});
+  stats_log.log_result("bar.c", {"preprocessed_cache_hit"});
 
   CHECK(Util::read_file("stats.log")
         == "# foo.c\ncache_miss\n# bar.c\npreprocessed_cache_hit\n");