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