Skip to content

Commit fa59de1

Browse files
committed
feat: add perf walltime support
1 parent 1bf3eee commit fa59de1

File tree

7 files changed

+86
-13
lines changed

7 files changed

+86
-13
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
jobs:
1010
tests:
1111
runs-on: ubuntu-latest
12-
steps:
12+
steps:
1313
- name: Checkout code
1414
uses: actions/checkout@v3
1515

@@ -29,7 +29,7 @@ jobs:
2929
make -j
3030
3131
- name: Run tests
32-
run: |
32+
run: |
3333
cd core/build-tests
3434
GTEST_OUTPUT=json:test-results/ ctest
3535
@@ -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 }}
@@ -104,10 +106,12 @@ jobs:
104106
- name: Build and run benchmarks
105107
run: |
106108
bazel build //examples/google_benchmark_bazel:my_benchmark --//core:codspeed_mode=${{ matrix.codspeed-mode }}
107-
109+
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ 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 = ["-Wno-unused-variable", "-Wno-unused-parameter", "-Wno-unused-but-set-variable"],
18+
)
19+
1120
# Define the codspeed library
1221
cc_library(
1322
name = "codspeed",
@@ -25,6 +34,7 @@ cc_library(
2534
":walltime_mode": ["CODSPEED_ENABLED", "CODSPEED_WALLTIME"],
2635
"//conditions:default": [],
2736
}),
37+
deps = [":instrument_hooks"],
2838
visibility = ["//visibility:public"],
2939
)
3040

core/CMakeLists.txt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,34 @@ 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

14-
# Add the library
15+
# 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+
target_compile_options(
23+
instrument_hooks
24+
PRIVATE
25+
-Wno-maybe-uninitialized
26+
-Wno-unused-variable
27+
-Wno-unused-parameter
28+
-Wno-unused-but-set-variable
29+
-Wno-type-limits
30+
)
31+
32+
# Add the main library
1533
add_library(
1634
codspeed
1735
src/codspeed.cpp
@@ -20,13 +38,17 @@ add_library(
2038
src/workspace.cpp
2139
)
2240

41+
# Link instrument_hooks to codspeed
42+
target_link_libraries(codspeed PRIVATE instrument_hooks)
43+
2344
# Version
2445
add_compile_definitions(CODSPEED_VERSION="${CODSPEED_VERSION}")
2546

2647
# Specify the include directories for users of the library
2748
target_include_directories(
2849
codspeed
2950
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
51+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/instrument-hooks/includes>
3052
)
3153

3254
# Disable valgrind compilation errors
@@ -116,7 +138,7 @@ install(
116138
)
117139

118140
install(
119-
TARGETS codspeed
141+
TARGETS codspeed instrument_hooks
120142
EXPORT codspeed-targets
121143
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
122144
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 b003e50

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)