| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/sys_info.h" | 5 #include "base/sys_info.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "base/threading/thread_restrictions.h" | 21 #include "base/threading/thread_restrictions.h" |
| 22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 23 | 23 |
| 24 #if defined(OS_ANDROID) | 24 #if defined(OS_ANDROID) |
| 25 #include <sys/vfs.h> | 25 #include <sys/vfs.h> |
| 26 #define statvfs statfs // Android uses a statvfs-like statfs struct and call. | 26 #define statvfs statfs // Android uses a statvfs-like statfs struct and call. |
| 27 #else | 27 #else |
| 28 #include <sys/statvfs.h> | 28 #include <sys/statvfs.h> |
| 29 #endif | 29 #endif |
| 30 | 30 |
| 31 #if defined(OS_LINUX) |
| 32 #include <linux/magic.h> |
| 33 #include <sys/vfs.h> |
| 34 #endif |
| 35 |
| 31 namespace { | 36 namespace { |
| 32 | 37 |
| 33 #if !defined(OS_OPENBSD) | 38 #if !defined(OS_OPENBSD) |
| 34 int NumberOfProcessors() { | 39 int NumberOfProcessors() { |
| 35 // sysconf returns the number of "logical" (not "physical") processors on both | 40 // sysconf returns the number of "logical" (not "physical") processors on both |
| 36 // Mac and Linux. So we get the number of max available "logical" processors. | 41 // Mac and Linux. So we get the number of max available "logical" processors. |
| 37 // | 42 // |
| 38 // Note that the number of "currently online" processors may be fewer than the | 43 // Note that the number of "currently online" processors may be fewer than the |
| 39 // returned value of NumberOfProcessors(). On some platforms, the kernel may | 44 // returned value of NumberOfProcessors(). On some platforms, the kernel may |
| 40 // make some processors offline intermittently, to save power when system | 45 // make some processors offline intermittently, to save power when system |
| (...skipping 25 matching lines...) Expand all Loading... |
| 66 NOTREACHED(); | 71 NOTREACHED(); |
| 67 return 0; | 72 return 0; |
| 68 } | 73 } |
| 69 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; | 74 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; |
| 70 } | 75 } |
| 71 | 76 |
| 72 base::LazyInstance< | 77 base::LazyInstance< |
| 73 base::internal::LazySysInfoValue<int64_t, AmountOfVirtualMemory>>::Leaky | 78 base::internal::LazySysInfoValue<int64_t, AmountOfVirtualMemory>>::Leaky |
| 74 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; | 79 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; |
| 75 | 80 |
| 81 #if defined(OS_LINUX) |
| 82 bool IsStatsZeroIfUnlimited(const base::FilePath& path) { |
| 83 struct statfs stats; |
| 84 |
| 85 if (HANDLE_EINTR(statfs(path.value().c_str(), &stats)) != 0) |
| 86 return false; |
| 87 |
| 88 switch (stats.f_type) { |
| 89 case TMPFS_MAGIC: |
| 90 case HUGETLBFS_MAGIC: |
| 91 case RAMFS_MAGIC: |
| 92 return true; |
| 93 } |
| 94 return false; |
| 95 } |
| 96 #endif |
| 97 |
| 76 bool GetDiskSpaceInfo(const base::FilePath& path, | 98 bool GetDiskSpaceInfo(const base::FilePath& path, |
| 77 int64_t* available_bytes, | 99 int64_t* available_bytes, |
| 78 int64_t* total_bytes) { | 100 int64_t* total_bytes) { |
| 79 struct statvfs stats; | 101 struct statvfs stats; |
| 80 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) | 102 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) |
| 81 return false; | 103 return false; |
| 82 | 104 |
| 83 if (available_bytes) | 105 #if defined(OS_LINUX) |
| 84 *available_bytes = static_cast<int64_t>(stats.f_bavail) * stats.f_frsize; | 106 const bool zero_size_means_unlimited = |
| 85 if (total_bytes) | 107 stats.f_blocks == 0 && IsStatsZeroIfUnlimited(path); |
| 86 *total_bytes = static_cast<int64_t>(stats.f_blocks) * stats.f_frsize; | 108 #else |
| 109 const bool zero_size_means_unlimited = false; |
| 110 #endif |
| 111 |
| 112 if (available_bytes) { |
| 113 *available_bytes = |
| 114 zero_size_means_unlimited |
| 115 ? std::numeric_limits<int64_t>::max() |
| 116 : static_cast<int64_t>(stats.f_bavail) * stats.f_frsize; |
| 117 } |
| 118 |
| 119 if (total_bytes) { |
| 120 *total_bytes = zero_size_means_unlimited |
| 121 ? std::numeric_limits<int64_t>::max() |
| 122 : static_cast<int64_t>(stats.f_blocks) * stats.f_frsize; |
| 123 } |
| 87 return true; | 124 return true; |
| 88 } | 125 } |
| 89 | 126 |
| 90 } // namespace | 127 } // namespace |
| 91 | 128 |
| 92 namespace base { | 129 namespace base { |
| 93 | 130 |
| 94 #if !defined(OS_OPENBSD) | 131 #if !defined(OS_OPENBSD) |
| 95 int SysInfo::NumberOfProcessors() { | 132 int SysInfo::NumberOfProcessors() { |
| 96 return g_lazy_number_of_processors.Get().value(); | 133 return g_lazy_number_of_processors.Get().value(); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 } | 198 } |
| 162 return arch; | 199 return arch; |
| 163 } | 200 } |
| 164 | 201 |
| 165 // static | 202 // static |
| 166 size_t SysInfo::VMAllocationGranularity() { | 203 size_t SysInfo::VMAllocationGranularity() { |
| 167 return getpagesize(); | 204 return getpagesize(); |
| 168 } | 205 } |
| 169 | 206 |
| 170 } // namespace base | 207 } // namespace base |
| OLD | NEW |