Skip to content

Commit 01e9b26

Browse files
committed
feat: add instrument-hooks library
1 parent 199a78a commit 01e9b26

File tree

10 files changed

+87
-18
lines changed

10 files changed

+87
-18
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jobs:
2020
steps:
2121
- name: Checkout code
2222
uses: actions/checkout@v3
23+
with:
24+
submodules: "recursive"
2325

2426
- name: Cache build
2527
uses: actions/cache@v3
@@ -62,6 +64,8 @@ jobs:
6264
steps:
6365
- name: Checkout code
6466
uses: actions/checkout@v3
67+
with:
68+
submodules: "recursive"
6569

6670
- name: Cache build
6771
uses: actions/cache@v3
@@ -81,6 +85,8 @@ jobs:
8185
- name: Run the benchmarks
8286
uses: CodSpeedHQ/action@main
8387
if: matrix.codspeed-mode != 'off'
88+
env:
89+
CODSPEED_PERF_ENABLED: true
8490
with:
8591
run: examples/google_benchmark_cmake/build/benchmark_example
8692
token: ${{ secrets.CODSPEED_TOKEN }}
@@ -98,6 +104,8 @@ jobs:
98104
runs-on: ${{ matrix.runner }}
99105
steps:
100106
- uses: actions/checkout@v4
107+
with:
108+
submodules: "recursive"
101109

102110
- name: Set up Bazel
103111
uses: bazel-contrib/setup-bazel@0.14.0
@@ -116,6 +124,8 @@ jobs:
116124
- name: Run the benchmarks
117125
uses: CodSpeedHQ/action@main
118126
if: matrix.codspeed-mode != 'off'
127+
env:
128+
CODSPEED_PERF_ENABLED: true
119129
with:
120130
run: bazel run //examples/google_benchmark_bazel:my_benchmark --//core:codspeed_mode=${{ matrix.codspeed-mode }}
121131
token: ${{ secrets.CODSPEED_TOKEN }}
@@ -128,6 +138,8 @@ jobs:
128138
steps:
129139
- name: Checkout code
130140
uses: actions/checkout@v3
141+
with:
142+
submodules: "recursive"
131143

132144
- name: Cache build
133145
uses: actions/cache@v3
@@ -156,6 +168,8 @@ jobs:
156168
runs-on: windows-latest
157169
steps:
158170
- uses: actions/checkout@v4
171+
with:
172+
submodules: "recursive"
159173

160174
- name: Set up Bazel
161175
uses: bazel-contrib/setup-bazel@0.14.0

.gitmodules

Whitespace-only changes.

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# See https://pre-commit.com for more information
22
# See https://pre-commit.com/hooks.html for more hooks
3-
exclude: '^(google_benchmark/.*|core/instrument-hooks/.*|.*/build/.*|build/.*|core/include/valgrind\.h|core/include/callgrind\.h)'
3+
exclude: '^(google_benchmark/.*|.*/build/.*|build/.*|core/include/valgrind\.h|core/include/callgrind\.h)'
44
files: ^(core|examples)/.*$
55

66
repos:

MODULE.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
bazel_dep(name = "rules_cc", version = "0.0.17")
2+
bazel_dep(name = "instrument_hooks", version = "1.0.0")
3+
4+
git_override(
5+
module_name = "instrument_hooks",
6+
commit = "42ed74076c697c2f06c5ac81a84ccee983d7f140",
7+
remote = "https://github.com/CodSpeedHQ/instrument-hooks",
8+
)

core/BUILD

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

11+
1112
# Define the codspeed library
1213
cc_library(
1314
name = "codspeed",
@@ -25,6 +26,7 @@ cc_library(
2526
":walltime_mode": ["CODSPEED_ENABLED", "CODSPEED_WALLTIME"],
2627
"//conditions:default": [],
2728
}),
29+
deps = ["@instrument_hooks//:instrument_hooks"],
2830
visibility = ["//visibility:public"],
2931
)
3032

core/CMakeLists.txt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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)
@@ -11,7 +11,15 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
1111
# Add the include directory
1212
include_directories(include)
1313

