Chromium Code Reviews| Index: third_party/WebKit/Source/platform/scheduler/renderer/task_queue_throttler.h |
| diff --git a/third_party/WebKit/Source/platform/scheduler/renderer/throttling_helper.h b/third_party/WebKit/Source/platform/scheduler/renderer/task_queue_throttler.h |
| similarity index 35% |
| rename from third_party/WebKit/Source/platform/scheduler/renderer/throttling_helper.h |
| rename to third_party/WebKit/Source/platform/scheduler/renderer/task_queue_throttler.h |
| index c78266c55f731dfa24454e33ade47f1d41c66845..7816bff4f64ad75a28aa9f75c0c30b5546a0423e 100644 |
| --- a/third_party/WebKit/Source/platform/scheduler/renderer/throttling_helper.h |
| +++ b/third_party/WebKit/Source/platform/scheduler/renderer/task_queue_throttler.h |
| @@ -6,8 +6,13 @@ |
| #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELPER_H_ |
| #include <set> |
| +#include <map> |
|
Sami
2016/09/15 16:30:30
Do we need this?
altimin
2016/09/16 13:38:48
Done.
|
| +#include <unordered_map> |
| +#include "base/logging.h" |
| #include "base/macros.h" |
| +#include "base/optional.h" |
| +#include "base/threading/thread_checker.h" |
| #include "platform/scheduler/base/cancelable_closure_holder.h" |
| #include "platform/scheduler/base/time_domain.h" |
| #include "public/platform/WebViewScheduler.h" |
| @@ -19,36 +24,116 @@ class RendererSchedulerImpl; |
| class ThrottledTimeDomain; |
| class WebFrameSchedulerImpl; |
| -// The job of the ThrottlingHelper is to run tasks posted on throttled queues at |
| -// most once per second. This is done by disabling throttled queues and running |
| +// The job of the TaskQueueThrottler is to control tasks posted on throttled |
| +// queues. TaskQueueThrottler |
|
alex clarke (OOO till 29th)
2016/09/15 12:19:35
s/TaskQueueThrottler/The TaskQueueThrottler:
altimin
2016/09/15 15:52:11
Done.
|
| +// - runs throttled tasks once per second, |
| +// - controls time budget for task queues grouped in TimeBudgetPools. |
| +// This is done by disabling throttled queues and running |
|
alex clarke (OOO till 29th)
2016/09/15 12:19:35
Maybe add a blank line before this.
altimin
2016/09/15 15:52:11
Done.
|
| // a special "heart beat" function |PumpThrottledTasks| which when run |
| // temporarily enables throttled queues and inserts a fence to ensure tasks |
| // posted from a throttled task run next time the queue is pumped. |
| // |
| -// Of course the ThrottlingHelper isn't the only sub-system that wants to enable |
| -// or disable queues. E.g. RendererSchedulerImpl also does this for policy |
| -// reasons. To prevent the systems from fighting, clients of ThrottlingHelper |
| -// must use SetQueueEnabled rather than calling the function directly on the |
| -// queue. |
| +// Of course the TaskQueueThrottler isn't the only sub-system that wants to |
| +// enable or disable queues. E.g. RendererSchedulerImpl also does this for |
| +// policy reasons. To prevent the systems from fighting, clients of |
| +// TaskQueueThrottler must use SetQueueEnabled rather than calling the function |
| +// directly on the queue. |
| // |
| // There may be more than one system that wishes to throttle a queue (e.g. |
| -// renderer suspension vs tab level suspension) so the ThrottlingHelper keeps a |
| -// count of the number of systems that wish a queue to be throttled. |
| +// renderer suspension vs tab level suspension) so the TaskQueueThrottler keeps |
| +// a count of the number of systems that wish a queue to be throttled. |
| // See IncreaseThrottleRefCount & DecreaseThrottleRefCount. |
| -class BLINK_PLATFORM_EXPORT ThrottlingHelper : public TimeDomain::Observer { |
| +// |
| +// This class is main-thread only. |
| +class BLINK_PLATFORM_EXPORT TaskQueueThrottler : public TimeDomain::Observer { |
| public: |
| - ThrottlingHelper(RendererSchedulerImpl* renderer_scheduler, |
| - const char* tracing_category); |
| + // TimeBudgetPool represents a group of task queues which share a limit |
| + // on execution time. |
| + // |
|
alex clarke (OOO till 29th)
2016/09/15 12:19:35
Consider putting "This limit..." onto the same lin
altimin
2016/09/15 15:52:11
Done.
|
| + // This limit applies when task queues are already throttled by |
| + // TaskQueueThrottler. |
| + class TimeBudgetPool { |
| + public: |
| + ~TimeBudgetPool(); |
| + |
| + // Throttle task queues from this time budget pool if tasks are running |
| + // for more than |cpu_percentage| per cent of wall time. |
| + // This function does not affect internal time budget level. |
| + void SetTimeBudget(base::TimeTicks now, double cpu_percentage); |
| + |
| + // Adds |queue| to given pool. If the pool restriction does not allow |
| + // a task to be run immediately and |queue| is throttled, |queue| becomes |
| + // disabled. |
| + void AddQueue(base::TimeTicks now, TaskQueue* queue); |
| + // Removes |queue| from given pool. If it is throttled, it does not |
| + // become enabled immediately, but a call to |PumpThrottledTasks| |
| + // is scheduled. |
| + void RemoveQueue(base::TimeTicks now, TaskQueue* queue); |
| + |
| + void RecordTaskRunTime(base::TimeDelta task_run_time); |
| + |
| + // Enables this time budget pool. Queues from this pool will be |
| + // throttled based on their run time. |
| + void EnableThrottling(LazyNow* now); |
| + // Disables with time budget pool. Queues from this pool will not be |
| + // throttled based on their run time. A call to |PumpThrottledTasks| |
| + // will be scheduled to enable this queues back again and respect |
| + // timer alignment. Internal budget level will not regenerate with time. |
| + void DisableThrottling(LazyNow* now); |
| + bool IsThrottlingEnabled() const; |
| + |
| + const char* Name() const; |
| + |
| + // All queues should be removed before calling Close(). |
| + void Close(); |
| + |
| + private: |
| + friend class TaskQueueThrottler; |
| + |
| + FRIEND_TEST_ALL_PREFIXES(TaskQueueThrottlerTest, TimeBudgetPool); |
| + |
| + TimeBudgetPool(const char* name, |
| + TaskQueueThrottler* task_queue_throttler, |
| + base::TimeTicks now); |
| + |
| + bool IsAllowedToRun(base::TimeTicks now); |
| + base::TimeTicks GetNextAllowedRunTime(); |
| + |
| + // Advances |last_checkpoint_| to |now| if needed and recalculates |
| + // budget level. |
| + void Advance(base::TimeTicks now); |
| + |
| + // Returns state for tracing. |
| + void AsValueInto(base::trace_event::TracedValue* state, |
| + base::TimeTicks now) const; |
| + |
| + const char* name_; // NOT OWNED |
| + |
| + TaskQueueThrottler* task_queue_throttler_; |
| + |
| + base::TimeDelta current_budget_level_; |
| + base::TimeDelta max_budget_level_; |
| + base::TimeTicks last_checkpoint_; |
| + double cpu_percentage_; |
| + bool is_enabled_; |
| + |
| + std::unordered_set<TaskQueue*> associated_task_queues_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TimeBudgetPool); |
| + }; |
| - ~ThrottlingHelper() override; |
| + TaskQueueThrottler(RendererSchedulerImpl* renderer_scheduler, |
| + const char* tracing_category); |
| + |
| + ~TaskQueueThrottler() override; |
| // TimeDomain::Observer implementation: |
| - void OnTimeDomainHasImmediateWork() override; |
| - void OnTimeDomainHasDelayedWork() override; |
| + void OnTimeDomainHasImmediateWork(TaskQueue*) override; |
| + void OnTimeDomainHasDelayedWork(TaskQueue*) override; |
| // The purpose of this method is to make sure throttling doesn't conflict with |
| // enabling/disabling the queue for policy reasons. |
| - // If |task_queue| is throttled then the ThrottlingHelper remembers the |
| + // If |task_queue| is throttled then the TaskQueueThrottler remembers the |
| // |enabled| setting. In addition if |enabled| is false then the queue is |
| // immediatly disabled. Otherwise if |task_queue| not throttled then |
| // TaskQueue::SetEnabled(enabled) is called. |
| @@ -69,40 +154,69 @@ class BLINK_PLATFORM_EXPORT ThrottlingHelper : public TimeDomain::Observer { |
| // Returns true if the |task_queue| is throttled. |
| bool IsThrottled(TaskQueue* task_queue) const; |
| - // Tells the ThrottlingHelper we're using virtual time, which disables all |
| + // Tells the TaskQueueThrottler we're using virtual time, which disables all |
| // throttling. |
| void EnableVirtualTime(); |
| const ThrottledTimeDomain* time_domain() const { return time_domain_.get(); } |
| - static base::TimeTicks ThrottledRunTime(base::TimeTicks unthrottled_runtime); |
| + static base::TimeTicks AlignedThrottledRunTime( |
| + base::TimeTicks unthrottled_runtime); |
| const scoped_refptr<TaskQueue>& task_runner() const { return task_runner_; } |
| + // Returned object is owned by |TaskQueueThrottler|. |
| + TimeBudgetPool* CreateTimeBudgetPool(const char* name); |
| + |
| + void OnTaskRunTimeReported(TaskQueue* task_queue, |
|
alex clarke (OOO till 29th)
2016/09/15 12:19:36
Please document what this does.
altimin
2016/09/15 15:52:11
Done.
|
| + base::TimeTicks start_time, |
| + base::TimeTicks end_time); |
| + |
| + void AsValueInto(base::trace_event::TracedValue* state, |
| + base::TimeTicks now) const; |
| + |
| private: |
| struct Metadata { |
| - Metadata() : throttling_ref_count(0), enabled(false) {} |
| + Metadata() |
| + : throttling_ref_count(0), enabled(false), time_budget_pool(nullptr) {} |
| Metadata(size_t ref_count, bool is_enabled) |
| - : throttling_ref_count(ref_count), enabled(is_enabled) {} |
| + : throttling_ref_count(ref_count), |
| + enabled(is_enabled), |
| + time_budget_pool(nullptr) {} |
| size_t throttling_ref_count; |
| bool enabled; |
| + |
| + TimeBudgetPool* time_budget_pool; |
|
alex clarke (OOO till 29th)
2016/09/15 12:19:35
// NOT OWNED
altimin
2016/09/15 15:52:11
Deleted.
|
| }; |
| - using TaskQueueMap = std::map<TaskQueue*, Metadata>; |
| + using TaskQueueMap = std::unordered_map<TaskQueue*, Metadata>; |
| void PumpThrottledTasks(); |
| // Note |unthrottled_runtime| might be in the past. When this happens we |
| // compute the delay to the next runtime based on now rather than |
| // unthrottled_runtime. |
| - void MaybeSchedulePumpThrottledTasksLocked( |
| + void MaybeSchedulePumpThrottledTasks( |
| const tracked_objects::Location& from_here, |
| base::TimeTicks now, |
| - base::TimeTicks unthrottled_runtime); |
| + base::TimeTicks runtime); |
| + |
| + TimeBudgetPool* GetTimeBudgetPoolForQueue(TaskQueue* queue); |
| + |
| + // Schedule pumping because of given task queue. |
| + void MaybeSchedulePumpQueue( |
| + const tracked_objects::Location& from_here, |
| + base::TimeTicks now, |
| + TaskQueue* queue, |
| + base::Optional<base::TimeTicks> next_possible_run_time = base::nullopt); |
|
Sami
2016/09/15 16:30:30
nit: default arguments are generally not recommend
altimin
2016/09/16 13:38:48
Done.
|
| + |
| + // Return next possible time when queue is allowed to run in accordance |
| + // with throttling policy. |
| + base::TimeTicks GetNextAllowedRunTime(base::TimeTicks now, TaskQueue* queue); |
| TaskQueueMap throttled_queues_; |
| - base::Closure forward_immediate_work_closure_; |
| + base::Callback<void(TaskQueue*)> forward_immediate_work_callback_; |
| scoped_refptr<TaskQueue> task_runner_; |
| RendererSchedulerImpl* renderer_scheduler_; // NOT OWNED |
| base::TickClock* tick_clock_; // NOT OWNED |
| @@ -110,12 +224,16 @@ class BLINK_PLATFORM_EXPORT ThrottlingHelper : public TimeDomain::Observer { |
| std::unique_ptr<ThrottledTimeDomain> time_domain_; |
| CancelableClosureHolder pump_throttled_tasks_closure_; |
| - base::TimeTicks pending_pump_throttled_tasks_runtime_; |
| + base::Optional<base::TimeTicks> pending_pump_throttled_tasks_runtime_; |
| bool virtual_time_; |
| - base::WeakPtrFactory<ThrottlingHelper> weak_factory_; |
| + std::unordered_map<TimeBudgetPool*, std::unique_ptr<TimeBudgetPool>> |
| + time_budget_pools_; |
| + std::unordered_map<TaskQueue*, TimeBudgetPool*> time_budget_pool_for_queue_; |
| + |
| + base::WeakPtrFactory<TaskQueueThrottler> weak_factory_; |
| - DISALLOW_COPY_AND_ASSIGN(ThrottlingHelper); |
| + DISALLOW_COPY_AND_ASSIGN(TaskQueueThrottler); |
| }; |
| } // namespace scheduler |