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 <optional> 13 #include <string> 14 #include <string_view> 15 16 #include "base/base_export.h" 17 #include "base/functional/callback_forward.h" 18 #include "base/gtest_prod_util.h" 19 #include "base/metrics/field_trial_params.h" 20 #include "base/time/time.h" 21 #include "build/build_config.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 // 127 // For iOS, corresponding hardware can be found at 128 // https://deviceatlas.com/resources/clientside/ios-hardware-identification 129 static std::string HardwareModelName(); 130 131 // Returns the SOC manufacturer's name or an empty string if the manufacturer 132 // is unknown or an error occurred. 133 // e.g. "Google" on Pixel 8 Pro. Only implemented on Android, returns an 134 // empty string on other platforms. 135 static std::string SocManufacturer(); 136 137 #if BUILDFLAG(IS_MAC) 138 struct HardwareModelNameSplit { 139 std::string category; 140 int model = 0; 141 int variant = 0; 142 }; 143 // Hardware model names on the Mac are of the shape "Mac," where the 144 // prefix is the general category, the is the model, and the is the 145 // variant. This function takes the hardware model name as returned by 146 // HardwareModelName() above, and returns it split into its constituent parts. 147 // Returns nullopt if the value cannot be parsed. 148 // 149 // /!\ WARNING 150 // 151 // This is NOT A USEFUL FUNCTION and SHOULD NOT BE USED. While the `model` 152 // value does inform as to what generation of hardware it is within the 153 // `category`, this is not useful in determining the capabilities of the 154 // hardware. Instead of using the `model` value, check the actual capabilities 155 // of the hardware to verify what it can do rather than relying on a hardware 156 // model name. In addition, while the `category` value used to have meaning 157 // and could be used to determine the type of hardware (e.g. desktop vs 158 // laptop), in 2022 Apple started using the generic category of "Mac", thus 159 // removing its usefulness when used alone. While the entire model string as 160 // returned by HardwareModelName() above can be useful for identifying a 161 // specific piece of equipment, splitting apart it is not useful. 162 // 163 // Do not add any further callers! When the aforementioned 2022-era hardware 164 // is the minimum requirement for Chromium, remove this function and adjust 165 // all callers appropriately. 166 static std::optional<HardwareModelNameSplit> SplitHardwareModelNameDoNotUse( 167 std::string_view name); 168 #endif 169 170 struct HardwareInfo { 171 std::string manufacturer; 172 std::string model; 173 }; 174 // Returns via |callback| a struct containing descriptive UTF-8 strings for 175 // the current machine manufacturer and model, or empty strings if the 176 // information is unknown or an error occurred. Implemented on Windows, macOS, 177 // iOS, Linux, Chrome OS and Android. 178 static void GetHardwareInfo(base::OnceCallback<void(HardwareInfo)> callback); 179 180 // Returns the name of the host operating system. 181 static std::string OperatingSystemName(); 182 183 // Returns the version of the host operating system. 184 static std::string OperatingSystemVersion(); 185 186 // Retrieves detailed numeric values for the OS version. 187 // DON'T USE THIS ON THE MAC OR WINDOWS to determine the current OS release 188 // for OS version-specific feature checks and workarounds. If you must use an 189 // OS version check instead of a feature check, use 190 // base::mac::MacOSVersion()/MacOSMajorVersion() family from 191 // base/mac/mac_util.h, or base::win::GetVersion() from 192 // base/win/windows_version.h. 193 static void OperatingSystemVersionNumbers(int32_t* major_version, 194 int32_t* minor_version, 195 int32_t* bugfix_version); 196 197 // Returns the architecture of the running operating system. 198 // Exact return value may differ across platforms. 199 // e.g. a 32-bit x86 kernel on a 64-bit capable CPU will return "x86", 200 // whereas a x86-64 kernel on the same CPU will return "x86_64" 201 static std::string OperatingSystemArchitecture(); 202 203 // Returns the architecture of the running process, which might be different 204 // than the architecture returned by OperatingSystemArchitecture() (e.g. 205 // macOS Rosetta, a 32-bit binary on a 64-bit OS, etc). 206 // Will return one of: "x86", "x86_64", "ARM", "ARM_64", or an empty string if 207 // none of the above. 208 static std::string ProcessCPUArchitecture(); 209 210 // Returns the CPU model name of the system. If it can not be figured out, 211 // an empty string is returned. 212 // More detailed info can be obtained from base/cpu.h. 213 static std::string CPUModelName(); 214 215 // Return the smallest amount of memory (in bytes) which the VM system will 216 // allocate. 217 static size_t VMAllocationGranularity(); 218 219 #if BUILDFLAG(IS_CHROMEOS) 220 // Set |value| and return true if LsbRelease contains information about |key|. 221 static bool GetLsbReleaseValue(const std::string& key, std::string* value); 222 223 // Convenience function for GetLsbReleaseValue("CHROMEOS_RELEASE_BOARD",...). 224 // Returns "unknown" if CHROMEOS_RELEASE_BOARD is not set. Otherwise, returns 225 // the full name of the board. Note that the returned value often differs 226 // between developers' systems and devices that use official builds. E.g. for 227 // a developer-built image, the function could return 'glimmer', while in an 228 // official build, it may be something like 'glimmer-signed-mp-v4keys'. 229 // 230 // NOTE: Strings returned by this function should be treated as opaque values 231 // within Chrome (e.g. for reporting metrics elsewhere). If you need to make 232 // Chrome behave differently for different Chrome OS devices, either directly 233 // check for the hardware feature that you care about (preferred) or add a 234 // command-line flag to Chrome and pass it from session_manager (based on 235 // whether a USE flag is set or not). See https://goo.gl/BbBkzg for more 236 // details. 237 static std::string GetLsbReleaseBoard(); 238 239 // Returns the creation time of /etc/lsb-release. (Used to get the date and 240 // time of the Chrome OS build). 241 static Time GetLsbReleaseTime(); 242 243 // Returns true when actually running in a Chrome OS environment. 244 static bool IsRunningOnChromeOS(); 245 246 // Overrides |lsb_release| and |lsb_release_time|. Overrides cannot be nested. 247 // Call ResetChromeOSVersionInfoForTest() to restore the previous values. 248 // Prefer base::test::ScopedChromeOSVersionInfo to calling this function. 249 static void SetChromeOSVersionInfoForTest(const std::string& lsb_release, 250 const Time& lsb_release_time); 251 252 // Undoes the function above. 253 static void ResetChromeOSVersionInfoForTest(); 254 255 // Returns the kernel version of the host operating system. 256 static std::string KernelVersion(); 257 258 // Crashes if running on Chrome OS non-test image. Use only for really 259 // sensitive and risky use cases. Only works while running in verified mode, 260 // this check an easily be bypassed in dev mode. 261 static void CrashIfChromeOSNonTestImage(); 262 #endif // BUILDFLAG(IS_CHROMEOS) 263 264 #if BUILDFLAG(IS_ANDROID) 265 // Returns the Android build's codename. 266 static std::string GetAndroidBuildCodename(); 267 268 // Returns the Android build ID. 269 static std::string GetAndroidBuildID(); 270 271 // Returns the Android hardware EGL system property. 272 static std::string GetAndroidHardwareEGL(); 273 #endif // BUILDFLAG(IS_ANDROID) 274 275 #if BUILDFLAG(IS_IOS) 276 // Returns the iOS build number string which is normally an alphanumeric 277 // string like 12E456. This build number can differentiate between different 278 // versions of iOS that may have the same major/minor/bugfix version numbers. 279 // For example, iOS beta releases have the same version number but different 280 // build number strings. 281 static std::string GetIOSBuildNumber(); 282 283 // Overrides the hardware model name. The overridden value is used instead of 284 // `StringSysctl({CTL_HW, HW_MACHINE})`. `name` should not be empty. 285 static void OverrideHardwareModelName(std::string name); 286 #endif // BUILDFLAG(IS_IOS) 287 288 // Returns true for low-end devices that may require extreme tradeoffs, 289 // including user-visible changes, for acceptable performance. 290 // For general memory optimizations, consider |AmountOfPhysicalMemoryMB|. 291 // 292 // On Android this returns: 293 // true when memory <= 1GB on Android O and later. 294 // true when memory <= 512MB on Android N and earlier. 295 // This is not the same as "low-memory" and will be false on a large number of 296 // <=1GB pre-O Android devices. See: |detectLowEndDevice| in SysUtils.java. 297 // On Desktop this returns true when memory <= 2GB. 298 static bool IsLowEndDevice(); 299 300 // The same as IsLowEndDevice() except on Android. 301 // 302 // On Android this returns: 303 // true when IsLowEndDevice() returns true. 304 // true when the physical memory of the device is 4gb or 6gb and 305 // the feature: kPartialLowEndModeOnMidEndDevices() is enabled. 306 static bool IsLowEndDeviceOrPartialLowEndModeEnabled(); 307 static bool IsLowEndDeviceOrPartialLowEndModeEnabled( 308 const FeatureParam<bool>& param_for_exclusion); 309 310 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) 311 // Returns true for Android devices whose memory is X GB, considering 312 // carveouts. The carveouts is memory reserved by the system, e.g. 313 // for drivers, MTE, etc. It's very common for querying app to see 314 // hundreds MBs less than actual physical memory installed on the system. 315 // Addendum: This logic should also work for ChromeOS. 316 static bool Is3GbDevice(); 317 static bool Is4GbDevice(); 318 static bool Is6GbDevice(); 319 // Returns true for Android devices whose memory is 4GB or 6GB, considering 320 // carveouts. 321 static bool Is4GbOr6GbDevice(); 322 #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) 323 324 #if BUILDFLAG(IS_MAC) 325 // Indicates that CPU security mitigations are enabled for the current 326 // process. This is used to control the behavior of NumberOfProcessors(), see 327 // comment on that method. 328 static void SetCpuSecurityMitigationsEnabled(); 329 330 // Resets all state associated with CPU security mitigations. 331 static void ResetCpuSecurityMitigationsEnabledForTesting(); 332 #endif 333 334 private: 335 friend class test::ScopedAmountOfPhysicalMemoryOverride; 336 FRIEND_TEST_ALL_PREFIXES(SysInfoTest, AmountOfAvailablePhysicalMemory); 337 FRIEND_TEST_ALL_PREFIXES(debug::SystemMetricsTest, ParseMeminfo); 338 339 static int NumberOfEfficientProcessorsImpl(); 340 static uint64_t AmountOfPhysicalMemoryImpl(); 341 static uint64_t AmountOfAvailablePhysicalMemoryImpl(); 342 static bool IsLowEndDeviceImpl(); 343 static HardwareInfo GetHardwareInfoSync(); 344 345 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \ 346 BUILDFLAG(IS_AIX) 347 static uint64_t AmountOfAvailablePhysicalMemory( 348 const SystemMemoryInfoKB& meminfo); 349 #endif 350 351 // Sets the amount of physical memory in MB for testing, thus allowing tests 352 // to run irrespective of the host machine's configuration. 353 static std::optional<uint64_t> SetAmountOfPhysicalMemoryMbForTesting( 354 uint64_t amount_of_memory_mb); 355 static void ClearAmountOfPhysicalMemoryMbForTesting(); 356 }; 357 358 } // namespace base 359 360 #endif // BASE_SYSTEM_SYS_INFO_H_ 361