Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 1 | // Copyright (c) 2021 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 | |
Benoit Lize | 7ee77d3 | 2021-03-04 17:26:14 | [diff] [blame] | 5 | #include "content/common/partition_alloc_support.h" |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include "base/allocator/allocator_shim.h" |
| 10 | #include "base/allocator/buildflags.h" |
Bartek Nowierski | c9f1e7ef | 2021-06-28 14:09:30 | [diff] [blame] | 11 | #include "base/allocator/partition_allocator/partition_alloc_config.h" |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 12 | #include "base/allocator/partition_allocator/partition_alloc_features.h" |
Anton Bikineev | fd91bbc | 2021-03-21 12:35:49 | [diff] [blame] | 13 | #include "base/allocator/partition_allocator/starscan/pcscan.h" |
Michael Lippautz | d34f7b2c | 2021-04-21 08:32:26 | [diff] [blame] | 14 | #include "base/allocator/partition_allocator/starscan/pcscan_scheduling.h" |
Anton Bikineev | 8cf9e1e3 | 2021-04-26 23:52:38 | [diff] [blame] | 15 | #include "base/allocator/partition_allocator/starscan/stack/stack.h" |
Benoit Lize | 6ae0027 | 2021-02-25 15:56:53 | [diff] [blame] | 16 | #include "base/allocator/partition_allocator/thread_cache.h" |
Michael Lippautz | d34f7b2c | 2021-04-21 08:32:26 | [diff] [blame] | 17 | #include "base/bind.h" |
| 18 | #include "base/callback.h" |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 19 | #include "base/feature_list.h" |
Michael Lippautz | d34f7b2c | 2021-04-21 08:32:26 | [diff] [blame] | 20 | #include "base/no_destructor.h" |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 21 | #include "build/build_config.h" |
| 22 | #include "content/public/common/content_switches.h" |
| 23 | |
| 24 | #if defined(OS_ANDROID) |
| 25 | #include "base/system/sys_info.h" |
| 26 | #endif |
| 27 | |
Benoit Lize | 6d3d74ef | 2021-09-21 13:07:55 | [diff] [blame] | 28 | #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
| 29 | #include "base/allocator/partition_allocator/memory_reclaimer.h" |
| 30 | #include "base/threading/thread_task_runner_handle.h" |
| 31 | #endif |
| 32 | |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 33 | namespace content { |
| 34 | namespace internal { |
| 35 | |
| 36 | namespace { |
| 37 | |
Anton Bikineev | a39a995b | 2021-03-04 13:03:00 | [diff] [blame] | 38 | void SetProcessNameForPCScan(const std::string& process_type) { |
| 39 | const char* name = [&process_type] { |
| 40 | if (process_type.empty()) { |
| 41 | // Empty means browser process. |
| 42 | return "Browser"; |
| 43 | } |
| 44 | if (process_type == switches::kRendererProcess) |
| 45 | return "Renderer"; |
| 46 | if (process_type == switches::kGpuProcess) |
| 47 | return "Gpu"; |
| 48 | if (process_type == switches::kUtilityProcess) |
| 49 | return "Utility"; |
| 50 | return static_cast<const char*>(nullptr); |
| 51 | }(); |
| 52 | |
| 53 | if (name) { |
Anton Bikineev | 37a0e6c | 2021-04-28 08:51:42 | [diff] [blame] | 54 | base::internal::PCScan::SetProcessName(name); |
Anton Bikineev | a39a995b | 2021-03-04 13:03:00 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Anton Bikineev | ea8859b | 2021-04-29 19:17:22 | [diff] [blame] | 58 | bool EnablePCScanForMallocPartitionsIfNeeded() { |
Bartek Nowierski | bba1846 | 2021-06-02 02:34:47 | [diff] [blame] | 59 | #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && defined(PA_ALLOW_PCSCAN) |
Anton Bikineev | 2732527 | 2021-09-23 13:43:11 | [diff] [blame] | 60 | using Config = base::internal::PCScan::InitConfig; |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 61 | DCHECK(base::FeatureList::GetInstance()); |
| 62 | if (base::FeatureList::IsEnabled(base::features::kPartitionAllocPCScan)) { |
Anton Bikineev | 2732527 | 2021-09-23 13:43:11 | [diff] [blame] | 63 | base::allocator::EnablePCScan({Config::WantedWriteProtectionMode::kEnabled, |
| 64 | Config::SafepointMode::kEnabled}); |
Anton Bikineev | ea8859b | 2021-04-29 19:17:22 | [diff] [blame] | 65 | return true; |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 66 | } |
Bartek Nowierski | bba1846 | 2021-06-02 02:34:47 | [diff] [blame] | 67 | #endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && defined(PA_ALLOW_PCSCAN) |
Anton Bikineev | ea8859b | 2021-04-29 19:17:22 | [diff] [blame] | 68 | return false; |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 69 | } |
| 70 | |
Anton Bikineev | ea8859b | 2021-04-29 19:17:22 | [diff] [blame] | 71 | bool EnablePCScanForMallocPartitionsInBrowserProcessIfNeeded() { |
Bartek Nowierski | bba1846 | 2021-06-02 02:34:47 | [diff] [blame] | 72 | #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && defined(PA_ALLOW_PCSCAN) |
Anton Bikineev | 2732527 | 2021-09-23 13:43:11 | [diff] [blame] | 73 | using Config = base::internal::PCScan::InitConfig; |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 74 | DCHECK(base::FeatureList::GetInstance()); |
| 75 | if (base::FeatureList::IsEnabled( |
| 76 | base::features::kPartitionAllocPCScanBrowserOnly)) { |
Anton Bikineev | 2732527 | 2021-09-23 13:43:11 | [diff] [blame] | 77 | const Config::WantedWriteProtectionMode wp_mode = |
| 78 | base::FeatureList::IsEnabled(base::features::kPartitionAllocDCScan) |
| 79 | ? Config::WantedWriteProtectionMode::kEnabled |
| 80 | : Config::WantedWriteProtectionMode::kDisabled; |
Anton Bikineev | 6b5ab95 | 2021-05-19 23:57:57 | [diff] [blame] | 81 | #if !defined(PA_STARSCAN_UFFD_WRITE_PROTECTOR_SUPPORTED) |
Anton Bikineev | 2732527 | 2021-09-23 13:43:11 | [diff] [blame] | 82 | CHECK_EQ(Config::WantedWriteProtectionMode::kDisabled, wp_mode) |
Anton Bikineev | 6b5ab95 | 2021-05-19 23:57:57 | [diff] [blame] | 83 | << "DCScan is currently only supported on Linux based systems"; |
| 84 | #endif |
Anton Bikineev | 2732527 | 2021-09-23 13:43:11 | [diff] [blame] | 85 | base::allocator::EnablePCScan({wp_mode, Config::SafepointMode::kEnabled}); |
Anton Bikineev | ea8859b | 2021-04-29 19:17:22 | [diff] [blame] | 86 | return true; |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 87 | } |
Bartek Nowierski | bba1846 | 2021-06-02 02:34:47 | [diff] [blame] | 88 | #endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && defined(PA_ALLOW_PCSCAN) |
Anton Bikineev | ea8859b | 2021-04-29 19:17:22 | [diff] [blame] | 89 | return false; |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 90 | } |
| 91 | |
Anton Bikineev | d250933 | 2021-09-16 17:14:57 | [diff] [blame] | 92 | bool EnablePCScanForMallocPartitionsInRendererProcessIfNeeded() { |
| 93 | #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && defined(PA_ALLOW_PCSCAN) |
Anton Bikineev | 2732527 | 2021-09-23 13:43:11 | [diff] [blame] | 94 | using Config = base::internal::PCScan::InitConfig; |
Anton Bikineev | d250933 | 2021-09-16 17:14:57 | [diff] [blame] | 95 | DCHECK(base::FeatureList::GetInstance()); |
| 96 | if (base::FeatureList::IsEnabled( |
| 97 | base::features::kPartitionAllocPCScanRendererOnly)) { |
Anton Bikineev | 2732527 | 2021-09-23 13:43:11 | [diff] [blame] | 98 | const Config::WantedWriteProtectionMode wp_mode = |
| 99 | base::FeatureList::IsEnabled(base::features::kPartitionAllocDCScan) |
| 100 | ? Config::WantedWriteProtectionMode::kEnabled |
| 101 | : Config::WantedWriteProtectionMode::kDisabled; |
Anton Bikineev | d250933 | 2021-09-16 17:14:57 | [diff] [blame] | 102 | #if !defined(PA_STARSCAN_UFFD_WRITE_PROTECTOR_SUPPORTED) |
Anton Bikineev | 2732527 | 2021-09-23 13:43:11 | [diff] [blame] | 103 | CHECK_EQ(Config::WantedWriteProtectionMode::kDisabled, wp_mode) |
Anton Bikineev | d250933 | 2021-09-16 17:14:57 | [diff] [blame] | 104 | << "DCScan is currently only supported on Linux based systems"; |
| 105 | #endif |
Anton Bikineev | 2732527 | 2021-09-23 13:43:11 | [diff] [blame] | 106 | base::allocator::EnablePCScan({wp_mode, Config::SafepointMode::kDisabled}); |
Anton Bikineev | d250933 | 2021-09-16 17:14:57 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | #endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && defined(PA_ALLOW_PCSCAN) |
| 110 | return false; |
| 111 | } |
| 112 | |
Keishi Hattori | af499ac | 2021-09-22 23:45:04 | [diff] [blame] | 113 | } // namespace |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 114 | |
| 115 | void ReconfigurePartitionForKnownProcess(const std::string& process_type) { |
| 116 | DCHECK_NE(process_type, switches::kZygoteProcess); |
Keishi Hattori | af499ac | 2021-09-22 23:45:04 | [diff] [blame] | 117 | // TODO(keishi): Move the code to enable BRP back here after Finch |
| 118 | // experiments. |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 119 | } |
| 120 | |
Benoit Lize | 6ae0027 | 2021-02-25 15:56:53 | [diff] [blame] | 121 | PartitionAllocSupport::PartitionAllocSupport() = default; |
| 122 | |
Bartek Nowierski | 8c30bb9 | 2021-01-27 21:43:24 | [diff] [blame] | 123 | void PartitionAllocSupport::ReconfigureEarlyish( |
| 124 | const std::string& process_type) { |
| 125 | { |
| 126 | base::AutoLock scoped_lock(lock_); |
| 127 | // TODO(bartekn): Switch to DCHECK once confirmed there are no issues. |
| 128 | CHECK(!called_earlyish_) |
| 129 | << "ReconfigureEarlyish was already called for process '" |
| 130 | << established_process_type_ << "'; current process: '" << process_type |
| 131 | << "'"; |
| 132 | |
| 133 | called_earlyish_ = true; |
| 134 | established_process_type_ = process_type; |
| 135 | } |
| 136 | |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 137 | if (process_type != switches::kZygoteProcess) { |
| 138 | ReconfigurePartitionForKnownProcess(process_type); |
| 139 | } |
| 140 | |
| 141 | // These initializations are only relevant for PartitionAlloc-Everywhere |
| 142 | // builds. |
| 143 | #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 144 | base::allocator::EnablePartitionAllocMemoryReclaimer(); |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 145 | #endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
| 146 | } |
| 147 | |
Bartek Nowierski | 8c30bb9 | 2021-01-27 21:43:24 | [diff] [blame] | 148 | void PartitionAllocSupport::ReconfigureAfterZygoteFork( |
| 149 | const std::string& process_type) { |
| 150 | { |
| 151 | base::AutoLock scoped_lock(lock_); |
| 152 | // TODO(bartekn): Switch to DCHECK once confirmed there are no issues. |
| 153 | CHECK(!called_after_zygote_fork_) |
| 154 | << "ReconfigureAfterZygoteFork was already called for process '" |
| 155 | << established_process_type_ << "'; current process: '" << process_type |
| 156 | << "'"; |
| 157 | DCHECK(called_earlyish_) |
| 158 | << "Attempt to call ReconfigureAfterZygoteFork without calling " |
| 159 | "ReconfigureEarlyish; current process: '" |
| 160 | << process_type << "'"; |
| 161 | DCHECK_EQ(established_process_type_, switches::kZygoteProcess) |
| 162 | << "Attempt to call ReconfigureAfterZygoteFork while " |
| 163 | "ReconfigureEarlyish was called on non-zygote process '" |
| 164 | << established_process_type_ << "'; current process: '" << process_type |
| 165 | << "'"; |
| 166 | |
| 167 | called_after_zygote_fork_ = true; |
| 168 | established_process_type_ = process_type; |
| 169 | } |
| 170 | |
Bartek Nowierski | aacb40a | 2021-01-29 18:15:26 | [diff] [blame] | 171 | if (process_type != switches::kZygoteProcess) { |
| 172 | ReconfigurePartitionForKnownProcess(process_type); |
| 173 | } |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 174 | } |
| 175 | |
Bartek Nowierski | 8c30bb9 | 2021-01-27 21:43:24 | [diff] [blame] | 176 | void PartitionAllocSupport::ReconfigureAfterFeatureListInit( |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 177 | const std::string& process_type) { |
Bartek Nowierski | 8c30bb9 | 2021-01-27 21:43:24 | [diff] [blame] | 178 | { |
| 179 | base::AutoLock scoped_lock(lock_); |
| 180 | // Avoid initializing more than once. |
| 181 | // TODO(bartekn): See if can be converted to (D)CHECK. |
| 182 | if (called_after_feature_list_init_) { |
| 183 | DCHECK_EQ(established_process_type_, process_type) |
| 184 | << "ReconfigureAfterFeatureListInit was already called for process '" |
| 185 | << established_process_type_ << "'; current process: '" |
| 186 | << process_type << "'"; |
| 187 | return; |
| 188 | } |
| 189 | DCHECK(called_earlyish_) |
| 190 | << "Attempt to call ReconfigureAfterFeatureListInit without calling " |
| 191 | "ReconfigureEarlyish; current process: '" |
| 192 | << process_type << "'"; |
| 193 | DCHECK_NE(established_process_type_, switches::kZygoteProcess) |
| 194 | << "Attempt to call ReconfigureAfterFeatureListInit without calling " |
| 195 | "ReconfigureAfterZygoteFork; current process: '" |
| 196 | << process_type << "'"; |
| 197 | DCHECK_EQ(established_process_type_, process_type) |
| 198 | << "ReconfigureAfterFeatureListInit wasn't called for an already " |
| 199 | "established process '" |
| 200 | << established_process_type_ << "'; current process: '" << process_type |
| 201 | << "'"; |
| 202 | |
| 203 | called_after_feature_list_init_ = true; |
| 204 | } |
| 205 | |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 206 | DCHECK_NE(process_type, switches::kZygoteProcess); |
| 207 | // TODO(bartekn): Switch to DCHECK once confirmed there are no issues. |
| 208 | CHECK(base::FeatureList::GetInstance()); |
| 209 | |
Keishi Hattori | af499ac | 2021-09-22 23:45:04 | [diff] [blame] | 210 | bool enable_brp = false; |
Keishi Hattori | b6c84292 | 2021-09-22 23:48:58 | [diff] [blame] | 211 | #if BUILDFLAG(USE_BACKUP_REF_PTR) |
Keishi Hattori | af499ac | 2021-09-22 23:45:04 | [diff] [blame] | 212 | if (base::FeatureList::IsEnabled( |
| 213 | base::features::kPartitionAllocBackupRefPtr)) { |
| 214 | // No specified process type means this is the Browser process. |
| 215 | enable_brp = process_type.empty(); |
| 216 | if (base::features::kBackupRefPtrEnabledProcessesParam.Get() == |
| 217 | base::features::BackupRefPtrEnabledProcesses::kBrowserAndRenderer) { |
| 218 | enable_brp |= process_type == switches::kRendererProcess; |
| 219 | } |
| 220 | base::allocator::ConfigurePartitionBackupRefPtrSupport(enable_brp); |
| 221 | } |
| 222 | #endif |
| 223 | |
Bartek Nowierski | cd920ae1 | 2021-02-25 12:17:30 | [diff] [blame] | 224 | #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
| 225 | base::allocator::ReconfigurePartitionAllocLazyCommit(); |
Benoit Lize | 2dd8eec | 2021-03-05 08:58:33 | [diff] [blame] | 226 | #endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
Bartek Nowierski | cd920ae1 | 2021-02-25 12:17:30 | [diff] [blame] | 227 | |
Keishi Hattori | b6c84292 | 2021-09-22 23:48:58 | [diff] [blame] | 228 | // Don't enable PCScan if BRP is enabled. |
| 229 | if (enable_brp) |
| 230 | return; |
| 231 | |
Anton Bikineev | ea8859b | 2021-04-29 19:17:22 | [diff] [blame] | 232 | bool scan_enabled = EnablePCScanForMallocPartitionsIfNeeded(); |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 233 | // No specified process type means this is the Browser process. |
| 234 | if (process_type.empty()) { |
Anton Bikineev | ea8859b | 2021-04-29 19:17:22 | [diff] [blame] | 235 | scan_enabled = scan_enabled || |
| 236 | EnablePCScanForMallocPartitionsInBrowserProcessIfNeeded(); |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 237 | } |
Anton Bikineev | d250933 | 2021-09-16 17:14:57 | [diff] [blame] | 238 | if (process_type == switches::kRendererProcess) { |
| 239 | scan_enabled = scan_enabled || |
| 240 | EnablePCScanForMallocPartitionsInRendererProcessIfNeeded(); |
| 241 | } |
Anton Bikineev | ea8859b | 2021-04-29 19:17:22 | [diff] [blame] | 242 | if (scan_enabled) { |
| 243 | if (base::FeatureList::IsEnabled( |
| 244 | base::features::kPartitionAllocPCScanStackScanning)) { |
Nohemi Fernandez | 1386e84c | 2021-04-27 14:57:15 | [diff] [blame] | 245 | #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
Anton Bikineev | ea8859b | 2021-04-29 19:17:22 | [diff] [blame] | 246 | base::internal::PCScan::EnableStackScanning(); |
| 247 | // Notify PCScan about the main thread. |
| 248 | base::internal::PCScan::NotifyThreadCreated( |
| 249 | base::internal::GetStackTop()); |
| 250 | #endif |
| 251 | } |
Hannes Payer | a33df7f | 2021-06-22 18:44:14 | [diff] [blame] | 252 | if (base::FeatureList::IsEnabled( |
| 253 | base::features::kPartitionAllocPCScanImmediateFreeing)) { |
| 254 | base::internal::PCScan::EnableImmediateFreeing(); |
| 255 | } |
Anton Bikineev | 7f11c21 | 2021-08-24 12:03:09 | [diff] [blame] | 256 | if (base::FeatureList::IsEnabled( |
| 257 | base::features::kPartitionAllocPCScanEagerClearing)) { |
| 258 | base::internal::PCScan::SetClearType( |
| 259 | base::internal::PCScan::ClearType::kEager); |
| 260 | } |
Anton Bikineev | ea8859b | 2021-04-29 19:17:22 | [diff] [blame] | 261 | SetProcessNameForPCScan(process_type); |
| 262 | } |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 263 | } |
| 264 | |
Benoit Lize | 7ee77d3 | 2021-03-04 17:26:14 | [diff] [blame] | 265 | void PartitionAllocSupport::ReconfigureAfterTaskRunnerInit( |
Benoit Lize | 6ae0027 | 2021-02-25 15:56:53 | [diff] [blame] | 266 | const std::string& process_type) { |
| 267 | { |
| 268 | base::AutoLock scoped_lock(lock_); |
| 269 | |
| 270 | // Init only once. |
| 271 | if (called_after_thread_pool_init_) |
| 272 | return; |
| 273 | |
| 274 | DCHECK_EQ(established_process_type_, process_type); |
| 275 | // Enforce ordering. |
| 276 | DCHECK(called_earlyish_); |
| 277 | DCHECK(called_after_feature_list_init_); |
| 278 | |
| 279 | called_after_thread_pool_init_ = true; |
| 280 | } |
| 281 | |
Benoit Lize | 6ae0027 | 2021-02-25 15:56:53 | [diff] [blame] | 282 | #if defined(PA_THREAD_CACHE_SUPPORTED) && \ |
| 283 | BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
Benoit Lize | 7ee77d3 | 2021-03-04 17:26:14 | [diff] [blame] | 284 | // This should be called in specific processes, as the main thread is |
| 285 | // initialized later. |
| 286 | DCHECK(process_type != switches::kZygoteProcess); |
| 287 | |
Benoit Lize | 2066b73 | 2021-08-20 15:34:35 | [diff] [blame] | 288 | auto& registry = base::internal::ThreadCacheRegistry::Instance(); |
| 289 | registry.StartPeriodicPurge(); |
Benoit Lize | 2dd8eec | 2021-03-05 08:58:33 | [diff] [blame] | 290 | |
| 291 | #if defined(OS_ANDROID) |
Benoit Lize | 2066b73 | 2021-08-20 15:34:35 | [diff] [blame] | 292 | // Lower thread cache limits to avoid stranding too much memory in the caches. |
| 293 | if (base::SysInfo::IsLowEndDevice()) { |
| 294 | registry.SetThreadCacheMultiplier( |
| 295 | base::internal::ThreadCache::kDefaultMultiplier / 2.); |
Benoit Lize | 6ae0027 | 2021-02-25 15:56:53 | [diff] [blame] | 296 | } |
Benoit Lize | 2066b73 | 2021-08-20 15:34:35 | [diff] [blame] | 297 | #endif // defined(OS_ANDROID) |
Benoit Lize | 04f5dbb | 2021-03-25 19:21:30 | [diff] [blame] | 298 | |
| 299 | // Renderer processes are more performance-sensitive, increase thread cache |
| 300 | // limits. |
Benoit Lize | 006cfcb | 2021-03-29 16:18:52 | [diff] [blame] | 301 | if (process_type == switches::kRendererProcess && |
| 302 | base::FeatureList::IsEnabled( |
| 303 | base::features::kPartitionAllocLargeThreadCacheSize)) { |
Benoit Lize | 8e471f2f | 2021-03-30 13:29:10 | [diff] [blame] | 304 | #if defined(OS_ANDROID) && !defined(ARCH_CPU_64_BITS) |
| 305 | // Don't use a higher threshold on Android 32 bits, as long as memory usage |
| 306 | // is not carefully tuned. Only control the threshold here to avoid changing |
| 307 | // the rest of the code below. |
| 308 | // As of 2021, 64 bits Android devices are not memory constrained. |
Benoit Lize | 4443f05 | 2021-09-13 13:27:02 | [diff] [blame] | 309 | largest_cached_size_ = |
| 310 | base::internal::ThreadCacheLimits::kDefaultSizeThreshold; |
Benoit Lize | 8e471f2f | 2021-03-30 13:29:10 | [diff] [blame] | 311 | #else |
Benoit Lize | 4443f05 | 2021-09-13 13:27:02 | [diff] [blame] | 312 | largest_cached_size_ = |
| 313 | base::internal::ThreadCacheLimits::kLargeSizeThreshold; |
Benoit Lize | 04f5dbb | 2021-03-25 19:21:30 | [diff] [blame] | 314 | base::internal::ThreadCache::SetLargestCachedSize( |
Benoit Lize | 4443f05 | 2021-09-13 13:27:02 | [diff] [blame] | 315 | base::internal::ThreadCacheLimits::kLargeSizeThreshold); |
Benoit Lize | 8e471f2f | 2021-03-30 13:29:10 | [diff] [blame] | 316 | #endif // defined(OS_ANDROID) && !defined(ARCH_CPU_64_BITS) |
Benoit Lize | 04f5dbb | 2021-03-25 19:21:30 | [diff] [blame] | 317 | } |
| 318 | |
Benoit Lize | 6ae0027 | 2021-02-25 15:56:53 | [diff] [blame] | 319 | #endif // defined(PA_THREAD_CACHE_SUPPORTED) && |
| 320 | // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
Michael Lippautz | d34f7b2c | 2021-04-21 08:32:26 | [diff] [blame] | 321 | |
| 322 | if (base::FeatureList::IsEnabled( |
| 323 | base::features::kPartitionAllocPCScanMUAwareScheduler)) { |
| 324 | // Assign PCScan a task-based scheduling backend. |
| 325 | static base::NoDestructor<base::internal::MUAwareTaskBasedBackend> |
| 326 | mu_aware_task_based_backend{ |
Anton Bikineev | 37a0e6c | 2021-04-28 08:51:42 | [diff] [blame] | 327 | base::internal::PCScan::scheduler(), |
Michael Lippautz | d34f7b2c | 2021-04-21 08:32:26 | [diff] [blame] | 328 | base::BindRepeating([](base::TimeDelta delay) { |
Anton Bikineev | 37a0e6c | 2021-04-28 08:51:42 | [diff] [blame] | 329 | base::internal::PCScan::PerformDelayedScan(delay); |
Michael Lippautz | d34f7b2c | 2021-04-21 08:32:26 | [diff] [blame] | 330 | })}; |
Anton Bikineev | 37a0e6c | 2021-04-28 08:51:42 | [diff] [blame] | 331 | base::internal::PCScan::scheduler().SetNewSchedulingBackend( |
Michael Lippautz | d34f7b2c | 2021-04-21 08:32:26 | [diff] [blame] | 332 | *mu_aware_task_based_backend.get()); |
| 333 | } |
Benoit Lize | 6d3d74ef | 2021-09-21 13:07:55 | [diff] [blame] | 334 | |
| 335 | #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
| 336 | base::PartitionAllocMemoryReclaimer::Instance()->Start( |
| 337 | base::ThreadTaskRunnerHandle::Get()); |
| 338 | #endif |
Benoit Lize | 6ae0027 | 2021-02-25 15:56:53 | [diff] [blame] | 339 | } |
| 340 | |
Benoit Lize | 481c0c7 | 2021-03-30 10:09:50 | [diff] [blame] | 341 | void PartitionAllocSupport::OnForegrounded() { |
| 342 | #if defined(PA_THREAD_CACHE_SUPPORTED) && \ |
| 343 | BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
| 344 | { |
| 345 | base::AutoLock scoped_lock(lock_); |
| 346 | if (established_process_type_ != switches::kRendererProcess) |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | base::internal::ThreadCache::SetLargestCachedSize(largest_cached_size_); |
| 351 | #endif // defined(PA_THREAD_CACHE_SUPPORTED) && |
| 352 | // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
| 353 | } |
| 354 | |
| 355 | void PartitionAllocSupport::OnBackgrounded() { |
| 356 | #if defined(PA_THREAD_CACHE_SUPPORTED) && \ |
| 357 | BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
| 358 | { |
| 359 | base::AutoLock scoped_lock(lock_); |
| 360 | if (established_process_type_ != switches::kRendererProcess) |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | // Performance matters less for background renderers, don't pay the memory |
| 365 | // cost. |
| 366 | // |
| 367 | // TODO(lizeb): Consider forcing a one-off thread cache purge. |
| 368 | base::internal::ThreadCache::SetLargestCachedSize( |
Benoit Lize | 4443f05 | 2021-09-13 13:27:02 | [diff] [blame] | 369 | base::internal::ThreadCacheLimits::kDefaultSizeThreshold); |
Benoit Lize | 481c0c7 | 2021-03-30 10:09:50 | [diff] [blame] | 370 | #endif // defined(PA_THREAD_CACHE_SUPPORTED) && |
| 371 | // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
| 372 | } |
| 373 | |
Bartek Nowierski | 8daf56e | 2021-01-27 11:53:19 | [diff] [blame] | 374 | } // namespace internal |
Bartek Nowierski | aacb40a | 2021-01-29 18:15:26 | [diff] [blame] | 375 | } // namespace content |