[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] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 10 | #include <sys/utsname.h> |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 11 | #include <unistd.h> |
12 | |||||
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 13 | #include "base/basictypes.h" |
14 | #include "base/file_util.h" | ||||
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 15 | #include "base/logging.h" |
[email protected] | dda29c94 | 2012-08-21 19:50:03 | [diff] [blame] | 16 | #include "base/threading/thread_restrictions.h" |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 17 | #include "base/utf_string_conversions.h" |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 18 | |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 19 | #if defined(OS_ANDROID) |
20 | #include <sys/vfs.h> | ||||
21 | #define statvfs statfs // Android uses a statvfs-like statfs struct and call. | ||||
22 | #else | ||||
23 | #include <sys/statvfs.h> | ||||
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 24 | #endif |
25 | |||||
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 26 | namespace base { |
27 | |||||
[email protected] | 5f5ac6a | 2009-11-23 21:57:11 | [diff] [blame] | 28 | #if !defined(OS_OPENBSD) |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 29 | int SysInfo::NumberOfProcessors() { |
30 | // It seems that sysconf returns the number of "logical" processors on both | ||||
[email protected] | 5f5ac6a | 2009-11-23 21:57:11 | [diff] [blame] | 31 | // Mac and Linux. So we get the number of "online logical" processors. |
[email protected] | 0203ed7 | 2009-07-02 13:59:08 | [diff] [blame] | 32 | long res = sysconf(_SC_NPROCESSORS_ONLN); |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 33 | if (res == -1) { |
34 | NOTREACHED(); | ||||
35 | return 1; | ||||
36 | } | ||||
37 | |||||
38 | return static_cast<int>(res); | ||||
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 39 | } |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 40 | #endif |
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 41 | |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 42 | // static |
[email protected] | 13326bb | 2009-10-14 00:41:56 | [diff] [blame] | 43 | int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) { |
[email protected] | dda29c94 | 2012-08-21 19:50:03 | [diff] [blame] | 44 | base::ThreadRestrictions::AssertIOAllowed(); |
45 | |||||
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 46 | struct statvfs stats; |
[email protected] | 13326bb | 2009-10-14 00:41:56 | [diff] [blame] | 47 | if (statvfs(path.value().c_str(), &stats) != 0) { |
[email protected] | 02ee34a | 2008-09-20 01:16:23 | [diff] [blame] | 48 | return -1; |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 49 | } |
50 | return static_cast<int64>(stats.f_bavail) * stats.f_frsize; | ||||
51 | } | ||||
52 | |||||
[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 53 | #if !defined(OS_MACOSX) |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 54 | // static |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 55 | std::string SysInfo::OperatingSystemName() { |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 56 | struct utsname info; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 57 | if (uname(&info) < 0) { |
58 | NOTREACHED(); | ||||
59 | return ""; | ||||
60 | } | ||||
61 | return std::string(info.sysname); | ||||
62 | } | ||||
63 | |||||
64 | // static | ||||
65 | std::string SysInfo::OperatingSystemVersion() { | ||||
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 66 | struct utsname info; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 67 | if (uname(&info) < 0) { |
68 | NOTREACHED(); | ||||
69 | return ""; | ||||
70 | } | ||||
71 | return std::string(info.release); | ||||
72 | } | ||||
[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 73 | #endif |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 74 | |
75 | // static | ||||
76 | std::string SysInfo::CPUArchitecture() { | ||||
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 77 | struct utsname info; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 78 | if (uname(&info) < 0) { |
79 | NOTREACHED(); | ||||
80 | return ""; | ||||
81 | } | ||||
[email protected] | 56d0cef | 2012-09-26 02:49:42 | [diff] [blame^] | 82 | std::string arch(info.machine); |
83 | if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") { | ||||
84 | arch = "x86"; | ||||
85 | } else if (arch == "amd64") { | ||||
86 | arch = "x86_64"; | ||||
87 | } | ||||
88 | return arch; | ||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 89 | } |
90 | |||||
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 91 | // static |
92 | size_t SysInfo::VMAllocationGranularity() { | ||||
93 | return getpagesize(); | ||||
94 | } | ||||
95 | |||||
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 96 | } // namespace base |