Skip to content

Commit bf3c469

Browse files
--wip-- [skip ci]
1 parent 516e797 commit bf3c469

File tree

8 files changed

+61
-54
lines changed

8 files changed

+61
-54
lines changed

core/CMakeLists.txt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
cmake_minimum_required(VERSION 3.10)
22

3-
set(CODSPEED_VERSION 1.0)
3+
set(CODSPEED_VERSION 1.0.0)
44

5-
project(
6-
codspeed
7-
VERSION ${CODSPEED_VERSION}
8-
LANGUAGES CXX)
5+
project(codspeed VERSION ${CODSPEED_VERSION} LANGUAGES CXX)
96

107
# Specify the C++ standard
118
set(CMAKE_CXX_STANDARD 17)
@@ -22,10 +19,12 @@ add_compile_definitions(CODSPEED_VERSION="${CODSPEED_VERSION}")
2219

2320
# Disable valgrind compilation errors
2421
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
25-
# Disable the old-style-cast warning for the specific target
26-
target_compile_options(codspeed PRIVATE -Wno-old-style-cast)
22+
# Disable the old-style-cast warning for the specific target
23+
target_compile_options(codspeed PRIVATE -Wno-old-style-cast)
2724
endif()
2825

2926
# Specify the include directories for users of the library
3027
target_include_directories(
31-
codspeed PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
28+
codspeed
29+
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
30+
)

