[libc-commits] [libc] [libc] Improve Benchmark UI (PR #99796)

via libc-commits libc-commits at lists.llvm.org
Sun Jul 21 14:11:57 PDT 2024


https://github.com/jameshu15869 updated https://github.com/llvm/llvm-project/pull/99796

>From c8e917c6d79cefcacd72b068f07aec7f44e805f1 Mon Sep 17 00:00:00 2001
From: jameshu15869 <jhudson15869 at gmail.com>
Date: Sat, 20 Jul 2024 23:33:55 -0400
Subject: [PATCH 1/3] initial ui updates (pending printf %ns fix)

---
 libc/benchmarks/gpu/CMakeLists.txt       |  6 ++++-
 libc/benchmarks/gpu/LibcGpuBenchmark.cpp | 34 +++++++++++++++++++-----
 libc/benchmarks/gpu/LibcGpuBenchmark.h   | 17 +++++++-----
 3 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/libc/benchmarks/gpu/CMakeLists.txt b/libc/benchmarks/gpu/CMakeLists.txt
index 29e27724e1ab3..69518acff3a5d 100644
--- a/libc/benchmarks/gpu/CMakeLists.txt
+++ b/libc/benchmarks/gpu/CMakeLists.txt
@@ -7,7 +7,7 @@ function(add_benchmark benchmark_name)
     "BENCHMARK"
     "" # Optional arguments
     "" # Single value arguments
-    "LINK_LIBRARIES" # Multi-value arguments
+    "LINK_LIBRARIES;DEPENDS" # Multi-value arguments
     ${ARGN}
   )
   
@@ -20,6 +20,9 @@ function(add_benchmark benchmark_name)
     LINK_LIBRARIES
       LibcGpuBenchmark.hermetic
       ${BENCHMARK_LINK_LIBRARIES}
+    DEPENDS
+      libc.src.stdio.printf
+      ${BENCHMARK_DEPENDS}
     ${BENCHMARK_UNPARSED_ARGUMENTS}
   )
   get_fq_target_name(${benchmark_name} fq_target_name)
@@ -55,6 +58,7 @@ add_unittest_framework_library(
     libc.src.__support.fixedvector
     libc.src.time.clock
     libc.benchmarks.gpu.timing.timing
+    libc.src.stdio.printf
 )
 
 add_subdirectory(src)
diff --git a/libc/benchmarks/gpu/LibcGpuBenchmark.cpp b/libc/benchmarks/gpu/LibcGpuBenchmark.cpp
index c926d8efd7db2..28f812f5d170c 100644
--- a/libc/benchmarks/gpu/LibcGpuBenchmark.cpp
+++ b/libc/benchmarks/gpu/LibcGpuBenchmark.cpp
@@ -7,6 +7,7 @@
 #include "src/__support/GPU/utils.h"
 #include "src/__support/fixedvector.h"
 #include "src/__support/macros/config.h"
