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" |
mkolom | 01ac10b | 2017-03-22 01:47:29 | [diff] [blame] | 22 | #include "base/process/process_metrics.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 23 | #include "base/strings/stringprintf.h" |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 24 | |
| 25 | namespace base { |
| 26 | |
asvitkine | 666e5ee | 2017-05-04 22:52:00 | [diff] [blame^] | 27 | namespace { |
| 28 | |
| 29 | // Queries sysctlbyname() for the given key and returns the value from the |
| 30 | // system or the empty string on failure. |
| 31 | std::string GetSysctlValue(const char* key_name) { |
| 32 | char value[256]; |
| 33 | size_t len = arraysize(value); |
| 34 | if (sysctlbyname(key_name, &value, &len, nullptr, 0) == 0) { |
| 35 | DCHECK_GE(len, 1u); |
| 36 | DCHECK_EQ('\0', value[len - 1]); |
| 37 | return std::string(value, len - 1); |
| 38 | } |
| 39 | return std::string(); |
| 40 | } |
| 41 | |
| 42 | } // namespace |
| 43 | |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 44 | // static |
[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 45 | std::string SysInfo::OperatingSystemName() { |
| 46 | return "Mac OS X"; |
| 47 | } |
| 48 | |
| 49 | // static |
| 50 | std::string SysInfo::OperatingSystemVersion() { |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 51 | int32_t major, minor, bugfix; |
[email protected] | 6e001bb | 2011-05-25 20:53:18 | [diff] [blame] | 52 | OperatingSystemVersionNumbers(&major, &minor, &bugfix); |
| 53 | return base::StringPrintf("%d.%d.%d", major, minor, bugfix); |
| 54 | } |
| 55 | |
| 56 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 57 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 58 | int32_t* minor_version, |
| 59 | int32_t* bugfix_version) { |
tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame] | 60 | NSProcessInfo* processInfo = [NSProcessInfo processInfo]; |
| 61 | if ([processInfo respondsToSelector:@selector(operatingSystemVersion)]) { |
| 62 | NSOperatingSystemVersion version = [processInfo operatingSystemVersion]; |
| 63 | *major_version = version.majorVersion; |
| 64 | *minor_version = version.minorVersion; |
| 65 | *bugfix_version = version.patchVersion; |
| 66 | } else { |
| 67 | // -[NSProcessInfo operatingSystemVersion] is documented available in 10.10. |
| 68 | // It's also available via a private API since 10.9.2. For the remaining |
| 69 | // cases in 10.9, rely on ::Gestalt(..). Since this code is only needed for |
| 70 | // 10.9.0 and 10.9.1 and uses the recommended replacement thereafter, |
| 71 | // suppress the warning for this fallback case. |
sdy | 07171a4 | 2016-08-30 18:22:04 | [diff] [blame] | 72 | DCHECK(base::mac::IsOS10_9()); |
tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame] | 73 | #pragma clang diagnostic push |
| 74 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 75 | Gestalt(gestaltSystemVersionMajor, |
| 76 | reinterpret_cast<SInt32*>(major_version)); |
| 77 | Gestalt(gestaltSystemVersionMinor, |
| 78 | reinterpret_cast<SInt32*>(minor_version)); |
| 79 | Gestalt(gestaltSystemVersionBugFix, |
| 80 | reinterpret_cast<SInt32*>(bugfix_version)); |
| 81 | #pragma clang diagnostic pop |
| 82 | } |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 85 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 86 | int64_t SysInfo::AmountOfPhysicalMemory() { |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 87 | struct host_basic_info hostinfo; |
| 88 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
[email protected] | a07893f8 | 2014-05-28 23:40:08 | [diff] [blame] | 89 | base::mac::ScopedMachSendRight host(mach_host_self()); |
mark | da902e18 | 2015-10-20 18:36:13 | [diff] [blame] | 90 | int result = host_info(host.get(), |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 91 | HOST_BASIC_INFO, |
| 92 | reinterpret_cast<host_info_t>(&hostinfo), |
| 93 | &count); |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 94 | if (result != KERN_SUCCESS) { |
| 95 | NOTREACHED(); |
| 96 | return 0; |
| 97 | } |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 98 | DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 99 | return static_cast<int64_t>(hostinfo.max_mem); |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 102 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 103 | int64_t SysInfo::AmountOfAvailablePhysicalMemory() { |
mkolom | 01ac10b | 2017-03-22 01:47:29 | [diff] [blame] | 104 | SystemMemoryInfoKB info; |
| 105 | if (!GetSystemMemoryInfo(&info)) |
[email protected] | e6085c3 | 2012-11-07 06:58:14 | [diff] [blame] | 106 | return 0; |
mkolom | 01ac10b | 2017-03-22 01:47:29 | [diff] [blame] | 107 | // We should add inactive file-backed memory also but there is no such |
| 108 | // information from Mac OS unfortunately. |
| 109 | return static_cast<int64_t>(info.free + info.speculative) * 1024; |
[email protected] | 52bdcda | 2012-10-27 06:10:37 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // static |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 113 | std::string SysInfo::CPUModelName() { |
asvitkine | 666e5ee | 2017-05-04 22:52:00 | [diff] [blame^] | 114 | return GetSysctlValue("machdep.cpu.brand_string"); |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 115 | } |
| 116 | |
asvitkine | 666e5ee | 2017-05-04 22:52:00 | [diff] [blame^] | 117 | // static |
jeremy | ba4eca9 | 2014-11-06 18:47:10 | [diff] [blame] | 118 | std::string SysInfo::HardwareModelName() { |
asvitkine | 666e5ee | 2017-05-04 22:52:00 | [diff] [blame^] | 119 | return GetSysctlValue("hw.model"); |
jeremy | ba4eca9 | 2014-11-06 18:47:10 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 122 | } // namespace base |