• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "benchmark_api_internal.h"
18 #include "benchmark_runner.h"
19 #include "internal_macros.h"
20 
21 #ifndef BENCHMARK_OS_WINDOWS
22 #if !defined(BENCHMARK_OS_FUCHSIA) && !defined(BENCHMARK_OS_QURT)
23 #include <sys/resource.h>
24 #endif
25 #include <sys/time.h>
26 #include <unistd.h>
27 #endif
28 
29 #include <algorithm>
30 #include <atomic>
31 #include <condition_variable>
32 #include <cstdio>
33 #include <cstdlib>
34 #include <fstream>
35 #include <iostream>
36 #include <limits>
37 #include <map>
38 #include <memory>
39 #include <random>
40 #include <string>
41 #include <thread>
42 #include <utility>
43 
44 #include "check.h"
45 #include "colorprint.h"
46 #include "commandlineflags.h"
47 #include "complexity.h"
48 #include "counter.h"
49 #include "internal_macros.h"
50 #include "log.h"
51 #include "mutex.h"
52 #include "perf_counters.h"
53 #include "re.h"
54 #include "statistics.h"
55 #include "string_util.h"
56 #include "thread_manager.h"
57 #include "thread_timer.h"
58 
59 namespace benchmark {
60 // Print a list of benchmarks. This option overrides all other options.
61 BM_DEFINE_bool(benchmark_list_tests, false);
62 
63 // A regular expression that specifies the set of benchmarks to execute.  If
64 // this flag is empty, or if this flag is the string \"all\", all benchmarks
65 // linked into the binary are run.
66 BM_DEFINE_string(benchmark_filter, "");
67 
68 // Specification of how long to run the benchmark.
69 //
70 // It can be either an exact number of iterations (specified as `<integer>x`),
71 // or a minimum number of seconds (specified as `<float>s`). If the latter
72 // format (ie., min seconds) is used, the system may run the benchmark longer
73 // until the results are considered significant.
74 //
75 // For backward compatibility, the `s` suffix may be omitted, in which case,
76 // the specified number is interpreted as the number of seconds.
77 //
78 // For cpu-time based tests, this is the lower bound
79 // on the total cpu time used by all threads that make up the test.  For
80 // real-time based tests, this is the lower bound on the elapsed time of the
81 // benchmark execution, regardless of number of threads.
82 BM_DEFINE_string(benchmark_min_time, kDefaultMinTimeStr);
83 
84 // Minimum number of seconds a benchmark should be run before results should be
85 // taken into account. This e.g can be necessary for benchmarks of code which
86 // needs to fill some form of cache before performance is of interest.
87 // Note: results gathered within this period are discarded and not used for
88 // reported result.
89 BM_DEFINE_double(benchmark_min_warmup_time, 0.0);
90 
91 // The number of runs of each benchmark. If greater than 1, the mean and
92 // standard deviation of the runs will be reported.
93 BM_DEFINE_int32(benchmark_repetitions, 1);
94 
95 // If set, enable random interleaving of repetitions of all benchmarks.
96 // See http://github.com/google/benchmark/issues/1051 for details.
97 BM_DEFINE_bool(benchmark_enable_random_interleaving, false);
98 
99 // Report the result of each benchmark repetitions. When 'true' is specified
100 // only the mean, standard deviation, and other statistics are reported for
101 // repeated benchmarks. Affects all reporters.
102 BM_DEFINE_bool(benchmark_report_aggregates_only, false);
103 
104 // Display the result of each benchmark repetitions. When 'true' is specified
105 // only the mean, standard deviation, and other statistics are displayed for
106 // repeated benchmarks. Unlike benchmark_report_aggregates_only, only affects
107 // the display reporter, but  *NOT* file reporter, which will still contain
108 // all the output.
109 BM_DEFINE_bool(benchmark_display_aggregates_only, false);
110 
111 // The format to use for console output.
112 // Valid values are 'console', 'json', or 'csv'.
113 BM_DEFINE_string(benchmark_format, "console");
114 
115 // The format to use for file output.
116 // Valid values are 'console', 'json', or 'csv'.
117 BM_DEFINE_string(benchmark_out_format, "json");
118 
119 // The file to write additional output to.
120 BM_DEFINE_string(benchmark_out, "");
121 
122 // Whether to use colors in the output.  Valid values:
123 // 'true'/'yes'/1, 'false'/'no'/0, and 'auto'. 'auto' means to use colors if
124 // the output is being sent to a terminal and the TERM environment variable is
125 // set to a terminal type that supports colors.
126 BM_DEFINE_string(benchmark_color, "auto");
127 
128 // Whether to use tabular format when printing user counters to the console.
129 // Valid values: 'true'/'yes'/1, 'false'/'no'/0.  Defaults to false.
130 BM_DEFINE_bool(benchmark_counters_tabular, false);
131 
132 // List of additional perf counters to collect, in libpfm format. For more
133 // information about libpfm: https://man7.org/linux/man-pages/man3/libpfm.3.html
134 BM_DEFINE_string(benchmark_perf_counters, "");
135 
136 // Extra context to include in the output formatted as comma-separated key-value
137 // pairs. Kept internal as it's only used for parsing from env/command line.
138 BM_DEFINE_kvpairs(benchmark_context, {});
139 
140 // Set the default time unit to use for reports
141 // Valid values are 'ns', 'us', 'ms' or 's'
142 BM_DEFINE_string(benchmark_time_unit, "");
143 
144 // The level of verbose logging to output
145 BM_DEFINE_int32(v, 0);
146 
147 namespace internal {
148 
149 std::map<std::string, std::string>* global_context = nullptr;
150 
GetGlobalContext()151 BENCHMARK_EXPORT std::map<std::string, std::string>*& GetGlobalContext() {
152   return global_context;
153 }
154 
155 static void const volatile* volatile global_force_escape_pointer;
156 
157 // FIXME: Verify if LTO still messes this up?
UseCharPointer(char const volatile * const v)158 void UseCharPointer(char const volatile* const v) {
159   // We want to escape the pointer `v` so that the compiler can not eliminate
160   // computations that produced it. To do that, we escape the pointer by storing
161   // it into a volatile variable, since generally, volatile store, is not
162   // something the compiler is allowed to elide.
163   global_force_escape_pointer = reinterpret_cast<void const volatile*>(v);
164 }
165 
166 }  // namespace internal
167 
State(std::string name,IterationCount max_iters,const std::vector<int64_t> & ranges,int thread_i,int n_threads,internal::ThreadTimer * timer,internal::ThreadManager * manager,internal::PerfCountersMeasurement * perf_counters_measurement)168 State::State(std::string name, IterationCount max_iters,
169              const std::vector<int64_t>& ranges, int thread_i, int n_threads,
170              internal::ThreadTimer* timer, internal::ThreadManager* manager,
171              internal::PerfCountersMeasurement* perf_counters_measurement)
172     : total_iterations_(0),
173       batch_leftover_(0),
174       max_iterations(max_iters),
175       started_(false),
176       finished_(false),
177       skipped_(internal::NotSkipped),
178       range_(ranges),
179       complexity_n_(0),
180       name_(std::move(name)),
181       thread_index_(thread_i),
182       threads_(n_threads),
183       timer_(timer),
184       manager_(manager),
185       perf_counters_measurement_(perf_counters_measurement) {
186   BM_CHECK(max_iterations != 0) << "At least one iteration must be run";
187   BM_CHECK_LT(thread_index_, threads_)
188       << "thread_index must be less than threads";
189 
190   // Add counters with correct flag now.  If added with `counters[name]` in
191   // `PauseTiming`, a new `Counter` will be inserted the first time, which
192   // won't have the flag.  Inserting them now also reduces the allocations
193   // during the benchmark.
194   if (perf_counters_measurement_) {
195     for (const std::string& counter_name :
196          perf_counters_measurement_->names()) {
197       counters[counter_name] = Counter(0.0, Counter::kAvgIterations);
198     }
199   }
200 
201   // Note: The use of offsetof below is technically undefined until C++17
202   // because State is not a standard layout type. However, all compilers
203   // currently provide well-defined behavior as an extension (which is
204   // demonstrated since constexpr evaluation must diagnose all undefined
205   // behavior). However, GCC and Clang also warn about this use of offsetof,
206   // which must be suppressed.
207 #if defined(__INTEL_COMPILER)
208 #pragma warning push
209 #pragma warning(disable : 1875)
210 #elif defined(__GNUC__)
211 #pragma GCC diagnostic push
212 #pragma GCC diagnostic ignored "-Winvalid-offsetof"
213 #endif
214 #if defined(__NVCC__)
215 #pragma nv_diagnostic push
216 #pragma nv_diag_suppress 1427
217 #endif
218 #if defined(__NVCOMPILER)
219 #pragma diagnostic push
220 #pragma diag_suppress offset_in_non_POD_nonstandard
221 #endif
222   // Offset tests to ensure commonly accessed data is on the first cache line.
223   const int cache_line_size = 64;
224   static_assert(
225       offsetof(State, skipped_) <= (cache_line_size - sizeof(skipped_)), "");
226 #if defined(__INTEL_COMPILER)
227 #pragma warning pop
228 #elif defined(__GNUC__)
229 #pragma GCC diagnostic pop
230 #endif
231 #if defined(__NVCC__)
232 #pragma nv_diagnostic pop
233 #endif
234 #if defined(__NVCOMPILER)
235 #pragma diagnostic pop
236 #endif
237 }
238 
PauseTiming()239 void State::PauseTiming() {
240   // Add in time accumulated so far
241   BM_CHECK(started_ && !finished_ && !skipped());
242   timer_->StopTimer();
243   if (perf_counters_measurement_) {
244     std::vector<std::pair<std::string, double>> measurements;
245     if (!perf_counters_measurement_->Stop(measurements)) {
246       BM_CHECK(false) << "Perf counters read the value failed.";
247     }
248     for (const auto& name_and_measurement : measurements) {
249       const std::string& name = name_and_measurement.first;
250       const double measurement = name_and_measurement.second;
251       // Counter was inserted with `kAvgIterations` flag by the constructor.
252       assert(counters.find(name) != counters.end());
253       counters[name].value += measurement;
254     }
255   }
256 }
257 
ResumeTiming()258 void State::ResumeTiming() {
259   BM_CHECK(started_ && !finished_ && !skipped());
260   timer_->StartTimer();
261   if (perf_counters_measurement_) {
262     perf_counters_measurement_->Start();
263   }
264 }
265 
SkipWithMessage(const std::string & msg)266 void State::SkipWithMessage(const std::string& msg) {
267   skipped_ = internal::SkippedWithMessage;
268   {
269     MutexLock l(manager_->GetBenchmarkMutex());
270     if (internal::NotSkipped == manager_->results.skipped_) {
271       manager_->results.skip_message_ = msg;
272       manager_->results.skipped_ = skipped_;
273     }
274   }
275   total_iterations_ = 0;
276   if (timer_->running()) timer_->StopTimer();
277 }
278 
SkipWithError(const std::string & msg)279 void State::SkipWithError(const std::string& msg) {
280   skipped_ = internal::SkippedWithError;
281   {
282     MutexLock l(manager_->GetBenchmarkMutex());
283     if (internal::NotSkipped == manager_->results.skipped_) {
284       manager_->results.skip_message_ = msg;
285       manager_->results.skipped_ = skipped_;
286     }
287   }
288   total_iterations_ = 0;
289   if (timer_->running()) timer_->StopTimer();
290 }
291 
SetIterationTime(double seconds)292 void State::SetIterationTime(double seconds) {
293   timer_->SetIterationTime(seconds);
294 }
295 
SetLabel(const std::string & label)296 void State::SetLabel(const std::string& label) {
297   MutexLock l(manager_->GetBenchmarkMutex());
298   manager_->results.report_label_ = label;
299 }
300 
StartKeepRunning()301 void State::StartKeepRunning() {
302   BM_CHECK(!started_ && !finished_);
303   started_ = true;
304   total_iterations_ = skipped() ? 0 : max_iterations;
305   manager_->StartStopBarrier();
306   if (!skipped()) ResumeTiming();
307 }
308 
FinishKeepRunning()309 void State::FinishKeepRunning() {
310   BM_CHECK(started_ && (!finished_ || skipped()));
311   if (!skipped()) {
312     PauseTiming();
313   }
314   // Total iterations has now wrapped around past 0. Fix this.
315   total_iterations_ = 0;
316   finished_ = true;
317   manager_->StartStopBarrier();
318 }
319 
320 namespace internal {
321 namespace {
322 
323 // Flushes streams after invoking reporter methods that write to them. This
324 // ensures users get timely updates even when streams are not line-buffered.
FlushStreams(BenchmarkReporter * reporter)325 void FlushStreams(BenchmarkReporter* reporter) {
326   if (!reporter) return;
327   std::flush(reporter->GetOutputStream());
328   std::flush(reporter->GetErrorStream());
329 }
330 
331 // Reports in both display and file reporters.
Report(BenchmarkReporter * display_reporter,BenchmarkReporter * file_reporter,const RunResults & run_results)332 void Report(BenchmarkReporter* display_reporter,
333             BenchmarkReporter* file_reporter, const RunResults& run_results) {
334   auto report_one = [](BenchmarkReporter* reporter, bool aggregates_only,
335                        const RunResults& results) {
336     assert(reporter);
337     // If there are no aggregates, do output non-aggregates.
338     aggregates_only &= !results.aggregates_only.empty();
339     if (!aggregates_only) reporter->ReportRuns(results.non_aggregates);
340     if (!results.aggregates_only.empty())
341       reporter->ReportRuns(results.aggregates_only);
342   };
343 
344   report_one(display_reporter, run_results.display_report_aggregates_only,
345              run_results);
346   if (file_reporter)
347     report_one(file_reporter, run_results.file_report_aggregates_only,
348                run_results);
349 
350   FlushStreams(display_reporter);
351   FlushStreams(file_reporter);
352 }
353 
RunBenchmarks(const std::vector<BenchmarkInstance> & benchmarks,BenchmarkReporter * display_reporter,BenchmarkReporter * file_reporter)354 void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
355                    BenchmarkReporter* display_reporter,
356                    BenchmarkReporter* file_reporter) {
357   // Note the file_reporter can be null.
358   BM_CHECK(display_reporter != nullptr);
359 
360   // Determine the width of the name field using a minimum width of 10.
361   bool might_have_aggregates = FLAGS_benchmark_repetitions > 1;
362   size_t name_field_width = 10;
363   size_t stat_field_width = 0;
364   for (const BenchmarkInstance& benchmark : benchmarks) {
365     name_field_width =
366         std::max<size_t>(name_field_width, benchmark.name().str().size());
367     might_have_aggregates |= benchmark.repetitions() > 1;
368 
369     for (const auto& Stat : benchmark.statistics())
370       stat_field_width = std::max<size_t>(stat_field_width, Stat.name_.size());
371   }
372   if (might_have_aggregates) name_field_width += 1 + stat_field_width;
373 
374   // Print header here
375   BenchmarkReporter::Context context;
376   context.name_field_width = name_field_width;
377 
378   // Keep track of running times of all instances of each benchmark family.
379   std::map<int /*family_index*/, BenchmarkReporter::PerFamilyRunReports>
380       per_family_reports;
381 
382   if (display_reporter->ReportContext(context) &&
383       (!file_reporter || file_reporter->ReportContext(context))) {
384     FlushStreams(display_reporter);
385     FlushStreams(file_reporter);
386 
387     size_t num_repetitions_total = 0;
388 
389     // This perfcounters object needs to be created before the runners vector
390     // below so it outlasts their lifetime.
391     PerfCountersMeasurement perfcounters(
392         StrSplit(FLAGS_benchmark_perf_counters, ','));
393 
394     // Vector of benchmarks to run
395     std::vector<internal::BenchmarkRunner> runners;
396     runners.reserve(benchmarks.size());
397 
398     // Count the number of benchmarks with threads to warn the user in case
399     // performance counters are used.
400     int benchmarks_with_threads = 0;
401 
402     // Loop through all benchmarks
403     for (const BenchmarkInstance& benchmark : benchmarks) {
404       BenchmarkReporter::PerFamilyRunReports* reports_for_family = nullptr;
405       if (benchmark.complexity() != oNone)
406         reports_for_family = &per_family_reports[benchmark.family_index()];
407       benchmarks_with_threads += (benchmark.threads() > 1);
408       runners.emplace_back(benchmark, &perfcounters, reports_for_family);
409       int num_repeats_of_this_instance = runners.back().GetNumRepeats();
410       num_repetitions_total +=
411           static_cast<size_t>(num_repeats_of_this_instance);
412       if (reports_for_family)
413         reports_for_family->num_runs_total += num_repeats_of_this_instance;
414     }
415     assert(runners.size() == benchmarks.size() && "Unexpected runner count.");
416 
417     // The use of performance counters with threads would be unintuitive for
418     // the average user so we need to warn them about this case
419     if ((benchmarks_with_threads > 0) && (perfcounters.num_counters() > 0)) {
420       GetErrorLogInstance()
421           << "***WARNING*** There are " << benchmarks_with_threads
422           << " benchmarks with threads and " << perfcounters.num_counters()
423           << " performance counters were requested. Beware counters will "
424              "reflect the combined usage across all "
425              "threads.\n";
426     }
427 
428     std::vector<size_t> repetition_indices;
429     repetition_indices.reserve(num_repetitions_total);
430     for (size_t runner_index = 0, num_runners = runners.size();
431          runner_index != num_runners; ++runner_index) {
432       const internal::BenchmarkRunner& runner = runners[runner_index];
433       std::fill_n(std::back_inserter(repetition_indices),
434                   runner.GetNumRepeats(), runner_index);
435     }
436     assert(repetition_indices.size() == num_repetitions_total &&
437            "Unexpected number of repetition indexes.");
438 
439     if (FLAGS_benchmark_enable_random_interleaving) {
440       std::random_device rd;
441       std::mt19937 g(rd());
442       std::shuffle(repetition_indices.begin(), repetition_indices.end(), g);
443     }
444 
445     for (size_t repetition_index : repetition_indices) {
446       internal::BenchmarkRunner& runner = runners[repetition_index];
447       runner.DoOneRepetition();
448       if (runner.HasRepeatsRemaining()) continue;
449       // FIXME: report each repetition separately, not all of them in bulk.
450 
451       display_reporter->ReportRunsConfig(
452           runner.GetMinTime(), runner.HasExplicitIters(), runner.GetIters());
453       if (file_reporter)
454         file_reporter->ReportRunsConfig(
455             runner.GetMinTime(), runner.HasExplicitIters(), runner.GetIters());
456 
457       RunResults run_results = runner.GetResults();
458 
459       // Maybe calculate complexity report
460       if (const auto* reports_for_family = runner.GetReportsForFamily()) {
461         if (reports_for_family->num_runs_done ==
462             reports_for_family->num_runs_total) {
463           auto additional_run_stats = ComputeBigO(reports_for_family->Runs);
464           run_results.aggregates_only.insert(run_results.aggregates_only.end(),
465                                              additional_run_stats.begin(),
466                                              additional_run_stats.end());
467           per_family_reports.erase(
468               static_cast<int>(reports_for_family->Runs.front().family_index));
469         }
470       }
471 
472       Report(display_reporter, file_reporter, run_results);
473     }
474   }
475   display_reporter->Finalize();
476   if (file_reporter) file_reporter->Finalize();
477   FlushStreams(display_reporter);
478   FlushStreams(file_reporter);
479 }
480 
481 // Disable deprecated warnings temporarily because we need to reference
482 // CSVReporter but don't want to trigger -Werror=-Wdeprecated-declarations
483 BENCHMARK_DISABLE_DEPRECATED_WARNING
484 
CreateReporter(std::string const & name,ConsoleReporter::OutputOptions output_opts)485 std::unique_ptr<BenchmarkReporter> CreateReporter(
486     std::string const& name, ConsoleReporter::OutputOptions output_opts) {
487   typedef std::unique_ptr<BenchmarkReporter> PtrType;
488   if (name == "console") {
489     return PtrType(new ConsoleReporter(output_opts));
490   }
491   if (name == "json") {
492     return PtrType(new JSONReporter());
493   }
494   if (name == "csv") {
495     return PtrType(new CSVReporter());
496   }
497   std::cerr << "Unexpected format: '" << name << "'\n";
498   std::exit(1);
499 }
500 
501 BENCHMARK_RESTORE_DEPRECATED_WARNING
502 
503 }  // end namespace
504 
IsZero(double n)505 bool IsZero(double n) {
506   return std::abs(n) < std::numeric_limits<double>::epsilon();
507 }
508 
GetOutputOptions(bool force_no_color)509 ConsoleReporter::OutputOptions GetOutputOptions(bool force_no_color) {
510   int output_opts = ConsoleReporter::OO_Defaults;
511   auto is_benchmark_color = [force_no_color]() -> bool {
512     if (force_no_color) {
513       return false;
514     }
515     if (FLAGS_benchmark_color == "auto") {
516       return IsColorTerminal();
517     }
518     return IsTruthyFlagValue(FLAGS_benchmark_color);
519   };
520   if (is_benchmark_color()) {
521     output_opts |= ConsoleReporter::OO_Color;
522   } else {
523     output_opts &= ~ConsoleReporter::OO_Color;
524   }
525   if (FLAGS_benchmark_counters_tabular) {
526     output_opts |= ConsoleReporter::OO_Tabular;
527   } else {
528     output_opts &= ~ConsoleReporter::OO_Tabular;
529   }
530   return static_cast<ConsoleReporter::OutputOptions>(output_opts);
531 }
532 
533 }  // end namespace internal
534 
CreateDefaultDisplayReporter()535 BenchmarkReporter* CreateDefaultDisplayReporter() {
536   static auto default_display_reporter =
537       internal::CreateReporter(FLAGS_benchmark_format,
538                                internal::GetOutputOptions())
539           .release();
540   return default_display_reporter;
541 }
542 
RunSpecifiedBenchmarks()543 size_t RunSpecifiedBenchmarks() {
544   return RunSpecifiedBenchmarks(nullptr, nullptr, FLAGS_benchmark_filter);
545 }
546 
RunSpecifiedBenchmarks(std::string spec)547 size_t RunSpecifiedBenchmarks(std::string spec) {
548   return RunSpecifiedBenchmarks(nullptr, nullptr, std::move(spec));
549 }
550 
RunSpecifiedBenchmarks(BenchmarkReporter * display_reporter)551 size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter) {
552   return RunSpecifiedBenchmarks(display_reporter, nullptr,
553                                 FLAGS_benchmark_filter);
554 }
555 
RunSpecifiedBenchmarks(BenchmarkReporter * display_reporter,std::string spec)556 size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
557                               std::string spec) {
558   return RunSpecifiedBenchmarks(display_reporter, nullptr, std::move(spec));
559 }
560 
RunSpecifiedBenchmarks(BenchmarkReporter * display_reporter,BenchmarkReporter * file_reporter)561 size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
562                               BenchmarkReporter* file_reporter) {
563   return RunSpecifiedBenchmarks(display_reporter, file_reporter,
564                                 FLAGS_benchmark_filter);
565 }
566 
RunSpecifiedBenchmarks(BenchmarkReporter * display_reporter,BenchmarkReporter * file_reporter,std::string spec)567 size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
568                               BenchmarkReporter* file_reporter,
569                               std::string spec) {
570   if (spec.empty() || spec == "all")
571     spec = ".";  // Regexp that matches all benchmarks
572 
573   // Setup the reporters
574   std::ofstream output_file;
575   std::unique_ptr<BenchmarkReporter> default_display_reporter;
576   std::unique_ptr<BenchmarkReporter> default_file_reporter;
577   if (!display_reporter) {
578     default_display_reporter.reset(CreateDefaultDisplayReporter());
579     display_reporter = default_display_reporter.get();
580   }
581   auto& Out = display_reporter->GetOutputStream();
582   auto& Err = display_reporter->GetErrorStream();
583 
584   std::string const& fname = FLAGS_benchmark_out;
585   if (fname.empty() && file_reporter) {
586     Err << "A custom file reporter was provided but "
587            "--benchmark_out=<file> was not specified."
588         << std::endl;
589     Out.flush();
590     Err.flush();
591     std::exit(1);
592   }
593   if (!fname.empty()) {
594     output_file.open(fname);
595     if (!output_file.is_open()) {
596       Err << "invalid file name: '" << fname << "'" << std::endl;
597       Out.flush();
598       Err.flush();
599       std::exit(1);
600     }
601     if (!file_reporter) {
602       default_file_reporter = internal::CreateReporter(
603           FLAGS_benchmark_out_format, FLAGS_benchmark_counters_tabular
604                                           ? ConsoleReporter::OO_Tabular
605                                           : ConsoleReporter::OO_None);
606       file_reporter = default_file_reporter.get();
607     }
608     file_reporter->SetOutputStream(&output_file);
609     file_reporter->SetErrorStream(&output_file);
610   }
611 
612   std::vector<internal::BenchmarkInstance> benchmarks;
613   if (!FindBenchmarksInternal(spec, &benchmarks, &Err)) {
614     Out.flush();
615     Err.flush();
616     return 0;
617   }
618 
619   if (benchmarks.empty()) {
620     Err << "Failed to match any benchmarks against regex: " << spec << "\n";
621     Out.flush();
622     Err.flush();
623     return 0;
624   }
625 
626   if (FLAGS_benchmark_list_tests) {
627     for (auto const& benchmark : benchmarks)
628       Out << benchmark.name().str() << "\n";
629   } else {
630     internal::RunBenchmarks(benchmarks, display_reporter, file_reporter);
631   }
632 
633   Out.flush();
634   Err.flush();
635   return benchmarks.size();
636 }
637 
638 namespace {
639 // stores the time unit benchmarks use by default
640 TimeUnit default_time_unit = kNanosecond;
641 }  // namespace
642 
GetDefaultTimeUnit()643 TimeUnit GetDefaultTimeUnit() { return default_time_unit; }
644 
SetDefaultTimeUnit(TimeUnit unit)645 void SetDefaultTimeUnit(TimeUnit unit) { default_time_unit = unit; }
646 
GetBenchmarkFilter()647 std::string GetBenchmarkFilter() { return FLAGS_benchmark_filter; }
648 
SetBenchmarkFilter(std::string value)649 void SetBenchmarkFilter(std::string value) {
650   FLAGS_benchmark_filter = std::move(value);
651 }
652 
GetBenchmarkVerbosity()653 int32_t GetBenchmarkVerbosity() { return FLAGS_v; }
654 
RegisterMemoryManager(MemoryManager * manager)655 void RegisterMemoryManager(MemoryManager* manager) {
656   internal::memory_manager = manager;
657 }
658 
AddCustomContext(const std::string & key,const std::string & value)659 void AddCustomContext(const std::string& key, const std::string& value) {
660   if (internal::global_context == nullptr) {
661     internal::global_context = new std::map<std::string, std::string>();
662   }
663   if (!internal::global_context->emplace(key, value).second) {
664     std::cerr << "Failed to add custom context \"" << key << "\" as it already "
665               << "exists with value \"" << value << "\"\n";
666   }
667 }
668 
669 namespace internal {
670 
671 void (*HelperPrintf)();
672 
PrintUsageAndExit()673 void PrintUsageAndExit() {
674   HelperPrintf();
675   exit(0);
676 }
677 
SetDefaultTimeUnitFromFlag(const std::string & time_unit_flag)678 void SetDefaultTimeUnitFromFlag(const std::string& time_unit_flag) {
679   if (time_unit_flag == "s") {
680     return SetDefaultTimeUnit(kSecond);
681   }
682   if (time_unit_flag == "ms") {
683     return SetDefaultTimeUnit(kMillisecond);
684   }
685   if (time_unit_flag == "us") {
686     return SetDefaultTimeUnit(kMicrosecond);
687   }
688   if (time_unit_flag == "ns") {
689     return SetDefaultTimeUnit(kNanosecond);
690   }
691   if (!time_unit_flag.empty()) {
692     PrintUsageAndExit();
693   }
694 }
695 
ParseCommandLineFlags(int * argc,char ** argv)696 void ParseCommandLineFlags(int* argc, char** argv) {
697   using namespace benchmark;
698   BenchmarkReporter::Context::executable_name =
699       (argc && *argc > 0) ? argv[0] : "unknown";
700   for (int i = 1; argc && i < *argc; ++i) {
701     if (ParseBoolFlag(argv[i], "benchmark_list_tests",
702                       &FLAGS_benchmark_list_tests) ||
703         ParseStringFlag(argv[i], "benchmark_filter", &FLAGS_benchmark_filter) ||
704         ParseStringFlag(argv[i], "benchmark_min_time",
705                         &FLAGS_benchmark_min_time) ||
706         ParseDoubleFlag(argv[i], "benchmark_min_warmup_time",
707                         &FLAGS_benchmark_min_warmup_time) ||
708         ParseInt32Flag(argv[i], "benchmark_repetitions",
709                        &FLAGS_benchmark_repetitions) ||
710         ParseBoolFlag(argv[i], "benchmark_enable_random_interleaving",
711                       &FLAGS_benchmark_enable_random_interleaving) ||
712         ParseBoolFlag(argv[i], "benchmark_report_aggregates_only",
713                       &FLAGS_benchmark_report_aggregates_only) ||
714         ParseBoolFlag(argv[i], "benchmark_display_aggregates_only",
715                       &FLAGS_benchmark_display_aggregates_only) ||
716         ParseStringFlag(argv[i], "benchmark_format", &FLAGS_benchmark_format) ||
717         ParseStringFlag(argv[i], "benchmark_out", &FLAGS_benchmark_out) ||
718         ParseStringFlag(argv[i], "benchmark_out_format",
719                         &FLAGS_benchmark_out_format) ||
720         ParseStringFlag(argv[i], "benchmark_color", &FLAGS_benchmark_color) ||
721         ParseBoolFlag(argv[i], "benchmark_counters_tabular",
722                       &FLAGS_benchmark_counters_tabular) ||
723         ParseStringFlag(argv[i], "benchmark_perf_counters",
724                         &FLAGS_benchmark_perf_counters) ||
725         ParseKeyValueFlag(argv[i], "benchmark_context",
726                           &FLAGS_benchmark_context) ||
727         ParseStringFlag(argv[i], "benchmark_time_unit",
728                         &FLAGS_benchmark_time_unit) ||
729         ParseInt32Flag(argv[i], "v", &FLAGS_v)) {
730       for (int j = i; j != *argc - 1; ++j) argv[j] = argv[j + 1];
731 
732       --(*argc);
733       --i;
734     } else if (IsFlag(argv[i], "help")) {
735       PrintUsageAndExit();
736     }
737   }
738   for (auto const* flag :
739        {&FLAGS_benchmark_format, &FLAGS_benchmark_out_format}) {
740     if (*flag != "console" && *flag != "json" && *flag != "csv") {
741       PrintUsageAndExit();
742     }
743   }
744   SetDefaultTimeUnitFromFlag(FLAGS_benchmark_time_unit);
745   if (FLAGS_benchmark_color.empty()) {
746     PrintUsageAndExit();
747   }
748   for (const auto& kv : FLAGS_benchmark_context) {
749     AddCustomContext(kv.first, kv.second);
750   }
751 }
752 
InitializeStreams()753 int InitializeStreams() {
754   static std::ios_base::Init init;
755   return 0;
756 }
757 
758 }  // end namespace internal
759 
GetBenchmarkVersion()760 std::string GetBenchmarkVersion() {
761 #ifdef BENCHMARK_VERSION
762   return {BENCHMARK_VERSION};
763 #else
764   return {""};
765 #endif
766 }
767 
PrintDefaultHelp()768 void PrintDefaultHelp() {
769   fprintf(stdout,
770           "benchmark"
771           " [--benchmark_list_tests={true|false}]\n"
772           "          [--benchmark_filter=<regex>]\n"
773           "          [--benchmark_min_time=`<integer>x` OR `<float>s` ]\n"
774           "          [--benchmark_min_warmup_time=<min_warmup_time>]\n"
775           "          [--benchmark_repetitions=<num_repetitions>]\n"
776           "          [--benchmark_enable_random_interleaving={true|false}]\n"
777           "          [--benchmark_report_aggregates_only={true|false}]\n"
778           "          [--benchmark_display_aggregates_only={true|false}]\n"
779           "          [--benchmark_format=<console|json|csv>]\n"
780           "          [--benchmark_out=<filename>]\n"
781           "          [--benchmark_out_format=<json|console|csv>]\n"
782           "          [--benchmark_color={auto|true|false}]\n"
783           "          [--benchmark_counters_tabular={true|false}]\n"
784 #if defined HAVE_LIBPFM
785           "          [--benchmark_perf_counters=<counter>,...]\n"
786 #endif
787           "          [--benchmark_context=<key>=<value>,...]\n"
788           "          [--benchmark_time_unit={ns|us|ms|s}]\n"
789           "          [--v=<verbosity>]\n");
790 }
791 
Initialize(int * argc,char ** argv,void (* HelperPrintf)())792 void Initialize(int* argc, char** argv, void (*HelperPrintf)()) {
793   internal::HelperPrintf = HelperPrintf;
794   internal::ParseCommandLineFlags(argc, argv);
795   internal::LogLevel() = FLAGS_v;
796 }
797 
Shutdown()798 void Shutdown() { delete internal::global_context; }
799 
ReportUnrecognizedArguments(int argc,char ** argv)800 bool ReportUnrecognizedArguments(int argc, char** argv) {
801   for (int i = 1; i < argc; ++i) {
802     fprintf(stderr, "%s: error: unrecognized command-line flag: %s\n", argv[0],
803             argv[i]);
804   }
805   return argc > 1;
806 }
807 
808 }  // end namespace benchmark
809