Chromium Code Reviews| Index: third_party/WebKit/Source/platform/scheduler/base/thread_load_tracker.cc |
| diff --git a/third_party/WebKit/Source/platform/scheduler/base/thread_load_tracker.cc b/third_party/WebKit/Source/platform/scheduler/base/thread_load_tracker.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b579cdd26763579014908a6e9bfbfee264b8bb91 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/platform/scheduler/base/thread_load_tracker.cc |
| @@ -0,0 +1,96 @@ |
| +#include "platform/scheduler/base/thread_load_tracker.h" |
| + |
| +namespace blink { |
| +namespace scheduler { |
| + |
| +namespace { |
| + |
| +const int kLoadReportingIntervalInMilliseconds = 1000; |
|
alex clarke (OOO till 29th)
2016/08/22 16:13:13
nit: any particular reason for using different uni
altimin
2016/08/22 17:02:22
Initially it was 100, but I thought that it's a ba
|
| +const int kWaitingPeriodBeforeReportingInSeconds = 10; |
| + |
| +} // namespace |
| + |
| +ThreadLoadTracker::ThreadLoadTracker(base::TimeTicks now, |
| + const Callback& callback) |
|
alex clarke (OOO till 29th)
2016/08/22 16:13:13
nit: indent looks wrong in several places.
altimin
2016/08/22 17:02:22
I've run "git cl format", but it seems to work str
|
| + : current_time_(now), |
| + next_reporting_time_(now), |
| + thread_state_(ThreadState::ACTIVE), |
| + last_state_change_time_(now), |
| + waiting_period_( |
| + base::TimeDelta::FromSeconds(kWaitingPeriodBeforeReportingInSeconds)), |
| + reporting_interval_(base::TimeDelta::FromMilliseconds( |
| + kLoadReportingIntervalInMilliseconds)), |
| + callback_(callback) {} |
| + |
| +ThreadLoadTracker::~ThreadLoadTracker() {} |
| + |
| +void ThreadLoadTracker::Pause(base::TimeTicks now) { |
| + Advance(now, TaskState::IDLE); |
| + thread_state_ = ThreadState::PAUSED; |
| + last_state_change_time_ = now; |
| +} |
| + |
| +void ThreadLoadTracker::Resume(base::TimeTicks now) { |
| + Advance(now, TaskState::IDLE); |
| + thread_state_ = ThreadState::ACTIVE; |
| + last_state_change_time_ = now; |
| +} |
| + |
| +void ThreadLoadTracker::RecordTaskTime(base::TimeTicks start_time, |
| + base::TimeTicks end_time) { |
| + start_time = std::max(last_state_change_time_, start_time); |
| + end_time = std::max(last_state_change_time_, end_time); |
| + |
| + Advance(start_time, TaskState::IDLE); |
| + Advance(end_time, TaskState::TASK_RUNNING); |
| +} |
| + |
| +void ThreadLoadTracker::RecordIdle(base::TimeTicks now) { |
| + Advance(now, TaskState::IDLE); |
| +} |
| + |
| +void ThreadLoadTracker::Advance(base::TimeTicks now, |
| + TaskState task_state) { |
| + if (current_time_ > now) { |
| + return; |
| + } |
| + |
| + if (thread_state_ == ThreadState::PAUSED) { |
| + current_time_ = now; |
| + next_reporting_time_ = now + reporting_interval_; |
| + return; |
| + } |
| + |
| + while (true) { |
| + base::TimeTicks next_current_time = std::min(next_reporting_time_, now); |
| + |
| + base::TimeDelta delta = next_current_time - current_time_; |
| + |
| + if (thread_state_ == ThreadState::ACTIVE) { |
| + total_time_ += delta; |
| + if (task_state == TaskState::TASK_RUNNING) { |
| + total_runtime_ += delta; |
| + } |
| + } |
| + current_time_ = next_current_time; |
| + |
| + if (current_time_ == next_reporting_time_) { |
| + if (thread_state_ == ThreadState::ACTIVE && total_time_ >= waiting_period_) { |
| + callback_.Run(current_time_, Load()); |
| + } |
| + next_reporting_time_ += reporting_interval_; |
| + } else { |
| + break; |
| + } |
| + } |
| +} |
| + |
| +double ThreadLoadTracker::Load() { |
| + if (total_time_.is_zero()) { |
| + return 0; |
| + } |
| + return total_runtime_.InSecondsF() / total_time_.InSecondsF(); |
| +} |
| + |
| +} // namespace scheduler |
| +} // namespace blink |