gpu_info_darwin.m 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #import <Foundation/Foundation.h>
  2. #import <mach/mach.h>
  3. #include "gpu_info_darwin.h"
  4. uint64_t getRecommendedMaxVRAM() {
  5. id<MTLDevice> device = MTLCreateSystemDefaultDevice();
  6. uint64_t result = device.recommendedMaxWorkingSetSize;
  7. CFRelease(device);
  8. return result;
  9. }
  10. // getPhysicalMemory returns the total physical memory in bytes
  11. uint64_t getPhysicalMemory() {
  12. return [NSProcessInfo processInfo].physicalMemory;
  13. }
  14. // getFreeMemory returns the total free memory in bytes, including inactive
  15. // memory that can be reclaimed by the system.
  16. uint64_t getFreeMemory() {
  17. mach_port_t host_port = mach_host_self();
  18. mach_msg_type_number_t host_size = sizeof(vm_statistics64_data_t) / sizeof(integer_t);
  19. vm_size_t pagesize;
  20. vm_statistics64_data_t vm_stat;
  21. host_page_size(host_port, &pagesize);
  22. if (host_statistics64(host_port, HOST_VM_INFO64, (host_info64_t)&vm_stat, &host_size) != KERN_SUCCESS) {
  23. return 0;
  24. }
  25. uint64_t free_memory = (uint64_t)vm_stat.free_count * pagesize;
  26. free_memory += (uint64_t)vm_stat.speculative_count * pagesize;
  27. free_memory += (uint64_t)vm_stat.inactive_count * pagesize;
  28. return free_memory;
  29. }