sys_info.h 8.7 KB

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