• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 #include <algorithm>
8 
9 #include "base/base_switches.h"
10 #include "base/command_line.h"
11 #include "base/lazy_instance.h"
12 #include "base/sys_info_internal.h"
13 #include "base/time/time.h"
14 #include "build/build_config.h"
15 
16 namespace base {
17 namespace {
18 static const int kLowMemoryDeviceThresholdMB = 512;
19 }
20 
21 // static
AmountOfPhysicalMemory()22 int64_t SysInfo::AmountOfPhysicalMemory() {
23   if (base::CommandLine::ForCurrentProcess()->HasSwitch(
24           switches::kEnableLowEndDeviceMode)) {
25     return kLowMemoryDeviceThresholdMB * 1024 * 1024;
26   }
27 
28   return AmountOfPhysicalMemoryImpl();
29 }
30 
31 // static
AmountOfAvailablePhysicalMemory()32 int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
33   if (base::CommandLine::ForCurrentProcess()->HasSwitch(
34           switches::kEnableLowEndDeviceMode)) {
35     // Estimate the available memory by subtracting our memory used estimate
36     // from the fake |kLowMemoryDeviceThresholdMB| limit.
37     size_t memory_used =
38         AmountOfPhysicalMemoryImpl() - AmountOfAvailablePhysicalMemoryImpl();
39     size_t memory_limit = kLowMemoryDeviceThresholdMB * 1024 * 1024;
40     // std::min ensures no underflow, as |memory_used| can be > |memory_limit|.
41     return memory_limit - std::min(memory_used, memory_limit);
42   }
43 
44   return AmountOfAvailablePhysicalMemoryImpl();
45 }
46 
IsLowEndDevice()47 bool SysInfo::IsLowEndDevice() {
48   if (base::CommandLine::ForCurrentProcess()->HasSwitch(
49           switches::kEnableLowEndDeviceMode)) {
50     return true;
51   }
52 
53   return IsLowEndDeviceImpl();
54 }
55 
56 #if !defined(OS_ANDROID)
57 
DetectLowEndDevice()58 bool DetectLowEndDevice() {
59   CommandLine* command_line = CommandLine::ForCurrentProcess();
60   if (command_line->HasSwitch(switches::kEnableLowEndDeviceMode))
61     return true;
62   if (command_line->HasSwitch(switches::kDisableLowEndDeviceMode))
63     return false;
64 
65   int ram_size_mb = SysInfo::AmountOfPhysicalMemoryMB();
66   return (ram_size_mb > 0 && ram_size_mb <= kLowMemoryDeviceThresholdMB);
67 }
68 
69 static LazyInstance<
70   internal::LazySysInfoValue<bool, DetectLowEndDevice> >::Leaky
71   g_lazy_low_end_device = LAZY_INSTANCE_INITIALIZER;
72 
73 // static
IsLowEndDeviceImpl()74 bool SysInfo::IsLowEndDeviceImpl() {
75   return g_lazy_low_end_device.Get().value();
76 }
77 #endif
78 
79 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
HardwareModelName()80 std::string SysInfo::HardwareModelName() {
81   return std::string();
82 }
83 #endif
84 
85 // static
Uptime()86 base::TimeDelta SysInfo::Uptime() {
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.
90   int64_t uptime_in_microseconds = TimeTicks::Now().ToInternalValue();
91   return base::TimeDelta::FromMicroseconds(uptime_in_microseconds);
92 }
93 
94 }  // namespace base
95