Browse Source

Added ability to read GPU stats

Thomas Buck 6 years ago
parent
commit
841c287936

+ 19
- 4
CaseLights.xcodeproj/project.pbxproj View File

@@ -9,6 +9,7 @@
9 9
 /* Begin PBXBuildFile section */
10 10
 		E9109B111C2AACF400726111 /* SystemInfoKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E9109B0B1C2AAC1B00726111 /* SystemInfoKit.framework */; };
11 11
 		E9109B151C2AAEFD00726111 /* SystemInfoKit.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = E9109B0B1C2AAC1B00726111 /* SystemInfoKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
12
+		E9109B191C2AAFE100726111 /* GPUStats.m in Sources */ = {isa = PBXBuildFile; fileRef = E9109B181C2AAFE100726111 /* GPUStats.m */; };
12 13
 		E9AB18011C29F8CD00BF3C51 /* Serial.m in Sources */ = {isa = PBXBuildFile; fileRef = E9AB18001C29F8CD00BF3C51 /* Serial.m */; };
13 14
 		E9AB18061C2A173500BF3C51 /* Thread.m in Sources */ = {isa = PBXBuildFile; fileRef = E9AB18051C2A173500BF3C51 /* Thread.m */; };
14 15
 		E9F13E031C28B3D3004C6B95 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = E9F13E021C28B3D3004C6B95 /* AppDelegate.m */; };
@@ -56,8 +57,10 @@
56 57
 
57 58
 /* Begin PBXFileReference section */
