[ELF] Reorder SectionBase/InputSectionBase members
[llvm-project.git] / third-party / benchmark / src / benchmark_register.cc
blobe447c9a2d39ba40e44a158fb4d1c39e6563baa22
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "benchmark_register.h"
17 #ifndef BENCHMARK_OS_WINDOWS
18 #if !defined(BENCHMARK_OS_FUCHSIA) && !defined(BENCHMARK_OS_QURT)
19 #include <sys/resource.h>
20 #endif
21 #include <sys/time.h>
22 #include <unistd.h>
23 #endif
25 #include <algorithm>
26 #include <atomic>
27 #include <cinttypes>
28 #include <condition_variable>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <cstring>
32 #include <fstream>
33 #include <iostream>
34 #include <memory>
35 #include <numeric>
36 #include <sstream>
37 #include <thread>
39 #include "benchmark/benchmark.h"
40 #include "benchmark_api_internal.h"
41 #include "check.h"
42 #include "commandlineflags.h"
43 #include "complexity.h"
44 #include "internal_macros.h"
45 #include "log.h"
46 #include "mutex.h"
47 #include "re.h"
48 #include "statistics.h"
49 #include "string_util.h"
50 #include "timers.h"
52 namespace benchmark {
54 namespace {
55 // For non-dense Range, intermediate values are powers of kRangeMultiplier.
56 static constexpr int kRangeMultiplier = 8;
58 // The size of a benchmark family determines is the number of inputs to repeat
59 // the benchmark on. If this is "large" then warn the user during configuration.
60 static constexpr size_t kMaxFamilySize = 100;
62 static constexpr char kDisabledPrefix[] = "DISABLED_";
63 } // end namespace
65 namespace internal {
67 //=============================================================================//
68 // BenchmarkFamilies
69 //=============================================================================//
71 // Class for managing registered benchmarks. Note that each registered
72 // benchmark identifies a family of related benchmarks to run.
73 class BenchmarkFamilies {
74 public:
75 static BenchmarkFamilies* GetInstance();
77 // Registers a benchmark family and returns the index assigned to it.
78 size_t AddBenchmark(std::unique_ptr<Benchmark> family);
80 // Clear all registered benchmark families.
81 void ClearBenchmarks();
83 // Extract the list of benchmark instances that match the specified
84 // regular expression.
85 bool FindBenchmarks(std::string re,
86 std::vector<BenchmarkInstance>* benchmarks,
87 std::ostream* Err);
89 private:
90 BenchmarkFamilies() {}
92 std::vector<std::unique_ptr<Benchmark>> families_;
93 Mutex mutex_;
96 BenchmarkFamilies* BenchmarkFamilies::GetInstance() {
97 static BenchmarkFamilies instance;
98 return &instance;
101 size_t BenchmarkFamilies::AddBenchmark(std::unique_ptr<Benchmark> family) {
102 MutexLock l(mutex_);
103 size_t index = families_.size();
104 families_.push_back(std::move(family));
105 return index;
108 void BenchmarkFamilies::ClearBenchmarks() {
109 MutexLock l(mutex_);
110 families_.clear();
111 families_.shrink_to_fit();
114 bool BenchmarkFamilies::FindBenchmarks(
115 std::string spec, std::vector<BenchmarkInstance>* benchmarks,
116 std::ostream* ErrStream) {
117 BM_CHECK(ErrStream);
118 auto& Err = *ErrStream;
119 // Make regular expression out of command-line flag
120 std::string error_msg;
121 Regex re;
122 bool is_negative_filter = false;
123 if (spec[0] == '-') {
124 spec.replace(0, 1, "");
125 is_negative_filter = true;
127 if (!re.Init(spec, &error_msg)) {
128 Err << "Could not compile benchmark re: " << error_msg << std::endl;
129 return false;
132 // Special list of thread counts to use when none are specified
133 const std::vector<int> one_thread = {1};
135 int next_family_index = 0;
137 MutexLock l(mutex_);
138 for (std::unique_ptr<Benchmark>& family : families_) {
139 int family_index = next_family_index;
140 int per_family_instance_index = 0;
142 // Family was deleted or benchmark doesn't match
143 if (!family) continue;
145 if (family->ArgsCnt() == -1) {
146 family->Args({});
148 const std::vector<int>* thread_counts =
149 (family->thread_counts_.empty()
150 ? &one_thread
151 : &static_cast<const std::vector<int>&>(family->thread_counts_));
152 const size_t family_size = family->args_.size() * thread_counts->size();
153 // The benchmark will be run at least 'family_size' different inputs.
154 // If 'family_size' is very large warn the user.
155 if (family_size > kMaxFamilySize) {
156 Err << "The number of inputs is very large. " << family->name_
157 << " will be repeated at least " << family_size << " times.\n";
159 // reserve in the special case the regex ".", since we know the final
160 // family size. this doesn't take into account any disabled benchmarks
161 // so worst case we reserve more than we need.
162 if (spec == ".") benchmarks->reserve(benchmarks->size() + family_size);
164 for (auto const& args : family->args_) {
165 for (int num_threads : *thread_counts) {
166 BenchmarkInstance instance(family.get(), family_index,
167 per_family_instance_index, args,
168 num_threads);
170 const auto full_name = instance.name().str();
171 if (full_name.rfind(kDisabledPrefix, 0) != 0 &&
172 ((re.Match(full_name) && !is_negative_filter) ||
173 (!re.Match(full_name) && is_negative_filter))) {
174 benchmarks->push_back(std::move(instance));
176 ++per_family_instance_index;
178 // Only bump the next family index once we've estabilished that
179 // at least one instance of this family will be run.
180 if (next_family_index == family_index) ++next_family_index;
185 return true;
188 Benchmark* RegisterBenchmarkInternal(Benchmark* bench) {
189 std::unique_ptr<Benchmark> bench_ptr(bench);
190 BenchmarkFamilies* families = BenchmarkFamilies::GetInstance();
191 families->AddBenchmark(std::move(bench_ptr));
192 return bench;
195 // FIXME: This function is a hack so that benchmark.cc can access
196 // `BenchmarkFamilies`
197 bool FindBenchmarksInternal(const std::string& re,
198 std::vector<BenchmarkInstance>* benchmarks,
199 std::ostream* Err) {
200 return BenchmarkFamilies::GetInstance()->FindBenchmarks(re, benchmarks, Err);
203 //=============================================================================//
204 // Benchmark
205 //=============================================================================//
207 Benchmark::Benchmark(const std::string& name)
208 : name_(name),
209 aggregation_report_mode_(ARM_Unspecified),
210 time_unit_(GetDefaultTimeUnit()),
211 use_default_time_unit_(true),
212 range_multiplier_(kRangeMultiplier),
213 min_time_(0),
214 min_warmup_time_(0),
215 iterations_(0),
216 repetitions_(0),
217 measure_process_cpu_time_(false),
218 use_real_time_(false),
219 use_manual_time_(false),
220 complexity_(oNone),
221 complexity_lambda_(nullptr),
222 setup_(nullptr),
223 teardown_(nullptr) {
224 ComputeStatistics("mean", StatisticsMean);
225 ComputeStatistics("median", StatisticsMedian);
226 ComputeStatistics("stddev", StatisticsStdDev);
227 ComputeStatistics("cv", StatisticsCV, kPercentage);
230 Benchmark::~Benchmark() {}
232 Benchmark* Benchmark::Name(const std::string& name) {
233 SetName(name);
234 return this;
237 Benchmark* Benchmark::Arg(int64_t x) {
238 BM_CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
239 args_.push_back({x});
240 return this;
243 Benchmark* Benchmark::Unit(TimeUnit unit) {
244 time_unit_ = unit;
245 use_default_time_unit_ = false;
246 return this;
249 Benchmark* Benchmark::Range(int64_t start, int64_t limit) {
250 BM_CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
251 std::vector<int64_t> arglist;
252 AddRange(&arglist, start, limit, range_multiplier_);
254 for (int64_t i : arglist) {
255 args_.push_back({i});
257 return this;
260 Benchmark* Benchmark::Ranges(
261 const std::vector<std::pair<int64_t, int64_t>>& ranges) {
262 BM_CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast<int>(ranges.size()));
263 std::vector<std::vector<int64_t>> arglists(ranges.size());
264 for (std::size_t i = 0; i < ranges.size(); i++) {
265 AddRange(&arglists[i], ranges[i].first, ranges[i].second,
266 range_multiplier_);
269 ArgsProduct(arglists);
271 return this;
274 Benchmark* Benchmark::ArgsProduct(
275 const std::vector<std::vector<int64_t>>& arglists) {
276 BM_CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast<int>(arglists.size()));
278 std::vector<std::size_t> indices(arglists.size());
279 const std::size_t total = std::accumulate(
280 std::begin(arglists), std::end(arglists), std::size_t{1},
281 [](const std::size_t res, const std::vector<int64_t>& arglist) {
282 return res * arglist.size();
284 std::vector<int64_t> args;
285 args.reserve(arglists.size());
286 for (std::size_t i = 0; i < total; i++) {
287 for (std::size_t arg = 0; arg < arglists.size(); arg++) {
288 args.push_back(arglists[arg][indices[arg]]);
290 args_.push_back(args);
291 args.clear();
293 std::size_t arg = 0;
294 do {
295 indices[arg] = (indices[arg] + 1) % arglists[arg].size();
296 } while (indices[arg++] == 0 && arg < arglists.size());
299 return this;
302 Benchmark* Benchmark::ArgName(const std::string& name) {
303 BM_CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
304 arg_names_ = {name};
305 return this;
308 Benchmark* Benchmark::ArgNames(const std::vector<std::string>& names) {
309 BM_CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast<int>(names.size()));
310 arg_names_ = names;
311 return this;
314 Benchmark* Benchmark::DenseRange(int64_t start, int64_t limit, int step) {
315 BM_CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
316 BM_CHECK_LE(start, limit);
317 for (int64_t arg = start; arg <= limit; arg += step) {
318 args_.push_back({arg});
320 return this;
323 Benchmark* Benchmark::Args(const std::vector<int64_t>& args) {
324 BM_CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast<int>(args.size()));
325 args_.push_back(args);
326 return this;
329 Benchmark* Benchmark::Apply(void (*custom_arguments)(Benchmark* benchmark)) {
330 custom_arguments(this);
331 return this;
334 Benchmark* Benchmark::Setup(void (*setup)(const benchmark::State&)) {
335 BM_CHECK(setup != nullptr);
336 setup_ = setup;
337 return this;
340 Benchmark* Benchmark::Teardown(void (*teardown)(const benchmark::State&)) {
341 BM_CHECK(teardown != nullptr);
342 teardown_ = teardown;
343 return this;
346 Benchmark* Benchmark::RangeMultiplier(int multiplier) {
347 BM_CHECK(multiplier > 1);
348 range_multiplier_ = multiplier;
349 return this;
352 Benchmark* Benchmark::MinTime(double t) {
353 BM_CHECK(t > 0.0);
354 BM_CHECK(iterations_ == 0);
355 min_time_ = t;
356 return this;
359 Benchmark* Benchmark::MinWarmUpTime(double t) {
360 BM_CHECK(t >= 0.0);
361 BM_CHECK(iterations_ == 0);
362 min_warmup_time_ = t;
363 return this;
366 Benchmark* Benchmark::Iterations(IterationCount n) {
367 BM_CHECK(n > 0);
368 BM_CHECK(IsZero(min_time_));
369 BM_CHECK(IsZero(min_warmup_time_));
370 iterations_ = n;
371 return this;
374 Benchmark* Benchmark::Repetitions(int n) {
375 BM_CHECK(n > 0);
376 repetitions_ = n;
377 return this;
380 Benchmark* Benchmark::ReportAggregatesOnly(bool value) {
381 aggregation_report_mode_ = value ? ARM_ReportAggregatesOnly : ARM_Default;
382 return this;
385 Benchmark* Benchmark::DisplayAggregatesOnly(bool value) {
386 // If we were called, the report mode is no longer 'unspecified', in any case.
387 aggregation_report_mode_ = static_cast<AggregationReportMode>(
388 aggregation_report_mode_ | ARM_Default);
390 if (value) {
391 aggregation_report_mode_ = static_cast<AggregationReportMode>(
392 aggregation_report_mode_ | ARM_DisplayReportAggregatesOnly);
393 } else {
394 aggregation_report_mode_ = static_cast<AggregationReportMode>(
395 aggregation_report_mode_ & ~ARM_DisplayReportAggregatesOnly);
398 return this;
401 Benchmark* Benchmark::MeasureProcessCPUTime() {
402 // Can be used together with UseRealTime() / UseManualTime().
403 measure_process_cpu_time_ = true;
404 return this;
407 Benchmark* Benchmark::UseRealTime() {
408 BM_CHECK(!use_manual_time_)
409 << "Cannot set UseRealTime and UseManualTime simultaneously.";
410 use_real_time_ = true;
411 return this;
414 Benchmark* Benchmark::UseManualTime() {
415 BM_CHECK(!use_real_time_)
416 << "Cannot set UseRealTime and UseManualTime simultaneously.";
417 use_manual_time_ = true;
418 return this;
421 Benchmark* Benchmark::Complexity(BigO complexity) {
422 complexity_ = complexity;
423 return this;
426 Benchmark* Benchmark::Complexity(BigOFunc* complexity) {
427 complexity_lambda_ = complexity;
428 complexity_ = oLambda;
429 return this;
432 Benchmark* Benchmark::ComputeStatistics(const std::string& name,
433 StatisticsFunc* statistics,
434 StatisticUnit unit) {
435 statistics_.emplace_back(name, statistics, unit);
436 return this;
439 Benchmark* Benchmark::Threads(int t) {
440 BM_CHECK_GT(t, 0);
441 thread_counts_.push_back(t);
442 return this;
445 Benchmark* Benchmark::ThreadRange(int min_threads, int max_threads) {
446 BM_CHECK_GT(min_threads, 0);
447 BM_CHECK_GE(max_threads, min_threads);
449 AddRange(&thread_counts_, min_threads, max_threads, 2);
450 return this;
453 Benchmark* Benchmark::DenseThreadRange(int min_threads, int max_threads,
454 int stride) {
455 BM_CHECK_GT(min_threads, 0);
456 BM_CHECK_GE(max_threads, min_threads);
457 BM_CHECK_GE(stride, 1);
459 for (auto i = min_threads; i < max_threads; i += stride) {
460 thread_counts_.push_back(i);
462 thread_counts_.push_back(max_threads);
463 return this;
466 Benchmark* Benchmark::ThreadPerCpu() {
467 thread_counts_.push_back(CPUInfo::Get().num_cpus);
468 return this;
471 void Benchmark::SetName(const std::string& name) { name_ = name; }
473 const char* Benchmark::GetName() const { return name_.c_str(); }
475 int Benchmark::ArgsCnt() const {
476 if (args_.empty()) {
477 if (arg_names_.empty()) return -1;
478 return static_cast<int>(arg_names_.size());
480 return static_cast<int>(args_.front().size());
483 const char* Benchmark::GetArgName(int arg) const {
484 BM_CHECK_GE(arg, 0);
485 BM_CHECK_LT(arg, static_cast<int>(arg_names_.size()));
486 return arg_names_[arg].c_str();
489 TimeUnit Benchmark::GetTimeUnit() const {
490 return use_default_time_unit_ ? GetDefaultTimeUnit() : time_unit_;
493 //=============================================================================//
494 // FunctionBenchmark
495 //=============================================================================//
497 void FunctionBenchmark::Run(State& st) { func_(st); }
499 } // end namespace internal
501 void ClearRegisteredBenchmarks() {
502 internal::BenchmarkFamilies::GetInstance()->ClearBenchmarks();
505 std::vector<int64_t> CreateRange(int64_t lo, int64_t hi, int multi) {
506 std::vector<int64_t> args;
507 internal::AddRange(&args, lo, hi, multi);
508 return args;
511 std::vector<int64_t> CreateDenseRange(int64_t start, int64_t limit, int step) {
512 BM_CHECK_LE(start, limit);
513 std::vector<int64_t> args;
514 for (int64_t arg = start; arg <= limit; arg += step) {
515 args.push_back(arg);
517 return args;
520 } // end namespace benchmark