1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/system/sys_info.h"
6
7 #include <algorithm>
8
9 #include "base/base_switches.h"
10 #include "base/command_line.h"
11 #include "base/features.h"
12 #include "base/functional/bind.h"
13 #include "base/functional/callback.h"
14 #include "base/location.h"
15 #include "base/notreached.h"
16 #include "base/system/sys_info_internal.h"
17 #include "base/task/task_traits.h"
18 #include "base/task/thread_pool.h"
19 #include "base/time/time.h"
20 #include "build/build_config.h"
21
22 namespace base {
23 namespace {
24 #if BUILDFLAG(IS_IOS)
25 // For M99, 45% of devices have 2GB of RAM, and 55% have more.
26 constexpr uint64_t kLowMemoryDeviceThresholdMB = 1024;
27 #else
28 // Updated Desktop default threshold to match the Android 2021 definition.
29 constexpr uint64_t kLowMemoryDeviceThresholdMB = 2048;
30 #endif
31
32 std::optional<uint64_t> g_amount_of_physical_memory_mb_for_testing;
33 } // namespace
34
35 // static
NumberOfEfficientProcessors()36 int SysInfo::NumberOfEfficientProcessors() {
37 static int number_of_efficient_processors = NumberOfEfficientProcessorsImpl();
38 return number_of_efficient_processors;
39 }
40
41 // static
AmountOfPhysicalMemory()42 uint64_t SysInfo::AmountOfPhysicalMemory() {
43 constexpr uint64_t kMB = 1024 * 1024;
44
45 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
46 switches::kEnableLowEndDeviceMode)) {
47 // Keep using 512MB as the simulated RAM amount for when users or tests have
48 // manually enabled low-end device mode. Note this value is different from
49 // the threshold used for low end devices.
50 constexpr uint64_t kSimulatedMemoryForEnableLowEndDeviceMode = 512 * kMB;
51 return std::min(kSimulatedMemoryForEnableLowEndDeviceMode,
52 AmountOfPhysicalMemoryImpl());
53 }
54
55 if (g_amount_of_physical_memory_mb_for_testing) {
56 return g_amount_of_physical_memory_mb_for_testing.value() * kMB;
57 }
58
59 return AmountOfPhysicalMemoryImpl();
60 }
61
62 // static
AmountOfAvailablePhysicalMemory()63 uint64_t SysInfo::AmountOfAvailablePhysicalMemory() {
64 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
65 switches::kEnableLowEndDeviceMode)) {
66 // Estimate the available memory by subtracting our memory used estimate
67 // from the fake |kLowMemoryDeviceThresholdMB| limit.
68 uint64_t memory_used =
69 AmountOfPhysicalMemoryImpl() - AmountOfAvailablePhysicalMemoryImpl();
70 uint64_t memory_limit = kLowMemoryDeviceThresholdMB * 1024 * 1024;
71 // std::min ensures no underflow, as |memory_used| can be > |memory_limit|.
72 return memory_limit - std::min(memory_used, memory_limit);
73 }
74
75 return AmountOfAvailablePhysicalMemoryImpl();
76 }
77
IsLowEndDevice()78 bool SysInfo::IsLowEndDevice() {
79 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
80 switches::kEnableLowEndDeviceMode)) {
81 return true;
82 }
83
84 return IsLowEndDeviceImpl();
85 }
86
87 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
88
89 namespace {
90
91 enum class BucketizedSize {
92 k2GbOrLess,
93 k3Gb,
94 k4Gb,
95 k6Gb,
96 k8GbOrHigher,
97 };
98
GetSystemRamBucketizedSize()99 BucketizedSize GetSystemRamBucketizedSize() {
100 int physical_memory = base::SysInfo::AmountOfPhysicalMemoryMB();
101
102 // Because of Android carveouts, AmountOfPhysicalMemory() returns smaller
103 // than the actual memory size, So we will use a small lowerbound than "X"GB
104 // to discriminate real "X"GB devices from lower memory ones.
105 // Addendum: This logic should also work for ChromeOS.
106
107 constexpr int kUpperBound2GB = 2 * 1024; // inclusive
108 if (physical_memory <= kUpperBound2GB) {
109 return BucketizedSize::k2GbOrLess;
110 }
111
112 constexpr int kLowerBound3GB = kUpperBound2GB; // exclusive
113 constexpr int kUpperBound3GB = 3.2 * 1024; // inclusive
114 if (kLowerBound3GB < physical_memory && physical_memory <= kUpperBound3GB) {
115 return BucketizedSize::k3Gb;
116 }
117
118 constexpr int kLowerBound4GB = kUpperBound3GB; // exclusive
119 constexpr int kUpperBound4GB = 4 * 1024; // inclusive
120 if (kLowerBound4GB < physical_memory && physical_memory <= kUpperBound4GB) {
121 return BucketizedSize::k4Gb;
122 }
123
124 constexpr int kLowerBound6GB = kUpperBound4GB; // exclusive
125 constexpr int kUpperBound6GB = 6.5 * 1024 - 1; // inclusive
126 if (kLowerBound6GB < physical_memory && physical_memory <= kUpperBound6GB) {
127 return BucketizedSize::k6Gb;
128 }
129
130 return BucketizedSize::k8GbOrHigher;
131 }
132
GetCachedSystemRamBucketizedSize()133 BucketizedSize GetCachedSystemRamBucketizedSize() {
134 static BucketizedSize s_size = GetSystemRamBucketizedSize();
135 return s_size;
136 }
137
IsPartialLowEndModeOnMidRangeDevicesEnabled()138 bool IsPartialLowEndModeOnMidRangeDevicesEnabled() {
139 // TODO(crbug.com/1434873): make the feature not enable on 32-bit devices
140 // before launching or going to high Stable %.
141 return SysInfo::Is4GbOr6GbDevice() &&
142 base::FeatureList::IsEnabled(
143 features::kPartialLowEndModeOnMidRangeDevices);
144 }
145
IsPartialLowEndModeOn3GbDevicesEnabled()146 bool IsPartialLowEndModeOn3GbDevicesEnabled() {
147 return SysInfo::Is3GbDevice() &&
148 base::FeatureList::IsEnabled(features::kPartialLowEndModeOn3GbDevices);
149 }
150
151 } // namespace
152
Is3GbDevice()153 bool SysInfo::Is3GbDevice() {
154 return GetCachedSystemRamBucketizedSize() == BucketizedSize::k3Gb;
155 }
156
Is4GbDevice()157 bool SysInfo::Is4GbDevice() {
158 return GetCachedSystemRamBucketizedSize() == BucketizedSize::k4Gb;
159 }
160
Is4GbOr6GbDevice()161 bool SysInfo::Is4GbOr6GbDevice() {
162 return GetCachedSystemRamBucketizedSize() == BucketizedSize::k4Gb ||
163 GetCachedSystemRamBucketizedSize() == BucketizedSize::k6Gb;
164 }
165
Is6GbDevice()166 bool SysInfo::Is6GbDevice() {
167 return GetCachedSystemRamBucketizedSize() == BucketizedSize::k6Gb;
168 }
169
170 #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
171
172 // TODO(crbug.com/1434873): This method is for chromium native code.
173 // We need to update the java-side code, i.e.
174 // base/android/java/src/org/chromium/base/SysUtils.java,
175 // and to make the selected components in java to see this feature.
IsLowEndDeviceOrPartialLowEndModeEnabled()176 bool SysInfo::IsLowEndDeviceOrPartialLowEndModeEnabled() {
177 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
178 return base::SysInfo::IsLowEndDevice() ||
179 IsPartialLowEndModeOnMidRangeDevicesEnabled() ||
180 IsPartialLowEndModeOn3GbDevicesEnabled();
181 #else
182 return base::SysInfo::IsLowEndDevice();
183 #endif
184 }
185
IsLowEndDeviceOrPartialLowEndModeEnabled(const FeatureParam<bool> & param_for_exclusion)186 bool SysInfo::IsLowEndDeviceOrPartialLowEndModeEnabled(
187 const FeatureParam<bool>& param_for_exclusion) {
188 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
189 return base::SysInfo::IsLowEndDevice() ||
190 ((IsPartialLowEndModeOnMidRangeDevicesEnabled() ||
191 IsPartialLowEndModeOn3GbDevicesEnabled()) &&
192 !param_for_exclusion.Get());
193 #else
194 return base::SysInfo::IsLowEndDevice();
195 #endif
196 }
197
198 #if !BUILDFLAG(IS_ANDROID)
199 // The Android equivalent of this lives in `detectLowEndDevice()` at:
200 // base/android/java/src/org/chromium/base/SysUtils.java
DetectLowEndDevice()201 bool DetectLowEndDevice() {
202 CommandLine* command_line = CommandLine::ForCurrentProcess();
203 if (command_line->HasSwitch(switches::kEnableLowEndDeviceMode))
204 return true;
205 if (command_line->HasSwitch(switches::kDisableLowEndDeviceMode))
206 return false;
207
208 int ram_size_mb = SysInfo::AmountOfPhysicalMemoryMB();
209 return ram_size_mb > 0 &&
210 static_cast<uint64_t>(ram_size_mb) <= kLowMemoryDeviceThresholdMB;
211 }
212
213 // static
IsLowEndDeviceImpl()214 bool SysInfo::IsLowEndDeviceImpl() {
215 static internal::LazySysInfoValue<bool, DetectLowEndDevice> instance;
216 return instance.value();
217 }
218 #endif
219
220 #if !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_WIN) && \
221 !BUILDFLAG(IS_CHROMEOS)
HardwareModelName()222 std::string SysInfo::HardwareModelName() {
223 return std::string();
224 }
225 #endif
226
GetHardwareInfo(base::OnceCallback<void (HardwareInfo)> callback)227 void SysInfo::GetHardwareInfo(base::OnceCallback<void(HardwareInfo)> callback) {
228 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
229 constexpr base::TaskTraits kTraits = {base::MayBlock()};
230 #else
231 constexpr base::TaskTraits kTraits = {};
232 #endif
233
234 base::ThreadPool::PostTaskAndReplyWithResult(
235 FROM_HERE, kTraits, base::BindOnce(&GetHardwareInfoSync),
236 std::move(callback));
237 }
238
239 // static
Uptime()240 base::TimeDelta SysInfo::Uptime() {
241 // This code relies on an implementation detail of TimeTicks::Now() - that
242 // its return value happens to coincide with the system uptime value in
243 // microseconds, on Win/Mac/iOS/Linux/ChromeOS and Android.
244 int64_t uptime_in_microseconds = TimeTicks::Now().ToInternalValue();
245 return base::Microseconds(uptime_in_microseconds);
246 }
247
248 // static
ProcessCPUArchitecture()249 std::string SysInfo::ProcessCPUArchitecture() {
250 #if defined(ARCH_CPU_X86)
251 return "x86";
252 #elif defined(ARCH_CPU_X86_64)
253 return "x86_64";
254 #elif defined(ARCH_CPU_ARMEL)
255 return "ARM";
256 #elif defined(ARCH_CPU_ARM64)
257 return "ARM_64";
258 #elif defined(ARCH_CPU_RISCV64)
259 return "RISCV_64";
260 #else
261 return std::string();
262 #endif
263 }
264
265 // static
SetAmountOfPhysicalMemoryMbForTesting(const uint64_t amount_of_memory_mb)266 std::optional<uint64_t> SysInfo::SetAmountOfPhysicalMemoryMbForTesting(
267 const uint64_t amount_of_memory_mb) {
268 std::optional<uint64_t> current = g_amount_of_physical_memory_mb_for_testing;
269 g_amount_of_physical_memory_mb_for_testing.emplace(amount_of_memory_mb);
270 return current;
271 }
272
273 // static
ClearAmountOfPhysicalMemoryMbForTesting()274 void SysInfo::ClearAmountOfPhysicalMemoryMbForTesting() {
275 g_amount_of_physical_memory_mb_for_testing.reset();
276 }
277
278 } // namespace base
279