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 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_TASK_QUEUE_THROTTL
ER_H_ | 5 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_TASK_QUEUE_THROTTL
ER_H_ |
6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_TASK_QUEUE_THROTTL
ER_H_ | 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_TASK_QUEUE_THROTTL
ER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <unordered_map> | 9 #include <unordered_map> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/optional.h" | 13 #include "base/optional.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "platform/scheduler/base/cancelable_closure_holder.h" | 15 #include "platform/scheduler/base/cancelable_closure_holder.h" |
16 #include "platform/scheduler/base/time_domain.h" | 16 #include "platform/scheduler/base/time_domain.h" |
17 #include "platform/scheduler/renderer/budget_pool.h" | 17 #include "platform/scheduler/renderer/budget_pool.h" |
| 18 #include "platform/scheduler/renderer/cpu_time_budget_pool.h" |
| 19 #include "platform/scheduler/renderer/wake_up_budget_pool.h" |
18 #include "platform/scheduler/renderer/web_view_scheduler.h" | 20 #include "platform/scheduler/renderer/web_view_scheduler.h" |
19 | 21 |
20 namespace base { | 22 namespace base { |
21 namespace trace_event { | 23 namespace trace_event { |
22 class TracedValue; | 24 class TracedValue; |
23 } | 25 } |
24 } | 26 } |
25 | 27 |
26 namespace blink { | 28 namespace blink { |
27 namespace scheduler { | 29 namespace scheduler { |
28 | 30 |
29 class BudgetPool; | 31 class BudgetPool; |
30 class RendererSchedulerImpl; | 32 class RendererSchedulerImpl; |
31 class ThrottledTimeDomain; | 33 class ThrottledTimeDomain; |
32 class CPUTimeBudgetPool; | 34 class CPUTimeBudgetPool; |
| 35 class WakeUpBudgetPool; |
| 36 |
| 37 // kNewTasksOnly prevents new tasks from running (old tasks can run normally), |
| 38 // kAllTasks block queue completely. |
| 39 // kAllTasks-type block always blocks the queue completely. |
| 40 // kNewTasksOnly-type block does nothing when queue is already blocked by |
| 41 // kAllTasks, and overrides previous kNewTasksOnly block if any, which may |
| 42 // unblock some tasks. |
| 43 enum class QueueBlockType { kAllTasks, kNewTasksOnly }; |
33 | 44 |
34 // Interface for BudgetPool to interact with TaskQueueThrottler. | 45 // Interface for BudgetPool to interact with TaskQueueThrottler. |
35 class BLINK_PLATFORM_EXPORT BudgetPoolController { | 46 class BLINK_PLATFORM_EXPORT BudgetPoolController { |
36 public: | 47 public: |
37 virtual ~BudgetPoolController() {} | 48 virtual ~BudgetPoolController() {} |
38 | 49 |
39 // To be used by BudgetPool only, use BudgetPool::{Add,Remove}Queue | 50 // To be used by BudgetPool only, use BudgetPool::{Add,Remove}Queue |
40 // methods instead. | 51 // methods instead. |
41 virtual void AddQueueToBudgetPool(TaskQueue* queue, | 52 virtual void AddQueueToBudgetPool(TaskQueue* queue, |
42 BudgetPool* budget_pool) = 0; | 53 BudgetPool* budget_pool) = 0; |
43 virtual void RemoveQueueFromBudgetPool(TaskQueue* queue, | 54 virtual void RemoveQueueFromBudgetPool(TaskQueue* queue, |
44 BudgetPool* budget_pool) = 0; | 55 BudgetPool* budget_pool) = 0; |
45 | 56 |
46 // Deletes the budget pool. | 57 // Deletes the budget pool. |
47 virtual void UnregisterBudgetPool(BudgetPool* budget_pool) = 0; | 58 virtual void UnregisterBudgetPool(BudgetPool* budget_pool) = 0; |
48 | 59 |
49 // Insert a fence to prevent tasks from running and schedule a wake-up at | 60 // Ensure that an appropriate type of the fence is installed and schedule |
50 // an appropriate time. | 61 // a pump for this queue when needed. |
51 virtual void BlockQueue(base::TimeTicks now, TaskQueue* queue) = 0; | 62 virtual void UpdateQueueThrottlingState(base::TimeTicks now, |
52 | 63 TaskQueue* queue) = 0; |
53 // Schedule a call to unblock queue at an appropriate moment. | |
54 virtual void UnblockQueue(base::TimeTicks now, TaskQueue* queue) = 0; | |
55 | 64 |
56 // Returns true if the |queue| is throttled (i.e. added to TaskQueueThrottler | 65 // Returns true if the |queue| is throttled (i.e. added to TaskQueueThrottler |
57 // and throttling is not disabled). | 66 // and throttling is not disabled). |
58 virtual bool IsThrottled(TaskQueue* queue) const = 0; | 67 virtual bool IsThrottled(TaskQueue* queue) const = 0; |
59 }; | 68 }; |
60 | 69 |
61 // The job of the TaskQueueThrottler is to control when tasks posted on | 70 // The job of the TaskQueueThrottler is to control when tasks posted on |
62 // throttled queues get run. The TaskQueueThrottler: | 71 // throttled queues get run. The TaskQueueThrottler: |
63 // - runs throttled tasks once per second, | 72 // - runs throttled tasks once per second, |
64 // - controls time budget for task queues grouped in CPUTimeBudgetPools. | 73 // - controls time budget for task queues grouped in CPUTimeBudgetPools. |
(...skipping 27 matching lines...) Expand all Loading... |
92 | 101 |
93 // TaskQueue::Observer implementation: | 102 // TaskQueue::Observer implementation: |
94 void OnQueueNextWakeUpChanged(TaskQueue* queue, | 103 void OnQueueNextWakeUpChanged(TaskQueue* queue, |
95 base::TimeTicks wake_up) override; | 104 base::TimeTicks wake_up) override; |
96 | 105 |
97 // BudgetPoolController implementation: | 106 // BudgetPoolController implementation: |
98 void AddQueueToBudgetPool(TaskQueue* queue, BudgetPool* budget_pool) override; | 107 void AddQueueToBudgetPool(TaskQueue* queue, BudgetPool* budget_pool) override; |
99 void RemoveQueueFromBudgetPool(TaskQueue* queue, | 108 void RemoveQueueFromBudgetPool(TaskQueue* queue, |
100 BudgetPool* budget_pool) override; | 109 BudgetPool* budget_pool) override; |
101 void UnregisterBudgetPool(BudgetPool* budget_pool) override; | 110 void UnregisterBudgetPool(BudgetPool* budget_pool) override; |
102 void BlockQueue(base::TimeTicks now, TaskQueue* queue) override; | 111 void UpdateQueueThrottlingState(base::TimeTicks now, |
103 void UnblockQueue(base::TimeTicks now, TaskQueue* queue) override; | 112 TaskQueue* queue) override; |
104 bool IsThrottled(TaskQueue* queue) const override; | 113 bool IsThrottled(TaskQueue* queue) const override; |
105 | 114 |
106 // Increments the throttled refcount and causes |task_queue| to be throttled | 115 // Increments the throttled refcount and causes |task_queue| to be throttled |
107 // if its not already throttled. | 116 // if its not already throttled. |
108 void IncreaseThrottleRefCount(TaskQueue* task_queue); | 117 void IncreaseThrottleRefCount(TaskQueue* task_queue); |
109 | 118 |
110 // If the refcouint is non-zero it's decremented. If the throttled refcount | 119 // If the refcouint is non-zero it's decremented. If the throttled refcount |
111 // becomes zero then |task_queue| is unthrottled. If the refcount was already | 120 // becomes zero then |task_queue| is unthrottled. If the refcount was already |
112 // zero this function does nothing. | 121 // zero this function does nothing. |
113 void DecreaseThrottleRefCount(TaskQueue* task_queue); | 122 void DecreaseThrottleRefCount(TaskQueue* task_queue); |
(...skipping 11 matching lines...) Expand all Loading... |
125 | 134 |
126 const ThrottledTimeDomain* time_domain() const { return time_domain_.get(); } | 135 const ThrottledTimeDomain* time_domain() const { return time_domain_.get(); } |
127 | 136 |
128 static base::TimeTicks AlignedThrottledRunTime( | 137 static base::TimeTicks AlignedThrottledRunTime( |
129 base::TimeTicks unthrottled_runtime); | 138 base::TimeTicks unthrottled_runtime); |
130 | 139 |
131 const scoped_refptr<TaskQueue>& task_runner() const { return task_runner_; } | 140 const scoped_refptr<TaskQueue>& task_runner() const { return task_runner_; } |
132 | 141 |
133 // Returned object is owned by |TaskQueueThrottler|. | 142 // Returned object is owned by |TaskQueueThrottler|. |
134 CPUTimeBudgetPool* CreateCPUTimeBudgetPool(const char* name); | 143 CPUTimeBudgetPool* CreateCPUTimeBudgetPool(const char* name); |
| 144 WakeUpBudgetPool* CreateWakeUpBudgetPool(const char* name); |
135 | 145 |
136 // Accounts for given task for cpu-based throttling needs. | 146 // Accounts for given task for cpu-based throttling needs. |
137 void OnTaskRunTimeReported(TaskQueue* task_queue, | 147 void OnTaskRunTimeReported(TaskQueue* task_queue, |
138 base::TimeTicks start_time, | 148 base::TimeTicks start_time, |
139 base::TimeTicks end_time); | 149 base::TimeTicks end_time); |
140 | 150 |
141 void AsValueInto(base::trace_event::TracedValue* state, | 151 void AsValueInto(base::trace_event::TracedValue* state, |
142 base::TimeTicks now) const; | 152 base::TimeTicks now) const; |
143 private: | 153 private: |
144 struct Metadata { | 154 struct Metadata { |
(...skipping 10 matching lines...) Expand all Loading... |
155 // Note |unthrottled_runtime| might be in the past. When this happens we | 165 // Note |unthrottled_runtime| might be in the past. When this happens we |
156 // compute the delay to the next runtime based on now rather than | 166 // compute the delay to the next runtime based on now rather than |
157 // unthrottled_runtime. | 167 // unthrottled_runtime. |
158 void MaybeSchedulePumpThrottledTasks( | 168 void MaybeSchedulePumpThrottledTasks( |
159 const tracked_objects::Location& from_here, | 169 const tracked_objects::Location& from_here, |
160 base::TimeTicks now, | 170 base::TimeTicks now, |
161 base::TimeTicks runtime); | 171 base::TimeTicks runtime); |
162 | 172 |
163 // Return next possible time when queue is allowed to run in accordance | 173 // Return next possible time when queue is allowed to run in accordance |
164 // with throttling policy. | 174 // with throttling policy. |
165 base::TimeTicks GetNextAllowedRunTime(base::TimeTicks now, TaskQueue* queue); | 175 base::TimeTicks GetNextAllowedRunTime(TaskQueue* queue, |
| 176 base::TimeTicks desired_run_time); |
| 177 |
| 178 bool CanRunTasksAt(TaskQueue* queue, base::TimeTicks moment, bool is_wakeup); |
| 179 |
| 180 bool CanRunTasksUntil(TaskQueue* queue, |
| 181 base::TimeTicks now, |
| 182 base::TimeTicks moment); |
166 | 183 |
167 void MaybeDeleteQueueMetadata(TaskQueueMap::iterator it); | 184 void MaybeDeleteQueueMetadata(TaskQueueMap::iterator it); |
168 | 185 |
169 // Schedule a call PumpThrottledTasks at an appropriate moment for this queue. | 186 void UpdateQueueThrottlingStateInternal(base::TimeTicks now, |
170 void SchedulePumpQueue(const tracked_objects::Location& from_here, | 187 TaskQueue* queue, |
171 base::TimeTicks now, | 188 bool is_wake_up); |
172 TaskQueue* queue); | 189 |
| 190 base::Optional<QueueBlockType> GetQueueBlockType(base::TimeTicks now, |
| 191 TaskQueue* queue); |
173 | 192 |
174 TaskQueueMap queue_details_; | 193 TaskQueueMap queue_details_; |
175 base::Callback<void(TaskQueue*, base::TimeTicks)> | 194 base::Callback<void(TaskQueue*, base::TimeTicks)> |
176 forward_immediate_work_callback_; | 195 forward_immediate_work_callback_; |
177 scoped_refptr<TaskQueue> task_runner_; | 196 scoped_refptr<TaskQueue> task_runner_; |
178 RendererSchedulerImpl* renderer_scheduler_; // NOT OWNED | 197 RendererSchedulerImpl* renderer_scheduler_; // NOT OWNED |
179 base::TickClock* tick_clock_; // NOT OWNED | 198 base::TickClock* tick_clock_; // NOT OWNED |
180 const char* tracing_category_; // NOT OWNED | 199 const char* tracing_category_; // NOT OWNED |
181 std::unique_ptr<ThrottledTimeDomain> time_domain_; | 200 std::unique_ptr<ThrottledTimeDomain> time_domain_; |
182 | 201 |
183 CancelableClosureHolder pump_throttled_tasks_closure_; | 202 CancelableClosureHolder pump_throttled_tasks_closure_; |
184 base::Optional<base::TimeTicks> pending_pump_throttled_tasks_runtime_; | 203 base::Optional<base::TimeTicks> pending_pump_throttled_tasks_runtime_; |
185 bool allow_throttling_; | 204 bool allow_throttling_; |
186 | 205 |
187 std::unordered_map<BudgetPool*, std::unique_ptr<BudgetPool>> budget_pools_; | 206 std::unordered_map<BudgetPool*, std::unique_ptr<BudgetPool>> budget_pools_; |
188 | 207 |
189 base::WeakPtrFactory<TaskQueueThrottler> weak_factory_; | 208 base::WeakPtrFactory<TaskQueueThrottler> weak_factory_; |
190 | 209 |
191 DISALLOW_COPY_AND_ASSIGN(TaskQueueThrottler); | 210 DISALLOW_COPY_AND_ASSIGN(TaskQueueThrottler); |
192 }; | 211 }; |
193 | 212 |
194 } // namespace scheduler | 213 } // namespace scheduler |
195 } // namespace blink | 214 } // namespace blink |
196 | 215 |
197 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_TASK_QUEUE_THRO
TTLER_H_ | 216 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_TASK_QUEUE_THRO
TTLER_H_ |
OLD | NEW |