1 // Copyright 2012 The Chromium Authors 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_SYSTEM_SYS_INFO_H_ 6 #define BASE_SYSTEM_SYS_INFO_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <map> 12 #include <string> 13 #include <string_view> 14 15 #include "base/base_export.h" 16 #include "base/functional/callback_forward.h" 17 #include "base/gtest_prod_util.h" 18 #include "base/metrics/field_trial_params.h" 19 #include "base/time/time.h" 20 #include "build/build_config.h" 21 #include "third_party/abseil-cpp/absl/types/optional.h" 22 23 #if BUILDFLAG(IS_MAC) 24 #include "base/feature_list.h" 25 #endif 26 27 namespace base { 28 29 #if BUILDFLAG(IS_MAC) 30 // When enabled, NumberOfProcessors() returns the number of physical processors 31 // instead of the number of logical processors if CPU security mitigations are 32 // enabled for the current process. 33 BASE_EXPORT BASE_DECLARE_FEATURE(kNumberOfCoresWithCpuSecurityMitigation); 34 #endif 35 36 #if BUILDFLAG(IS_CHROMEOS_ASH) 37 // Strings for environment variables. 38 BASE_EXPORT extern const char kLsbReleaseKey[]; 39 BASE_EXPORT extern const char kLsbReleaseTimeKey[]; 40 #endif 41 42 namespace debug { 43 FORWARD_DECLARE_TEST(SystemMetricsTest, ParseMeminfo); 44 } 45 46 namespace test { 47 class ScopedAmountOfPhysicalMemoryOverride; 48 } 49 50 class FilePath; 51 struct SystemMemoryInfoKB; 52 53 class BASE_EXPORT SysInfo { 54 public: 55 // Returns the number of processors/cores available for the current 56 // application. This is typically the number of logical cores installed on the 57 // system, but could instead be the number of physical cores when 58 // SetCpuSecurityMitigationsEnabled() has been invoked to indicate that CPU 59 // security mitigations are enabled on Mac. 60 // On some platforms this may cache the resulting value in its implementation, 61 // e.g. on Linux/ChromeOS where this function cannot run in a sandbox and so 62 // a cached value must be returned. 63 static int NumberOfProcessors(); 64 65 // Returns the number of the most efficient logical processors for the current 66 // application. This is typically e-cores on Intel hybrid architecture, or 67 // LITTLE cores on ARM bit.LITTLE architecture. 68 // Returns 0 on symmetric architecture or when it failed to recognize. 69 // This function will cache the result value in its implementation. 70 static int NumberOfEfficientProcessors(); 71 72 // Return the number of bytes of physical memory on the current machine. 73 // If low-end device mode is manually enabled via command line flag, this 74 // will return the lesser of the actual physical memory, or 512MB. 75 static uint64_t AmountOfPhysicalMemory(); 76 77 // Return the number of bytes of current available physical memory on the 78 // machine. 79 // (The amount of memory that can be allocated without any significant 80 // impact on the system. It can lead to freeing inactive file-backed 81 // and/or speculative file-backed memory). 82 static uint64_t AmountOfAvailablePhysicalMemory(); 83 84 // Return the number of bytes of virtual memory of this process. A return 85 // value of zero means that there is no limit on the available virtual 86 // memory. 87 static uint64_t AmountOfVirtualMemory(); 88 89 // Return the number of megabytes of physical memory on the current machine. AmountOfPhysicalMemoryMB()90 static int AmountOfPhysicalMemoryMB() { 91 return static_cast<int>(AmountOfPhysicalMemory() / 1024 / 1024); 92 } 93 94 // Return the number of megabytes of available virtual memory, or zero if it 95 // is unlimited. AmountOfVirtualMemoryMB()96 static int AmountOfVirtualMemoryMB() { 97 return static_cast<int>(AmountOfVirtualMemory() / 1024 / 1024); 98 } 99 100 // Return the available disk space in bytes on the volume containing |path|, 101 // or -1 on failure. 102 static int64_t AmountOfFreeDiskSpace(const FilePath& path); 103 104 // Return the total disk space in bytes on the volume containing |path|, or -1 105 // on failure. 106 static int64_t AmountOfTotalDiskSpace(const FilePath& path); 107 108 #if BUILDFLAG(IS_FUCHSIA) 109 // Sets the total amount of disk space to report under the specified |path|. 110 // If |bytes| is -ve then any existing entry for |path| is removed. 111 static void SetAmountOfTotalDiskSpace(const FilePath& path, int64_t bytes); 112 #endif 113 114 // Returns system uptime. 115 static TimeDelta Uptime(); 116 117 // Returns a descriptive string for the current machine model or an empty 118 // string if the machine model is unknown or an error occurred. 119 // e.g. "MacPro1,1" on Mac, "iPhone9,3" on iOS or "Nexus 5" on Android. Only 120 // implemented on macOS, iOS, Android, Chrome OS and Windows. This returns an 121 // empty string on other platforms. 122 // 123 // For macOS, a useful reference of the resulting strings returned by this 124 // function and their corresponding hardware can be found at 125 // https://everymac.com/systems/by_capability/mac-specs-by-machine-model-machine-id.html 126 static std::string HardwareModelName(); 127 128 #if BUILDFLAG(IS_MAC) 129 struct HardwareModelNameSplit { 130 std::string category; 131 int model = 0; 132 int variant = 0; 133 }; 134 // Hardware model names on the Mac are of the shape "Mac," where the 135 // prefix is the general category, the is the model, and the is the 136 // variant. This function takes the hardware model name as returned by 137 // HardwareModelName() above, and returns it split into its constituent parts. 138 // Returns nullopt if the value cannot be parsed. 139 // 140 // /!\ WARNING 141 // 142 // This is NOT A USEFUL FUNCTION and SHOULD NOT BE USED. While the `model` 143 // value does inform as to what generation of hardware it is within the 144 // `category`, this is not useful in determining the capabilities of the 145 // hardware. Instead of using the `model` value, check the actual capabilities 146 // of the hardware to verify what it can do rather than relying on a hardware 147 // model name. In addition, while the `category` value used to have meaning 148 // and could be used to determine the type of hardware (e.g. desktop vs 149 // laptop), in 2022 Apple started using the generic category of "Mac", thus 150 // removing its usefulness when used alone. While the entire model string as 151 // returned by HardwareModelName() above can be useful for identifying a 152 // specific piece of equipment, splitting apart it is not useful. 153 // 154 // Do not add any further callers! When the aforementioned 2022-era hardware 155 // is the minimum requirement for Chromium, remove this function and adjust 156 // all callers appropriately. 157 static absl::optional<HardwareModelNameSplit> SplitHardwareModelNameDoNotUse( 158 std::string_view name); 159 #endif 160 161 struct HardwareInfo { 162 std::string manufacturer; 163 std::string model; 164 }; 165 // Returns via |callback| a struct containing descriptive UTF-8 strings for 166 // the current machine manufacturer and model, or empty strings if the 167 // information is unknown or an error occurred. Implemented on Windows, macOS, 168 // iOS, Linux, Chrome OS and Android. 169 static void GetHardwareInfo(base::OnceCallback<void(HardwareInfo)> callback); 170 171 // Returns the name of the host operating system. 172 static std::string OperatingSystemName(); 173 174 // Returns the version of the host operating system. 175 static std::string OperatingSystemVersion(); 176 177 // Retrieves detailed numeric values for the OS version. 178 // DON'T USE THIS ON THE MAC OR WINDOWS to determine the current OS release 179 // for OS version-specific feature checks and workarounds. If you must use an 180 // OS version check instead of a feature check, use 181 // base::mac::MacOSVersion()/MacOSMajorVersion() family from 182 // base/mac/mac_util.h, or base::win::GetVersion() from 183 // base/win/windows_version.h. 184 static void OperatingSystemVersionNumbers(int32_t* major_version, 185 int32_t* minor_version, 186 int32_t* bugfix_version); 187 188 // Returns the architecture of the running operating system. 189 // Exact return value may differ across platforms. 190 // e.g. a 32-bit x86 kernel on a 64-bit capable CPU will return "x86", 191 // whereas a x86-64 kernel on the same CPU will return "x86_64" 192 static std::string OperatingSystemArchitecture(); 193 194 // Returns the architecture of the running process, which might be different 195 // than the architecture returned by OperatingSystemArchitecture() (e.g. 196 // macOS Rosetta, a 32-bit binary on a 64-bit OS, etc). 197 // Will return one of: "x86", "x86_64", "ARM", "ARM_64", or an empty string if 198 // none of the above. 199 static std::string ProcessCPUArchitecture(); 200 201 // Returns the CPU model name of the system. If it can not be figured out, 202 // an empty string is returned. 203 // More detailed info can be obtained from base/cpu.h. 204 static std::string CPUModelName(); 205 206 // Return the smallest amount of memory (in bytes) which the VM system will 207 // allocate. 208 static size_t VMAllocationGranularity(); 209 210 #if BUILDFLAG(IS_CHROMEOS) 211 // Set |value| and return true if LsbRelease contains information about |key|. 212 static bool GetLsbReleaseValue(const std::string& key, std::string* value); 213 214 // Convenience function for GetLsbReleaseValue("CHROMEOS_RELEASE_BOARD",...). 215 // Returns "unknown" if CHROMEOS_RELEASE_BOARD is not set. Otherwise, returns 216 // the full name of the board. Note that the returned value often differs 217 // between developers' systems and devices that use official builds. E.g. for 218 // a developer-built image, the function could return 'glimmer', while in an 219 // official build, it may be something like 'glimmer-signed-mp-v4keys'. 220 // 221 // NOTE: Strings returned by this function should be treated as opaque values 222 // within Chrome (e.g. for reporting metrics elsewhere). If you need to make 223 // Chrome behave differently for different Chrome OS devices, either directly 224 // check for the hardware feature that you care about (preferred) or add a 225 // command-line flag to Chrome and pass it from session_manager (based on 226 // whether a USE flag is set or not). See https://goo.gl/BbBkzg for more 227 // details. 228 static std::string GetLsbReleaseBoard(); 229 230 // Returns the creation time of /etc/lsb-release. (Used to get the date and 231 // time of the Chrome OS build). 232 static Time GetLsbReleaseTime(); 233 234 // Returns true when actually running in a Chrome OS environment. 235 static bool IsRunningOnChromeOS(); 236 237 // Overrides |lsb_release| and |lsb_release_time|. Overrides cannot be nested. 238 // Call ResetChromeOSVersionInfoForTest() to restore the previous values. 239 // Prefer base::test::ScopedChromeOSVersionInfo to calling this function. 240 static void SetChromeOSVersionInfoForTest(const std::string& lsb_release, 241 const Time& lsb_release_time); 242 243 // Undoes the function above. 244 static void ResetChromeOSVersionInfoForTest(); 245 246 // Returns the kernel version of the host operating system. 247 static std::string KernelVersion(); 248 249 // Crashes if running on Chrome OS non-test image. Use only for really 250 // sensitive and risky use cases. Only works while running in verified mode, 251 // this check an easily be bypassed in dev mode. 252 static void CrashIfChromeOSNonTestImage(); 253 #endif // BUILDFLAG(IS_CHROMEOS) 254 255 #if BUILDFLAG(IS_ANDROID) 256 // Returns the Android build's codename. 257 static std::string GetAndroidBuildCodename(); 258 259 // Returns the Android build ID. 260 static std::string GetAndroidBuildID(); 261 262 // Returns the Android hardware EGL system property. 263 static std::string GetAndroidHardwareEGL(); 264 #endif // BUILDFLAG(IS_ANDROID) 265 266 #if BUILDFLAG(IS_IOS) 267 // Returns the iOS build number string which is normally an alphanumeric 268 // string like 12E456. This build number can differentiate between different 269 // versions of iOS that may have the same major/minor/bugfix version numbers. 270 // For example, iOS beta releases have the same version number but different 271 // build number strings. 272 static std::string GetIOSBuildNumber(); 273 #endif // BUILDFLAG(IS_IOS) 274 275 // Returns true for low-end devices that may require extreme tradeoffs, 276 // including user-visible changes, for acceptable performance. 277 // For general memory optimizations, consider |AmountOfPhysicalMemoryMB|. 278 // 279 // On Android this returns: 280 // true when memory <= 1GB on Android O and later. 281 // true when memory <= 512MB on Android N and earlier. 282 // This is not the same as "low-memory" and will be false on a large number of 283 // <=1GB pre-O Android devices. See: |detectLowEndDevice| in SysUtils.java. 284 // On Desktop this returns true when memory <= 2GB. 285 static bool IsLowEndDevice(); 286 287 // The same as IsLowEndDevice() except on Android. 288 // 289 // On Android this returns: 290 // true when IsLowEndDevice() returns true. 291 // true when the physical memory of the device is 4gb or 6gb and 292 // the feature: kPartialLowEndModeOnMidEndDevices() is enabled. 293 static bool IsLowEndDeviceOrPartialLowEndModeEnabled(); 294 static bool IsLowEndDeviceOrPartialLowEndModeEnabled( 295 const FeatureParam<bool>& param_for_exclusion); 296 297 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) 298 // Returns true for Android devices whose memory is X GB, considering 299 // carveouts. The carveouts is memory reserved by the system, e.g. 300 // for drivers, MTE, etc. It's very common for querying app to see 301 // hundreds MBs less than actual physical memory installed on the system. 302 // Addendum: This logic should also work for ChromeOS. 303 static bool Is3GbDevice(); 304 static bool Is4GbDevice(); 305 static bool Is6GbDevice(); 306 // Returns true for Android devices whose memory is 4GB or 6GB, considering 307 // carveouts. 308 static bool Is4GbOr6GbDevice(); 309 #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) 310 311 #if BUILDFLAG(IS_MAC) 312 // Indicates that CPU security mitigations are enabled for the current 313 // process. This is used to control the behavior of NumberOfProcessors(), see 314 // comment on that method. 315 static void SetCpuSecurityMitigationsEnabled(); 316 317 // Resets all state associated with CPU security mitigations. 318 static void ResetCpuSecurityMitigationsEnabledForTesting(); 319 #endif 320 321 private: 322 friend class test::ScopedAmountOfPhysicalMemoryOverride; 323 FRIEND_TEST_ALL_PREFIXES(SysInfoTest, AmountOfAvailablePhysicalMemory); 324 FRIEND_TEST_ALL_PREFIXES(debug::SystemMetricsTest, ParseMeminfo); 325 326 static int NumberOfEfficientProcessorsImpl(); 327 static uint64_t AmountOfPhysicalMemoryImpl(); 328 static uint64_t AmountOfAvailablePhysicalMemoryImpl(); 329 static bool IsLowEndDeviceImpl(); 330 static HardwareInfo GetHardwareInfoSync(); 331 332 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \ 333 BUILDFLAG(IS_AIX) 334 static uint64_t AmountOfAvailablePhysicalMemory( 335 const SystemMemoryInfoKB& meminfo); 336 #endif 337 338 // Sets the amount of physical memory in MB for testing, thus allowing tests 339 // to run irrespective of the host machine's configuration. 340 static absl::optional<uint64_t> SetAmountOfPhysicalMemoryMbForTesting( 341 uint64_t amount_of_memory_mb); 342 static void ClearAmountOfPhysicalMemoryMbForTesting(); 343 }; 344 345 } // namespace base 346 347 #endif // BASE_SYSTEM_SYS_INFO_H_ 348