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