Include the benchmark's family-name in State (#1511)

* Include the benchmark's family-name in State

For compat with internal library, where State::name() returns the benchmark's family name.

* added missing files from prev commit

* fix field-init order error

* added test
This commit is contained in:
Vy Nguyen 2023-01-10 11:48:17 -05:00 committed by GitHub
parent fe65457e80
commit a3235d7b69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 12 deletions

View File

@ -825,6 +825,9 @@ class BENCHMARK_EXPORT State {
return max_iterations - total_iterations_ + batch_leftover_;
}
BENCHMARK_ALWAYS_INLINE
std::string name() const { return name_; }
private:
// items we expect on the first cache line (ie 64 bytes of the struct)
// When total_iterations_ is 0, KeepRunning() and friends will return false.
@ -854,9 +857,9 @@ class BENCHMARK_EXPORT State {
UserCounters counters;
private:
State(IterationCount max_iters, const std::vector<int64_t>& ranges,
int thread_i, int n_threads, internal::ThreadTimer* timer,
internal::ThreadManager* manager,
State(std::string name, IterationCount max_iters,
const std::vector<int64_t>& ranges, int thread_i, int n_threads,
internal::ThreadTimer* timer, internal::ThreadManager* manager,
internal::PerfCountersMeasurement* perf_counters_measurement);
void StartKeepRunning();
@ -865,6 +868,7 @@ class BENCHMARK_EXPORT State {
bool KeepRunningInternal(IterationCount n, bool is_batch);
void FinishKeepRunning();
const std::string name_;
const int thread_index_;
const int threads_;

View File

@ -148,9 +148,9 @@ void UseCharPointer(char const volatile*) {}
} // namespace internal
State::State(IterationCount max_iters, const std::vector<int64_t>& ranges,
int thread_i, int n_threads, internal::ThreadTimer* timer,
internal::ThreadManager* manager,
State::State(std::string name, IterationCount max_iters,
const std::vector<int64_t>& ranges, int thread_i, int n_threads,
internal::ThreadTimer* timer, internal::ThreadManager* manager,
internal::PerfCountersMeasurement* perf_counters_measurement)
: total_iterations_(0),
batch_leftover_(0),
@ -160,6 +160,7 @@ State::State(IterationCount max_iters, const std::vector<int64_t>& ranges,
error_occurred_(false),
range_(ranges),
complexity_n_(0),
name_(std::move(name)),
thread_index_(thread_i),
threads_(n_threads),
timer_(timer),

View File

@ -93,24 +93,24 @@ State BenchmarkInstance::Run(
IterationCount iters, int thread_id, internal::ThreadTimer* timer,
internal::ThreadManager* manager,
internal::PerfCountersMeasurement* perf_counters_measurement) const {
State st(iters, args_, thread_id, threads_, timer, manager,
perf_counters_measurement);
State st(name_.function_name, iters, args_, thread_id, threads_, timer,
manager, perf_counters_measurement);
benchmark_.Run(st);
return st;
}
void BenchmarkInstance::Setup() const {
if (setup_) {
State st(/*iters*/ 1, args_, /*thread_id*/ 0, threads_, nullptr, nullptr,
nullptr);
State st(name_.function_name, /*iters*/ 1, args_, /*thread_id*/ 0, threads_,
nullptr, nullptr, nullptr);
setup_(st);
}
}
void BenchmarkInstance::Teardown() const {
if (teardown_) {
State st(/*iters*/ 1, args_, /*thread_id*/ 0, threads_, nullptr, nullptr,
nullptr);
State st(name_.function_name, /*iters*/ 1, args_, /*thread_id*/ 0, threads_,
nullptr, nullptr, nullptr);
teardown_(st);
}
}

View File

@ -245,4 +245,13 @@ BENCHMARK(BM_DenseThreadRanges)->Arg(1)->DenseThreadRange(1, 3);
BENCHMARK(BM_DenseThreadRanges)->Arg(2)->DenseThreadRange(1, 4, 2);
BENCHMARK(BM_DenseThreadRanges)->Arg(3)->DenseThreadRange(5, 14, 3);
static void BM_BenchmarkName(benchmark::State& state) {
for (auto _ : state) {
}
// Check that the benchmark name is passed correctly to `state`.
assert("BM_BenchmarkName" == state.name());
}
BENCHMARK(BM_BenchmarkName);
BENCHMARK_MAIN();