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> |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 10 | #include <sys/utsname.h> |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | |
brettw@chromium.org | 50c9465 | 2009-10-07 11:10:20 +0900 | [diff] [blame] | 13 | #include "base/basictypes.h" |
| 14 | #include "base/file_util.h" |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 15 | #include "base/lazy_instance.h" |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 16 | #include "base/logging.h" |
avi@chromium.org | 17f6062 | 2013-06-08 03:37:07 +0900 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 18 | #include "base/sys_info_internal.h" |
haruki@chromium.org | b0efbba | 2012-08-22 04:50:03 +0900 | [diff] [blame] | 19 | #include "base/threading/thread_restrictions.h" |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 20 | |
michaelbai@google.com | 2251c62 | 2011-06-22 07:34:50 +0900 | [diff] [blame] | 21 | #if defined(OS_ANDROID) |
| 22 | #include <sys/vfs.h> |
| 23 | #define statvfs statfs // Android uses a statvfs-like statfs struct and call. |
| 24 | #else |
| 25 | #include <sys/statvfs.h> |
michaelbai@google.com | 2251c62 | 2011-06-22 07:34:50 +0900 | [diff] [blame] | 26 | #endif |
| 27 | |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 28 | namespace { |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 29 | |
evan@chromium.org | fae7268 | 2009-11-24 06:57:11 +0900 | [diff] [blame] | 30 | #if !defined(OS_OPENBSD) |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 31 | int NumberOfProcessors() { |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 32 | // It seems that sysconf returns the number of "logical" processors on both |
evan@chromium.org | fae7268 | 2009-11-24 06:57:11 +0900 | [diff] [blame] | 33 | // Mac and Linux. So we get the number of "online logical" processors. |
jeremy@chromium.org | 92b13a1 | 2009-07-02 22:59:08 +0900 | [diff] [blame] | 34 | long res = sysconf(_SC_NPROCESSORS_ONLN); |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 35 | if (res == -1) { |
| 36 | NOTREACHED(); |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | return static_cast<int>(res); |
deanm@chromium.org | 319f044 | 2008-09-17 22:10:45 +0900 | [diff] [blame] | 41 | } |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 42 | |
| 43 | base::LazyInstance< |
| 44 | base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky |
| 45 | g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER; |
| 46 | #endif |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 50 | namespace base { |
| 51 | |
| 52 | #if !defined(OS_OPENBSD) |
| 53 | int SysInfo::NumberOfProcessors() { |
| 54 | return g_lazy_number_of_processors.Get().value(); |
| 55 | } |
evan@chromium.org | 84b05b8 | 2009-11-12 11:17:19 +0900 | [diff] [blame] | 56 | #endif |
deanm@chromium.org | 319f044 | 2008-09-17 22:10:45 +0900 | [diff] [blame] | 57 | |
deanm@chromium.org | ee45e8f | 2008-09-18 21:34:24 +0900 | [diff] [blame] | 58 | // static |
tony@chromium.org | 1e25e00 | 2009-10-14 09:41:56 +0900 | [diff] [blame] | 59 | int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) { |
haruki@chromium.org | b0efbba | 2012-08-22 04:50:03 +0900 | [diff] [blame] | 60 | base::ThreadRestrictions::AssertIOAllowed(); |
| 61 | |
deanm@chromium.org | ee45e8f | 2008-09-18 21:34:24 +0900 | [diff] [blame] | 62 | struct statvfs stats; |
phajdan.jr@chromium.org | 1f2edf3 | 2013-03-26 04:03:06 +0900 | [diff] [blame] | 63 | if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) |
rvargas@google.com | af00029 | 2008-09-20 10:16:23 +0900 | [diff] [blame] | 64 | return -1; |
deanm@chromium.org | ee45e8f | 2008-09-18 21:34:24 +0900 | [diff] [blame] | 65 | return static_cast<int64>(stats.f_bavail) * stats.f_frsize; |
| 66 | } |
| 67 | |
dfalcantara@chromium.org | efbe531 | 2012-10-04 05:10:44 +0900 | [diff] [blame] | 68 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 69 | // static |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 70 | std::string SysInfo::OperatingSystemName() { |
chromium@hybridsource.org | 8f85a6a | 2011-06-25 13:54:41 +0900 | [diff] [blame] | 71 | struct utsname info; |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 72 | if (uname(&info) < 0) { |
| 73 | NOTREACHED(); |
dcheng@chromium.org | 8164c2c | 2013-04-09 17:46:45 +0900 | [diff] [blame] | 74 | return std::string(); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 75 | } |
| 76 | return std::string(info.sysname); |
| 77 | } |
dfalcantara@chromium.org | efbe531 | 2012-10-04 05:10:44 +0900 | [diff] [blame] | 78 | #endif |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 79 | |
zmo@chromium.org | ad89ff5 | 2013-05-07 13:52:45 +0900 | [diff] [blame] | 80 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 81 | // static |
| 82 | std::string SysInfo::OperatingSystemVersion() { |
chromium@hybridsource.org | 8f85a6a | 2011-06-25 13:54:41 +0900 | [diff] [blame] | 83 | struct utsname info; |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 84 | if (uname(&info) < 0) { |
| 85 | NOTREACHED(); |
dcheng@chromium.org | 8164c2c | 2013-04-09 17:46:45 +0900 | [diff] [blame] | 86 | return std::string(); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 87 | } |
| 88 | return std::string(info.release); |
| 89 | } |
stuartmorgan@chromium.org | 56f1efd | 2011-05-26 05:53:18 +0900 | [diff] [blame] | 90 | #endif |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 91 | |
| 92 | // static |
thestig@chromium.org | e170665 | 2012-10-16 10:58:21 +0900 | [diff] [blame] | 93 | std::string SysInfo::OperatingSystemArchitecture() { |
chromium@hybridsource.org | 8f85a6a | 2011-06-25 13:54:41 +0900 | [diff] [blame] | 94 | struct utsname info; |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 95 | if (uname(&info) < 0) { |
| 96 | NOTREACHED(); |
dcheng@chromium.org | 8164c2c | 2013-04-09 17:46:45 +0900 | [diff] [blame] | 97 | return std::string(); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 98 | } |
thestig@chromium.org | c913724 | 2012-09-26 11:49:42 +0900 | [diff] [blame] | 99 | std::string arch(info.machine); |
| 100 | if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") { |
| 101 | arch = "x86"; |
| 102 | } else if (arch == "amd64") { |
| 103 | arch = "x86_64"; |
| 104 | } |
| 105 | return arch; |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 106 | } |
| 107 | |
agl@chromium.org | 3d71fda | 2009-01-22 10:42:15 +0900 | [diff] [blame] | 108 | // static |
| 109 | size_t SysInfo::VMAllocationGranularity() { |
| 110 | return getpagesize(); |
| 111 | } |
| 112 | |
deanm@chromium.org | 42a34eb | 2008-09-17 19:09:39 +0900 | [diff] [blame] | 113 | } // namespace base |