Chromium Code Reviews| Index: components/scheduler/renderer/queueing_time_estimator.cc |
| diff --git a/components/scheduler/renderer/queueing_time_estimator.cc b/components/scheduler/renderer/queueing_time_estimator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..55c3fee3f4bb43ff0d870e0c7c4e284154f8ebee |
| --- /dev/null |
| +++ b/components/scheduler/renderer/queueing_time_estimator.cc |
| @@ -0,0 +1,108 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/scheduler/renderer/queueing_time_estimator.h" |
| + |
| +#include "components/scheduler/renderer/coarse_duration_timer.h" |
| + |
| +namespace scheduler { |
| + |
| +namespace { |
| + |
| +base::TimeDelta ExpectedQueueingTimeFromTask(base::TimeTicks task_start, |
| + base::TimeTicks task_end, |
| + base::TimeTicks window_start, |
| + base::TimeTicks window_end) { |
| + DCHECK(task_start <= task_end); |
|
Lei Zhang
2016/06/08 01:05:27
DCHECK_LE() and friends are preferred, BTW. They w
tdresser
2016/06/08 14:25:05
Thanks, I'll clean this up if the overall approach
|
| + DCHECK(task_start <= window_end); |
| + DCHECK(window_start < window_end); |
| + DCHECK(task_end >= window_start); |
| + base::TimeTicks task_in_window_start_time = |
| + std::max(task_start, window_start); |
| + base::TimeTicks task_in_window_end_time = |
| + std::min(task_end, window_end); |
| + DCHECK(task_in_window_end_time <= task_in_window_end_time); |
| + |
| + double probability_of_this_task = |
| + static_cast<double>((task_in_window_end_time - task_in_window_start_time) |
| + .InMicroseconds()) / |
| + (window_end - window_start).InMicroseconds(); |
| + |
| + base::TimeDelta expected_queueing_duration_within_task = |
| + ((task_end - task_in_window_start_time) + |
| + (task_end - task_in_window_end_time)) / |
| + 2; |
| + |
| + return base::TimeDelta::FromMillisecondsD( |
| + probability_of_this_task * |
| + expected_queueing_duration_within_task.InMillisecondsF()); |
| +} |
| + |
| +} // namespace |
| + |
| +QueueingTimeEstimator::QueueingTimeEstimator( |
| + QueueingTimeEstimator::Client* client, |
| + CoarseDurationTimer* timer, |
| + base::TimeDelta window_duration) |
| + : client_(client), |
| + timer_(timer), |
| + outstanding_task_count_(0), |
| + window_duration_(window_duration) {} |
| + |
| +QueueingTimeEstimator::~QueueingTimeEstimator() {} |
| + |
| +void QueueingTimeEstimator::WillProcessTask( |
| + const base::PendingTask& pending_task) { |
| + // Avoid measuring the duration in nested run loops. |
| + if (++outstanding_task_count_ != 1) |
| + return; |
| + |
| + timer_->BeginInterval(); |
| +} |
| + |
| +void QueueingTimeEstimator::DidProcessTask( |
| + const base::PendingTask& pending_task) { |
| + if (--outstanding_task_count_ != 0) |
| + return; |
| + |
| + base::TimeDelta duration = timer_->EndInterval(); |
| + if (duration.is_zero()) |
| + return; |
| + |
| + base::TimeTicks task_end_time = timer_->most_recent_time(); |
| + base::TimeTicks task_start_time = task_end_time - duration; |
| + |
| + // If this is the first task, set the window start time such that the window |
| + // and the task end at the same time. |
| + if (window_start_time_.is_null()) |
| + window_start_time_ = task_end_time - window_duration_; |
| + |
| + bool start_past_window = |
| + task_start_time > window_start_time_ + window_duration_; |
| + bool end_past_window = |
| + task_end_time > window_start_time_ + window_duration_; |
| + |
| + while (end_past_window) { |
| + if (!start_past_window) { |
| + // Include the current task in this window. |
| + current_expected_queueing_time_ += ExpectedQueueingTimeFromTask( |
| + task_start_time, task_end_time, window_start_time_, |
| + window_start_time_ + window_duration_); |
| + } |
| + client_->OnQueueingTimeForWindowEstimated(current_expected_queueing_time_); |
| + window_start_time_ += window_duration_; |
| + current_expected_queueing_time_ = base::TimeDelta(); |
| + |
| + start_past_window = |
| + task_start_time > window_start_time_ + window_duration_; |
| + end_past_window = |
| + task_end_time > window_start_time_ + window_duration_; |
| + } |
| + |
| + current_expected_queueing_time_ += ExpectedQueueingTimeFromTask( |
| + task_start_time, task_end_time, window_start_time_, |
| + window_start_time_ + window_duration_); |
| +} |
| + |
| +} // namespace scheduler |