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