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"
8 #include <sys/sysctl.h>
10 #import <UIKit/UIKit.h>
12 #include "base/logging.h"
13 #include "base/mac/scoped_mach_port.h"
14 #include "base/mac/scoped_nsautorelease_pool.h"
15 #include "base/strings/sys_string_conversions.h"
20 std::string SysInfo::OperatingSystemName() {
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]));
28 // Examples of returned value: 'iPhone OS' on iPad 5.1.1
34 std::string SysInfo::OperatingSystemVersion() {
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]));
42 return *system_version;
46 void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
48 int32* bugfix_version) {
49 base::mac::ScopedNSAutoreleasePool pool;
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);
65 int64 SysInfo::AmountOfPhysicalMemory() {
66 struct host_basic_info hostinfo;
67 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
68 base::mac::ScopedMachSendRight host(mach_host_self());
69 int result = host_info(host,
71 reinterpret_cast<host_info_t>(&hostinfo),
73 if (result != KERN_SUCCESS) {
77 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
78 return static_cast<int64>(hostinfo.max_mem);
82 int64 SysInfo::AmountOfAvailablePhysicalMemory() {
83 base::mac::ScopedMachSendRight host(mach_host_self());
84 vm_statistics_data_t vm_info;
85 mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
86 if (host_statistics(host.get(),
88 reinterpret_cast<host_info_t>(&vm_info),
89 &count) != KERN_SUCCESS) {
94 return static_cast<int64>(
95 vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE;
99 std::string SysInfo::CPUModelName() {
101 size_t len = arraysize(name);
102 if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0)
104 return std::string();