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