1 #ifndef BENCHMARK_THREAD_TIMER_H
2 #define BENCHMARK_THREAD_TIMER_H
11 explicit ThreadTimer(bool measure_process_cpu_time_
)
12 : measure_process_cpu_time(measure_process_cpu_time_
) {}
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
25 start_real_time_
= ChronoClockNow();
26 start_cpu_time_
= ReadCpuTimerOfChoice();
29 // Called by each thread
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.
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 {
48 return real_time_used_
;
51 // REQUIRES: timer is not running
52 double cpu_time_used() const {
54 return cpu_time_used_
;
57 // REQUIRES: timer is not running
58 double manual_time_used() const {
60 return manual_time_used_
;
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