Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 earliest time when the next pump can be scheduled to run new tasks. |
|
alex clarke (OOO till 29th)
2017/04/21 09:14:19
Returns the earliest...
altimin
2017/04/25 13:22:35
Done.
| |
| 43 virtual base::TimeTicks GetNextAllowedRunTime() = 0; | 45 virtual base::TimeTicks GetNextAllowedRunTime( |
| 46 base::TimeTicks desired_run_time) const = 0; | |
| 44 | 47 |
| 45 // Returns true at a task can be run immediately at the given time. | 48 // Returns true at a task can be run immediately at the given time. |
| 46 virtual bool HasEnoughBudgetToRun(base::TimeTicks now) = 0; | 49 virtual bool CanRunTasksAt(base::TimeTicks moment) const = 0; |
| 50 | |
| 51 // Returns true if tasks can run at any time up to |moment|. | |
| 52 virtual bool CanRunTasksUntil(base::TimeTicks now, | |
| 53 base::TimeTicks moment) const = 0; | |
| 54 | |
| 55 // Notifies budget pool that queue has work with desired run time. | |
| 56 virtual void OnTaskQueueHasWork(TaskQueue* queue, | |
| 57 base::TimeTicks now, | |
| 58 base::TimeTicks desired_run_time) = 0; | |
| 59 | |
| 60 // Notifies budget pool that wakeup has happened. | |
| 61 virtual void OnWakeup(base::TimeTicks now) = 0; | |
| 47 | 62 |
| 48 // Returns state for tracing. | 63 // Returns state for tracing. |
| 49 virtual void AsValueInto(base::trace_event::TracedValue* state, | 64 virtual void AsValueInto(base::trace_event::TracedValue* state, |
| 50 base::TimeTicks now) const = 0; | 65 base::TimeTicks now) const = 0; |
| 51 | 66 |
| 52 // Adds |queue| to given pool. If the pool restriction does not allow | 67 // 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 | 68 // a task to be run immediately and |queue| is throttled, |queue| becomes |
| 54 // disabled. | 69 // disabled. |
| 55 void AddQueue(base::TimeTicks now, TaskQueue* queue); | 70 void AddQueue(base::TimeTicks now, TaskQueue* queue); |
| 56 | 71 |
| 57 // Removes |queue| from given pool. If it is throttled, it does not | 72 // Removes |queue| from given pool. If it is throttled, it does not |
| 58 // become enabled immediately, but a call to |PumpThrottledTasks| | 73 // become enabled immediately, but a call to |PumpThrottledTasks| |
| 59 // is scheduled. | 74 // is scheduled. |
|
alex clarke (OOO till 29th)
2017/04/21 09:14:19
Maybe comment this schedules a wake up if needed.
altimin
2017/04/25 13:22:35
Done.
| |
| 60 void RemoveQueue(base::TimeTicks now, TaskQueue* queue); | 75 void RemoveQueue(base::TimeTicks now, TaskQueue* queue); |
| 61 | 76 |
| 77 // Unlike RemoveQueue, does not schedule a new wakeup for the queue. | |
| 78 void UnregisterQueue(TaskQueue* queue); | |
| 79 | |
| 62 // Enables this time budget pool. Queues from this pool will be | 80 // Enables this time budget pool. Queues from this pool will be |
| 63 // throttled based on their run time. | 81 // throttled based on their run time. |
| 64 void EnableThrottling(LazyNow* now); | 82 void EnableThrottling(LazyNow* now); |
| 65 | 83 |
| 66 // Disables with time budget pool. Queues from this pool will not be | 84 // Disables with time budget pool. Queues from this pool will not be |
| 67 // throttled based on their run time. A call to |PumpThrottledTasks| | 85 // throttled based on their run time. A call to |PumpThrottledTasks| |
| 68 // will be scheduled to enable this queues back again and respect | 86 // will be scheduled to enable this queues back again and respect |
| 69 // timer alignment. Internal budget level will not regenerate with time. | 87 // timer alignment. Internal budget level will not regenerate with time. |
| 70 void DisableThrottling(LazyNow* now); | 88 void DisableThrottling(LazyNow* now); |
| 71 | 89 |
| 72 bool IsThrottlingEnabled() const; | 90 bool IsThrottlingEnabled() const; |
| 73 | 91 |
| 74 // All queues should be removed before calling Close(). | 92 // All queues should be removed before calling Close(). |
| 75 void Close(); | 93 void Close(); |
| 76 | 94 |
| 77 // Block all associated queues and schedule them to run when appropriate. | 95 // Block all associated queues and schedule them to run when appropriate. |
| 78 void BlockThrottledQueues(base::TimeTicks now); | 96 void BlockThrottledQueues(base::TimeTicks now); |
| 79 | 97 |
| 80 protected: | 98 protected: |
| 81 BudgetPool(const char* name, BudgetPoolController* budget_pool_controller); | 99 BudgetPool(const char* name, BudgetPoolController* budget_pool_controller); |
| 82 | 100 |
| 101 // Specify how this budget pool should block affected queues. | |
| 102 virtual QueueBlockType GetBlockType() const = 0; | |
| 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_; |
| 89 }; | 110 }; |
| 90 | 111 |
| 91 // CPUTimeBudgetPool represents a collection of task queues which share a limit | 112 // CPUTimeBudgetPool represents a collection of task queues which share a limit |
| 92 // on total cpu time. | 113 // on total cpu time. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 // queues even if they are allowed to run with increased budget level. | 156 // queues even if they are allowed to run with increased budget level. |
| 136 void GrantAdditionalBudget(base::TimeTicks now, base::TimeDelta budget_level); | 157 void GrantAdditionalBudget(base::TimeTicks now, base::TimeDelta budget_level); |
| 137 | 158 |
| 138 // Set callback which will be called every time when this budget pool | 159 // Set callback which will be called every time when this budget pool |
| 139 // is throttled. Throttling duration (time until the queue is allowed | 160 // is throttled. Throttling duration (time until the queue is allowed |
| 140 // to run again) is passed as a parameter to callback. | 161 // to run again) is passed as a parameter to callback. |
| 141 void SetReportingCallback( | 162 void SetReportingCallback( |
| 142 base::Callback<void(base::TimeDelta)> reporting_callback); | 163 base::Callback<void(base::TimeDelta)> reporting_callback); |
| 143 | 164 |
| 144 // BudgetPool implementation: | 165 // BudgetPool implementation: |
| 145 void RecordTaskRunTime(base::TimeTicks start_time, | 166 void RecordTaskRunTime(TaskQueue* queue, |
| 167 base::TimeTicks start_time, | |
| 146 base::TimeTicks end_time) final; | 168 base::TimeTicks end_time) final; |
| 147 bool HasEnoughBudgetToRun(base::TimeTicks now) final; | 169 bool CanRunTasksUntil(base::TimeTicks now, base::TimeTicks until) const final; |
| 148 base::TimeTicks GetNextAllowedRunTime() final; | 170 bool CanRunTasksAt(base::TimeTicks now) const final; |
| 171 base::TimeTicks GetNextAllowedRunTime( | |
| 172 base::TimeTicks desired_run_time) const final; | |
| 173 void OnTaskQueueHasWork(TaskQueue* queue, | |
| 174 base::TimeTicks now, | |
| 175 base::TimeTicks desired_run_time) final; | |
| 176 void OnWakeup(base::TimeTicks now) final; | |
| 149 void AsValueInto(base::trace_event::TracedValue* state, | 177 void AsValueInto(base::trace_event::TracedValue* state, |
| 150 base::TimeTicks now) const final; | 178 base::TimeTicks now) const final; |
| 151 | 179 |
| 180 protected: | |
| 181 QueueBlockType GetBlockType() const final; | |
| 182 | |
| 152 private: | 183 private: |
| 153 FRIEND_TEST_ALL_PREFIXES(TaskQueueThrottlerTest, CPUTimeBudgetPool); | 184 FRIEND_TEST_ALL_PREFIXES(TaskQueueThrottlerTest, CPUTimeBudgetPool); |
| 154 | 185 |
| 155 // Advances |last_checkpoint_| to |now| if needed and recalculates | 186 // Advances |last_checkpoint_| to |now| if needed and recalculates |
| 156 // budget level. | 187 // budget level. |
| 157 void Advance(base::TimeTicks now); | 188 void Advance(base::TimeTicks now); |
| 158 | 189 |
| 159 // Increase |current_budget_level_| to satisfy max throttling duration | 190 // Increase |current_budget_level_| to satisfy max throttling duration |
| 160 // condition if necessary. | 191 // condition if necessary. |
| 161 // Decrease |current_budget_level_| to satisfy max budget level | 192 // Decrease |current_budget_level_| to satisfy max budget level |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 177 | 208 |
| 178 base::TimeDelta current_budget_level_; | 209 base::TimeDelta current_budget_level_; |
| 179 base::TimeTicks last_checkpoint_; | 210 base::TimeTicks last_checkpoint_; |
| 180 double cpu_percentage_; | 211 double cpu_percentage_; |
| 181 | 212 |
| 182 base::Callback<void(base::TimeDelta)> reporting_callback_; | 213 base::Callback<void(base::TimeDelta)> reporting_callback_; |
| 183 | 214 |
| 184 DISALLOW_COPY_AND_ASSIGN(CPUTimeBudgetPool); | 215 DISALLOW_COPY_AND_ASSIGN(CPUTimeBudgetPool); |
| 185 }; | 216 }; |
| 186 | 217 |
| 218 // WakeupBudgetPool represents a collection of task queues which share a limit | |
| 219 // on total cpu time. | |
| 220 class BLINK_PLATFORM_EXPORT WakeupBudgetPool : public BudgetPool { | |
|
alex clarke (OOO till 29th)
2017/04/21 09:14:19
I think we should try to keep to 1 major class per
altimin
2017/04/25 13:22:35
I was thinking that these classes are too similar
alex clarke (OOO till 29th)
2017/04/27 10:28:14
Sorry to be a pain but I think we should separate
altimin
2017/04/27 11:07:40
Done.
| |
| 221 public: | |
| 222 WakeupBudgetPool(const char* name, | |
| 223 BudgetPoolController* budget_pool_controller, | |
| 224 base::TimeTicks now); | |
| 225 ~WakeupBudgetPool(); | |
|
alex clarke (OOO till 29th)
2017/04/21 09:14:19
override?
altimin
2017/04/25 13:22:35
Done.
| |
| 226 | |
| 227 void SetWakeupRate(base::TimeTicks now, double wakeups_per_second); | |
|
alex clarke (OOO till 29th)
2017/04/21 09:14:19
Why do we need to pass |now| into this? I notice
altimin
2017/04/25 13:22:35
Done.
| |
| 228 | |
| 229 void SetWakeupDuration(base::TimeTicks now, base::TimeDelta duration); | |
|
alex clarke (OOO till 29th)
2017/04/21 09:14:19
Same question.
altimin
2017/04/25 13:22:35
Done.
| |
| 230 | |
| 231 // BudgetPool implementation: | |
| 232 void RecordTaskRunTime(TaskQueue* queue, | |
| 233 base::TimeTicks start_time, | |
| 234 base::TimeTicks end_time) final; | |
| 235 bool CanRunTasksUntil(base::TimeTicks now, | |
| 236 base::TimeTicks moment) const final; | |
| 237 bool CanRunTasksAt(base::TimeTicks now) const final; | |
| 238 base::TimeTicks GetNextAllowedRunTime( | |
| 239 base::TimeTicks desired_run_time) const final; | |
| 240 void OnTaskQueueHasWork(TaskQueue* queue, | |
| 241 base::TimeTicks now, | |
| 242 base::TimeTicks desired_run_time) final; | |
| 243 void OnWakeup(base::TimeTicks now) final; | |
| 244 void AsValueInto(base::trace_event::TracedValue* state, | |
| 245 base::TimeTicks now) const final; | |
| 246 | |
| 247 protected: | |
| 248 QueueBlockType GetBlockType() const final; | |
| 249 | |
| 250 private: | |
| 251 base::TimeTicks NextWakeup() const; | |
| 252 | |
| 253 double wakeups_per_second_; | |
| 254 base::TimeDelta wakeup_duration_; | |
| 255 | |
| 256 base::TimeTicks last_wakeup_; | |
| 257 | |
| 258 DISALLOW_COPY_AND_ASSIGN(WakeupBudgetPool); | |
| 259 }; | |
| 260 | |
| 187 } // namespace scheduler | 261 } // namespace scheduler |
| 188 } // namespace blink | 262 } // namespace blink |
| 189 | 263 |
| 190 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ | 264 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ |
| OLD | NEW |