gpt4 book ai didi

objective-c - 以编程方式获取 OS X 中的 GPU 使用百分比

转载 作者:太空狗 更新时间:2023-10-30 03:54:54 25 4
gpt4 key购买 nike

在 OS X(10.6 和 10.7)上的 Cocoa/Objective-C 中是否有标准方法获取 GPU 使用百分比?

最佳答案

好好享受吧,GPU 和 RAM 的使用,在谨慎的 GPU 上不起作用,顺便说一句,因为它不公开性能监控字典。我的 MBP 有一个 NVIDIA gpu,应该也可以在 ATI 上工作,但我不确定 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;
}

关于objective-c - 以编程方式获取 OS X 中的 GPU 使用百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10110658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com