khorimoto@chromium.org | c7e4e00 | 2012-04-25 10:59:44 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 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 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 8 | #include <stddef.h> |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 11 | #include <map> |
thestig@chromium.org | c913724 | 2012-09-26 11:49:42 +0900 | [diff] [blame] | 12 | #include <string> |
| 13 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 14 | #include "base/base_export.h" |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 16 | #include "base/gtest_prod_util.h" |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 17 | #include "base/time/time.h" |
thestig@chromium.org | c913724 | 2012-09-26 11:49:42 +0900 | [diff] [blame] | 18 | #include "build/build_config.h" |
tony@chromium.org | 1e25e00 | 2009-10-14 09:41:56 +0900 | [diff] [blame] | 19 | |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 20 | namespace base { |
| 21 | |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 22 | namespace debug { |
| 23 | FORWARD_DECLARE_TEST(SystemMetricsTest, ParseMeminfo); |
| 24 | } |
| 25 | |
| 26 | struct SystemMemoryInfoKB; |
| 27 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 28 | class BASE_EXPORT SysInfo { |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 29 | public: |
| 30 | // Return the number of logical processors/cores on the current machine. |
| 31 | static int NumberOfProcessors(); |
jeremy@chromium.org | 0da11fa | 2009-02-11 11:01:51 +0900 | [diff] [blame] | 32 | |
deanm@chromium.org | 319f044 | 2008-09-17 22:10:45 +0900 | [diff] [blame] | 33 | // Return the number of bytes of physical memory on the current machine. |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 34 | static int64_t AmountOfPhysicalMemory(); |
deanm@chromium.org | 530ddd9 | 2008-09-18 21:18:14 +0900 | [diff] [blame] | 35 | |
hongbo.min@intel.com | f6312b1 | 2012-10-27 15:10:37 +0900 | [diff] [blame] | 36 | // Return the number of bytes of current available physical memory on the |
| 37 | // machine. |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 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). |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 41 | static int64_t AmountOfAvailablePhysicalMemory(); |
hongbo.min@intel.com | f6312b1 | 2012-10-27 15:10:37 +0900 | [diff] [blame] | 42 | |
jochen@chromium.org | 5ac0b17 | 2014-04-09 08:19:16 +0900 | [diff] [blame] | 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. |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 46 | static int64_t AmountOfVirtualMemory(); |
jochen@chromium.org | 5ac0b17 | 2014-04-09 08:19:16 +0900 | [diff] [blame] | 47 | |
deanm@chromium.org | 530ddd9 | 2008-09-18 21:18:14 +0900 | [diff] [blame] | 48 | // Return the number of megabytes of physical memory on the current machine. |
| 49 | static int AmountOfPhysicalMemoryMB() { |
| 50 | return static_cast<int>(AmountOfPhysicalMemory() / 1024 / 1024); |
| 51 | } |
deanm@chromium.org | ee45e8f | 2008-09-18 21:34:24 +0900 | [diff] [blame] | 52 | |
jochen@chromium.org | 5ac0b17 | 2014-04-09 08:19:16 +0900 | [diff] [blame] | 53 | // Return the number of megabytes of available virtual memory, or zero if it |
| 54 | // is unlimited. |
| 55 | static int AmountOfVirtualMemoryMB() { |
| 56 | return static_cast<int>(AmountOfVirtualMemory() / 1024 / 1024); |
| 57 | } |
| 58 | |
rvargas@google.com | af00029 | 2008-09-20 10:16:23 +0900 | [diff] [blame] | 59 | // Return the available disk space in bytes on the volume containing |path|, |
| 60 | // or -1 on failure. |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 61 | static int64_t AmountOfFreeDiskSpace(const FilePath& path); |
deanm@chromium.org | ee45e8f | 2008-09-18 21:34:24 +0900 | [diff] [blame] | 62 | |
fukino | 1892bc2 | 2016-06-11 04:35:55 +0900 | [diff] [blame] | 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 | |
sque | babf673 | 2015-11-05 09:01:03 +0900 | [diff] [blame] | 67 | // Returns system uptime. |
| 68 | static TimeDelta Uptime(); |
jeremy@chromium.org | de658ed | 2012-10-29 10:04:09 +0900 | [diff] [blame] | 69 | |
jeremy | b43af09 | 2014-11-07 03:47:10 +0900 | [diff] [blame] | 70 | // Returns a descriptive string for the current machine model or an empty |
tdresser | f393369 | 2015-07-17 00:41:04 +0900 | [diff] [blame] | 71 | // string if the machine model is unknown or an error occured. |
asvitkine | 6a7c772 | 2017-05-05 07:52:00 +0900 | [diff] [blame] | 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. |
jeremy | b43af09 | 2014-11-07 03:47:10 +0900 | [diff] [blame] | 75 | static std::string HardwareModelName(); |
| 76 | |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 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 | |
jeremy@chromium.org | 7730edb | 2009-02-25 01:37:13 +0900 | [diff] [blame] | 83 | // Retrieves detailed numeric values for the OS version. |
mark@chromium.org | 71cce47 | 2011-06-15 03:18:38 +0900 | [diff] [blame] | 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. |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 89 | static void OperatingSystemVersionNumbers(int32_t* major_version, |
| 90 | int32_t* minor_version, |
| 91 | int32_t* bugfix_version); |
jeremy@chromium.org | 7730edb | 2009-02-25 01:37:13 +0900 | [diff] [blame] | 92 | |
thestig@chromium.org | c913724 | 2012-09-26 11:49:42 +0900 | [diff] [blame] | 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" |
thestig@chromium.org | e170665 | 2012-10-16 10:58:21 +0900 | [diff] [blame] | 97 | static std::string OperatingSystemArchitecture(); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 98 | |
thestig@chromium.org | c913724 | 2012-09-26 11:49:42 +0900 | [diff] [blame] | 99 | // Avoid using this. Use base/cpu.h to get information about the CPU instead. |
| 100 | // http://crbug.com/148884 |
hongbo.min@intel.com | 893f7d6 | 2012-09-08 18:53:01 +0900 | [diff] [blame] | 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 | |
agl@chromium.org | 3d71fda | 2009-01-22 10:42:15 +0900 | [diff] [blame] | 105 | // Return the smallest amount of memory (in bytes) which the VM system will |
| 106 | // allocate. |
| 107 | static size_t VMAllocationGranularity(); |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 108 | |
| 109 | #if defined(OS_CHROMEOS) |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 110 | typedef std::map<std::string, std::string> LsbReleaseMap; |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 111 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 112 | // Returns the contents of /etc/lsb-release as a map. |
| 113 | static const LsbReleaseMap& GetLsbReleaseMap(); |
khorimoto@chromium.org | c7e4e00 | 2012-04-25 10:59:44 +0900 | [diff] [blame] | 114 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 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",...). |
Daniel Erat | d133780 | 2017-08-11 13:06:59 +0900 | [diff] [blame] | 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. |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 132 | static std::string GetLsbReleaseBoard(); |
| 133 | |
Daniel Erat | d133780 | 2017-08-11 13:06:59 +0900 | [diff] [blame] | 134 | // DEPRECATED: Please see GetLsbReleaseBoard's comment. |
igorcov | 352c134 | 2016-12-02 04:42:32 +0900 | [diff] [blame] | 135 | // Convenience function for GetLsbReleaseBoard() removing trailing "-signed-*" |
| 136 | // if present. Returns "unknown" if CHROMEOS_RELEASE_BOARD is not set. |
Daniel Erat | d133780 | 2017-08-11 13:06:59 +0900 | [diff] [blame] | 137 | // TODO(derat): Delete this after October 2017. |
igorcov | 352c134 | 2016-12-02 04:42:32 +0900 | [diff] [blame] | 138 | static std::string GetStrippedReleaseBoard(); |
| 139 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 140 | // Returns the creation time of /etc/lsb-release. (Used to get the date and |
| 141 | // time of the Chrome OS build). |
| 142 | static Time GetLsbReleaseTime(); |
| 143 | |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 144 | // Returns true when actually running in a Chrome OS environment. |
| 145 | static bool IsRunningOnChromeOS(); |
| 146 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 147 | // Test method to force re-parsing of lsb-release. |
| 148 | static void SetChromeOSVersionInfoForTest(const std::string& lsb_release, |
| 149 | const Time& lsb_release_time); |
thestig@chromium.org | c913724 | 2012-09-26 11:49:42 +0900 | [diff] [blame] | 150 | #endif // defined(OS_CHROMEOS) |
ulan@chromium.org | 8ac7ea0 | 2012-05-26 00:31:37 +0900 | [diff] [blame] | 151 | |
| 152 | #if defined(OS_ANDROID) |
dfalcantara@chromium.org | 774888d | 2012-08-22 08:55:52 +0900 | [diff] [blame] | 153 | // Returns the Android build's codename. |
| 154 | static std::string GetAndroidBuildCodename(); |
| 155 | |
| 156 | // Returns the Android build ID. |
| 157 | static std::string GetAndroidBuildID(); |
| 158 | |
ulan@chromium.org | 8ac7ea0 | 2012-05-26 00:31:37 +0900 | [diff] [blame] | 159 | static int DalvikHeapSizeMB(); |
epenner@chromium.org | 066b81b | 2013-02-17 03:15:50 +0900 | [diff] [blame] | 160 | static int DalvikHeapGrowthLimitMB(); |
thestig@chromium.org | c913724 | 2012-09-26 11:49:42 +0900 | [diff] [blame] | 161 | #endif // defined(OS_ANDROID) |
c.shu@samsung.com | da21aff | 2014-06-27 01:55:27 +0900 | [diff] [blame] | 162 | |
| 163 | // Returns true if this is a low-end device. |
Chris Harrelson | 52a4da5 | 2017-07-25 06:47:31 +0900 | [diff] [blame] | 164 | // Low-end device refers to devices having a very low amount of total |
| 165 | // system memory, typically <= 1GB. |
| 166 | // See also SysUtils.java, method isLowEndDevice. |
c.shu@samsung.com | da21aff | 2014-06-27 01:55:27 +0900 | [diff] [blame] | 167 | static bool IsLowEndDevice(); |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 168 | |
| 169 | private: |
| 170 | FRIEND_TEST_ALL_PREFIXES(SysInfoTest, AmountOfAvailablePhysicalMemory); |
| 171 | FRIEND_TEST_ALL_PREFIXES(debug::SystemMetricsTest, ParseMeminfo); |
| 172 | |
Eric Karl | 3009718 | 2017-07-26 10:18:37 +0900 | [diff] [blame] | 173 | static int64_t AmountOfPhysicalMemoryImpl(); |
| 174 | static int64_t AmountOfAvailablePhysicalMemoryImpl(); |
Eric Karl | 88a9299 | 2018-03-14 01:34:03 +0900 | [diff] [blame] | 175 | static bool IsLowEndDeviceImpl(); |
Eric Karl | 3009718 | 2017-07-26 10:18:37 +0900 | [diff] [blame] | 176 | |
rayb | 157aae3 | 2017-04-27 07:35:08 +0900 | [diff] [blame] | 177 | #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX) |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 178 | static int64_t AmountOfAvailablePhysicalMemory( |
| 179 | const SystemMemoryInfoKB& meminfo); |
| 180 | #endif |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | } // namespace base |
| 184 | |
| 185 | #endif // BASE_SYS_INFO_H_ |