【发布时间】:2012-04-24 00:52:44
【问题描述】:
在 OS X(10.6 和 10.7)上的 Cocoa/Objective-C 中,是否有一种标准方法可以获取 GPU 百分比使用率?
【问题讨论】:
-
您是否找到了一种以编程方式获取 GPU 使用率的方法?我也面临同样的问题..
-
@Andrea3000:不,还在等待答案...
标签: objective-c macos gpu
在 OS X(10.6 和 10.7)上的 Cocoa/Objective-C 中,是否有一种标准方法可以获取 GPU 百分比使用率?
【问题讨论】:
标签: objective-c macos 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;
}
【讨论】:
vramUsedBytes 和 GPU Core Utilization 不存在(不再存在)。解决方案是从IOAccelerator 或IOPCIDevice 中选择"VRAM,totalMB"。例如 Intel GPU 的 VRAM 在IOAccelerator,AMD 的 VRAM 在IOPCIDevice。在IOAccelerator 的PerformanceStatistics 中,您只能找到用于活动GPU 的vramFreeBytes。 GPU Core Utilization 也已重命名为 Device Utilization % 或 GPU Activity(%)。