Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Side by Side Diff: third_party/WebKit/Source/platform/scheduler/renderer/task_queue_throttler.h

Issue 2258133002: [scheduler] Implement time-based cpu throttling. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changed similarity Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
9 10
11 #include "base/logging.h"
10 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/optional.h"
14 #include "base/threading/thread_checker.h"
11 #include "platform/scheduler/base/cancelable_closure_holder.h" 15 #include "platform/scheduler/base/cancelable_closure_holder.h"
12 #include "platform/scheduler/base/time_domain.h" 16 #include "platform/scheduler/base/time_domain.h"
13 #include "public/platform/WebViewScheduler.h" 17 #include "public/platform/WebViewScheduler.h"
14 18
15 namespace blink { 19 namespace blink {
16 namespace scheduler { 20 namespace scheduler {
17 21
18 class RendererSchedulerImpl; 22 class RendererSchedulerImpl;
19 class ThrottledTimeDomain; 23 class ThrottledTimeDomain;
20 class WebFrameSchedulerImpl; 24 class WebFrameSchedulerImpl;
21 25
22 // The job of the ThrottlingHelper is to run tasks posted on throttled queues at 26 // 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 27 // queues. TaskQueueThrottler
28 // - runs throttled tasks once per second,
29 // - controls time budget for task queues grouped in TimeBudgetPools.
30 // This is done by disabling throttled queues and running
24 // a special "heart beat" function |PumpThrottledTasks| which when run 31 // a special "heart beat" function |PumpThrottledTasks| which when run
25 // temporarily enables throttled queues and inserts a fence to ensure tasks 32 // temporarily enables throttled queues and inserts a fence to ensure tasks
26 // posted from a throttled task run next time the queue is pumped. 33 // posted from a throttled task run next time the queue is pumped.
27 // 34 //
28 // Of course the ThrottlingHelper isn't the only sub-system that wants to enable 35 // 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.
36 // enable
29 // or disable queues. E.g. RendererSchedulerImpl also does this for policy 37 // or disable queues. E.g. RendererSchedulerImpl also does this for policy
30 // reasons. To prevent the systems from fighting, clients of ThrottlingHelper 38 // reasons. To prevent the systems from fighting, clients of TaskQueueThrottler
31 // must use SetQueueEnabled rather than calling the function directly on the 39 // must use SetQueueEnabled rather than calling the function directly on the
32 // queue. 40 // 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
44 // a
36 // count of the number of systems that wish a queue to be throttled. 45 // 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 class BLINK_PLATFORM_EXPORT TaskQueueThrottler : public TimeDomain::Observer {
39 public: 48 public:
40 ThrottlingHelper(RendererSchedulerImpl* renderer_scheduler, 49 // This class is main-thread only.
41 const char* tracing_category); 50 class TimeBudgetPool {
51 public:
52 ~TimeBudgetPool();
42 53
43 ~ThrottlingHelper() override; 54 // Throttle task queues from this time budget pool if tasks are running
55 // for more than |cpu_percentage| per cent of wall time.
56 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.
57
58 void AddQueue(TaskQueue* queue);
59 void RemoveQueue(TaskQueue* queue);
60
61 void RecordTaskRunTime(base::TimeDelta task_run_time);
62
63 bool IsAllowedToRun(base::TimeTicks now);
64 base::TimeTicks NextAllowedRunTime();
65
66 void Close();
67
68 private:
69 friend class TaskQueueThrottler;
70
71 TimeBudgetPool(TaskQueueThrottler* task_queue_throttler,
72 base::TimeTicks now);
73
74 inline void CheckOnValidThread() const;
75
76 TaskQueueThrottler* task_queue_throttler_;
77
78 base::TimeDelta current_budget_level_;
79 base::TimeTicks last_checkpoint_;
80 double cpu_percentage_;
81 };
Sami 2016/09/07 15:20:43 DISALLOW_COPY_AND_ASSIGN
altimin 2016/09/09 15:43:58 Done.
82
83 TaskQueueThrottler(RendererSchedulerImpl* renderer_scheduler,
84 const char* tracing_category);
85
86 ~TaskQueueThrottler() override;
44 87
45 // TimeDomain::Observer implementation: 88 // TimeDomain::Observer implementation:
46 void OnTimeDomainHasImmediateWork() override; 89 void OnTimeDomainHasImmediateWork() override;
47 void OnTimeDomainHasDelayedWork() override; 90 void OnTimeDomainHasDelayedWork() override;
48 91
49 // The purpose of this method is to make sure throttling doesn't conflict with 92 // The purpose of this method is to make sure throttling doesn't conflict with
50 // enabling/disabling the queue for policy reasons. 93 // enabling/disabling the queue for policy reasons.
51 // If |task_queue| is throttled then the ThrottlingHelper remembers the 94 // If |task_queue| is throttled then the TaskQueueThrottler remembers the
52 // |enabled| setting. In addition if |enabled| is false then the queue is 95 // |enabled| setting. In addition if |enabled| is false then the queue is
53 // immediatly disabled. Otherwise if |task_queue| not throttled then 96 // immediatly disabled. Otherwise if |task_queue| not throttled then
54 // TaskQueue::SetEnabled(enabled) is called. 97 // TaskQueue::SetEnabled(enabled) is called.
55 void SetQueueEnabled(TaskQueue* task_queue, bool enabled); 98 void SetQueueEnabled(TaskQueue* task_queue, bool enabled);
56 99
57 // Increments the throttled refcount and causes |task_queue| to be throttled 100 // Increments the throttled refcount and causes |task_queue| to be throttled
58 // if its not already throttled. 101 // if its not already throttled.
59 void IncreaseThrottleRefCount(TaskQueue* task_queue); 102 void IncreaseThrottleRefCount(TaskQueue* task_queue);
60 103
61 // If the refcouint is non-zero it's decremented. If the throttled refcount 104 // 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 105 // becomes zero then |task_queue| is unthrottled. If the refcount was already
63 // zero this function does nothing. 106 // zero this function does nothing.
64 void DecreaseThrottleRefCount(TaskQueue* task_queue); 107 void DecreaseThrottleRefCount(TaskQueue* task_queue);
65 108
66 // Removes |task_queue| from |throttled_queues_|. 109 // Removes |task_queue| from |throttled_queues_|.
67 void UnregisterTaskQueue(TaskQueue* task_queue); 110 void UnregisterTaskQueue(TaskQueue* task_queue);
68 111
69 // Returns true if the |task_queue| is throttled. 112 // Returns true if the |task_queue| is throttled.
70 bool IsThrottled(TaskQueue* task_queue) const; 113 bool IsThrottled(TaskQueue* task_queue) const;
71 114
72 // Tells the ThrottlingHelper we're using virtual time, which disables all 115 // Tells the TaskQueueThrottler we're using virtual time, which disables all
73 // throttling. 116 // throttling.
74 void EnableVirtualTime(); 117 void EnableVirtualTime();
75 118
76 const ThrottledTimeDomain* time_domain() const { return time_domain_.get(); } 119 const ThrottledTimeDomain* time_domain() const { return time_domain_.get(); }
77 120
78 static base::TimeTicks ThrottledRunTime(base::TimeTicks unthrottled_runtime); 121 static base::TimeTicks AlignedThrottledRunTime(
122 base::TimeTicks unthrottled_runtime);
79 123
80 const scoped_refptr<TaskQueue>& task_runner() const { return task_runner_; } 124 const scoped_refptr<TaskQueue>& task_runner() const { return task_runner_; }
81 125
126 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.
127
128 void OnTaskRunTimeReported(TaskQueue* task_queue,
129 base::TimeTicks start_time,
130 base::TimeTicks end_time);
131
82 private: 132 private:
83 struct Metadata { 133 struct Metadata {
84 Metadata() : throttling_ref_count(0), enabled(false) {} 134 Metadata() : throttling_ref_count(0), enabled(false) {}
85 135
86 Metadata(size_t ref_count, bool is_enabled) 136 Metadata(size_t ref_count, bool is_enabled)
87 : throttling_ref_count(ref_count), enabled(is_enabled) {} 137 : throttling_ref_count(ref_count), enabled(is_enabled) {}
88 138
89 size_t throttling_ref_count; 139 size_t throttling_ref_count;
90 bool enabled; 140 bool enabled;
91 }; 141 };
92 using TaskQueueMap = std::map<TaskQueue*, Metadata>; 142 using TaskQueueMap = std::map<TaskQueue*, Metadata>;
93 143
94 void PumpThrottledTasks(); 144 void PumpThrottledTasks();
95 145
96 // Note |unthrottled_runtime| might be in the past. When this happens we 146 // 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 147 // compute the delay to the next runtime based on now rather than
98 // unthrottled_runtime. 148 // unthrottled_runtime.
99 void MaybeSchedulePumpThrottledTasksLocked( 149 void MaybeSchedulePumpThrottledTasks(
100 const tracked_objects::Location& from_here, 150 const tracked_objects::Location& from_here,
101 base::TimeTicks now, 151 base::TimeTicks now,
102 base::TimeTicks unthrottled_runtime); 152 base::TimeTicks unthrottled_runtime);
103 153
104 TaskQueueMap throttled_queues_; 154 TaskQueueMap throttled_queues_;
105 base::Closure forward_immediate_work_closure_; 155 base::Closure forward_immediate_work_closure_;
106 scoped_refptr<TaskQueue> task_runner_; 156 scoped_refptr<TaskQueue> task_runner_;
107 RendererSchedulerImpl* renderer_scheduler_; // NOT OWNED 157 RendererSchedulerImpl* renderer_scheduler_; // NOT OWNED
108 base::TickClock* tick_clock_; // NOT OWNED 158 base::TickClock* tick_clock_; // NOT OWNED
109 const char* tracing_category_; // NOT OWNED 159 const char* tracing_category_; // NOT OWNED
110 std::unique_ptr<ThrottledTimeDomain> time_domain_; 160 std::unique_ptr<ThrottledTimeDomain> time_domain_;
111 161
112 CancelableClosureHolder pump_throttled_tasks_closure_; 162 CancelableClosureHolder pump_throttled_tasks_closure_;
113 base::TimeTicks pending_pump_throttled_tasks_runtime_; 163 base::Optional<base::TimeTicks> pending_pump_throttled_tasks_runtime_;
114 bool virtual_time_; 164 bool virtual_time_;
115 165
116 base::WeakPtrFactory<ThrottlingHelper> weak_factory_; 166 std::map<TimeBudgetPool*, std::unique_ptr<TimeBudgetPool>> time_budget_pools_;
167 std::map<TaskQueue*, TimeBudgetPool*> time_budget_pool_for_queue_;
117 168
118 DISALLOW_COPY_AND_ASSIGN(ThrottlingHelper); 169 base::WeakPtrFactory<TaskQueueThrottler> weak_factory_;
170
171 DISALLOW_COPY_AND_ASSIGN(TaskQueueThrottler);
119 }; 172 };
120 173
121 } // namespace scheduler 174 } // namespace scheduler
122 } // namespace blink 175 } // namespace blink
123 176
124 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELP ER_H_ 177 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELP ER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698