Simple RGB LED controller for Mac OS X
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GPUStats.m 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // GPUStats.m
  3. // CaseLights
  4. //
  5. // For more informations refer to this StackOverflow answer:
  6. // http://stackoverflow.com/a/22440235
  7. //
  8. // Created by Thomas Buck on 23.12.15.
  9. // Copyright © 2015 xythobuz. All rights reserved.
  10. //
  11. #import <IOKit/IOKitLib.h>
  12. #import "GPUStats.h"
  13. @implementation GPUStats
  14. + (NSInteger)getGPUUsage:(NSNumber **)usage freeVRAM:(NSNumber **)free usedVRAM:(NSNumber **)used {
  15. if ((usage == nil) || (free == nil) || (used == nil)) {
  16. NSLog(@"Invalid use of getGPUUsage!\n");
  17. return 1;
  18. }
  19. *usage = nil;
  20. *free = nil;
  21. *used = nil;
  22. CFMutableDictionaryRef pciDevices = IOServiceMatching(kIOAcceleratorClassName);
  23. io_iterator_t iterator;
  24. if (IOServiceGetMatchingServices(kIOMasterPortDefault, pciDevices, &iterator) == kIOReturnSuccess) {
  25. io_registry_entry_t registry;
  26. while ((registry = IOIteratorNext(iterator))) {
  27. CFMutableDictionaryRef services;
  28. if (IORegistryEntryCreateCFProperties(registry, &services, kCFAllocatorDefault, kNilOptions) == kIOReturnSuccess) {
  29. CFMutableDictionaryRef properties = (CFMutableDictionaryRef)CFDictionaryGetValue(services, CFSTR("PerformanceStatistics"));
  30. if (properties) {
  31. const void *gpuUsage = CFDictionaryGetValue(properties, CFSTR("GPU Core Utilization"));
  32. const void *freeVRAM = CFDictionaryGetValue(properties, CFSTR("vramFreeBytes"));
  33. const void *usedVRAM = CFDictionaryGetValue(properties, CFSTR("vramUsedBytes"));
  34. if (gpuUsage && freeVRAM && usedVRAM) {
  35. // Found the GPU. Store this reference for the next call
  36. static ssize_t gpuUsageNum = 0;
  37. static ssize_t freeVRAMNum = 0;
  38. static ssize_t usedVRAMNum = 0;
  39. CFNumberGetValue((CFNumberRef)gpuUsage, kCFNumberSInt64Type, &gpuUsageNum);
  40. CFNumberGetValue((CFNumberRef)freeVRAM, kCFNumberSInt64Type, &freeVRAMNum);
  41. CFNumberGetValue((CFNumberRef)usedVRAM, kCFNumberSInt64Type, &usedVRAMNum);
  42. *usage = [NSNumber numberWithDouble:gpuUsageNum / 10000000.0];
  43. *free = [NSNumber numberWithDouble:freeVRAMNum];
  44. *used = [NSNumber numberWithDouble:usedVRAMNum];
  45. #ifdef DEBUG
  46. NSLog(@"GPU: %.3f%% VRAM: %.3f%% (%.2fMB)\n", gpuUsageNum / 10000000.0, ((double)usedVRAMNum) / (freeVRAMNum + usedVRAMNum) * 100.0, (usedVRAMNum + freeVRAMNum) / (1024.0 * 1024.0));
  47. #endif
  48. }
  49. }
  50. CFRelease(services);
  51. }
  52. IOObjectRelease(registry);
  53. }
  54. IOObjectRelease(iterator);
  55. } else {
  56. NSLog(@"Couldn't list PCI devices!\n");
  57. }
  58. if ((*usage != nil) && (*free != nil) && (*used != nil)) {
  59. return 0;
  60. } else {
  61. NSLog(@"Error reading GPU data!\n");
  62. return 1;
  63. }
  64. }
  65. @end