Skip to content

Commit a973930

Browse files
committed
feat: add instrument-hooks library
1 parent fa115d2 commit a973930

File tree

8 files changed

+74
-14
lines changed

8 files changed

+74
-14
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/[email protected]
@@ -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/[email protected]

.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 = "8e872739f0d6866810edb9b4f1816a53bfdc11a7",
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 8e872739f0d6866810edb9b4f1816a53bfdc11a7
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: 34 additions & 9 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,37 @@ 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() {
27-
CALLGRIND_ZERO_STATS;
28-
CALLGRIND_START_INSTRUMENTATION;
47+
instrument_hooks_start_benchmark(get_hooks());
2948
}
3049

3150
__attribute__((always_inline)) inline void measurement_stop(
32-
const std::string &name) {
33-
CALLGRIND_STOP_INSTRUMENTATION;
34-
CALLGRIND_DUMP_STATS_AT(name.c_str());
51+
const std::string& name) {
52+
instrument_hooks_stop_benchmark(get_hooks());
53+
54+
#ifdef _WIN32
55+
auto current_pid = _getpid();
56+
#else
57+
auto current_pid = getpid();
58+
#endif
59+
instrument_hooks_executed_benchmark(get_hooks(), current_pid, name.c_str());
3560
};
3661
#else
3762
// Stub implementations for non-instrumentation builds
3863
inline bool measurement_is_instrumented() { return false; }
3964
inline void measurement_set_metadata() {}
4065
inline void measurement_start() {}
41-
inline void measurement_stop(const std::string &name) { (void)name; }
66+
inline void measurement_stop(const std::string& name) { (void)name; }
4267
#endif
4368

4469
#endif // MEASUREMENT_H

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)