| 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 | #include "base/process/process_metrics.h" |
| 21 | #include "base/stl_util.h" |
| 22 | #include "base/strings/string_util.h" |
| 23 | #include "base/strings/stringprintf.h" |
| 24 |
| 25 | namespace base { |
| 26 |
| 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 = base::size(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 |
| 44 | // static |
| 45 | std::string SysInfo::OperatingSystemName() { |
| 46 | return "Mac OS X"; |
| 47 | } |
| 48 |
| 49 | // static |
| 50 | std::string SysInfo::OperatingSystemVersion() { |
| 51 | int32_t major, minor, bugfix; |
| 52 | OperatingSystemVersionNumbers(&major, &minor, &bugfix); |
| 53 | return base::StringPrintf("%d.%d.%d", major, minor, bugfix); |
| 54 | } |
| 55 |
| 56 | // static |
| 57 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 58 | int32_t* minor_version, |
| 59 | int32_t* bugfix_version) { |
| 60 | NSOperatingSystemVersion version = |
| 61 | [[NSProcessInfo processInfo] operatingSystemVersion]; |
| 62 | *major_version = version.majorVersion; |
| 63 | *minor_version = version.minorVersion; |
| 64 | *bugfix_version = version.patchVersion; |
| 65 | } |
| 66 |
| 67 | // static |
| 68 | int64_t SysInfo::AmountOfPhysicalMemoryImpl() { |
| 69 | struct host_basic_info hostinfo; |
| 70 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
| 71 | base::mac::ScopedMachSendRight host(mach_host_self()); |
| 72 | int result = host_info(host.get(), HOST_BASIC_INFO, |
| 73 | reinterpret_cast<host_info_t>(&hostinfo), &count); |
| 74 | if (result != KERN_SUCCESS) { |
| 75 | NOTREACHED(); |
| 76 | return 0; |
| 77 | } |
| 78 | DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
| 79 | return static_cast<int64_t>(hostinfo.max_mem); |
| 80 | } |
| 81 |
| 82 | // static |
| 83 | int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { |
| 84 | SystemMemoryInfoKB info; |
| 85 | if (!GetSystemMemoryInfo(&info)) |
| 86 | return 0; |
| 87 | // We should add inactive file-backed memory also but there is no such |
| 88 | // information from Mac OS unfortunately. |
| 89 | return static_cast<int64_t>(info.free + info.speculative) * 1024; |
| 90 | } |
| 91 |
| 92 | // static |
| 93 | std::string SysInfo::CPUModelName() { |
| 94 | return GetSysctlValue("machdep.cpu.brand_string"); |
| 95 | } |
| 96 |
| 97 | // static |
| 98 | std::string SysInfo::HardwareModelName() { |
| 99 | return GetSysctlValue("hw.model"); |
| 100 | } |
| 101 |
| 102 | // static |
| 103 | SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() { |
| 104 | HardwareInfo info; |
| 105 | info.manufacturer = "Apple Inc."; |
| 106 | info.model = HardwareModelName(); |
| 107 | DCHECK(IsStringUTF8(info.manufacturer)); |
| 108 | DCHECK(IsStringUTF8(info.model)); |
| 109 | return info; |
| 110 | } |
| 111 |
| 112 | } // namespace base |
| 113 |