tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame^] | 1 | // Copyright 2016 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> |
tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame^] | 9 | #import <Foundation/Foundation.h> |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 10 | #include <mach/mach_host.h> |
| 11 | #include <mach/mach_init.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 14 | #include <sys/sysctl.h> |
| 15 | #include <sys/types.h> |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 16 | |
| 17 | #include "base/logging.h" |
tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame^] | 18 | #include "base/mac/mac_util.h" |
[email protected] | 431d0a1 | 2012-10-16 20:17:58 | [diff] [blame] | 19 | #include "base/mac/scoped_mach_port.h" |
tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame^] | 20 | #import "base/mac/sdk_forward_declarations.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 21 | #include "base/macros.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 22 | #include "base/strings/stringprintf.h" |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 23 | |
| 24 | namespace base { |
| 25 | |
| 26 | // static |
[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 27 | std::string SysInfo::OperatingSystemName() { |
| 28 | return "Mac OS X"; |
| 29 | } |
| 30 | |
| 31 | // static |
| 32 | std::string SysInfo::OperatingSystemVersion() { |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 33 | int32_t major, minor, bugfix; |
[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 34 | OperatingSystemVersionNumbers(&major, &minor, &bugfix); |
| 35 | return base::StringPrintf("%d.%d.%d", major, minor, bugfix); |
| 36 | } |
| 37 | |
| 38 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 39 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 40 | int32_t* minor_version, |
| 41 | int32_t* bugfix_version) { |
tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame^] | 42 | NSProcessInfo* processInfo = [NSProcessInfo processInfo]; |
| 43 | if ([processInfo respondsToSelector:@selector(operatingSystemVersion)]) { |
| 44 | NSOperatingSystemVersion version = [processInfo operatingSystemVersion]; |
| 45 | *major_version = version.majorVersion; |
| 46 | *minor_version = version.minorVersion; |
| 47 | *bugfix_version = version.patchVersion; |
| 48 | } else { |
| 49 | // -[NSProcessInfo operatingSystemVersion] is documented available in 10.10. |
| 50 | // It's also available via a private API since 10.9.2. For the remaining |
| 51 | // cases in 10.9, rely on ::Gestalt(..). Since this code is only needed for |
| 52 | // 10.9.0 and 10.9.1 and uses the recommended replacement thereafter, |
| 53 | // suppress the warning for this fallback case. |
| 54 | DCHECK(base::mac::IsOSMavericks()); |
| 55 | #pragma clang diagnostic push |
| 56 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 57 | Gestalt(gestaltSystemVersionMajor, |
| 58 | reinterpret_cast<SInt32*>(major_version)); |
| 59 | Gestalt(gestaltSystemVersionMinor, |
| 60 | reinterpret_cast<SInt32*>(minor_version)); |
| 61 | Gestalt(gestaltSystemVersionBugFix, |
| 62 | reinterpret_cast<SInt32*>(bugfix_version)); |
| 63 | #pragma clang diagnostic pop |
| 64 | } |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 67 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 68 | int64_t SysInfo::AmountOfPhysicalMemory() { |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 69 | struct host_basic_info hostinfo; |
| 70 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
[email protected] | a07893f8 | 2014-05-28 23:40:08 | [diff] [blame] | 71 | base::mac::ScopedMachSendRight host(mach_host_self()); |
mark | da902e18 | 2015-10-20 18:36:13 | [diff] [blame] | 72 | int result = host_info(host.get(), |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 73 | HOST_BASIC_INFO, |
| 74 | reinterpret_cast<host_info_t>(&hostinfo), |
| 75 | &count); |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 76 | if (result != KERN_SUCCESS) { |
| 77 | NOTREACHED(); |
| 78 | return 0; |
| 79 | } |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 80 | DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 81 | return static_cast<int64_t>(hostinfo.max_mem); |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 84 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 85 | int64_t SysInfo::AmountOfAvailablePhysicalMemory() { |
[email protected] | a07893f8 | 2014-05-28 23:40:08 | [diff] [blame] | 86 | base::mac::ScopedMachSendRight host(mach_host_self()); |
[email protected] | e6085c3 | 2012-11-07 06:58:14 | [diff] [blame] | 87 | vm_statistics_data_t vm_info; |
| 88 | mach_msg_type_number_t count = HOST_VM_INFO_COUNT; |
| 89 | |
| 90 | if (host_statistics(host.get(), |
| 91 | HOST_VM_INFO, |
| 92 | reinterpret_cast<host_info_t>(&vm_info), |
| 93 | &count) != KERN_SUCCESS) { |
| 94 | NOTREACHED(); |
| 95 | return 0; |
| 96 | } |
| 97 | |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 98 | return static_cast<int64_t>(vm_info.free_count - vm_info.speculative_count) * |
| 99 | PAGE_SIZE; |
[email protected] | 52bdcda | 2012-10-27 06:10:37 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // static |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 103 | std::string SysInfo::CPUModelName() { |
| 104 | char name[256]; |
| 105 | size_t len = arraysize(name); |
| 106 | if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0) |
| 107 | return name; |
| 108 | return std::string(); |
| 109 | } |
| 110 | |
jeremy | ba4eca9 | 2014-11-06 18:47:10 | [diff] [blame] | 111 | std::string SysInfo::HardwareModelName() { |
| 112 | char model[256]; |
| 113 | size_t len = sizeof(model); |
| 114 | if (sysctlbyname("hw.model", model, &len, NULL, 0) == 0) |
| 115 | return std::string(model, 0, len); |
| 116 | return std::string(); |
| 117 | } |
| 118 | |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 119 | } // namespace base |