blob: 60a753192317c5557bc9773f4290e3e87eb8a52d [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"
mkolom01ac10b2017-03-22 01:47:2918#include "base/process/process_metrics.h"
[email protected]9fe1a5b2013-02-07 19:18:0319#include "base/strings/sys_string_conversions.h"
[email protected]c6d5f8c2012-07-11 12:15:0820
21namespace base {
22
asvitkine666e5ee2017-05-04 22:52:0023namespace {
24
25// Queries sysctlbyname() for the given key and returns the value from the
26// system or the empty string on failure.
27std::string GetSysctlValue(const char* key_name) {
28 char value[256];
29 size_t len = arraysize(value);
30 if (sysctlbyname(key_name, &value, &len, nullptr, 0) == 0) {
31 DCHECK_GE(len, 1u);
32 DCHECK_EQ('\0', value[len - 1]);
33 return std::string(value, len - 1);
34 }
35 return std::string();
36}
37
38} // namespace
39
[email protected]c6d5f8c2012-07-11 12:15:0840// static
41std::string SysInfo::OperatingSystemName() {
[email protected]dba7cf22012-12-10 18:50:3542 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]));
48 });
[email protected]c6d5f8c2012-07-11 12:15:0849 // Examples of returned value: 'iPhone OS' on iPad 5.1.1
50 // and iPhone 5.1.1.
[email protected]dba7cf22012-12-10 18:50:3551 return *system_name;
[email protected]c6d5f8c2012-07-11 12:15:0852}
53
54// static
55std::string SysInfo::OperatingSystemVersion() {
[email protected]dba7cf22012-12-10 18:50:3556 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]));
62 });
63 return *system_version;
[email protected]c6d5f8c2012-07-11 12:15:0864}
65
66// static
avidd4e614352015-12-09 00:44:4967void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
68 int32_t* minor_version,
69 int32_t* bugfix_version) {
[email protected]c6d5f8c2012-07-11 12:15:0870 base::mac::ScopedNSAutoreleasePool pool;
[email protected]dba7cf22012-12-10 18:50:3571 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);
76 if (num_read < 1)
77 *major_version = 0;
78 if (num_read < 2)
79 *minor_version = 0;
80 if (num_read < 3)
81 *bugfix_version = 0;
82 }
[email protected]c6d5f8c2012-07-11 12:15:0883}
84
85// static
Eric Karla628c092017-07-26 01:18:3786int64_t SysInfo::AmountOfPhysicalMemoryImpl() {
[email protected]c6d5f8c2012-07-11 12:15:0887 struct host_basic_info hostinfo;
88 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
[email protected]a07893f82014-05-28 23:40:0889 base::mac::ScopedMachSendRight host(mach_host_self());
markda902e182015-10-20 18:36:1390 int result = host_info(host.get(),
[email protected]c6d5f8c2012-07-11 12:15:0891 HOST_BASIC_INFO,
92 reinterpret_cast<host_info_t>(&hostinfo),
93 &count);
94 if (result != KERN_SUCCESS) {
95 NOTREACHED();
96 return 0;
97 }
98 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
avidd4e614352015-12-09 00:44:4999 return static_cast<int64_t>(hostinfo.max_mem);
[email protected]c6d5f8c2012-07-11 12:15:08100}
101
[email protected]d5df0c8c2012-09-08 09:53:01102// static
Eric Karla628c092017-07-26 01:18:37103int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
mkolom01ac10b2017-03-22 01:47:29104 SystemMemoryInfoKB info;
105 if (!GetSystemMemoryInfo(&info))
[email protected]381bc392014-03-05 10:28:59106 return 0;
mkolom01ac10b2017-03-22 01:47:29107 // 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;
[email protected]381bc392014-03-05 10:28:59110}
111
112// static
[email protected]d5df0c8c2012-09-08 09:53:01113std::string SysInfo::CPUModelName() {
asvitkine666e5ee2017-05-04 22:52:00114 return GetSysctlValue("machdep.cpu.brand_string");
115}
116
117// static
118std::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";
123#else
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");
127#endif
[email protected]d5df0c8c2012-09-08 09:53:01128}
129
[email protected]c6d5f8c2012-07-11 12:15:08130} // namespace base