python获取win系统GPU使用

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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值