【问题标题】:Programmatically get GPU percent usage in OS X以编程方式获取 OS X 中的 GPU 使用百分比
【发布时间】:2012-04-24 00:52:44
【问题描述】:

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

【问题讨论】:

  • 您是否找到了一种以编程方式获取 GPU 使用率的方法?我也面临同样的问题..
  • @Andrea3000:不,还在等待答案...

标签: objective-c macos gpu


【解决方案1】:

享受它,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;
}

【讨论】:

  • 所以没有办法同时获得专用和谨慎的 gpu 数据?
  • 不,谨慎的 GPU 不公开性能数据,我还发现 ATI 都没有公开它,只是 VRAM 使用情况,但 GPU 使用情况没有:/
  • @Leonardo Bernardini,该代码示例对我不起作用。尽管“perf_properties”似乎获得了不错的值,但它的成员:“gpuCoreUtilization”、“freeVram”和“usedVram”都是 NULL。我意识到代码是在大约 3 年前提供的。我正在使用 Xcode 8.2.1。
  • 不要认为 Xcode 版本或 SDK 有问题,而是 GPU 驱动程序有问题。正如我在测试中所说的那样,一些 cpus 会填满字典,而另一些则不会,所以应该有另一种方法
  • vramUsedBytesGPU Core Utilization 不存在(不再存在)。解决方案是从IOAcceleratorIOPCIDevice 中选择"VRAM,totalMB"。例如 Intel GPU 的 VRAM 在IOAccelerator,AMD 的 VRAM 在IOPCIDevice。在IOAcceleratorPerformanceStatistics 中,您只能找到用于活动GPU 的vramFreeBytesGPU Core Utilization 也已重命名为 Device Utilization %GPU Activity(%)
猜你喜欢
  • 2019-11-11
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 2011-01-10
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多