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.
5 #include "base/sys_info.h"
10 #include <sys/sysctl.h>
11 #include <sys/types.h>
12 #import <UIKit/UIKit.h>
14 #include "base/logging.h"
15 #include "base/mac/scoped_mach_port.h"
16 #include "base/mac/scoped_nsautorelease_pool.h"
17 #include "base/macros.h"
18 #include "base/process/process_metrics.h"
19 #include "base/strings/sys_string_conversions.h"
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) {
29 size_t len = arraysize(value);
30 if (sysctlbyname(key_name, &value, &len, nullptr, 0) == 0) {
32 DCHECK_EQ('\0', value[len - 1]);
33 return std::string(value, len - 1);
41 std::string SysInfo::OperatingSystemName() {
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]));
49 // Examples of returned value: 'iPhone OS' on iPad 5.1.1
55 std::string SysInfo::OperatingSystemVersion() {
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]));
63 return *system_version;
67 void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
68 int32_t* minor_version,
69 int32_t* bugfix_version) {
70 base::mac::ScopedNSAutoreleasePool pool;
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);
86 int64_t SysInfo::AmountOfPhysicalMemoryImpl() {
87 struct host_basic_info hostinfo;
88 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
89 base::mac::ScopedMachSendRight host(mach_host_self());
90 int result = host_info(host.get(),
92 reinterpret_cast<host_info_t>(&hostinfo),
94 if (result != KERN_SUCCESS) {
98 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
99 return static_cast<int64_t>(hostinfo.max_mem);
103 int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
104 SystemMemoryInfoKB info;
105 if (!GetSystemMemoryInfo(&info))
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;
113 std::string SysInfo::CPUModelName() {
114 return GetSysctlValue("machdep.cpu.brand_string");
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";
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");