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. TaskQueueThrottler |
| 29 // - runs throttled tasks once per second, | |
| 30 // - controls time budget for task queues grouped in TimeBudgetPools. | |
| 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 class BLINK_PLATFORM_EXPORT TaskQueueThrottler : public TimeDomain::Observer { |
| 39 public: | 47 public: |
| 40 ThrottlingHelper(RendererSchedulerImpl* renderer_scheduler, | 48 // This class is main-thread only. |
|
alex clarke (OOO till 29th)
2016/09/12 17:45:27
The TaskQueueThrottler (nee-ThrottlingHelper) is m
altimin
2016/09/14 11:23:18
Done.
| |
| 41 const char* tracing_category); | 49 class TimeBudgetPool { |
|
alex clarke (OOO till 29th)
2016/09/12 17:45:27
Do we even want this API to be public?
alex clarke (OOO till 29th)
2016/09/12 17:45:27
Please add an overview comment explaining what the
altimin
2016/09/14 11:23:18
Done.
altimin
2016/09/14 11:23:18
It's public inside scheduler and I think we want t
| |
| 50 public: | |
| 51 ~TimeBudgetPool(); | |
| 42 | 52 |
| 43 ~ThrottlingHelper() override; | 53 // Throttle task queues from this time budget pool if tasks are running |
| 54 // for more than |cpu_percentage| per cent of wall time. | |
| 55 // This function does not affect internal time budget level. | |
| 56 void SetTimeBudget(base::TimeTicks now, double cpu_percentage); | |
| 57 | |
| 58 void AddQueue(base::TimeTicks now, TaskQueue* queue); | |
|
alex clarke (OOO till 29th)
2016/09/12 17:45:27
Please document these. Side effects such |queue|
Sami
2016/09/12 17:49:10
We should probably document the side-effects here
altimin
2016/09/14 11:23:17
Done.
altimin
2016/09/14 11:23:17
Done.
| |
| 59 void RemoveQueue(base::TimeTicks now, TaskQueue* queue); | |
| 60 | |
| 61 void RecordTaskRunTime(base::TimeDelta task_run_time); | |
|
Sami
2016/09/12 17:49:10
Does all of this functionality need to be public o
altimin
2016/09/14 11:23:18
Done.
| |
| 62 | |
| 63 bool IsAllowedToRun(base::TimeTicks now); | |
| 64 base::TimeTicks NextAllowedRunTime(); | |
| 65 | |
| 66 void Enable(base::TimeTicks now); | |
|
alex clarke (OOO till 29th)
2016/09/12 17:45:27
Please document Enable & Disable.
Sami
2016/09/12 17:49:10
What do enable/disable mean for a budget pool? May
altimin
2016/09/14 11:23:17
Done.
altimin
2016/09/14 11:23:18
It means "activate this pool and start throttling
| |
| 67 void Disable(base::TimeTicks now); | |
| 68 bool IsEnabled() const; | |
| 69 | |
| 70 const char* Name() const; | |
| 71 | |
| 72 // All queues should be removed before calling Close(). | |
| 73 void Close(); | |
|
alex clarke (OOO till 29th)
2016/09/12 17:45:27
This only seems to be called by test code, is that
altimin
2016/09/14 11:23:17
For the moment — yes, it's expected.
Lifetime is s
| |
| 74 | |
| 75 private: | |
| 76 friend class TaskQueueThrottler; | |
| 77 | |
| 78 TimeBudgetPool(const char* name, | |
| 79 TaskQueueThrottler* task_queue_throttler, | |
| 80 base::TimeTicks now); | |
| 81 | |
| 82 // Advances |last_checkpoint_| to |now| if needed and recalculates | |
| 83 // budget level. | |
| 84 void Advance(base::TimeTicks now); | |
| 85 | |
| 86 // Returns state for tracing. | |
| 87 void AsValueInto(base::trace_event::TracedValue* state, | |
| 88 base::TimeTicks now) const; | |
| 89 | |
| 90 const char* name_; // NOT OWNED | |
| 91 | |
| 92 TaskQueueThrottler* task_queue_throttler_; | |
| 93 | |
| 94 base::TimeDelta current_budget_level_; | |
| 95 base::TimeDelta max_budget_level_; | |
| 96 base::TimeTicks last_checkpoint_; | |
| 97 double cpu_percentage_; | |
| 98 bool is_enabled_; | |
| 99 | |
| 100 std::unordered_set<TaskQueue*> assosiated_task_queues_; | |
|
Sami
2016/09/12 17:49:10
typo: associated.
Also I think we need to make th
altimin
2016/09/14 11:23:17
Done.
| |
| 101 | |
| 102 DISALLOW_COPY_AND_ASSIGN(TimeBudgetPool); | |
| 103 }; | |
| 104 | |
| 105 TaskQueueThrottler(RendererSchedulerImpl* renderer_scheduler, | |
| 106 const char* tracing_category); | |
| 107 | |
| 108 ~TaskQueueThrottler() override; | |
| 44 | 109 |
| 45 // TimeDomain::Observer implementation: | 110 // TimeDomain::Observer implementation: |
| 46 void OnTimeDomainHasImmediateWork() override; | 111 void OnTimeDomainHasImmediateWork() override; |
| 47 void OnTimeDomainHasDelayedWork() override; | 112 void OnTimeDomainHasDelayedWork() override; |
| 48 | 113 |
| 49 // The purpose of this method is to make sure throttling doesn't conflict with | 114 // The purpose of this method is to make sure throttling doesn't conflict with |
| 50 // enabling/disabling the queue for policy reasons. | 115 // enabling/disabling the queue for policy reasons. |
| 51 // If |task_queue| is throttled then the ThrottlingHelper remembers the | 116 // If |task_queue| is throttled then the TaskQueueThrottler remembers the |
| 52 // |enabled| setting. In addition if |enabled| is false then the queue is | 117 // |enabled| setting. In addition if |enabled| is false then the queue is |
| 53 // immediatly disabled. Otherwise if |task_queue| not throttled then | 118 // immediatly disabled. Otherwise if |task_queue| not throttled then |
| 54 // TaskQueue::SetEnabled(enabled) is called. | 119 // TaskQueue::SetEnabled(enabled) is called. |
| 55 void SetQueueEnabled(TaskQueue* task_queue, bool enabled); | 120 void SetQueueEnabled(TaskQueue* task_queue, bool enabled); |
| 56 | 121 |
| 57 // Increments the throttled refcount and causes |task_queue| to be throttled | 122 // Increments the throttled refcount and causes |task_queue| to be throttled |
| 58 // if its not already throttled. | 123 // if its not already throttled. |
| 59 void IncreaseThrottleRefCount(TaskQueue* task_queue); | 124 void IncreaseThrottleRefCount(TaskQueue* task_queue); |
| 60 | 125 |
| 61 // If the refcouint is non-zero it's decremented. If the throttled refcount | 126 // 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 | 127 // becomes zero then |task_queue| is unthrottled. If the refcount was already |
| 63 // zero this function does nothing. | 128 // zero this function does nothing. |
| 64 void DecreaseThrottleRefCount(TaskQueue* task_queue); | 129 void DecreaseThrottleRefCount(TaskQueue* task_queue); |
| 65 | 130 |
| 66 // Removes |task_queue| from |throttled_queues_|. | 131 // Removes |task_queue| from |throttled_queues_|. |
| 67 void UnregisterTaskQueue(TaskQueue* task_queue); | 132 void UnregisterTaskQueue(TaskQueue* task_queue); |
| 68 | 133 |
| 69 // Returns true if the |task_queue| is throttled. | 134 // Returns true if the |task_queue| is throttled. |
| 70 bool IsThrottled(TaskQueue* task_queue) const; | 135 bool IsThrottled(TaskQueue* task_queue) const; |
| 71 | 136 |
| 72 // Tells the ThrottlingHelper we're using virtual time, which disables all | 137 // Tells the TaskQueueThrottler we're using virtual time, which disables all |
| 73 // throttling. | 138 // throttling. |
| 74 void EnableVirtualTime(); | 139 void EnableVirtualTime(); |
| 75 | 140 |
| 76 const ThrottledTimeDomain* time_domain() const { return time_domain_.get(); } | 141 const ThrottledTimeDomain* time_domain() const { return time_domain_.get(); } |
| 77 | 142 |
| 78 static base::TimeTicks ThrottledRunTime(base::TimeTicks unthrottled_runtime); | 143 static base::TimeTicks AlignedThrottledRunTime( |
| 144 base::TimeTicks unthrottled_runtime); | |
| 79 | 145 |
| 80 const scoped_refptr<TaskQueue>& task_runner() const { return task_runner_; } | 146 const scoped_refptr<TaskQueue>& task_runner() const { return task_runner_; } |
| 81 | 147 |
| 148 // Returned object is owned by |TaskQueueThrottler|. | |
| 149 TimeBudgetPool* CreateTimeBudgetPool(const char* name); | |
|
alex clarke (OOO till 29th)
2016/09/12 17:45:26
This only seems to be called by tests, is that exp
altimin
2016/09/14 11:23:18
For the moment yes, but it will change in the next
| |
| 150 | |
| 151 void OnTaskRunTimeReported(TaskQueue* task_queue, | |
| 152 base::TimeTicks start_time, | |
| 153 base::TimeTicks end_time); | |
| 154 | |
| 155 void AsValueInto(base::trace_event::TracedValue* state, | |
| 156 base::TimeTicks now) const; | |
| 157 | |
| 82 private: | 158 private: |
| 83 struct Metadata { | 159 struct Metadata { |
| 84 Metadata() : throttling_ref_count(0), enabled(false) {} | 160 Metadata() : throttling_ref_count(0), enabled(false) {} |
| 85 | 161 |
| 86 Metadata(size_t ref_count, bool is_enabled) | 162 Metadata(size_t ref_count, bool is_enabled) |
| 87 : throttling_ref_count(ref_count), enabled(is_enabled) {} | 163 : throttling_ref_count(ref_count), enabled(is_enabled) {} |
| 88 | 164 |
| 89 size_t throttling_ref_count; | 165 size_t throttling_ref_count; |
| 90 bool enabled; | 166 bool enabled; |
| 91 }; | 167 }; |
| 92 using TaskQueueMap = std::map<TaskQueue*, Metadata>; | 168 using TaskQueueMap = std::map<TaskQueue*, Metadata>; |
| 93 | 169 |
| 94 void PumpThrottledTasks(); | 170 void PumpThrottledTasks(); |
| 95 | 171 |
| 96 // Note |unthrottled_runtime| might be in the past. When this happens we | 172 // 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 | 173 // compute the delay to the next runtime based on now rather than |
| 98 // unthrottled_runtime. | 174 // unthrottled_runtime. |
| 99 void MaybeSchedulePumpThrottledTasksLocked( | 175 void MaybeSchedulePumpThrottledTasks( |
| 100 const tracked_objects::Location& from_here, | 176 const tracked_objects::Location& from_here, |
| 101 base::TimeTicks now, | 177 base::TimeTicks now, |
| 102 base::TimeTicks unthrottled_runtime); | 178 base::TimeTicks runtime); |
| 179 | |
| 180 TimeBudgetPool* GetTimeBudgetPoolForQueue(TaskQueue* queue); | |
| 181 | |
| 182 // Schedule pumping because of given task queue. | |
| 183 void MaybeSchedulePumpQueue(const tracked_objects::Location& from_here, | |
| 184 base::TimeTicks now, | |
| 185 TaskQueue* queue); | |
| 186 void MaybeSchedulePumpQueueWithBudget( | |
| 187 const tracked_objects::Location& from_here, | |
| 188 base::TimeTicks now, | |
| 189 TaskQueue* queue, | |
| 190 TimeBudgetPool* budget); | |
| 103 | 191 |
| 104 TaskQueueMap throttled_queues_; | 192 TaskQueueMap throttled_queues_; |
| 105 base::Closure forward_immediate_work_closure_; | 193 base::Closure forward_immediate_work_closure_; |
| 106 scoped_refptr<TaskQueue> task_runner_; | 194 scoped_refptr<TaskQueue> task_runner_; |
| 107 RendererSchedulerImpl* renderer_scheduler_; // NOT OWNED | 195 RendererSchedulerImpl* renderer_scheduler_; // NOT OWNED |
| 108 base::TickClock* tick_clock_; // NOT OWNED | 196 base::TickClock* tick_clock_; // NOT OWNED |
| 109 const char* tracing_category_; // NOT OWNED | 197 const char* tracing_category_; // NOT OWNED |
| 110 std::unique_ptr<ThrottledTimeDomain> time_domain_; | 198 std::unique_ptr<ThrottledTimeDomain> time_domain_; |
| 111 | 199 |
| 112 CancelableClosureHolder pump_throttled_tasks_closure_; | 200 CancelableClosureHolder pump_throttled_tasks_closure_; |
| 113 base::TimeTicks pending_pump_throttled_tasks_runtime_; | 201 base::Optional<base::TimeTicks> pending_pump_throttled_tasks_runtime_; |
| 114 bool virtual_time_; | 202 bool virtual_time_; |
| 115 | 203 |
| 116 base::WeakPtrFactory<ThrottlingHelper> weak_factory_; | 204 std::unordered_map<TimeBudgetPool*, std::unique_ptr<TimeBudgetPool>> |
| 205 time_budget_pools_; | |
|
alex clarke (OOO till 29th)
2016/09/12 17:45:27
This construct gives me pause for thought. I can'
altimin
2016/09/14 11:23:18
My understanding was that TaskQueueThrottler (and
| |
| 206 std::unordered_map<TaskQueue*, TimeBudgetPool*> time_budget_pool_for_queue_; | |
|
alex clarke (OOO till 29th)
2016/09/12 17:45:27
Can we instead move TimeBudgetPool* into struct Me
altimin
2016/09/14 11:23:17
It will break the assumption that queue is throttl
alex clarke (OOO till 29th)
2016/09/15 12:19:34
I don't see why we'd want to have multiple maps (t
| |
| 117 | 207 |
| 118 DISALLOW_COPY_AND_ASSIGN(ThrottlingHelper); | 208 base::WeakPtrFactory<TaskQueueThrottler> weak_factory_; |
| 209 | |
| 210 DISALLOW_COPY_AND_ASSIGN(TaskQueueThrottler); | |
| 119 }; | 211 }; |
| 120 | 212 |
| 121 } // namespace scheduler | 213 } // namespace scheduler |
| 122 } // namespace blink | 214 } // namespace blink |
| 123 | 215 |
| 124 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELP ER_H_ | 216 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELP ER_H_ |
| OLD | NEW |