tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
jeremy@chromium.org | 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 | |
evan@chromium.org | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 7 | #include <ApplicationServices/ApplicationServices.h> |
jeremy@chromium.org | 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> |
evan@chromium.org | 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> |
hongbo.min@intel.com | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 14 | #include <sys/sysctl.h> |
| 15 | #include <sys/types.h> |
evan@chromium.org | 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" |
rsesek@chromium.org | 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" |
avi@chromium.org | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 23 | #include "base/strings/stringprintf.h" |
jeremy@chromium.org | 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 | |
jeremy@chromium.org | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 44 | // static |
stuartmorgan@chromium.org | 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; |
stuartmorgan@chromium.org | 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]; |
Peter Collingbourne | 8cc8696f | 2017-07-11 18:36:42 | [diff] [blame] | 61 | // We should try to avoid using Gestalt here because it has been observed to |
| 62 | // spin up threads among other things. Using an availability check here would |
| 63 | // prevent us from using the private API in 10.9.2. So use a |
| 64 | // respondsToSelector check here instead and silence the warning. |
tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame] | 65 | if ([processInfo respondsToSelector:@selector(operatingSystemVersion)]) { |
Peter Collingbourne | 8cc8696f | 2017-07-11 18:36:42 | [diff] [blame] | 66 | #pragma clang diagnostic push |
| 67 | #pragma clang diagnostic ignored "-Wunguarded-availability" |
tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame] | 68 | NSOperatingSystemVersion version = [processInfo operatingSystemVersion]; |
Peter Collingbourne | 8cc8696f | 2017-07-11 18:36:42 | [diff] [blame] | 69 | #pragma clang diagnostic pop |
tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame] | 70 | *major_version = version.majorVersion; |
| 71 | *minor_version = version.minorVersion; |
| 72 | *bugfix_version = version.patchVersion; |
| 73 | } else { |
| 74 | // -[NSProcessInfo operatingSystemVersion] is documented available in 10.10. |
| 75 | // It's also available via a private API since 10.9.2. For the remaining |
| 76 | // cases in 10.9, rely on ::Gestalt(..). Since this code is only needed for |
| 77 | // 10.9.0 and 10.9.1 and uses the recommended replacement thereafter, |
| 78 | // suppress the warning for this fallback case. |
sdy | 07171a4 | 2016-08-30 18:22:04 | [diff] [blame] | 79 | DCHECK(base::mac::IsOS10_9()); |
tapted | 0ffa1e4 | 2016-03-31 22:28:27 | [diff] [blame] | 80 | #pragma clang diagnostic push |
| 81 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 82 | Gestalt(gestaltSystemVersionMajor, |
| 83 | reinterpret_cast<SInt32*>(major_version)); |
| 84 | Gestalt(gestaltSystemVersionMinor, |
| 85 | reinterpret_cast<SInt32*>(minor_version)); |
| 86 | Gestalt(gestaltSystemVersionBugFix, |
| 87 | reinterpret_cast<SInt32*>(bugfix_version)); |
| 88 | #pragma clang diagnostic pop |
| 89 | } |
jeremy@chromium.org | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 90 | } |
| 91 | |
evan@chromium.org | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 92 | // static |
Eric Karl | a628c09 | 2017-07-26 01:18:37 | [diff] [blame^] | 93 | int64_t SysInfo::AmountOfPhysicalMemoryImpl() { |
evan@chromium.org | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 94 | struct host_basic_info hostinfo; |
| 95 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
rsesek@chromium.org | a07893f | 2014-05-28 23:40:08 | [diff] [blame] | 96 | base::mac::ScopedMachSendRight host(mach_host_self()); |
mark | da902e18 | 2015-10-20 18:36:13 | [diff] [blame] | 97 | int result = host_info(host.get(), |
evan@chromium.org | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 98 | HOST_BASIC_INFO, |
| 99 | reinterpret_cast<host_info_t>(&hostinfo), |
| 100 | &count); |
evan@chromium.org | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 101 | if (result != KERN_SUCCESS) { |
| 102 | NOTREACHED(); |
| 103 | return 0; |
| 104 | } |
chenyu@chromium.org | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 105 | DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 106 | return static_cast<int64_t>(hostinfo.max_mem); |
evan@chromium.org | c858763 | 2009-11-12 02:17:19 | [diff] [blame] | 107 | } |
| 108 | |
hongbo.min@intel.com | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 109 | // static |
Eric Karl | a628c09 | 2017-07-26 01:18:37 | [diff] [blame^] | 110 | int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { |
mkolom | 01ac10b | 2017-03-22 01:47:29 | [diff] [blame] | 111 | SystemMemoryInfoKB info; |
| 112 | if (!GetSystemMemoryInfo(&info)) |
hongbo.min@intel.com | e6085c3 | 2012-11-07 06:58:14 | [diff] [blame] | 113 | return 0; |
mkolom | 01ac10b | 2017-03-22 01:47:29 | [diff] [blame] | 114 | // We should add inactive file-backed memory also but there is no such |
| 115 | // information from Mac OS unfortunately. |
| 116 | return static_cast<int64_t>(info.free + info.speculative) * 1024; |
hongbo.min@intel.com | 52bdcda | 2012-10-27 06:10:37 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // static |
hongbo.min@intel.com | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 120 | std::string SysInfo::CPUModelName() { |
asvitkine | 666e5ee | 2017-05-04 22:52:00 | [diff] [blame] | 121 | return GetSysctlValue("machdep.cpu.brand_string"); |
hongbo.min@intel.com | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 122 | } |
| 123 | |
asvitkine | 666e5ee | 2017-05-04 22:52:00 | [diff] [blame] | 124 | // static |
jeremy | ba4eca9 | 2014-11-06 18:47:10 | [diff] [blame] | 125 | std::string SysInfo::HardwareModelName() { |
asvitkine | 666e5ee | 2017-05-04 22:52:00 | [diff] [blame] | 126 | return GetSysctlValue("hw.model"); |
jeremy | ba4eca9 | 2014-11-06 18:47:10 | [diff] [blame] | 127 | } |
| 128 | |
jeremy@chromium.org | 71aa16c | 2009-02-24 16:37:13 | [diff] [blame] | 129 | } // namespace base |