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