Skip to content

Commit a964f2e

Browse files
authored
[libc] Improve Benchmark UI (#99796)
This PR changes the output to resemble Google Benchmark. e.g. ``` Running Suite: LlvmLibcIsAlNumGpuBenchmark Benchmark | Cycles | Min | Max | Iterations | Time (ns) | Stddev | Threads | ----------------------------------------------------------------------------------------------------- IsAlnum | 92 | 76 | 482 | 23 | 86500 | 76 | 64 | IsAlnumSingleThread | 87 | 76 | 302 | 20 | 72000 | 49 | 1 | IsAlnumSingleWave | 87 | 76 | 302 | 20 | 72000 | 49 | 32 | IsAlnumCapital | 89 | 76 | 299 | 17 | 78500 | 52 | 64 | IsAlnumNotAlnum | 87 | 76 | 303 | 20 | 76000 | 49 | 64 | ```
1 parent 568845a commit a964f2e

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

libc/benchmarks/gpu/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function(add_benchmark benchmark_name)
77
"BENCHMARK"
88
"" # Optional arguments
99
"" # Single value arguments
10-
"LINK_LIBRARIES" # Multi-value arguments
10+
"LINK_LIBRARIES;DEPENDS" # Multi-value arguments
1111
${ARGN}
1212
)
1313

@@ -20,6 +20,9 @@ function(add_benchmark benchmark_name)
2020
LINK_LIBRARIES
2121
LibcGpuBenchmark.hermetic
2222
${BENCHMARK_LINK_LIBRARIES}
23+
DEPENDS
24+
libc.src.stdio.printf
25+
${BENCHMARK_DEPENDS}
2326
${BENCHMARK_UNPARSED_ARGUMENTS}
2427
)
2528
get_fq_target_name(${benchmark_name} fq_target_name)
@@ -55,6 +58,7 @@ add_unittest_framework_library(
5558
libc.src.__support.fixedvector
5659
libc.src.time.clock
5760
libc.benchmarks.gpu.timing.timing
61+
libc.src.stdio.printf
5862
)
5963

6064
add_subdirectory(src)

libc/benchmarks/gpu/LibcGpuBenchmark.cpp

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "src/__support/GPU/utils.h"
88
#include "src/__support/fixedvector.h"
99
#include "src/__support/macros/config.h"
10+
#include "src/stdio/printf.h"
1011
#include "src/time/gpu/time_utils.h"
1112

