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.
14
15#include "benchmark_register.h"
16
17#ifndef BENCHMARK_OS_WINDOWS
18#ifndef BENCHMARK_OS_FUCHSIA
19#include <sys/resource.h>
20#endif
21#include <sys/time.h>
22#include <unistd.h>
23#endif
24
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>
38
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"
51
52namespace benchmark {
53
54namespace {
55// For non-dense Range, intermediate values are powers of kRangeMultiplier.
56static const int kRangeMultiplier = 8;
57// The size of a benchmark family determines is the number of inputs to repeat
58// the benchmark on. If this is "large" then warn the user during configuration.
59static const size_t kMaxFamilySize = 100;
60} // end namespace
61
62namespace internal {
63
64//=============================================================================//
65// BenchmarkFamilies
66//=============================================================================//
67
68// Class for managing registered benchmarks. Note that each registered
69// benchmark identifies a family of related benchmarks to run.
70class BenchmarkFamilies {
71 public:
72 static BenchmarkFamilies* GetInstance();
73
74 // Registers a benchmark family and returns the index assigned to it.
75 size_t AddBenchmark(std::unique_ptr<Benchmark> family);
76
77 // Clear all registered benchmark families.
78 void ClearBenchmarks();
79
80 // Extract the list of benchmark instances that match the specified
81 // regular expression.
82 bool FindBenchmarks(std::string re,
83 std::vector<BenchmarkInstance>* benchmarks,
84 std::ostream* Err);
85
86 private:
87 BenchmarkFamilies() {}
88
89 std::vector<std::unique_ptr<Benchmark>> families_;
90 Mutex mutex_;
91};
92
93BenchmarkFamilies* BenchmarkFamilies::GetInstance() {
94 static BenchmarkFamilies instance;
95 return &instance;
96}
97
98size_t BenchmarkFamilies::AddBenchmark(std::unique_ptr<Benchmark> family) {
99 MutexLock l(mutex_);
100 size_t index = families_.size();
101 families_.push_back(std::move(family));
102 return index;
103}
104
105void BenchmarkFamilies::ClearBenchmarks() {
106 MutexLock l(mutex_);
107 families_.clear();
108 families_.shrink_to_fit();
109}
110
111bool BenchmarkFamilies::FindBenchmarks(
112 std::string spec, std::vector<BenchmarkInstance>* benchmarks,
113 std::ostream* ErrStream) {
114 CHECK(ErrStream);
115 auto& Err = *ErrStream;
116 // Make regular expression out of command-line flag
117 std::string error_msg;
118 Regex re;
119 bool isNegativeFilter = false;
120 if (spec[0] == '-') {
121 spec.replace(0, 1, "");
122 isNegativeFilter = true;
123 }
124 if (!re.Init(spec, &error_msg)) {
125 Err << "Could not compile benchmark re: " << error_msg << std::endl;
126 return false;
127 }
128
129 // Special list of thread counts to use when none are specified
130 const std::vector<int> one_thread = {1};
131
132 MutexLock l(mutex_);
133 for (std::unique_ptr<Benchmark>& family : families_) {
134 // Family was deleted or benchmark doesn't match
135 if (!family) continue;
136
137 if (family->ArgsCnt() == -1) {
138 family->Args({});
139 }
140 const std::vector<int>* thread_counts =
141 (family->thread_counts_.empty()
142 ? &one_thread
143 : &static_cast<const std::vector<int>&>(family->thread_counts_));
144 const size_t family_size = family->args_.size() * thread_counts->size();
145 // The benchmark will be run at least 'family_size' different inputs.
146 // If 'family_size' is very large warn the user.
147 if (family_size > kMaxFamilySize) {
148 Err << "The number of inputs is very large. " << family->name_
149 << " will be repeated at least " << family_size << " times.\n";
150 }
151 // reserve in the special case the regex ".", since we know the final
152 // family size.
153 if (spec == ".") benchmarks->reserve(family_size);
154
155 for (auto const& args : family->args_) {
156 for (int num_threads : *thread_counts) {
157 BenchmarkInstance instance(family.get(), args, num_threads);
158
159 const auto full_name = instance.name().str();
160 if ((re.Match(full_name) && !isNegativeFilter) ||
161 (!re.Match(full_name) && isNegativeFilter)) {
162 instance.last_benchmark_instance = (&args == &family->args_.back());
163 benchmarks->push_back(std::move(instance));
164 }
165 }
166 }
167 }
168 return true;
169}
170
171Benchmark* RegisterBenchmarkInternal(Benchmark* bench) {
172 std::unique_ptr<Benchmark> bench_ptr(bench);
173 BenchmarkFamilies* families = BenchmarkFamilies::GetInstance();
174 families->AddBenchmark(std::move(bench_ptr));
175 return bench;
176}
177
178// FIXME: This function is a hack so that benchmark.cc can access
179// `BenchmarkFamilies`
180bool FindBenchmarksInternal(const std::string& re,
181 std::vector<BenchmarkInstance>* benchmarks,
182 std::ostream* Err) {
183 return BenchmarkFamilies::GetInstance()->FindBenchmarks(re, benchmarks, Err);
184}
185
186//=============================================================================//
187// Benchmark
188//=============================================================================//
189
190Benchmark::Benchmark(const char* name)
191 : name_(name),
192 aggregation_report_mode_(ARM_Unspecified),
193 time_unit_(kNanosecond),
194 range_multiplier_(kRangeMultiplier),
195 min_time_(0),
196 iterations_(0),
197 repetitions_(0),
198 measure_process_cpu_time_(false),
199 use_real_time_(false),
200 use_manual_time_(false),
201 complexity_(oNone),
202 complexity_lambda_(nullptr) {
203 ComputeStatistics("mean", StatisticsMean);
204 ComputeStatistics("median", StatisticsMedian);
205 ComputeStatistics("stddev", StatisticsStdDev);
206}
207
208Benchmark::~Benchmark() {}
209
210Benchmark* Benchmark::Name(const std::string& name) {
211 SetName(name.c_str());
212 return this;
213}
214
215Benchmark* Benchmark::Arg(int64_t x) {
216 CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
217 args_.push_back({x});
218 return this;
219}
220
221Benchmark* Benchmark::Unit(TimeUnit unit) {
222 time_unit_ = unit;
223 return this;
224}
225
226Benchmark* Benchmark::Range(int64_t start, int64_t limit) {
227 CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
228 std::vector<int64_t> arglist;
229 AddRange(&arglist, start, limit, range_multiplier_);
230
231 for (int64_t i : arglist) {
232 args_.push_back({i});
233 }
234 return this;
235}
236
237Benchmark* Benchmark::Ranges(
238 const std::vector<std::pair<int64_t, int64_t>>& ranges) {
239 CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast<int>(ranges.size()));
240 std::vector<std::vector<int64_t>> arglists(ranges.size());
241 for (std::size_t i = 0; i < ranges.size(); i++) {
242 AddRange(&arglists[i], ranges[i].first, ranges[i].second,
243 range_multiplier_);
244 }
245
246 ArgsProduct(arglists);
247
248 return this;
249}
250
251Benchmark* Benchmark::ArgsProduct(
252 const std::vector<std::vector<int64_t>>& arglists) {
253 CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast<int>(arglists.size()));
254
255 std::vector<std::size_t> indices(arglists.size());
256 const std::size_t total = std::accumulate(
257 std::begin(arglists), std::end(arglists), std::size_t{1},
258 [](const std::size_t res, const std::vector<int64_t>& arglist) {
259 return res * arglist.size();
260 });
261 std::vector<int64_t> args;
262 args.reserve(arglists.size());
263 for (std::size_t i = 0; i < total; i++) {
264 for (std::size_t arg = 0; arg < arglists.size(); arg++) {
265 args.push_back(arglists[arg][indices[arg]]);
266 }
267 args_.push_back(args);
268 args.clear();
269
270 std::size_t arg = 0;
271 do {
272 indices[arg] = (indices[arg] + 1) % arglists[arg].size();
273 } while (indices[arg++] == 0 && arg < arglists.size());
274 }
275
276 return this;
277}
278
279Benchmark* Benchmark::ArgName(const std::string& name) {
280 CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
281 arg_names_ = {name};
282 return this;
283}
284
285Benchmark* Benchmark::ArgNames(const std::vector<std::string>& names) {
286 CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast<int>(names.size()));
287 arg_names_ = names;
288 return this;
289}
290
291Benchmark* Benchmark::DenseRange(int64_t start, int64_t limit, int step) {
292 CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
293 CHECK_LE(start, limit);
294 for (int64_t arg = start; arg <= limit; arg += step) {
295 args_.push_back({arg});
296 }
297 return this;
298}
299
300Benchmark* Benchmark::Args(const std::vector<int64_t>& args) {
301 CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast<int>(args.size()));
302 args_.push_back(args);
303 return this;
304}
305
306Benchmark* Benchmark::Apply(void (*custom_arguments)(Benchmark* benchmark)) {
307 custom_arguments(this);
308 return this;
309}
310
311Benchmark* Benchmark::RangeMultiplier(int multiplier) {
312 CHECK(multiplier > 1);
313 range_multiplier_ = multiplier;
314 return this;
315}
316
317Benchmark* Benchmark::MinTime(double t) {
318 CHECK(t > 0.0);
319 CHECK(iterations_ == 0);
320 min_time_ = t;
321 return this;
322}
323
324Benchmark* Benchmark::Iterations(IterationCount n) {
325 CHECK(n > 0);
326 CHECK(IsZero(min_time_));
327 iterations_ = n;
328 return this;
329}
330
331Benchmark* Benchmark::Repetitions(int n) {
332 CHECK(n > 0);
333 repetitions_ = n;
334 return this;
335}
336
337Benchmark* Benchmark::ReportAggregatesOnly(bool value) {
338 aggregation_report_mode_ = value ? ARM_ReportAggregatesOnly : ARM_Default;
339 return this;
340}
341
342Benchmark* Benchmark::DisplayAggregatesOnly(bool value) {
343 // If we were called, the report mode is no longer 'unspecified', in any case.
344 aggregation_report_mode_ = static_cast<AggregationReportMode>(
345 aggregation_report_mode_ | ARM_Default);
346
347 if (value) {
348 aggregation_report_mode_ = static_cast<AggregationReportMode>(
349 aggregation_report_mode_ | ARM_DisplayReportAggregatesOnly);
350 } else {
351 aggregation_report_mode_ = static_cast<AggregationReportMode>(
352 aggregation_report_mode_ & ~ARM_DisplayReportAggregatesOnly);
353 }
354
355 return this;
356}
357
358Benchmark* Benchmark::MeasureProcessCPUTime() {
359 // Can be used together with UseRealTime() / UseManualTime().
360 measure_process_cpu_time_ = true;
361 return this;
362}
363
364Benchmark* Benchmark::UseRealTime() {
365 CHECK(!use_manual_time_)
366 << "Cannot set UseRealTime and UseManualTime simultaneously.";
367 use_real_time_ = true;
368 return this;
369}
370
371Benchmark* Benchmark::UseManualTime() {
372 CHECK(!use_real_time_)
373 << "Cannot set UseRealTime and UseManualTime simultaneously.";
374 use_manual_time_ = true;
375 return this;
376}
377
378Benchmark* Benchmark::Complexity(BigO complexity) {
379 complexity_ = complexity;
380 return this;
381}
382
383Benchmark* Benchmark::Complexity(BigOFunc* complexity) {
384 complexity_lambda_ = complexity;
385 complexity_ = oLambda;
386 return this;
387}
388
389Benchmark* Benchmark::ComputeStatistics(std::string name,
390 StatisticsFunc* statistics) {
391 statistics_.emplace_back(name, statistics);
392 return this;
393}
394
395Benchmark* Benchmark::Threads(int t) {
396 CHECK_GT(t, 0);
397 thread_counts_.push_back(t);
398 return this;
399}
400
401Benchmark* Benchmark::ThreadRange(int min_threads, int max_threads) {
402 CHECK_GT(min_threads, 0);
403 CHECK_GE(max_threads, min_threads);
404
405 AddRange(&thread_counts_, min_threads, max_threads, 2);
406 return this;
407}
408
409Benchmark* Benchmark::DenseThreadRange(int min_threads, int max_threads,
410 int stride) {
411 CHECK_GT(min_threads, 0);
412 CHECK_GE(max_threads, min_threads);
413 CHECK_GE(stride, 1);
414
415 for (auto i = min_threads; i < max_threads; i += stride) {
416 thread_counts_.push_back(i);
417 }
418 thread_counts_.push_back(max_threads);
419 return this;
420}
421
422Benchmark* Benchmark::ThreadPerCpu() {
423 thread_counts_.push_back(CPUInfo::Get().num_cpus);
424 return this;
425}
426
427void Benchmark::SetName(const char* name) { name_ = name; }
428
429int Benchmark::ArgsCnt() const {
430 if (args_.empty()) {
431 if (arg_names_.empty()) return -1;
432 return static_cast<int>(arg_names_.size());
433 }
434 return static_cast<int>(args_.front().size());
435}
436
437//=============================================================================//
438// FunctionBenchmark
439//=============================================================================//
440
441void FunctionBenchmark::Run(State& st) { func_(st); }
442
443} // end namespace internal
444
445void ClearRegisteredBenchmarks() {
446 internal::BenchmarkFamilies::GetInstance()->ClearBenchmarks();
447}
448
449} // end namespace benchmark
450