core/src/codspeed.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ std::string join(const std::vector<std::string> &elements,
1818

1919
CodSpeed::CodSpeed() : is_instrumented(measurement_is_instrumented()) {
2020
if (!is_instrumented) {
21-
std::cout
21+
std::cerr
2222
<< "NOTICE: codspeed is enabled, but no performance measurement will "
2323
"be made since it's running in an unknown environment."
2424
<< std::endl;
@@ -48,6 +48,6 @@ void CodSpeed::end_benchmark() {
4848
std::string action_str = is_instrumented ? "Measured" : "Checked";
4949
std::string group_str =
5050
group_stack.empty() ? "" : " (group: " + join(group_stack, "/") + ")";
51-
std::cout << action_str << ": " << current_benchmark << group_str
51+
std::cerr << action_str << ": " << current_benchmark << group_str
5252
<< std::endl;
5353
}

examples/google_benchmark/CMakeLists.txt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
cmake_minimum_required(VERSION 3.12)
22
include(FetchContent)
33

4-
project(
5-
codspeed_picobench_compat
6-
VERSION 0.0.0
7-
LANGUAGES CXX)
4+
project(codspeed_picobench_compat VERSION 0.0.0 LANGUAGES CXX)
85

96
set(BENCHMARK_DOWNLOAD_DEPENDENCIES ON)
107

11-
# FetchContent_Declare( google_benchmark GIT_REPOSITORY
12-
# https://github.com/CodSpeedHQ/codspeed-cpp GIT_TAG
13-
# origin/cod-291-implement-the-core-instrumentation-library-in-c SOURCE_SUBDIR
14-
# google_benchmark)
15-
16-
FetchContent_Declare(google_benchmark
17-
SOURCE_DIR ${CMAKE_SOURCE_DIR}/../../google_benchmark)
8+
FetchContent_Declare(
9+
google_benchmark
10+
SOURCE_DIR
11+
${CMAKE_SOURCE_DIR}/../../google_benchmark
12+
)
1813

1914
FetchContent_MakeAvailable(google_benchmark)
2015

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
buildInputs =
3939
with pkgs;
4040
[
41-
cmake-language-server
41+
gersemi
4242
clang-tools
4343
]
4444
++ commonBuildInputs;
Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
add_subdirectory(${PROJECT_SOURCE_DIR}/../core codspeed)
22

33
execute_process(
4-
COMMAND git rev-parse --show-toplevel
5-
OUTPUT_VARIABLE GIT_ROOT_DIR
6-
OUTPUT_STRIP_TRAILING_WHITESPACE)
4+
COMMAND git rev-parse --show-toplevel
5+
OUTPUT_VARIABLE GIT_ROOT_DIR
6+
OUTPUT_STRIP_TRAILING_WHITESPACE
7+
RESULT_VARIABLE GIT_COMMAND_RESULT
8+
)
79

8-
target_compile_definitions(codspeed
9-
INTERFACE -DCODSPEED_GIT_ROOT_DIR="${GIT_ROOT_DIR}")
10+
if(NOT GIT_COMMAND_RESULT EQUAL 0)
11+
message(
12+
WARNING
13+
"Failed to determine the git root directory.\
14+
Check that git is in your PATH, and that you are in a git repository.\
15+
Continuing, but codspeed features will not be useable"
16+
)
17+
# Default to user's cmake source directory
18+
set(GIT_ROOT_DIR ${CMAKE_SOURCE_DIR})
19+
endif()
1020

11-
# Step 1: Check if CODSPEED_MODE is set via the command line
21+
target_compile_definitions(
22+
codspeed
23+
INTERFACE -DCODSPEED_GIT_ROOT_DIR="${GIT_ROOT_DIR}"
24+
)
1225

13-
# CMake cache kind of breaks this mechanism, keeping it for first time
14-
# defaulting
15-
if(NOT DEFINED CODSPEED_MODE)
16-
# Step 2: Check the environment variable CODSPEED_MODE
17-
if(DEFINED $ENV{CODSPEED_RUNNER_MODE})
18-
set(CODSPEED_MODE $ENV{CODSPEED_RUNNER_MODE} FORCE)
26+
if(DEFINED CODSPEED_MODE)
27+
# Define a preprocessor macro based on the build mode
28+
if(CODSPEED_MODE STREQUAL "instrumentation")
29+
target_compile_definitions(
30+
codspeed
31+
INTERFACE -DCODSPEED_INSTRUMENTATION
32+
)
33+
elseif(CODSPEED_MODE STREQUAL "walltime")
34+
target_compile_definitions(codspeed INTERFACE -DCODSPEED_WALLTIME)
1935
else()
20-
# Step 3: Default to "instrumentation" if no value is provided
21-
set(CODSPEED_MODE "instrumentation")
36+
message(
37+
FATAL_ERROR
38+
"Invalid build mode: ${CODSPEED_MODE}. Use 'instrumentation' or 'walltime'."
39+
)
2240
endif()
2341
endif()
2442

25-
# Define a preprocessor macro based on the build mode
26-
if(CODSPEED_MODE STREQUAL "instrumentation")
27-
target_compile_definitions(codspeed INTERFACE -DCODSPEED_INSTRUMENTATION)
28-
elseif(CODSPEED_MODE STREQUAL "walltime")
29-
target_compile_definitions(codspeed INTERFACE -DCODSPEED_WALLTIME)
30-
else()
31-
message(
32-
FATAL_ERROR
33-
"Invalid build mode: ${CODSPEED_MODE}. Use 'instrumentation' or 'walltime'."
34-
)
35-
endif()
36-
3743
message(STATUS "Build mode set to: ${CODSPEED_MODE}")

google_benchmark/src/benchmark.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,16 @@ void Report(BenchmarkReporter* display_reporter,
379379
void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
380380
BenchmarkReporter* display_reporter,
381381
BenchmarkReporter* file_reporter) {
382+
auto& Err = display_reporter->GetErrorStream();
383+
382384
// Note the file_reporter can be null.
383385
BM_CHECK(display_reporter != nullptr);
384386

385387
// Determine the width of the name field using a minimum width of 10.
386388
#ifdef CODSPEED_INSTRUMENTATION
387-
std::cout << "Codspeed mode: instrumentation" << "\n";
389+
Err << "Codspeed mode: instrumentation" << "\n";
388390
#elif defined(CODSPEED_WALLTIME)
389-
std::cout << "Codspeed mode walltime" << "\n";
391+
Err << "Codspeed mode: walltime" << "\n";
390392
#endif
391393

392394
bool might_have_aggregates = FLAGS_benchmark_repetitions > 1;

google_benchmark/src/benchmark_api_internal.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ State BenchmarkInstance::RunInstrumented(
9898
ProfilerManager* profiler_manager) const {
9999
State st(name_.function_name, 1, args_, 0, 1, timer, manager,
100100
perf_counters_measurement, profiler_manager);
101+
// Do one repetition to avoid flakiness due to inconcistencies in CPU cache
102+
// from execution order
103+
benchmark_.Run(st);
101104
codspeed->start_benchmark(name().str());
102105
benchmark_.Run(st);
103106
codspeed->end_benchmark();

google_benchmark/src/benchmark_runner.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,20 +461,22 @@ void BenchmarkRunner::RunProfilerManager(IterationCount profile_iterations) {
461461
}
462462

463463
void BenchmarkRunner::DoOneRepetition() {
464-
assert(HasRepeatsRemaining() && "Already done all repetitions?");
465-
466-
const bool is_the_first_repetition = num_repetitions_done == 0;
467-
468464
#ifdef CODSPEED_INSTRUMENTATION
469465
std::unique_ptr<internal::ThreadManager> manager;
470466
manager.reset(new internal::ThreadManager(b.threads()));
471467
internal::ThreadTimer timer = internal::ThreadTimer::Create();
468+
b.Setup();
472469
State st = b.RunInstrumented(CodSpeed::getInstance(), &timer, manager.get(),
473470
nullptr, nullptr);
471+
b.Teardown();
474472

475473
return;
476474
#endif
477475

476+
assert(HasRepeatsRemaining() && "Already done all repetitions?");
477+
478+
const bool is_the_first_repetition = num_repetitions_done == 0;
479+
478480
// In case a warmup phase is requested by the benchmark, run it now.
479481
// After running the warmup phase the BenchmarkRunner should be in a state as
480482
// this warmup never happened except the fact that warmup_done is set. Every

0 commit comments

Comments
 (0)