blob: 9a95298e692d7bbf3fb124528f96d355b784d9d2 [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>
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
9#include <stdint.h>
[email protected]d5df0c8c2012-09-08 09:53:0110#include <sys/sysctl.h>
11#include <sys/types.h>
tapted574f09c2015-05-19 13:08:0812#import <UIKit/UIKit.h>
[email protected]c6d5f8c2012-07-11 12:15:0813
14#include "base/logging.h"
[email protected]431d0a12012-10-16 20:17:5815#include "base/mac/scoped_mach_port.h"
[email protected]c6d5f8c2012-07-11 12:15:0816#include "base/mac/scoped_nsautorelease_pool.h"
avi9b6f42932015-12-26 22:15:1417#include "base/macros.h"
[email protected]9fe1a5b2013-02-07 19:18:0318#include "base/strings/sys_string_conversions.h"
[email protected]c6d5f8c2012-07-11 12:15:0819
20namespace base {
21
22// static
23std::string SysInfo::OperatingSystemName() {
[email protected]dba7cf22012-12-10 18:50:3524 static dispatch_once_t get_system_name_once;
25 static std::string* system_name;
26 dispatch_once(&get_system_name_once, ^{
27 base::mac::ScopedNSAutoreleasePool pool;
28 system_name = new std::string(
29 SysNSStringToUTF8([[UIDevice currentDevice] systemName]));
30 });
[email protected]c6d5f8c2012-07-11 12:15:0831 // Examples of returned value: 'iPhone OS' on iPad 5.1.1
32 // and iPhone 5.1.1.
[email protected]dba7cf22012-12-10 18:50:3533 return *system_name;
[email protected]c6d5f8c2012-07-11 12:15:0834}
35
36// static
37std::string SysInfo::OperatingSystemVersion() {
[email protected]dba7cf22012-12-10 18:50:3538 static dispatch_once_t get_system_version_once;
39 static std::string* system_version;
40 dispatch_once(&get_system_version_once, ^{
41 base::mac::ScopedNSAutoreleasePool pool;
42 system_version = new std::string(
43 SysNSStringToUTF8([[UIDevice currentDevice] systemVersion]));
44 });
45 return *system_version;
[email protected]c6d5f8c2012-07-11 12:15:0846}
47
48// static
avidd4e614352015-12-09 00:44:4949void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
50 int32_t* minor_version,
51 int32_t* bugfix_version) {
[email protected]c6d5f8c2012-07-11 12:15:0852 base::mac::ScopedNSAutoreleasePool pool;
[email protected]dba7cf22012-12-10 18:50:3553 std::string system_version = OperatingSystemVersion();
54 if (!system_version.empty()) {
55 // Try to parse out the version numbers from the string.
56 int num_read = sscanf(system_version.c_str(), "%d.%d.%d", major_version,
57 minor_version, bugfix_version);
58 if (num_read < 1)
59 *major_version = 0;
60 if (num_read < 2)
61 *minor_version = 0;
62 if (num_read < 3)
63 *bugfix_version = 0;
64 }
[email protected]c6d5f8c2012-07-11 12:15:0865}
66
67// static
avidd4e614352015-12-09 00:44:4968int64_t SysInfo::AmountOfPhysicalMemory() {
[email protected]c6d5f8c2012-07-11 12:15:0869 struct host_basic_info hostinfo;
70 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
[email protected]a07893f82014-05-28 23:40:0871 base::mac::ScopedMachSendRight host(mach_host_self());
markda902e182015-10-20 18:36:1372 int result = host_info(host.get(),
[email protected]c6d5f8c2012-07-11 12:15:0873 HOST_BASIC_INFO,
74 reinterpret_cast<host_info_t>(&hostinfo),
75 &count);
76 if (result != KERN_SUCCESS) {
77 NOTREACHED();
78 return 0;
79 }
80 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
avidd4e614352015-12-09 00:44:4981 return static_cast<int64_t>(hostinfo.max_mem);
[email protected]c6d5f8c2012-07-11 12:15:0882}
83
[email protected]d5df0c8c2012-09-08 09:53:0184// static
avidd4e614352015-12-09 00:44:4985int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
[email protected]a07893f82014-05-28 23:40:0886 base::mac::ScopedMachSendRight host(mach_host_self());
[email protected]381bc392014-03-05 10:28:5987 vm_statistics_data_t vm_info;
88 mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
89 if (host_statistics(host.get(),
90 HOST_VM_INFO,
91 reinterpret_cast<host_info_t>(&vm_info),
92 &count) != KERN_SUCCESS) {
93 NOTREACHED();
94 return 0;
95 }
96
avidd4e614352015-12-09 00:44:4997 return static_cast<int64_t>(vm_info.free_count - vm_info.speculative_count) *
98 PAGE_SIZE;
[email protected]381bc392014-03-05 10:28:5999}
100
101// static
[email protected]d5df0c8c2012-09-08 09:53:01102std::string SysInfo::CPUModelName() {
103 char name[256];
104 size_t len = arraysize(name);
105 if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0)
106 return name;
107 return std::string();
108}
109
[email protected]c6d5f8c2012-07-11 12:15:08110} // namespace base