Skip to content

Commit 02695d6

Browse files
committed
feat: add perf walltime support
1 parent c361eb3 commit 02695d6

File tree

7 files changed

+106
-9
lines changed

7 files changed

+106
-9
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ jobs:
7373
- name: Run the benchmarks
7474
uses: CodSpeedHQ/action@main
7575
if: matrix.codspeed-mode != 'off'
76+
env:
77+
CODSPEED_PERF_ENABLED: true
7678
with:
7779
run: examples/google_benchmark_cmake/build/benchmark_example
7880
token: ${{ secrets.CODSPEED_TOKEN }}
@@ -108,6 +110,8 @@ jobs:
108110
- name: Run the benchmarks
109111
uses: CodSpeedHQ/action@main
110112
if: matrix.codspeed-mode != 'off'
113+
env:
114+
CODSPEED_PERF_ENABLED: true
111115
with:
112116
run: bazel run //examples/google_benchmark_bazel:my_benchmark --//core:codspeed_mode=${{ matrix.codspeed-mode }}
113117
token: ${{ secrets.CODSPEED_TOKEN }}

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "core/instrument-hooks"]
2+
path = core/instrument-hooks
3+
url = https://github.com/CodSpeedHQ/instrument-hooks

core/BUILD

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ config_setting(
88
constraint_values = ["@platforms//os:windows"],
99
)
1010

11+
# Define the instrument-hooks library separately to apply warning suppressions
12+
cc_library(
13+
name = "instrument_hooks",
14+
srcs = ["instrument-hooks/dist/core.c"],
15+
hdrs = glob(["instrument-hooks/includes/*.h"]),
16+
includes = ["instrument-hooks/includes"],
17+
copts = select({
18+
":windows": [
19+
"/wd4101", # unreferenced local variable (equivalent to -Wno-unused-variable)
20+
"/wd4189", # local variable is initialized but not referenced (equivalent to -Wno-unused-but-set-variable)
21+
"/wd4100", # unreferenced formal parameter (equivalent to -Wno-unused-parameter)
22+
],
23+
"//conditions:default": [
24+
"-Wno-unused-variable",
25+
"-Wno-unused-parameter",
26+
"-Wno-unused-but-set-variable",
27+
],
28+
}),
29+
)
30+
1131
# Define the codspeed library
1232
cc_library(
1333
name = "codspeed",
@@ -25,6 +45,7 @@ cc_library(
2545
":walltime_mode": ["CODSPEED_ENABLED", "CODSPEED_WALLTIME"],
2646
"//conditions:default": [],
2747
}),
48+
deps = [":instrument_hooks"],
2849
visibility = ["//visibility:public"],
2950
)
3051

core/CMakeLists.txt

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,47 @@ cmake_minimum_required(VERSION 3.10)
22

33
set(CODSPEED_VERSION 1.2.0)
44

5-
project(codspeed VERSION ${CODSPEED_VERSION} LANGUAGES CXX)
5+
project(codspeed VERSION ${CODSPEED_VERSION} LANGUAGES CXX C)
66

77
# Specify the C++ standard
88
set(CMAKE_CXX_STANDARD 17)
99
set(CMAKE_CXX_STANDARD_REQUIRED True)
1010

1111
# Add the include directory
1212
include_directories(include)
13+
include_directories(instrument-hooks/includes)
1314

