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