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 #ifndef BENCHMARK_RUNNER_H_ 16 #define BENCHMARK_RUNNER_H_ 17 18 #include <thread> 19 #include <vector> 20 21 #include "benchmark_api_internal.h" 22 #include "internal_macros.h" 23 #include "perf_counters.h" 24 #include "thread_manager.h" 25 26 DECLARE_double(benchmark_min_time); 27 28 DECLARE_int32(benchmark_repetitions); 29 30 DECLARE_bool(benchmark_report_aggregates_only); 31 32 DECLARE_bool(benchmark_display_aggregates_only); 33 34 DECLARE_string(benchmark_perf_counters); 35 36 namespace benchmark { 37 38 namespace internal { 39 40 extern MemoryManager* memory_manager; 41 42 struct RunResults { 43 std::vector<BenchmarkReporter::Run> non_aggregates; 44 std::vector<BenchmarkReporter::Run> aggregates_only; 45 46 bool display_report_aggregates_only = false; 47 bool file_report_aggregates_only = false; 48 }; 49 50 class BenchmarkRunner { 51 public: 52 BenchmarkRunner(const benchmark::internal::BenchmarkInstance& b_, 53 BenchmarkReporter::PerFamilyRunReports* reports_for_family); 54 GetNumRepeats()55 int GetNumRepeats() const { return repeats; } 56 HasRepeatsRemaining()57 bool HasRepeatsRemaining() const { 58 return GetNumRepeats() != num_repetitions_done; 59 } 60 61 void DoOneRepetition(); 62 63 RunResults&& GetResults(); 64 GetReportsForFamily()65 BenchmarkReporter::PerFamilyRunReports* GetReportsForFamily() const { 66 return reports_for_family; 67 }; 68 69 private: 70 RunResults run_results; 71 72 const benchmark::internal::BenchmarkInstance& b; 73 BenchmarkReporter::PerFamilyRunReports* reports_for_family; 74 75 const double min_time; 76 const int repeats; 77 const bool has_explicit_iteration_count; 78 79 int num_repetitions_done = 0; 80 81 std::vector<std::thread> pool; 82 83 IterationCount iters; // preserved between repetitions! 84 // So only the first repetition has to find/calculate it, 85 // the other repetitions will just use that precomputed iteration count. 86 87 PerfCountersMeasurement perf_counters_measurement; 88 PerfCountersMeasurement* const perf_counters_measurement_ptr; 89 90 struct IterationResults { 91 internal::ThreadManager::Result results; 92 IterationCount iters; 93 double seconds; 94 }; 95 IterationResults DoNIterations(); 96 97 IterationCount PredictNumItersNeeded(const IterationResults& i) const; 98 99 bool ShouldReportIterationResults(const IterationResults& i) const; 100 }; 101 102 } // namespace internal 103 104 } // end namespace benchmark 105 106 #endif // BENCHMARK_RUNNER_H_ 107