Skip to content

Commit 66a07a7

Browse files
committed
feat: add perf walltime support
1 parent d68b802 commit 66a07a7

File tree

7 files changed

+87
-12
lines changed

7 files changed

+87
-12
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
@@ -3,6 +3,15 @@ load("@rules_cc//cc:defs.bzl", "cc_library")
33

44
CODSPEED_VERSION = "1.1.1"
55

6+
# Define the instrument-hooks library separately to apply warning suppressions
7+
cc_library(
8+
name = "instrument_hooks",
9+
srcs = ["instrument-hooks/dist/core.c"],
10+
hdrs = glob(["instrument-hooks/includes/*.h"]),
11+
includes = ["instrument-hooks/includes"],
12+
copts = ["-Wno-unused-variable", "-Wno-unused-parameter", "-Wno-unused-but-set-variable"],
13+
)
14+
615
# Define the codspeed library
716
cc_library(
817
name = "codspeed",
@@ -16,6 +25,7 @@ cc_library(
1625
":walltime_mode": ["CODSPEED_ENABLED", "CODSPEED_WALLTIME"],
1726
"//conditions:default": [],
1827
}),
28+
deps = [":instrument_hooks"],
1929
visibility = ["//visibility:public"],
2030
)
2131

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.1.1)
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: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,26 @@
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
#include "callgrind.h"
712

13+
extern "C" {
14+
#include "core.h"
15+
}
16+
17+
inline InstrumentHooks* get_hooks() {
18+
static InstrumentHooks* g_hooks = nullptr;
19+
if (!g_hooks) {
20+
g_hooks = instrument_hooks_init();
21+
}
22+
return g_hooks;
23+
}
24+
825
inline std::string get_version() {
926
#ifdef CODSPEED_VERSION
1027
return {CODSPEED_VERSION};
@@ -13,22 +30,40 @@ inline std::string get_version() {
1330
#endif
1431
}
1532

16-
inline bool measurement_is_instrumented() { return RUNNING_ON_VALGRIND; }
33+
inline pid_t current_pid() {
34+
#ifdef _WIN32
35+
return _getpid();
36+
#else
37+
return getpid();
38+
#endif
39+
}
40+
41+
inline bool measurement_is_instrumented() {
42+
return instrument_hooks_is_instrumented(get_hooks());
43+
}
1744

1845
inline void measurement_set_metadata() {
19-
std::string metadata = "Metadata: codspeed-cpp " + get_version();
20-
CALLGRIND_DUMP_STATS_AT(metadata.c_str());
46+
std::string version = get_version();
47+
instrument_hooks_set_integration(get_hooks(), "codspeed-cpp",
48+
version.c_str());
2149
}
2250

2351
__attribute__((always_inline)) inline void measurement_start() {
52+
// Keep the callgrind macros here, so that they are properly inlined.
53+
// Otherwise, we have an additional function call overhead to the
54+
// instrument-hooks library.
2455
CALLGRIND_ZERO_STATS;
2556
CALLGRIND_START_INSTRUMENTATION;
57+
58+
instrument_hooks_start_benchmark(get_hooks());
2659
}
2760

2861
__attribute__((always_inline)) inline void measurement_stop(
29-
const std::string &name) {
62+
const std::string& name) {
3063
CALLGRIND_STOP_INSTRUMENTATION;
31-
CALLGRIND_DUMP_STATS_AT(name.c_str());
64+
65+
instrument_hooks_stop_benchmark(get_hooks());
66+
instrument_hooks_executed_benchmark(get_hooks(), current_pid(), name.c_str());
3267
};
3368

3469
#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)