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