Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/scheduler/base/thread_load_tracker.h" | 5 #include "platform/scheduler/base/thread_load_tracker.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 namespace blink { | 9 namespace blink { |
| 10 namespace scheduler { | 10 namespace scheduler { |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 const int kLoadReportingIntervalInSeconds = 1; | 14 const int kLoadReportingIntervalInSeconds = 1; |
| 15 const int kWaitingPeriodBeforeReportingInSeconds = 10; | 15 const int kWaitingPeriodBeforeReportingInSeconds = 10; |
|
Sami
2016/10/05 15:28:12
The description talks about a one minute interval
altimin
2016/10/05 16:26:56
Description is wrong, fixed.
| |
| 16 | 16 |
| 17 } // namespace | 17 } // namespace |
| 18 | 18 |
| 19 ThreadLoadTracker::ThreadLoadTracker(base::TimeTicks now, | 19 ThreadLoadTracker::ThreadLoadTracker(base::TimeTicks now, |
| 20 const Callback& callback) | 20 const Callback& callback) |
| 21 : time_(now), | 21 : time_(now), |
| 22 next_reporting_time_(now), | |
| 23 thread_state_(ThreadState::ACTIVE), | 22 thread_state_(ThreadState::ACTIVE), |
| 24 last_state_change_time_(now), | 23 last_state_change_time_(now), |
| 25 waiting_period_( | 24 waiting_period_( |
| 26 base::TimeDelta::FromSeconds(kWaitingPeriodBeforeReportingInSeconds)), | 25 base::TimeDelta::FromSeconds(kWaitingPeriodBeforeReportingInSeconds)), |
| 27 reporting_interval_( | 26 reporting_interval_( |
| 28 base::TimeDelta::FromSeconds(kLoadReportingIntervalInSeconds)), | 27 base::TimeDelta::FromSeconds(kLoadReportingIntervalInSeconds)), |
| 29 callback_(callback) {} | 28 callback_(callback) { |
| 29 next_reporting_time_ = now + waiting_period_; | |
| 30 } | |
| 30 | 31 |
| 31 ThreadLoadTracker::~ThreadLoadTracker() {} | 32 ThreadLoadTracker::~ThreadLoadTracker() {} |
| 32 | 33 |
| 33 void ThreadLoadTracker::Pause(base::TimeTicks now) { | 34 void ThreadLoadTracker::Pause(base::TimeTicks now) { |
| 34 Advance(now, TaskState::IDLE); | 35 Advance(now, TaskState::IDLE); |
| 35 thread_state_ = ThreadState::PAUSED; | 36 thread_state_ = ThreadState::PAUSED; |
| 36 last_state_change_time_ = now; | 37 last_state_change_time_ = now; |
| 37 } | 38 } |
| 38 | 39 |
| 39 void ThreadLoadTracker::Resume(base::TimeTicks now) { | 40 void ThreadLoadTracker::Resume(base::TimeTicks now) { |
| 40 Advance(now, TaskState::IDLE); | 41 Advance(now, TaskState::IDLE); |
| 41 thread_state_ = ThreadState::ACTIVE; | 42 thread_state_ = ThreadState::ACTIVE; |
| 42 last_state_change_time_ = now; | 43 last_state_change_time_ = now; |
| 44 | |
| 45 next_reporting_time_ = now + reporting_interval_; | |
| 46 run_time_inside_window_ = base::TimeDelta(); | |
| 43 } | 47 } |
| 44 | 48 |
| 45 void ThreadLoadTracker::RecordTaskTime(base::TimeTicks start_time, | 49 void ThreadLoadTracker::RecordTaskTime(base::TimeTicks start_time, |
| 46 base::TimeTicks end_time) { | 50 base::TimeTicks end_time) { |
| 47 start_time = std::max(last_state_change_time_, start_time); | 51 start_time = std::max(last_state_change_time_, start_time); |
| 48 end_time = std::max(last_state_change_time_, end_time); | 52 end_time = std::max(last_state_change_time_, end_time); |
| 49 | 53 |
| 50 Advance(start_time, TaskState::IDLE); | 54 Advance(start_time, TaskState::IDLE); |
| 55 | |
|
Sami
2016/10/05 15:28:12
Meant to add this blank line?
altimin
2016/10/05 16:26:55
Done.
| |
| 51 Advance(end_time, TaskState::TASK_RUNNING); | 56 Advance(end_time, TaskState::TASK_RUNNING); |
| 52 } | 57 } |
| 53 | 58 |
| 54 void ThreadLoadTracker::RecordIdle(base::TimeTicks now) { | 59 void ThreadLoadTracker::RecordIdle(base::TimeTicks now) { |
| 55 Advance(now, TaskState::IDLE); | 60 Advance(now, TaskState::IDLE); |
| 56 } | 61 } |
| 57 | 62 |
| 63 namespace { | |
| 64 | |
| 65 // Calculates length of intersection of two time intervals. | |
| 66 base::TimeDelta Intersection(base::TimeTicks left1, | |
| 67 base::TimeTicks right1, | |
| 68 base::TimeTicks left2, | |
| 69 base::TimeTicks right2) { | |
| 70 base::TimeTicks left = std::max(left1, left2); | |
|
Sami
2016/10/05 15:28:12
DCHECK_LT(left1, right1);
DCHECK_LT(left2, right2)
altimin
2016/10/05 16:26:55
Done.
| |
| 71 base::TimeTicks right = std::min(right1, right2); | |
| 72 | |
| 73 if (left <= right) | |
| 74 return right - left; | |
| 75 | |
| 76 return base::TimeDelta(); | |
| 77 } | |
| 78 } | |
|
Sami
2016/10/05 15:28:12
} // namespace
altimin
2016/10/05 16:26:55
Done.
| |
| 79 | |
| 58 void ThreadLoadTracker::Advance(base::TimeTicks now, TaskState task_state) { | 80 void ThreadLoadTracker::Advance(base::TimeTicks now, TaskState task_state) { |
| 59 // This function advances |time_| to now and calls |callback_| | 81 // This function advances |time_| to now and calls |callback_| |
| 60 // when appropriate. | 82 // when appropriate. |
| 61 if (time_ > now) { | 83 if (time_ > now) { |
|
Sami
2016/10/05 15:28:12
Random question: why would the time ever go backwa
altimin
2016/10/05 16:26:55
I was afraid of renderer going offscreen / onscree
| |
| 62 return; | 84 return; |
| 63 } | 85 } |
| 64 | 86 |
| 65 if (thread_state_ == ThreadState::PAUSED) { | 87 if (thread_state_ == ThreadState::PAUSED) { |
| 66 // If the load tracker is paused, bail out early. | 88 // If the load tracker is paused, bail out early. |
| 67 time_ = now; | 89 time_ = now; |
| 68 next_reporting_time_ = now + reporting_interval_; | |
| 69 return; | 90 return; |
| 70 } | 91 } |
| 71 | 92 |
| 72 while (time_ < now) { | 93 while (time_ < now) { |
| 73 // Forward time_ to the earliest of following: | 94 // Forward time_ to the earliest of following: |
|
alex clarke (OOO till 29th)
2016/10/05 15:25:22
nit pick: s/Forward/Advance (here and below)
altimin
2016/10/05 16:26:55
Done.
| |
| 74 // a) time to call |callback_| | 95 // a) time to call |callback_| |
| 75 // b) requested time to forward (|now|). | 96 // b) requested time to forward (|now|). |
| 76 base::TimeTicks next_current_time = std::min(next_reporting_time_, now); | 97 base::TimeTicks next_current_time = std::min(next_reporting_time_, now); |
| 77 | 98 |
| 78 base::TimeDelta delta = next_current_time - time_; | 99 base::TimeDelta delta = next_current_time - time_; |
| 79 | 100 |
| 80 // Forward time and recalculate |total_time_| and |total_runtime_|. | 101 // Forward time and recalculate |recorded_time_| and |
|
alex clarke (OOO till 29th)
2016/10/05 15:25:22
maybe: Keep a running total of the time spent runn
altimin
2016/10/05 16:26:55
Done.
| |
| 102 // |run_time_inside_window_|. | |
| 81 if (thread_state_ == ThreadState::ACTIVE) { | 103 if (thread_state_ == ThreadState::ACTIVE) { |
|
alex clarke (OOO till 29th)
2016/10/05 15:25:23
Currently thread_state_ is always ThreadState::ACT
altimin
2016/10/05 16:26:55
Very well noticed, thanks!
| |
| 82 total_time_ += delta; | 104 recorded_time_ += delta; |
| 83 if (task_state == TaskState::TASK_RUNNING) { | 105 if (task_state == TaskState::TASK_RUNNING) { |
| 84 total_runtime_ += delta; | 106 run_time_inside_window_ += |
| 107 Intersection(next_reporting_time_ - reporting_interval_, | |
| 108 next_reporting_time_, time_, time_ + delta); | |
|
Sami
2016/10/05 15:28:12
time_ + delta will never be later than next_report
altimin
2016/10/05 16:26:55
Yes, but I believe that a simple intersection is e
| |
| 85 } | 109 } |
| 86 } | 110 } |
| 111 | |
| 87 time_ = next_current_time; | 112 time_ = next_current_time; |
| 88 | 113 |
| 89 if (time_ == next_reporting_time_) { | 114 if (time_ == next_reporting_time_) { |
| 90 // Call |callback_| if need and update next callback time. | 115 // Call |callback_| if need and update next callback time. |
| 91 if (thread_state_ == ThreadState::ACTIVE && | 116 if (thread_state_ == ThreadState::ACTIVE && |
| 92 total_time_ >= waiting_period_) { | 117 recorded_time_ >= waiting_period_) { |
| 93 callback_.Run(time_, Load()); | 118 callback_.Run(time_, Load()); |
| 94 } | 119 } |
| 95 next_reporting_time_ += reporting_interval_; | 120 next_reporting_time_ += reporting_interval_; |
| 121 run_time_inside_window_ = base::TimeDelta(); | |
| 96 } | 122 } |
| 97 } | 123 } |
| 98 } | 124 } |
| 99 | 125 |
| 100 double ThreadLoadTracker::Load() { | 126 double ThreadLoadTracker::Load() { |
| 101 if (total_time_.is_zero()) { | 127 return run_time_inside_window_.InSecondsF() / |
| 102 return 0; | 128 reporting_interval_.InSecondsF(); |
| 103 } | |
| 104 return total_runtime_.InSecondsF() / total_time_.InSecondsF(); | |
| 105 } | 129 } |
| 106 | 130 |
| 107 } // namespace scheduler | 131 } // namespace scheduler |
| 108 } // namespace blink | 132 } // namespace blink |
| OLD | NEW |