• 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 #ifndef BASE_SYS_INFO_H_
6 #define BASE_SYS_INFO_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <map>
12 #include <string>
13 
14 #include "base/base_export.h"
15 #include "base/files/file_path.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/time/time.h"
18 #include "build/build_config.h"
19 
20 namespace base {
21 
22 namespace debug {
23 FORWARD_DECLARE_TEST(SystemMetricsTest, ParseMeminfo);
24 }
25 
26 struct SystemMemoryInfoKB;
27 
28 class BASE_EXPORT SysInfo {
29  public:
30   // Return the number of logical processors/cores on the current machine.
31   static int NumberOfProcessors();
32 
33   // Return the number of bytes of physical memory on the current machine.
34   static int64_t AmountOfPhysicalMemory();
35 
36   // Return the number of bytes of current available physical memory on the
37   // machine.
38   // (The amount of memory that can be allocated without any significant
39   // impact on the system. It can lead to freeing inactive file-backed
40   // and/or speculative file-backed memory).
41   static int64_t AmountOfAvailablePhysicalMemory();
42 
43   // Return the number of bytes of virtual memory of this process. A return
44   // value of zero means that there is no limit on the available virtual
45   // memory.
46   static int64_t AmountOfVirtualMemory();
47 
48   // Return the number of megabytes of physical memory on the current machine.
AmountOfPhysicalMemoryMB()49   static int AmountOfPhysicalMemoryMB() {
50     return static_cast<int>(AmountOfPhysicalMemory() / 1024 / 1024);
51   }
52 
53   // Return the number of megabytes of available virtual memory, or zero if it
54   // is unlimited.
AmountOfVirtualMemoryMB()55   static int AmountOfVirtualMemoryMB() {
56     return static_cast<int>(AmountOfVirtualMemory() / 1024 / 1024);
57   }
58 
59   // Return the available disk space in bytes on the volume containing |path|,
60   // or -1 on failure.
61   static int64_t AmountOfFreeDiskSpace(const FilePath& path);
62 
63   // Return the total disk space in bytes on the volume containing |path|, or -1
64   // on failure.
65   static int64_t AmountOfTotalDiskSpace(const FilePath& path);
66 
67   // Returns system uptime.
68   static TimeDelta Uptime();
69 
70   // Returns a descriptive string for the current machine model or an empty
71   // string if the machine model is unknown or an error occured.
72   // e.g. "MacPro1,1" on Mac, "iPhone9,3" on iOS or "Nexus 5" on Android. Only
73   // implemented on OS X, iOS, Android, and Chrome OS. This returns an empty
74   // string on other platforms.
75   static std::string HardwareModelName();
76 
77   // Returns the name of the host operating system.
78   static std::string OperatingSystemName();
79 
80   // Returns the version of the host operating system.
81   static std::string OperatingSystemVersion();
82 
83   // Retrieves detailed numeric values for the OS version.
84   // DON'T USE THIS ON THE MAC OR WINDOWS to determine the current OS release
85   // for OS version-specific feature checks and workarounds. If you must use
86   // an OS version check instead of a feature check, use the base::mac::IsOS*
87   // family from base/mac/mac_util.h, or base::win::GetVersion from
88   // base/win/windows_version.h.
89   static void OperatingSystemVersionNumbers(int32_t* major_version,
90                                             int32_t* minor_version,
91                                             int32_t* bugfix_version);
92 
93   // Returns the architecture of the running operating system.
94   // Exact return value may differ across platforms.
95   // e.g. a 32-bit x86 kernel on a 64-bit capable CPU will return "x86",
96   //      whereas a x86-64 kernel on the same CPU will return "x86_64"
97   static std::string OperatingSystemArchitecture();
98 
99   // Avoid using this. Use base/cpu.h to get information about the CPU instead.
100   // http://crbug.com/148884
101   // Returns the CPU model name of the system. If it can not be figured out,
102   // an empty string is returned.
103   static std::string CPUModelName();
104 
105   // Return the smallest amount of memory (in bytes) which the VM system will
106   // allocate.
107   static size_t VMAllocationGranularity();
108 
109 #if defined(OS_CHROMEOS)
110   typedef std::map<std::string, std::string> LsbReleaseMap;
111 
112   // Returns the contents of /etc/lsb-release as a map.
113   static const LsbReleaseMap& GetLsbReleaseMap();
114 
115   // If |key| is present in the LsbReleaseMap, sets |value| and returns true.
116   static bool GetLsbReleaseValue(const std::string& key, std::string* value);
117 
118   // Convenience function for GetLsbReleaseValue("CHROMEOS_RELEASE_BOARD",...).
119   // Returns "unknown" if CHROMEOS_RELEASE_BOARD is not set. Otherwise, returns
120   // the full name of the board. Note that the returned value often differs
121   // between developers' systems and devices that use official builds. E.g. for
122   // a developer-built image, the function could return 'glimmer', while in an
123   // official build, it may be something like 'glimmer-signed-mp-v4keys'.
124   //
125   // NOTE: Strings returned by this function should be treated as opaque values
126   // within Chrome (e.g. for reporting metrics elsewhere). If you need to make
127   // Chrome behave differently for different Chrome OS devices, either directly
128   // check for the hardware feature that you care about (preferred) or add a
129   // command-line flag to Chrome and pass it from session_manager (based on
130   // whether a USE flag is set or not). See https://goo.gl/BbBkzg for more
131   // details.
132   static std::string GetLsbReleaseBoard();
133 
134   // Returns the creation time of /etc/lsb-release. (Used to get the date and
135   // time of the Chrome OS build).
136   static Time GetLsbReleaseTime();
137 
138   // Returns true when actually running in a Chrome OS environment.
139   static bool IsRunningOnChromeOS();
140 
141   // Test method to force re-parsing of lsb-release.
142   static void SetChromeOSVersionInfoForTest(const std::string& lsb_release,
143                                             const Time& lsb_release_time);
144 #endif  // defined(OS_CHROMEOS)
145 
146 #if defined(OS_ANDROID)
147   // Returns the Android build's codename.
148   static std::string GetAndroidBuildCodename();
149 
150   // Returns the Android build ID.
151   static std::string GetAndroidBuildID();
152 
153   static int DalvikHeapSizeMB();
154   static int DalvikHeapGrowthLimitMB();
155 #endif  // defined(OS_ANDROID)
156 
157   // Returns true if this is a low-end device.
158   // Low-end device refers to devices having a very low amount of total
159   // system memory, typically <= 1GB.
160   // See also SysUtils.java, method isLowEndDevice.
161   static bool IsLowEndDevice();
162 
163  private:
164   FRIEND_TEST_ALL_PREFIXES(SysInfoTest, AmountOfAvailablePhysicalMemory);
165   FRIEND_TEST_ALL_PREFIXES(debug::SystemMetricsTest, ParseMeminfo);
166 
167   static int64_t AmountOfPhysicalMemoryImpl();
168   static int64_t AmountOfAvailablePhysicalMemoryImpl();
169   static bool IsLowEndDeviceImpl();
170 
171 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX)
172   static int64_t AmountOfAvailablePhysicalMemory(
173       const SystemMemoryInfoKB& meminfo);
174 #endif
175 };
176 
177 }  // namespace base
178 
179 #endif  // BASE_SYS_INFO_H_
180