stuartmorgan@chromium.org | 56f1efd | 2011-05-26 05:53:18 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/sys_info.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <string.h> |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 9 | #include <sys/param.h> |
jochen@chromium.org | 5ac0b17 | 2014-04-09 08:19:16 +0900 | [diff] [blame] | 10 | #include <sys/resource.h> |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 11 | #include <sys/utsname.h> |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 12 | #include <unistd.h> |
| 13 | |
brettw@chromium.org | 50c9465 | 2009-10-07 11:10:20 +0900 | [diff] [blame] | 14 | #include "base/basictypes.h" |
brettw@chromium.org | 01f3da4 | 2014-08-14 05:22:14 +0900 | [diff] [blame] | 15 | #include "base/files/file_util.h" |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 16 | #include "base/lazy_instance.h" |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 17 | #include "base/logging.h" |
avi@chromium.org | 17f6062 | 2013-06-08 03:37:07 +0900 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 19 | #include "base/sys_info_internal.h" |
haruki@chromium.org | b0efbba | 2012-08-22 04:50:03 +0900 | [diff] [blame] | 20 | #include "base/threading/thread_restrictions.h" |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 21 | |
michaelbai@google.com | 2251c62 | 2011-06-22 07:34:50 +0900 | [diff] [blame] | 22 | #if defined(OS_ANDROID) |
| 23 | #include <sys/vfs.h> |
| 24 | #define statvfs statfs // Android uses a statvfs-like statfs struct and call. |
| 25 | #else |
| 26 | #include <sys/statvfs.h> |
michaelbai@google.com | 2251c62 | 2011-06-22 07:34:50 +0900 | [diff] [blame] | 27 | #endif |
| 28 | |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 29 | namespace { |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 30 | |
evan@chromium.org | fae7268 | 2009-11-24 06:57:11 +0900 | [diff] [blame] | 31 | #if !defined(OS_OPENBSD) |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 32 | int NumberOfProcessors() { |
jchuang@chromium.org | ab5f646 | 2014-08-18 13:52:48 +0900 | [diff] [blame] | 33 | // sysconf returns the number of "logical" (not "physical") processors on both |
| 34 | // Mac and Linux. So we get the number of max available "logical" processors. |
| 35 | // |
| 36 | // Note that the number of "currently online" processors may be fewer than the |
| 37 | // returned value of NumberOfProcessors(). On some platforms, the kernel may |
| 38 | // make some processors offline intermittently, to save power when system |
| 39 | // loading is low. |
| 40 | // |
| 41 | // One common use case that needs to know the processor count is to create |
| 42 | // optimal number of threads for optimization. It should make plan according |
| 43 | // to the number of "max available" processors instead of "currently online" |
| 44 | // ones. The kernel should be smart enough to make all processors online when |
| 45 | // it has sufficient number of threads waiting to run. |
| 46 | long res = sysconf(_SC_NPROCESSORS_CONF); |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 47 | if (res == -1) { |
| 48 | NOTREACHED(); |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | return static_cast<int>(res); |
deanm@chromium.org | 319f044 | 2008-09-17 22:10:45 +0900 | [diff] [blame] | 53 | } |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 54 | |
| 55 | base::LazyInstance< |
| 56 | base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky |
| 57 | g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER; |
| 58 | #endif |
| 59 | |
jochen@chromium.org | 5ac0b17 | 2014-04-09 08:19:16 +0900 | [diff] [blame] | 60 | int64 AmountOfVirtualMemory() { |
| 61 | struct rlimit limit; |
| 62 | int result = getrlimit(RLIMIT_DATA, &limit); |
| 63 | if (result != 0) { |
| 64 | NOTREACHED(); |
| 65 | return 0; |
| 66 | } |
| 67 | return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; |
| 68 | } |
| 69 | |
| 70 | base::LazyInstance< |
| 71 | base::internal::LazySysInfoValue<int64, AmountOfVirtualMemory> >::Leaky |
| 72 | g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; |
| 73 | |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 74 | } // namespace |
| 75 | |
| 76 | namespace base { |
| 77 | |
| 78 | #if !defined(OS_OPENBSD) |
| 79 | int SysInfo::NumberOfProcessors() { |
| 80 | return g_lazy_number_of_processors.Get().value(); |
| 81 | } |
evan@chromium.org | 84b05b8 | 2009-11-12 11:17:19 +0900 | [diff] [blame] | 82 | #endif |
deanm@chromium.org | 319f044 | 2008-09-17 22:10:45 +0900 | [diff] [blame] | 83 | |
deanm@chromium.org | ee45e8f | 2008-09-18 21:34:24 +0900 | [diff] [blame] | 84 | // static |
jochen@chromium.org | 5ac0b17 | 2014-04-09 08:19:16 +0900 | [diff] [blame] | 85 | int64 SysInfo::AmountOfVirtualMemory() { |
| 86 | return g_lazy_virtual_memory.Get().value(); |
| 87 | } |
| 88 | |
| 89 | // static |
tony@chromium.org | 1e25e00 | 2009-10-14 09:41:56 +0900 | [diff] [blame] | 90 | int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) { |
haruki@chromium.org | b0efbba | 2012-08-22 04:50:03 +0900 | [diff] [blame] | 91 | base::ThreadRestrictions::AssertIOAllowed(); |
| 92 | |
deanm@chromium.org | ee45e8f | 2008-09-18 21:34:24 +0900 | [diff] [blame] | 93 | struct statvfs stats; |
phajdan.jr@chromium.org | 1f2edf3 | 2013-03-26 04:03:06 +0900 | [diff] [blame] | 94 | if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) |
rvargas@google.com | af00029 | 2008-09-20 10:16:23 +0900 | [diff] [blame] | 95 | return -1; |
deanm@chromium.org | ee45e8f | 2008-09-18 21:34:24 +0900 | [diff] [blame] | 96 | return static_cast<int64>(stats.f_bavail) * stats.f_frsize; |
| 97 | } |
| 98 | |
dfalcantara@chromium.org | efbe531 | 2012-10-04 05:10:44 +0900 | [diff] [blame] | 99 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 100 | // static |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 101 | std::string SysInfo::OperatingSystemName() { |
chromium@hybridsource.org | 8f85a6a | 2011-06-25 13:54:41 +0900 | [diff] [blame] | 102 | struct utsname info; |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 103 | if (uname(&info) < 0) { |
| 104 | NOTREACHED(); |
dcheng@chromium.org | 8164c2c | 2013-04-09 17:46:45 +0900 | [diff] [blame] | 105 | return std::string(); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 106 | } |
| 107 | return std::string(info.sysname); |
| 108 | } |
dfalcantara@chromium.org | efbe531 | 2012-10-04 05:10:44 +0900 | [diff] [blame] | 109 | #endif |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 110 | |
zmo@chromium.org | ad89ff5 | 2013-05-07 13:52:45 +0900 | [diff] [blame] | 111 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 112 | // static |
| 113 | std::string SysInfo::OperatingSystemVersion() { |
chromium@hybridsource.org | 8f85a6a | 2011-06-25 13:54:41 +0900 | [diff] [blame] | 114 | struct utsname info; |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 115 | if (uname(&info) < 0) { |
| 116 | NOTREACHED(); |
dcheng@chromium.org | 8164c2c | 2013-04-09 17:46:45 +0900 | [diff] [blame] | 117 | return std::string(); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 118 | } |
| 119 | return std::string(info.release); |
| 120 | } |
stuartmorgan@chromium.org | 56f1efd | 2011-05-26 05:53:18 +0900 | [diff] [blame] | 121 | #endif |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 122 | |
| 123 | // static |
thestig@chromium.org | e170665 | 2012-10-16 10:58:21 +0900 | [diff] [blame] | 124 | std::string SysInfo::OperatingSystemArchitecture() { |
chromium@hybridsource.org | 8f85a6a | 2011-06-25 13:54:41 +0900 | [diff] [blame] | 125 | struct utsname info; |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 126 | if (uname(&info) < 0) { |
| 127 | NOTREACHED(); |
dcheng@chromium.org | 8164c2c | 2013-04-09 17:46:45 +0900 | [diff] [blame] | 128 | return std::string(); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 129 | } |
thestig@chromium.org | c913724 | 2012-09-26 11:49:42 +0900 | [diff] [blame] | 130 | std::string arch(info.machine); |
| 131 | if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") { |
| 132 | arch = "x86"; |
| 133 | } else if (arch == "amd64") { |
| 134 | arch = "x86_64"; |
| 135 | } |
| 136 | return arch; |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 137 | } |
| 138 | |
agl@chromium.org | 3d71fda | 2009-01-22 10:42:15 +0900 | [diff] [blame] | 139 | // static |
| 140 | size_t SysInfo::VMAllocationGranularity() { |
| 141 | return getpagesize(); |
| 142 | } |
| 143 | |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 144 | } // namespace base |