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/time/time.h"
17 #include "build/build_config.h"
18 
19 namespace base {
20 
21 class BASE_EXPORT SysInfo {
22  public:
23   // Return the number of logical processors/cores on the current machine.
24   static int NumberOfProcessors();
25 
26   // Return the number of bytes of physical memory on the current machine.
27   static int64_t AmountOfPhysicalMemory();
28 
29   // Return the number of bytes of current available physical memory on the
30   // machine.
31   static int64_t AmountOfAvailablePhysicalMemory();
32 
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 
38   // Return the number of megabytes of physical memory on the current machine.
AmountOfPhysicalMemoryMB()39   static int AmountOfPhysicalMemoryMB() {
40     return static_cast<int>(AmountOfPhysicalMemory() / 1024 / 1024);
41   }
42 
43   // Return the number of megabytes of available virtual memory, or zero if it
44   // is unlimited.
AmountOfVirtualMemoryMB()45   static int AmountOfVirtualMemoryMB() {
46     return static_cast<int>(AmountOfVirtualMemory() / 1024 / 1024);
47   }
48 
49   // Return the available disk space in bytes on the volume containing |path|,
50   // or -1 on failure.
51   static int64_t AmountOfFreeDiskSpace(const FilePath& path);
52 
53   // Returns system uptime.
54   static TimeDelta Uptime();
55 
56   // Returns a descriptive string for the current machine model or an empty
57   // string if the machine model is unknown or an error occured.
58   // e.g. "MacPro1,1" on Mac, or "Nexus 5" on Android. Only implemented on OS X,
59   // Android, and Chrome OS. This returns an empty string on other platforms.
60   static std::string HardwareModelName();
61 
62   // Returns the name of the host operating system.
63   static std::string OperatingSystemName();
64 
65   // Returns the version of the host operating system.
66   static std::string OperatingSystemVersion();
67 
68   // Retrieves detailed numeric values for the OS version.
69   // TODO(port): Implement a Linux version of this method and enable the
70   // corresponding unit test.
71   // DON'T USE THIS ON THE MAC OR WINDOWS to determine the current OS release
72   // for OS version-specific feature checks and workarounds. If you must use
73   // an OS version check instead of a feature check, use the base::mac::IsOS*
74   // family from base/mac/mac_util.h, or base::win::GetVersion from
75   // base/win/windows_version.h.
76   static void OperatingSystemVersionNumbers(int32_t* major_version,
77                                             int32_t* minor_version,
78                                             int32_t* bugfix_version);
79 
80   // Returns the architecture of the running operating system.
81   // Exact return value may differ across platforms.
82   // e.g. a 32-bit x86 kernel on a 64-bit capable CPU will return "x86",
83   //      whereas a x86-64 kernel on the same CPU will return "x86_64"
84   static std::string OperatingSystemArchitecture();
85 
86   // Avoid using this. Use base/cpu.h to get information about the CPU instead.
87   // http://crbug.com/148884
88   // Returns the CPU model name of the system. If it can not be figured out,
89   // an empty string is returned.
90   static std::string CPUModelName();
91 
92   // Return the smallest amount of memory (in bytes) which the VM system will
93   // allocate.
94   static size_t VMAllocationGranularity();
95 
96 #if defined(OS_POSIX) && !defined(OS_MACOSX)
97   // Returns the maximum SysV shared memory segment size, or zero if there is no
98   // limit.
99   static uint64_t MaxSharedMemorySize();
100 #endif  // defined(OS_POSIX) && !defined(OS_MACOSX)
101 
102 #if defined(OS_CHROMEOS)
103   typedef std::map<std::string, std::string> LsbReleaseMap;
104 
105   // Returns the contents of /etc/lsb-release as a map.
106   static const LsbReleaseMap& GetLsbReleaseMap();
107 
108   // If |key| is present in the LsbReleaseMap, sets |value| and returns true.
109   static bool GetLsbReleaseValue(const std::string& key, std::string* value);
110 
111   // Convenience function for GetLsbReleaseValue("CHROMEOS_RELEASE_BOARD",...).
112   // Returns "unknown" if CHROMEOS_RELEASE_BOARD is not set.
113   static std::string GetLsbReleaseBoard();
114 
115   // Returns the creation time of /etc/lsb-release. (Used to get the date and
116   // time of the Chrome OS build).
117   static Time GetLsbReleaseTime();
118 
119   // Returns true when actually running in a Chrome OS environment.
120   static bool IsRunningOnChromeOS();
121 
122   // Test method to force re-parsing of lsb-release.
123   static void SetChromeOSVersionInfoForTest(const std::string& lsb_release,
124                                             const Time& lsb_release_time);
125 #endif  // defined(OS_CHROMEOS)
126 
127 #if defined(OS_ANDROID)
128   // Returns the Android build's codename.
129   static std::string GetAndroidBuildCodename();
130 
131   // Returns the Android build ID.
132   static std::string GetAndroidBuildID();
133 
134   static int DalvikHeapSizeMB();
135   static int DalvikHeapGrowthLimitMB();
136 #endif  // defined(OS_ANDROID)
137 
138   // Returns true if this is a low-end device.
139   // Low-end device refers to devices having less than 512M memory in the
140   // current implementation.
141   static bool IsLowEndDevice();
142 };
143 
144 }  // namespace base
145 
146 #endif  // BASE_SYS_INFO_H_
147