From ec1e09f939ab3298abc47b7494c6b7fb10a1ae40 Mon Sep 17 00:00:00 2001 From: pleroy Date: Thu, 29 May 2014 15:49:38 +0200 Subject: [PATCH] Ignore the CPU time consumed when timing is paused. Conflicts: src/benchmark.cc --- include/benchmark/benchmark.h | 6 ++++-- src/benchmark.cc | 30 ++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index e7ec5315..c8c2115d 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -276,8 +276,10 @@ class State { double start_time_; int64_t stop_time_micros_; - double start_pause_; - double pause_time_; + double start_pause_cpu_; + double pause_cpu_time_; + double start_pause_real_; + double pause_real_time_; // Total number of iterations for all finished runs. int64_t total_iterations_; diff --git a/src/benchmark.cc b/src/benchmark.cc index 1ad2e32a..8f589a79 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -940,8 +940,10 @@ State::State(FastClock* clock, SharedState* s, int t) start_cpu_(0.0), start_time_(0.0), stop_time_micros_(0.0), - start_pause_(0.0), - pause_time_(0.0), + start_pause_cpu_(0.0), + pause_cpu_time_(0.0), + start_pause_real_(0.0), + pause_real_time_(0.0), total_iterations_(0), interval_micros_(static_cast(kNumMicrosPerSecond * FLAGS_benchmark_min_time / @@ -955,7 +957,7 @@ State::State(FastClock* clock, SharedState* s, int t) bool State::KeepRunning() { // Fast path if ((FLAGS_benchmark_iterations == 0 && - !clock_->HasReached(stop_time_micros_ + pause_time_)) || + !clock_->HasReached(stop_time_micros_ + pause_real_time_)) || iterations_ < FLAGS_benchmark_iterations) { ++iterations_; return true; @@ -999,9 +1001,15 @@ bool State::KeepRunning() { return ret; } -void State::PauseTiming() { start_pause_ = walltime::Now(); } +void State::PauseTiming() { + start_pause_cpu_ = MyCPUUsage() + ChildrenCPUUsage(); + start_pause_real_ = walltime::Now(); +} -void State::ResumeTiming() { pause_time_ += walltime::Now() - start_pause_; } +void State::ResumeTiming() { + pause_cpu_time_ += MyCPUUsage() + ChildrenCPUUsage() - start_pause_cpu_; + pause_real_time_ += walltime::Now() - start_pause_real_; +} void State::SetBytesProcessed(int64_t bytes) { CHECK_EQ(STATE_STOPPED, state_); @@ -1078,7 +1086,8 @@ void State::NewInterval() { << "\n"; #endif iterations_ = 0; - pause_time_ = 0; + pause_cpu_time_ = 0; + pause_real_time_ = 0; start_cpu_ = MyCPUUsage() + ChildrenCPUUsage(); start_time_ = walltime::Now(); } else { @@ -1110,11 +1119,12 @@ bool State::FinishInterval() { const double accumulated_time = walltime::Now() - start_time_; const double total_overhead = overhead * iterations_; - CHECK_LT(pause_time_, accumulated_time); - CHECK_LT(pause_time_ + total_overhead, accumulated_time); + CHECK_LT(pause_real_time_, accumulated_time); + CHECK_LT(pause_real_time_ + total_overhead, accumulated_time); data.real_accumulated_time = - accumulated_time - (pause_time_ + total_overhead); - data.cpu_accumulated_time = (MyCPUUsage() + ChildrenCPUUsage()) - start_cpu_; + accumulated_time - (pause_real_time_ + total_overhead); + data.cpu_accumulated_time = (MyCPUUsage() + ChildrenCPUUsage()) - + (pause_cpu_time_ + start_cpu_); total_iterations_ += iterations_; bool keep_going = false;