[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> |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 8 | #include <sys/sysctl.h> |
| 9 | #include <sys/types.h> |
tapted | 574f09c | 2015-05-19 13:08:08 | [diff] [blame] | 10 | #import <UIKit/UIKit.h> |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 11 | |
| 12 | #include "base/logging.h" |
[email protected] | 431d0a1 | 2012-10-16 20:17:58 | [diff] [blame] | 13 | #include "base/mac/scoped_mach_port.h" |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 14 | #include "base/mac/scoped_nsautorelease_pool.h" |
[email protected] | 9fe1a5b | 2013-02-07 19:18:03 | [diff] [blame] | 15 | #include "base/strings/sys_string_conversions.h" |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | // static |
| 20 | std::string SysInfo::OperatingSystemName() { |
[email protected] | dba7cf2 | 2012-12-10 18:50:35 | [diff] [blame] | 21 | static dispatch_once_t get_system_name_once; |
| 22 | static std::string* system_name; |
| 23 | dispatch_once(&get_system_name_once, ^{ |
| 24 | base::mac::ScopedNSAutoreleasePool pool; |
| 25 | system_name = new std::string( |
| 26 | SysNSStringToUTF8([[UIDevice currentDevice] systemName])); |
| 27 | }); |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 28 | // Examples of returned value: 'iPhone OS' on iPad 5.1.1 |
| 29 | // and iPhone 5.1.1. |
[email protected] | dba7cf2 | 2012-12-10 18:50:35 | [diff] [blame] | 30 | return *system_name; |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // static |
| 34 | std::string SysInfo::OperatingSystemVersion() { |
[email protected] | dba7cf2 | 2012-12-10 18:50:35 | [diff] [blame] | 35 | static dispatch_once_t get_system_version_once; |
| 36 | static std::string* system_version; |
| 37 | dispatch_once(&get_system_version_once, ^{ |
| 38 | base::mac::ScopedNSAutoreleasePool pool; |
| 39 | system_version = new std::string( |
| 40 | SysNSStringToUTF8([[UIDevice currentDevice] systemVersion])); |
| 41 | }); |
| 42 | return *system_version; |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame^] | 46 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 47 | int32_t* minor_version, |
| 48 | int32_t* bugfix_version) { |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 49 | base::mac::ScopedNSAutoreleasePool pool; |
[email protected] | dba7cf2 | 2012-12-10 18:50:35 | [diff] [blame] | 50 | std::string system_version = OperatingSystemVersion(); |
| 51 | if (!system_version.empty()) { |
| 52 | // Try to parse out the version numbers from the string. |
| 53 | int num_read = sscanf(system_version.c_str(), "%d.%d.%d", major_version, |
| 54 | minor_version, bugfix_version); |
| 55 | if (num_read < 1) |
| 56 | *major_version = 0; |
| 57 | if (num_read < 2) |
| 58 | *minor_version = 0; |
| 59 | if (num_read < 3) |
| 60 | *bugfix_version = 0; |
| 61 | } |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame^] | 65 | int64_t SysInfo::AmountOfPhysicalMemory() { |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 66 | struct host_basic_info hostinfo; |
| 67 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
[email protected] | a07893f8 | 2014-05-28 23:40:08 | [diff] [blame] | 68 | base::mac::ScopedMachSendRight host(mach_host_self()); |
mark | da902e18 | 2015-10-20 18:36:13 | [diff] [blame] | 69 | int result = host_info(host.get(), |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 70 | HOST_BASIC_INFO, |
| 71 | reinterpret_cast<host_info_t>(&hostinfo), |
| 72 | &count); |
| 73 | if (result != KERN_SUCCESS) { |
| 74 | NOTREACHED(); |
| 75 | return 0; |
| 76 | } |
| 77 | DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame^] | 78 | return static_cast<int64_t>(hostinfo.max_mem); |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 81 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame^] | 82 | int64_t SysInfo::AmountOfAvailablePhysicalMemory() { |
[email protected] | a07893f8 | 2014-05-28 23:40:08 | [diff] [blame] | 83 | base::mac::ScopedMachSendRight host(mach_host_self()); |
[email protected] | 381bc39 | 2014-03-05 10:28:59 | [diff] [blame] | 84 | vm_statistics_data_t vm_info; |
| 85 | mach_msg_type_number_t count = HOST_VM_INFO_COUNT; |
| 86 | if (host_statistics(host.get(), |
| 87 | HOST_VM_INFO, |
| 88 | reinterpret_cast<host_info_t>(&vm_info), |
| 89 | &count) != KERN_SUCCESS) { |
| 90 | NOTREACHED(); |
| 91 | return 0; |
| 92 | } |
| 93 | |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame^] | 94 | return static_cast<int64_t>(vm_info.free_count - vm_info.speculative_count) * |
| 95 | PAGE_SIZE; |
[email protected] | 381bc39 | 2014-03-05 10:28:59 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // static |
[email protected] | d5df0c8c | 2012-09-08 09:53:01 | [diff] [blame] | 99 | std::string SysInfo::CPUModelName() { |
| 100 | char name[256]; |
| 101 | size_t len = arraysize(name); |
| 102 | if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0) |
| 103 | return name; |
| 104 | return std::string(); |
| 105 | } |
| 106 | |
[email protected] | c6d5f8c | 2012-07-11 12:15:08 | [diff] [blame] | 107 | } // namespace base |