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 #endif | |
34 | |
31 namespace { | 35 namespace { |
32 | 36 |
33 #if !defined(OS_OPENBSD) | 37 #if !defined(OS_OPENBSD) |
34 int NumberOfProcessors() { | 38 int NumberOfProcessors() { |
35 // sysconf returns the number of "logical" (not "physical") processors on both | 39 // 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. | 40 // Mac and Linux. So we get the number of max available "logical" processors. |
37 // | 41 // |
38 // Note that the number of "currently online" processors may be fewer than the | 42 // Note that the number of "currently online" processors may be fewer than the |
39 // returned value of NumberOfProcessors(). On some platforms, the kernel may | 43 // returned value of NumberOfProcessors(). On some platforms, the kernel may |
40 // make some processors offline intermittently, to save power when system | 44 // make some processors offline intermittently, to save power when system |
(...skipping 25 matching lines...) Expand all Loading... | |
66 NOTREACHED(); | 70 NOTREACHED(); |
67 return 0; | 71 return 0; |
68 } | 72 } |
69 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; | 73 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; |
70 } | 74 } |
71 | 75 |
72 base::LazyInstance< | 76 base::LazyInstance< |
73 base::internal::LazySysInfoValue<int64_t, AmountOfVirtualMemory>>::Leaky | 77 base::internal::LazySysInfoValue<int64_t, AmountOfVirtualMemory>>::Leaky |
74 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; | 78 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; |
75 | 79 |
80 #if defined(OS_LINUX) | |
81 bool IsStatsZeroIfUnlimited(unsigned long int fsid) { | |
82 // On Linux, stats.f_blocks is 0 when memory based file system (like tmpfs, | |
83 // ramfs, or hugetlbfs), is mounted without any size limit (i.e. size set to | |
84 // 0). | |
85 return fsid == TMPFS_MAGIC || fsid == HUGETLBFS_MAGIC || fsid == RAMFS_MAGIC; | |
86 } | |
87 #endif | |
88 | |
76 bool GetDiskSpaceInfo(const base::FilePath& path, | 89 bool GetDiskSpaceInfo(const base::FilePath& path, |
77 int64_t* available_bytes, | 90 int64_t* available_bytes, |
78 int64_t* total_bytes) { | 91 int64_t* total_bytes) { |
79 struct statvfs stats; | 92 struct statvfs stats; |
80 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) | 93 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) |
81 return false; | 94 return false; |
82 | 95 |
83 if (available_bytes) | 96 #if defined(OS_LINUX) |
84 *available_bytes = static_cast<int64_t>(stats.f_bavail) * stats.f_frsize; | 97 const bool zero_size_means_unlimited = |
85 if (total_bytes) | 98 stats.f_blocks == 0 && IsStatsZeroIfUnlimited(stats.f_fsid); |
Lei Zhang
2016/08/01 21:30:55
Are you sure f_fsid is the right field to check? L
Sriram
2016/08/02 00:03:24
You are right - I misunderstood the man page and f
| |
86 *total_bytes = static_cast<int64_t>(stats.f_blocks) * stats.f_frsize; | 99 #else |
100 const bool zero_size_means_unlimited = false; | |
101 #endif | |
102 | |
103 if (available_bytes) { | |
104 *available_bytes = | |
105 zero_size_means_unlimited | |
106 ? std::numeric_limits<int64_t>::max() | |
107 : static_cast<int64_t>(stats.f_bavail) * stats.f_frsize; | |
108 } | |
109 | |
110 if (total_bytes) { | |
111 *total_bytes = zero_size_means_unlimited | |
112 ? std::numeric_limits<int64_t>::max() | |
113 : static_cast<int64_t>(stats.f_blocks) * stats.f_frsize; | |
114 } | |
115 | |
87 return true; | 116 return true; |
88 } | 117 } |
89 | 118 |
90 } // namespace | 119 } // namespace |
91 | 120 |
92 namespace base { | 121 namespace base { |
93 | 122 |
94 #if !defined(OS_OPENBSD) | 123 #if !defined(OS_OPENBSD) |
95 int SysInfo::NumberOfProcessors() { | 124 int SysInfo::NumberOfProcessors() { |
96 return g_lazy_number_of_processors.Get().value(); | 125 return g_lazy_number_of_processors.Get().value(); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 } | 190 } |
162 return arch; | 191 return arch; |
163 } | 192 } |
164 | 193 |
165 // static | 194 // static |
166 size_t SysInfo::VMAllocationGranularity() { | 195 size_t SysInfo::VMAllocationGranularity() { |
167 return getpagesize(); | 196 return getpagesize(); |
168 } | 197 } |
169 | 198 |
170 } // namespace base | 199 } // namespace base |
OLD | NEW |