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

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

Issue 2778123003: [scheduler] Add WakeupBudgetPool. (Closed)
Patch Set: addressed comments Created 3 years, 7 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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_BUDGET_POOL_H_ 5 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_
6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_
7 7
8 #include <unordered_set> 8 #include <unordered_set>
9 9
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/gtest_prod_util.h" 11 #include "base/gtest_prod_util.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/optional.h" 13 #include "base/optional.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "platform/scheduler/base/lazy_now.h" 15 #include "platform/scheduler/base/lazy_now.h"
16 16
17 namespace base { 17 namespace base {
18 namespace trace_event { 18 namespace trace_event {
19 class TracedValue; 19 class TracedValue;
20 } 20 }
21 } 21 }
22 22
23 namespace blink { 23 namespace blink {
24 namespace scheduler { 24 namespace scheduler {
25 25
26 class TaskQueue; 26 class TaskQueue;
27 class BudgetPoolController; 27 class BudgetPoolController;
28 enum class QueueBlockType;
28 29
29 // BudgetPool represents a group of task queues which share a limit 30 // BudgetPool represents a group of task queues which share a limit
30 // on a resource. This limit applies when task queues are already throttled 31 // on a resource. This limit applies when task queues are already throttled
31 // by TaskQueueThrottler. 32 // by TaskQueueThrottler.
32 class BLINK_PLATFORM_EXPORT BudgetPool { 33 class BLINK_PLATFORM_EXPORT BudgetPool {
33 public: 34 public:
34 virtual ~BudgetPool(); 35 virtual ~BudgetPool();
35 36
36 const char* Name() const; 37 const char* Name() const;
37 38
38 // Report task run time to the budget pool. 39 // Report task run time to the budget pool.
39 virtual void RecordTaskRunTime(base::TimeTicks start_time, 40 virtual void RecordTaskRunTime(TaskQueue* queue,
41 base::TimeTicks start_time,
40 base::TimeTicks end_time) = 0; 42 base::TimeTicks end_time) = 0;
41 43
42 // Retuns earliest time (can be in the past) when the next task can run. 44 // Returns the earliest time when the next pump can be scheduled to run
43 virtual base::TimeTicks GetNextAllowedRunTime() = 0; 45 // new tasks.
46 virtual base::TimeTicks GetNextAllowedRunTime(
47 base::TimeTicks desired_run_time) const = 0;
44 48
45 // Returns true at a task can be run immediately at the given time. 49 // Returns true if a task can run at the given time.
46 virtual bool HasEnoughBudgetToRun(base::TimeTicks now) = 0; 50 virtual bool CanRunTasksAt(base::TimeTicks moment, bool is_wake_up) const = 0;
51
52 // Returns true if tasks can run at any time up to |moment|.
53 virtual bool CanRunTasksUntil(base::TimeTicks now,
54 base::TimeTicks moment) const = 0;
55
56 // Notifies budget pool that queue has work with desired run time.
57 virtual void OnQueueNextWakeUpChanged(TaskQueue* queue,
58 base::TimeTicks now,
59 base::TimeTicks desired_run_time) = 0;
60
61 // Notifies budget pool that wakeup has happened.
62 virtual void OnWakeUp(base::TimeTicks now) = 0;
63
64 // Specify how this budget pool should block affected queues.
65 virtual QueueBlockType GetBlockType() const = 0;
47 66
48 // Returns state for tracing. 67 // Returns state for tracing.
49 virtual void AsValueInto(base::trace_event::TracedValue* state, 68 virtual void AsValueInto(base::trace_event::TracedValue* state,
50 base::TimeTicks now) const = 0; 69 base::TimeTicks now) const = 0;
51 70
52 // Adds |queue| to given pool. If the pool restriction does not allow 71 // Adds |queue| to given pool. If the pool restriction does not allow
53 // a task to be run immediately and |queue| is throttled, |queue| becomes 72 // a task to be run immediately and |queue| is throttled, |queue| becomes
54 // disabled. 73 // disabled.
55 void AddQueue(base::TimeTicks now, TaskQueue* queue); 74 void AddQueue(base::TimeTicks now, TaskQueue* queue);
56 75
57 // Removes |queue| from given pool. If it is throttled, it does not 76 // Removes |queue| from given pool. If it is throttled, it does not
58 // become enabled immediately, but a call to |PumpThrottledTasks| 77 // become enabled immediately, but a wake-up is scheduled if needed.
59 // is scheduled.
60 void RemoveQueue(base::TimeTicks now, TaskQueue* queue); 78 void RemoveQueue(base::TimeTicks now, TaskQueue* queue);
61 79
80 // Unlike RemoveQueue, does not schedule a new wake-up for the queue.
81 void UnregisterQueue(TaskQueue* queue);
82
62 // Enables this time budget pool. Queues from this pool will be 83 // Enables this time budget pool. Queues from this pool will be
63 // throttled based on their run time. 84 // throttled based on their run time.
64 void EnableThrottling(LazyNow* now); 85 void EnableThrottling(LazyNow* now);
65 86
66 // Disables with time budget pool. Queues from this pool will not be 87 // Disables with time budget pool. Queues from this pool will not be
67 // throttled based on their run time. A call to |PumpThrottledTasks| 88 // throttled based on their run time. A call to |PumpThrottledTasks|
68 // will be scheduled to enable this queues back again and respect 89 // will be scheduled to enable this queues back again and respect
69 // timer alignment. Internal budget level will not regenerate with time. 90 // timer alignment. Internal budget level will not regenerate with time.
70 void DisableThrottling(LazyNow* now); 91 void DisableThrottling(LazyNow* now);
71 92
72 bool IsThrottlingEnabled() const; 93 bool IsThrottlingEnabled() const;
73 94
74 // All queues should be removed before calling Close(). 95 // All queues should be removed before calling Close().
75 void Close(); 96 void Close();
76 97
77 // Block all associated queues and schedule them to run when appropriate. 98 // Block all associated queues and schedule them to run when appropriate.
78 void BlockThrottledQueues(base::TimeTicks now); 99 void BlockThrottledQueues(base::TimeTicks now);
79 100
80 protected: 101 protected:
81 BudgetPool(const char* name, BudgetPoolController* budget_pool_controller); 102 BudgetPool(const char* name, BudgetPoolController* budget_pool_controller);
82 103
83 const char* name_; // NOT OWNED 104 const char* name_; // NOT OWNED
84 105
85 BudgetPoolController* budget_pool_controller_; 106 BudgetPoolController* budget_pool_controller_;
86 107
87 std::unordered_set<TaskQueue*> associated_task_queues_; 108 std::unordered_set<TaskQueue*> associated_task_queues_;
88 bool is_enabled_; 109 bool is_enabled_;
110
111 private:
112 void DissociateQueue(TaskQueue* queue);
alex clarke (OOO till 29th) 2017/04/27 10:28:14 Why do we need this and UnregisterQueue?
altimin 2017/04/27 11:07:41 I believe that it's confusing to call UnregisterQu
89 }; 113 };
90 114
91 // CPUTimeBudgetPool represents a collection of task queues which share a limit 115 // CPUTimeBudgetPool represents a collection of task queues which share a limit
92 // on total cpu time. 116 // on total cpu time.
93 class BLINK_PLATFORM_EXPORT CPUTimeBudgetPool : public BudgetPool { 117 class BLINK_PLATFORM_EXPORT CPUTimeBudgetPool : public BudgetPool {
94 public: 118 public:
95 CPUTimeBudgetPool(const char* name, 119 CPUTimeBudgetPool(const char* name,
96 BudgetPoolController* budget_pool_controller, 120 BudgetPoolController* budget_pool_controller,
97 base::TimeTicks now); 121 base::TimeTicks now);
98 122
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 // queues even if they are allowed to run with increased budget level. 159 // queues even if they are allowed to run with increased budget level.
136 void GrantAdditionalBudget(base::TimeTicks now, base::TimeDelta budget_level); 160 void GrantAdditionalBudget(base::TimeTicks now, base::TimeDelta budget_level);
137 161
138 // Set callback which will be called every time when this budget pool 162 // Set callback which will be called every time when this budget pool
139 // is throttled. Throttling duration (time until the queue is allowed 163 // is throttled. Throttling duration (time until the queue is allowed
140 // to run again) is passed as a parameter to callback. 164 // to run again) is passed as a parameter to callback.
141 void SetReportingCallback( 165 void SetReportingCallback(
142 base::Callback<void(base::TimeDelta)> reporting_callback); 166 base::Callback<void(base::TimeDelta)> reporting_callback);
143 167
144 // BudgetPool implementation: 168 // BudgetPool implementation:
145 void RecordTaskRunTime(base::TimeTicks start_time, 169 void RecordTaskRunTime(TaskQueue* queue,
170 base::TimeTicks start_time,
146 base::TimeTicks end_time) final; 171 base::TimeTicks end_time) final;
147 bool HasEnoughBudgetToRun(base::TimeTicks now) final; 172 bool CanRunTasksUntil(base::TimeTicks now, base::TimeTicks until) const final;
148 base::TimeTicks GetNextAllowedRunTime() final; 173 bool CanRunTasksAt(base::TimeTicks now, bool is_wake_up) const final;
174 base::TimeTicks GetNextAllowedRunTime(
175 base::TimeTicks desired_run_time) const final;
176 void OnQueueNextWakeUpChanged(TaskQueue* queue,
177 base::TimeTicks now,
178 base::TimeTicks desired_run_time) final;
179 void OnWakeUp(base::TimeTicks now) final;
149 void AsValueInto(base::trace_event::TracedValue* state, 180 void AsValueInto(base::trace_event::TracedValue* state,
150 base::TimeTicks now) const final; 181 base::TimeTicks now) const final;
151 182
183 protected:
184 QueueBlockType GetBlockType() const final;
185
152 private: 186 private:
153 FRIEND_TEST_ALL_PREFIXES(TaskQueueThrottlerTest, CPUTimeBudgetPool); 187 FRIEND_TEST_ALL_PREFIXES(TaskQueueThrottlerTest, CPUTimeBudgetPool);
154 188
155 // Advances |last_checkpoint_| to |now| if needed and recalculates 189 // Advances |last_checkpoint_| to |now| if needed and recalculates
156 // budget level. 190 // budget level.
157 void Advance(base::TimeTicks now); 191 void Advance(base::TimeTicks now);
158 192
159 // Increase |current_budget_level_| to satisfy max throttling duration 193 // Increase |current_budget_level_| to satisfy max throttling duration
160 // condition if necessary. 194 // condition if necessary.
161 // Decrease |current_budget_level_| to satisfy max budget level 195 // Decrease |current_budget_level_| to satisfy max budget level
(...skipping 15 matching lines...) Expand all
177 211
178 base::TimeDelta current_budget_level_; 212 base::TimeDelta current_budget_level_;
179 base::TimeTicks last_checkpoint_; 213 base::TimeTicks last_checkpoint_;
180 double cpu_percentage_; 214 double cpu_percentage_;
181 215
182 base::Callback<void(base::TimeDelta)> reporting_callback_; 216 base::Callback<void(base::TimeDelta)> reporting_callback_;
183 217
184 DISALLOW_COPY_AND_ASSIGN(CPUTimeBudgetPool); 218 DISALLOW_COPY_AND_ASSIGN(CPUTimeBudgetPool);
185 }; 219 };
186 220
221 // WakeUpBudgetPool represents a collection of task queues which share a limit
222 // on total cpu time.
223 class BLINK_PLATFORM_EXPORT WakeUpBudgetPool : public BudgetPool {
224 public:
225 WakeUpBudgetPool(const char* name,
226 BudgetPoolController* budget_pool_controller,
227 base::TimeTicks now);
228 ~WakeUpBudgetPool() override;
229
230 // Note: this does not have an immediate effect and should be called only
231 // during initialization of a WakeUpBudgetPool.
232 void SetWakeUpRate(double wakeups_per_second);
233
234 // Note: this does not have an immediate effect and should be called only
235 // during initialization of a WakeUpBudgetPool.
236 void SetWakeUpDuration(base::TimeDelta duration);
237
238 // BudgetPool implementation:
239 void RecordTaskRunTime(TaskQueue* queue,
240 base::TimeTicks start_time,
241 base::TimeTicks end_time) final;
242 bool CanRunTasksUntil(base::TimeTicks now,
243 base::TimeTicks moment) const final;
244 bool CanRunTasksAt(base::TimeTicks now, bool is_wake_up) const final;
245 base::TimeTicks GetNextAllowedRunTime(
246 base::TimeTicks desired_run_time) const final;
247 void OnQueueNextWakeUpChanged(TaskQueue* queue,
248 base::TimeTicks now,
249 base::TimeTicks desired_run_time) final;
250 void OnWakeUp(base::TimeTicks now) final;
251 void AsValueInto(base::trace_event::TracedValue* state,
252 base::TimeTicks now) const final;
253
254 protected:
255 QueueBlockType GetBlockType() const final;
256
257 private:
258 base::Optional<base::TimeTicks> NextWakeUp() const;
259
260 double wakeups_per_second_;
261 base::TimeDelta wakeup_duration_;
262
263 base::Optional<base::TimeTicks> last_wakeup_;
264
265 DISALLOW_COPY_AND_ASSIGN(WakeUpBudgetPool);
266 };
267
187 } // namespace scheduler 268 } // namespace scheduler
188 } // namespace blink 269 } // namespace blink
189 270
190 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ 271 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698