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