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