58 59
 		E9109B051C2AAC1B00726111 /* SystemInfoKit.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = SystemInfoKit.xcodeproj; path = JSystemInfoKit/SystemInfoKit.xcodeproj; sourceTree = "<group>"; };
59
-		E9AB17FF1C29F8CD00BF3C51 /* Serial.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Serial.h; sourceTree = "<group>"; };
60
-		E9AB18001C29F8CD00BF3C51 /* Serial.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Serial.m; sourceTree = "<group>"; };
60
+		E9109B171C2AAFE100726111 /* GPUStats.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = GPUStats.h; path = CaseLights/GPUStats.h; sourceTree = "<group>"; };
61
+		E9109B181C2AAFE100726111 /* GPUStats.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GPUStats.m; path = CaseLights/GPUStats.m; sourceTree = "<group>"; };
62
+		E9AB17FF1C29F8CD00BF3C51 /* Serial.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Serial.h; path = CaseLights/Serial.h; sourceTree = "<group>"; };
63
+		E9AB18001C29F8CD00BF3C51 /* Serial.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Serial.m; path = CaseLights/Serial.m; sourceTree = "<group>"; };
61 64
 		E9AB18041C2A173500BF3C51 /* Thread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Thread.h; sourceTree = "<group>"; };
62 65
 		E9AB18051C2A173500BF3C51 /* Thread.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Thread.m; sourceTree = "<group>"; };
63 66
 		E9F13DFE1C28B3D3004C6B95 /* CaseLights.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CaseLights.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -90,6 +93,18 @@
90 93
 			name = Products;
91 94
 			sourceTree = "<group>";
92 95
 		};
96
+		E9109B161C2AAFA200726111 /* LowLevel */ = {
97
+			isa = PBXGroup;
98
+			children = (
99
+				E9AB17FF1C29F8CD00BF3C51 /* Serial.h */,
100
+				E9AB18001C29F8CD00BF3C51 /* Serial.m */,
101
+				E9109B171C2AAFE100726111 /* GPUStats.h */,
102
+				E9109B181C2AAFE100726111 /* GPUStats.m */,
103
+			);
104
+			name = LowLevel;
105
+			path = ..;
106
+			sourceTree = "<group>";
107
+		};
93 108
 		E9F13DF51C28B3D3004C6B95 = {
94 109
 			isa = PBXGroup;
95 110
 			children = (
@@ -114,11 +129,10 @@
114 129
 				E9F13E021C28B3D3004C6B95 /* AppDelegate.m */,
115 130
 				E9AB18041C2A173500BF3C51 /* Thread.h */,
116 131
 				E9AB18051C2A173500BF3C51 /* Thread.m */,
132
+				E9109B161C2AAFA200726111 /* LowLevel */,
117 133
 				E9F13E071C28B3D3004C6B95 /* Assets.xcassets */,
118 134
 				E9F13E091C28B3D3004C6B95 /* MainMenu.xib */,
119 135
 				E9F13E0C1C28B3D3004C6B95 /* Info.plist */,
120
-				E9AB17FF1C29F8CD00BF3C51 /* Serial.h */,
121
-				E9AB18001C29F8CD00BF3C51 /* Serial.m */,
122 136
 				E9F13E041C28B3D3004C6B95 /* Supporting Files */,
123 137
 			);
124 138
 			path = CaseLights;
@@ -245,6 +259,7 @@
245 259
 			buildActionMask = 2147483647;
246 260
 			files = (
247 261
 				E9AB18011C29F8CD00BF3C51 /* Serial.m in Sources */,
262
+				E9109B191C2AAFE100726111 /* GPUStats.m in Sources */,
248 263
 				E9F13E061C28B3D3004C6B95 /* main.m in Sources */,
249 264
 				E9F13E031C28B3D3004C6B95 /* AppDelegate.m in Sources */,
250 265
 				E9AB18061C2A173500BF3C51 /* Thread.m in Sources */,

+ 20
- 1
CaseLights/AppDelegate.m View File

@@ -8,6 +8,7 @@
8 8
 
9 9
 #import "AppDelegate.h"
10 10
 #import "Serial.h"
11
+#import "GPUStats.h"
11 12
 
12 13
 #define PREF_SERIAL_PORT @"SerialPort"
13 14
 #define PREF_LIGHTS_STATE @"LightState"
@@ -60,7 +61,21 @@
60 61
     
61 62
     // TODO Prepare animations menu
62 63
     
63
-    // TODO Prepare visualizations menu
64
+    // Check if GPU Stats are available, add menu items if so
65
+    NSNumber *usage;
66
+    NSNumber *freeVRAM;
67
+    NSNumber *usedVRAM;
68
+    if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
69
+        NSLog(@"Error reading GPU information\n");
70
+    } else {
71
+        NSMenuItem *itemUsage = [[NSMenuItem alloc] initWithTitle:@"GPU Usage" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
72
+        [menuVisualizations addItem:itemUsage];
73
+        
74
+        NSMenuItem *itemVRAM = [[NSMenuItem alloc] initWithTitle:@"VRAM Usage" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
75
+        [menuVisualizations addItem:itemVRAM];
76
+        
77
+        // TODO Enable item if it was last used
78
+    }
64 79
     
65 80
     // Prepare serial port menu
66 81
     NSArray *ports = [Serial listSerialPorts];
@@ -141,6 +156,10 @@
141 156
     
142 157
 }
143 158
 
159
+- (void)selectedGPUVisualization:(NSMenuItem *)sender {
160
+    
161
+}
162
+
144 163
 - (void)selectedSerialPort:(NSMenuItem *)source {
145 164
     // Store selection for next start-up
146 165
     NSUserDefaults *store = [NSUserDefaults standardUserDefaults];

+ 15
- 0
CaseLights/GPUStats.h View File

@@ -0,0 +1,15 @@
1
+//
2
+//  GPUStats.h
3
+//  CaseLights
4
+//
5
+//  Created by Thomas Buck on 23.12.15.
6
+//  Copyright © 2015 xythobuz. All rights reserved.
7
+//
8
+
9
+#import <Foundation/Foundation.h>
10
+
11
+@interface GPUStats : NSObject
12
+
13
++ (NSInteger)getGPUUsage:(NSNumber **)usage freeVRAM:(NSNumber **)free usedVRAM:(NSNumber **)used;
14
+
15
+@end

+ 71
- 0
CaseLights/GPUStats.m View File

@@ -0,0 +1,71 @@
1
+//
2
+//  GPUStats.m
3
+//  CaseLights
4
+//
5
+//  Created by Thomas Buck on 23.12.15.
6
+//  Copyright © 2015 xythobuz. All rights reserved.
7
+//
8
+
9
+#import <IOKit/IOKitLib.h>
10
+
11
+#import "GPUStats.h"
12
+
13
+@implementation GPUStats
14
+
15
++ (NSInteger)getGPUUsage:(NSNumber **)usage freeVRAM:(NSNumber **)free usedVRAM:(NSNumber **)used {
16
+    if ((usage == nil) || (free == nil) || (used == nil)) {
17
+        NSLog(@"Invalid use of getGPUUsage!\n");
18
+        return 1;
19
+    }
20
+    
21
+    *usage = nil;
22
+    *free = nil;
23
+    *used = nil;
24
+    
25
+    CFMutableDictionaryRef pciDevices = IOServiceMatching(kIOAcceleratorClassName);
26
+    io_iterator_t iterator;
27
+    if (IOServiceGetMatchingServices(kIOMasterPortDefault, pciDevices, &iterator) == kIOReturnSuccess) {
28
+        io_registry_entry_t registry;
29
+        while ((registry = IOIteratorNext(iterator))) {
30
+            CFMutableDictionaryRef services;
31
+            if (IORegistryEntryCreateCFProperties(registry, &services, kCFAllocatorDefault, kNilOptions) == kIOReturnSuccess) {
32
+                CFMutableDictionaryRef properties = (CFMutableDictionaryRef)CFDictionaryGetValue(services, CFSTR("PerformanceStatistics"));
33
+                if (properties) {
34
+                    const void *gpuUsage = CFDictionaryGetValue(properties, CFSTR("GPU Core Utilization"));
35
+                    const void *freeVRAM = CFDictionaryGetValue(properties, CFSTR("vramFreeBytes"));
36
+                    const void *usedVRAM = CFDictionaryGetValue(properties, CFSTR("vramUsedBytes"));
37
+                    
38
+                    if (gpuUsage && freeVRAM && usedVRAM) {
39
+                        // Found the GPU. Store this reference for the next call
40
+                        static ssize_t gpuUsageNum = 0;
41
+                        static ssize_t freeVRAMNum = 0;
42
+                        static ssize_t usedVRAMNum = 0;
43
+                        CFNumberGetValue((CFNumberRef)gpuUsage, kCFNumberSInt64Type, &gpuUsageNum);
44
+                        CFNumberGetValue((CFNumberRef)freeVRAM, kCFNumberSInt64Type, &freeVRAMNum);
45
+                        CFNumberGetValue((CFNumberRef)usedVRAM, kCFNumberSInt64Type, &usedVRAMNum);
46
+                        *usage = [NSNumber numberWithDouble:gpuUsageNum / (double)10000000];
47
+                        *free = [NSNumber numberWithDouble:freeVRAMNum];
48
+                        *used = [NSNumber numberWithDouble:usedVRAMNum];
49
+                        
50
+                        //NSLog(@"GPU: %.3f%% VRAM: %.3f%%\n", gpuUsageNum / (double)10000000,
51
+                              //usedVRAMNum / (double)(freeVRAMNum + usedVRAMNum) * 100.0);
52
+                    }
53
+                }
54
+                CFRelease(services);
55
+            }
56
+            IOObjectRelease(registry);
57
+        }
58
+        IOObjectRelease(iterator);
59
+    } else {
60
+        NSLog(@"Couldn't list PCI devices!\n");
61
+    }
62
+    
63
+    if ((*usage != nil) && (*free != nil) && (*used != nil)) {
64
+        return 0;
65
+    } else {
66
+        NSLog(@"Error reading GPU data!\n");
67
+        return 1;
68
+    }
69
+}
70
+
71
+@end

+ 1
- 1
CaseLights/Info.plist View File

@@ -21,7 +21,7 @@
21 21
 	<key>CFBundleSignature</key>
22 22
 	<string>????</string>
23 23
 	<key>CFBundleVersion</key>
24
-	<string>34</string>
24
+	<string>43</string>
25 25
 	<key>LSApplicationCategoryType</key>
26 26
 	<string>public.app-category.utilities</string>
27 27
 	<key>LSMinimumSystemVersion</key>

+ 2
- 0
CaseLights/Serial.m View File

@@ -9,6 +9,8 @@
9 9
 //  Copyright © 2015 xythobuz. All rights reserved.
10 10
 //
11 11
 
12
+#import <Cocoa/Cocoa.h>
13
+#import <IOKit/IOKitLib.h>
12 14
 #import <IOKit/serial/IOSerialKeys.h>
13 15
 
14 16
 #import "Serial.h"

Loading…
Cancel
Save