[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> |
| 10 | #include <unistd.h> |
| 11 | |
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame^] | 12 | #if defined(OS_MACOSX) |
| 13 | #include <mach/mach_host.h> |
| 14 | #include <mach/mach_init.h> |
| 15 | #endif |
| 16 | |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 17 | #include "base/logging.h" |
| 18 | |
| 19 | namespace base { |
| 20 | |
| 21 | int SysInfo::NumberOfProcessors() { |
| 22 | // It seems that sysconf returns the number of "logical" processors on both |
| 23 | // mac and linux. So we get the number of "online logical" processors. |
| 24 | long res = sysconf(_SC_NPROCESSORS_ONLN); |
| 25 | if (res == -1) { |
| 26 | NOTREACHED(); |
| 27 | return 1; |
| 28 | } |
| 29 | |
| 30 | return static_cast<int>(res); |
| 31 | } |
| 32 | |
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame^] | 33 | // static |
| 34 | int64 SysInfo::AmountOfPhysicalMemory() { |
| 35 | // _SC_PHYS_PAGES is not part of POSIX and not available on OS X |
| 36 | #if defined(OS_MACOSX) |
| 37 | struct host_basic_info hostinfo; |
| 38 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
| 39 | int result = host_info(mach_host_self(), |
| 40 | HOST_BASIC_INFO, |
| 41 | reinterpret_cast<host_info_t>(&hostinfo), |
| 42 | &count); |
| 43 | DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
| 44 | if (result != KERN_SUCCESS) { |
| 45 | NOTREACHED(); |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | return static_cast<int64>(hostinfo.max_mem); |
| 50 | #else |
| 51 | long pages = sysconf(_SC_PHYS_PAGES); |
| 52 | long page_size = sysconf(_SC_PAGE_SIZE); |
| 53 | if (pages == -1 || page_size == -1) { |
| 54 | NOTREACHED(); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | return static_cast<int64>(pages) * page_size; |
| 59 | #endif |
| 60 | } |
| 61 | |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 62 | } // namespace base |