| 6 | #define BASE_SYSTEM_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/callback_forward.h" |
| 16 | #include "base/files/file_path.h" |
| 17 | #include "base/gtest_prod_util.h" |
| 18 | #include "base/time/time.h" |
| 19 | #include "build/build_config.h" |
| 20 |
| 21 | namespace base { |
| 22 |
| 23 | namespace debug { |
| 24 | FORWARD_DECLARE_TEST(SystemMetricsTest, ParseMeminfo); |
| 25 | } |
| 26 |
| 27 | struct SystemMemoryInfoKB; |
| 28 |
| 29 | class BASE_EXPORT SysInfo { |
| 30 | public: |
| 31 | // Return the number of logical processors/cores on the current machine. |
| 32 | static int NumberOfProcessors(); |
| 33 |
| 34 | // Return the number of bytes of physical memory on the current machine. |
| 35 | static int64_t AmountOfPhysicalMemory(); |
| 36 |
| 37 | // Return the number of bytes of current available physical memory on the |
| 38 | // machine. |
| 39 | // (The amount of memory that can be allocated without any significant |
| 40 | // impact on the system. It can lead to freeing inactive file-backed |
| 41 | // and/or speculative file-backed memory). |
| 42 | static int64_t AmountOfAvailablePhysicalMemory(); |
| 43 |
| 44 | // Return the number of bytes of virtual memory of this process. A return |
| 45 | // value of zero means that there is no limit on the available virtual |
| 46 | // memory. |
| 47 | static int64_t AmountOfVirtualMemory(); |
| 48 |
| 49 | // Return the number of megabytes of physical memory on the current machine. |
| 50 | static int AmountOfPhysicalMemoryMB() { |
| 51 | return static_cast<int>(AmountOfPhysicalMemory() / 1024 / 1024); |
| 52 | } |
| 53 |
| 54 | // Return the number of megabytes of available virtual memory, or zero if it |
| 55 | // is unlimited. |
| 56 | static int AmountOfVirtualMemoryMB() { |
| 57 | return static_cast<int>(AmountOfVirtualMemory() / 1024 / 1024); |
| 58 | } |
| 59 |
| 60 | // Return the available disk space in bytes on the volume containing |path|, |
| 61 | // or -1 on failure. |
| 62 | static int64_t AmountOfFreeDiskSpace(const FilePath& path); |
| 63 |
| 64 | // Return the total disk space in bytes on the volume containing |path|, or -1 |
| 65 | // on failure. |
| 66 | static int64_t AmountOfTotalDiskSpace(const FilePath& path); |
| 67 |
| 68 | // Returns system uptime. |
| 69 | static TimeDelta Uptime(); |
| 70 |
| 71 | // Returns a descriptive string for the current machine model or an empty |
| 72 | // string if the machine model is unknown or an error occurred. |
| 73 | // e.g. "MacPro1,1" on Mac, "iPhone9,3" on iOS or "Nexus 5" on Android. Only |
| 74 | // implemented on OS X, iOS, and Android. This returns an empty string on |
| 75 | // other platforms. |
| 76 | static std::string HardwareModelName(); |
| 77 |
| 78 | struct HardwareInfo { |
| 79 | std::string manufacturer; |
| 80 | std::string model; |
| 81 | // On Windows, this is the BIOS serial number. Unsupported platforms will be |
| 82 | // set to an empty string. |
| 83 | // Note: validate any new usage with the privacy team. |
| 84 | // TODO(crbug.com/907518): Implement support on other platforms. |
| 85 | std::string serial_number; |
| 86 |
| 87 | bool operator==(const HardwareInfo& rhs) const { |
| 88 | return manufacturer == rhs.manufacturer && model == rhs.model && |
| 89 | serial_number == rhs.serial_number; |
| 90 | } |
| 91 | }; |
| 92 | // Returns via |callback| a struct containing descriptive UTF-8 strings for |
| 93 | // the current machine manufacturer and model, or empty strings if the |
| 94 | // information is unknown or an error occurred. Implemented on Windows, OS X, |
| 95 | // iOS, Linux, Chrome OS and Android. |
| 96 | static void GetHardwareInfo(base::OnceCallback<void(HardwareInfo)> callback); |
| 97 |
| 98 | // Returns the name of the host operating system. |
| 99 | static std::string OperatingSystemName(); |
| 100 |
| 101 | // Returns the version of the host operating system. |
| 102 | static std::string OperatingSystemVersion(); |
| 103 |
| 104 | // Retrieves detailed numeric values for the OS version. |
| 105 | // DON'T USE THIS ON THE MAC OR WINDOWS to determine the current OS release |
| 106 | // for OS version-specific feature checks and workarounds. If you must use |
| 107 | // an OS version check instead of a feature check, use the base::mac::IsOS* |
| 108 | // family from base/mac/mac_util.h, or base::win::GetVersion from |
| 109 | // base/win/windows_version.h. |
| 110 | static void OperatingSystemVersionNumbers(int32_t* major_version, |
| 111 | int32_t* minor_version, |
| 112 | int32_t* bugfix_version); |
| 113 |
| 114 | // Returns the architecture of the running operating system. |
| 115 | // Exact return value may differ across platforms. |
| 116 | // e.g. a 32-bit x86 kernel on a 64-bit capable CPU will return "x86", |
| 117 | // whereas a x86-64 kernel on the same CPU will return "x86_64" |
| 118 | static std::string OperatingSystemArchitecture(); |
| 119 |
| 120 | // Avoid using this. Use base/cpu.h to get information about the CPU instead. |
| 121 | // http://crbug.com/148884 |
| 122 | // Returns the CPU model name of the system. If it can not be figured out, |
| 123 | // an empty string is returned. |
| 124 | static std::string CPUModelName(); |
| 125 |
| 126 | // Return the smallest amount of memory (in bytes) which the VM system will |
| 127 | // allocate. |
| 128 | static size_t VMAllocationGranularity(); |
| 129 |
| 130 | #if defined(OS_CHROMEOS) |
| 131 | // Set |value| and return true if LsbRelease contains information about |key|. |
| 132 | static bool GetLsbReleaseValue(const std::string& key, std::string* value); |
| 133 |
| 134 | // Convenience function for GetLsbReleaseValue("CHROMEOS_RELEASE_BOARD",...). |
| 135 | // Returns "unknown" if CHROMEOS_RELEASE_BOARD is not set. Otherwise, returns |
| 136 | // the full name of the board. Note that the returned value often differs |
| 137 | // between developers' systems and devices that use official builds. E.g. for |
| 138 | // a developer-built image, the function could return 'glimmer', while in an |
| 139 | // official build, it may be something like 'glimmer-signed-mp-v4keys'. |
| 140 | // |
| 141 | // NOTE: Strings returned by this function should be treated as opaque values |
| 142 | // within Chrome (e.g. for reporting metrics elsewhere). If you need to make |
| 143 | // Chrome behave differently for different Chrome OS devices, either directly |
| 144 | // check for the hardware feature that you care about (preferred) or add a |
| 145 | // command-line flag to Chrome and pass it from session_manager (based on |
| 146 | // whether a USE flag is set or not). See https://goo.gl/BbBkzg for more |
| 147 | // details. |
| 148 | static std::string GetLsbReleaseBoard(); |
| 149 |
| 150 | // Returns the creation time of /etc/lsb-release. (Used to get the date and |
| 151 | // time of the Chrome OS build). |
| 152 | static Time GetLsbReleaseTime(); |
| 153 |
| 154 | // Returns true when actually running in a Chrome OS environment. |
| 155 | static bool IsRunningOnChromeOS(); |
| 156 |
| 157 | // Test method to force re-parsing of lsb-release. |
| 158 | static void SetChromeOSVersionInfoForTest(const std::string& lsb_release, |
| 159 | const Time& lsb_release_time); |
| 160 |
| 161 | // Returns the kernel version of the host operating system. |
| 162 | static std::string KernelVersion(); |
| 163 | #endif // defined(OS_CHROMEOS) |
| 164 |
| 165 | #if defined(OS_ANDROID) |
| 166 | // Returns the Android build's codename. |
| 167 | static std::string GetAndroidBuildCodename(); |
| 168 |
| 169 | // Returns the Android build ID. |
| 170 | static std::string GetAndroidBuildID(); |
| 171 |
| 172 | // Returns the Android hardware EGL system property. |
| 173 | static std::string GetAndroidHardwareEGL(); |
| 174 |
| 175 | static int DalvikHeapSizeMB(); |
| 176 | static int DalvikHeapGrowthLimitMB(); |
| 177 | #endif // defined(OS_ANDROID) |
| 178 |
| 179 | #if defined(OS_IOS) |
| 180 | // Returns the iOS build number string which is normally an alphanumeric |
| 181 | // string like 12E456. This build number can differentiate between different |
| 182 | // versions of iOS that may have the same major/minor/bugfix version numbers. |
| 183 | // For example, iOS beta releases have the same version number but different |
| 184 | // build number strings. |
| 185 | static std::string GetIOSBuildNumber(); |
| 186 | #endif // defined(OS_IOS) |
| 187 |
| 188 | // Returns true for low-end devices that may require extreme tradeoffs, |
| 189 | // including user-visible changes, for acceptable performance. |
| 190 | // For general memory optimizations, consider |AmountOfPhysicalMemoryMB|. |
| 191 | // |
| 192 | // On Android this returns: |
| 193 | // true when memory <= 1GB on Android O and later. |
| 194 | // true when memory <= 512MB on Android N and earlier. |
| 195 | // This is not the same as "low-memory" and will be false on a large number of |
| 196 | // <=1GB pre-O Android devices. See: |detectLowEndDevice| in SysUtils.java. |
| 197 | // On Desktop this returns true when memory <= 512MB. |
| 198 | static bool IsLowEndDevice(); |
| 199 |
| 200 | private: |
| 201 | FRIEND_TEST_ALL_PREFIXES(SysInfoTest, AmountOfAvailablePhysicalMemory); |
| 202 | FRIEND_TEST_ALL_PREFIXES(debug::SystemMetricsTest, ParseMeminfo); |
| 203 |
| 204 | static int64_t AmountOfPhysicalMemoryImpl(); |
| 205 | static int64_t AmountOfAvailablePhysicalMemoryImpl(); |
| 206 | static bool IsLowEndDeviceImpl(); |
| 207 | static HardwareInfo GetHardwareInfoSync(); |
| 208 |
| 209 | #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX) |
| 210 | static int64_t AmountOfAvailablePhysicalMemory( |
| 211 | const SystemMemoryInfoKB& meminfo); |
| 212 | #endif |
| 213 | }; |
| 214 |
| 215 | } // namespace base |
| 216 |
| 217 | #endif // BASE_SYSTEM_SYS_INFO_H_ |
| 218 |