Skip to content

Commit 7f7ce86

Browse files
committed
feat: add instrument-hooks library
1 parent c959a91 commit 7f7ce86

File tree

11 files changed

+148
-18
lines changed

11 files changed

+148
-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

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

.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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@ config_setting(
88
constraint_values = ["@platforms//os:windows"],
99
)
1010

11+
# Instrument-hooks library with 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+
"/wd4245", # signed/unsigned mismatch
23+
"/wd4132", # const object should be initialized
24+
"/wd4146", # unary minus operator applied to unsigned type
25+
],
26+
"//conditions:default": [
27+
"-Wno-maybe-uninitialized",
28+
"-Wno-unused-variable",
29+
"-Wno-unused-parameter",
30+
"-Wno-unused-but-set-variable",
31+
"-Wno-type-limits",
32+
],
33+
}),
34+
visibility = ["//visibility:public"],
35+
)
36+
37+
1138
# Define the codspeed library
1239
cc_library(
1340
name = "codspeed",
@@ -25,6 +52,7 @@ cc_library(
2552
":walltime_mode": ["CODSPEED_ENABLED", "CODSPEED_WALLTIME"],
2653
"//conditions:default": [],
2754
}),
55+
deps = [":instrument_hooks"],
2856
visibility = ["//visibility:public"],
2957
)
3058

core/CMakeLists.txt

Lines changed: 46 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,46 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
1111
# Add the include directory
1212
include_directories(include)
1313

14-
# Add the library
14+
# Add the instrument_hooks library
15+
add_library(
16+
instrument_hooks
17+
STATIC
18+
instrument-hooks/dist/core.c
19+
)
20+
21+
target_include_directories(
22+
instrument_hooks
23+
PUBLIC
24+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/instrument-hooks/includes>
25+
$<INSTALL_INTERFACE:includes>
26+
)
27+
28+
# Suppress warnings for the instrument_hooks library
29+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
30+
target_compile_options(
31+
instrument_hooks
32+
PRIVATE
33+
-Wno-maybe-uninitialized
34+
-Wno-unused-variable
35+
-Wno-unused-parameter
36+
-Wno-unused-but-set-variable
37+
-Wno-type-limits
38+
)
39+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
40+
target_compile_options(
41+
instrument_hooks
42+
PRIVATE
43+
/wd4101 # unreferenced local variable (equivalent to -Wno-unused-variable)
44+
/wd4189 # local variable is initialized but not referenced (equivalent to -Wno-unused-but-set-variable)
45+
/wd4100 # unreferenced formal parameter (equivalent to -Wno-unused-parameter)
46+
/wd4245 # signed/unsigned mismatch
47+
/wd4132 # const object should be initialized
48+
/wd4146 # unary minus operator applied to unsigned type
49+
)
50+
endif()
51+
52+
53+
# Add the main library
1554
add_library(
1655
codspeed
1756
src/codspeed.cpp
@@ -20,13 +59,17 @@ add_library(
2059
src/workspace.cpp
2160
)
2261

62+
# Link instrument_hooks to codspeed
63+
target_link_libraries(codspeed PRIVATE instrument_hooks)
64+
2365
# Version
2466
add_compile_definitions(CODSPEED_VERSION="${CODSPEED_VERSION}")
2567

2668
# Specify the include directories for users of the library
2769
target_include_directories(
2870
codspeed
2971
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
72+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/instrument-hooks/includes>
3073
)
3174

3275
# Disable valgrind compilation errors
@@ -116,7 +159,7 @@ install(
116159
)
117160

118161
install(
119-
TARGETS codspeed
162+
TARGETS codspeed instrument_hooks
120163
EXPORT codspeed-targets
121164
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
122165
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/instrument-hooks

Submodule instrument-hooks added at 9907c26

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
}

0 commit comments

Comments
 (0)