Is there a standard way of getting GPU percent usage in Cocoa/Objective-C on OS X (10.6 and 10.7)?
Enjoy it, GPU and RAM usage , doesn't work on the discreet GPU btw because it does not expose the performances monitoring dictionary. My MBP has an NVIDIA gpu, should work on ATI too but I'm not sure 100%
#include <CoreFoundation/CoreFoundation.h>
#include <Cocoa/Cocoa.h>
#include <IOKit/IOKitLib.h>
int main(int argc, const char * argv[])
{
while (1) {
// Get dictionary of all the PCI Devicces
CFMutableDictionaryRef matchDict = IOServiceMatching(kIOAcceleratorClassName);
// Create an iterator
io_iterator_t iterator;
if (IOServiceGetMatchingServices(kIOMasterPortDefault,matchDict,
&iterator) == kIOReturnSuccess)
{
// Iterator for devices found
io_registry_entry_t regEntry;
while ((regEntry = IOIteratorNext(iterator))) {
// Put this services object into a dictionary object.
CFMutableDictionaryRef serviceDictionary;
if (IORegistryEntryCreateCFProperties(regEntry,
&serviceDictionary,
kCFAllocatorDefault,
kNilOptions) != kIOReturnSuccess)
{
// Service dictionary creation failed.
IOObjectRelease(regEntry);
continue;
}
CFMutableDictionaryRef perf_properties = (CFMutableDictionaryRef) CFDictionaryGetValue( serviceDictionary, CFSTR("PerformanceStatistics") );
if (perf_properties) {
static ssize_t gpuCoreUse=0;
static ssize_t freeVramCount=0;
static ssize_t usedVramCount=0;
const void* gpuCoreUtilization = CFDictionaryGetValue(perf_properties, CFSTR("GPU Core Utilization"));
const void* freeVram = CFDictionaryGetValue(perf_properties, CFSTR("vramFreeBytes"));
const void* usedVram = CFDictionaryGetValue(perf_properties, CFSTR("vramUsedBytes"));
if (gpuCoreUtilization && freeVram && usedVram)
{
CFNumberGetValue( (CFNumberRef) gpuCoreUtilization, kCFNumberSInt64Type, &gpuCoreUse);
CFNumberGetValue( (CFNumberRef) freeVram, kCFNumberSInt64Type, &freeVramCount);
CFNumberGetValue( (CFNumberRef) usedVram, kCFNumberSInt64Type, &usedVramCount);
NSLog(@"GPU: %.3f%% VRAM: %.3f%%",gpuCoreUse/(double)10000000,usedVramCount/(double)(freeVramCount+usedVramCount)*100.0);
}
}
CFRelease(serviceDictionary);
IOObjectRelease(regEntry);
}
IOObjectRelease(iterator);
}
sleep(1);
}
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With