sys_info_mac.mm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "base/system/sys_info.h"
  5. #import <Foundation/Foundation.h>
  6. #include <mach/mach_host.h>
  7. #include <mach/mach_init.h>
  8. #include <stddef.h>
  9. #include <stdint.h>
  10. #include <sys/sysctl.h>
  11. #include <sys/types.h>
  12. #include "base/check_op.h"
  13. #include "base/mac/mac_util.h"
  14. #include "base/mac/scoped_mach_port.h"
  15. #include "base/notreached.h"
  16. #include "base/numerics/safe_conversions.h"
  17. #include "base/process/process_metrics.h"
  18. #include "base/strings/string_util.h"
  19. #include "base/strings/stringprintf.h"
  20. namespace base {
  21. namespace {
  22. // Queries sysctlbyname() for the given key and returns the value from the
  23. // system or the empty string on failure.
  24. std::string GetSysctlValue(const char* key_name) {
  25. char value[256];
  26. size_t len = std::size(value);
  27. if (sysctlbyname(key_name, &value, &len, nullptr, 0) == 0) {
  28. DCHECK_GE(len, 1u);
  29. DCHECK_EQ('\0', value[len - 1]);
  30. return std::string(value, len - 1);
  31. }
  32. return std::string();
  33. }
  34. } // namespace
  35. // static
  36. std::string SysInfo::OperatingSystemName() {
  37. return "Mac OS X";
  38. }
  39. // static
  40. std::string SysInfo::OperatingSystemVersion() {
  41. int32_t major, minor, bugfix;
  42. OperatingSystemVersionNumbers(&major, &minor, &bugfix);
  43. return base::StringPrintf("%d.%d.%d", major, minor, bugfix);
  44. }
  45. // static
  46. void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
  47. int32_t* minor_version,
  48. int32_t* bugfix_version) {
  49. NSOperatingSystemVersion version =
  50. [[NSProcessInfo processInfo] operatingSystemVersion];
  51. *major_version = static_cast<int32_t>(version.majorVersion);
  52. *minor_version = static_cast<int32_t>(version.minorVersion);
  53. *bugfix_version = static_cast<int32_t>(version.patchVersion);
  54. }
  55. // static
  56. std::string SysInfo::OperatingSystemArchitecture() {
  57. switch (mac::GetCPUType()) {
  58. case mac::CPUType::kIntel:
  59. return "x86_64";
  60. case mac::CPUType::kTranslatedIntel:
  61. case mac::CPUType::kArm:
  62. return "arm64";
  63. }
  64. }
  65. // static
  66. uint64_t SysInfo::AmountOfPhysicalMemoryImpl() {
  67. struct host_basic_info hostinfo;
  68. mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
  69. base::mac::ScopedMachSendRight host(mach_host_self());
  70. int result = host_info(host.get(), HOST_BASIC_INFO,
  71. reinterpret_cast<host_info_t>(&hostinfo), &count);
  72. if (result != KERN_SUCCESS) {
  73. NOTREACHED();
  74. return 0;
  75. }
  76. DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
  77. return hostinfo.max_mem;
  78. }
  79. // static
  80. uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
  81. SystemMemoryInfoKB info;
  82. if (!GetSystemMemoryInfo(&info))
  83. return 0;
  84. // We should add inactive file-backed memory also but there is no such
  85. // information from Mac OS unfortunately.
  86. return checked_cast<uint64_t>(info.free + info.speculative) * 1024;
  87. }
  88. // static
  89. std::string SysInfo::CPUModelName() {
  90. return GetSysctlValue("machdep.cpu.brand_string");
  91. }
  92. // static
  93. std::string SysInfo::HardwareModelName() {
  94. return GetSysctlValue("hw.model");
  95. }
  96. // static
  97. SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() {
  98. HardwareInfo info;
  99. info.manufacturer = "Apple Inc.";
  100. info.model = HardwareModelName();
  101. DCHECK(IsStringUTF8(info.manufacturer));
  102. DCHECK(IsStringUTF8(info.model));
  103. return info;
  104. }
  105. } // namespace base