1 // Copyright 2018 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_BLINK_RENDERER_PLATFORM_SCHEDULER_COMMON_FEATURES_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_COMMON_FEATURES_H_
7 
8 #include "base/feature_list.h"
9 #include "base/metrics/field_trial_params.h"
10 #include "base/time/time.h"
11 #include "third_party/blink/renderer/platform/platform_export.h"
12 
13 namespace blink {
14 namespace scheduler {
15 
16 const base::Feature kDedicatedWorkerThrottling{
17     "BlinkSchedulerWorkerThrottling", base::FEATURE_DISABLED_BY_DEFAULT};
18 
19 const base::Feature kBestEffortPriorityForFindInPage{
20     "BlinkSchedulerBestEffortPriorityForFindInPage",
21     base::FEATURE_DISABLED_BY_DEFAULT};
22 
23 // COMPOSITING PRIORITY EXPERIMENT CONTROLS
24 
25 // If enabled, the compositor will always be set to kVeryHighPriority if it
26 // is not already set to kHighestPriority.
27 const base::Feature kVeryHighPriorityForCompositingAlways{
28     "BlinkSchedulerVeryHighPriorityForCompositingAlways",
29     base::FEATURE_DISABLED_BY_DEFAULT};
30 
31 // If enabled, compositor priority will be set to kVeryHighPriority if it will
32 // be fast and is not already set to kHighestPriority.
33 const base::Feature kVeryHighPriorityForCompositingWhenFast{
34     "BlinkSchedulerVeryHighPriorityForCompositingWhenFast",
35     base::FEATURE_DISABLED_BY_DEFAULT};
36 
37 // If enabled, compositor priority will be set to kVeryHighPriority if the last
38 // task completed was not a compositor task, and kNormalPriority if the last
39 // task completed was a compositor task.
40 const base::Feature kVeryHighPriorityForCompositingAlternating{
41     "BlinkSchedulerVeryHighPriorityForCompositingAlternating",
42     base::FEATURE_DISABLED_BY_DEFAULT};
43 
44 // If enabled, compositor priority will be set to kVeryHighPriority if no
45 // compositor task has run for some time determined by the finch parameter
46 // kCompositingDelayLength. Once a compositor task runs, it will be reset
47 // to kNormalPriority.
48 const base::Feature kVeryHighPriorityForCompositingAfterDelay{
49     "BlinkSchedulerVeryHighPriorityForCompositingAfterDelay",
50     base::FEATURE_ENABLED_BY_DEFAULT};
51 
52 // Param for kVeryHighPriorityForCompositingAfterDelay experiment. How long
53 // in ms the compositor will wait to be prioritized if no compositor tasks run.
54 constexpr base::FeatureParam<int> kCompositingDelayLength{
55     &kVeryHighPriorityForCompositingAfterDelay, "CompositingDelayLength", 100};
56 
57 // If enabled, compositor priority will be set to kVeryHighPriority until
58 // a budget has been exhausted. Once the budget runs out, the priority will
59 // be set to kNormalPriority until there is enough budget to reprioritize.
60 const base::Feature kVeryHighPriorityForCompositingBudget{
61     "BlinkSchedulerVeryHighPriorityForCompositingBudget",
62     base::FEATURE_DISABLED_BY_DEFAULT};
63 
64 // Param for kVeryHighPriorityForCompositingBudget experiment. This param
65 // controls how much CPU time the compositor will be prioritized for, its
66 // budget. Measured in ms.
67 constexpr base::FeatureParam<int> kInitialCompositorBudgetInMilliseconds{
68     &kVeryHighPriorityForCompositingBudget,
69     "InitialCompositorBudgetInMilliseconds", 250};
70 
71 // Param for kVeryHighPriorityForCompositingBudget experiment. This param
72 // controls the rate at which the budget is recovered.
73 constexpr base::FeatureParam<double> kCompositorBudgetRecoveryRate{
74     &kVeryHighPriorityForCompositingBudget, "CompositorBudgetRecoveryRate",
75     0.25};
76 
77 // This feature functions as an experiment parameter for the
78 // VeryHighPriorityForCompositing alternating, delay, and budget experiments.
79 // When enabled, it does nothing unless one of these experiments is also
80 // enabled. If one of these experiments is enabled it will change the behavior
81 // of that experiment such that the stop signal for prioritzation of the
82 // compositor is a BeginMainFrame task instead of any compositor task.
83 const base::Feature kPrioritizeCompositingUntilBeginMainFrame{
84     "BlinkSchedulerPrioritizeCompositingUntilBeginMainFrame",
85     base::FEATURE_ENABLED_BY_DEFAULT};
86 
87 // LOAD PRIORITY EXPERIMENT CONTROLS
88 
89 // Enables setting the priority of background (with no audio) pages'
90 // task queues to low priority.
91 const base::Feature kLowPriorityForBackgroundPages{
92     "BlinkSchedulerLowPriorityForBackgroundPages",
93     base::FEATURE_DISABLED_BY_DEFAULT};
94 
95 // Enables setting the priority of background (with no audio) pages'
96 // task queues to best effort.
97 const base::Feature kBestEffortPriorityForBackgroundPages{
98     "BlinkSchedulerBestEffortPriorityForBackgroundPages",
99     base::FEATURE_DISABLED_BY_DEFAULT};
100 
101 // Enables setting the priority of sub-frame task queues to low
102 // priority.
103 const base::Feature kLowPriorityForSubFrame{
104     "BlinkSchedulerLowPriorityForSubFrame", base::FEATURE_DISABLED_BY_DEFAULT};
105 
106 // Enables setting the priority of throttleable task queues to
107 // low priority.
108 const base::Feature kLowPriorityForThrottleableTask{
109     "BlinkSchedulerLowPriorityForThrottleableTask",
110     base::FEATURE_DISABLED_BY_DEFAULT};
111 
112 // Enables setting the priority of sub-frame throttleable
113 // task queues to low priority.
114 const base::Feature kLowPriorityForSubFrameThrottleableTask{
115     "BlinkSchedulerLowPriorityForSubFrameThrottleableTask",
116     base::FEATURE_DISABLED_BY_DEFAULT};
117 
118 // Enables setting the priority of hidden frame task queues to
119 // low priority.
120 const base::Feature kLowPriorityForHiddenFrame{
121     "BlinkSchedulerLowPriorityForHiddenFrame",
122     base::FEATURE_DISABLED_BY_DEFAULT};
123 
124 // Used along with |kLowPriorityForHiddenFrame|,
125 // |kLowPriorityForSubFrameThrottleableTask|, |kLowPriorityForThrottleableTask|,
126 // |kLowPriorityForSubFrame| to enable one of these experiments only during the
127 // load use case.
128 const base::Feature kFrameExperimentOnlyWhenLoading{
129     "BlinkSchedulerFrameExperimentOnlyWhenLoading",
130     base::FEATURE_DISABLED_BY_DEFAULT};
131 
132 // Enables setting the priority of an ad frame to low priority.
133 const base::Feature kLowPriorityForAdFrame{
134     "BlinkSchedulerLowPriorityForAdFrame", base::FEATURE_DISABLED_BY_DEFAULT};
135 
136 // Enables setting the priority of an ad frame to best effort priority.
137 const base::Feature kBestEffortPriorityForAdFrame{
138     "BlinkSchedulerBestEffortPriorityForAdFrame",
139     base::FEATURE_DISABLED_BY_DEFAULT};
140 
141 // Used along with |kLowPriorityForAdFrame| or |kBestEffortPriorityForAdFrame|
142 // to enable one of these experiments only during the load use case.
143 const base::Feature kAdFrameExperimentOnlyWhenLoading{
144     "BlinkSchedulerAdFrameExperimentOnlyWhenLoading",
145     base::FEATURE_DISABLED_BY_DEFAULT};
146 
147 // Enables using a resource's fetch priority to determine the priority of the
148 // resource's loading tasks posted to blink's scheduler.
149 const base::Feature kUseResourceFetchPriority{
150     "BlinkSchedulerResourceFetchPriority", base::FEATURE_DISABLED_BY_DEFAULT};
151 
152 // Enables using a resource's fetch priority to determine the priority of the
153 // resource's loading tasks posted to blink's scheduler only for resources
154 // requested during the loading phase.
155 const base::Feature kUseResourceFetchPriorityOnlyWhenLoading{
156     "BlinkSchedulerResourceFetchPriorityOnlyWhenLoading",
157     base::FEATURE_DISABLED_BY_DEFAULT};
158 
159 // Enables setting the priority of cross-origin task queues to
160 // low priority.
161 const base::Feature kLowPriorityForCrossOrigin{
162     "BlinkSchedulerLowPriorityForCrossOrigin",
163     base::FEATURE_DISABLED_BY_DEFAULT};
164 
165 // Enables setting the priority of cross-origin task queues to
166 // low priority during loading only.
167 const base::Feature kLowPriorityForCrossOriginOnlyWhenLoading{
168     "BlinkSchedulerLowPriorityForCrossOriginOnlyWhenLoading",
169     base::FEATURE_DISABLED_BY_DEFAULT};
170 
171 // Prioritizes loading and compositing tasks while loading.
172 const base::Feature kPrioritizeCompositingAndLoadingDuringEarlyLoading{
173     "PrioritizeCompositingAndLoadingDuringEarlyLoading",
174     base::FEATURE_DISABLED_BY_DEFAULT};
175 
176 // Prioritizes one BeginMainFrame after input.
177 const base::Feature kPrioritizeCompositingAfterInput{
178     "PrioritizeCompositingAfterInput", base::FEATURE_DISABLED_BY_DEFAULT};
179 
180 // Enable setting high priority database task type from field trial parameters.
181 const base::Feature kHighPriorityDatabaseTaskType{
182     "HighPriorityDatabaseTaskType", base::FEATURE_DISABLED_BY_DEFAULT};
183 
184 // When features::kIntensiveWakeUpThrottling is enabled, wake ups from timers
185 // with a high nesting level are limited to 1 per
186 // GetIntensiveWakeUpThrottlingDurationBetweenWakeUp() in a page that has been
187 // backgrounded for GetIntensiveWakeUpThrottlingGracePeriod(). If
188 // CanIntensivelyThrottleLowNestingLevel() is true, this policy is also applied
189 // to timers with a non-zero delay and a low nesting level.
190 //
191 // Intensive wake up throttling is enforced in addition to other throttling
192 // mechanisms:
193 //  - 1 wake up per second in a background page or hidden cross-origin frame
194 //  - 1% CPU time in a page that has been backgrounded for 10 seconds
195 //
196 // Feature tracking bug: https://crbug.com/1075553
197 //
198 // Note that features::kIntensiveWakeUpThrottling should not be read from;
199 // rather the provided accessors should be used, which also take into account
200 // the managed policy override of the feature.
201 //
202 // Parameter name and default values, exposed for testing.
203 constexpr int kIntensiveWakeUpThrottling_DurationBetweenWakeUpsSeconds_Default =
204     60;
205 constexpr const char*
206     kIntensiveWakeUpThrottling_DurationBetweenWakeUpsSeconds_Name =
207         "duration_between_wake_ups_seconds";
208 constexpr int kIntensiveWakeUpThrottling_GracePeriodSeconds_Default = 5 * 60;
209 constexpr const char*
210     kIntensiveWakeUpThrottling_CanIntensivelyThrottleLowNestingLevel_Name =
211         "can_intensively_throttle_low_nesting_level";
212 constexpr const bool
213     kIntensiveWakeUpThrottling_CanIntensivelyThrottleLowNestingLevel_Default =
214         false;
215 
216 // Exposed so that multiple tests can tinker with the policy override.
217 PLATFORM_EXPORT void
218 ClearIntensiveWakeUpThrottlingPolicyOverrideCacheForTesting();
219 // Determines if the feature is enabled, taking into account base::Feature
220 // settings and policy overrides.
221 PLATFORM_EXPORT bool IsIntensiveWakeUpThrottlingEnabled();
222 // Duration between wake ups for the kIntensiveWakeUpThrottling feature.
223 PLATFORM_EXPORT base::TimeDelta
224 GetIntensiveWakeUpThrottlingDurationBetweenWakeUps();
225 // Grace period after hiding a page during which there is no intensive wake up
226 // throttling for the kIntensiveWakeUpThrottling feature.
227 PLATFORM_EXPORT base::TimeDelta GetIntensiveWakeUpThrottlingGracePeriod();
228 // The duration for which intensive throttling should be inhibited for
229 // same-origin frames when the page title or favicon is updated. 0 seconds means
230 // that updating the title or favicon has no effect on intensive throttling.
231 PLATFORM_EXPORT base::TimeDelta
232 GetTimeToInhibitIntensiveThrottlingOnTitleOrFaviconUpdate();
233 // Whether timers with a non-zero delay and a low nesting level can be
234 // intensively throttled.
235 PLATFORM_EXPORT bool CanIntensivelyThrottleLowNestingLevel();
236 
237 // Per-agent scheduling experiments.
238 constexpr base::Feature kPerAgentSchedulingExperiments{
239     "BlinkSchedulerPerAgentSchedulingExperiments",
240     base::FEATURE_DISABLED_BY_DEFAULT};
241 
242 // Queues the per-agent scheduling experiment should affect.
243 enum class PerAgentAffectedQueues {
244   // Strategy only applies to non-main agent timer queues. These can be safely
245   // disabled/deprioritized without causing any known issues.
246   kTimerQueues,
247   // Strategy applies to all non-main agent queues. This may cause some task
248   // ordering issues.
249   kAllQueues,
250 };
251 
252 constexpr base::FeatureParam<PerAgentAffectedQueues>::Option
253     kPerAgentQueuesOptions[] = {
254         {PerAgentAffectedQueues::kTimerQueues, "timer-queues"},
255         {PerAgentAffectedQueues::kAllQueues, "all-queues"}};
256 
257 constexpr base::FeatureParam<PerAgentAffectedQueues> kPerAgentQueues{
258     &kPerAgentSchedulingExperiments, "queues",
259     PerAgentAffectedQueues::kTimerQueues, &kPerAgentQueuesOptions};
260 
261 // Effect the per-agent scheduling strategy should have.
262 enum class PerAgentSlowDownMethod {
263   // Affected queues will be disabled.
264   kDisable,
265   // Affected queues will have their priority reduced to |kBestEffortPriority|.
266   kBestEffort,
267 };
268 
269 constexpr base::FeatureParam<PerAgentSlowDownMethod>::Option
270     kPerAgentMethodOptions[] = {
271         {PerAgentSlowDownMethod::kDisable, "disable"},
272         {PerAgentSlowDownMethod::kBestEffort, "best-effort"}};
273 
274 constexpr base::FeatureParam<PerAgentSlowDownMethod> kPerAgentMethod{
275     &kPerAgentSchedulingExperiments, "method", PerAgentSlowDownMethod::kDisable,
276     &kPerAgentMethodOptions};
277 
278 // Delay to wait after the signal is reached, before "stopping" the strategy.
279 constexpr base::FeatureParam<int> kPerAgentDelayMs{
280     &kPerAgentSchedulingExperiments, "delay_ms", 0};
281 
282 // Signal the per-agent scheduling strategy should wait for.
283 enum class PerAgentSignal {
284   // Strategy will be active until all main frames reach First Meaningful Paint
285   // (+delay, if set).
286   kFirstMeaningfulPaint,
287   // Strategy will be active until all main frames finish loading (+delay, if
288   // set).
289   kOnLoad,
290   // Strategy will be active until the delay has passed since all main frames
291   // were created (or navigated).
292   kDelayOnly,
293 };
294 
295 constexpr base::FeatureParam<PerAgentSignal>::Option kPerAgentSignalOptions[] =
296     {{PerAgentSignal::kFirstMeaningfulPaint, "fmp"},
297      {PerAgentSignal::kOnLoad, "onload"},
298      {PerAgentSignal::kDelayOnly, "delay"}};
299 
300 constexpr base::FeatureParam<PerAgentSignal> kPerAgentSignal{
301     &kPerAgentSchedulingExperiments, "signal",
302     PerAgentSignal::kFirstMeaningfulPaint, &kPerAgentSignalOptions};
303 
304 }  // namespace scheduler
305 }  // namespace blink
306 
307 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_COMMON_FEATURES_H_
308