 Chromium Code Reviews
 Chromium Code Reviews Issue 2258133002:
  [scheduler] Implement time-based cpu throttling.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 2258133002:
  [scheduler] Implement time-based cpu throttling.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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 59% | 
| 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..6b32924e3911b25bdd53e8123fcb28e81d0254b0 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,12 @@ | 
| #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELPER_H_ | 
| #include <set> | 
| +#include <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,28 +23,67 @@ 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 | 
| +// - runs throttled tasks once per second, | 
| +// - controls time budget for task queues grouped in TimeBudgetPools. | 
| +// This is done by disabling throttled queues and running | 
| // 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 | 
| +// Of course the TaskQueueThrottler isn't the only sub-system that wants to | 
| 
Sami
2016/09/07 15:20:44
Could you re-wrap the text here so it doesn't look
 
altimin
2016/09/09 15:43:58
Done.
 | 
| +// enable | 
| // or disable queues. E.g. RendererSchedulerImpl also does this for policy | 
| -// reasons. To prevent the systems from fighting, clients of ThrottlingHelper | 
| +// 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 | 
| +// 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 { | 
| +class BLINK_PLATFORM_EXPORT TaskQueueThrottler : public TimeDomain::Observer { | 
| public: | 
| - ThrottlingHelper(RendererSchedulerImpl* renderer_scheduler, | 
| - const char* tracing_category); | 
| + // This class is main-thread only. | 
| + class TimeBudgetPool { | 
| + public: | 
| + ~TimeBudgetPool(); | 
| - ~ThrottlingHelper() override; | 
| + // Throttle task queues from this time budget pool if tasks are running | 
| + // for more than |cpu_percentage| per cent of wall time. | 
| + void SetTimeBudget(double cpu_percentange); | 
| 
Sami
2016/09/07 15:20:44
typo: percentage
Please also mention what this do
 
altimin
2016/09/09 15:43:58
Done.
 | 
| + | 
| + void AddQueue(TaskQueue* queue); | 
| + void RemoveQueue(TaskQueue* queue); | 
| + | 
| + void RecordTaskRunTime(base::TimeDelta task_run_time); | 
| + | 
| + bool IsAllowedToRun(base::TimeTicks now); | 
| + base::TimeTicks NextAllowedRunTime(); | 
| + | 
| + void Close(); | 
| + | 
| + private: | 
| + friend class TaskQueueThrottler; | 
| + | 
| + TimeBudgetPool(TaskQueueThrottler* task_queue_throttler, | 
| + base::TimeTicks now); | 
| + | 
| + inline void CheckOnValidThread() const; | 
| + | 
| + TaskQueueThrottler* task_queue_throttler_; | 
| + | 
| + base::TimeDelta current_budget_level_; | 
| + base::TimeTicks last_checkpoint_; | 
| + double cpu_percentage_; | 
| + }; | 
| 
Sami
2016/09/07 15:20:43
DISALLOW_COPY_AND_ASSIGN
 
altimin
2016/09/09 15:43:58
Done.
 | 
| + | 
| + TaskQueueThrottler(RendererSchedulerImpl* renderer_scheduler, | 
| + const char* tracing_category); | 
| + | 
| + ~TaskQueueThrottler() override; | 
| // TimeDomain::Observer implementation: | 
| void OnTimeDomainHasImmediateWork() override; | 
| @@ -48,7 +91,7 @@ class BLINK_PLATFORM_EXPORT ThrottlingHelper : public TimeDomain::Observer { | 
| // 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,16 +112,23 @@ 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_; } | 
| + TimeBudgetPool* CreateTimeBudgetPool(); | 
| 
Sami
2016/09/07 15:20:44
Who owns this? (unique_ptr if its the caller)
 
altimin
2016/09/09 15:43:58
Done.
 | 
| + | 
| + void OnTaskRunTimeReported(TaskQueue* task_queue, | 
| + base::TimeTicks start_time, | 
| + base::TimeTicks end_time); | 
| + | 
| private: | 
| struct Metadata { | 
| Metadata() : throttling_ref_count(0), enabled(false) {} | 
| @@ -96,7 +146,7 @@ class BLINK_PLATFORM_EXPORT ThrottlingHelper : public TimeDomain::Observer { | 
| // 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); | 
| @@ -110,12 +160,15 @@ 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::map<TimeBudgetPool*, std::unique_ptr<TimeBudgetPool>> time_budget_pools_; | 
| + std::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 |