[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/sys_info.h" |
| 6 | |
| 7 | #include <errno.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 10 | #include <string.h> |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 11 | #include <sys/param.h> |
[email protected] | 275a29de | 2014-04-08 23:19:16 | [diff] [blame] | 12 | #include <sys/resource.h> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 13 | #include <sys/utsname.h> |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 14 | #include <unistd.h> |
| 15 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 16 | #include "base/files/file_util.h" |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 17 | #include "base/lazy_instance.h" |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 18 | #include "base/logging.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 19 | #include "base/strings/utf_string_conversions.h" |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 20 | #include "base/sys_info_internal.h" |
[email protected] | dda29c94 | 2012-08-21 19:50:03 | [diff] [blame] | 21 | #include "base/threading/thread_restrictions.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 22 | #include "build/build_config.h" |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 23 | |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 24 | #if defined(OS_ANDROID) |
| 25 | #include <sys/vfs.h> |
| 26 | #define statvfs statfs // Android uses a statvfs-like statfs struct and call. |
| 27 | #else |
| 28 | #include <sys/statvfs.h> |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 29 | #endif |
| 30 | |
sriramsr | 9bbda429 | 2016-08-02 22:42:31 | [diff] [blame] | 31 | #if defined(OS_LINUX) |
| 32 | #include <linux/magic.h> |
| 33 | #include <sys/vfs.h> |
| 34 | #endif |
| 35 | |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 36 | namespace { |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 37 | |
scottmg | aaa608f | 2017-05-24 05:56:09 | [diff] [blame] | 38 | #if !defined(OS_OPENBSD) && !defined(OS_FUCHSIA) |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 39 | int NumberOfProcessors() { |
[email protected] | d617e8b | 2014-08-18 04:52:48 | [diff] [blame] | 40 | // sysconf returns the number of "logical" (not "physical") processors on both |
| 41 | // Mac and Linux. So we get the number of max available "logical" processors. |
| 42 | // |
| 43 | // Note that the number of "currently online" processors may be fewer than the |
| 44 | // returned value of NumberOfProcessors(). On some platforms, the kernel may |
| 45 | // make some processors offline intermittently, to save power when system |
| 46 | // loading is low. |
| 47 | // |
| 48 | // One common use case that needs to know the processor count is to create |
| 49 | // optimal number of threads for optimization. It should make plan according |
| 50 | // to the number of "max available" processors instead of "currently online" |
| 51 | // ones. The kernel should be smart enough to make all processors online when |
| 52 | // it has sufficient number of threads waiting to run. |
| 53 | long res = sysconf(_SC_NPROCESSORS_CONF); |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 54 | if (res == -1) { |
| 55 | NOTREACHED(); |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | return static_cast<int>(res); |
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 60 | } |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 61 | |
phoglund | 5b052f0 | 2016-07-21 12:41:04 | [diff] [blame] | 62 | base::LazyInstance< |
| 63 | base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 64 | g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER; |
Wez | 8f8498b3 | 2017-08-18 04:26:34 | [diff] [blame] | 65 | #endif // !defined(OS_OPENBSD) && !defined(OS_FUCHSIA) |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 66 | |
Wez | 8f8498b3 | 2017-08-18 04:26:34 | [diff] [blame] | 67 | #if !defined(OS_FUCHSIA) |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 68 | int64_t AmountOfVirtualMemory() { |
[email protected] | 275a29de | 2014-04-08 23:19:16 | [diff] [blame] | 69 | struct rlimit limit; |
| 70 | int result = getrlimit(RLIMIT_DATA, &limit); |
| 71 | if (result != 0) { |
| 72 | NOTREACHED(); |
| 73 | return 0; |
| 74 | } |
| 75 | return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; |
| 76 | } |
| 77 | |
phoglund | 5b052f0 | 2016-07-21 12:41:04 | [diff] [blame] | 78 | base::LazyInstance< |
| 79 | base::internal::LazySysInfoValue<int64_t, AmountOfVirtualMemory>>::Leaky |
[email protected] | 275a29de | 2014-04-08 23:19:16 | [diff] [blame] | 80 | g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; |
Wez | 8f8498b3 | 2017-08-18 04:26:34 | [diff] [blame] | 81 | #endif // !defined(OS_FUCHSIA) |
[email protected] | 275a29de | 2014-04-08 23:19:16 | [diff] [blame] | 82 | |
sriramsr | 9bbda429 | 2016-08-02 22:42:31 | [diff] [blame] | 83 | #if defined(OS_LINUX) |
| 84 | bool IsStatsZeroIfUnlimited(const base::FilePath& path) { |
| 85 | struct statfs stats; |
| 86 | |
| 87 | if (HANDLE_EINTR(statfs(path.value().c_str(), &stats)) != 0) |
| 88 | return false; |
| 89 | |
| 90 | switch (stats.f_type) { |
| 91 | case TMPFS_MAGIC: |
| 92 | case HUGETLBFS_MAGIC: |
| 93 | case RAMFS_MAGIC: |
| 94 | return true; |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | #endif |
| 99 | |
fukino | 50a4481 | 2016-06-10 19:35:55 | [diff] [blame] | 100 | bool GetDiskSpaceInfo(const base::FilePath& path, |
| 101 | int64_t* available_bytes, |
| 102 | int64_t* total_bytes) { |
| 103 | struct statvfs stats; |
| 104 | if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) |
| 105 | return false; |
| 106 | |
sriramsr | 9bbda429 | 2016-08-02 22:42:31 | [diff] [blame] | 107 | #if defined(OS_LINUX) |
| 108 | const bool zero_size_means_unlimited = |
| 109 | stats.f_blocks == 0 && IsStatsZeroIfUnlimited(path); |
| 110 | #else |
| 111 | const bool zero_size_means_unlimited = false; |
| 112 | #endif |
| 113 | |
| 114 | if (available_bytes) { |
| 115 | *available_bytes = |
| 116 | zero_size_means_unlimited |
| 117 | ? std::numeric_limits<int64_t>::max() |
| 118 | : static_cast<int64_t>(stats.f_bavail) * stats.f_frsize; |
| 119 | } |
| 120 | |
| 121 | if (total_bytes) { |
| 122 | *total_bytes = zero_size_means_unlimited |
| 123 | ? std::numeric_limits<int64_t>::max() |
| 124 | : static_cast<int64_t>(stats.f_blocks) * stats.f_frsize; |
| 125 | } |
fukino | 50a4481 | 2016-06-10 19:35:55 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 129 | } // namespace |
| 130 | |
phoglund | 5b052f0 | 2016-07-21 12:41:04 | [diff] [blame] | 131 | namespace base { |
| 132 | |
scottmg | aaa608f | 2017-05-24 05:56:09 | [diff] [blame] | 133 | #if !defined(OS_OPENBSD) && !defined(OS_FUCHSIA) |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 134 | int SysInfo::NumberOfProcessors() { |
| 135 | return g_lazy_number_of_processors.Get().value(); |
| 136 | } |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 137 | #endif |
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 138 | |
Wez | 8f8498b3 | 2017-08-18 04:26:34 | [diff] [blame] | 139 | #if !defined(OS_FUCHSIA) |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 140 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 141 | int64_t SysInfo::AmountOfVirtualMemory() { |
[email protected] | 275a29de | 2014-04-08 23:19:16 | [diff] [blame] | 142 | return g_lazy_virtual_memory.Get().value(); |
| 143 | } |
Wez | 8f8498b3 | 2017-08-18 04:26:34 | [diff] [blame] | 144 | #endif |
[email protected] | 275a29de | 2014-04-08 23:19:16 | [diff] [blame] | 145 | |
| 146 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 147 | int64_t SysInfo::AmountOfFreeDiskSpace(const FilePath& path) { |
[email protected] | dda29c94 | 2012-08-21 19:50:03 | [diff] [blame] | 148 | base::ThreadRestrictions::AssertIOAllowed(); |
| 149 | |
fukino | 50a4481 | 2016-06-10 19:35:55 | [diff] [blame] | 150 | int64_t available; |
| 151 | if (!GetDiskSpaceInfo(path, &available, nullptr)) |
[email protected] | 02ee34a | 2008-09-20 01:16:23 | [diff] [blame] | 152 | return -1; |
fukino | 50a4481 | 2016-06-10 19:35:55 | [diff] [blame] | 153 | return available; |
| 154 | } |
| 155 | |
| 156 | // static |
| 157 | int64_t SysInfo::AmountOfTotalDiskSpace(const FilePath& path) { |
| 158 | base::ThreadRestrictions::AssertIOAllowed(); |
| 159 | |
| 160 | int64_t total; |
| 161 | if (!GetDiskSpaceInfo(path, nullptr, &total)) |
| 162 | return -1; |
| 163 | return total; |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | 5106b3a | 2012-10-03 20:10:44 | [diff] [blame] | 166 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 167 | // static |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 168 | std::string SysInfo::OperatingSystemName() { |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 169 | struct utsname info; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 170 | if (uname(&info) < 0) { |
| 171 | NOTREACHED(); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 172 | return std::string(); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 173 | } |
| 174 | return std::string(info.sysname); |
| 175 | } |
[email protected] | 5106b3a | 2012-10-03 20:10:44 | [diff] [blame] | 176 | #endif |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 177 | |
[email protected] | 82aeeaa | 2013-05-07 04:52:45 | [diff] [blame] | 178 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 179 | // static |
| 180 | std::string SysInfo::OperatingSystemVersion() { |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 181 | struct utsname info; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 182 | if (uname(&info) < 0) { |
| 183 | NOTREACHED(); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 184 | return std::string(); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 185 | } |
| 186 | return std::string(info.release); |
| 187 | } |
[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 188 | #endif |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 189 | |
romax | 2cd67b5 | 2017-03-22 20:01:46 | [diff] [blame] | 190 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 191 | // static |
| 192 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 193 | int32_t* minor_version, |
| 194 | int32_t* bugfix_version) { |
| 195 | struct utsname info; |
| 196 | if (uname(&info) < 0) { |
| 197 | NOTREACHED(); |
| 198 | *major_version = 0; |
| 199 | *minor_version = 0; |
| 200 | *bugfix_version = 0; |
| 201 | return; |
| 202 | } |
| 203 | int num_read = sscanf(info.release, "%d.%d.%d", major_version, minor_version, |
| 204 | bugfix_version); |
| 205 | if (num_read < 1) |
| 206 | *major_version = 0; |
| 207 | if (num_read < 2) |
| 208 | *minor_version = 0; |
| 209 | if (num_read < 3) |
| 210 | *bugfix_version = 0; |
| 211 | } |
| 212 | #endif |
| 213 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 214 | // static |
[email protected] | 0b6a4fb | 2012-10-16 01:58:21 | [diff] [blame] | 215 | std::string SysInfo::OperatingSystemArchitecture() { |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 216 | struct utsname info; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 217 | if (uname(&info) < 0) { |
| 218 | NOTREACHED(); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 219 | return std::string(); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 220 | } |
[email protected] | 56d0cef | 2012-09-26 02:49:42 | [diff] [blame] | 221 | std::string arch(info.machine); |
| 222 | if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") { |
| 223 | arch = "x86"; |
| 224 | } else if (arch == "amd64") { |
| 225 | arch = "x86_64"; |
rayb | 0088ee5 | 2017-04-26 22:35:08 | [diff] [blame] | 226 | } else if (std::string(info.sysname) == "AIX") { |
| 227 | arch = "ppc64"; |
[email protected] | 56d0cef | 2012-09-26 02:49:42 | [diff] [blame] | 228 | } |
| 229 | return arch; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 230 | } |
| 231 | |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 232 | // static |
| 233 | size_t SysInfo::VMAllocationGranularity() { |
| 234 | return getpagesize(); |
| 235 | } |
| 236 | |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 237 | } // namespace base |