| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/scheduler/renderer/throttling_helper.h" | 5 #include "components/scheduler/renderer/throttling_helper.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "components/scheduler/base/real_time_domain.h" | 8 #include "components/scheduler/base/real_time_domain.h" |
| 9 #include "components/scheduler/child/scheduler_tqm_delegate.h" | 9 #include "components/scheduler/child/scheduler_tqm_delegate.h" |
| 10 #include "components/scheduler/renderer/auto_advancing_virtual_time_domain.h" |
| 10 #include "components/scheduler/renderer/renderer_scheduler_impl.h" | 11 #include "components/scheduler/renderer/renderer_scheduler_impl.h" |
| 11 #include "components/scheduler/renderer/throttled_time_domain.h" | 12 #include "components/scheduler/renderer/throttled_time_domain.h" |
| 12 #include "components/scheduler/renderer/web_frame_scheduler_impl.h" | 13 #include "components/scheduler/renderer/web_frame_scheduler_impl.h" |
| 13 #include "third_party/WebKit/public/platform/WebFrameScheduler.h" | 14 #include "third_party/WebKit/public/platform/WebFrameScheduler.h" |
| 14 | 15 |
| 15 namespace scheduler { | 16 namespace scheduler { |
| 16 | 17 |
| 17 ThrottlingHelper::ThrottlingHelper(RendererSchedulerImpl* renderer_scheduler, | 18 ThrottlingHelper::ThrottlingHelper(RendererSchedulerImpl* renderer_scheduler, |
| 18 const char* tracing_category) | 19 const char* tracing_category) |
| 19 : task_runner_(renderer_scheduler->ControlTaskRunner()), | 20 : task_runner_(renderer_scheduler->ControlTaskRunner()), |
| 20 renderer_scheduler_(renderer_scheduler), | 21 renderer_scheduler_(renderer_scheduler), |
| 21 tick_clock_(renderer_scheduler->tick_clock()), | 22 tick_clock_(renderer_scheduler->tick_clock()), |
| 22 tracing_category_(tracing_category), | 23 tracing_category_(tracing_category), |
| 23 time_domain_(new ThrottledTimeDomain(this, tracing_category)), | 24 time_domain_(new ThrottledTimeDomain(this, tracing_category)), |
| 25 virtual_time_(false), |
| 24 weak_factory_(this) { | 26 weak_factory_(this) { |
| 25 suspend_timers_when_backgrounded_closure_.Reset(base::Bind( | 27 pump_throttled_tasks_closure_.Reset(base::Bind( |
| 26 &ThrottlingHelper::PumpThrottledTasks, weak_factory_.GetWeakPtr())); | 28 &ThrottlingHelper::PumpThrottledTasks, weak_factory_.GetWeakPtr())); |
| 27 forward_immediate_work_closure_ = | 29 forward_immediate_work_closure_ = |
| 28 base::Bind(&ThrottlingHelper::OnTimeDomainHasImmediateWork, | 30 base::Bind(&ThrottlingHelper::OnTimeDomainHasImmediateWork, |
| 29 weak_factory_.GetWeakPtr()); | 31 weak_factory_.GetWeakPtr()); |
| 30 | 32 |
| 31 renderer_scheduler_->RegisterTimeDomain(time_domain_.get()); | 33 renderer_scheduler_->RegisterTimeDomain(time_domain_.get()); |
| 32 } | 34 } |
| 33 | 35 |
| 34 ThrottlingHelper::~ThrottlingHelper() { | 36 ThrottlingHelper::~ThrottlingHelper() { |
| 35 // It's possible for queues to be still throttled, so we need to tidy up | 37 // It's possible for queues to be still throttled, so we need to tidy up |
| (...skipping 20 matching lines...) Expand all Loading... |
| 56 // We don't enable the queue here because it's throttled and there might be | 58 // We don't enable the queue here because it's throttled and there might be |
| 57 // tasks in it's work queue that would execute immediatly rather than after | 59 // tasks in it's work queue that would execute immediatly rather than after |
| 58 // PumpThrottledTasks runs. | 60 // PumpThrottledTasks runs. |
| 59 if (!enabled) | 61 if (!enabled) |
| 60 task_queue->SetQueueEnabled(false); | 62 task_queue->SetQueueEnabled(false); |
| 61 } | 63 } |
| 62 | 64 |
| 63 void ThrottlingHelper::IncreaseThrottleRefCount(TaskQueue* task_queue) { | 65 void ThrottlingHelper::IncreaseThrottleRefCount(TaskQueue* task_queue) { |
| 64 DCHECK_NE(task_queue, task_runner_.get()); | 66 DCHECK_NE(task_queue, task_runner_.get()); |
| 65 | 67 |
| 68 if (virtual_time_) |
| 69 return; |
| 70 |
| 66 std::pair<TaskQueueMap::iterator, bool> insert_result = | 71 std::pair<TaskQueueMap::iterator, bool> insert_result = |
| 67 throttled_queues_.insert(std::make_pair( | 72 throttled_queues_.insert(std::make_pair( |
| 68 task_queue, Metadata(1, task_queue->IsQueueEnabled()))); | 73 task_queue, Metadata(1, task_queue->IsQueueEnabled()))); |
| 69 | 74 |
| 70 if (insert_result.second) { | 75 if (insert_result.second) { |
| 71 // The insert was succesful so we need to throttle the queue. | 76 // The insert was succesful so we need to throttle the queue. |
| 72 task_queue->SetTimeDomain(time_domain_.get()); | 77 task_queue->SetTimeDomain(time_domain_.get()); |
| 73 task_queue->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL); | 78 task_queue->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL); |
| 74 task_queue->SetQueueEnabled(false); | 79 task_queue->SetQueueEnabled(false); |
| 75 | 80 |
| 76 if (!task_queue->IsEmpty()) { | 81 if (!task_queue->IsEmpty()) { |
| 77 if (task_queue->HasPendingImmediateWork()) { | 82 if (task_queue->HasPendingImmediateWork()) { |
| 78 OnTimeDomainHasImmediateWork(); | 83 OnTimeDomainHasImmediateWork(); |
| 79 } else { | 84 } else { |
| 80 OnTimeDomainHasDelayedWork(); | 85 OnTimeDomainHasDelayedWork(); |
| 81 } | 86 } |
| 82 } | 87 } |
| 83 } else { | 88 } else { |
| 84 // An entry already existed in the map so we need to increment the refcount. | 89 // An entry already existed in the map so we need to increment the refcount. |
| 85 insert_result.first->second.throttling_ref_count++; | 90 insert_result.first->second.throttling_ref_count++; |
| 86 } | 91 } |
| 87 } | 92 } |
| 88 | 93 |
| 89 void ThrottlingHelper::DecreaseThrottleRefCount(TaskQueue* task_queue) { | 94 void ThrottlingHelper::DecreaseThrottleRefCount(TaskQueue* task_queue) { |
| 95 if (virtual_time_) |
| 96 return; |
| 97 |
| 90 TaskQueueMap::iterator iter = throttled_queues_.find(task_queue); | 98 TaskQueueMap::iterator iter = throttled_queues_.find(task_queue); |
| 91 | 99 |
| 92 if (iter != throttled_queues_.end() && | 100 if (iter != throttled_queues_.end() && |
| 93 --iter->second.throttling_ref_count == 0) { | 101 --iter->second.throttling_ref_count == 0) { |
| 94 bool enabled = iter->second.enabled; | 102 bool enabled = iter->second.enabled; |
| 95 // The refcount has become zero, we need to unthrottle the queue. | 103 // The refcount has become zero, we need to unthrottle the queue. |
| 96 throttled_queues_.erase(iter); | 104 throttled_queues_.erase(iter); |
| 97 | 105 |
| 98 task_queue->SetTimeDomain(renderer_scheduler_->real_time_domain()); | 106 task_queue->SetTimeDomain(renderer_scheduler_->real_time_domain()); |
| 99 task_queue->SetPumpPolicy(TaskQueue::PumpPolicy::AUTO); | 107 task_queue->SetPumpPolicy(TaskQueue::PumpPolicy::AUTO); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 base::TimeTicks unthrottled_runtime) { | 168 base::TimeTicks unthrottled_runtime) { |
| 161 const base::TimeDelta one_second = base::TimeDelta::FromSeconds(1); | 169 const base::TimeDelta one_second = base::TimeDelta::FromSeconds(1); |
| 162 return unthrottled_runtime + one_second - | 170 return unthrottled_runtime + one_second - |
| 163 ((unthrottled_runtime - base::TimeTicks()) % one_second); | 171 ((unthrottled_runtime - base::TimeTicks()) % one_second); |
| 164 } | 172 } |
| 165 | 173 |
| 166 void ThrottlingHelper::MaybeSchedulePumpThrottledTasksLocked( | 174 void ThrottlingHelper::MaybeSchedulePumpThrottledTasksLocked( |
| 167 const tracked_objects::Location& from_here, | 175 const tracked_objects::Location& from_here, |
| 168 base::TimeTicks now, | 176 base::TimeTicks now, |
| 169 base::TimeTicks unthrottled_runtime) { | 177 base::TimeTicks unthrottled_runtime) { |
| 178 if (virtual_time_) |
| 179 return; |
| 180 |
| 170 base::TimeTicks throttled_runtime = | 181 base::TimeTicks throttled_runtime = |
| 171 ThrottledRunTime(std::max(now, unthrottled_runtime)); | 182 ThrottledRunTime(std::max(now, unthrottled_runtime)); |
| 172 // If there is a pending call to PumpThrottledTasks and it's sooner than | 183 // If there is a pending call to PumpThrottledTasks and it's sooner than |
| 173 // |unthrottled_runtime| then return. | 184 // |unthrottled_runtime| then return. |
| 174 if (!pending_pump_throttled_tasks_runtime_.is_null() && | 185 if (!pending_pump_throttled_tasks_runtime_.is_null() && |
| 175 throttled_runtime >= pending_pump_throttled_tasks_runtime_) { | 186 throttled_runtime >= pending_pump_throttled_tasks_runtime_) { |
| 176 return; | 187 return; |
| 177 } | 188 } |
| 178 | 189 |
| 179 pending_pump_throttled_tasks_runtime_ = throttled_runtime; | 190 pending_pump_throttled_tasks_runtime_ = throttled_runtime; |
| 180 | 191 |
| 181 suspend_timers_when_backgrounded_closure_.Cancel(); | 192 pump_throttled_tasks_closure_.Cancel(); |
| 182 | 193 |
| 183 base::TimeDelta delay = pending_pump_throttled_tasks_runtime_ - now; | 194 base::TimeDelta delay = pending_pump_throttled_tasks_runtime_ - now; |
| 184 TRACE_EVENT1(tracing_category_, | 195 TRACE_EVENT1(tracing_category_, |
| 185 "ThrottlingHelper::MaybeSchedulePumpThrottledTasksLocked", | 196 "ThrottlingHelper::MaybeSchedulePumpThrottledTasksLocked", |
| 186 "delay_till_next_pump_ms", delay.InMilliseconds()); | 197 "delay_till_next_pump_ms", delay.InMilliseconds()); |
| 187 task_runner_->PostDelayedTask( | 198 task_runner_->PostDelayedTask( |
| 188 from_here, suspend_timers_when_backgrounded_closure_.callback(), delay); | 199 from_here, pump_throttled_tasks_closure_.callback(), delay); |
| 200 } |
| 201 |
| 202 void ThrottlingHelper::EnableVirtualTime() { |
| 203 virtual_time_ = true; |
| 204 |
| 205 pump_throttled_tasks_closure_.Cancel(); |
| 206 |
| 207 while (!throttled_queues_.empty()) { |
| 208 TaskQueue* task_queue = throttled_queues_.begin()->first; |
| 209 bool enabled = throttled_queues_.begin()->second.enabled; |
| 210 |
| 211 throttled_queues_.erase(throttled_queues_.begin()); |
| 212 |
| 213 task_queue->SetTimeDomain(renderer_scheduler_->GetVirtualTimeDomain()); |
| 214 task_queue->SetPumpPolicy(TaskQueue::PumpPolicy::AUTO); |
| 215 task_queue->SetQueueEnabled(enabled); |
| 216 } |
| 189 } | 217 } |
| 190 | 218 |
| 191 } // namespace scheduler | 219 } // namespace scheduler |
| OLD | NEW |