Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 1 | // Copyright 2016 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" |
| 6 | |
| 7 | #include <ApplicationServices/ApplicationServices.h> |
| 8 | #include <CoreServices/CoreServices.h> |
| 9 | #import <Foundation/Foundation.h> |
| 10 | #include <mach/mach_host.h> |
| 11 | #include <mach/mach_init.h> |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 | #include <sys/sysctl.h> |
| 15 | #include <sys/types.h> |
| 16 | |
| 17 | #include "base/logging.h" |
| 18 | #include "base/mac/mac_util.h" |
| 19 | #include "base/mac/scoped_mach_port.h" |
| 20 | #import "base/mac/sdk_forward_declarations.h" |
| 21 | #include "base/macros.h" |
| 22 | #include "base/process/process_metrics.h" |
| 23 | #include "base/strings/string_util.h" |
| 24 | #include "base/strings/stringprintf.h" |
| 25 | |
| 26 | namespace base { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | // Queries sysctlbyname() for the given key and returns the value from the |
| 31 | // system or the empty string on failure. |
| 32 | std::string GetSysctlValue(const char* key_name) { |
| 33 | char value[256]; |
| 34 | size_t len = arraysize(value); |
| 35 | if (sysctlbyname(key_name, &value, &len, nullptr, 0) == 0) { |
| 36 | DCHECK_GE(len, 1u); |
| 37 | DCHECK_EQ('\0', value[len - 1]); |
| 38 | return std::string(value, len - 1); |
| 39 | } |
| 40 | return std::string(); |
| 41 | } |
| 42 | |
| 43 | } // namespace |
| 44 | |
| 45 | // static |
| 46 | std::string SysInfo::OperatingSystemName() { |
| 47 | return "Mac OS X"; |
| 48 | } |
| 49 | |
| 50 | // static |
| 51 | std::string SysInfo::OperatingSystemVersion() { |
| 52 | int32_t major, minor, bugfix; |
| 53 | OperatingSystemVersionNumbers(&major, &minor, &bugfix); |
| 54 | return base::StringPrintf("%d.%d.%d", major, minor, bugfix); |
| 55 | } |
| 56 | |
| 57 | // static |
| 58 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 59 | int32_t* minor_version, |
| 60 | int32_t* bugfix_version) { |
| 61 | if (@available(macOS 10.10, *)) { |
| 62 | NSOperatingSystemVersion version = |
| 63 | [[NSProcessInfo processInfo] operatingSystemVersion]; |
| 64 | *major_version = version.majorVersion; |
| 65 | *minor_version = version.minorVersion; |
| 66 | *bugfix_version = version.patchVersion; |
| 67 | } else { |
| 68 | NOTREACHED(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // static |
| 73 | int64_t SysInfo::AmountOfPhysicalMemoryImpl() { |
| 74 | struct host_basic_info hostinfo; |
| 75 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
| 76 | base::mac::ScopedMachSendRight host(mach_host_self()); |
| 77 | int result = host_info(host.get(), |
| 78 | HOST_BASIC_INFO, |
| 79 | reinterpret_cast<host_info_t>(&hostinfo), |
| 80 | &count); |
| 81 | if (result != KERN_SUCCESS) { |
| 82 | NOTREACHED(); |
| 83 | return 0; |
| 84 | } |
| 85 | DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
| 86 | return static_cast<int64_t>(hostinfo.max_mem); |
| 87 | } |
| 88 | |
| 89 | // static |
| 90 | int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { |
| 91 | SystemMemoryInfoKB info; |
| 92 | if (!GetSystemMemoryInfo(&info)) |
| 93 | return 0; |
| 94 | // We should add inactive file-backed memory also but there is no such |
| 95 | // information from Mac OS unfortunately. |
| 96 | return static_cast<int64_t>(info.free + info.speculative) * 1024; |
| 97 | } |
| 98 | |
| 99 | // static |
| 100 | std::string SysInfo::CPUModelName() { |
| 101 | return GetSysctlValue("machdep.cpu.brand_string"); |
| 102 | } |
| 103 | |
| 104 | // static |
| 105 | std::string SysInfo::HardwareModelName() { |
| 106 | return GetSysctlValue("hw.model"); |
| 107 | } |
| 108 | |
| 109 | // static |
| 110 | SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() { |
| 111 | HardwareInfo info; |
| 112 | info.manufacturer = "Apple Inc."; |
| 113 | info.model = HardwareModelName(); |
| 114 | DCHECK(IsStringUTF8(info.manufacturer)); |
| 115 | DCHECK(IsStringUTF8(info.model)); |
| 116 | return info; |
| 117 | } |
| 118 | |
| 119 | } // namespace base |