[go: nahoru, domu]

blob: 8ba29cc40b83029b71748263b5f8e84d530f6fd4 [file] [log] [blame]
tapted0ffa1e42016-03-31 22:28:271// Copyright 2016 The Chromium Authors. All rights reserved.
jeremy@chromium.org71aa16c2009-02-24 16:37:132// 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
evan@chromium.orgc8587632009-11-12 02:17:197#include <ApplicationServices/ApplicationServices.h>
jeremy@chromium.org71aa16c2009-02-24 16:37:138#include <CoreServices/CoreServices.h>
tapted0ffa1e42016-03-31 22:28:279#import <Foundation/Foundation.h>
evan@chromium.orgc8587632009-11-12 02:17:1910#include <mach/mach_host.h>
11#include <mach/mach_init.h>
avi9b6f42932015-12-26 22:15:1412#include <stddef.h>
13#include <stdint.h>
hongbo.min@intel.comd5df0c8c2012-09-08 09:53:0114#include <sys/sysctl.h>
15#include <sys/types.h>
evan@chromium.orgc8587632009-11-12 02:17:1916
17#include "base/logging.h"
tapted0ffa1e42016-03-31 22:28:2718#include "base/mac/mac_util.h"
rsesek@chromium.org431d0a12012-10-16 20:17:5819#include "base/mac/scoped_mach_port.h"
tapted0ffa1e42016-03-31 22:28:2720#import "base/mac/sdk_forward_declarations.h"
avi9b6f42932015-12-26 22:15:1421#include "base/macros.h"
mkolom01ac10b2017-03-22 01:47:2922#include "base/process/process_metrics.h"
avi@chromium.orgc851cfd2013-06-10 20:11:1423#include "base/strings/stringprintf.h"
jeremy@chromium.org71aa16c2009-02-24 16:37:1324
25namespace base {
26
asvitkine666e5ee2017-05-04 22:52:0027namespace {
28
29// Queries sysctlbyname() for the given key and returns the value from the
30// system or the empty string on failure.
31std::string GetSysctlValue(const char* key_name) {
32 char value[256];
33 size_t len = arraysize(value);
34 if (sysctlbyname(key_name, &value, &len, nullptr, 0) == 0) {
35 DCHECK_GE(len, 1u);
36 DCHECK_EQ('\0', value[len - 1]);
37 return std::string(value, len - 1);
38 }
39 return std::string();
40}
41
42} // namespace
43
jeremy@chromium.org71aa16c2009-02-24 16:37:1344// static
stuartmorgan@chromium.org6e001bb2011-05-25 20:53:1845std::string SysInfo::OperatingSystemName() {
46 return "Mac OS X";
47}
48
49// static
50std::string SysInfo::OperatingSystemVersion() {
avidd4e614352015-12-09 00:44:4951 int32_t major, minor, bugfix;
stuartmorgan@chromium.org6e001bb2011-05-25 20:53:1852 OperatingSystemVersionNumbers(&major, &minor, &bugfix);
53 return base::StringPrintf("%d.%d.%d", major, minor, bugfix);
54}
55
56// static
avidd4e614352015-12-09 00:44:4957void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
58 int32_t* minor_version,
59 int32_t* bugfix_version) {
tapted0ffa1e42016-03-31 22:28:2760 NSProcessInfo* processInfo = [NSProcessInfo processInfo];
Peter Collingbourne8cc8696f2017-07-11 18:36:4261 // We should try to avoid using Gestalt here because it has been observed to
62 // spin up threads among other things. Using an availability check here would
63 // prevent us from using the private API in 10.9.2. So use a
64 // respondsToSelector check here instead and silence the warning.
tapted0ffa1e42016-03-31 22:28:2765 if ([processInfo respondsToSelector:@selector(operatingSystemVersion)]) {
Peter Collingbourne8cc8696f2017-07-11 18:36:4266#pragma clang diagnostic push
67#pragma clang diagnostic ignored "-Wunguarded-availability"
tapted0ffa1e42016-03-31 22:28:2768 NSOperatingSystemVersion version = [processInfo operatingSystemVersion];
Peter Collingbourne8cc8696f2017-07-11 18:36:4269#pragma clang diagnostic pop
tapted0ffa1e42016-03-31 22:28:2770 *major_version = version.majorVersion;
71 *minor_version = version.minorVersion;
72 *bugfix_version = version.patchVersion;
73 } else {
74 // -[NSProcessInfo operatingSystemVersion] is documented available in 10.10.
75 // It's also available via a private API since 10.9.2. For the remaining
76 // cases in 10.9, rely on ::Gestalt(..). Since this code is only needed for
77 // 10.9.0 and 10.9.1 and uses the recommended replacement thereafter,
78 // suppress the warning for this fallback case.
sdy07171a42016-08-30 18:22:0479 DCHECK(base::mac::IsOS10_9());
tapted0ffa1e42016-03-31 22:28:2780#pragma clang diagnostic push
81#pragma clang diagnostic ignored "-Wdeprecated-declarations"
82 Gestalt(gestaltSystemVersionMajor,
83 reinterpret_cast<SInt32*>(major_version));
84 Gestalt(gestaltSystemVersionMinor,
85 reinterpret_cast<SInt32*>(minor_version));
86 Gestalt(gestaltSystemVersionBugFix,
87 reinterpret_cast<SInt32*>(bugfix_version));
88#pragma clang diagnostic pop
89 }
jeremy@chromium.org71aa16c2009-02-24 16:37:1390}
91
evan@chromium.orgc8587632009-11-12 02:17:1992// static
Eric Karla628c092017-07-26 01:18:3793int64_t SysInfo::AmountOfPhysicalMemoryImpl() {
evan@chromium.orgc8587632009-11-12 02:17:1994 struct host_basic_info hostinfo;
95 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
rsesek@chromium.orga07893f2014-05-28 23:40:0896 base::mac::ScopedMachSendRight host(mach_host_self());
markda902e182015-10-20 18:36:1397 int result = host_info(host.get(),
evan@chromium.orgc8587632009-11-12 02:17:1998 HOST_BASIC_INFO,
99 reinterpret_cast<host_info_t>(&hostinfo),
100 &count);
evan@chromium.orgc8587632009-11-12 02:17:19101 if (result != KERN_SUCCESS) {
102 NOTREACHED();
103 return 0;
104 }
chenyu@chromium.orgc6d5f8c2012-07-11 12:15:08105 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
avidd4e614352015-12-09 00:44:49106 return static_cast<int64_t>(hostinfo.max_mem);
evan@chromium.orgc8587632009-11-12 02:17:19107}
108
hongbo.min@intel.comd5df0c8c2012-09-08 09:53:01109// static
Eric Karla628c092017-07-26 01:18:37110int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
mkolom01ac10b2017-03-22 01:47:29111 SystemMemoryInfoKB info;
112 if (!GetSystemMemoryInfo(&info))
hongbo.min@intel.come6085c32012-11-07 06:58:14113 return 0;
mkolom01ac10b2017-03-22 01:47:29114 // We should add inactive file-backed memory also but there is no such
115 // information from Mac OS unfortunately.
116 return static_cast<int64_t>(info.free + info.speculative) * 1024;
hongbo.min@intel.com52bdcda2012-10-27 06:10:37117}
118
119// static
hongbo.min@intel.comd5df0c8c2012-09-08 09:53:01120std::string SysInfo::CPUModelName() {
asvitkine666e5ee2017-05-04 22:52:00121 return GetSysctlValue("machdep.cpu.brand_string");
hongbo.min@intel.comd5df0c8c2012-09-08 09:53:01122}
123
asvitkine666e5ee2017-05-04 22:52:00124// static
jeremyba4eca92014-11-06 18:47:10125std::string SysInfo::HardwareModelName() {
asvitkine666e5ee2017-05-04 22:52:00126 return GetSysctlValue("hw.model");
jeremyba4eca92014-11-06 18:47:10127}
128
jeremy@chromium.org71aa16c2009-02-24 16:37:13129} // namespace base