Skip to content

Commit ddf6425

Browse files
committed
feat: add support for benchmark markers
1 parent dcb45c2 commit ddf6425

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

core/include/measurement.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef MEASUREMENT_H
22
#define MEASUREMENT_H
33

4+
#include <cassert>
45
#include <string>
56
#ifdef _WIN32
67
#include <process.h>
@@ -49,4 +50,23 @@ ALWAYS_INLINE void measurement_set_executed_benchmark(const std::string& name) {
4950
instrument_hooks_executed_benchmark(g_hooks, current_pid, name.c_str());
5051
}
5152

53+
ALWAYS_INLINE uint64_t measurement_current_timestamp() {
54+
return instrument_hooks_current_timestamp();
55+
}
56+
57+
ALWAYS_INLINE int8_t measurement_add_marker(uint8_t marker_type,
58+
uint64_t timestamp) {
59+
auto pid = getpid();
60+
return instrument_hooks_add_marker(g_hooks, pid, marker_type, timestamp);
61+
}
62+
63+
ALWAYS_INLINE void measurement_add_benchmark_timestamps(uint64_t start,
64+
uint64_t end) {
65+
assert(start <= end);
66+
assert(start != 0 && end != 0);
67+
68+
measurement_add_marker(MARKER_TYPE_BENCHMARK_START, start);
69+
measurement_add_marker(MARKER_TYPE_BENCHMARK_END, end);
70+
}
71+
5272
#endif // MEASUREMENT_H

google_benchmark/include/benchmark/benchmark.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State {
948948
const IterationCount max_iterations;
949949
#if defined(CODSPEED_INSTRUMENTATION) || defined(CODSPEED_WALLTIME)
950950
codspeed::CodSpeed* codspeed_;
951+
uint64_t resume_timestamp_;
951952
#endif
952953

953954
private:
@@ -1044,11 +1045,13 @@ struct State::StateIterator {
10441045
private:
10451046
friend class State;
10461047
BENCHMARK_ALWAYS_INLINE
1047-
StateIterator() : cached_(0), parent_() {}
1048+
StateIterator() : cached_(0), parent_()
1049+
{}
10481050

10491051
BENCHMARK_ALWAYS_INLINE
10501052
explicit StateIterator(State* st)
1051-
: cached_(st->skipped() ? 0 : st->max_iterations), parent_(st) {}
1053+
: cached_(st->skipped() ? 0 : st->max_iterations), parent_(st)
1054+
{}
10521055

10531056
public:
10541057
BENCHMARK_ALWAYS_INLINE
@@ -1063,7 +1066,9 @@ struct State::StateIterator {
10631066

10641067
BENCHMARK_ALWAYS_INLINE
10651068
bool operator!=(StateIterator const&) const {
1066-
if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) return true;
1069+
if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) {
1070+
return true;
1071+
}
10671072
#ifdef CODSPEED_INSTRUMENTATION
10681073
measurement_stop();
10691074
#endif

google_benchmark/src/benchmark.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "codspeed.h"
2020
#include "internal_macros.h"
2121

22+
#ifdef CODSPEED_WALLTIME
23+
#include "measurement.hpp"
24+
#endif
25+
2226
#ifndef BENCHMARK_OS_WINDOWS
2327
#if !defined(BENCHMARK_OS_FUCHSIA) && !defined(BENCHMARK_OS_QURT)
2428
#include <sys/resource.h>
@@ -185,6 +189,7 @@ State::State(std::string name, IterationCount max_iters,
185189
max_iterations(max_iters),
186190
#if defined(CODSPEED_INSTRUMENTATION) || defined(CODSPEED_WALLTIME)
187191
codspeed_(codspeed),
192+
resume_timestamp_(0),
188193
#endif
189194
started_(false),
190195
finished_(false),
@@ -255,6 +260,15 @@ void State::PauseTiming() {
255260
// Add in time accumulated so far
256261
BM_CHECK(started_ && !finished_ && !skipped());
257262
timer_->StopTimer();
263+
264+
#ifdef CODSPEED_WALLTIME
265+
if (resume_timestamp_ != 0) {
266+
uint64_t pause_timestamp = measurement_current_timestamp();
267+
measurement_add_benchmark_timestamps(resume_timestamp_, pause_timestamp);
268+
resume_timestamp_ = 0;
269+
}
270+
#endif
271+
258272
if (perf_counters_measurement_ != nullptr) {
259273
std::vector<std::pair<std::string, double>> measurements;
260274
if (!perf_counters_measurement_->Stop(measurements)) {
@@ -271,6 +285,11 @@ void State::PauseTiming() {
271285
}
272286

273287
void State::ResumeTiming() {
288+
#ifdef CODSPEED_WALLTIME
289+
BM_CHECK(resume_timestamp_ == 0);
290+
resume_timestamp_ = measurement_current_timestamp();
291+
#endif
292+
274293
BM_CHECK(started_ && !finished_ && !skipped());
275294
timer_->StartTimer();
276295
if (perf_counters_measurement_ != nullptr) {

0 commit comments

Comments
 (0)