在OS X中以编程方式获取GPU的使用率

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

你找到了以编程方式获取GPU使用情况的方法吗?我也遇到了同样的问题。 - Andrea3000
@Andrea3000:没有,还在等答案... - houbysoft
1个回答

8

享受它吧,GPU 和 RAM 使用情况。顺便说一下,它不能在独立的 GPU 上运行,因为它没有公开性能监控字典。我的 MBP 有一个 NVIDIA GPU,ATI 也应该可以用,但我不确定百分之百。

#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;
}

那么没有办法同时获取专用和独立显卡的数据吗? - Coldsteel48
你好,我如何修改代码以支持2个或更多的GPU呢? 虽然我没有这样的硬件,但我不知道从哪里开始,请你帮帮我? - Coldsteel48
1
@Leonardo Bernardini,这个代码示例对我不起作用。虽然“perf_properties”似乎得到了一个很好的值,但它的成员:“gpuCoreUtilization”,“freeVram”和“usedVram”都为NULL。我意识到这段代码是大约3年前提供的。我正在使用Xcode 8.2.1。 - Kaydell
1
不要认为 Xcode 版本或 SDK 是问题,而是 GPU 驱动程序。正如我在测试中所说,一些 CPU 填充字典,而其他 CPU 则没有,因此可能还有另一种方法。 - Leonardo Bernardini
6
vramUsedBytesGPU Core Utilization已经不存在。解决方法是从IOAccelerator或者IOPCIDevice选取"VRAM,totalMB"。例如,Intel GPU的VRAM在IOAccelerator中,而AMD的VRAM在IOPCIDevice中。在IOAcceleratorPerformanceStatistics中,你只能找到正在使用的GPU的vramFreeBytesGPU Core Utilization已被重命名为Device Utilization %GPU Activity(%) - Marc J. Schmidt
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接