Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / third-party / benchmark / src / thread_timer.h
blobeb23f5956150f0bfde5b26fbb0f77dbc445b7418
1 #ifndef BENCHMARK_THREAD_TIMER_H
2 #define BENCHMARK_THREAD_TIMER_H
4 #include "check.h"
5 #include "timers.h"
7 namespace benchmark {
8 namespace internal {
10 class ThreadTimer {
11 explicit ThreadTimer(bool measure_process_cpu_time_)
12 : measure_process_cpu_time(measure_process_cpu_time_) {}
14 public:
15 static ThreadTimer Create() {
16 return ThreadTimer(/*measure_process_cpu_time_=*/false);
18 static ThreadTimer CreateProcessCpuTime() {
19 return ThreadTimer(/*measure_process_cpu_time_=*/true);
22 // Called by each thread
23 void StartTimer() {
24 running_ = true;
25 start_real_time_ = ChronoClockNow();
26 start_cpu_time_ = ReadCpuTimerOfChoice();
29 // Called by each thread
30 void StopTimer() {
31 BM_CHECK(running_);
32 running_ = false;
33 real_time_used_ += ChronoClockNow() - start_real_time_;
34 // Floating point error can result in the subtraction producing a negative
35 // time. Guard against that.
36 cpu_time_used_ +=
37 std::max<double>(ReadCpuTimerOfChoice() - start_cpu_time_, 0);
40 // Called by each thread
41 void SetIterationTime(double seconds) { manual_time_used_ += seconds; }
43 bool running() const { return running_; }
45 // REQUIRES: timer is not running
46 double real_time_used() const {
47 BM_CHECK(!running_);
48 return real_time_used_;
51 // REQUIRES: timer is not running
52 double cpu_time_used() const {
53 BM_CHECK(!running_);
54 return cpu_time_used_;
57 // REQUIRES: timer is not running
58 double manual_time_used() const {
59 BM_CHECK(!running_);
60 return manual_time_used_;
63 private:
64 double ReadCpuTimerOfChoice() const {
65 if (measure_process_cpu_time) return ProcessCPUUsage();
66 return ThreadCPUUsage();
69 // should the thread, or the process, time be measured?
70 const bool measure_process_cpu_time;
72 bool running_ = false; // Is the timer running
73 double start_real_time_ = 0; // If running_
74 double start_cpu_time_ = 0; // If running_
76 // Accumulated time so far (does not contain current slice if running_)
77 double real_time_used_ = 0;
78 double cpu_time_used_ = 0;
79 // Manually set iteration time. User sets this with SetIterationTime(seconds).
80 double manual_time_used_ = 0;
83 } // namespace internal
84 } // namespace benchmark
86 #endif // BENCHMARK_THREAD_TIMER_H