Skip to content

Commit 472d164

Browse files
feat(google_benchmark): only run benchmark once in instrumentation mode
1 parent 72cd41c commit 472d164

File tree

7 files changed

+88
-6
lines changed

7 files changed

+88
-6
lines changed

google_benchmark/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ option(BENCHMARK_ENABLE_ASSEMBLY_TESTS "Enable building and running the assembly
9797
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
9898
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
9999

100+
# Codspeed specific configuration
101+
include(Codspeed)
100102

101103
# Read the git tags to determine the project version
102104
include(GetGitVersion)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Step 1: Check if CODSPEED_MODE is set via the command line
2+
3+
if(DEFINED CODSPEED_MODE)
4+
# Define a preprocessor macro based on the build mode
5+
if(CODSPEED_MODE STREQUAL "instrumentation")
6+
target_compile_definitions(
7+
codspeed
8+
INTERFACE -DCODSPEED_INSTRUMENTATION
9+
)
10+
elseif(CODSPEED_MODE STREQUAL "walltime")
11+
target_compile_definitions(codspeed INTERFACE -DCODSPEED_WALLTIME)
12+
else()
13+
message(
14+
FATAL_ERROR
15+
"Invalid build mode: ${CODSPEED_MODE}. Use 'instrumentation' or 'walltime'."
16+
)
17+
endif()
18+
endif()
19+
20+
message(STATUS "Build mode set to: ${CODSPEED_MODE}")

google_benchmark/src/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ set_target_properties(benchmark_main PROPERTIES
8282
)
8383
target_link_libraries(benchmark_main PUBLIC benchmark::benchmark)
8484

85-
add_subdirectory(${PROJECT_SOURCE_DIR}/../core codspeed)
86-
87-
target_link_libraries(benchmark PRIVATE codspeed)
85+
# TODO: Find a way to not expose codspeed headers to downstream users
86+
target_link_libraries(benchmark PUBLIC codspeed)
8887

8988
set(generated_dir "${PROJECT_BINARY_DIR}")
9089

google_benchmark/src/benchmark.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,16 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
382382
// Note the file_reporter can be null.
383383
BM_CHECK(display_reporter != nullptr);
384384

385-
hello_codspeed();
386-
385+
#ifdef CODSPEED_ENABLED
386+
auto& Err = display_reporter->GetErrorStream();
387387
// Determine the width of the name field using a minimum width of 10.
388+
#ifdef CODSPEED_INSTRUMENTATION
389+
Err << "Codspeed mode: instrumentation" << "\n";
390+
#elif defined(CODSPEED_WALLTIME)
391+
Err << "Codspeed mode: walltime" << "\n";
392+
#endif
393+
#endif // CODSPEED_ENABLED
394+
388395
bool might_have_aggregates = FLAGS_benchmark_repetitions > 1;
389396
size_t name_field_width = 10;
390397
size_t stat_field_width = 0;

google_benchmark/src/benchmark_api_internal.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
#include <cinttypes>
44

5+
#include "codspeed.h"
56
#include "string_util.h"
7+
#include "thread_timer.h"
68

79
namespace benchmark {
810
namespace internal {
@@ -89,6 +91,28 @@ BenchmarkInstance::BenchmarkInstance(Benchmark* benchmark, int family_idx,
8991
teardown_ = benchmark_.teardown_;
9092
}
9193

94+
#ifdef CODSPEED_INSTRUMENTATION
95+
State BenchmarkInstance::RunInstrumented(
96+
CodSpeed* codspeed, internal::ThreadTimer* timer,
97+
internal::ThreadManager* manager,
98+
internal::PerfCountersMeasurement* perf_counters_measurement,
99+
ProfilerManager* profiler_manager) const {
100+
State st(name_.function_name, 1, args_, 0, 1, timer, manager,
101+
perf_counters_measurement, profiler_manager);
102+
// Do one repetition to avoid flakiness due to inconcistencies in CPU cache
103+
// from execution order
104+
105+
internal::ThreadTimer warmup_timer = internal::ThreadTimer::Create();
106+
State warmup_state(name_.function_name, 1, args_, 0, 1, &warmup_timer,
107+
manager, perf_counters_measurement, profiler_manager);
108+
benchmark_.Run(warmup_state);
109+
codspeed->start_benchmark(name().str());
110+
benchmark_.Run(st);
111+
codspeed->end_benchmark();
112+
return st;
113+
}
114+
#endif
115+
92116
State BenchmarkInstance::Run(
93117
IterationCount iters, int thread_id, internal::ThreadTimer* timer,
94118
internal::ThreadManager* manager,

google_benchmark/src/benchmark_api_internal.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include <vector>
1010

1111
#include "benchmark/benchmark.h"
12+
#ifdef CODSPEED_INSTRUMENTATION
13+
#include "codspeed.h"
14+
#endif
1215
#include "commandlineflags.h"
1316

1417
namespace benchmark {
@@ -34,7 +37,13 @@ class BenchmarkInstance {
3437
BigO complexity() const { return complexity_; }
3538
BigOFunc* complexity_lambda() const { return complexity_lambda_; }
3639
const std::vector<Statistics>& statistics() const { return statistics_; }
37-
int repetitions() const { return repetitions_; }
40+
int repetitions() const {
41+
#ifdef CODSPEED_INSTRUMENTATION
42+
return 1;
43+
#else
44+
return repetitions_;
45+
#endif
46+
}
3847
double min_time() const { return min_time_; }
3948
double min_warmup_time() const { return min_warmup_time_; }
4049
IterationCount iterations() const { return iterations_; }
@@ -47,6 +56,14 @@ class BenchmarkInstance {
4756
internal::PerfCountersMeasurement* perf_counters_measurement,
4857
ProfilerManager* profiler_manager) const;
4958

59+
#ifdef CODSPEED_INSTRUMENTATION
60+
State RunInstrumented(
61+
CodSpeed* codspeed, internal::ThreadTimer* timer,
62+
internal::ThreadManager* manager,
63+
internal::PerfCountersMeasurement* perf_counters_measurement,
64+
ProfilerManager* profiler_manager) const;
65+
#endif
66+
5067
private:
5168
BenchmarkName name_;
5269
Benchmark& benchmark_;

google_benchmark/src/benchmark_runner.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <utility>
4343

4444
#include "check.h"
45+
#include "codspeed.h"
4546
#include "colorprint.h"
4647
#include "commandlineflags.h"
4748
#include "complexity.h"
@@ -460,6 +461,18 @@ void BenchmarkRunner::RunProfilerManager(IterationCount profile_iterations) {
460461
}
461462

462463
void BenchmarkRunner::DoOneRepetition() {
464+
#ifdef CODSPEED_INSTRUMENTATION
465+
std::unique_ptr<internal::ThreadManager> manager;
466+
manager.reset(new internal::ThreadManager(b.threads()));
467+
internal::ThreadTimer timer = internal::ThreadTimer::Create();
468+
b.Setup();
469+
State st = b.RunInstrumented(CodSpeed::getInstance(), &timer, manager.get(),
470+
nullptr, nullptr);
471+
b.Teardown();
472+
473+
return;
474+
#endif
475+
463476
assert(HasRepeatsRemaining() && "Already done all repetitions?");
464477

465478
const bool is_the_first_repetition = num_repetitions_done == 0;

0 commit comments

Comments
 (0)