• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2016 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
7#include <ApplicationServices/ApplicationServices.h>
8#include <CoreServices/CoreServices.h>
9#import <Foundation/Foundation.h>
10#include <mach/mach_host.h>
11#include <mach/mach_init.h>
12#include <stddef.h>
13#include <stdint.h>
14#include <sys/sysctl.h>
15#include <sys/types.h>
16
17#include "base/logging.h"
18#include "base/mac/mac_util.h"
19#include "base/mac/scoped_mach_port.h"
20#import "base/mac/sdk_forward_declarations.h"
21#include "base/macros.h"
22#include "base/strings/stringprintf.h"
23
24namespace base {
25
26// static
27std::string SysInfo::OperatingSystemName() {
28  return "Mac OS X";
29}
30
31// static
32std::string SysInfo::OperatingSystemVersion() {
33  int32_t major, minor, bugfix;
34  OperatingSystemVersionNumbers(&major, &minor, &bugfix);
35  return base::StringPrintf("%d.%d.%d", major, minor, bugfix);
36}
37
38// static
39void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
40                                            int32_t* minor_version,
41                                            int32_t* bugfix_version) {
42#if defined(MAC_OS_X_VERSION_10_10)
43  NSProcessInfo* processInfo = [NSProcessInfo processInfo];
44  if ([processInfo respondsToSelector:@selector(operatingSystemVersion)]) {
45    NSOperatingSystemVersion version = [processInfo operatingSystemVersion];
46    *major_version = version.majorVersion;
47    *minor_version = version.minorVersion;
48    *bugfix_version = version.patchVersion;
49  } else {
50#else
51  // Android buildbots are too old and have trouble using the forward
52  // declarations for some reason. Conditionally-compile the above block
53  // only when building on a more modern version of OS X.
54  if (true) {
55#endif
56    // -[NSProcessInfo operatingSystemVersion] is documented available in 10.10.
57    // It's also available via a private API since 10.9.2. For the remaining
58    // cases in 10.9, rely on ::Gestalt(..). Since this code is only needed for
59    // 10.9.0 and 10.9.1 and uses the recommended replacement thereafter,
60    // suppress the warning for this fallback case.
61    DCHECK(base::mac::IsOSMavericks());
62#pragma clang diagnostic push
63#pragma clang diagnostic ignored "-Wdeprecated-declarations"
64    Gestalt(gestaltSystemVersionMajor,
65            reinterpret_cast<SInt32*>(major_version));
66    Gestalt(gestaltSystemVersionMinor,
67            reinterpret_cast<SInt32*>(minor_version));
68    Gestalt(gestaltSystemVersionBugFix,
69            reinterpret_cast<SInt32*>(bugfix_version));
70#pragma clang diagnostic pop
71  }
72}
73
74// static
75int64_t SysInfo::AmountOfPhysicalMemory() {
76  struct host_basic_info hostinfo;
77  mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
78  base::mac::ScopedMachSendRight host(mach_host_self());
79  int result = host_info(host.get(),
80                         HOST_BASIC_INFO,
81                         reinterpret_cast<host_info_t>(&hostinfo),
82                         &count);
83  if (result != KERN_SUCCESS) {
84    NOTREACHED();
85    return 0;
86  }
87  DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
88  return static_cast<int64_t>(hostinfo.max_mem);
89}
90
91// static
92int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
93  base::mac::ScopedMachSendRight host(mach_host_self());
94  vm_statistics_data_t vm_info;
95  mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
96
97  if (host_statistics(host.get(),
98                      HOST_VM_INFO,
99                      reinterpret_cast<host_info_t>(&vm_info),
100                      &count) != KERN_SUCCESS) {
101    NOTREACHED();
102    return 0;
103  }
104
105  return static_cast<int64_t>(vm_info.free_count - vm_info.speculative_count) *
106         PAGE_SIZE;
107}
108
109// static
110std::string SysInfo::CPUModelName() {
111  char name[256];
112  size_t len = arraysize(name);
113  if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0)
114    return name;
115  return std::string();
116}
117
118std::string SysInfo::HardwareModelName() {
119  char model[256];
120  size_t len = sizeof(model);
121  if (sysctlbyname("hw.model", model, &len, NULL, 0) == 0)
122    return std::string(model, 0, len);
123  return std::string();
124}
125
126}  // namespace base
127