Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/scheduler/base/queueing_time_estimator.h" | |
| 6 | |
| 7 #include "base/time/default_tick_clock.h" | |
| 8 | |
| 9 namespace scheduler { | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 base::TimeDelta ExpectedQueueingTimeFromTask(base::TimeTicks task_start, | |
|
alex clarke (OOO till 29th)
2016/07/05 13:47:45
Please add a comment explaining the maths behind h
tdresser
2016/07/05 14:30:13
Done.
| |
| 14 base::TimeTicks task_end, | |
| 15 base::TimeTicks window_start, | |
| 16 base::TimeTicks window_end) { | |
| 17 DCHECK(task_start <= task_end); | |
| 18 DCHECK(task_start <= window_end); | |
| 19 DCHECK(window_start < window_end); | |
| 20 DCHECK(task_end >= window_start); | |
| 21 base::TimeTicks task_in_window_start_time = | |
| 22 std::max(task_start, window_start); | |
| 23 base::TimeTicks task_in_window_end_time = | |
| 24 std::min(task_end, window_end); | |
| 25 DCHECK(task_in_window_end_time <= task_in_window_end_time); | |
| 26 | |
| 27 double probability_of_this_task = | |
| 28 static_cast<double>((task_in_window_end_time - task_in_window_start_time) | |
| 29 .InMicroseconds()) / | |
| 30 (window_end - window_start).InMicroseconds(); | |
| 31 | |
| 32 base::TimeDelta expected_queueing_duration_within_task = | |
| 33 ((task_end - task_in_window_start_time) + | |
| 34 (task_end - task_in_window_end_time)) / | |
| 35 2; | |
| 36 | |
| 37 return base::TimeDelta::FromMillisecondsD( | |
| 38 probability_of_this_task * | |
| 39 expected_queueing_duration_within_task.InMillisecondsF()); | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 QueueingTimeEstimator::QueueingTimeEstimator( | |
| 45 QueueingTimeEstimator::Client* client, | |
| 46 base::TimeDelta window_duration) | |
| 47 : client_(client), | |
| 48 window_duration_(window_duration), | |
| 49 window_start_time_() {} | |
| 50 | |
| 51 void QueueingTimeEstimator::OnToplevelTaskCompleted( | |
| 52 base::TimeTicks task_start_time, | |
| 53 base::TimeTicks task_end_time) { | |
| 54 if (window_start_time_.is_null()) | |
| 55 window_start_time_ = task_start_time; | |
| 56 | |
| 57 bool start_past_window = | |
| 58 task_start_time > window_start_time_ + window_duration_; | |
| 59 bool end_past_window = task_end_time > window_start_time_ + window_duration_; | |
|
alex clarke (OOO till 29th)
2016/07/05 13:47:45
Maybe make end_past_window a private helper functi
tdresser
2016/07/05 14:30:13
Used helper method for both.
| |
| 60 | |
| 61 while (end_past_window) { | |
|
alex clarke (OOO till 29th)
2016/07/05 13:47:45
I hope this is unlikely (given |window_duration_|
tdresser
2016/07/05 14:30:13
We can't make this O(1), because we want to report
| |
| 62 if (!start_past_window) { | |
| 63 // Include the current task in this window. | |
| 64 current_expected_queueing_time_ += ExpectedQueueingTimeFromTask( | |
| 65 task_start_time, task_end_time, window_start_time_, | |
| 66 window_start_time_ + window_duration_); | |
| 67 } | |
| 68 client_->OnQueueingTimeForWindowEstimated(current_expected_queueing_time_); | |
| 69 window_start_time_ += window_duration_; | |
| 70 current_expected_queueing_time_ = base::TimeDelta(); | |
|
alex clarke (OOO till 29th)
2016/07/05 13:47:45
Seems like we're loosing data here. Does this caus
tdresser
2016/07/05 14:30:13
Sorry, what data are we losing? We just reported o
| |
| 71 | |
| 72 start_past_window = | |
| 73 task_start_time > window_start_time_ + window_duration_; | |
| 74 end_past_window = task_end_time > window_start_time_ + window_duration_; | |
| 75 } | |
| 76 | |
| 77 current_expected_queueing_time_ += ExpectedQueueingTimeFromTask( | |
| 78 task_start_time, task_end_time, window_start_time_, | |
| 79 window_start_time_ + window_duration_); | |
| 80 } | |
| 81 | |
| 82 } // namespace scheduler | |
| OLD | NEW |