-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkGPU.py
187 lines (174 loc) · 7.38 KB
/
checkGPU.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import wmi
import re
import psutil
import time
import traceback
import threading
import win32com.client
from os import path
class gameCheck:
def __init__(self, threshold, times = 3, gameListFile = "gamelist.txt", excludeListFile = "excludelist.txt", useCOM = True):
self.w = wmi.WMI(find_classes=False)
self.isGaming = False
self.activeTasks = {}
self.loop = 0
self.seqCount = 0
self.percThreshold = threshold
self.times = times #number of times - 1
#self.delaySec = delaySec
self.useCOM = useCOM
self.debugMsg = ""
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
self.objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
self.gameListFile = gameListFile
self.excludeListFile = excludeListFile
self.gameList = []
self.excludeList = []
self.activeGamePID = None
self.readGameFile()
self.readExcludeFile()
self.isGmListDirty = False
self.isExListDirty = False
def readGameFile(self):
if (path.exists(self.gameListFile)):
fh = open(self.gameListFile,"r")
self.gameList = fh.readlines()
for c,item in enumerate(self.gameList):
self.gameList[c] = item.rstrip()
fh.close()
self.isGmListDirty = False
def writeGameFile(self, name):
fh = open(self.gameListFile,"a")
fh.write(name)
fh.close()
self.isGmListDirty = True
def isOnGameList(self, newItem):
if (self.isGmListDirty == True):
self.debugMsg = self.debugMsg + f"Rereading Include file"
self.readGameFile()
for item in self.gameList:
if (newItem == item):
return True
return False
def readExcludeFile(self):
if (path.exists(self.excludeListFile)):
fh = open(self.excludeListFile,"r")
self.excludeList = fh.readlines()
for c,item in enumerate(self.excludeList):
self.excludeList[c] = item.rstrip()
fh.close()
self.isExListDirty = False
def writeExcludeFile(self):
fh = open(self.excludeListFile,"a")
fh.write(psutil.Process(int(self.activeGamePID)).name() + '\n')
fh.close()
self.isExListDirty = True
def isOnExcludeList(self, newItem):
if (self.isExListDirty == True):
self.debugMsg = self.debugMsg + f"Rereading Exclude file"
self.readExcludeFile()
for item in self.excludeList:
if (newItem == item):
return True
return False
def mIsGaming(self):
pid = 0
pName = ""
usage = ""
#self.debugMsg = ""
#self.loop = 0
gpu = None
try:
if (self.useCOM == True):
gpu = self.objSWbemServices.ExecQuery("SELECT * FROM Win32_PerfFormattedData_GPUPerformanceCounters_GPUEngine")
else:
gpu = self.w.Win32_PerfFormattedData_GPUPerformanceCounters_GPUEngine()
#search for cached game
for task in gpu:
m = re.search("^pid_([0-9]+)_luid.+",task.name)
pid = m.group(1)
if (psutil.pid_exists(int(pid))):
pName = psutil.Process(int(pid)).name()
else:
continue
if (self.isOnExcludeList(pName) == True):
self.debugMsg = f"Found excluded game, {pName}\n"
continue
if ( (self.isOnGameList(pName) == True) and (self.isOnExcludeList(pName) == False) ):
self.debugMsg = f"found cached 3d app: ({pName}:{pid})\n"
self.isGaming = True
self.activeGamePID = pid
return True
#enumerate gpu loads
for task in gpu:
if (task.name.find('engtype_Graphics') > -1 or task.name.find('engtype_3D') > -1 or task.name.find('engtype_VR') > -1):
m = re.search("^pid_([0-9]+)_luid.+",task.name)
pid = m.group(1)
if (psutil.pid_exists(int(pid))):
pName = psutil.Process(int(pid)).name()
else:
pName = "Expired Task"
continue
#skip the desktop window manager (dwm.exe) for VR
#if (self.isOnExcludeList(pName) == True):
# continue
usage = task.UtilizationPercentage
if ( (int(usage) > self.percThreshold) and (self.isOnExcludeList(pName) == False) ):
if (pid in self.activeTasks.keys()):
self.activeTasks[pid] = self.activeTasks[pid] + 1
else:
self.activeTasks[pid] = 1
self.debugMsg = self.debugMsg + f"found {pid}-\t{pName}: {usage}\n"
#check keys to see if a task has exceeded for self.time passes
if (self.loop == self.times):
for x in self.activeTasks:
if (self.activeTasks[x] > self.times):
self.debugMsg = self.debugMsg + f"adding active 3d app: ({x})\n"
self.writeGameFile(psutil.Process(int(x)).name() + '\n')
self.isGaming = True
self.activeTasks.clear()
self.activeGamePID = x
return True
self.activeTasks.clear()
self.loop = 0
else:
self.loop = self.loop + 1
return True
return True
except Exception as err:
self.debugMsg = self.debugMsg + f"Caught an exception:\n {err}:\n"
self.debugMsg = self.debugMsg + traceback.format_exc()
return False
def mNotGaming(self):
self.debugMsg = ""
try:
if (psutil.pid_exists(int(self.activeGamePID)) == False):
self.debugMsg = self.debugMsg + f"Game has exited\n"
#self.activeTasks.clear()
self.isGaming = False
# for x in self.activeTasks:
# if (self.activeTasks[x] > self.times):
# if (psutil.pid_exists(int(x)) == False):
# self.debugMsg = self.debugMsg + f"Game has exited\n"
# self.activeTasks.clear()
# self.isGaming = False
# break
return True
except Exception as err:
self.debugMsg = self.debugMsg + f"Caught an exception:\n {err}:\n {traceback.format_exc()}"
return False
if __name__ == "__main__":
g = gameCheck(15)
while True:
print(f"Isgaming: {g.isGaming}")
if (g.isGaming == False):
start = time.time()
g.mIsGaming()
print(g.debugMsg, end='')
end = time.time()
print(f"loop time: {end - start}")
else:
g.mNotGaming()
print(g.debugMsg, end='')
time.sleep(3)