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 7#import <UIKit/UIKit.h> 8#include <mach/mach.h> 9#include <sys/sysctl.h> 10#include <sys/types.h> 11 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" 16 17namespace base { 18 19// static 20std::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])); 27 }); 28 // Examples of returned value: 'iPhone OS' on iPad 5.1.1 29 // and iPhone 5.1.1. 30 return *system_name; 31} 32 33// static 34std::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])); 41 }); 42 return *system_version; 43} 44 45// static 46void SysInfo::OperatingSystemVersionNumbers(int32* major_version, 47 int32* minor_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); 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 } 62} 63 64// static 65int64 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, 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); 78 return static_cast<int64>(hostinfo.max_mem); 79} 80 81// static 82int64 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(), 87 HOST_VM_INFO, 88 reinterpret_cast<host_info_t>(&vm_info), 89 &count) != KERN_SUCCESS) { 90 NOTREACHED(); 91 return 0; 92 } 93 94 return static_cast<int64>( 95 vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE; 96} 97 98// static 99std::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 107} // namespace base 108