xref: /aosp_15_r20/external/libchrome/base/sys_info.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_SYS_INFO_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_SYS_INFO_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include <map>
12*635a8641SAndroid Build Coastguard Worker #include <string>
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/files/file_path.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/gtest_prod_util.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h"
18*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
19*635a8641SAndroid Build Coastguard Worker 
20*635a8641SAndroid Build Coastguard Worker namespace base {
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker namespace debug {
23*635a8641SAndroid Build Coastguard Worker FORWARD_DECLARE_TEST(SystemMetricsTest, ParseMeminfo);
24*635a8641SAndroid Build Coastguard Worker }
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker struct SystemMemoryInfoKB;
27*635a8641SAndroid Build Coastguard Worker 
28*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT SysInfo {
29*635a8641SAndroid Build Coastguard Worker  public:
30*635a8641SAndroid Build Coastguard Worker   // Return the number of logical processors/cores on the current machine.
31*635a8641SAndroid Build Coastguard Worker   static int NumberOfProcessors();
32*635a8641SAndroid Build Coastguard Worker 
33*635a8641SAndroid Build Coastguard Worker   // Return the number of bytes of physical memory on the current machine.
34*635a8641SAndroid Build Coastguard Worker   static int64_t AmountOfPhysicalMemory();
35*635a8641SAndroid Build Coastguard Worker 
36*635a8641SAndroid Build Coastguard Worker   // Return the number of bytes of current available physical memory on the
37*635a8641SAndroid Build Coastguard Worker   // machine.
38*635a8641SAndroid Build Coastguard Worker   // (The amount of memory that can be allocated without any significant
39*635a8641SAndroid Build Coastguard Worker   // impact on the system. It can lead to freeing inactive file-backed
40*635a8641SAndroid Build Coastguard Worker   // and/or speculative file-backed memory).
41*635a8641SAndroid Build Coastguard Worker   static int64_t AmountOfAvailablePhysicalMemory();
42*635a8641SAndroid Build Coastguard Worker 
43*635a8641SAndroid Build Coastguard Worker   // Return the number of bytes of virtual memory of this process. A return
44*635a8641SAndroid Build Coastguard Worker   // value of zero means that there is no limit on the available virtual
45*635a8641SAndroid Build Coastguard Worker   // memory.
46*635a8641SAndroid Build Coastguard Worker   static int64_t AmountOfVirtualMemory();
47*635a8641SAndroid Build Coastguard Worker 
48*635a8641SAndroid Build Coastguard Worker   // Return the number of megabytes of physical memory on the current machine.
AmountOfPhysicalMemoryMB()49*635a8641SAndroid Build Coastguard Worker   static int AmountOfPhysicalMemoryMB() {
50*635a8641SAndroid Build Coastguard Worker     return static_cast<int>(AmountOfPhysicalMemory() / 1024 / 1024);
51*635a8641SAndroid Build Coastguard Worker   }
52*635a8641SAndroid Build Coastguard Worker 
53*635a8641SAndroid Build Coastguard Worker   // Return the number of megabytes of available virtual memory, or zero if it
54*635a8641SAndroid Build Coastguard Worker   // is unlimited.
AmountOfVirtualMemoryMB()55*635a8641SAndroid Build Coastguard Worker   static int AmountOfVirtualMemoryMB() {
56*635a8641SAndroid Build Coastguard Worker     return static_cast<int>(AmountOfVirtualMemory() / 1024 / 1024);
57*635a8641SAndroid Build Coastguard Worker   }
58*635a8641SAndroid Build Coastguard Worker 
59*635a8641SAndroid Build Coastguard Worker   // Return the available disk space in bytes on the volume containing |path|,
60*635a8641SAndroid Build Coastguard Worker   // or -1 on failure.
61*635a8641SAndroid Build Coastguard Worker   static int64_t AmountOfFreeDiskSpace(const FilePath& path);
62*635a8641SAndroid Build Coastguard Worker 
63*635a8641SAndroid Build Coastguard Worker   // Return the total disk space in bytes on the volume containing |path|, or -1
64*635a8641SAndroid Build Coastguard Worker   // on failure.
65*635a8641SAndroid Build Coastguard Worker   static int64_t AmountOfTotalDiskSpace(const FilePath& path);
66*635a8641SAndroid Build Coastguard Worker 
67*635a8641SAndroid Build Coastguard Worker   // Returns system uptime.
68*635a8641SAndroid Build Coastguard Worker   static TimeDelta Uptime();
69*635a8641SAndroid Build Coastguard Worker 
70*635a8641SAndroid Build Coastguard Worker   // Returns a descriptive string for the current machine model or an empty
71*635a8641SAndroid Build Coastguard Worker   // string if the machine model is unknown or an error occured.
72*635a8641SAndroid Build Coastguard Worker   // e.g. "MacPro1,1" on Mac, "iPhone9,3" on iOS or "Nexus 5" on Android. Only
73*635a8641SAndroid Build Coastguard Worker   // implemented on OS X, iOS, Android, and Chrome OS. This returns an empty
74*635a8641SAndroid Build Coastguard Worker   // string on other platforms.
75*635a8641SAndroid Build Coastguard Worker   static std::string HardwareModelName();
76*635a8641SAndroid Build Coastguard Worker 
77*635a8641SAndroid Build Coastguard Worker   // Returns the name of the host operating system.
78*635a8641SAndroid Build Coastguard Worker   static std::string OperatingSystemName();
79*635a8641SAndroid Build Coastguard Worker 
80*635a8641SAndroid Build Coastguard Worker   // Returns the version of the host operating system.
81*635a8641SAndroid Build Coastguard Worker   static std::string OperatingSystemVersion();
82*635a8641SAndroid Build Coastguard Worker 
83*635a8641SAndroid Build Coastguard Worker   // Retrieves detailed numeric values for the OS version.
84*635a8641SAndroid Build Coastguard Worker   // DON'T USE THIS ON THE MAC OR WINDOWS to determine the current OS release
85*635a8641SAndroid Build Coastguard Worker   // for OS version-specific feature checks and workarounds. If you must use
86*635a8641SAndroid Build Coastguard Worker   // an OS version check instead of a feature check, use the base::mac::IsOS*
87*635a8641SAndroid Build Coastguard Worker   // family from base/mac/mac_util.h, or base::win::GetVersion from
88*635a8641SAndroid Build Coastguard Worker   // base/win/windows_version.h.
89*635a8641SAndroid Build Coastguard Worker   static void OperatingSystemVersionNumbers(int32_t* major_version,
90*635a8641SAndroid Build Coastguard Worker                                             int32_t* minor_version,
91*635a8641SAndroid Build Coastguard Worker                                             int32_t* bugfix_version);
92*635a8641SAndroid Build Coastguard Worker 
93*635a8641SAndroid Build Coastguard Worker   // Returns the architecture of the running operating system.
94*635a8641SAndroid Build Coastguard Worker   // Exact return value may differ across platforms.
95*635a8641SAndroid Build Coastguard Worker   // e.g. a 32-bit x86 kernel on a 64-bit capable CPU will return "x86",
96*635a8641SAndroid Build Coastguard Worker   //      whereas a x86-64 kernel on the same CPU will return "x86_64"
97*635a8641SAndroid Build Coastguard Worker   static std::string OperatingSystemArchitecture();
98*635a8641SAndroid Build Coastguard Worker 
99*635a8641SAndroid Build Coastguard Worker   // Avoid using this. Use base/cpu.h to get information about the CPU instead.
100*635a8641SAndroid Build Coastguard Worker   // http://crbug.com/148884
101*635a8641SAndroid Build Coastguard Worker   // Returns the CPU model name of the system. If it can not be figured out,
102*635a8641SAndroid Build Coastguard Worker   // an empty string is returned.
103*635a8641SAndroid Build Coastguard Worker   static std::string CPUModelName();
104*635a8641SAndroid Build Coastguard Worker 
105*635a8641SAndroid Build Coastguard Worker   // Return the smallest amount of memory (in bytes) which the VM system will
106*635a8641SAndroid Build Coastguard Worker   // allocate.
107*635a8641SAndroid Build Coastguard Worker   static size_t VMAllocationGranularity();
108*635a8641SAndroid Build Coastguard Worker 
109*635a8641SAndroid Build Coastguard Worker #if defined(OS_CHROMEOS)
110*635a8641SAndroid Build Coastguard Worker   typedef std::map<std::string, std::string> LsbReleaseMap;
111*635a8641SAndroid Build Coastguard Worker 
112*635a8641SAndroid Build Coastguard Worker   // Returns the contents of /etc/lsb-release as a map.
113*635a8641SAndroid Build Coastguard Worker   static const LsbReleaseMap& GetLsbReleaseMap();
114*635a8641SAndroid Build Coastguard Worker 
115*635a8641SAndroid Build Coastguard Worker   // If |key| is present in the LsbReleaseMap, sets |value| and returns true.
116*635a8641SAndroid Build Coastguard Worker   static bool GetLsbReleaseValue(const std::string& key, std::string* value);
117*635a8641SAndroid Build Coastguard Worker 
118*635a8641SAndroid Build Coastguard Worker   // Convenience function for GetLsbReleaseValue("CHROMEOS_RELEASE_BOARD",...).
119*635a8641SAndroid Build Coastguard Worker   // Returns "unknown" if CHROMEOS_RELEASE_BOARD is not set. Otherwise, returns
120*635a8641SAndroid Build Coastguard Worker   // the full name of the board. Note that the returned value often differs
121*635a8641SAndroid Build Coastguard Worker   // between developers' systems and devices that use official builds. E.g. for
122*635a8641SAndroid Build Coastguard Worker   // a developer-built image, the function could return 'glimmer', while in an
123*635a8641SAndroid Build Coastguard Worker   // official build, it may be something like 'glimmer-signed-mp-v4keys'.
124*635a8641SAndroid Build Coastguard Worker   //
125*635a8641SAndroid Build Coastguard Worker   // NOTE: Strings returned by this function should be treated as opaque values
126*635a8641SAndroid Build Coastguard Worker   // within Chrome (e.g. for reporting metrics elsewhere). If you need to make
127*635a8641SAndroid Build Coastguard Worker   // Chrome behave differently for different Chrome OS devices, either directly
128*635a8641SAndroid Build Coastguard Worker   // check for the hardware feature that you care about (preferred) or add a
129*635a8641SAndroid Build Coastguard Worker   // command-line flag to Chrome and pass it from session_manager (based on
130*635a8641SAndroid Build Coastguard Worker   // whether a USE flag is set or not). See https://goo.gl/BbBkzg for more
131*635a8641SAndroid Build Coastguard Worker   // details.
132*635a8641SAndroid Build Coastguard Worker   static std::string GetLsbReleaseBoard();
133*635a8641SAndroid Build Coastguard Worker 
134*635a8641SAndroid Build Coastguard Worker   // Returns the creation time of /etc/lsb-release. (Used to get the date and
135*635a8641SAndroid Build Coastguard Worker   // time of the Chrome OS build).
136*635a8641SAndroid Build Coastguard Worker   static Time GetLsbReleaseTime();
137*635a8641SAndroid Build Coastguard Worker 
138*635a8641SAndroid Build Coastguard Worker   // Returns true when actually running in a Chrome OS environment.
139*635a8641SAndroid Build Coastguard Worker   static bool IsRunningOnChromeOS();
140*635a8641SAndroid Build Coastguard Worker 
141*635a8641SAndroid Build Coastguard Worker   // Test method to force re-parsing of lsb-release.
142*635a8641SAndroid Build Coastguard Worker   static void SetChromeOSVersionInfoForTest(const std::string& lsb_release,
143*635a8641SAndroid Build Coastguard Worker                                             const Time& lsb_release_time);
144*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_CHROMEOS)
145*635a8641SAndroid Build Coastguard Worker 
146*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
147*635a8641SAndroid Build Coastguard Worker   // Returns the Android build's codename.
148*635a8641SAndroid Build Coastguard Worker   static std::string GetAndroidBuildCodename();
149*635a8641SAndroid Build Coastguard Worker 
150*635a8641SAndroid Build Coastguard Worker   // Returns the Android build ID.
151*635a8641SAndroid Build Coastguard Worker   static std::string GetAndroidBuildID();
152*635a8641SAndroid Build Coastguard Worker 
153*635a8641SAndroid Build Coastguard Worker   static int DalvikHeapSizeMB();
154*635a8641SAndroid Build Coastguard Worker   static int DalvikHeapGrowthLimitMB();
155*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_ANDROID)
156*635a8641SAndroid Build Coastguard Worker 
157*635a8641SAndroid Build Coastguard Worker   // Returns true if this is a low-end device.
158*635a8641SAndroid Build Coastguard Worker   // Low-end device refers to devices having a very low amount of total
159*635a8641SAndroid Build Coastguard Worker   // system memory, typically <= 1GB.
160*635a8641SAndroid Build Coastguard Worker   // See also SysUtils.java, method isLowEndDevice.
161*635a8641SAndroid Build Coastguard Worker   static bool IsLowEndDevice();
162*635a8641SAndroid Build Coastguard Worker 
163*635a8641SAndroid Build Coastguard Worker  private:
164*635a8641SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(SysInfoTest, AmountOfAvailablePhysicalMemory);
165*635a8641SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(debug::SystemMetricsTest, ParseMeminfo);
166*635a8641SAndroid Build Coastguard Worker 
167*635a8641SAndroid Build Coastguard Worker   static int64_t AmountOfPhysicalMemoryImpl();
168*635a8641SAndroid Build Coastguard Worker   static int64_t AmountOfAvailablePhysicalMemoryImpl();
169*635a8641SAndroid Build Coastguard Worker   static bool IsLowEndDeviceImpl();
170*635a8641SAndroid Build Coastguard Worker 
171*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX)
172*635a8641SAndroid Build Coastguard Worker   static int64_t AmountOfAvailablePhysicalMemory(
173*635a8641SAndroid Build Coastguard Worker       const SystemMemoryInfoKB& meminfo);
174*635a8641SAndroid Build Coastguard Worker #endif
175*635a8641SAndroid Build Coastguard Worker };
176*635a8641SAndroid Build Coastguard Worker 
177*635a8641SAndroid Build Coastguard Worker }  // namespace base
178*635a8641SAndroid Build Coastguard Worker 
179*635a8641SAndroid Build Coastguard Worker #endif  // BASE_SYS_INFO_H_
180