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