Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: e0672c7bbefdf23a1fce146ebb913e246af59e61 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2022 The Chromium Authors
Clark DuVall9e982282022-05-04 22:12:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Clark DuVall9e982282022-05-04 22:12:125#include "base/features.h"
Francois Doray5e9ab02b2024-04-05 18:15:026
François Doray4824e312025-06-19 20:01:157#include <atomic>
8
François Doray1b049922025-09-29 18:47:339#include "base/containers/variant_map.h"
Xiyuan Xiae9dc9d42025-09-25 16:14:1310#include "base/debug/stack_trace.h"
François Doray74932f812025-04-14 02:58:3911#include "base/files/file_path.h"
Francois Doray5e9ab02b2024-04-05 18:15:0212#include "base/task/sequence_manager/sequence_manager_impl.h"
13#include "base/threading/platform_thread.h"
Dave Tapuskaabd48812025-02-13 20:58:0714#include "build/blink_buildflags.h"
Francois Doray5e9ab02b2024-04-05 18:15:0215#include "build/buildflag.h"
16
17#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
Olivier Li Shing Tat-Dupuisd69c84e2024-06-07 16:45:0418#include "base/message_loop/message_pump_epoll.h"
Francois Doray5e9ab02b2024-04-05 18:15:0219#endif
20
21#if BUILDFLAG(IS_APPLE)
Francois Doraye91825b2024-04-05 18:15:1322#include "base/files/file.h"
Francois Doray5e9ab02b2024-04-05 18:15:0223#include "base/message_loop/message_pump_apple.h"
Francois Doray5e9ab02b2024-04-05 18:15:0224#include "base/synchronization/condition_variable.h"
Dave Tapuskaabd48812025-02-13 20:58:0725
26#if !BUILDFLAG(IS_IOS) || !BUILDFLAG(USE_BLINK)
27#include "base/message_loop/message_pump_kqueue.h"
28#endif
29
Francois Doray5e9ab02b2024-04-05 18:15:0230#endif
31
32#if BUILDFLAG(IS_ANDROID)
33#include "base/android/input_hint_checker.h"
34#endif
35
36#if BUILDFLAG(IS_WIN)
Jie Jiang353ef9d12024-10-25 15:38:4537#include "base/task/sequence_manager/thread_controller_power_monitor.h"
Francois Doray5e9ab02b2024-04-05 18:15:0238#endif
Clark DuVall9e982282022-05-04 22:12:1239
40namespace base::features {
41
François Doray4824e312025-06-19 20:01:1542namespace {
43
44// An atomic is used because this can be queried racily by a thread checking if
45// an optimization is enabled and a thread initializing this from the
46// FeatureList. All operations use std::memory_order_relaxed because there are
47// no dependent memory operations.
48std::atomic_bool g_is_reduce_ppms_enabled{false};
49
50} // namespace
51
Clark DuVall9e982282022-05-04 22:12:1252// Alphabetical:
53
Jiahe Zhang02495c82025-09-09 16:55:0954// When enabled, the compositor threads (including GPU) will be boosted to
55// kInteractive when not in input or loading scenarios.
56BASE_FEATURE(kBoostCompositorThreadsPriorityWhenIdle,
Jiahe Zhang02495c82025-09-09 16:55:0957 FEATURE_DISABLED_BY_DEFAULT);
58
Takashi Toyoshima4fb09e52025-02-27 19:41:5859// Controls caching within BASE_FEATURE_PARAM(). This is feature-controlled
60// so that ScopedFeatureList can disable it to turn off caching.
Xiaohan Wang019c1152025-09-09 05:11:2061BASE_FEATURE(kFeatureParamWithCache, FEATURE_ENABLED_BY_DEFAULT);
Takashi Toyoshimaf031fbef2024-08-19 06:48:1562
François Doray74932f812025-04-14 02:58:3963// Whether a fast implementation of FilePath::IsParent is used. This feature
64// exists to ensure that the fast implementation can be disabled quickly if
65// issues are found with it.
Xiaohan Wang019c1152025-09-09 05:11:2066BASE_FEATURE(kFastFilePathIsParent, FEATURE_ENABLED_BY_DEFAULT);
François Doray74932f812025-04-14 02:58:3967
Yoshisato Yanagisawaaacf49512024-07-24 05:15:5268// Use non default low memory device threshold.
69// Value should be given via |LowMemoryDeviceThresholdMB|.
Sam Maier9899f5e2025-01-30 22:24:5570#if BUILDFLAG(IS_ANDROID)
71// LINT.IfChange
72#define LOW_MEMORY_DEVICE_THRESHOLD_MB 1024
73// LINT.ThenChange(//base/android/java/src/org/chromium/base/SysUtils.java)
74#elif BUILDFLAG(IS_IOS)
Yoshisato Yanagisawaaacf49512024-07-24 05:15:5275// For M99, 45% of devices have 2GB of RAM, and 55% have more.
76#define LOW_MEMORY_DEVICE_THRESHOLD_MB 1024
77#else
78// Updated Desktop default threshold to match the Android 2021 definition.
79#define LOW_MEMORY_DEVICE_THRESHOLD_MB 2048
80#endif
Xiaohan Wang019c1152025-09-09 05:11:2081BASE_FEATURE(kLowEndMemoryExperiment, FEATURE_DISABLED_BY_DEFAULT);
François Doray9cee8042025-08-15 19:02:5582BASE_FEATURE_PARAM(int,
Takashi Toyoshimacff0ce62025-01-17 14:32:1283 kLowMemoryDeviceThresholdMB,
84 &kLowEndMemoryExperiment,
85 "LowMemoryDeviceThresholdMB",
86 LOW_MEMORY_DEVICE_THRESHOLD_MB);
Yoshisato Yanagisawaaacf49512024-07-24 05:15:5287
Xiaohan Wang019c1152025-09-09 05:11:2088BASE_FEATURE(kReducePPMs, FEATURE_DISABLED_BY_DEFAULT);
Olivier Li Shing Tat-Dupuisbccdddd2025-06-12 16:45:3189
Joe Masonbc6dbfb922025-10-24 15:03:1790// Apply base::ScopedBestEffortExecutionFence to registered task queues as well
91// as the thread pool.
92BASE_FEATURE(kScopedBestEffortExecutionFenceForTaskQueue,
93 base::FEATURE_DISABLED_BY_DEFAULT);
94
Xiyuan Xiae9dc9d42025-09-25 16:14:1395// Whether to restrict the max gap between the frame pointer and the stack end
96// for stack scanning. If the gap is beyond the given gap threshold, the stack
97// end is treated as unreliable. Stack scanning stops when that happens.
98// This feature is only in effect when BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
99// is on and `TraceStackFramePointers` would run stack scanning. Default gap
100// threshold is an absurdly large 100MB.
101// The feature is enabled by default on ChromeOS where crashes caused by
102// unreliable stack end are found. See https://crbug.com/402542102
103BASE_FEATURE(kStackScanMaxFramePointerToStackEndGap,
104#if BUILDFLAG(IS_CHROMEOS)
105 FEATURE_ENABLED_BY_DEFAULT
106#else
107 FEATURE_DISABLED_BY_DEFAULT
108#endif
109);
110BASE_FEATURE_PARAM(int,
111 kStackScanMaxFramePointerToStackEndGapThresholdMB,
112 &kStackScanMaxFramePointerToStackEndGap,
113 "StackScanMaxFramePointerToStackEndGapThresholdMB",
114 100);
115
Erik Chen6968d882023-10-17 23:14:42116#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
Takashi Sakamotod8d1dbb02023-10-23 08:55:40117// Force to enable LowEndDeviceMode partially on Android 3Gb devices.
118// (see PartialLowEndModeOnMidRangeDevices below)
Xiaohan Wang019c1152025-09-09 05:11:20119BASE_FEATURE(kPartialLowEndModeOn3GbDevices, FEATURE_DISABLED_BY_DEFAULT);
Takashi Sakamotod8d1dbb02023-10-23 08:55:40120
Erik Chen6968d882023-10-17 23:14:42121// Used to enable LowEndDeviceMode partially on Android and ChromeOS mid-range
122// devices. Such devices aren't considered low-end, but we'd like experiment
123// with a subset of low-end features to see if we get a good memory vs.
124// performance tradeoff.
Takashi Sakamotoad4404ffa2023-04-25 06:23:27125//
Alison Galed965ba02024-04-26 21:50:54126// TODO(crbug.com/40264947): |#if| out 32-bit before launching or going to
Takashi Sakamotoad4404ffa2023-04-25 06:23:27127// high Stable %, because we will enable the feature only for <8GB 64-bit
128// devices, where we didn't ship yet. However, we first need a larger
129// population to collect data.
Xiaohan Wang019c1152025-09-09 05:11:20130BASE_FEATURE(kPartialLowEndModeOnMidRangeDevices,
Erik Chen6968d882023-10-17 23:14:42131#if BUILDFLAG(IS_ANDROID)
Francois Doray5e9ab02b2024-04-05 18:15:02132 FEATURE_ENABLED_BY_DEFAULT);
Erik Chen6968d882023-10-17 23:14:42133#elif BUILDFLAG(IS_CHROMEOS)
Francois Doray5e9ab02b2024-04-05 18:15:02134 FEATURE_DISABLED_BY_DEFAULT);
Erik Chen6968d882023-10-17 23:14:42135#endif
Takashi Sakamotobe870e42023-05-19 06:52:03136
Erik Chen6968d882023-10-17 23:14:42137#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
138
139#if BUILDFLAG(IS_ANDROID)
Shintaro Kawamura1ec1f492025-03-27 02:09:51140// Enable not perceptible binding without cpu priority boosting.
Shin Kawamuraf5671a522025-10-28 01:00:09141BASE_FEATURE(kBackgroundNotPerceptibleBinding, FEATURE_ENABLED_BY_DEFAULT);
Shintaro Kawamura1ec1f492025-03-27 02:09:51142
Shin Kawamura1fb5cc02025-09-18 01:04:57143// Whether to use effective binding state to manage child process bindings.
144// ChildProcessConnection will binds at most 2 service connections only,
145// the connection for the effective binding state and waived binding.
146BASE_FEATURE(kEffectiveBindingState, FEATURE_DISABLED_BY_DEFAULT);
147
elabadysayedb2575f92024-07-22 16:49:39148// If enabled, post registering PowerMonitor broadcast receiver to a background
149// thread,
Xiaohan Wang019c1152025-09-09 05:11:20150BASE_FEATURE(kPostPowerMonitorBroadcastReceiverInitToBackground,
elabadysayedc559967e2024-10-31 01:55:57151 FEATURE_ENABLED_BY_DEFAULT);
elabadysayed65d3c882024-07-30 18:36:20152// If enabled, getMyMemoryState IPC will be posted to background.
Xiaohan Wang019c1152025-09-09 05:11:20153BASE_FEATURE(kPostGetMyMemoryStateToBackground, FEATURE_ENABLED_BY_DEFAULT);
Shintaro Kawamura0bff6b52025-05-07 04:26:39154
Shin Kawamurab3737612025-09-22 02:14:23155// Use a single connection and rebindService() to manage the binding to a child
156// process service.
157BASE_FEATURE(kRebindingChildServiceConnectionController,
158 FEATURE_DISABLED_BY_DEFAULT);
159
Shin Kawamura356dc6e2025-10-23 08:20:32160// Use a batch API to rebind service connections.
161BASE_FEATURE(kRebindServiceBatchApi, FEATURE_DISABLED_BY_DEFAULT);
162
Shin Kawamuraad4d82d2025-09-26 00:50:06163// Use ChildServiceConnectionController.isUnbound() instead of isConnected() to
164// check the connection state in ChildProcessConnection.
165BASE_FEATURE(kUseIsUnboundCheck, FEATURE_DISABLED_BY_DEFAULT);
166
Shintaro Kawamura0bff6b52025-05-07 04:26:39167// Use shared service connection to rebind a service binding to update the LRU
168// in the ProcessList of OomAdjuster.
Xiaohan Wang019c1152025-09-09 05:11:20169BASE_FEATURE(kUseSharedRebindServiceConnection, FEATURE_ENABLED_BY_DEFAULT);
Ashley Newson579daef72025-09-09 21:01:32170
171// Use madvise MADV_WILLNEED to prefetch the native library. This replaces the
172// default mechanism of pre-reading the memory from a forked process.
173BASE_FEATURE(kLibraryPrefetcherMadvise, FEATURE_DISABLED_BY_DEFAULT);
174
175// If > 0, split the madvise range into chunks of this many bytes, rounded up to
176// a page size. The default of 1 therefore rounds to a whole page.
177BASE_FEATURE_PARAM(size_t,
178 kLibraryPrefetcherMadviseLength,
179 &kLibraryPrefetcherMadvise,
180 "length",
181 1);
182
183// Whether to fall back to the fork-and-read method if madvise is not supported.
184// Does not trigger fork-and-read if madvise failed during the actual prefetch.
185BASE_FEATURE_PARAM(bool,
186 kLibraryPrefetcherMadviseFallback,
187 &kLibraryPrefetcherMadvise,
188 "fallback",
189 true);
Takashi Sakamotoad4404ffa2023-04-25 06:23:27190#endif // BUILDFLAG(IS_ANDROID)
191
Alex Attarff9124b2025-10-08 13:56:57192// When enabled, GetTerminationStatus() returns
193// TERMINATION_STATUS_EVICTED_FOR_MEMORY for processes terminated due to commit
194// failures. Otherwise, it returns TERMINATION_STATUS_OOM.
195BASE_FEATURE(kUseTerminationStatusMemoryExhaustion,
196 FEATURE_DISABLED_BY_DEFAULT);
197
François Doray4824e312025-06-19 20:01:15198bool IsReducePPMsEnabled() {
199 return g_is_reduce_ppms_enabled.load(std::memory_order_relaxed);
200}
201
Gabriel Charette72dd8da2025-11-10 18:17:12202void Init() {
François Doray4824e312025-06-19 20:01:15203 g_is_reduce_ppms_enabled.store(FeatureList::IsEnabled(kReducePPMs),
204 std::memory_order_relaxed);
205
Francois Doray5e9ab02b2024-04-05 18:15:02206 sequence_manager::internal::SequenceManagerImpl::InitializeFeatures();
Gabriel Charette72dd8da2025-11-10 18:17:12207 sequence_manager::internal::ThreadController::InitializeFeatures();
Francois Doray5e9ab02b2024-04-05 18:15:02208
Xiyuan Xiae9dc9d42025-09-25 16:14:13209 debug::StackTrace::InitializeFeatures();
François Doray74932f812025-04-14 02:58:39210 FilePath::InitializeFeatures();
François Doray1b049922025-09-29 18:47:33211 InitializeVariantMapFeatures();
François Doray74932f812025-04-14 02:58:39212
Francois Doray5e9ab02b2024-04-05 18:15:02213#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
Olivier Li Shing Tat-Dupuisd69c84e2024-06-07 16:45:04214 MessagePumpEpoll::InitializeFeatures();
Francois Doray5e9ab02b2024-04-05 18:15:02215#endif
216
217#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_CHROMEOS)
218 PlatformThread::InitializeFeatures();
219#endif
220
221#if BUILDFLAG(IS_APPLE)
Francois Doray5e9ab02b2024-04-05 18:15:02222 MessagePumpCFRunLoopBase::InitializeFeatures();
Dave Tapuskaabd48812025-02-13 20:58:07223
224// Kqueue is not used for ios blink.
225#if !BUILDFLAG(IS_IOS) || !BUILDFLAG(USE_BLINK)
Francois Doray5e9ab02b2024-04-05 18:15:02226 MessagePumpKqueue::InitializeFeatures();
227#endif
228
Dave Tapuskaabd48812025-02-13 20:58:07229#endif
230
Francois Doray5e9ab02b2024-04-05 18:15:02231#if BUILDFLAG(IS_ANDROID)
232 android::InputHintChecker::InitializeFeatures();
233#endif
234
235#if BUILDFLAG(IS_WIN)
Jie Jiang353ef9d12024-10-25 15:38:45236 sequence_manager::internal::ThreadControllerPowerMonitor::
237 InitializeFeatures();
Francois Doray5e9ab02b2024-04-05 18:15:02238#endif
239}
240
Clark DuVall9e982282022-05-04 22:12:12241} // namespace base::features