[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 1 | // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
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" | ||||
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 6 | #include "base/basictypes.h" |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 7 | |
8 | #include <errno.h> | ||||
9 | #include <string.h> | ||||
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 10 | #include <sys/statvfs.h> |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 11 | #include <unistd.h> |
12 | |||||
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 13 | #if defined(OS_MACOSX) |
14 | #include <mach/mach_host.h> | ||||
15 | #include <mach/mach_init.h> | ||||
16 | #endif | ||||
17 | |||||
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 18 | #include "base/logging.h" |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 19 | #include "base/string_util.h" |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 20 | |
21 | namespace base { | ||||
22 | |||||
23 | int SysInfo::NumberOfProcessors() { | ||||
24 | // It seems that sysconf returns the number of "logical" processors on both | ||||
25 | // mac and linux. So we get the number of "online logical" processors. | ||||
26 | long res = sysconf(_SC_NPROCESSORS_ONLN); | ||||
27 | if (res == -1) { | ||||
28 | NOTREACHED(); | ||||
29 | return 1; | ||||
30 | } | ||||
31 | |||||
32 | return static_cast<int>(res); | ||||
33 | } | ||||
34 | |||||
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 35 | // static |
36 | int64 SysInfo::AmountOfPhysicalMemory() { | ||||
37 | // _SC_PHYS_PAGES is not part of POSIX and not available on OS X | ||||
38 | #if defined(OS_MACOSX) | ||||
39 | struct host_basic_info hostinfo; | ||||
40 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; | ||||
41 | int result = host_info(mach_host_self(), | ||||
42 | HOST_BASIC_INFO, | ||||
43 | reinterpret_cast<host_info_t>(&hostinfo), | ||||
44 | &count); | ||||
45 | DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); | ||||
46 | if (result != KERN_SUCCESS) { | ||||
47 | NOTREACHED(); | ||||
48 | return 0; | ||||
49 | } | ||||
50 | |||||
51 | return static_cast<int64>(hostinfo.max_mem); | ||||
52 | #else | ||||
53 | long pages = sysconf(_SC_PHYS_PAGES); | ||||
54 | long page_size = sysconf(_SC_PAGE_SIZE); | ||||
55 | if (pages == -1 || page_size == -1) { | ||||
56 | NOTREACHED(); | ||||
57 | return 0; | ||||
58 | } | ||||
59 | |||||
60 | return static_cast<int64>(pages) * page_size; | ||||
61 | #endif | ||||
62 | } | ||||
63 | |||||
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 64 | // static |
65 | int64 SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) { | ||||
66 | struct statvfs stats; | ||||
67 | if (statvfs(WideToUTF8(path).c_str(), &stats) != 0) { | ||||
[email protected] | 02ee34a | 2008-09-20 01:16:23 | [diff] [blame^] | 68 | return -1; |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 69 | } |
70 | return static_cast<int64>(stats.f_bavail) * stats.f_frsize; | ||||
71 | } | ||||
72 | |||||
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 73 | } // namespace base |