1415
# Add the library
16+
add_library(
17+
instrument_hooks
18+
instrument-hooks/dist/core.c
19+
)
20+
21+
# Suppress warnings for the instrument_hooks library
22+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
23+
target_compile_options(
24+
instrument_hooks
25+
PRIVATE
26+
-Wno-maybe-uninitialized
27+
-Wno-unused-variable
28+
-Wno-unused-parameter
29+
-Wno-unused-but-set-variable
30+
-Wno-type-limits
31+
)
32+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
33+
target_compile_options(
34+
instrument_hooks
35+
PRIVATE
36+
/wd4101 # unreferenced local variable (equivalent to -Wno-unused-variable)
37+
/wd4189 # local variable is initialized but not referenced (equivalent to -Wno-unused-but-set-variable)
38+
/wd4100 # unreferenced formal parameter (equivalent to -Wno-unused-parameter)
39+
/wd4245 # signed/unsigned mismatch
40+
/wd4132 # const object should be initialized
41+
/wd4146 # unary minus operator applied to unsigned type
42+
)
43+
endif()
44+
45+
# Add the main library
1546
add_library(
1647
codspeed
1748
src/codspeed.cpp
@@ -20,13 +51,17 @@ add_library(
2051
src/workspace.cpp
2152
)
2253

54+
# Link instrument_hooks to codspeed
55+
target_link_libraries(codspeed PRIVATE instrument_hooks)
56+
2357
# Version
2458
add_compile_definitions(CODSPEED_VERSION="${CODSPEED_VERSION}")
2559

2660
# Specify the include directories for users of the library
2761
target_include_directories(
2862
codspeed
2963
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
64+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/instrument-hooks/includes>
3065
)
3166

3267
# Disable valgrind compilation errors
@@ -116,7 +151,7 @@ install(
116151
)
117152

118153
install(
119-
TARGETS codspeed
154+
TARGETS codspeed instrument_hooks
120155
EXPORT codspeed-targets
121156
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
122157
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}

core/include/measurement.hpp

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@
22
#define MEASUREMENT_H
33

44
#include <string>
5+
#ifdef _WIN32
6+
#include <process.h>
7+
#else
8+
#include <unistd.h>
9+
#endif
510

611
#ifdef CODSPEED_INSTRUMENTATION
712
#include "callgrind.h"
813
#endif
914

15+
extern "C" {
16+
#include "core.h"
17+
}
18+
19+
inline InstrumentHooks* get_hooks() {
20+
static InstrumentHooks* g_hooks = nullptr;
21+
if (!g_hooks) {
22+
g_hooks = instrument_hooks_init();
23+
}
24+
return g_hooks;
25+
}
26+
1027
inline std::string get_version() {
1128
#ifdef CODSPEED_VERSION
1229
return {CODSPEED_VERSION};
@@ -16,29 +33,45 @@ inline std::string get_version() {
1633
}
1734

1835
#ifdef CODSPEED_INSTRUMENTATION
19-
inline bool measurement_is_instrumented() { return RUNNING_ON_VALGRIND; }
36+
inline bool measurement_is_instrumented() {
37+
return instrument_hooks_is_instrumented(get_hooks());
38+
}
2039

2140
inline void measurement_set_metadata() {
22-
std::string metadata = "Metadata: codspeed-cpp " + get_version();
23-
CALLGRIND_DUMP_STATS_AT(metadata.c_str());
41+
std::string version = get_version();
42+
instrument_hooks_set_integration(get_hooks(), "codspeed-cpp",
43+
version.c_str());
2444
}
2545

2646
__attribute__((always_inline)) inline void measurement_start() {
47+
// Keep the callgrind macros here, so that they are properly inlined.
48+
// Otherwise, we have an additional function call overhead to the
49+
// instrument-hooks library.
2750
CALLGRIND_ZERO_STATS;
2851
CALLGRIND_START_INSTRUMENTATION;
52+
53+
instrument_hooks_start_benchmark(get_hooks());
2954
}
3055

3156
__attribute__((always_inline)) inline void measurement_stop(
32-
const std::string &name) {
57+
const std::string& name) {
3358
CALLGRIND_STOP_INSTRUMENTATION;
34-
CALLGRIND_DUMP_STATS_AT(name.c_str());
59+
60+
instrument_hooks_stop_benchmark(get_hooks());
61+
62+
#ifdef _WIN32
63+
auto current_pid = _getpid();
64+
#else
65+
auto current_pid = getpid();
66+
#endif
67+
instrument_hooks_executed_benchmark(get_hooks(), current_pid, name.c_str());
3568
};
3669
#else
3770
// Stub implementations for non-instrumentation builds
3871
inline bool measurement_is_instrumented() { return false; }
3972
inline void measurement_set_metadata() {}
4073
inline void measurement_start() {}
41-
inline void measurement_stop(const std::string &name) { (void)name; }
74+
inline void measurement_stop(const std::string& name) { (void)name; }
4275
#endif
4376

4477
#endif // MEASUREMENT_H

core/instrument-hooks

Submodule instrument-hooks added at 9db4a71

google_benchmark/src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")
9292
set(pkg_config "${generated_dir}/${PROJECT_NAME}.pc")
9393
set(pkg_config_main "${generated_dir}/${PROJECT_NAME}_main.pc")
9494
# TODO: Find a way to not expose codspeed headers to downstream users
95-
set(targets_to_export benchmark benchmark_main codspeed)
95+
set(targets_to_export benchmark benchmark_main codspeed instrument_hooks)
9696
set(targets_export_name "${PROJECT_NAME}Targets")
9797

9898
set(namespace "${PROJECT_NAME}::")

0 commit comments

Comments
 (0)