1213
namespace LIBC_NAMESPACE_DECL {
@@ -73,10 +74,16 @@ struct AtomicBenchmarkSums {
7374
};
7475

7576
AtomicBenchmarkSums all_results;
77+
const char *header_format_string =
78+
"Benchmark | Cycles | Min | Max | Iterations | "
79+
"Time | Stddev | Threads |\n";
80+
const char *output_format_string =
81+
"%-20s |%8ld |%8ld |%8ld |%11ld |%9ld %2s |%9ld |%9d |\n";
82+
83+
constexpr auto GREEN = "\033[32m";
84+
constexpr auto RESET = "\033[0m";
7685

7786
void print_results(Benchmark *b) {
78-
constexpr auto GREEN = "\033[32m";
79-
constexpr auto RESET = "\033[0m";
8087

8188
BenchmarkResult result;
8289
cpp::atomic_thread_fence(cpp::MemoryOrder::RELEASE);
@@ -92,21 +99,51 @@ void print_results(Benchmark *b) {
9299
all_results.samples_sum.load(cpp::MemoryOrder::RELAXED) / num_threads;
93100
result.total_iterations =
94101
all_results.iterations_sum.load(cpp::MemoryOrder::RELAXED) / num_threads;
95-
result.total_time =
102+
const uint64_t duration_ns =
96103
all_results.time_sum.load(cpp::MemoryOrder::RELAXED) / num_threads;
104+
const uint64_t duration_us = duration_ns / 1000;
105+
const uint64_t duration_ms = duration_ns / (1000 * 1000);
106+
uint64_t converted_duration = duration_ns;
107+
cpp::string time_unit;
108+
if (duration_ms != 0) {
109+
converted_duration = duration_ms;
110+
time_unit = cpp::string("ms");
111+
} else if (duration_us != 0) {
112+
converted_duration = duration_us;
113+
time_unit = cpp::string("us");
114+
} else {
115+
converted_duration = duration_ns;
116+
time_unit = cpp::string("ns");
117+
}
118+
result.total_time = converted_duration;
119+
// result.total_time =
120+
// all_results.time_sum.load(cpp::MemoryOrder::RELAXED) / num_threads;
97121
cpp::atomic_thread_fence(cpp::MemoryOrder::RELEASE);
98122

99-
log << GREEN << "[ RUN ] " << RESET << b->get_name() << '\n';
100-
log << GREEN << "[ OK ] " << RESET << b->get_name() << ": "
101-
<< result.cycles << " cycles, " << result.min << " min, " << result.max
102-
<< " max, " << result.total_iterations << " iterations, "
103-
<< result.total_time << " ns, "
104-
<< static_cast<uint64_t>(result.standard_deviation)
105-
<< " stddev (num threads: " << num_threads << ")\n";
123+
LIBC_NAMESPACE::printf(
124+
output_format_string, b->get_test_name().data(), result.cycles,
125+
result.min, result.max, result.total_iterations, result.total_time,
126+
time_unit.data(), static_cast<uint64_t>(result.standard_deviation),
127+
num_threads);
128+
}
129+
130+
void print_header() {
131+
LIBC_NAMESPACE::printf("%s", GREEN);
132+
LIBC_NAMESPACE::printf("Running Suite: %-10s\n",
133+
benchmarks[0]->get_suite_name().data());
134+
LIBC_NAMESPACE::printf("%s", RESET);
135+
LIBC_NAMESPACE::printf(header_format_string);
136+
LIBC_NAMESPACE::printf(
137+
"---------------------------------------------------------------------"
138+
"--------------------------------\n");
106139
}
107140

108141
void Benchmark::run_benchmarks() {
109142
uint64_t id = gpu::get_thread_id();
143+
144+
if (id == 0)
145+
print_header();
146+
110147
gpu::sync_threads();
111148

112149
for (Benchmark *b : benchmarks) {

libc/benchmarks/gpu/LibcGpuBenchmark.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,20 @@ BenchmarkResult benchmark(const BenchmarkOptions &options,
8181

8282
class Benchmark {
8383
const cpp::function<uint64_t(void)> func;
84-
const cpp::string_view name;
84+
const cpp::string_view suite_name;
85+
const cpp::string_view test_name;
8586
const uint8_t flags;
8687

8788
public:
88-
Benchmark(cpp::function<uint64_t(void)> func, char const *name, uint8_t flags)
89-
: func(func), name(name), flags(flags) {
89+
Benchmark(cpp::function<uint64_t(void)> func, char const *suite_name,
90+
char const *test_name, uint8_t flags)
91+
: func(func), suite_name(suite_name), test_name(test_name), flags(flags) {
9092
add_benchmark(this);
9193
}
9294

9395
static void run_benchmarks();
94-
const cpp::string_view get_name() const { return name; }
96+
const cpp::string_view get_suite_name() const { return suite_name; }
97+
const cpp::string_view get_test_name() const { return test_name; }
9598

9699
protected:
97100
static void add_benchmark(Benchmark *benchmark);
@@ -107,16 +110,16 @@ class Benchmark {
107110

108111
#define BENCHMARK(SuiteName, TestName, Func) \
109112
LIBC_NAMESPACE::benchmarks::Benchmark SuiteName##_##TestName##_Instance( \
110-
Func, #SuiteName "." #TestName, 0)
113+
Func, #SuiteName, #TestName, 0)
111114

112115
#define SINGLE_THREADED_BENCHMARK(SuiteName, TestName, Func) \
113116
LIBC_NAMESPACE::benchmarks::Benchmark SuiteName##_##TestName##_Instance( \
114-
Func, #SuiteName "." #TestName, \
117+
Func, #SuiteName, #TestName, \
115118
LIBC_NAMESPACE::benchmarks::BenchmarkFlags::SINGLE_THREADED)
116119

117120
#define SINGLE_WAVE_BENCHMARK(SuiteName, TestName, Func) \
118121
LIBC_NAMESPACE::benchmarks::Benchmark SuiteName##_##TestName##_Instance( \
119-
Func, #SuiteName "." #TestName, \
122+
Func, #SuiteName, #TestName, \
120123
LIBC_NAMESPACE::benchmarks::BenchmarkFlags::SINGLE_WAVE)
121124

122125
#endif

0 commit comments

Comments
 (0)