-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathgpu_usage.m
50 lines (44 loc) · 1.66 KB
/
gpu_usage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "gpu_usage.h"
#ifdef __APPLE__
#include <CoreFoundation/CoreFoundation.h>
#include <Cocoa/Cocoa.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/graphics/IOGraphicsLib.h>
float getGPUCoreUsage(int gpu_number) {
static ssize_t gpuUsage=0;
int i = 0;
CFMutableDictionaryRef dict = IOServiceMatching(kIOAcceleratorClassName);
io_iterator_t it;
if (IOServiceGetMatchingServices(kIOMasterPortDefault,dict,
&it) == kIOReturnSuccess) {
io_registry_entry_t entry;
while ((entry = IOIteratorNext(it))) {
CFMutableDictionaryRef serviceDictionary;
if (IORegistryEntryCreateCFProperties(entry,
&serviceDictionary,
kCFAllocatorDefault,
kNilOptions) != kIOReturnSuccess) {
IOObjectRelease(entry);
IOObjectRelease(it);
return 0;
}
CFMutableDictionaryRef properties = (CFMutableDictionaryRef) CFDictionaryGetValue( serviceDictionary, CFSTR("PerformanceStatistics") );
if (properties) {
const void* gpuCoreUtilization = CFDictionaryGetValue(properties, CFSTR("GPU Core Utilization"));
if (gpuCoreUtilization) {
CFNumberGetValue( (CFNumberRef) gpuCoreUtilization, kCFNumberSInt64Type, &gpuUsage);
}
}
CFRelease(serviceDictionary);
IOObjectRelease(entry);
if (i == gpu_number) {
IOObjectRelease(it);
return gpuUsage/10000000;
}
i++;
}
IOObjectRelease(it);
}
return 0;
}
#endif