Skip to content

Commit b616032

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

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
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/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ 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

1415
# Add the library
1516
add_library(
@@ -18,6 +19,7 @@ add_library(
1819
src/walltime.cpp
1920
src/uri.cpp
2021
src/workspace.cpp
22+
instrument-hooks/dist/core.c
2123
)
2224

2325
# Version
@@ -27,6 +29,7 @@ add_compile_definitions(CODSPEED_VERSION="${CODSPEED_VERSION}")
2729
target_include_directories(
2830
codspeed
2931
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
32+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/instrument-hooks/includes>
3033
)
3134

3235
# Disable valgrind compilation errors

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

0 commit comments

Comments
 (0)