[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [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> | ||||
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 9 | #include <sys/param.h> |
[email protected] | 275a29de | 2014-04-08 23:19:16 | [diff] [blame^] | 10 | #include <sys/resource.h> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 11 | #include <sys/utsname.h> |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 12 | #include <unistd.h> |
13 | |||||
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 14 | #include "base/basictypes.h" |
15 | #include "base/file_util.h" | ||||
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 16 | #include "base/lazy_instance.h" |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 17 | #include "base/logging.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 19 | #include "base/sys_info_internal.h" |
[email protected] | dda29c94 | 2012-08-21 19:50:03 | [diff] [blame] | 20 | #include "base/threading/thread_restrictions.h" |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 21 | |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [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> | ||||
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 27 | #endif |
28 | |||||
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 29 | namespace { |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 30 | |
[email protected] | 5f5ac6a | 2009-11-23 21:57:11 | [diff] [blame] | 31 | #if !defined(OS_OPENBSD) |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 32 | int NumberOfProcessors() { |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 33 | // It seems that sysconf returns the number of "logical" processors on both |
[email protected] | 5f5ac6a | 2009-11-23 21:57:11 | [diff] [blame] | 34 | // Mac and Linux. So we get the number of "online logical" processors. |
[email protected] | 0203ed7 | 2009-07-02 13:59:08 | [diff] [blame] | 35 | long res = sysconf(_SC_NPROCESSORS_ONLN); |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 36 | if (res == -1) { |
37 | NOTREACHED(); | ||||
38 | return 1; | ||||
39 | } | ||||
40 | |||||
41 | return static_cast<int>(res); | ||||
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 42 | } |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 43 | |
44 | base::LazyInstance< | ||||
45 | base::internal::LazySysInfoValue<int, NumberOfProcessors> >::Leaky | ||||
46 | g_lazy_number_of_processors = LAZY_INSTANCE_INITIALIZER; | ||||
47 | #endif | ||||
48 | |||||
[email protected] | 275a29de | 2014-04-08 23:19:16 | [diff] [blame^] | 49 | int64 AmountOfVirtualMemory() { |
50 | struct rlimit limit; | ||||
51 | int result = getrlimit(RLIMIT_DATA, &limit); | ||||
52 | if (result != 0) { | ||||
53 | NOTREACHED(); | ||||
54 | return 0; | ||||
55 | } | ||||
56 | return limit.rlim_cur == RLIM_INFINITY ? 0 : limit.rlim_cur; | ||||
57 | } | ||||
58 | |||||
59 | base::LazyInstance< | ||||
60 | base::internal::LazySysInfoValue<int64, AmountOfVirtualMemory> >::Leaky | ||||
61 | g_lazy_virtual_memory = LAZY_INSTANCE_INITIALIZER; | ||||
62 | |||||
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 63 | } // namespace |
64 | |||||
65 | namespace base { | ||||
66 | |||||
67 | #if !defined(OS_OPENBSD) | ||||
68 | int SysInfo::NumberOfProcessors() { | ||||
69 | return g_lazy_number_of_processors.Get().value(); | ||||
70 | } | ||||
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 71 | #endif |
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 72 | |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 73 | // static |
[email protected] | 275a29de | 2014-04-08 23:19:16 | [diff] [blame^] | 74 | int64 SysInfo::AmountOfVirtualMemory() { |
75 | return g_lazy_virtual_memory.Get().value(); | ||||
76 | } | ||||
77 | |||||
78 | // static | ||||
[email protected] | 13326bb | 2009-10-14 00:41:56 | [diff] [blame] | 79 | int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) { |
[email protected] | dda29c94 | 2012-08-21 19:50:03 | [diff] [blame] | 80 | base::ThreadRestrictions::AssertIOAllowed(); |
81 | |||||
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 82 | struct statvfs stats; |
[email protected] | 0444358 | 2013-03-25 19:03:06 | [diff] [blame] | 83 | if (HANDLE_EINTR(statvfs(path.value().c_str(), &stats)) != 0) |
[email protected] | 02ee34a | 2008-09-20 01:16:23 | [diff] [blame] | 84 | return -1; |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 85 | return static_cast<int64>(stats.f_bavail) * stats.f_frsize; |
86 | } | ||||
87 | |||||
[email protected] | 5106b3a | 2012-10-03 20:10:44 | [diff] [blame] | 88 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 89 | // static |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 90 | std::string SysInfo::OperatingSystemName() { |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 91 | struct utsname info; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 92 | if (uname(&info) < 0) { |
93 | NOTREACHED(); | ||||
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 94 | return std::string(); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 95 | } |
96 | return std::string(info.sysname); | ||||
97 | } | ||||
[email protected] | 5106b3a | 2012-10-03 20:10:44 | [diff] [blame] | 98 | #endif |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 99 | |
[email protected] | 82aeeaa | 2013-05-07 04:52:45 | [diff] [blame] | 100 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 101 | // static |
102 | std::string SysInfo::OperatingSystemVersion() { | ||||
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 103 | struct utsname info; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 104 | if (uname(&info) < 0) { |
105 | NOTREACHED(); | ||||
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 106 | return std::string(); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 107 | } |
108 | return std::string(info.release); | ||||
109 | } | ||||
[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 110 | #endif |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 111 | |
112 | // static | ||||
[email protected] | 0b6a4fb | 2012-10-16 01:58:21 | [diff] [blame] | 113 | std::string SysInfo::OperatingSystemArchitecture() { |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 114 | struct utsname info; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 115 | if (uname(&info) < 0) { |
116 | NOTREACHED(); | ||||
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 117 | return std::string(); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 118 | } |
[email protected] | 56d0cef | 2012-09-26 02:49:42 | [diff] [blame] | 119 | std::string arch(info.machine); |
120 | if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") { | ||||
121 | arch = "x86"; | ||||
122 | } else if (arch == "amd64") { | ||||
123 | arch = "x86_64"; | ||||
124 | } | ||||
125 | return arch; | ||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 126 | } |
127 | |||||
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 128 | // static |
129 | size_t SysInfo::VMAllocationGranularity() { | ||||
130 | return getpagesize(); | ||||
131 | } | ||||
132 | |||||
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 133 | } // namespace base |