| 6 |
| 7 | #include <errno.h> |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | #include <string.h> |
| 11 | #include <sys/param.h> |
| 12 | #include <sys/resource.h> |
| 13 | #include <sys/utsname.h> |
| 14 | #include <unistd.h> |
| 15 |
| 16 | #include "base/files/file_util.h" |
| 17 | #include "base/lazy_instance.h" |
| 18 | #include "base/logging.h" |
| 19 | #include "base/strings/utf_string_conversions.h" |
| 20 | #include "base/system/sys_info_internal.h" |
| 21 | #include "base/threading/scoped_blocking_call.h" |
| 22 | #include "build/build_config.h" |
| 23 |
| 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> |
| 29 | #endif |
| 30 |
| 31 | #if defined(OS_LINUX) |
| 32 | #include <linux/magic.h> |
| 33 | #include <sys/vfs.h> |
| 34 | #endif |
| 35 |
| 36 | namespace { |
| 37 |
| 38 | #if !defined(OS_OPENBSD) |
| 39 | int NumberOfProcessors() { |
| 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); |
| 54 | if (res == -1) { |
| 55 | NOTREACHED(); |
| 56 | return 1; |
| 57 | } |
| 58 |
| 59 | return static_cast<int>(res); |
| 60 | } |
| 61 |
| 62 | base::LazyInstance<base::internal::LazySysInfoValue<int, NumberOfProcessors>>:: |
| 63 | Leaky g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER; |
| 64 | #endif // !defined(OS_OPENBSD) |
| 65 |
| 66 | int64_t AmountOfVirtualMemory() { |
| 67 | struct rlimit limit; |
| 68 | int result = getrlimit(RLIMIT_DATA, &limit); |
| 69 | if (result != 0) { |
| 70 | NOTREACHED(); |
| 71 | return 0; |
| 72 | } |
| 73 | return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; |
| 74 | } |
| 75 |
| 76 | base::LazyInstance< |
| 77 | base::internal::LazySysInfoValue<int64_t, AmountOfVirtualMemory>>::Leaky |
| 78 | g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; |
| 79 |
| 80 | #if defined(OS_LINUX) |
| 81 | bool IsStatsZeroIfUnlimited(const base::FilePath& path) { |
| 82 | struct statfs stats; |
| 83 |
| 84 | if (HANDLE_EINTR(statfs(path.value().c_str(), &stats)) != 0) |
| 85 | return false; |
| 86 |
| 87 | switch (stats.f_type) { |
| 88 | case TMPFS_MAGIC: |
| 89 | case HUGETLBFS_MAGIC: |
| 90 | case RAMFS_MAGIC: |
| 91 | return true; |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | #endif // defined(OS_LINUX) |
| 96 |
| 97 | bool GetDiskSpaceInfo(const base::FilePath& path, |
| 98 | int64_t* available_bytes, |
| 99 | int64_t* total_bytes) { |
| 100 | struct statvfs stats; |
| 101 | if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) |
| 102 | return false; |
| 103 |
| 104 | #if defined(OS_LINUX) |
| 105 | const bool zero_size_means_unlimited = |
| 106 | stats.f_blocks == 0 && IsStatsZeroIfUnlimited(path); |
| 107 | #else |
| 108 | const bool zero_size_means_unlimited = false; |
| 109 | #endif |
| 110 |
| 111 | if (available_bytes) { |
| 112 | *available_bytes = |
| 113 | zero_size_means_unlimited |
| 114 | ? std::numeric_limits<int64_t>::max() |
| 115 | : static_cast<int64_t>(stats.f_bavail) * stats.f_frsize; |
| 116 | } |
| 117 |
| 118 | if (total_bytes) { |
| 119 | *total_bytes = zero_size_means_unlimited |
| 120 | ? std::numeric_limits<int64_t>::max() |
| 121 | : static_cast<int64_t>(stats.f_blocks) * stats.f_frsize; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 |
| 126 | } // namespace |
| 127 |
| 128 | namespace base { |
| 129 |
| 130 | #if !defined(OS_OPENBSD) |
| 131 | int SysInfo::NumberOfProcessors() { |
| 132 | return g_lazy_number_of_processors.Get().value(); |
| 133 | } |
| 134 | #endif // !defined(OS_OPENBSD) |
| 135 |
| 136 | // static |
| 137 | int64_t SysInfo::AmountOfVirtualMemory() { |
| 138 | return g_lazy_virtual_memory.Get().value(); |
| 139 | } |
| 140 |
| 141 | // static |
| 142 | int64_t SysInfo::AmountOfFreeDiskSpace(const FilePath& path) { |
| 143 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 144 | base::BlockingType::MAY_BLOCK); |
| 145 |
| 146 | int64_t available; |
| 147 | if (!GetDiskSpaceInfo(path, &available, nullptr)) |
| 148 | return -1; |
| 149 | return available; |
| 150 | } |
| 151 |
| 152 | // static |
| 153 | int64_t SysInfo::AmountOfTotalDiskSpace(const FilePath& path) { |
| 154 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 155 | base::BlockingType::MAY_BLOCK); |
| 156 |
| 157 | int64_t total; |
| 158 | if (!GetDiskSpaceInfo(path, nullptr, &total)) |
| 159 | return -1; |
| 160 | return total; |
| 161 | } |
| 162 |
| 163 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 164 | // static |
| 165 | std::string SysInfo::OperatingSystemName() { |
| 166 | struct utsname info; |
| 167 | if (uname(&info) < 0) { |
| 168 | NOTREACHED(); |
| 169 | return std::string(); |
| 170 | } |
| 171 | return std::string(info.sysname); |
| 172 | } |
| 173 | #endif //! defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 174 |
| 175 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 176 | // static |
| 177 | std::string SysInfo::OperatingSystemVersion() { |
| 178 | struct utsname info; |
| 179 | if (uname(&info) < 0) { |
| 180 | NOTREACHED(); |
| 181 | return std::string(); |
| 182 | } |
| 183 | return std::string(info.release); |
| 184 | } |
| 185 | #endif |
| 186 |
| 187 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 188 | // static |
| 189 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 190 | int32_t* minor_version, |
| 191 | int32_t* bugfix_version) { |
| 192 | struct utsname info; |
| 193 | if (uname(&info) < 0) { |
| 194 | NOTREACHED(); |
| 195 | *major_version = 0; |
| 196 | *minor_version = 0; |
| 197 | *bugfix_version = 0; |
| 198 | return; |
| 199 | } |
| 200 | int num_read = sscanf(info.release, "%d.%d.%d", major_version, minor_version, |
| 201 | bugfix_version); |
| 202 | if (num_read < 1) |
| 203 | *major_version = 0; |
| 204 | if (num_read < 2) |
| 205 | *minor_version = 0; |
| 206 | if (num_read < 3) |
| 207 | *bugfix_version = 0; |
| 208 | } |
| 209 | #endif |
| 210 |
| 211 | // static |
| 212 | std::string SysInfo::OperatingSystemArchitecture() { |
| 213 | struct utsname info; |
| 214 | if (uname(&info) < 0) { |
| 215 | NOTREACHED(); |
| 216 | return std::string(); |
| 217 | } |
| 218 | std::string arch(info.machine); |
| 219 | if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") { |
| 220 | arch = "x86"; |
| 221 | } else if (arch == "amd64") { |
| 222 | arch = "x86_64"; |
| 223 | } else if (std::string(info.sysname) == "AIX") { |
| 224 | arch = "ppc64"; |
| 225 | } |
| 226 | return arch; |
| 227 | } |
| 228 |
| 229 | // static |
| 230 | size_t SysInfo::VMAllocationGranularity() { |
| 231 | return getpagesize(); |
| 232 | } |
| 233 |
| 234 | } // namespace base |
| 235 |