+#include "src/stdio/printf.h"
 #include "src/time/gpu/time_utils.h"
 
 namespace LIBC_NAMESPACE_DECL {
@@ -73,6 +74,11 @@ struct AtomicBenchmarkSums {
 };
 
 AtomicBenchmarkSums all_results;
+const char *header_format_string =
+    "Benchmark            |  Cycles |     Min |     Max | Iterations |   Time "
+    "(ns) |   Stddev |  Threads |\n";
+const char *output_format_string =
+    "%-20s |%8ld |%8ld |%8ld |%11ld |%12ld |%9ld |%9d |\n";
 
 void print_results(Benchmark *b) {
   constexpr auto GREEN = "\033[32m";
@@ -96,17 +102,33 @@ void print_results(Benchmark *b) {
       all_results.time_sum.load(cpp::MemoryOrder::RELAXED) / num_threads;
   cpp::atomic_thread_fence(cpp::MemoryOrder::RELEASE);
 
-  log << GREEN << "[ RUN      ] " << RESET << b->get_name() << '\n';
-  log << GREEN << "[       OK ] " << RESET << b->get_name() << ": "
-      << result.cycles << " cycles, " << result.min << " min, " << result.max
-      << " max, " << result.total_iterations << " iterations, "
-      << result.total_time << " ns, "
-      << static_cast<uint64_t>(result.standard_deviation)
+  log << GREEN << "[ RUN      ] " << RESET << b->get_suite_name() << '.'
+      << b->get_test_name() << '\n';
+  log << GREEN << "[       OK ] " << RESET << b->get_suite_name() << '.'
+      << b->get_test_name() << ": " << result.cycles << " cycles, "
+      << result.min << " min, " << result.max << " max, "
+      << result.total_iterations << " iterations, " << result.total_time
+      << " ns, " << static_cast<uint64_t>(result.standard_deviation)
       << " stddev (num threads: " << num_threads << ")\n";
+
+  printf(output_format_string, b->get_test_name().data(), result.cycles,
+         result.min, result.max, result.total_iterations, result.total_time,
+         static_cast<uint64_t>(result.standard_deviation), num_threads);
+}
+
+void print_header() {
+  printf("Running Suite: %-10s\n", benchmarks[0]->get_suite_name().data());
+  printf(header_format_string);
+  printf("---------------------------------------------------------------------"
+         "----------------------\n");
 }
 
 void Benchmark::run_benchmarks() {
   uint64_t id = gpu::get_thread_id();
+
+  if (id == 0)
+    print_header();
+
   gpu::sync_threads();
 
   for (Benchmark *b : benchmarks) {
diff --git a/libc/benchmarks/gpu/LibcGpuBenchmark.h b/libc/benchmarks/gpu/LibcGpuBenchmark.h
index 29d7ba8b0a132..c07fab9ccfbe3 100644
--- a/libc/benchmarks/gpu/LibcGpuBenchmark.h
+++ b/libc/benchmarks/gpu/LibcGpuBenchmark.h
@@ -81,17 +81,20 @@ BenchmarkResult benchmark(const BenchmarkOptions &options,
 
 class Benchmark {
   const cpp::function<uint64_t(void)> func;
-  const cpp::string_view name;
+  const cpp::string_view suite_name;
+  const cpp::string_view test_name;
   const uint8_t flags;
 
 public:
-  Benchmark(cpp::function<uint64_t(void)> func, char const *name, uint8_t flags)
-      : func(func), name(name), flags(flags) {
+  Benchmark(cpp::function<uint64_t(void)> func, char const *suite_name,
+            char const *test_name, uint8_t flags)
+      : func(func), suite_name(suite_name), test_name(test_name), flags(flags) {
     add_benchmark(this);
   }
 
   static void run_benchmarks();
-  const cpp::string_view get_name() const { return name; }
+  const cpp::string_view get_suite_name() const { return suite_name; }
+  const cpp::string_view get_test_name() const { return test_name; }
 
 protected:
   static void add_benchmark(Benchmark *benchmark);
@@ -107,16 +110,16 @@ class Benchmark {
 
 #define BENCHMARK(SuiteName, TestName, Func)                                   \
   LIBC_NAMESPACE::benchmarks::Benchmark SuiteName##_##TestName##_Instance(     \
-      Func, #SuiteName "." #TestName, 0)
+      Func, #SuiteName, #TestName, 0)
 
 #define SINGLE_THREADED_BENCHMARK(SuiteName, TestName, Func)                   \
   LIBC_NAMESPACE::benchmarks::Benchmark SuiteName##_##TestName##_Instance(     \
-      Func, #SuiteName "." #TestName,                                          \
+      Func, #SuiteName, #TestName,                                             \
       LIBC_NAMESPACE::benchmarks::BenchmarkFlags::SINGLE_THREADED)
 
 #define SINGLE_WAVE_BENCHMARK(SuiteName, TestName, Func)                       \
   LIBC_NAMESPACE::benchmarks::Benchmark SuiteName##_##TestName##_Instance(     \
-      Func, #SuiteName "." #TestName,                                          \
+      Func, #SuiteName, #TestName,                                             \
       LIBC_NAMESPACE::benchmarks::BenchmarkFlags::SINGLE_WAVE)
 
 #endif

>From 62560d964a0bda6b23f02a76f37fcc129c83e504 Mon Sep 17 00:00:00 2001
From: jameshu15869 <jhudson15869 at gmail.com>
Date: Sat, 20 Jul 2024 23:57:12 -0400
Subject: [PATCH 2/3] remove old logging

---
 libc/benchmarks/gpu/LibcGpuBenchmark.cpp | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/libc/benchmarks/gpu/LibcGpuBenchmark.cpp b/libc/benchmarks/gpu/LibcGpuBenchmark.cpp
index 28f812f5d170c..ffb2433479234 100644
--- a/libc/benchmarks/gpu/LibcGpuBenchmark.cpp
+++ b/libc/benchmarks/gpu/LibcGpuBenchmark.cpp
@@ -80,9 +80,10 @@ const char *header_format_string =
 const char *output_format_string =
     "%-20s |%8ld |%8ld |%8ld |%11ld |%12ld |%9ld |%9d |\n";
 
+constexpr auto GREEN = "\033[32m";
+constexpr auto RESET = "\033[0m";
+
 void print_results(Benchmark *b) {
-  constexpr auto GREEN = "\033[32m";
-  constexpr auto RESET = "\033[0m";
 
   BenchmarkResult result;
   cpp::atomic_thread_fence(cpp::MemoryOrder::RELEASE);
@@ -102,25 +103,18 @@ void print_results(Benchmark *b) {
       all_results.time_sum.load(cpp::MemoryOrder::RELAXED) / num_threads;
   cpp::atomic_thread_fence(cpp::MemoryOrder::RELEASE);
 
-  log << GREEN << "[ RUN      ] " << RESET << b->get_suite_name() << '.'
-      << b->get_test_name() << '\n';
-  log << GREEN << "[       OK ] " << RESET << b->get_suite_name() << '.'
-      << b->get_test_name() << ": " << result.cycles << " cycles, "
-      << result.min << " min, " << result.max << " max, "
-      << result.total_iterations << " iterations, " << result.total_time
-      << " ns, " << static_cast<uint64_t>(result.standard_deviation)
-      << " stddev (num threads: " << num_threads << ")\n";
-
   printf(output_format_string, b->get_test_name().data(), result.cycles,
          result.min, result.max, result.total_iterations, result.total_time,
          static_cast<uint64_t>(result.standard_deviation), num_threads);
 }
 
 void print_header() {
+  printf("%s", GREEN);
   printf("Running Suite: %-10s\n", benchmarks[0]->get_suite_name().data());
+  printf("%s", RESET);
   printf(header_format_string);
   printf("---------------------------------------------------------------------"
-         "----------------------\n");
+         "--------------------------------\n");
 }
 
 void Benchmark::run_benchmarks() {

>From 30de37c21bb691aa9cc5fd406fb52e066d37ebcd Mon Sep 17 00:00:00 2001
From: jameshu15869 <jhudson15869 at gmail.com>
Date: Sun, 21 Jul 2024 17:10:27 -0400
Subject: [PATCH 3/3] change time unit based on nearest SI value

---
 libc/benchmarks/gpu/LibcGpuBenchmark.cpp | 47 +++++++++++++++++-------
 1 file changed, 34 insertions(+), 13 deletions(-)

diff --git a/libc/benchmarks/gpu/LibcGpuBenchmark.cpp b/libc/benchmarks/gpu/LibcGpuBenchmark.cpp
index ffb2433479234..59de18c20417d 100644
--- a/libc/benchmarks/gpu/LibcGpuBenchmark.cpp
+++ b/libc/benchmarks/gpu/LibcGpuBenchmark.cpp
@@ -75,10 +75,10 @@ struct AtomicBenchmarkSums {
 
 AtomicBenchmarkSums all_results;
 const char *header_format_string =
-    "Benchmark            |  Cycles |     Min |     Max | Iterations |   Time "
-    "(ns) |   Stddev |  Threads |\n";
+    "Benchmark            |  Cycles |     Min |     Max | Iterations |        "
+    "Time |   Stddev |  Threads |\n";
 const char *output_format_string =
-    "%-20s |%8ld |%8ld |%8ld |%11ld |%12ld |%9ld |%9d |\n";
+    "%-20s |%8ld |%8ld |%8ld |%11ld |%9ld %2s |%9ld |%9d |\n";
 
 constexpr auto GREEN = "\033[32m";
 constexpr auto RESET = "\033[0m";
@@ -99,22 +99,43 @@ void print_results(Benchmark *b) {
       all_results.samples_sum.load(cpp::MemoryOrder::RELAXED) / num_threads;
   result.total_iterations =
       all_results.iterations_sum.load(cpp::MemoryOrder::RELAXED) / num_threads;
-  result.total_time =
+  const uint64_t duration_ns =
       all_results.time_sum.load(cpp::MemoryOrder::RELAXED) / num_threads;
+  const uint64_t duration_us = duration_ns / 1000;
+  const uint64_t duration_ms = duration_ns / (1000 * 1000);
+  uint64_t converted_duration = duration_ns;
+  cpp::string time_unit;
+  if (duration_ms != 0) {
+    converted_duration = duration_ms;
+    time_unit = cpp::string("ms");
+  } else if (duration_us != 0) {
+    converted_duration = duration_us;
+    time_unit = cpp::string("us");
+  } else {
+    converted_duration = duration_ns;
+    time_unit = cpp::string("ns");
+  }
+  result.total_time = converted_duration;
+  // result.total_time =
+  //     all_results.time_sum.load(cpp::MemoryOrder::RELAXED) / num_threads;
   cpp::atomic_thread_fence(cpp::MemoryOrder::RELEASE);
 
-  printf(output_format_string, b->get_test_name().data(), result.cycles,
-         result.min, result.max, result.total_iterations, result.total_time,
-         static_cast<uint64_t>(result.standard_deviation), num_threads);
+  LIBC_NAMESPACE::printf(
+      output_format_string, b->get_test_name().data(), result.cycles,
+      result.min, result.max, result.total_iterations, result.total_time,
+      time_unit.data(), static_cast<uint64_t>(result.standard_deviation),
+      num_threads);
 }
 
 void print_header() {
-  printf("%s", GREEN);
-  printf("Running Suite: %-10s\n", benchmarks[0]->get_suite_name().data());
-  printf("%s", RESET);
-  printf(header_format_string);
-  printf("---------------------------------------------------------------------"
-         "--------------------------------\n");
+  LIBC_NAMESPACE::printf("%s", GREEN);
+  LIBC_NAMESPACE::printf("Running Suite: %-10s\n",
+                         benchmarks[0]->get_suite_name().data());
+  LIBC_NAMESPACE::printf("%s", RESET);
+  LIBC_NAMESPACE::printf(header_format_string);
+  LIBC_NAMESPACE::printf(
+      "---------------------------------------------------------------------"
+      "--------------------------------\n");
 }
 
 void Benchmark::run_benchmarks() {



More information about the libc-commits mailing list