Skip to content

Commit c897b7b

Browse files
committed
feat: add support for benchmark markers
1 parent 33eaedb commit c897b7b

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,9 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State {
949949
#if defined(CODSPEED_INSTRUMENTATION) || defined(CODSPEED_WALLTIME)
950950
codspeed::CodSpeed* codspeed_;
951951
#endif
952+
#ifdef CODSPEED_WALLTIME
953+
uint64_t resume_timestamp_;
954+
#endif
952955

953956
private:
954957
bool started_;
@@ -1044,11 +1047,13 @@ struct State::StateIterator {
10441047
private:
10451048
friend class State;
10461049
BENCHMARK_ALWAYS_INLINE
1047-
StateIterator() : cached_(0), parent_() {}
1050+
StateIterator() : cached_(0), parent_()
1051+
{}
10481052

10491053
BENCHMARK_ALWAYS_INLINE
10501054
explicit StateIterator(State* st)
1051-
: cached_(st->skipped() ? 0 : st->max_iterations), parent_(st) {}
1055+
: cached_(st->skipped() ? 0 : st->max_iterations), parent_(st)
1056+
{}
10521057

10531058
public:
10541059
BENCHMARK_ALWAYS_INLINE
@@ -1063,7 +1068,9 @@ struct State::StateIterator {
10631068

10641069
BENCHMARK_ALWAYS_INLINE
10651070
bool operator!=(StateIterator const&) const {
1066-
if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) return true;
1071+
if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) {
1072+
return true;
1073+
}
10671074
#ifdef CODSPEED_INSTRUMENTATION
10681075
measurement_stop();
10691076
#endif

google_benchmark/src/benchmark.cc

Lines changed: 24 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,9 @@ 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+
#endif
193+
#ifdef CODSPEED_WALLTIME
194+
resume_timestamp_(0),
188195
#endif
189196
started_(false),
190197
finished_(false),
@@ -252,9 +259,21 @@ State::State(std::string name, IterationCount max_iters,
252259
}
253260

254261
void State::PauseTiming() {
262+
#ifdef CODSPEED_WALLTIME
263+
uint64_t pause_timestamp = measurement_current_timestamp();
264+
#endif
265+
255266
// Add in time accumulated so far
256267
BM_CHECK(started_ && !finished_ && !skipped());
257268
timer_->StopTimer();
269+
270+
#ifdef CODSPEED_WALLTIME
271+
if (resume_timestamp_ != 0) {
272+
measurement_add_benchmark_timestamps(resume_timestamp_, pause_timestamp);
273+
resume_timestamp_ = 0;
274+
}
275+
#endif
276+
258277
if (perf_counters_measurement_ != nullptr) {
259278
std::vector<std::pair<std::string, double>> measurements;
260279
if (!perf_counters_measurement_->Stop(measurements)) {
@@ -276,6 +295,11 @@ void State::ResumeTiming() {
276295
if (perf_counters_measurement_ != nullptr) {
277296
perf_counters_measurement_->Start();
278297
}
298+
299+
#ifdef CODSPEED_WALLTIME
300+
BM_CHECK(resume_timestamp_ == 0);
301+
resume_timestamp_ = measurement_current_timestamp();
302+
#endif
279303
}
280304

281305
void State::SkipWithMessage(const std::string& msg) {

0 commit comments

Comments
 (0)