thread_timer.h
Go to the documentation of this file.
1 #ifndef BENCHMARK_THREAD_TIMER_H
2 #define BENCHMARK_THREAD_TIMER_H
3 
4 #include "check.h"
5 #include "timers.h"
6 
7 namespace benchmark {
8 namespace internal {
9 
10 class ThreadTimer {
11  explicit ThreadTimer(bool measure_process_cpu_time_)
12  : measure_process_cpu_time(measure_process_cpu_time_) {}
13 
14  public:
15  static ThreadTimer Create() {
16  return ThreadTimer(/*measure_process_cpu_time_=*/false);
17  }
19  return ThreadTimer(/*measure_process_cpu_time_=*/true);
20  }
21 
22  // Called by each thread
23  void StartTimer() {
24  running_ = true;
27  }
28 
29  // Called by each thread
30  void StopTimer() {
32  running_ = false;
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);
38  }
39 
40  // Called by each thread
42 
43  bool running() const { return running_; }
44 
45  // REQUIRES: timer is not running
46  double real_time_used() const {
48  return real_time_used_;
49  }
50 
51  // REQUIRES: timer is not running
52  double cpu_time_used() const {
54  return cpu_time_used_;
55  }
56 
57  // REQUIRES: timer is not running
58  double manual_time_used() const {
60  return manual_time_used_;
61  }
62 
63  private:
64  double ReadCpuTimerOfChoice() const {
66  return ThreadCPUUsage();
67  }
68 
69  // should the thread, or the process, time be measured?
71 
72  bool running_ = false; // Is the timer running
73  double start_real_time_ = 0; // If running_
74  double start_cpu_time_ = 0; // If running_
75 
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;
81 };
82 
83 } // namespace internal
84 } // namespace benchmark
85 
86 #endif // BENCHMARK_THREAD_TIMER_H
benchmark::internal::ThreadTimer::SetIterationTime
void SetIterationTime(double seconds)
Definition: thread_timer.h:41
absl::time_internal::cctz::seconds
std::chrono::duration< std::int_fast64_t > seconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:40
benchmark::internal::ThreadTimer::start_real_time_
double start_real_time_
Definition: thread_timer.h:73
benchmark::ProcessCPUUsage
double ProcessCPUUsage()
Definition: benchmark/src/timers.cc:110
check.h
benchmark
Definition: bm_alarm.cc:55
benchmark::internal::ThreadTimer
Definition: thread_timer.h:10
benchmark::internal::ThreadTimer::cpu_time_used
double cpu_time_used() const
Definition: thread_timer.h:52
BM_CHECK
#define BM_CHECK(b)
Definition: benchmark/src/check.h:58
timers.h
benchmark::internal::ThreadTimer::manual_time_used_
double manual_time_used_
Definition: thread_timer.h:80
benchmark::internal::ThreadTimer::start_cpu_time_
double start_cpu_time_
Definition: thread_timer.h:74
benchmark::internal::ThreadTimer::StopTimer
void StopTimer()
Definition: thread_timer.h:30
benchmark::internal::ThreadTimer::ThreadTimer
ThreadTimer(bool measure_process_cpu_time_)
Definition: thread_timer.h:11
benchmark::internal::ThreadTimer::Create
static ThreadTimer Create()
Definition: thread_timer.h:15
benchmark::internal::ThreadTimer::StartTimer
void StartTimer()
Definition: thread_timer.h:23
benchmark::ChronoClockNow
double ChronoClockNow()
Definition: third_party/benchmark/src/timers.h:38
benchmark::internal::ThreadTimer::running_
bool running_
Definition: thread_timer.h:72
benchmark::internal::ThreadTimer::running
bool running() const
Definition: thread_timer.h:43
benchmark::ThreadCPUUsage
double ThreadCPUUsage()
Definition: benchmark/src/timers.cc:141
benchmark::internal::ThreadTimer::real_time_used
double real_time_used() const
Definition: thread_timer.h:46
benchmark::internal::ThreadTimer::ThreadTimer
ThreadTimer()=default
benchmark::internal::ThreadTimer::cpu_time_used_
double cpu_time_used_
Definition: thread_timer.h:78
benchmark::internal::ThreadTimer::real_time_used_
double real_time_used_
Definition: thread_timer.h:77
benchmark::internal::ThreadTimer::ReadCpuTimerOfChoice
double ReadCpuTimerOfChoice() const
Definition: thread_timer.h:64
benchmark::internal::ThreadTimer::measure_process_cpu_time
const bool measure_process_cpu_time
Definition: thread_timer.h:70
internal
Definition: benchmark/test/output_test_helper.cc:20
benchmark::internal::ThreadTimer::CreateProcessCpuTime
static ThreadTimer CreateProcessCpuTime()
Definition: thread_timer.h:18
benchmark::internal::ThreadTimer::manual_time_used
double manual_time_used() const
Definition: thread_timer.h:58


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:37