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 5 #ifndef BASE_SYS_INFO_H_ 6 #define BASE_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/files/file_path.h" 16 #include "base/gtest_prod_util.h" 17 #include "base/time/time.h" 18 #include "build/build_config.h" 19 20 namespace base { 21 22 namespace debug { 23 FORWARD_DECLARE_TEST(SystemMetricsTest, ParseMeminfo); 24 } 25 26 struct SystemMemoryInfoKB; 27 28 class BASE_EXPORT SysInfo { 29 public: 30 // Return the number of logical processors/cores on the current machine. 31 static int NumberOfProcessors(); 32 33 // Return the number of bytes of physical memory on the current machine. 34 static int64_t AmountOfPhysicalMemory(); 35 36 // Return the number of bytes of current available physical memory on the 37 // machine. 38 // (The amount of memory that can be allocated without any significant 39 // impact on the system. It can lead to freeing inactive file-backed 40 // and/or speculative file-backed memory). 41 static int64_t AmountOfAvailablePhysicalMemory(); 42 43 // Return the number of bytes of virtual memory of this process. A return 44 // value of zero means that there is no limit on the available virtual 45 // memory. 46 static int64_t AmountOfVirtualMemory(); 47 48 // Return the number of megabytes of physical memory on the current machine. 49 static int AmountOfPhysicalMemoryMB() { 50 return static_cast<int>(AmountOfPhysicalMemory() / 1024 / 1024); 51 } 52 53 // Return the number of megabytes of available virtual memory, or zero if it 54 // is unlimited. 55 static int AmountOfVirtualMemoryMB() { 56 return static_cast<int>(AmountOfVirtualMemory() / 1024 / 1024); 57 } 58 59 // Return the available disk space in bytes on the volume containing |path|, 60 // or -1 on failure. 61 static int64_t AmountOfFreeDiskSpace(const FilePath& path); 62 63 // Return the total disk space in bytes on the volume containing |path|, or -1 64 // on failure. 65 static int64_t AmountOfTotalDiskSpace(const FilePath& path); 66 67 // Returns system uptime. 68 static TimeDelta Uptime(); 69 70 // Returns a descriptive string for the current machine model or an empty 71 // string if the machine model is unknown or an error occured. 72 // e.g. "MacPro1,1" on Mac, or "Nexus 5" on Android. Only implemented on OS X, 73 // Android, and Chrome OS. This returns an empty string on other platforms. 74 static std::string HardwareModelName(); 75 76 // Returns the name of the host operating system. 77 static std::string OperatingSystemName(); 78 79 // Returns the version of the host operating system. 80 static std::string OperatingSystemVersion(); 81 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 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 98 // Avoid using this. Use base/cpu.h to get information about the CPU instead. 99 // http://crbug.com/148884 100 // Returns the CPU model name of the system. If it can not be figured out, 101 // an empty string is returned. 102 static std::string CPUModelName(); 103 104 // Return the smallest amount of memory (in bytes) which the VM system will 105 // allocate. 106 static size_t VMAllocationGranularity(); 107 108 #if defined(OS_CHROMEOS) 109 typedef std::map<std::string, std::string> LsbReleaseMap; 110 111 // Returns the contents of /etc/lsb-release as a map. 112 static const LsbReleaseMap& GetLsbReleaseMap(); 113 114 // If |key| is present in the LsbReleaseMap, sets |value| and returns true. 115 static bool GetLsbReleaseValue(const std::string& key, std::string* value); 116 117 // Convenience function for GetLsbReleaseValue("CHROMEOS_RELEASE_BOARD",...). 118 // Returns "unknown" if CHROMEOS_RELEASE_BOARD is not set. Otherwise returns 119 // the full name of the board. WARNING: the returned value often differs in 120 // developer built system compared to devices that use the official version. 121 // E.g. for developer built version, the function could return 'glimmer' while 122 // for officially used versions it would be like 'glimmer-signed-mp-v4keys'. 123 // Use GetStrippedReleaseBoard() function if you need only the short name of 124 // the board (would be 'glimmer' in the case described above). 125 static std::string GetLsbReleaseBoard(); 126 127 // Convenience function for GetLsbReleaseBoard() removing trailing "-signed-*" 128 // if present. Returns "unknown" if CHROMEOS_RELEASE_BOARD is not set. 129 static std::string GetStrippedReleaseBoard(); 130 131 // Returns the creation time of /etc/lsb-release. (Used to get the date and 132 // time of the Chrome OS build). 133 static Time GetLsbReleaseTime(); 134 135 // Returns true when actually running in a Chrome OS environment. 136 static bool IsRunningOnChromeOS(); 137 138 // Test method to force re-parsing of lsb-release. 139 static void SetChromeOSVersionInfoForTest(const std::string& lsb_release, 140 const Time& lsb_release_time); 141 #endif // defined(OS_CHROMEOS) 142 143 #if defined(OS_ANDROID) 144 // Returns the Android build's codename. 145 static std::string GetAndroidBuildCodename(); 146 147 // Returns the Android build ID. 148 static std::string GetAndroidBuildID(); 149 150 static int DalvikHeapSizeMB(); 151 static int DalvikHeapGrowthLimitMB(); 152 #endif // defined(OS_ANDROID) 153 154 // Returns true if this is a low-end device. 155 // Low-end device refers to devices having less than 512M memory in the 156 // current implementation. 157 static bool IsLowEndDevice(); 158 159 private: 160 FRIEND_TEST_ALL_PREFIXES(SysInfoTest, AmountOfAvailablePhysicalMemory); 161 FRIEND_TEST_ALL_PREFIXES(debug::SystemMetricsTest, ParseMeminfo); 162 163 #if defined(OS_LINUX) || defined(OS_ANDROID) 164 static int64_t AmountOfAvailablePhysicalMemory( 165 const SystemMemoryInfoKB& meminfo); 166 #endif 167 }; 168 169 } // namespace base 170 171 #endif // BASE_SYS_INFO_H_ 172