Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,11 @@ If set, the `ProfilerManager::AfterSetupStart` and
end of a separate benchmark run to allow user code to collect and report
user-provided profile metrics.

From within either hook, `ProfilerManager::GetState()` returns a pointer to
the `benchmark::State` of the run being profiled, which gives access to run
information such as the benchmark name (e.g. for registering named profiling
regions). Outside of the two hooks it returns `nullptr`.

Output collected from this profiling run must be reported separately.

<a name="using-register-benchmark" />
Expand Down
13 changes: 13 additions & 0 deletions include/benchmark/managers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace benchmark {

class State;

class MemoryManager {
public:
static constexpr int64_t TombstoneValue = std::numeric_limits<int64_t>::max();
Expand Down Expand Up @@ -56,6 +58,17 @@ class ProfilerManager {
virtual ~ProfilerManager() {}
virtual void AfterSetupStart() = 0;
virtual void BeforeTeardownStop() = 0;

protected:
// The State of the benchmark run being profiled, giving access to e.g. the
// benchmark name (for a named profiling region). Non-null only while
// AfterSetupStart() or BeforeTeardownStop() is executing; any other caller
// observes nullptr.
const State* GetState() const { return state_; }

private:
friend class State;
const State* state_ = nullptr;
};

BENCHMARK_EXPORT
Expand Down
4 changes: 4 additions & 0 deletions src/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ void State::StartKeepRunning() {
started_ = true;
total_iterations_ = skipped() ? 0 : max_iterations;
if (BENCHMARK_BUILTIN_EXPECT(profiler_manager_ != nullptr, false)) {
profiler_manager_->state_ = this;
profiler_manager_->AfterSetupStart();
profiler_manager_->state_ = nullptr;
}
manager_->StartStopBarrier();
if (!skipped()) {
Expand All @@ -349,7 +351,9 @@ void State::FinishKeepRunning() {
finished_ = true;
manager_->StartStopBarrier();
if (BENCHMARK_BUILTIN_EXPECT(profiler_manager_ != nullptr, false)) {
profiler_manager_->state_ = this;
profiler_manager_->BeforeTeardownStop();
profiler_manager_->state_ = nullptr;
}
}

Expand Down
16 changes: 15 additions & 1 deletion test/profiler_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cassert>
#include <memory>
#include <string>

#include "benchmark/benchmark_api.h"
#include "benchmark/managers.h"
Expand All @@ -13,11 +14,21 @@
namespace {
class TestProfilerManager : public benchmark::ProfilerManager {
public:
void AfterSetupStart() override { ++start_called; }
void AfterSetupStart() override {
++start_called;
if (GetState() != nullptr) {
benchmark_name = GetState()->name();
}
}
void BeforeTeardownStop() override { ++stop_called; }

// Expose the protected accessor so the harness can verify it yields
// nullptr outside the hooks.
const benchmark::State* StateOutsideHooks() const { return GetState(); }

int start_called = 0;
int stop_called = 0;
std::string benchmark_name;
};

void BM_empty(benchmark::State& state) {
Expand Down Expand Up @@ -55,4 +66,7 @@ int main(int argc, char* argv[]) {

assert(pm->start_called == 1);
assert(pm->stop_called == 1);
assert(pm->benchmark_name == "BM_empty");
// Outside the hooks the state must not be observable.
assert(pm->StateOutsideHooks() == nullptr);
}