1
0

system-info-macos.mm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "system-info.hpp"
  2. #ifdef __aarch64__
  3. #import <util/platform.h>
  4. #import <Foundation/Foundation.h>
  5. #import <Foundation/NSProcessInfo.h>
  6. #import <sys/sysctl.h>
  7. #import <sys/types.h>
  8. namespace {
  9. std::optional<std::string> getCpuName()
  10. {
  11. std::string name;
  12. size_t size;
  13. int ret;
  14. ret = sysctlbyname("machdep.cpu.brand_string", nullptr, &size, nullptr, 0);
  15. if (ret != 0)
  16. return std::nullopt;
  17. name.resize(size);
  18. ret = sysctlbyname("machdep.cpu.brand_string", name.data(), &size, nullptr, 0);
  19. if (ret != 0)
  20. return std::nullopt;
  21. // Remove null terminator
  22. name.resize(name.find('\0'));
  23. return name;
  24. }
  25. // Apple Silicon Macs have a single SoC that contains both GPU and CPU, the same information is valid for both.
  26. void fillSoCInfo(GoLiveApi::Capabilities &capabilities)
  27. {
  28. capabilities.cpu.name = getCpuName();
  29. // Getting the frequency is not supported on Apple Silicon.
  30. capabilities.cpu.physical_cores = os_get_physical_cores();
  31. capabilities.cpu.logical_cores = os_get_logical_cores();
  32. capabilities.memory.total = os_get_sys_total_size();
  33. capabilities.memory.free = os_get_sys_free_size();
  34. // Apple Silicon does not support dGPUs, there's only going to be one (the SoC).
  35. GoLiveApi::Gpu gpu;
  36. gpu.model = capabilities.cpu.name.value_or("Unknown");
  37. gpu.vendor_id = 0x106b; // Always Apple
  38. gpu.device_id = 0; // Always 0 for Apple Silicon
  39. std::vector<GoLiveApi::Gpu> gpus;
  40. gpus.push_back(std::move(gpu));
  41. capabilities.gpu = gpus;
  42. }
  43. void fillSystemInfo(GoLiveApi::System &sysinfo)
  44. {
  45. NSProcessInfo *procInfo = [NSProcessInfo processInfo];
  46. NSOperatingSystemVersion versionObj = [procInfo operatingSystemVersion];
  47. sysinfo.name = "macOS";
  48. sysinfo.bits = 64; // 32-bit macOS is long deprecated.
  49. sysinfo.version = [[procInfo operatingSystemVersionString] UTF8String];
  50. switch (versionObj.majorVersion) {
  51. case 11:
  52. sysinfo.release = "Big Sur";
  53. break;
  54. case 12:
  55. sysinfo.release = "Monterey";
  56. break;
  57. case 13:
  58. sysinfo.release = "Ventura";
  59. break;
  60. case 14:
  61. sysinfo.release = "Sonoma";
  62. break;
  63. case 15:
  64. sysinfo.release = "Sequoia";
  65. break;
  66. default:
  67. sysinfo.release = "unknown";
  68. }
  69. sysinfo.arm = true;
  70. sysinfo.armEmulation = false;
  71. }
  72. } // namespace
  73. void system_info(GoLiveApi::Capabilities &capabilities)
  74. {
  75. fillSoCInfo(capabilities);
  76. fillSystemInfo(capabilities.system);
  77. }
  78. #else /* !__aarch64__ */
  79. void system_info(GoLiveApi::Capabilities &)
  80. {
  81. /* Not implemented */
  82. }
  83. #endif