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