14-
# Add the library
14+
include(FetchContent)
15+
FetchContent_Declare(
16+
instrument_hooks
17+
GIT_REPOSITORY https://github.com/CodSpeedHQ/instrument-hooks/
18+
GIT_TAG 42ed74076c697c2f06c5ac81a84ccee983d7f140
19+
)
20+
FetchContent_MakeAvailable(instrument_hooks)
21+
22+
# Add the main library
1523
add_library(
1624
codspeed
1725
src/codspeed.cpp
@@ -20,13 +28,17 @@ add_library(
2028
src/workspace.cpp
2129
)
2230

31+
# Link instrument_hooks to codspeed
32+
target_link_libraries(codspeed PRIVATE instrument_hooks)
33+
2334
# Version
2435
add_compile_definitions(CODSPEED_VERSION="${CODSPEED_VERSION}")
2536

2637
# Specify the include directories for users of the library
2738
target_include_directories(
2839
codspeed
2940
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
41+
$<BUILD_INTERFACE:${instrument_hooks_SOURCE_DIR}/includes>
3042
)
3143

3244
# Disable valgrind compilation errors
@@ -116,7 +128,7 @@ install(
116128
)
117129

118130
install(
119-
TARGETS codspeed
131+
TARGETS codspeed instrument_hooks
120132
EXPORT codspeed-targets
121133
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
122134
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}

core/include/measurement.hpp

Lines changed: 40 additions & 11 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+
static InstrumentHooks* g_hooks = nullptr;
20+
21+
inline void measurement_init() {
22+
if (!g_hooks) {
23+
g_hooks = instrument_hooks_init();
24+
}
25+
}
26+
1027
inline std::string get_version() {
1128
#ifdef CODSPEED_VERSION
1229
return {CODSPEED_VERSION};
@@ -16,29 +33,41 @@ 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(g_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(g_hooks, "codspeed-cpp", version.c_str());
2443
}
2544

2645
__attribute__((always_inline)) inline void measurement_start() {
27-
CALLGRIND_ZERO_STATS;
28-
CALLGRIND_START_INSTRUMENTATION;
46+
instrument_hooks_start_benchmark_inline(g_hooks);
47+
}
48+
49+
__attribute__((always_inline)) inline void measurement_stop() {
50+
instrument_hooks_stop_benchmark_inline(g_hooks);
2951
}
3052

31-
__attribute__((always_inline)) inline void measurement_stop(
32-
const std::string &name) {
33-
CALLGRIND_STOP_INSTRUMENTATION;
34-
CALLGRIND_DUMP_STATS_AT(name.c_str());
35-
};
53+
__attribute__((always_inline)) inline void measurement_executed_benchmark(
54+
const std::string& name) {
55+
#ifdef _WIN32
56+
auto current_pid = _getpid();
57+
#else
58+
auto current_pid = getpid();
59+
#endif
60+
instrument_hooks_executed_benchmark(g_hooks, current_pid, name.c_str());
61+
}
3662
#else
3763
// Stub implementations for non-instrumentation builds
3864
inline bool measurement_is_instrumented() { return false; }
3965
inline void measurement_set_metadata() {}
4066
inline void measurement_start() {}
41-
inline void measurement_stop(const std::string &name) { (void)name; }
67+
inline void measurement_stop() {}
68+
inline void measurement_executed_benchmark(const std::string& name) {
69+
(void)name;
70+
}
4271
#endif
4372

4473
#endif // MEASUREMENT_H

core/src/codspeed.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ CodSpeed::CodSpeed() : is_instrumented(measurement_is_instrumented()) {
5555
"be made since it's running in an unknown environment."
5656
<< std::endl;
5757
}
58+
measurement_init();
5859
measurement_set_metadata();
5960
}
6061

@@ -85,7 +86,8 @@ void CodSpeed::start_benchmark(const std::string &name) {
8586
}
8687

8788
void CodSpeed::end_benchmark() {
88-
measurement_stop(current_benchmark);
89+
measurement_executed_benchmark(current_benchmark);
90+
8991
benchmarked.push_back(current_benchmark);
9092
std::string action_str = is_instrumented ? "Measured" : "Checked";
9193
std::string group_str =

google_benchmark/include/benchmark/benchmark.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,8 @@ struct State::StateIterator {
10651065
bool operator!=(StateIterator const&) const {
10661066
if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) return true;
10671067
#ifdef CODSPEED_INSTRUMENTATION
1068+
measurement_stop();
1069+
10681070
if (parent_->codspeed_ != NULL) {
10691071
parent_->codspeed_->end_benchmark();
10701072
}
@@ -1086,8 +1088,9 @@ inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() {
10861088
#ifdef CODSPEED_INSTRUMENTATION
10871089
if (this->codspeed_ != NULL) {
10881090
this->codspeed_->start_benchmark(name_);
1089-
measurement_start();
10901091
}
1092+
1093+
measurement_start();
10911094
#endif
10921095
return StateIterator();
10931096
}

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)