[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [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 | |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 7 | #include <ApplicationServices/ApplicationServices.h> |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 8 | #include <CoreServices/CoreServices.h> |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 9 | #include <mach/mach_host.h> |
| 10 | #include <mach/mach_init.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 11 | #include <stddef.h> |
| 12 | #include <stdint.h> |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 13 | #include <sys/sysctl.h> |
| 14 | #include <sys/types.h> |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 15 | |
| 16 | #include "base/logging.h" |
[email protected] | 431d0a1 | 2012-10-16 20:17:58 | [diff] [blame] | 17 | #include "base/mac/scoped_mach_port.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 18 | #include "base/macros.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 19 | #include "base/strings/stringprintf.h" |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 20 | |
| 21 | namespace base { |
| 22 | |
| 23 | // static |
[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 24 | std::string SysInfo::OperatingSystemName() { |
| 25 | return "Mac OS X"; |
| 26 | } |
| 27 | |
| 28 | // static |
| 29 | std::string SysInfo::OperatingSystemVersion() { |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 30 | int32_t major, minor, bugfix; |
[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 31 | OperatingSystemVersionNumbers(&major, &minor, &bugfix); |
| 32 | return base::StringPrintf("%d.%d.%d", major, minor, bugfix); |
| 33 | } |
| 34 | |
| 35 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 36 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 37 | int32_t* minor_version, |
| 38 | int32_t* bugfix_version) { |
[email protected] | 0203ed7 | 2009-07-02 13:59:08 | [diff] [blame] | 39 | Gestalt(gestaltSystemVersionMajor, |
[email protected] | 3a3e5b3 | 2009-08-21 22:30:47 | [diff] [blame] | 40 | reinterpret_cast<SInt32*>(major_version)); |
[email protected] | 0203ed7 | 2009-07-02 13:59:08 | [diff] [blame] | 41 | Gestalt(gestaltSystemVersionMinor, |
[email protected] | 3a3e5b3 | 2009-08-21 22:30:47 | [diff] [blame] | 42 | reinterpret_cast<SInt32*>(minor_version)); |
[email protected] | 0203ed7 | 2009-07-02 13:59:08 | [diff] [blame] | 43 | Gestalt(gestaltSystemVersionBugFix, |
[email protected] | 3a3e5b3 | 2009-08-21 22:30:47 | [diff] [blame] | 44 | reinterpret_cast<SInt32*>(bugfix_version)); |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 47 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 48 | int64_t SysInfo::AmountOfPhysicalMemory() { |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 49 | struct host_basic_info hostinfo; |
| 50 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
[email protected] | a07893f8 | 2014-05-28 23:40:08 | [diff] [blame] | 51 | base::mac::ScopedMachSendRight host(mach_host_self()); |
mark | da902e18 | 2015-10-20 18:36:13 | [diff] [blame] | 52 | int result = host_info(host.get(), |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 53 | HOST_BASIC_INFO, |
| 54 | reinterpret_cast<host_info_t>(&hostinfo), |
| 55 | &count); |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 56 | if (result != KERN_SUCCESS) { |
| 57 | NOTREACHED(); |
| 58 | return 0; |
| 59 | } |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 60 | DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 61 | return static_cast<int64_t>(hostinfo.max_mem); |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 64 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 65 | int64_t SysInfo::AmountOfAvailablePhysicalMemory() { |
[email protected] | a07893f8 | 2014-05-28 23:40:08 | [diff] [blame] | 66 | base::mac::ScopedMachSendRight host(mach_host_self()); |
[email protected] | e6085c3 | 2012-11-07 06:58:14 | [diff] [blame] | 67 | vm_statistics_data_t vm_info; |
| 68 | mach_msg_type_number_t count = HOST_VM_INFO_COUNT; |
| 69 | |
| 70 | if (host_statistics(host.get(), |
| 71 | HOST_VM_INFO, |
| 72 | reinterpret_cast<host_info_t>(&vm_info), |
| 73 | &count) != KERN_SUCCESS) { |
| 74 | NOTREACHED(); |
| 75 | return 0; |
| 76 | } |
| 77 | |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 78 | return static_cast<int64_t>(vm_info.free_count - vm_info.speculative_count) * |
| 79 | PAGE_SIZE; |
[email protected] | 52bdcda | 2012-10-27 06:10:37 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // static |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 83 | std::string SysInfo::CPUModelName() { |
| 84 | char name[256]; |
| 85 | size_t len = arraysize(name); |
| 86 | if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0) |
| 87 | return name; |
| 88 | return std::string(); |
| 89 | } |
| 90 | |
jeremy | ba4eca9 | 2014-11-06 18:47:10 | [diff] [blame] | 91 | std::string SysInfo::HardwareModelName() { |
| 92 | char model[256]; |
| 93 | size_t len = sizeof(model); |
| 94 | if (sysctlbyname("hw.model", model, &len, NULL, 0) == 0) |
| 95 | return std::string(model, 0, len); |
| 96 | return std::string(); |
| 97 | } |
| 98 | |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 99 | } // namespace base |