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 namespace base { |
| 32 |
31 namespace { | 33 namespace { |
32 | 34 |
33 #if !defined(OS_OPENBSD) | 35 #if !defined(OS_OPENBSD) |
34 int NumberOfProcessors() { | 36 int NumberOfProcessors() { |
35 // sysconf returns the number of "logical" (not "physical") processors on both | 37 // 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. | 38 // Mac and Linux. So we get the number of max available "logical" processors. |
37 // | 39 // |
38 // Note that the number of "currently online" processors may be fewer than the | 40 // Note that the number of "currently online" processors may be fewer than the |
39 // returned value of NumberOfProcessors(). On some platforms, the kernel may | 41 // returned value of NumberOfProcessors(). On some platforms, the kernel may |
40 // make some processors offline intermittently, to save power when system | 42 // make some processors offline intermittently, to save power when system |
41 // loading is low. | 43 // loading is low. |
42 // | 44 // |
43 // One common use case that needs to know the processor count is to create | 45 // One common use case that needs to know the processor count is to create |
44 // optimal number of threads for optimization. It should make plan according | 46 // optimal number of threads for optimization. It should make plan according |
45 // to the number of "max available" processors instead of "currently online" | 47 // to the number of "max available" processors instead of "currently online" |
46 // ones. The kernel should be smart enough to make all processors online when | 48 // ones. The kernel should be smart enough to make all processors online when |
47 // it has sufficient number of threads waiting to run. | 49 // it has sufficient number of threads waiting to run. |
48 long res = sysconf(_SC_NPROCESSORS_CONF); | 50 long res = sysconf(_SC_NPROCESSORS_CONF); |
49 if (res == -1) { | 51 if (res == -1) { |
50 NOTREACHED(); | 52 NOTREACHED(); |
51 return 1; | 53 return 1; |
52 } | 54 } |
53 | 55 |
54 return static_cast<int>(res); | 56 return static_cast<int>(res); |
55 } | 57 } |
56 | 58 |
57 base::LazyInstance< | 59 LazyInstance<internal::LazySysInfoValue<int, NumberOfProcessors>>::Leaky |
58 base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky | |
59 g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER; | 60 g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER; |
60 #endif | 61 #endif |
61 | 62 |
62 int64_t AmountOfVirtualMemory() { | 63 int64_t AmountOfVirtualMemory() { |
63 struct rlimit limit; | 64 struct rlimit limit; |
64 int result = getrlimit(RLIMIT_DATA, &limit); | 65 int result = getrlimit(RLIMIT_DATA, &limit); |
65 if (result != 0) { | 66 if (result != 0) { |
66 NOTREACHED(); | 67 NOTREACHED(); |
67 return 0; | 68 return 0; |
68 } | 69 } |
69 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; | 70 return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; |
70 } | 71 } |
71 | 72 |
72 base::LazyInstance< | 73 LazyInstance<internal::LazySysInfoValue<int64_t, AmountOfVirtualMemory>>::Leaky |
73 base::internal::LazySysInfoValue<int64_t, AmountOfVirtualMemory>>::Leaky | |
74 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; | 74 g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; |
75 | 75 |
76 bool GetDiskSpaceInfo(const base::FilePath& path, | 76 bool GetDiskSpaceInfo(const base::FilePath& path, |
77 int64_t* available_bytes, | 77 int64_t* available_bytes, |
78 int64_t* total_bytes) { | 78 int64_t* total_bytes) { |
79 struct statvfs stats; | 79 struct statvfs stats; |
80 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) | 80 if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) |
81 return false; | 81 return false; |
82 | 82 |
83 if (available_bytes) | 83 #if defined(OS_LINUX) |
84 *available_bytes = static_cast<int64_t>(stats.f_bavail) * stats.f_frsize; | 84 // On Linux, stats.f_blocks is 0 when memory based file system (like tmpfs, |
85 if (total_bytes) | 85 // ramfs, or hugetlbfs), is mounted without any size limit (i.e. size set to |
86 *total_bytes = static_cast<int64_t>(stats.f_blocks) * stats.f_frsize; | 86 // 0). |
| 87 FileSystemType fs_type; |
| 88 const bool zero_size_means_unlimited = stats.f_blocks == 0 && |
| 89 GetFileSystemType(path, &fs_type) && |
| 90 fs_type == FILE_SYSTEM_MEMORY; |
| 91 #else |
| 92 const bool zero_size_means_unlimited = false; |
| 93 #endif |
| 94 |
| 95 if (available_bytes) { |
| 96 *available_bytes = |
| 97 zero_size_means_unlimited |
| 98 ? std::numeric_limits<int64_t>::max() |
| 99 : static_cast<int64_t>(stats.f_bavail) * stats.f_frsize; |
| 100 } |
| 101 |
| 102 if (total_bytes) { |
| 103 *total_bytes = zero_size_means_unlimited |
| 104 ? std::numeric_limits<int64_t>::max() |
| 105 : static_cast<int64_t>(stats.f_blocks) * stats.f_frsize; |
| 106 } |
87 return true; | 107 return true; |
88 } | 108 } |
89 | 109 |
90 } // namespace | 110 } // namespace |
91 | 111 |
92 namespace base { | |
93 | |
94 #if !defined(OS_OPENBSD) | 112 #if !defined(OS_OPENBSD) |
95 int SysInfo::NumberOfProcessors() { | 113 int SysInfo::NumberOfProcessors() { |
96 return g_lazy_number_of_processors.Get().value(); | 114 return g_lazy_number_of_processors.Get().value(); |
97 } | 115 } |
98 #endif | 116 #endif |
99 | 117 |
100 // static | 118 // static |
101 int64_t SysInfo::AmountOfVirtualMemory() { | 119 int64_t SysInfo::AmountOfVirtualMemory() { |
102 return g_lazy_virtual_memory.Get().value(); | 120 return g_lazy_virtual_memory.Get().value(); |
103 } | 121 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 } | 179 } |
162 return arch; | 180 return arch; |
163 } | 181 } |
164 | 182 |
165 // static | 183 // static |
166 size_t SysInfo::VMAllocationGranularity() { | 184 size_t SysInfo::VMAllocationGranularity() { |
167 return getpagesize(); | 185 return getpagesize(); |
168 } | 186 } |
169 | 187 |
170 } // namespace base | 188 } // namespace base |
OLD | NEW |