blob: 6e58715bf5e68fdee5edfaa6ee424e163f38946a [file] [log] [blame]
khorimoto@chromium.orgc7e4e002012-04-25 10:59:44 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
deanm@chromium.org42a34eb2008-09-17 19:09:39 +09002// 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
avia6a6a682015-12-27 07:15:14 +09008#include <stddef.h>
avi95a2f372015-12-09 09:44:49 +09009#include <stdint.h>
10
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090011#include <map>
thestig@chromium.orgc9137242012-09-26 11:49:42 +090012#include <string>
13
darin@chromium.orge585bed2011-08-06 00:34:00 +090014#include "base/base_export.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090015#include "base/files/file_path.h"
mkolomaf6f3f82017-03-22 10:47:29 +090016#include "base/gtest_prod_util.h"
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +090017#include "base/time/time.h"
thestig@chromium.orgc9137242012-09-26 11:49:42 +090018#include "build/build_config.h"
tony@chromium.org1e25e002009-10-14 09:41:56 +090019
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090020namespace base {
21
mkolomaf6f3f82017-03-22 10:47:29 +090022namespace debug {
23FORWARD_DECLARE_TEST(SystemMetricsTest, ParseMeminfo);
24}
25
26struct SystemMemoryInfoKB;
27
darin@chromium.orge585bed2011-08-06 00:34:00 +090028class BASE_EXPORT SysInfo {
deanm@chromium.org42a34eb2008-09-17 19:09:39 +090029 public:
30 // Return the number of logical processors/cores on the current machine.
31 static int NumberOfProcessors();
jeremy@chromium.org0da11fa2009-02-11 11:01:51 +090032
deanm@chromium.org319f0442008-09-17 22:10:45 +090033 // Return the number of bytes of physical memory on the current machine.
avi95a2f372015-12-09 09:44:49 +090034 static int64_t AmountOfPhysicalMemory();
deanm@chromium.org530ddd92008-09-18 21:18:14 +090035
hongbo.min@intel.comf6312b12012-10-27 15:10:37 +090036 // Return the number of bytes of current available physical memory on the
37 // machine.
mkolomaf6f3f82017-03-22 10:47:29 +090038 // (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).
avi95a2f372015-12-09 09:44:49 +090041 static int64_t AmountOfAvailablePhysicalMemory();
hongbo.min@intel.comf6312b12012-10-27 15:10:37 +090042
jochen@chromium.org5ac0b172014-04-09 08:19:16 +090043 // 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.
avi95a2f372015-12-09 09:44:49 +090046 static int64_t AmountOfVirtualMemory();
jochen@chromium.org5ac0b172014-04-09 08:19:16 +090047
deanm@chromium.org530ddd92008-09-18 21:18:14 +090048 // 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 }
deanm@chromium.orgee45e8f2008-09-18 21:34:24 +090052
jochen@chromium.org5ac0b172014-04-09 08:19:16 +090053 // 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
rvargas@google.comaf000292008-09-20 10:16:23 +090059 // Return the available disk space in bytes on the volume containing |path|,
60 // or -1 on failure.
avi95a2f372015-12-09 09:44:49 +090061 static int64_t AmountOfFreeDiskSpace(const FilePath& path);
deanm@chromium.orgee45e8f2008-09-18 21:34:24 +090062
fukino1892bc22016-06-11 04:35:55 +090063 // 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
squebabf6732015-11-05 09:01:03 +090067 // Returns system uptime.
68 static TimeDelta Uptime();
jeremy@chromium.orgde658ed2012-10-29 10:04:09 +090069
jeremyb43af092014-11-07 03:47:10 +090070 // Returns a descriptive string for the current machine model or an empty
tdresserf3933692015-07-17 00:41:04 +090071 // string if the machine model is unknown or an error occured.
asvitkine6a7c7722017-05-05 07:52:00 +090072 // e.g. "MacPro1,1" on Mac, "iPhone9,3" on iOS or "Nexus 5" on Android. Only
73 // implemented on OS X, iOS, Android, and Chrome OS. This returns an empty
74 // string on other platforms.
jeremyb43af092014-11-07 03:47:10 +090075 static std::string HardwareModelName();
76
mark@chromium.orgb93c0542008-09-30 07:18:01 +090077 // Returns the name of the host operating system.
78 static std::string OperatingSystemName();
79
80 // Returns the version of the host operating system.
81 static std::string OperatingSystemVersion();
82
jeremy@chromium.org7730edb2009-02-25 01:37:13 +090083 // Retrieves detailed numeric values for the OS version.
mark@chromium.org71cce472011-06-15 03:18:38 +090084 // 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.
avi95a2f372015-12-09 09:44:49 +090089 static void OperatingSystemVersionNumbers(int32_t* major_version,
90 int32_t* minor_version,
91 int32_t* bugfix_version);
jeremy@chromium.org7730edb2009-02-25 01:37:13 +090092
thestig@chromium.orgc9137242012-09-26 11:49:42 +090093 // Returns the architecture of the running operating system.
94 // Exact return value may differ across platforms.
95 // e.g. a 32-bit x86 kernel on a 64-bit capable CPU will return "x86",
96 // whereas a x86-64 kernel on the same CPU will return "x86_64"
thestig@chromium.orge1706652012-10-16 10:58:21 +090097 static std::string OperatingSystemArchitecture();
mark@chromium.orgb93c0542008-09-30 07:18:01 +090098
thestig@chromium.orgc9137242012-09-26 11:49:42 +090099 // Avoid using this. Use base/cpu.h to get information about the CPU instead.
100 // http://crbug.com/148884
hongbo.min@intel.com893f7d62012-09-08 18:53:01 +0900101 // Returns the CPU model name of the system. If it can not be figured out,
102 // an empty string is returned.
103 static std::string CPUModelName();
104
agl@chromium.org3d71fda2009-01-22 10:42:15 +0900105 // Return the smallest amount of memory (in bytes) which the VM system will
106 // allocate.
107 static size_t VMAllocationGranularity();
evan@chromium.org54b72102009-07-22 09:35:18 +0900108
109#if defined(OS_CHROMEOS)
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900110 typedef std::map<std::string, std::string> LsbReleaseMap;
evan@chromium.org54b72102009-07-22 09:35:18 +0900111
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900112 // Returns the contents of /etc/lsb-release as a map.
113 static const LsbReleaseMap& GetLsbReleaseMap();
khorimoto@chromium.orgc7e4e002012-04-25 10:59:44 +0900114
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900115 // If |key| is present in the LsbReleaseMap, sets |value| and returns true.
116 static bool GetLsbReleaseValue(const std::string& key, std::string* value);
117
118 // Convenience function for GetLsbReleaseValue("CHROMEOS_RELEASE_BOARD",...).
Daniel Eratd1337802017-08-11 13:06:59 +0900119 // Returns "unknown" if CHROMEOS_RELEASE_BOARD is not set. Otherwise, returns
120 // the full name of the board. Note that the returned value often differs
121 // between developers' systems and devices that use official builds. E.g. for
122 // a developer-built image, the function could return 'glimmer', while in an
123 // official build, it may be something like 'glimmer-signed-mp-v4keys'.
124 //
125 // NOTE: Strings returned by this function should be treated as opaque values
126 // within Chrome (e.g. for reporting metrics elsewhere). If you need to make
127 // Chrome behave differently for different Chrome OS devices, either directly
128 // check for the hardware feature that you care about (preferred) or add a
129 // command-line flag to Chrome and pass it from session_manager (based on
130 // whether a USE flag is set or not). See https://goo.gl/BbBkzg for more
131 // details.
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900132 static std::string GetLsbReleaseBoard();
133
Daniel Eratd1337802017-08-11 13:06:59 +0900134 // DEPRECATED: Please see GetLsbReleaseBoard's comment.
igorcov352c1342016-12-02 04:42:32 +0900135 // Convenience function for GetLsbReleaseBoard() removing trailing "-signed-*"
136 // if present. Returns "unknown" if CHROMEOS_RELEASE_BOARD is not set.
Daniel Eratd1337802017-08-11 13:06:59 +0900137 // TODO(derat): Delete this after October 2017.
igorcov352c1342016-12-02 04:42:32 +0900138 static std::string GetStrippedReleaseBoard();
139
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900140 // Returns the creation time of /etc/lsb-release. (Used to get the date and
141 // time of the Chrome OS build).
142 static Time GetLsbReleaseTime();
143
stevenjb@chromium.org861313b2013-09-28 04:28:24 +0900144 // Returns true when actually running in a Chrome OS environment.
145 static bool IsRunningOnChromeOS();
146
stevenjb@chromium.org2f4de042013-09-27 06:51:23 +0900147 // Test method to force re-parsing of lsb-release.
148 static void SetChromeOSVersionInfoForTest(const std::string& lsb_release,
149 const Time& lsb_release_time);
thestig@chromium.orgc9137242012-09-26 11:49:42 +0900150#endif // defined(OS_CHROMEOS)
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900151
152#if defined(OS_ANDROID)
dfalcantara@chromium.org774888d2012-08-22 08:55:52 +0900153 // Returns the Android build's codename.
154 static std::string GetAndroidBuildCodename();
155
156 // Returns the Android build ID.
157 static std::string GetAndroidBuildID();
158
ulan@chromium.org8ac7ea02012-05-26 00:31:37 +0900159 static int DalvikHeapSizeMB();
epenner@chromium.org066b81b2013-02-17 03:15:50 +0900160 static int DalvikHeapGrowthLimitMB();
thestig@chromium.orgc9137242012-09-26 11:49:42 +0900161#endif // defined(OS_ANDROID)
c.shu@samsung.comda21aff2014-06-27 01:55:27 +0900162
163 // Returns true if this is a low-end device.
Chris Harrelson52a4da52017-07-25 06:47:31 +0900164 // Low-end device refers to devices having a very low amount of total
165 // system memory, typically <= 1GB.
166 // See also SysUtils.java, method isLowEndDevice.
c.shu@samsung.comda21aff2014-06-27 01:55:27 +0900167 static bool IsLowEndDevice();
mkolomaf6f3f82017-03-22 10:47:29 +0900168
169 private:
170 FRIEND_TEST_ALL_PREFIXES(SysInfoTest, AmountOfAvailablePhysicalMemory);
171 FRIEND_TEST_ALL_PREFIXES(debug::SystemMetricsTest, ParseMeminfo);
172
Eric Karl30097182017-07-26 10:18:37 +0900173 static int64_t AmountOfPhysicalMemoryImpl();
174 static int64_t AmountOfAvailablePhysicalMemoryImpl();
Eric Karl88a92992018-03-14 01:34:03 +0900175 static bool IsLowEndDeviceImpl();
Eric Karl30097182017-07-26 10:18:37 +0900176
rayb157aae32017-04-27 07:35:08 +0900177#if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX)
mkolomaf6f3f82017-03-22 10:47:29 +0900178 static int64_t AmountOfAvailablePhysicalMemory(
179 const SystemMemoryInfoKB& meminfo);
180#endif
deanm@chromium.org42a34eb2008-09-17 19:09:39 +0900181};
182
183} // namespace base
184
185#endif // BASE_SYS_INFO_H_