| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
| Clark DuVall | 9e98228 | 2022-05-04 22:12:12 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| Clark DuVall | 9e98228 | 2022-05-04 22:12:12 | [diff] [blame] | 5 | #include "base/features.h" |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 6 | |
| François Doray | 4824e31 | 2025-06-19 20:01:15 | [diff] [blame] | 7 | #include <atomic> |
| 8 | |
| François Doray | 1b04992 | 2025-09-29 18:47:33 | [diff] [blame] | 9 | #include "base/containers/variant_map.h" |
| Xiyuan Xia | e9dc9d4 | 2025-09-25 16:14:13 | [diff] [blame] | 10 | #include "base/debug/stack_trace.h" |
| François Doray | 74932f81 | 2025-04-14 02:58:39 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 12 | #include "base/task/sequence_manager/sequence_manager_impl.h" |
| 13 | #include "base/threading/platform_thread.h" |
| Dave Tapuska | abd4881 | 2025-02-13 20:58:07 | [diff] [blame] | 14 | #include "build/blink_buildflags.h" |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 15 | #include "build/buildflag.h" |
| 16 | |
| 17 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) |
| Olivier Li Shing Tat-Dupuis | d69c84e | 2024-06-07 16:45:04 | [diff] [blame] | 18 | #include "base/message_loop/message_pump_epoll.h" |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 19 | #endif |
| 20 | |
| 21 | #if BUILDFLAG(IS_APPLE) |
| Francois Doray | e91825b | 2024-04-05 18:15:13 | [diff] [blame] | 22 | #include "base/files/file.h" |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 23 | #include "base/message_loop/message_pump_apple.h" |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 24 | #include "base/synchronization/condition_variable.h" |
| Dave Tapuska | abd4881 | 2025-02-13 20:58:07 | [diff] [blame] | 25 | |
| 26 | #if !BUILDFLAG(IS_IOS) || !BUILDFLAG(USE_BLINK) |
| 27 | #include "base/message_loop/message_pump_kqueue.h" |
| 28 | #endif |
| 29 | |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | #if BUILDFLAG(IS_ANDROID) |
| 33 | #include "base/android/input_hint_checker.h" |
| 34 | #endif |
| 35 | |
| 36 | #if BUILDFLAG(IS_WIN) |
| Jie Jiang | 353ef9d1 | 2024-10-25 15:38:45 | [diff] [blame] | 37 | #include "base/task/sequence_manager/thread_controller_power_monitor.h" |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 38 | #endif |
| Clark DuVall | 9e98228 | 2022-05-04 22:12:12 | [diff] [blame] | 39 | |
| 40 | namespace base::features { |
| 41 | |
| François Doray | 4824e31 | 2025-06-19 20:01:15 | [diff] [blame] | 42 | namespace { |
| 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. |
| 48 | std::atomic_bool g_is_reduce_ppms_enabled{false}; |
| 49 | |
| 50 | } // namespace |
| 51 | |
| Clark DuVall | 9e98228 | 2022-05-04 22:12:12 | [diff] [blame] | 52 | // Alphabetical: |
| 53 | |
| Jiahe Zhang | 02495c8 | 2025-09-09 16:55:09 | [diff] [blame] | 54 | // When enabled, the compositor threads (including GPU) will be boosted to |
| 55 | // kInteractive when not in input or loading scenarios. |
| 56 | BASE_FEATURE(kBoostCompositorThreadsPriorityWhenIdle, |
| Jiahe Zhang | 02495c8 | 2025-09-09 16:55:09 | [diff] [blame] | 57 | FEATURE_DISABLED_BY_DEFAULT); |
| 58 | |
| Takashi Toyoshima | 4fb09e5 | 2025-02-27 19:41:58 | [diff] [blame] | 59 | // Controls caching within BASE_FEATURE_PARAM(). This is feature-controlled |
| 60 | // so that ScopedFeatureList can disable it to turn off caching. |
| Xiaohan Wang | 019c115 | 2025-09-09 05:11:20 | [diff] [blame] | 61 | BASE_FEATURE(kFeatureParamWithCache, FEATURE_ENABLED_BY_DEFAULT); |
| Takashi Toyoshima | f031fbef | 2024-08-19 06:48:15 | [diff] [blame] | 62 | |
| François Doray | 74932f81 | 2025-04-14 02:58:39 | [diff] [blame] | 63 | // 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 Wang | 019c115 | 2025-09-09 05:11:20 | [diff] [blame] | 66 | BASE_FEATURE(kFastFilePathIsParent, FEATURE_ENABLED_BY_DEFAULT); |
| François Doray | 74932f81 | 2025-04-14 02:58:39 | [diff] [blame] | 67 | |
| Yoshisato Yanagisawa | aacf4951 | 2024-07-24 05:15:52 | [diff] [blame] | 68 | // Use non default low memory device threshold. |
| 69 | // Value should be given via |LowMemoryDeviceThresholdMB|. |
| Sam Maier | 9899f5e | 2025-01-30 22:24:55 | [diff] [blame] | 70 | #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 Yanagisawa | aacf4951 | 2024-07-24 05:15:52 | [diff] [blame] | 75 | // 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 Wang | 019c115 | 2025-09-09 05:11:20 | [diff] [blame] | 81 | BASE_FEATURE(kLowEndMemoryExperiment, FEATURE_DISABLED_BY_DEFAULT); |
| François Doray | 9cee804 | 2025-08-15 19:02:55 | [diff] [blame] | 82 | BASE_FEATURE_PARAM(int, |
| Takashi Toyoshima | cff0ce6 | 2025-01-17 14:32:12 | [diff] [blame] | 83 | kLowMemoryDeviceThresholdMB, |
| 84 | &kLowEndMemoryExperiment, |
| 85 | "LowMemoryDeviceThresholdMB", |
| 86 | LOW_MEMORY_DEVICE_THRESHOLD_MB); |
| Yoshisato Yanagisawa | aacf4951 | 2024-07-24 05:15:52 | [diff] [blame] | 87 | |
| Xiaohan Wang | 019c115 | 2025-09-09 05:11:20 | [diff] [blame] | 88 | BASE_FEATURE(kReducePPMs, FEATURE_DISABLED_BY_DEFAULT); |
| Olivier Li Shing Tat-Dupuis | bccdddd | 2025-06-12 16:45:31 | [diff] [blame] | 89 | |
| Joe Mason | bc6dbfb92 | 2025-10-24 15:03:17 | [diff] [blame] | 90 | // Apply base::ScopedBestEffortExecutionFence to registered task queues as well |
| 91 | // as the thread pool. |
| 92 | BASE_FEATURE(kScopedBestEffortExecutionFenceForTaskQueue, |
| 93 | base::FEATURE_DISABLED_BY_DEFAULT); |
| 94 | |
| Xiyuan Xia | e9dc9d4 | 2025-09-25 16:14:13 | [diff] [blame] | 95 | // 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 |
| 103 | BASE_FEATURE(kStackScanMaxFramePointerToStackEndGap, |
| 104 | #if BUILDFLAG(IS_CHROMEOS) |
| 105 | FEATURE_ENABLED_BY_DEFAULT |
| 106 | #else |
| 107 | FEATURE_DISABLED_BY_DEFAULT |
| 108 | #endif |
| 109 | ); |
| 110 | BASE_FEATURE_PARAM(int, |
| 111 | kStackScanMaxFramePointerToStackEndGapThresholdMB, |
| 112 | &kStackScanMaxFramePointerToStackEndGap, |
| 113 | "StackScanMaxFramePointerToStackEndGapThresholdMB", |
| 114 | 100); |
| 115 | |
| Erik Chen | 6968d88 | 2023-10-17 23:14:42 | [diff] [blame] | 116 | #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) |
| Takashi Sakamoto | d8d1dbb0 | 2023-10-23 08:55:40 | [diff] [blame] | 117 | // Force to enable LowEndDeviceMode partially on Android 3Gb devices. |
| 118 | // (see PartialLowEndModeOnMidRangeDevices below) |
| Xiaohan Wang | 019c115 | 2025-09-09 05:11:20 | [diff] [blame] | 119 | BASE_FEATURE(kPartialLowEndModeOn3GbDevices, FEATURE_DISABLED_BY_DEFAULT); |
| Takashi Sakamoto | d8d1dbb0 | 2023-10-23 08:55:40 | [diff] [blame] | 120 | |
| Erik Chen | 6968d88 | 2023-10-17 23:14:42 | [diff] [blame] | 121 | // 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 Sakamoto | ad4404ffa | 2023-04-25 06:23:27 | [diff] [blame] | 125 | // |
| Alison Gale | d965ba0 | 2024-04-26 21:50:54 | [diff] [blame] | 126 | // TODO(crbug.com/40264947): |#if| out 32-bit before launching or going to |
| Takashi Sakamoto | ad4404ffa | 2023-04-25 06:23:27 | [diff] [blame] | 127 | // 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 Wang | 019c115 | 2025-09-09 05:11:20 | [diff] [blame] | 130 | BASE_FEATURE(kPartialLowEndModeOnMidRangeDevices, |
| Erik Chen | 6968d88 | 2023-10-17 23:14:42 | [diff] [blame] | 131 | #if BUILDFLAG(IS_ANDROID) |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 132 | FEATURE_ENABLED_BY_DEFAULT); |
| Erik Chen | 6968d88 | 2023-10-17 23:14:42 | [diff] [blame] | 133 | #elif BUILDFLAG(IS_CHROMEOS) |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 134 | FEATURE_DISABLED_BY_DEFAULT); |
| Erik Chen | 6968d88 | 2023-10-17 23:14:42 | [diff] [blame] | 135 | #endif |
| Takashi Sakamoto | be870e4 | 2023-05-19 06:52:03 | [diff] [blame] | 136 | |
| Erik Chen | 6968d88 | 2023-10-17 23:14:42 | [diff] [blame] | 137 | #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) |
| 138 | |
| 139 | #if BUILDFLAG(IS_ANDROID) |
| Shintaro Kawamura | 1ec1f49 | 2025-03-27 02:09:51 | [diff] [blame] | 140 | // Enable not perceptible binding without cpu priority boosting. |
| Shin Kawamura | f5671a52 | 2025-10-28 01:00:09 | [diff] [blame] | 141 | BASE_FEATURE(kBackgroundNotPerceptibleBinding, FEATURE_ENABLED_BY_DEFAULT); |
| Shintaro Kawamura | 1ec1f49 | 2025-03-27 02:09:51 | [diff] [blame] | 142 | |
| Shin Kawamura | 1fb5cc0 | 2025-09-18 01:04:57 | [diff] [blame] | 143 | // 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. |
| 146 | BASE_FEATURE(kEffectiveBindingState, FEATURE_DISABLED_BY_DEFAULT); |
| 147 | |
| elabadysayed | b2575f9 | 2024-07-22 16:49:39 | [diff] [blame] | 148 | // If enabled, post registering PowerMonitor broadcast receiver to a background |
| 149 | // thread, |
| Xiaohan Wang | 019c115 | 2025-09-09 05:11:20 | [diff] [blame] | 150 | BASE_FEATURE(kPostPowerMonitorBroadcastReceiverInitToBackground, |
| elabadysayed | c559967e | 2024-10-31 01:55:57 | [diff] [blame] | 151 | FEATURE_ENABLED_BY_DEFAULT); |
| elabadysayed | 65d3c88 | 2024-07-30 18:36:20 | [diff] [blame] | 152 | // If enabled, getMyMemoryState IPC will be posted to background. |
| Xiaohan Wang | 019c115 | 2025-09-09 05:11:20 | [diff] [blame] | 153 | BASE_FEATURE(kPostGetMyMemoryStateToBackground, FEATURE_ENABLED_BY_DEFAULT); |
| Shintaro Kawamura | 0bff6b5 | 2025-05-07 04:26:39 | [diff] [blame] | 154 | |
| Shin Kawamura | b373761 | 2025-09-22 02:14:23 | [diff] [blame] | 155 | // Use a single connection and rebindService() to manage the binding to a child |
| 156 | // process service. |
| 157 | BASE_FEATURE(kRebindingChildServiceConnectionController, |
| 158 | FEATURE_DISABLED_BY_DEFAULT); |
| 159 | |
| Shin Kawamura | 356dc6e | 2025-10-23 08:20:32 | [diff] [blame] | 160 | // Use a batch API to rebind service connections. |
| 161 | BASE_FEATURE(kRebindServiceBatchApi, FEATURE_DISABLED_BY_DEFAULT); |
| 162 | |
| Shin Kawamura | ad4d82d | 2025-09-26 00:50:06 | [diff] [blame] | 163 | // Use ChildServiceConnectionController.isUnbound() instead of isConnected() to |
| 164 | // check the connection state in ChildProcessConnection. |
| 165 | BASE_FEATURE(kUseIsUnboundCheck, FEATURE_DISABLED_BY_DEFAULT); |
| 166 | |
| Shintaro Kawamura | 0bff6b5 | 2025-05-07 04:26:39 | [diff] [blame] | 167 | // Use shared service connection to rebind a service binding to update the LRU |
| 168 | // in the ProcessList of OomAdjuster. |
| Xiaohan Wang | 019c115 | 2025-09-09 05:11:20 | [diff] [blame] | 169 | BASE_FEATURE(kUseSharedRebindServiceConnection, FEATURE_ENABLED_BY_DEFAULT); |
| Ashley Newson | 579daef7 | 2025-09-09 21:01:32 | [diff] [blame] | 170 | |
| 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. |
| 173 | BASE_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. |
| 177 | BASE_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. |
| 185 | BASE_FEATURE_PARAM(bool, |
| 186 | kLibraryPrefetcherMadviseFallback, |
| 187 | &kLibraryPrefetcherMadvise, |
| 188 | "fallback", |
| 189 | true); |
| Takashi Sakamoto | ad4404ffa | 2023-04-25 06:23:27 | [diff] [blame] | 190 | #endif // BUILDFLAG(IS_ANDROID) |
| 191 | |
| Alex Attar | ff9124b | 2025-10-08 13:56:57 | [diff] [blame] | 192 | // When enabled, GetTerminationStatus() returns |
| 193 | // TERMINATION_STATUS_EVICTED_FOR_MEMORY for processes terminated due to commit |
| 194 | // failures. Otherwise, it returns TERMINATION_STATUS_OOM. |
| 195 | BASE_FEATURE(kUseTerminationStatusMemoryExhaustion, |
| 196 | FEATURE_DISABLED_BY_DEFAULT); |
| 197 | |
| François Doray | 4824e31 | 2025-06-19 20:01:15 | [diff] [blame] | 198 | bool IsReducePPMsEnabled() { |
| 199 | return g_is_reduce_ppms_enabled.load(std::memory_order_relaxed); |
| 200 | } |
| 201 | |
| Gabriel Charette | 72dd8da | 2025-11-10 18:17:12 | [diff] [blame] | 202 | void Init() { |
| François Doray | 4824e31 | 2025-06-19 20:01:15 | [diff] [blame] | 203 | g_is_reduce_ppms_enabled.store(FeatureList::IsEnabled(kReducePPMs), |
| 204 | std::memory_order_relaxed); |
| 205 | |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 206 | sequence_manager::internal::SequenceManagerImpl::InitializeFeatures(); |
| Gabriel Charette | 72dd8da | 2025-11-10 18:17:12 | [diff] [blame] | 207 | sequence_manager::internal::ThreadController::InitializeFeatures(); |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 208 | |
| Xiyuan Xia | e9dc9d4 | 2025-09-25 16:14:13 | [diff] [blame] | 209 | debug::StackTrace::InitializeFeatures(); |
| François Doray | 74932f81 | 2025-04-14 02:58:39 | [diff] [blame] | 210 | FilePath::InitializeFeatures(); |
| François Doray | 1b04992 | 2025-09-29 18:47:33 | [diff] [blame] | 211 | InitializeVariantMapFeatures(); |
| François Doray | 74932f81 | 2025-04-14 02:58:39 | [diff] [blame] | 212 | |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 213 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) |
| Olivier Li Shing Tat-Dupuis | d69c84e | 2024-06-07 16:45:04 | [diff] [blame] | 214 | MessagePumpEpoll::InitializeFeatures(); |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 215 | #endif |
| 216 | |
| 217 | #if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_CHROMEOS) |
| 218 | PlatformThread::InitializeFeatures(); |
| 219 | #endif |
| 220 | |
| 221 | #if BUILDFLAG(IS_APPLE) |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 222 | MessagePumpCFRunLoopBase::InitializeFeatures(); |
| Dave Tapuska | abd4881 | 2025-02-13 20:58:07 | [diff] [blame] | 223 | |
| 224 | // Kqueue is not used for ios blink. |
| 225 | #if !BUILDFLAG(IS_IOS) || !BUILDFLAG(USE_BLINK) |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 226 | MessagePumpKqueue::InitializeFeatures(); |
| 227 | #endif |
| 228 | |
| Dave Tapuska | abd4881 | 2025-02-13 20:58:07 | [diff] [blame] | 229 | #endif |
| 230 | |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 231 | #if BUILDFLAG(IS_ANDROID) |
| 232 | android::InputHintChecker::InitializeFeatures(); |
| 233 | #endif |
| 234 | |
| 235 | #if BUILDFLAG(IS_WIN) |
| Jie Jiang | 353ef9d1 | 2024-10-25 15:38:45 | [diff] [blame] | 236 | sequence_manager::internal::ThreadControllerPowerMonitor:: |
| 237 | InitializeFeatures(); |
| Francois Doray | 5e9ab02b | 2024-04-05 18:15:02 | [diff] [blame] | 238 | #endif |
| 239 | } |
| 240 | |
| Clark DuVall | 9e98228 | 2022-05-04 22:12:12 | [diff] [blame] | 241 | } // namespace base::features |