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

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

Issue 2258133002: [scheduler] Implement time-based cpu throttling. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 4 years, 2 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELPER_ H_
6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELPER_ H_
7
8 #include <set>
9
10 #include "base/macros.h"
11 #include "platform/scheduler/base/cancelable_closure_holder.h"
12 #include "platform/scheduler/base/time_domain.h"
13 #include "public/platform/WebViewScheduler.h"
14
15 namespace blink {
16 namespace scheduler {
17
18 class RendererSchedulerImpl;
19 class ThrottledTimeDomain;
20 class WebFrameSchedulerImpl;
21
22 // The job of the ThrottlingHelper is to run tasks posted on throttled queues at
23 // most once per second. This is done by disabling throttled queues and running
24 // a special "heart beat" function |PumpThrottledTasks| which when run
25 // temporarily enables throttled queues and inserts a fence to ensure tasks
26 // posted from a throttled task run next time the queue is pumped.
27 //
28 // Of course the ThrottlingHelper isn't the only sub-system that wants to enable
29 // or disable queues. E.g. RendererSchedulerImpl also does this for policy
30 // reasons. To prevent the systems from fighting, clients of ThrottlingHelper
31 // must use SetQueueEnabled rather than calling the function directly on the
32 // queue.
33 //
34 // 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
36 // count of the number of systems that wish a queue to be throttled.
37 // See IncreaseThrottleRefCount & DecreaseThrottleRefCount.
38 class BLINK_PLATFORM_EXPORT ThrottlingHelper : public TimeDomain::Observer {
39 public:
40 ThrottlingHelper(RendererSchedulerImpl* renderer_scheduler,
41 const char* tracing_category);
42
43 ~ThrottlingHelper() override;
44
45 // TimeDomain::Observer implementation:
46 void OnTimeDomainHasImmediateWork() override;
47 void OnTimeDomainHasDelayedWork() override;
48
49 // The purpose of this method is to make sure throttling doesn't conflict with
50 // enabling/disabling the queue for policy reasons.
51 // If |task_queue| is throttled then the ThrottlingHelper remembers the
52 // |enabled| setting. In addition if |enabled| is false then the queue is
53 // immediatly disabled. Otherwise if |task_queue| not throttled then
54 // TaskQueue::SetEnabled(enabled) is called.
55 void SetQueueEnabled(TaskQueue* task_queue, bool enabled);
56
57 // Increments the throttled refcount and causes |task_queue| to be throttled
58 // if its not already throttled.
59 void IncreaseThrottleRefCount(TaskQueue* task_queue);
60
61 // 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
63 // zero this function does nothing.
64 void DecreaseThrottleRefCount(TaskQueue* task_queue);
65
66 // Removes |task_queue| from |throttled_queues_|.
67 void UnregisterTaskQueue(TaskQueue* task_queue);
68
69 // Returns true if the |task_queue| is throttled.
70 bool IsThrottled(TaskQueue* task_queue) const;
71
72 // Tells the ThrottlingHelper we're using virtual time, which disables all
73 // throttling.
74 void EnableVirtualTime();
75
76 const ThrottledTimeDomain* time_domain() const { return time_domain_.get(); }
77
78 static base::TimeTicks ThrottledRunTime(base::TimeTicks unthrottled_runtime);
79
80 const scoped_refptr<TaskQueue>& task_runner() const { return task_runner_; }
81
82 private:
83 struct Metadata {
84 Metadata() : throttling_ref_count(0), enabled(false) {}
85
86 Metadata(size_t ref_count, bool is_enabled)
87 : throttling_ref_count(ref_count), enabled(is_enabled) {}
88
89 size_t throttling_ref_count;
90 bool enabled;
91 };
92 using TaskQueueMap = std::map<TaskQueue*, Metadata>;
93
94 void PumpThrottledTasks();
95
96 // 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
98 // unthrottled_runtime.
99 void MaybeSchedulePumpThrottledTasksLocked(
100 const tracked_objects::Location& from_here,
101 base::TimeTicks now,
102 base::TimeTicks unthrottled_runtime);
103
104 TaskQueueMap throttled_queues_;
105 base::Closure forward_immediate_work_closure_;
106 scoped_refptr<TaskQueue> task_runner_;
107 RendererSchedulerImpl* renderer_scheduler_; // NOT OWNED
108 base::TickClock* tick_clock_; // NOT OWNED
109 const char* tracing_category_; // NOT OWNED
110 std::unique_ptr<ThrottledTimeDomain> time_domain_;
111
112 CancelableClosureHolder pump_throttled_tasks_closure_;
113 base::TimeTicks pending_pump_throttled_tasks_runtime_;
114 bool virtual_time_;
115
116 base::WeakPtrFactory<ThrottlingHelper> weak_factory_;
117
118 DISALLOW_COPY_AND_ASSIGN(ThrottlingHelper);
119 };
120
121 } // namespace scheduler
122 } // namespace blink
123
124 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_THROTTLING_HELP ER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698