[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 7 | #include <mach/mach.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 10 | #include <sys/sysctl.h> |
| 11 | #include <sys/types.h> |
tapted | 574f09c | 2015-05-19 13:08:08 | [diff] [blame] | 12 | #import <UIKit/UIKit.h> |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 13 | |
| 14 | #include "base/logging.h" |
[email protected] | 431d0a1 | 2012-10-16 20:17:58 | [diff] [blame] | 15 | #include "base/mac/scoped_mach_port.h" |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 16 | #include "base/mac/scoped_nsautorelease_pool.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 17 | #include "base/macros.h" |
mkolom | 01ac10b | 2017-03-22 01:47:29 | [diff] [blame] | 18 | #include "base/process/process_metrics.h" |
[email protected] | 9fe1a5b | 2013-02-07 19:18:03 | [diff] [blame] | 19 | #include "base/strings/sys_string_conversions.h" |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 20 | |
| 21 | namespace base { |
| 22 | |
asvitkine | 666e5ee | 2017-05-04 22:52:00 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | // Queries sysctlbyname() for the given key and returns the value from the |
| 26 | // system or the empty string on failure. |
| 27 | std::string GetSysctlValue(const char* key_name) { |
| 28 | char value[256]; |
| 29 | size_t len = arraysize(value); |
| 30 | if (sysctlbyname(key_name, &value, &len, nullptr, 0) == 0) { |
| 31 | DCHECK_GE(len, 1u); |
| 32 | DCHECK_EQ('\0', value[len - 1]); |
| 33 | return std::string(value, len - 1); |
| 34 | } |
| 35 | return std::string(); |
| 36 | } |
| 37 | |
| 38 | } // namespace |
| 39 | |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 40 | // static |
| 41 | std::string SysInfo::OperatingSystemName() { |
[email protected] | dba7cf2 | 2012-12-10 18:50:35 | [diff] [blame] | 42 | static dispatch_once_t get_system_name_once; |
| 43 | static std::string* system_name; |
| 44 | dispatch_once(&get_system_name_once, ^{ |
| 45 | base::mac::ScopedNSAutoreleasePool pool; |
| 46 | system_name = new std::string( |
| 47 | SysNSStringToUTF8([[UIDevice currentDevice] systemName])); |
| 48 | }); |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 49 | // Examples of returned value: 'iPhone OS' on iPad 5.1.1 |
| 50 | // and iPhone 5.1.1. |
[email protected] | dba7cf2 | 2012-12-10 18:50:35 | [diff] [blame] | 51 | return *system_name; |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // static |
| 55 | std::string SysInfo::OperatingSystemVersion() { |
[email protected] | dba7cf2 | 2012-12-10 18:50:35 | [diff] [blame] | 56 | static dispatch_once_t get_system_version_once; |
| 57 | static std::string* system_version; |
| 58 | dispatch_once(&get_system_version_once, ^{ |
| 59 | base::mac::ScopedNSAutoreleasePool pool; |
| 60 | system_version = new std::string( |
| 61 | SysNSStringToUTF8([[UIDevice currentDevice] systemVersion])); |
| 62 | }); |
| 63 | return *system_version; |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 67 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 68 | int32_t* minor_version, |
| 69 | int32_t* bugfix_version) { |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 70 | base::mac::ScopedNSAutoreleasePool pool; |
[email protected] | dba7cf2 | 2012-12-10 18:50:35 | [diff] [blame] | 71 | std::string system_version = OperatingSystemVersion(); |
| 72 | if (!system_version.empty()) { |
| 73 | // Try to parse out the version numbers from the string. |
| 74 | int num_read = sscanf(system_version.c_str(), "%d.%d.%d", major_version, |
| 75 | minor_version, bugfix_version); |
| 76 | if (num_read < 1) |
| 77 | *major_version = 0; |
| 78 | if (num_read < 2) |
| 79 | *minor_version = 0; |
| 80 | if (num_read < 3) |
| 81 | *bugfix_version = 0; |
| 82 | } |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // static |
Eric Karl | a628c09 | 2017-07-26 01:18:37 | [diff] [blame] | 86 | int64_t SysInfo::AmountOfPhysicalMemoryImpl() { |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [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] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 91 | HOST_BASIC_INFO, |
| 92 | reinterpret_cast<host_info_t>(&hostinfo), |
| 93 | &count); |
| 94 | if (result != KERN_SUCCESS) { |
| 95 | NOTREACHED(); |
| 96 | return 0; |
| 97 | } |
| 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] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 102 | // static |
Eric Karl | a628c09 | 2017-07-26 01:18:37 | [diff] [blame] | 103 | int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { |
mkolom | 01ac10b | 2017-03-22 01:47:29 | [diff] [blame] | 104 | SystemMemoryInfoKB info; |
| 105 | if (!GetSystemMemoryInfo(&info)) |
[email protected] | 381bc39 | 2014-03-05 10:28:59 | [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 iOS unfortunately. |
| 109 | return static_cast<int64_t>(info.free + info.speculative) * 1024; |
[email protected] | 381bc39 | 2014-03-05 10:28:59 | [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"); |
| 115 | } |
| 116 | |
| 117 | // static |
| 118 | std::string SysInfo::HardwareModelName() { |
| 119 | #if TARGET_OS_SIMULATOR |
| 120 | // On the simulator, "hw.machine" returns "i386" or "x86_64" which doesn't |
| 121 | // match the expected format, so supply a fake string here. |
| 122 | return "Simulator1,1"; |
| 123 | #else |
| 124 | // Note: This uses "hw.machine" instead of "hw.model" like the Mac code, |
| 125 | // because "hw.model" doesn't always return the right string on some devices. |
| 126 | return GetSysctlValue("hw.machine"); |
| 127 | #endif |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 130 | } // namespace base |