OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/scheduler/renderer/throttling_helper.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "components/scheduler/base/real_time_domain.h" | |
9 #include "components/scheduler/base/virtual_time_domain.h" | |
10 #include "components/scheduler/child/scheduler_tqm_delegate.h" | |
11 #include "components/scheduler/renderer/renderer_scheduler_impl.h" | |
12 #include "components/scheduler/renderer/web_frame_scheduler_impl.h" | |
13 #include "third_party/WebKit/public/platform/WebFrameScheduler.h" | |
14 | |
15 namespace scheduler { | |
16 | |
17 ThrottlingHelper::ThrottlingHelper(RendererSchedulerImpl* renderer_scheduler, | |
18 const char* tracing_category) | |
19 : task_runner_(renderer_scheduler->ControlTaskRunner()), | |
20 renderer_scheduler_(renderer_scheduler), | |
21 tick_clock_(renderer_scheduler->tick_clock()), | |
22 tracing_category_(tracing_category), | |
23 time_domain_(new VirtualTimeDomain(this, tick_clock_->NowTicks())), | |
24 pending_pump_throttled_tasks_(false), | |
25 weak_factory_(this) { | |
26 pump_throttled_tasks_closure_ = base::Bind( | |
27 &ThrottlingHelper::PumpThrottledTasks, weak_factory_.GetWeakPtr()); | |
28 forward_immediate_work_closure_ = | |
29 base::Bind(&ThrottlingHelper::OnTimeDomainHasImmediateWork, | |
30 weak_factory_.GetWeakPtr()); | |
31 renderer_scheduler_->RegisterTimeDomain(time_domain_.get()); | |
32 } | |
33 | |
34 ThrottlingHelper::~ThrottlingHelper() { | |
35 renderer_scheduler_->UnregisterTimeDomain(time_domain_.get()); | |
36 } | |
37 | |
38 void ThrottlingHelper::Throttle(TaskQueue* task_queue) { | |
39 throttled_queues_.insert(task_queue); | |
40 | |
41 task_queue->SetTimeDomain(time_domain_.get()); | |
42 task_queue->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL); | |
43 | |
44 MaybeSchedulePumpThrottledTasksLocked(); | |
45 } | |
46 | |
47 void ThrottlingHelper::Unthrottle(TaskQueue* task_queue) { | |
48 throttled_queues_.erase(task_queue); | |
49 | |
50 task_queue->SetTimeDomain(renderer_scheduler_->real_time_domain()); | |
51 task_queue->SetPumpPolicy(TaskQueue::PumpPolicy::AUTO); | |
52 } | |
53 | |
54 void ThrottlingHelper::OnTimeDomainHasImmediateWork() { | |
55 // Forward to the main thread if called from another thread. | |
56 if (!task_runner_->RunsTasksOnCurrentThread()) { | |
57 task_runner_->PostTask(FROM_HERE, forward_immediate_work_closure_); | |
58 return; | |
59 } | |
60 TRACE_EVENT0(tracing_category_, | |
61 "ThrottlingHelper::OnTimeDomainHasImmediateWork"); | |
62 MaybeSchedulePumpThrottledTasksLocked(); | |
63 } | |
64 | |
65 void ThrottlingHelper::OnTimeDomainHasDelayedWork() { | |
66 TRACE_EVENT0(tracing_category_, | |
67 "ThrottlingHelper::OnTimeDomainHasDelayedWork"); | |
68 MaybeSchedulePumpThrottledTasksLocked(); | |
69 } | |
70 | |
71 void ThrottlingHelper::PumpThrottledTasks() { | |
72 TRACE_EVENT0(tracing_category_, "ThrottlingHelper::PumpThrottledTasks"); | |
73 pending_pump_throttled_tasks_ = false; | |
74 | |
75 time_domain_->AdvanceTo(tick_clock_->NowTicks()); | |
76 bool work_to_do = false; | |
77 for (TaskQueue* task_queue : throttled_queues_) { | |
78 if (task_queue->GetQueueState() == TaskQueue::QueueState::EMPTY) | |
79 continue; | |
80 | |
81 work_to_do = true; | |
82 task_queue->PumpQueue(); | |
83 } | |
84 | |
85 if (work_to_do) | |
86 MaybeSchedulePumpThrottledTasksLocked(); | |
87 } | |
88 | |
89 /* static */ | |
90 base::TimeDelta ThrottlingHelper::DelayToNextRunTimeInSeconds( | |
91 base::TimeTicks now) { | |
92 const base::TimeDelta one_second = base::TimeDelta::FromSeconds(1); | |
93 return one_second - ((now - base::TimeTicks()) % one_second); | |
94 } | |
95 | |
96 void ThrottlingHelper::MaybeSchedulePumpThrottledTasksLocked() { | |
97 if (pending_pump_throttled_tasks_) | |
98 return; | |
99 | |
100 pending_pump_throttled_tasks_ = true; | |
101 task_runner_->PostDelayedTask( | |
102 FROM_HERE, pump_throttled_tasks_closure_, | |
103 DelayToNextRunTimeInSeconds(tick_clock_->NowTicks())); | |
104 } | |
105 | |
106 } // namespace scheduler | |
OLD | NEW |