sys_info.h 8.8 KB

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