| 6 |
| 7 | #include <algorithm> |
| 8 |
| 9 | #include "base/base_switches.h" |
| 10 | #include "base/bind.h" |
| 11 | #include "base/callback.h" |
| 12 | #include "base/command_line.h" |
| 13 | #include "base/location.h" |
| 14 | #include "base/logging.h" |
| 15 | #include "base/no_destructor.h" |
| 16 | #include "base/system/sys_info_internal.h" |
| 17 | #include "base/task/post_task.h" |
| 18 | #include "base/task/task_traits.h" |
| 19 | #include "base/task/thread_pool.h" |
| 20 | #include "base/task_runner_util.h" |
| 21 | #include "base/time/time.h" |
| 22 | #include "build/build_config.h" |
| 23 |
| 24 | namespace base { |
| 25 | namespace { |
| 26 | static const int kLowMemoryDeviceThresholdMB = 512; |
| 27 | } // namespace |
| 28 |
| 29 | // static |
| 30 | int64_t SysInfo::AmountOfPhysicalMemory() { |
| 31 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 32 | switches::kEnableLowEndDeviceMode)) { |
| 33 | return kLowMemoryDeviceThresholdMB * 1024 * 1024; |
| 34 | } |
| 35 |
| 36 | return AmountOfPhysicalMemoryImpl(); |
| 37 | } |
| 38 |
| 39 | // static |
| 40 | int64_t SysInfo::AmountOfAvailablePhysicalMemory() { |
| 41 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 42 | switches::kEnableLowEndDeviceMode)) { |
| 43 | // Estimate the available memory by subtracting our memory used estimate |
| 44 | // from the fake |kLowMemoryDeviceThresholdMB| limit. |
| 45 | size_t memory_used = |
| 46 | AmountOfPhysicalMemoryImpl() - AmountOfAvailablePhysicalMemoryImpl(); |
| 47 | size_t memory_limit = kLowMemoryDeviceThresholdMB * 1024 * 1024; |
| 48 | // std::min ensures no underflow, as |memory_used| can be > |memory_limit|. |
| 49 | return memory_limit - std::min(memory_used, memory_limit); |
| 50 | } |
| 51 |
| 52 | return AmountOfAvailablePhysicalMemoryImpl(); |
| 53 | } |
| 54 |
| 55 | bool SysInfo::IsLowEndDevice() { |
| 56 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 57 | switches::kEnableLowEndDeviceMode)) { |
| 58 | return true; |
| 59 | } |
| 60 |
| 61 | return IsLowEndDeviceImpl(); |
| 62 | } |
| 63 |
| 64 | #if !defined(OS_ANDROID) |
| 65 |
| 66 | bool DetectLowEndDevice() { |
| 67 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 68 | if (command_line->HasSwitch(switches::kEnableLowEndDeviceMode)) |
| 69 | return true; |
| 70 | if (command_line->HasSwitch(switches::kDisableLowEndDeviceMode)) |
| 71 | return false; |
| 72 |
| 73 | int ram_size_mb = SysInfo::AmountOfPhysicalMemoryMB(); |
| 74 | return (ram_size_mb > 0 && ram_size_mb <= kLowMemoryDeviceThresholdMB); |
| 75 | } |
| 76 |
| 77 | // static |
| 78 | bool SysInfo::IsLowEndDeviceImpl() { |
| 79 | static base::NoDestructor< |
| 80 | internal::LazySysInfoValue<bool, DetectLowEndDevice>> |
| 81 | instance; |
| 82 | return instance->value(); |
| 83 | } |
| 84 | #endif |
| 85 |
| 86 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 87 | std::string SysInfo::HardwareModelName() { |
| 88 | return std::string(); |
| 89 | } |
| 90 | #endif |
| 91 |
| 92 | void SysInfo::GetHardwareInfo(base::OnceCallback<void(HardwareInfo)> callback) { |
| 93 | #if defined(OS_WIN) |
| 94 | // On Windows the calls to GetHardwareInfoSync can take a really long time to |
| 95 | // complete as they depend on WMI, using the CONTINUE_ON_SHUTDOWN traits will |
| 96 | // prevent this task from blocking shutdown. |
| 97 | base::PostTaskAndReplyWithResult( |
| 98 | base::ThreadPool::CreateCOMSTATaskRunner( |
| 99 | {TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}) |
| 100 | .get(), |
| 101 | FROM_HERE, base::BindOnce(&GetHardwareInfoSync), std::move(callback)); |
| 102 | #elif defined(OS_ANDROID) || defined(OS_MACOSX) |
| 103 | base::ThreadPool::PostTaskAndReplyWithResult( |
| 104 | FROM_HERE, {}, base::BindOnce(&GetHardwareInfoSync), std::move(callback)); |
| 105 | #elif defined(OS_LINUX) |
| 106 | base::ThreadPool::PostTaskAndReplyWithResult( |
| 107 | FROM_HERE, {base::MayBlock()}, base::BindOnce(&GetHardwareInfoSync), |
| 108 | std::move(callback)); |
| 109 | #else |
| 110 | NOTIMPLEMENTED(); |
| 111 | base::ThreadPool::PostTask( |
| 112 | FROM_HERE, {}, base::BindOnce(std::move(callback), HardwareInfo())); |
| 113 | #endif |
| 114 | } |
| 115 |
| 116 | // static |
| 117 | base::TimeDelta SysInfo::Uptime() { |
| 118 | // This code relies on an implementation detail of TimeTicks::Now() - that |
| 119 | // its return value happens to coincide with the system uptime value in |
| 120 | // microseconds, on Win/Mac/iOS/Linux/ChromeOS and Android. |
| 121 | int64_t uptime_in_microseconds = TimeTicks::Now().ToInternalValue(); |
| 122 | return base::TimeDelta::FromMicroseconds(uptime_in_microseconds); |
| 123 | } |
| 124 |
| 125 | } // namespace base |
| 126 |