|
29 | 29 | #include <condition_variable> |
30 | 30 | #include <cstdio> |
31 | 31 | #include <cstdlib> |
32 | | -#include <cstring> |
33 | 32 | #include <fstream> |
34 | 33 | #include <iostream> |
35 | 34 | #include <memory> |
| 35 | +#include <string> |
36 | 36 | #include <thread> |
37 | 37 |
|
38 | 38 | #include "check.h" |
|
46 | 46 | #include "re.h" |
47 | 47 | #include "statistics.h" |
48 | 48 | #include "string_util.h" |
49 | | -#include "timers.h" |
| 49 | +#include "thread_manager.h" |
| 50 | +#include "thread_timer.h" |
50 | 51 |
|
51 | 52 | DEFINE_bool(benchmark_list_tests, false, |
52 | 53 | "Print a list of benchmarks. This option overrides all other " |
@@ -110,113 +111,6 @@ namespace internal { |
110 | 111 |
|
111 | 112 | void UseCharPointer(char const volatile*) {} |
112 | 113 |
|
113 | | -class ThreadManager { |
114 | | - public: |
115 | | - ThreadManager(int num_threads) |
116 | | - : alive_threads_(num_threads), start_stop_barrier_(num_threads) {} |
117 | | - |
118 | | - Mutex& GetBenchmarkMutex() const RETURN_CAPABILITY(benchmark_mutex_) { |
119 | | - return benchmark_mutex_; |
120 | | - } |
121 | | - |
122 | | - bool StartStopBarrier() EXCLUDES(end_cond_mutex_) { |
123 | | - return start_stop_barrier_.wait(); |
124 | | - } |
125 | | - |
126 | | - void NotifyThreadComplete() EXCLUDES(end_cond_mutex_) { |
127 | | - start_stop_barrier_.removeThread(); |
128 | | - if (--alive_threads_ == 0) { |
129 | | - MutexLock lock(end_cond_mutex_); |
130 | | - end_condition_.notify_all(); |
131 | | - } |
132 | | - } |
133 | | - |
134 | | - void WaitForAllThreads() EXCLUDES(end_cond_mutex_) { |
135 | | - MutexLock lock(end_cond_mutex_); |
136 | | - end_condition_.wait(lock.native_handle(), |
137 | | - [this]() { return alive_threads_ == 0; }); |
138 | | - } |
139 | | - |
140 | | - public: |
141 | | - struct Result { |
142 | | - double real_time_used = 0; |
143 | | - double cpu_time_used = 0; |
144 | | - double manual_time_used = 0; |
145 | | - int64_t bytes_processed = 0; |
146 | | - int64_t items_processed = 0; |
147 | | - int complexity_n = 0; |
148 | | - std::string report_label_; |
149 | | - std::string error_message_; |
150 | | - bool has_error_ = false; |
151 | | - UserCounters counters; |
152 | | - }; |
153 | | - GUARDED_BY(GetBenchmarkMutex()) Result results; |
154 | | - |
155 | | - private: |
156 | | - mutable Mutex benchmark_mutex_; |
157 | | - std::atomic<int> alive_threads_; |
158 | | - Barrier start_stop_barrier_; |
159 | | - Mutex end_cond_mutex_; |
160 | | - Condition end_condition_; |
161 | | -}; |
162 | | - |
163 | | -// Timer management class |
164 | | -class ThreadTimer { |
165 | | - public: |
166 | | - ThreadTimer() = default; |
167 | | - |
168 | | - // Called by each thread |
169 | | - void StartTimer() { |
170 | | - running_ = true; |
171 | | - start_real_time_ = ChronoClockNow(); |
172 | | - start_cpu_time_ = ThreadCPUUsage(); |
173 | | - } |
174 | | - |
175 | | - // Called by each thread |
176 | | - void StopTimer() { |
177 | | - CHECK(running_); |
178 | | - running_ = false; |
179 | | - real_time_used_ += ChronoClockNow() - start_real_time_; |
180 | | - // Floating point error can result in the subtraction producing a negative |
181 | | - // time. Guard against that. |
182 | | - cpu_time_used_ += std::max<double>(ThreadCPUUsage() - start_cpu_time_, 0); |
183 | | - } |
184 | | - |
185 | | - // Called by each thread |
186 | | - void SetIterationTime(double seconds) { manual_time_used_ += seconds; } |
187 | | - |
188 | | - bool running() const { return running_; } |
189 | | - |
190 | | - // REQUIRES: timer is not running |
191 | | - double real_time_used() { |
192 | | - CHECK(!running_); |
193 | | - return real_time_used_; |
194 | | - } |
195 | | - |
196 | | - // REQUIRES: timer is not running |
197 | | - double cpu_time_used() { |
198 | | - CHECK(!running_); |
199 | | - return cpu_time_used_; |
200 | | - } |
201 | | - |
202 | | - // REQUIRES: timer is not running |
203 | | - double manual_time_used() { |
204 | | - CHECK(!running_); |
205 | | - return manual_time_used_; |
206 | | - } |
207 | | - |
208 | | - private: |
209 | | - bool running_ = false; // Is the timer running |
210 | | - double start_real_time_ = 0; // If running_ |
211 | | - double start_cpu_time_ = 0; // If running_ |
212 | | - |
213 | | - // Accumulated time so far (does not contain current slice if running_) |
214 | | - double real_time_used_ = 0; |
215 | | - double cpu_time_used_ = 0; |
216 | | - // Manually set iteration time. User sets this with SetIterationTime(seconds). |
217 | | - double manual_time_used_ = 0; |
218 | | -}; |
219 | | - |
220 | 114 | namespace { |
221 | 115 |
|
222 | 116 | BenchmarkReporter::Run CreateRunReport( |
|
0 commit comments