python获取gpu使用
查询powershell命令: Get-Counter -ListSet *
CounterSetName : GPU Local Adapter Memory
MachineName : .
CounterSetType : MultiInstance
Description : The local memory usage of each adapter
Paths : {\GPU Local Adapter Memory(*)\Local Usage}
PathsWithInstances : {\GPU Local Adapter Memory(luid_0x00000000_0x0000D51C_phys_0_part_0)\Local Usage, \GPU Local Adapt
er Memory(luid_0x00000000_0x0000D805_phys_0_part_0)\Local Usage}
Counter : {\GPU Local Adapter Memory(*)\Local Usage}
CounterSetName : GPU Engine
MachineName : .
CounterSetType : MultiInstance
Description : The running time of each gpu engine
Paths : {\GPU Engine(*)\Utilization Percentage, \GPU Engine(*)\Running Time}
PathsWithInstances : {\GPU Engine(pid_10888_luid_0x00000000_0x0000D51C_phys_0_eng_0_engtype_3D)\Utilization Percentage,
\GPU Engine(pid_10888_luid_0x00000000_0x0000D51C_phys_0_eng_10_engtype_GDI Render)\Utilization Pe
rcentage, \GPU Engine(pid_10888_luid_0x00000000_0x0000D51C_phys_0_eng_11_engtype_VideoProcessing)\
Utilization Percentage, \GPU Engine(pid_10888_luid_0x00000000_0x0000D51C_phys_0_eng_12_engtype_)\U
tilization Percentage...}
Counter : {\GPU Engine(*)\Utilization Percentage, \GPU Engine(*)\Running Time}
CounterSetName : GPU Adapter Memory
MachineName : .
CounterSetType : MultiInstance
Description : The memory usage of each adapter
Paths : {\GPU Adapter Memory(*)\Shared Usage, \GPU Adapter Memory(*)\Dedicated Usage, \GPU Adapter Memory(
*)\Total Committed}
PathsWithInstances : {\GPU Adapter Memory(luid_0x00000000_0x0000D51C_phys_0)\Shared Usage, \GPU Adapter Memory(luid_0x0
0000000_0x0000D805_phys_0)\Shared Usage, \GPU Adapter Memory(luid_0x00000000_0x0000D51C_phys_0)\De
dicated Usage, \GPU Adapter Memory(luid_0x00000000_0x0000D805_phys_0)\Dedicated Usage...}
Counter : {\GPU Adapter Memory(*)\Shared Usage, \GPU Adapter Memory(*)\Dedicated Usage, \GPU Adapter Memory(
*)\Total Committed}
CounterSetName : GPU Process Memory
MachineName : .
CounterSetType : MultiInstance
Description : The memory usage of each process
Paths : {\GPU Process Memory(*)\Shared Usage, \GPU Process Memory(*)\Dedicated Usage, \GPU Process Memory(
*)\Non Local Usage, \GPU Process Memory(*)\Local Usage...}
PathsWithInstances : {\GPU Process Memory(pid_10888_luid_0x00000000_0x0000D51C_phys_0)\Shared Usage, \GPU Process Memor
y(pid_10888_luid_0x00000000_0x0000D805_phys_0)\Shared Usage, \GPU Process Memory(pid_11224_luid_0x
00000000_0x0000D805_phys_0)\Shared Usage, \GPU Process Memory(pid_11504_luid_0x00000000_0x0000D51C
_phys_0)\Shared Usage...}
Counter : {\GPU Process Memory(*)\Shared Usage, \GPU Process Memory(*)\Dedicated Usage, \GPU Process Memory(
*)\Non Local Usage, \GPU Process Memory(*)\Local Usage...}
CounterSetName : GPU Non Local Adapter Memory
MachineName : .
CounterSetType : SingleInstance
Description : The non local memory usage of each adapter
Paths : {\GPU Non Local Adapter Memory(*)\Non Local Usage}
PathsWithInstances : {\GPU Non Local Adapter Memory(*)\Non Local Usage}
Counter : {\GPU Non Local Adapter Memory(*)\Non Local Usage}
python调用
import subprocess
def get_gpu_dedicated_usage():
args = r'get-counter -counter "\GPU Adapter Memory(*)\Dedicated Usage"'
b_args = args.encode("CP437")
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
popen = subprocess.Popen("powershell.exe -NoLogo -NonInteractive -Command - ", shell=True, startupinfo=startupinfo,
stderr=subprocess.STDOUT,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
b_outs, b_error = popen.communicate(b_args, timeout=15)
outs = b_outs.decode("CP437").split("\r\n")
ret = 0
for i in outs:
if 'dedicated usage' in i:
ret += int(outs[outs.index(i) + 1].strip())
print(f'dedicated usage: {ret}')
return ret
if __name__ == '__main__':
get_gpu_dedicated_usage()