Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: components/scheduler/renderer/throttling_helper.cc

Issue 1441073006: Move throttling of background timers into the renderer scheduler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 work_to_do_(false),
26 weak_factory_(this) {
27 pump_throttled_tasks_closure_ = base::Bind(
28 &ThrottlingHelper::PumpThrottledTasks, weak_factory_.GetWeakPtr());
29 forward_immediate_work_closure_ =
30 base::Bind(&ThrottlingHelper::OnTimeDomainHasImmediateWork,
31 weak_factory_.GetWeakPtr());
32 renderer_scheduler_->RegisterTimeDomain(time_domain_);
33 }
34
35 ThrottlingHelper::~ThrottlingHelper() {
36 renderer_scheduler_->UnregisterTimeDomain(time_domain_);
37 }
38
39 void ThrottlingHelper::Throttle(TaskQueue* task_queue) {
40 throttled_queues_.insert(task_queue);
41
42 task_queue->SetTimeDomain(time_domain_);
43 task_queue->SetPumpPolicy(TaskQueue::PumpPolicy::MANUAL);
44
45 work_to_do_ |= task_queue->HasPendingImmediateTask();
46 work_to_do_ |= task_queue->HasPendingDelayedTask();
47
48 MaybeSchedulePumpThrottledTasksLocked();
49 }
50
51 void ThrottlingHelper::Unthrottle(TaskQueue* task_queue) {
52 throttled_queues_.erase(task_queue);
53
54 task_queue->SetTimeDomain(renderer_scheduler_->real_time_domain());
55 task_queue->SetPumpPolicy(TaskQueue::PumpPolicy::AUTO);
56 if (throttled_queues_.empty())
57 work_to_do_ = false;
58 }
59
60 void ThrottlingHelper::Unregister(TaskQueue* task_queue) {
61 throttled_queues_.erase(task_queue);
62
Sami 2015/11/25 17:12:25 DCHECK that the task queue isn't still in the virt
alex clarke (OOO till 29th) 2015/11/26 18:43:27 Acknowledged.
63 if (throttled_queues_.empty())
64 work_to_do_ = false;
65 }
66
67 void ThrottlingHelper::OnTimeDomainHasImmediateWork() {
68 // Forward to the main thread if called from another thread.
69 if (!task_runner_->RunsTasksOnCurrentThread()) {
70 task_runner_->PostTask(FROM_HERE, forward_immediate_work_closure_);
71 return;
72 }
73 TRACE_EVENT0(tracing_category_,
74 "ThrottlingHelper::OnTimeDomainHasImmediateWork");
75 work_to_do_ = true;
76 MaybeSchedulePumpThrottledTasksLocked();
77 }
78
79 void ThrottlingHelper::OnTimeDomainHasDelayedWork() {
80 TRACE_EVENT0(tracing_category_,
81 "ThrottlingHelper::OnTimeDomainHasDelayedWork");
82 work_to_do_ = true;
83 MaybeSchedulePumpThrottledTasksLocked();
84 }
85
86 void ThrottlingHelper::PumpThrottledTasks() {
87 TRACE_EVENT0(tracing_category_, "ThrottlingHelper::PumpThrottledTasks");
88 pending_pump_throttled_tasks_ = false;
89
90 time_domain_->AdvanceTo(tick_clock_->NowTicks());
91 work_to_do_ = false;
Sami 2015/11/25 17:12:25 Do we need this flag? Won't OnTimeDomainHasImmedia
alex clarke (OOO till 29th) 2015/11/26 18:43:27 Done.
92 for (TaskQueue* task_queue : throttled_queues_) {
93 if (task_queue->HasPendingImmediateTask() ||
94 task_queue->HasPendingDelayedTask()) {
95 task_queue->PumpQueue();
96 work_to_do_ = true;
97 }
98 }
99
100 MaybeSchedulePumpThrottledTasksLocked();
101 }
102
103 /* static */
104 base::TimeDelta ThrottlingHelper::DelayToNextRunTimeInSeconds(
105 base::TimeTicks now) {
106 const base::TimeDelta one_second = base::TimeDelta::FromSeconds(1);
107 return one_second - ((now - base::TimeTicks()) % one_second);
108 }
109
110 void ThrottlingHelper::MaybeSchedulePumpThrottledTasksLocked() {
111 if (pending_pump_throttled_tasks_ || !work_to_do_)
112 return;
113
114 pending_pump_throttled_tasks_ = true;
115 task_runner_->PostDelayedTask(
116 FROM_HERE, pump_throttled_tasks_closure_,
117 DelayToNextRunTimeInSeconds(tick_clock_->NowTicks()));
118 }
119
120 } // namespace scheduler
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698