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

Side by Side Diff: third_party/WebKit/Source/platform/scheduler/renderer/task_queue_throttler.h

Issue 2258133002: [scheduler] Implement time-based cpu throttling. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More fixes Created 4 years, 3 months 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
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_THROTTLING_HELPER_ H_ 5 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELPER_ H_
6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELPER_ H_ 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELPER_ H_
7 7
8 #include <set> 8 #include <set>
9 #include <unordered_map>
9 10
11 #include "base/logging.h"
10 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/optional.h"
14 #include "base/threading/thread_checker.h"
11 #include "platform/scheduler/base/cancelable_closure_holder.h" 15 #include "platform/scheduler/base/cancelable_closure_holder.h"
12 #include "platform/scheduler/base/time_domain.h" 16 #include "platform/scheduler/base/time_domain.h"
13 #include "public/platform/WebViewScheduler.h" 17 #include "public/platform/WebViewScheduler.h"
14 18
15 namespace blink { 19 namespace blink {
16 namespace scheduler { 20 namespace scheduler {
17 21
18 class RendererSchedulerImpl; 22 class RendererSchedulerImpl;
19 class ThrottledTimeDomain; 23 class ThrottledTimeDomain;
20 class WebFrameSchedulerImpl; 24 class WebFrameSchedulerImpl;
21 25
22 // The job of the ThrottlingHelper is to run tasks posted on throttled queues at 26 // The job of the TaskQueueThrottler is to control tasks posted on throttled
alex clarke (OOO till 29th) 2016/09/16 14:36:18 The job of the TaskQueueThrottler is to control wh
altimin 2016/09/16 14:46:59 Done.
23 // most once per second. This is done by disabling throttled queues and running 27 // queues. The TaskQueueThrottler:
28 // - runs throttled tasks once per second,
29 // - controls time budget for task queues grouped in TimeBudgetPools.
30 //
31 // This is done by disabling throttled queues and running
24 // a special "heart beat" function |PumpThrottledTasks| which when run 32 // a special "heart beat" function |PumpThrottledTasks| which when run
25 // temporarily enables throttled queues and inserts a fence to ensure tasks 33 // temporarily enables throttled queues and inserts a fence to ensure tasks
26 // posted from a throttled task run next time the queue is pumped. 34 // posted from a throttled task run next time the queue is pumped.
27 // 35 //
28 // Of course the ThrottlingHelper isn't the only sub-system that wants to enable 36 // Of course the TaskQueueThrottler isn't the only sub-system that wants to
29 // or disable queues. E.g. RendererSchedulerImpl also does this for policy 37 // enable or disable queues. E.g. RendererSchedulerImpl also does this for
30 // reasons. To prevent the systems from fighting, clients of ThrottlingHelper 38 // policy reasons. To prevent the systems from fighting, clients of
31 // must use SetQueueEnabled rather than calling the function directly on the 39 // TaskQueueThrottler must use SetQueueEnabled rather than calling the function
32 // queue. 40 // directly on the queue.
33 // 41 //
34 // There may be more than one system that wishes to throttle a queue (e.g. 42 // There may be more than one system that wishes to throttle a queue (e.g.
35 // renderer suspension vs tab level suspension) so the ThrottlingHelper keeps a 43 // renderer suspension vs tab level suspension) so the TaskQueueThrottler keeps
36 // count of the number of systems that wish a queue to be throttled. 44 // a count of the number of systems that wish a queue to be throttled.
37 // See IncreaseThrottleRefCount & DecreaseThrottleRefCount. 45 // See IncreaseThrottleRefCount & DecreaseThrottleRefCount.
38 class BLINK_PLATFORM_EXPORT ThrottlingHelper : public TimeDomain::Observer { 46 //
47 // This class is main-thread only.
48 class BLINK_PLATFORM_EXPORT TaskQueueThrottler : public TimeDomain::Observer {
39 public: 49 public:
40 ThrottlingHelper(RendererSchedulerImpl* renderer_scheduler, 50 // TimeBudgetPool represents a group of task queues which share a limit
41 const char* tracing_category); 51 // on execution time. This limit applies when task queues are already
52 // throttled by TaskQueueThrottler.
53 class TimeBudgetPool {
54 public:
55 ~TimeBudgetPool();
42 56
43 ~ThrottlingHelper() override; 57 // Throttle task queues from this time budget pool if tasks are running
58 // for more than |cpu_percentage| per cent of wall time.
59 // This function does not affect internal time budget level.
60 void SetTimeBudget(base::TimeTicks now, double cpu_percentage);
61
62 // Adds |queue| to given pool. If the pool restriction does not allow
63 // a task to be run immediately and |queue| is throttled, |queue| becomes
64 // disabled.
65 void AddQueue(base::TimeTicks now, TaskQueue* queue);
alex clarke (OOO till 29th) 2016/09/16 14:36:18 nit: blank line below this.
altimin 2016/09/16 14:46:59 Done.
66 // Removes |queue| from given pool. If it is throttled, it does not
67 // become enabled immediately, but a call to |PumpThrottledTasks|
68 // is scheduled.
69 void RemoveQueue(base::TimeTicks now, TaskQueue* queue);
70
71 void RecordTaskRunTime(base::TimeDelta task_run_time);
72
73 // Enables this time budget pool. Queues from this pool will be
74 // throttled based on their run time.
75 void EnableThrottling(LazyNow* now);
alex clarke (OOO till 29th) 2016/09/16 14:36:18 nit: blank line below this.
altimin 2016/09/16 14:46:59 Done.
76 // Disables with time budget pool. Queues from this pool will not be
77 // throttled based on their run time. A call to |PumpThrottledTasks|
78 // will be scheduled to enable this queues back again and respect
79 // timer alignment. Internal budget level will not regenerate with time.
80 void DisableThrottling(LazyNow* now);
81 bool IsThrottlingEnabled() const;
82
83 const char* Name() const;
84
85 // All queues should be removed before calling Close().
86 void Close();
87
88 private:
89 friend class TaskQueueThrottler;
90
91 FRIEND_TEST_ALL_PREFIXES(TaskQueueThrottlerTest, TimeBudgetPool);
92
93 TimeBudgetPool(const char* name,
94 TaskQueueThrottler* task_queue_throttler,
95 base::TimeTicks now);
96
97 bool HasEnoughBudgetToRun(base::TimeTicks now);
98 base::TimeTicks GetNextAllowedRunTime();
99
100 // Advances |last_checkpoint_| to |now| if needed and recalculates
101 // budget level.
102 void Advance(base::TimeTicks now);
103
104 // Returns state for tracing.
105 void AsValueInto(base::trace_event::TracedValue* state,
106 base::TimeTicks now) const;
107
108 // Disable all associated throttled queues.
109 void BlockQueues(base::TimeTicks now);
110
111 const char* name_; // NOT OWNED
112
113 TaskQueueThrottler* task_queue_throttler_;
114
115 base::TimeDelta current_budget_level_;
116 base::TimeDelta max_budget_level_;
117 base::TimeTicks last_checkpoint_;
118 double cpu_percentage_;
119 bool is_enabled_;
120
121 std::unordered_set<TaskQueue*> associated_task_queues_;
122
123 DISALLOW_COPY_AND_ASSIGN(TimeBudgetPool);
124 };
125
126 TaskQueueThrottler(RendererSchedulerImpl* renderer_scheduler,
127 const char* tracing_category);
128
129 ~TaskQueueThrottler() override;
44 130
45 // TimeDomain::Observer implementation: 131 // TimeDomain::Observer implementation:
46 void OnTimeDomainHasImmediateWork() override; 132 void OnTimeDomainHasImmediateWork(TaskQueue*) override;
47 void OnTimeDomainHasDelayedWork() override; 133 void OnTimeDomainHasDelayedWork(TaskQueue*) override;
48 134
49 // The purpose of this method is to make sure throttling doesn't conflict with 135 // The purpose of this method is to make sure throttling doesn't conflict with
50 // enabling/disabling the queue for policy reasons. 136 // enabling/disabling the queue for policy reasons.
51 // If |task_queue| is throttled then the ThrottlingHelper remembers the 137 // If |task_queue| is throttled then the TaskQueueThrottler remembers the
52 // |enabled| setting. In addition if |enabled| is false then the queue is 138 // |enabled| setting. In addition if |enabled| is false then the queue is
53 // immediatly disabled. Otherwise if |task_queue| not throttled then 139 // immediatly disabled. Otherwise if |task_queue| not throttled then
54 // TaskQueue::SetEnabled(enabled) is called. 140 // TaskQueue::SetEnabled(enabled) is called.
55 void SetQueueEnabled(TaskQueue* task_queue, bool enabled); 141 void SetQueueEnabled(TaskQueue* task_queue, bool enabled);
56 142
57 // Increments the throttled refcount and causes |task_queue| to be throttled 143 // Increments the throttled refcount and causes |task_queue| to be throttled
58 // if its not already throttled. 144 // if its not already throttled.
59 void IncreaseThrottleRefCount(TaskQueue* task_queue); 145 void IncreaseThrottleRefCount(TaskQueue* task_queue);
60 146
61 // If the refcouint is non-zero it's decremented. If the throttled refcount 147 // If the refcouint is non-zero it's decremented. If the throttled refcount
62 // becomes zero then |task_queue| is unthrottled. If the refcount was already 148 // becomes zero then |task_queue| is unthrottled. If the refcount was already
63 // zero this function does nothing. 149 // zero this function does nothing.
64 void DecreaseThrottleRefCount(TaskQueue* task_queue); 150 void DecreaseThrottleRefCount(TaskQueue* task_queue);
65 151
66 // Removes |task_queue| from |throttled_queues_|. 152 // Removes |task_queue| from |queue_details| and from appropriate budget pool.
67 void UnregisterTaskQueue(TaskQueue* task_queue); 153 void UnregisterTaskQueue(TaskQueue* task_queue);
68 154
69 // Returns true if the |task_queue| is throttled. 155 // Returns true if the |task_queue| is throttled.
70 bool IsThrottled(TaskQueue* task_queue) const; 156 bool IsThrottled(TaskQueue* task_queue) const;
71 157
72 // Tells the ThrottlingHelper we're using virtual time, which disables all 158 // Tells the TaskQueueThrottler we're using virtual time, which disables all
73 // throttling. 159 // throttling.
74 void EnableVirtualTime(); 160 void EnableVirtualTime();
75 161
76 const ThrottledTimeDomain* time_domain() const { return time_domain_.get(); } 162 const ThrottledTimeDomain* time_domain() const { return time_domain_.get(); }
77 163
78 static base::TimeTicks ThrottledRunTime(base::TimeTicks unthrottled_runtime); 164 static base::TimeTicks AlignedThrottledRunTime(
165 base::TimeTicks unthrottled_runtime);
79 166
80 const scoped_refptr<TaskQueue>& task_runner() const { return task_runner_; } 167 const scoped_refptr<TaskQueue>& task_runner() const { return task_runner_; }
81 168
169 // Returned object is owned by |TaskQueueThrottler|.
170 TimeBudgetPool* CreateTimeBudgetPool(const char* name);
171
172 // Accounts for given task for cpu-based throttling needs.
173 void OnTaskRunTimeReported(TaskQueue* task_queue,
174 base::TimeTicks start_time,
175 base::TimeTicks end_time);
176
177 void AsValueInto(base::trace_event::TracedValue* state,
178 base::TimeTicks now) const;
179
82 private: 180 private:
83 struct Metadata { 181 struct Metadata {
84 Metadata() : throttling_ref_count(0), enabled(false) {} 182 Metadata()
183 : throttling_ref_count(0), enabled(false), time_budget_pool(nullptr) {}
85 184
86 Metadata(size_t ref_count, bool is_enabled) 185 Metadata(size_t ref_count, bool is_enabled)
87 : throttling_ref_count(ref_count), enabled(is_enabled) {} 186 : throttling_ref_count(ref_count),
187 enabled(is_enabled),
188 time_budget_pool(nullptr) {}
88 189
89 size_t throttling_ref_count; 190 size_t throttling_ref_count;
90 bool enabled; 191 bool enabled;
192
193 TimeBudgetPool* time_budget_pool;
91 }; 194 };
92 using TaskQueueMap = std::map<TaskQueue*, Metadata>; 195 using TaskQueueMap = std::unordered_map<TaskQueue*, Metadata>;
93 196
94 void PumpThrottledTasks(); 197 void PumpThrottledTasks();
95 198
96 // Note |unthrottled_runtime| might be in the past. When this happens we 199 // Note |unthrottled_runtime| might be in the past. When this happens we
97 // compute the delay to the next runtime based on now rather than 200 // compute the delay to the next runtime based on now rather than
98 // unthrottled_runtime. 201 // unthrottled_runtime.
99 void MaybeSchedulePumpThrottledTasksLocked( 202 void MaybeSchedulePumpThrottledTasks(
100 const tracked_objects::Location& from_here, 203 const tracked_objects::Location& from_here,
101 base::TimeTicks now, 204 base::TimeTicks now,
102 base::TimeTicks unthrottled_runtime); 205 base::TimeTicks runtime);
103 206
104 TaskQueueMap throttled_queues_; 207 TimeBudgetPool* GetTimeBudgetPoolForQueue(TaskQueue* queue);
105 base::Closure forward_immediate_work_closure_; 208
209 // Schedule pumping because of given task queue.
210 void MaybeSchedulePumpQueue(
211 const tracked_objects::Location& from_here,
212 base::TimeTicks now,
213 TaskQueue* queue,
214 base::Optional<base::TimeTicks> next_possible_run_time);
215
216 // Return next possible time when queue is allowed to run in accordance
217 // with throttling policy.
218 base::TimeTicks GetNextAllowedRunTime(base::TimeTicks now, TaskQueue* queue);
219
220 void MaybeDeleteQueueMetadata(TaskQueueMap::iterator it);
221
222 TaskQueueMap queue_details_;
223 base::Callback<void(TaskQueue*)> forward_immediate_work_callback_;
106 scoped_refptr<TaskQueue> task_runner_; 224 scoped_refptr<TaskQueue> task_runner_;
107 RendererSchedulerImpl* renderer_scheduler_; // NOT OWNED 225 RendererSchedulerImpl* renderer_scheduler_; // NOT OWNED
108 base::TickClock* tick_clock_; // NOT OWNED 226 base::TickClock* tick_clock_; // NOT OWNED
109 const char* tracing_category_; // NOT OWNED 227 const char* tracing_category_; // NOT OWNED
110 std::unique_ptr<ThrottledTimeDomain> time_domain_; 228 std::unique_ptr<ThrottledTimeDomain> time_domain_;
111 229
112 CancelableClosureHolder pump_throttled_tasks_closure_; 230 CancelableClosureHolder pump_throttled_tasks_closure_;
113 base::TimeTicks pending_pump_throttled_tasks_runtime_; 231 base::Optional<base::TimeTicks> pending_pump_throttled_tasks_runtime_;
114 bool virtual_time_; 232 bool virtual_time_;
115 233
116 base::WeakPtrFactory<ThrottlingHelper> weak_factory_; 234 std::unordered_map<TimeBudgetPool*, std::unique_ptr<TimeBudgetPool>>
235 time_budget_pools_;
117 236
118 DISALLOW_COPY_AND_ASSIGN(ThrottlingHelper); 237 base::WeakPtrFactory<TaskQueueThrottler> weak_factory_;
238
239 DISALLOW_COPY_AND_ASSIGN(TaskQueueThrottler);
119 }; 240 };
120 241
121 } // namespace scheduler 242 } // namespace scheduler
122 } // namespace blink 243 } // namespace blink
123 244
124 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELP ER_H_ 245 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELP ER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698