blob: 56e7b556bbf4628dd37d26ac3933060cbf15969e [file] [log] [blame]
[email protected]c6d5f8c2012-07-11 12:15:081// 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]c6d5f8c2012-07-11 12:15:087#include <mach/mach.h>
[email protected]d5df0c8c2012-09-08 09:53:018#include <sys/sysctl.h>
9#include <sys/types.h>
tapted574f09c2015-05-19 13:08:0810#import <UIKit/UIKit.h>
[email protected]c6d5f8c2012-07-11 12:15:0811
12#include "base/logging.h"
[email protected]431d0a12012-10-16 20:17:5813#include "base/mac/scoped_mach_port.h"
[email protected]c6d5f8c2012-07-11 12:15:0814#include "base/mac/scoped_nsautorelease_pool.h"
[email protected]9fe1a5b2013-02-07 19:18:0315#include "base/strings/sys_string_conversions.h"
[email protected]c6d5f8c2012-07-11 12:15:0816
17namespace base {
18
19// static
20std::string SysInfo::OperatingSystemName() {
[email protected]dba7cf22012-12-10 18:50:3521 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 });
[email protected]c6d5f8c2012-07-11 12:15:0828 // Examples of returned value: 'iPhone OS' on iPad 5.1.1
29 // and iPhone 5.1.1.
[email protected]dba7cf22012-12-10 18:50:3530 return *system_name;
[email protected]c6d5f8c2012-07-11 12:15:0831}
32
33// static
34std::string SysInfo::OperatingSystemVersion() {
[email protected]dba7cf22012-12-10 18:50:3535 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;
[email protected]c6d5f8c2012-07-11 12:15:0843}
44
45// static
avidd4e614352015-12-09 00:44:4946void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
47 int32_t* minor_version,
48 int32_t* bugfix_version) {
[email protected]c6d5f8c2012-07-11 12:15:0849 base::mac::ScopedNSAutoreleasePool pool;
[email protected]dba7cf22012-12-10 18:50:3550 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 }
[email protected]c6d5f8c2012-07-11 12:15:0862}
63
64// static
avidd4e614352015-12-09 00:44:4965int64_t SysInfo::AmountOfPhysicalMemory() {
[email protected]c6d5f8c2012-07-11 12:15:0866 struct host_basic_info hostinfo;
67 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
[email protected]a07893f82014-05-28 23:40:0868 base::mac::ScopedMachSendRight host(mach_host_self());
markda902e182015-10-20 18:36:1369 int result = host_info(host.get(),
[email protected]c6d5f8c2012-07-11 12:15:0870 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);
avidd4e614352015-12-09 00:44:4978 return static_cast<int64_t>(hostinfo.max_mem);
[email protected]c6d5f8c2012-07-11 12:15:0879}
80
[email protected]d5df0c8c2012-09-08 09:53:0181// static
avidd4e614352015-12-09 00:44:4982int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
[email protected]a07893f82014-05-28 23:40:0883 base::mac::ScopedMachSendRight host(mach_host_self());
[email protected]381bc392014-03-05 10:28:5984 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
avidd4e614352015-12-09 00:44:4994 return static_cast<int64_t>(vm_info.free_count - vm_info.speculative_count) *
95 PAGE_SIZE;
[email protected]381bc392014-03-05 10:28:5996}
97
98// static
[email protected]d5df0c8c2012-09-08 09:53:0199std::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
[email protected]c6d5f8c2012-07-11 12:15:08107} // namespace base