[email protected] | 0f24e53e | 2012-10-29 01:04:09 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |||||
5 | #include "base/sys_info.h" | ||||
6 | |||||
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 7 | #include "base/base_switches.h" |
8 | #include "base/command_line.h" | ||||
9 | #include "base/lazy_instance.h" | ||||
cnwan | d4270e8 | 2015-05-06 06:11:45 | [diff] [blame] | 10 | #include "base/metrics/field_trial.h" |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
cnwan | d4270e8 | 2015-05-06 06:11:45 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 13 | #include "base/sys_info_internal.h" |
[email protected] | 8f9a3a5 | 2013-06-28 15:14:18 | [diff] [blame] | 14 | #include "base/time/time.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | 0f24e53e | 2012-10-29 01:04:09 | [diff] [blame] | 16 | |
17 | namespace base { | ||||
Eric Karl | a628c09 | 2017-07-26 01:18:37 | [diff] [blame] | 18 | namespace { |
19 | static const int kLowMemoryDeviceThresholdMB = 512; | ||||
20 | } | ||||
21 | |||||
22 | // static | ||||
23 | int64_t SysInfo::AmountOfPhysicalMemory() { | ||||
24 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( | ||||
25 | switches::kEnableLowEndDeviceMode)) { | ||||
26 | return kLowMemoryDeviceThresholdMB * 1024 * 1024; | ||||
27 | } | ||||
28 | |||||
29 | return AmountOfPhysicalMemoryImpl(); | ||||
30 | } | ||||
31 | |||||
32 | // static | ||||
33 | int64_t SysInfo::AmountOfAvailablePhysicalMemory() { | ||||
34 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( | ||||
35 | switches::kEnableLowEndDeviceMode)) { | ||||
36 | // Estimate the available memory by subtracting our memory used estimate | ||||
37 | // from the fake |kLowMemoryDeviceThresholdMB| limit. | ||||
38 | size_t memory_used = | ||||
39 | AmountOfPhysicalMemoryImpl() - AmountOfAvailablePhysicalMemoryImpl(); | ||||
40 | size_t memory_limit = kLowMemoryDeviceThresholdMB * 1024 * 1024; | ||||
41 | // std::min ensures no underflow, as |memory_used| can be > |memory_limit|. | ||||
42 | return memory_limit - std::min(memory_used, memory_limit); | ||||
43 | } | ||||
44 | |||||
45 | return AmountOfAvailablePhysicalMemoryImpl(); | ||||
46 | } | ||||
[email protected] | 0f24e53e | 2012-10-29 01:04:09 | [diff] [blame] | 47 | |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 48 | #if !defined(OS_ANDROID) |
49 | |||||
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 50 | bool DetectLowEndDevice() { |
51 | CommandLine* command_line = CommandLine::ForCurrentProcess(); | ||||
ssid | feb3f47d | 2014-12-16 16:04:02 | [diff] [blame] | 52 | if (command_line->HasSwitch(switches::kEnableLowEndDeviceMode)) |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 53 | return true; |
ssid | feb3f47d | 2014-12-16 16:04:02 | [diff] [blame] | 54 | if (command_line->HasSwitch(switches::kDisableLowEndDeviceMode)) |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 55 | return false; |
56 | |||||
57 | int ram_size_mb = SysInfo::AmountOfPhysicalMemoryMB(); | ||||
mariakhomenko | 0064e5e | 2016-04-28 19:25:33 | [diff] [blame] | 58 | return (ram_size_mb > 0 && ram_size_mb <= kLowMemoryDeviceThresholdMB); |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 59 | } |
60 | |||||
61 | static LazyInstance< | ||||
62 | internal::LazySysInfoValue<bool, DetectLowEndDevice> >::Leaky | ||||
63 | g_lazy_low_end_device = LAZY_INSTANCE_INITIALIZER; | ||||
64 | |||||
65 | // static | ||||
66 | bool SysInfo::IsLowEndDevice() { | ||||
cnwan | d4270e8 | 2015-05-06 06:11:45 | [diff] [blame] | 67 | const std::string group_name = |
68 | base::FieldTrialList::FindFullName("MemoryReduction"); | ||||
69 | |||||
70 | // Low End Device Mode will be enabled if this client is assigned to | ||||
71 | // one of those EnabledXXX groups. | ||||
brettw | 89365dc | 2015-06-16 05:52:47 | [diff] [blame] | 72 | if (StartsWith(group_name, "Enabled", CompareCase::SENSITIVE)) |
cnwan | d4270e8 | 2015-05-06 06:11:45 | [diff] [blame] | 73 | return true; |
74 | |||||
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 75 | return g_lazy_low_end_device.Get().value(); |
76 | } | ||||
77 | #endif | ||||
78 | |||||
asvitkine | 666e5ee | 2017-05-04 22:52:00 | [diff] [blame] | 79 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
jeremy | ba4eca9 | 2014-11-06 18:47:10 | [diff] [blame] | 80 | std::string SysInfo::HardwareModelName() { |
81 | return std::string(); | ||||
82 | } | ||||
83 | #endif | ||||
84 | |||||
[email protected] | 0f24e53e | 2012-10-29 01:04:09 | [diff] [blame] | 85 | // static |
sque | 0f78ba54 | 2015-11-05 00:01:03 | [diff] [blame] | 86 | base::TimeDelta SysInfo::Uptime() { |
[email protected] | 0f24e53e | 2012-10-29 01:04:09 | [diff] [blame] | 87 | // This code relies on an implementation detail of TimeTicks::Now() - that |
88 | // its return value happens to coincide with the system uptime value in | ||||
89 | // microseconds, on Win/Mac/iOS/Linux/ChromeOS and Android. | ||||
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 90 | int64_t uptime_in_microseconds = TimeTicks::Now().ToInternalValue(); |
sque | 0f78ba54 | 2015-11-05 00:01:03 | [diff] [blame] | 91 | return base::TimeDelta::FromMicroseconds(uptime_in_microseconds); |
[email protected] | 0f24e53e | 2012-10-29 01:04:09 | [diff] [blame] | 92 | } |
93 | |||||
94 | } // namespace base |