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 } // namespace
32
33 // static
NumberOfEfficientProcessors()34 int SysInfo::NumberOfEfficientProcessors() {
35 static int number_of_efficient_processors = NumberOfEfficientProcessorsImpl();
36 return number_of_efficient_processors;
37 }
38
39 // static
AmountOfPhysicalMemory()40 uint64_t SysInfo::AmountOfPhysicalMemory() {
41 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
42 switches::kEnableLowEndDeviceMode)) {
43 // Keep using 512MB as the simulated RAM amount for when users or tests have
44 // manually enabled low-end device mode. Note this value is different from
45 // the threshold used for low end devices.
46 constexpr uint64_t kSimulatedMemoryForEnableLowEndDeviceMode =
47 512 * 1024 * 1024;
48 return std::min(kSimulatedMemoryForEnableLowEndDeviceMode,
49 AmountOfPhysicalMemoryImpl());
50 }
51
52 return AmountOfPhysicalMemoryImpl();
53 }
54
55 // static
AmountOfAvailablePhysicalMemory()56 uint64_t SysInfo::AmountOfAvailablePhysicalMemory() {
57 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
58 switches::kEnableLowEndDeviceMode)) {
59 // Estimate the available memory by subtracting our memory used estimate
60 // from the fake |kLowMemoryDeviceThresholdMB| limit.
61 uint64_t memory_used =
62 AmountOfPhysicalMemoryImpl() - AmountOfAvailablePhysicalMemoryImpl();
63 uint64_t memory_limit = kLowMemoryDeviceThresholdMB * 1024 * 1024;
64 // std::min ensures no underflow, as |memory_used| can be > |memory_limit|.
65 return memory_limit - std::min(memory_used, memory_limit);
66 }
67
68 return AmountOfAvailablePhysicalMemoryImpl();
69 }
70
IsLowEndDevice()71 bool SysInfo::IsLowEndDevice() {
72 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
73 switches::kEnableLowEndDeviceMode)) {
74 return true;
75 }
76
77 return IsLowEndDeviceImpl();
78 }
79
80 #if BUILDFLAG(IS_ANDROID)
81
82 namespace {
83
IsAndroid4GbOr6GbDevice()84 bool IsAndroid4GbOr6GbDevice() {
85 // Because of Android carveouts, AmountOfPhysicalMemory() returns smaller
86 // than the actual memory size, So we will use a small lowerbound than 4GB
87 // to discriminate real 4GB devices from lower memory ones.
88 constexpr int kLowerBoundMB = 3.2 * 1024;
89 constexpr int kUpperBoundMB = 6 * 1024;
90 static bool is_4gb_or_6g_device =
91 kLowerBoundMB <= base::SysInfo::AmountOfPhysicalMemoryMB() &&
92 base::SysInfo::AmountOfPhysicalMemoryMB() <= kUpperBoundMB;
93 return is_4gb_or_6g_device;
94 }
95
IsPartialLowEndModeOnMidRangeDevicesEnabled()96 bool IsPartialLowEndModeOnMidRangeDevicesEnabled() {
97 // TODO(crbug.com/1434873): make the feature not enable on 32-bit devices
98 // before launching or going to high Stable %.
99 return IsAndroid4GbOr6GbDevice() &&
100 base::FeatureList::IsEnabled(
101 features::kPartialLowEndModeOnMidRangeDevices);
102 }
103
104 } // namespace
105
106 #endif // BUILDFLAG(IS_ANDROID)
107
108 // TODO(crbug.com/1434873): This method is for chromium native code.
109 // We need to update the java-side code, i.e.
110 // base/android/java/src/org/chromium/base/SysUtils.java,
111 // and to make the selected components in java to see this feature.
IsLowEndDeviceOrPartialLowEndModeEnabled()112 bool SysInfo::IsLowEndDeviceOrPartialLowEndModeEnabled() {
113 #if BUILDFLAG(IS_ANDROID)
114 return base::SysInfo::IsLowEndDevice() ||
115 IsPartialLowEndModeOnMidRangeDevicesEnabled();
116 #else
117 return base::SysInfo::IsLowEndDevice();
118 #endif
119 }
120
121 #if !BUILDFLAG(IS_ANDROID)
122 // The Android equivalent of this lives in `detectLowEndDevice()` at:
123 // base/android/java/src/org/chromium/base/SysUtils.java
DetectLowEndDevice()124 bool DetectLowEndDevice() {
125 CommandLine* command_line = CommandLine::ForCurrentProcess();
126 if (command_line->HasSwitch(switches::kEnableLowEndDeviceMode))
127 return true;
128 if (command_line->HasSwitch(switches::kDisableLowEndDeviceMode))
129 return false;
130
131 int ram_size_mb = SysInfo::AmountOfPhysicalMemoryMB();
132 return ram_size_mb > 0 &&
133 static_cast<uint64_t>(ram_size_mb) <= kLowMemoryDeviceThresholdMB;
134 }
135
136 // static
IsLowEndDeviceImpl()137 bool SysInfo::IsLowEndDeviceImpl() {
138 static internal::LazySysInfoValue<bool, DetectLowEndDevice> instance;
139 return instance.value();
140 }
141 #endif
142
143 #if !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_WIN) && \
144 !BUILDFLAG(IS_CHROMEOS)
HardwareModelName()145 std::string SysInfo::HardwareModelName() {
146 return std::string();
147 }
148 #endif
149
GetHardwareInfo(base::OnceCallback<void (HardwareInfo)> callback)150 void SysInfo::GetHardwareInfo(base::OnceCallback<void(HardwareInfo)> callback) {
151 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
152 constexpr base::TaskTraits kTraits = {base::MayBlock()};
153 #else
154 constexpr base::TaskTraits kTraits = {};
155 #endif
156
157 base::ThreadPool::PostTaskAndReplyWithResult(
158 FROM_HERE, kTraits, base::BindOnce(&GetHardwareInfoSync),
159 std::move(callback));
160 }
161
162 // static
Uptime()163 base::TimeDelta SysInfo::Uptime() {
164 // This code relies on an implementation detail of TimeTicks::Now() - that
165 // its return value happens to coincide with the system uptime value in
166 // microseconds, on Win/Mac/iOS/Linux/ChromeOS and Android.
167 int64_t uptime_in_microseconds = TimeTicks::Now().ToInternalValue();
168 return base::Microseconds(uptime_in_microseconds);
169 }
170
171 // static
ProcessCPUArchitecture()172 std::string SysInfo::ProcessCPUArchitecture() {
173 #if defined(ARCH_CPU_X86)
174 return "x86";
175 #elif defined(ARCH_CPU_X86_64)
176 return "x86_64";
177 #elif defined(ARCH_CPU_ARMEL)
178 return "ARM";
179 #elif defined(ARCH_CPU_ARM64)
180 return "ARM_64";
181 #else
182 return std::string();
183 #endif
184 }
185
186 } // namespace base
187