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

Side by Side Diff: Source/platform/scheduler/Scheduler.h

Issue 595023002: Implement idle task scheduling. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fix comment Created 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 Scheduler_h 5 #ifndef Scheduler_h
6 #define Scheduler_h 6 #define Scheduler_h
7 7
8 #include "platform/PlatformExport.h" 8 #include "platform/PlatformExport.h"
9 #include "platform/scheduler/TracedTask.h" 9 #include "platform/scheduler/TracedTask.h"
10 #include "wtf/DoubleBufferedDeque.h" 10 #include "wtf/DoubleBufferedDeque.h"
11 #include "wtf/Functional.h" 11 #include "wtf/Functional.h"
12 #include "wtf/Noncopyable.h" 12 #include "wtf/Noncopyable.h"
13 #include "wtf/ThreadingPrimitives.h" 13 #include "wtf/ThreadingPrimitives.h"
14 14
15 namespace blink { 15 namespace blink {
16 class WebThread; 16 class WebThread;
17 struct WebBeginFrameArgs; 17 struct WebBeginFrameArgs;
18 18
19 // The scheduler is an opinionated gateway for arranging work to be run on the 19 // The scheduler is an opinionated gateway for arranging work to be run on the
20 // main thread. It decides which tasks get priority over others based on a 20 // main thread. It decides which tasks get priority over others based on a
21 // scheduling policy and the overall system state. 21 // scheduling policy and the overall system state.
22 class PLATFORM_EXPORT Scheduler { 22 class PLATFORM_EXPORT Scheduler {
23 WTF_MAKE_NONCOPYABLE(Scheduler); 23 WTF_MAKE_NONCOPYABLE(Scheduler);
24 public: 24 public:
25 typedef Function<void()> Task; 25 typedef Function<void()> Task;
26 // An IdleTask is passed an allotted time in CLOCK_MONOTONIC milliseconds an d is expected to complete within this timeframe. 26 // An IdleTask is passed a deadline in CLOCK_MONOTONIC seconds and is expect ed to complete before this deadline.
27 typedef Function<void(double allottedTimeMs)> IdleTask; 27 typedef Function<void(double deadlineSeconds)> IdleTask;
28 28
29 static Scheduler* shared(); 29 static Scheduler* shared();
30 static void initializeOnMainThread(); 30 static void initializeOnMainThread();
31 static void shutdown(); 31 static void shutdown();
32 32
33 // Called to notify about the start of a new frame. 33 // Called to notify about the start of a new frame.
34 void willBeginFrame(const WebBeginFrameArgs&); 34 void willBeginFrame(double frameDeadlineSeconds);
35 35
36 // Called to notify that a previously begun frame was committed. 36 // Called to notify that a previously begun frame was committed.
37 void didCommitFrameToCompositor(); 37 void didCommitFrameToCompositor();
38 38
39 // The following entrypoints are used to schedule different types of tasks 39 // The following entrypoints are used to schedule different types of tasks
40 // to be run on the main thread. They can be called from any thread. 40 // to be run on the main thread. They can be called from any thread.
41 void postInputTask(const TraceLocation&, const Task&); 41 void postInputTask(const TraceLocation&, const Task&);
42 void postCompositorTask(const TraceLocation&, const Task&); 42 void postCompositorTask(const TraceLocation&, const Task&);
43 void postIpcTask(const TraceLocation&, const Task&); 43 void postIpcTask(const TraceLocation&, const Task&);
44 void postTask(const TraceLocation&, const Task&); // For generic (low priori ty) tasks. 44 void postTask(const TraceLocation&, const Task&); // For generic (low priori ty) tasks.
45 void postIdleTask(const TraceLocation&, const IdleTask&); // For non-critica l tasks which may be reordered relative to other task types. 45 // For non-critical tasks which may be reordered relative to other task type s and may be starved
46 // for an arbitrarily long time if no idle time is available.
47 void postIdleTask(const TraceLocation&, const IdleTask&);
46 48
47 // Tells the scheduler that the system received an input event. This causes the scheduler to go into 49 // Tells the scheduler that the system received an input event. This causes the scheduler to go into
48 // Compositor Priority mode for a short duration. 50 // Compositor Priority mode for a short duration.
49 void didReceiveInputEvent(); 51 void didReceiveInputEvent();
50 52
51 // Returns true if there is high priority work pending on the main thread 53 // Returns true if there is high priority work pending on the main thread
52 // and the caller should yield to let the scheduler service that work. 54 // and the caller should yield to let the scheduler service that work.
53 // Can be called on any thread. 55 // Can be called on any thread.
54 bool shouldYieldForHighPriorityWork() const; 56 bool shouldYieldForHighPriorityWork() const;
55 57
56 // The shared timer can be used to schedule a periodic callback which may 58 // The shared timer can be used to schedule a periodic callback which may
57 // get preempted by higher priority work. 59 // get preempted by higher priority work.
58 void setSharedTimerFiredFunction(void (*function)()); 60 void setSharedTimerFiredFunction(void (*function)());
59 void setSharedTimerFireInterval(double); 61 void setSharedTimerFireInterval(double);
60 void stopSharedTimer(); 62 void stopSharedTimer();
61 63
64 // Returns the time in CLOCK_MONOTONIC seconds until the deadline for runnin g idle tasks during
65 // the current frame expires.
66 double currentFrameDeadlineForIdleTasks() const;
Sami 2014/10/06 09:48:30 The name makes it sound like this is a deadline bu
rmcilroy 2014/10/06 11:29:42 Comment was unclear - fixed, thanks.
67
62 protected: 68 protected:
63 class MainThreadPendingTaskRunner; 69 class MainThreadPendingTaskRunner;
64 class MainThreadPendingHighPriorityTaskRunner; 70 class MainThreadPendingHighPriorityTaskRunner;
71 class MainThreadPendingIdleTaskRunner;
65 friend class MainThreadPendingTaskRunner; 72 friend class MainThreadPendingTaskRunner;
66 friend class MainThreadPendingHighPriorityTaskRunner; 73 friend class MainThreadPendingHighPriorityTaskRunner;
67 74
68 enum SchedulerPolicy { 75 enum SchedulerPolicy {
69 Normal, 76 Normal,
70 CompositorPriority, 77 CompositorPriority,
71 }; 78 };
72 79
73 Scheduler(); 80 Scheduler();
74 virtual ~Scheduler(); 81 virtual ~Scheduler();
75 82
76 void scheduleIdleTask(const TraceLocation&, const IdleTask&);
77 void postHighPriorityTaskInternal(const TraceLocation&, const Task&, const c har* traceName); 83 void postHighPriorityTaskInternal(const TraceLocation&, const Task&, const c har* traceName);
84 void postIdleTaskInternal(const TraceLocation&, const IdleTask&, const char* traceName);
78 85
79 static void sharedTimerAdapter(); 86 static void sharedTimerAdapter();
80 87
81 // Start of main thread only members ----------------------------------- 88 // Start of main thread only members -----------------------------------
82 89
83 // Only does work in CompositorPriority mode. Returns true if any work was d one. 90 // Only does work in CompositorPriority mode. Returns true if any work was d one.
84 bool runPendingHighPriorityTasksIfInCompositorPriority(); 91 bool runPendingHighPriorityTasksIfInCompositorPriority();
85 92
93 // Only does work if canRunIdleTask. Returns true if any work was done.
94 bool maybeRunPendingIdleTask();
95
96 TracedIdleTask takeFirstPendingIdleTask();
97
86 // Returns true if any work was done. 98 // Returns true if any work was done.
87 bool swapQueuesAndRunPendingTasks(); 99 bool swapQueuesAndRunPendingTasks();
88 100
89 void swapQueuesRunPendingTasksAndAllowHighPriorityTaskRunnerPosting(); 101 void swapQueuesRunPendingTasksAndAllowHighPriorityTaskRunnerPosting();
90 102
91 // Returns true if any work was done. 103 // Returns true if any work was done.
92 bool executeHighPriorityTasks(Deque<TracedTask>&); 104 bool executeHighPriorityTasks(Deque<TracedStandardTask>&);
105
106 // Returns true if the scheduler can run idle tasks at this time.
107 bool canRunIdleTask() const;
93 108
94 // Return the current SchedulerPolicy. 109 // Return the current SchedulerPolicy.
95 SchedulerPolicy schedulerPolicy() const; 110 SchedulerPolicy schedulerPolicy() const;
96 111
97 void maybeEnterNormalSchedulerPolicy(); 112 void maybeEnterNormalSchedulerPolicy();
98 113
99 // Must be called while m_pendingTasksMutex is locked. 114 // Must be called while m_pendingTasksMutex is locked.
100 void maybePostMainThreadPendingHighPriorityTaskRunner(); 115 void maybePostMainThreadPendingHighPriorityTaskRunner();
101 116
117 // Returns true if an idle task was posted to the main thread for execution.
118 bool maybePostMainThreadPendingIdleTask();
119
102 void tickSharedTimer(); 120 void tickSharedTimer();
103 121
104 void (*m_sharedTimerFunction)(); 122 void (*m_sharedTimerFunction)();
105 123
106 // End of main thread only members ------------------------------------- 124 // End of main thread only members -------------------------------------
107 125
108 bool hasPendingHighPriorityWork() const; 126 bool hasPendingHighPriorityWork() const;
109 127
110 void enterSchedulerPolicyLocked(SchedulerPolicy); 128 void enterSchedulerPolicyLocked(SchedulerPolicy);
111 129
112 void enterSchedulerPolicy(SchedulerPolicy); 130 void enterSchedulerPolicy(SchedulerPolicy);
113 131
114 static Scheduler* s_sharedScheduler; 132 static Scheduler* s_sharedScheduler;
115 133
116 WebThread* m_mainThread; 134 WebThread* m_mainThread;
117 135
118 // This mutex protects calls to the pending task queue, m_highPriorityTaskRu nnerPosted and 136 // This mutex protects calls to the pending task queue, m_highPriorityTaskRu nnerPosted and
119 // m_compositorPriorityPolicyEndTimeSeconds. 137 // m_compositorPriorityPolicyEndTimeSeconds.
120 Mutex m_pendingTasksMutex; 138 Mutex m_pendingTasksMutex;
121 DoubleBufferedDeque<TracedTask> m_pendingHighPriorityTasks; 139 DoubleBufferedDeque<TracedStandardTask> m_pendingHighPriorityTasks;
140 Deque<TracedIdleTask> m_pendingIdleTasks;
122 double m_compositorPriorityPolicyEndTimeSeconds; 141 double m_compositorPriorityPolicyEndTimeSeconds;
123 142
143 bool m_currentFrameCommitted;
144 double m_currentFrameDeadlineSeconds;
145
124 // Declared volatile as it is atomically incremented. 146 // Declared volatile as it is atomically incremented.
125 volatile int m_highPriorityTaskCount; 147 volatile int m_highPriorityTaskCount;
126 148
127 bool m_highPriorityTaskRunnerPosted; 149 bool m_highPriorityTaskRunnerPosted;
128 150
129 // Don't access m_schedulerPolicy directly, use enterSchedulerPolicyLocked a nd SchedulerPolicy instead. 151 // Don't access m_schedulerPolicy directly, use enterSchedulerPolicyLocked a nd SchedulerPolicy instead.
130 volatile int m_schedulerPolicy; 152 volatile int m_schedulerPolicy;
131 }; 153 };
132 154
133 } // namespace blink 155 } // namespace blink
134 156
135 #endif // Scheduler_h 157 #endif // Scheduler_h
OLDNEW
« no previous file with comments | « no previous file | Source/platform/scheduler/Scheduler.cpp » ('j') | Source/platform/scheduler/Scheduler.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698