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 "platform/scheduler/base/thread_load_tracker.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 namespace blink { |
| 10 namespace scheduler { |
| 11 |
| 12 namespace { |
| 13 |
| 14 const int kLoadReportingIntervalInSeconds = 1; |
| 15 const int kWaitingPeriodBeforeReportingInSeconds = 10; |
| 16 |
| 17 } // namespace |
| 18 |
| 19 ThreadLoadTracker::ThreadLoadTracker(base::TimeTicks now, |
| 20 const Callback& callback) |
| 21 : time_(now), |
| 22 next_reporting_time_(now), |
| 23 thread_state_(ThreadState::ACTIVE), |
| 24 last_state_change_time_(now), |
| 25 waiting_period_( |
| 26 base::TimeDelta::FromSeconds(kWaitingPeriodBeforeReportingInSeconds)), |
| 27 reporting_interval_( |
| 28 base::TimeDelta::FromSeconds(kLoadReportingIntervalInSeconds)), |
| 29 callback_(callback) {} |
| 30 |
| 31 ThreadLoadTracker::~ThreadLoadTracker() {} |
| 32 |
| 33 void ThreadLoadTracker::Pause(base::TimeTicks now) { |
| 34 Advance(now, TaskState::IDLE); |
| 35 thread_state_ = ThreadState::PAUSED; |
| 36 last_state_change_time_ = now; |
| 37 } |
| 38 |
| 39 void ThreadLoadTracker::Resume(base::TimeTicks now) { |
| 40 Advance(now, TaskState::IDLE); |
| 41 thread_state_ = ThreadState::ACTIVE; |
| 42 last_state_change_time_ = now; |
| 43 } |
| 44 |
| 45 void ThreadLoadTracker::RecordTaskTime(base::TimeTicks start_time, |
| 46 base::TimeTicks end_time) { |
| 47 start_time = std::max(last_state_change_time_, start_time); |
| 48 end_time = std::max(last_state_change_time_, end_time); |
| 49 |
| 50 Advance(start_time, TaskState::IDLE); |
| 51 Advance(end_time, TaskState::TASK_RUNNING); |
| 52 } |
| 53 |
| 54 void ThreadLoadTracker::RecordIdle(base::TimeTicks now) { |
| 55 Advance(now, TaskState::IDLE); |
| 56 } |
| 57 |
| 58 void ThreadLoadTracker::Advance(base::TimeTicks now, TaskState task_state) { |
| 59 // This function advances |time_| to now and calls |callback_| |
| 60 // when appropriate. |
| 61 if (time_ > now) { |
| 62 return; |
| 63 } |
| 64 |
| 65 if (thread_state_ == ThreadState::PAUSED) { |
| 66 // If the load tracker is paused, bail out early. |
| 67 time_ = now; |
| 68 next_reporting_time_ = now + reporting_interval_; |
| 69 return; |
| 70 } |
| 71 |
| 72 while (time_ < now) { |
| 73 // Forward time_ to the earliest of following: |
| 74 // a) time to call |callback_| |
| 75 // b) requested time to forward (|now|). |
| 76 base::TimeTicks next_current_time = std::min(next_reporting_time_, now); |
| 77 |
| 78 base::TimeDelta delta = next_current_time - time_; |
| 79 |
| 80 // Forward time and recalculate |total_time_| and |total_runtime_|. |
| 81 if (thread_state_ == ThreadState::ACTIVE) { |
| 82 total_time_ += delta; |
| 83 if (task_state == TaskState::TASK_RUNNING) { |
| 84 total_runtime_ += delta; |
| 85 } |
| 86 } |
| 87 time_ = next_current_time; |
| 88 |
| 89 if (time_ == next_reporting_time_) { |
| 90 // Call |callback_| if need and update next callback time. |
| 91 if (thread_state_ == ThreadState::ACTIVE && |
| 92 total_time_ >= waiting_period_) { |
| 93 callback_.Run(time_, Load()); |
| 94 } |
| 95 next_reporting_time_ += reporting_interval_; |
| 96 } |
| 97 } |
| 98 } |
| 99 |
| 100 double ThreadLoadTracker::Load() { |
| 101 if (total_time_.is_zero()) { |
| 102 return 0; |
| 103 } |
| 104 return total_runtime_.InSecondsF() / total_time_.InSecondsF(); |
| 105 } |
| 106 |
| 107 } // namespace scheduler |
| 108 } // namespace blink |
OLD | NEW |