blob: 9a329b9f0924bbdefce908cd51de4a6488c574f0 [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
23// static
24std::string SysInfo::OperatingSystemName() {
[email protected]dba7cf22012-12-10 18:50:3525 static dispatch_once_t get_system_name_once;
26 static std::string* system_name;
27 dispatch_once(&get_system_name_once, ^{
28 base::mac::ScopedNSAutoreleasePool pool;
29 system_name = new std::string(
30 SysNSStringToUTF8([[UIDevice currentDevice] systemName]));
31 });
[email protected]c6d5f8c2012-07-11 12:15:0832 // Examples of returned value: 'iPhone OS' on iPad 5.1.1
33 // and iPhone 5.1.1.
[email protected]dba7cf22012-12-10 18:50:3534 return *system_name;
[email protected]c6d5f8c2012-07-11 12:15:0835}
36
37// static
38std::string SysInfo::OperatingSystemVersion() {
[email protected]dba7cf22012-12-10 18:50:3539 static dispatch_once_t get_system_version_once;
40 static std::string* system_version;
41 dispatch_once(&get_system_version_once, ^{
42 base::mac::ScopedNSAutoreleasePool pool;
43 system_version = new std::string(
44 SysNSStringToUTF8([[UIDevice currentDevice] systemVersion]));
45 });
46 return *system_version;
[email protected]c6d5f8c2012-07-11 12:15:0847}
48
49// static
avidd4e614352015-12-09 00:44:4950void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
51 int32_t* minor_version,
52 int32_t* bugfix_version) {
[email protected]c6d5f8c2012-07-11 12:15:0853 base::mac::ScopedNSAutoreleasePool pool;
[email protected]dba7cf22012-12-10 18:50:3554 std::string system_version = OperatingSystemVersion();
55 if (!system_version.empty()) {
56 // Try to parse out the version numbers from the string.
57 int num_read = sscanf(system_version.c_str(), "%d.%d.%d", major_version,
58 minor_version, bugfix_version);
59 if (num_read < 1)
60 *major_version = 0;
61 if (num_read < 2)
62 *minor_version = 0;
63 if (num_read < 3)
64 *bugfix_version = 0;
65 }
[email protected]c6d5f8c2012-07-11 12:15:0866}
67
68// static
avidd4e614352015-12-09 00:44:4969int64_t SysInfo::AmountOfPhysicalMemory() {
[email protected]c6d5f8c2012-07-11 12:15:0870 struct host_basic_info hostinfo;
71 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
[email protected]a07893f82014-05-28 23:40:0872 base::mac::ScopedMachSendRight host(mach_host_self());
markda902e182015-10-20 18:36:1373 int result = host_info(host.get(),
[email protected]c6d5f8c2012-07-11 12:15:0874 HOST_BASIC_INFO,
75 reinterpret_cast<host_info_t>(&hostinfo),
76 &count);
77 if (result != KERN_SUCCESS) {
78 NOTREACHED();
79 return 0;
80 }
81 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
avidd4e614352015-12-09 00:44:4982 return static_cast<int64_t>(hostinfo.max_mem);
[email protected]c6d5f8c2012-07-11 12:15:0883}
84
[email protected]d5df0c8c2012-09-08 09:53:0185// static
avidd4e614352015-12-09 00:44:4986int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
mkolom01ac10b2017-03-22 01:47:2987 SystemMemoryInfoKB info;
88 if (!GetSystemMemoryInfo(&info))
[email protected]381bc392014-03-05 10:28:5989 return 0;
mkolom01ac10b2017-03-22 01:47:2990 // We should add inactive file-backed memory also but there is no such
91 // information from iOS unfortunately.
92 return static_cast<int64_t>(info.free + info.speculative) * 1024;
[email protected]381bc392014-03-05 10:28:5993}
94
95// static
[email protected]d5df0c8c2012-09-08 09:53:0196std::string SysInfo::CPUModelName() {
97 char name[256];
98 size_t len = arraysize(name);
99 if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0)
100 return name;
101 return std::string();
102}
103
[email protected]c6d5f8c2012-07-11 12:15:08104} // namespace base