我曾尝试Get-Process
获取Get-CimInstance Win32_Process
GPU 使用情况和 GPU 内存,但它们都无法提供这些信息。
在Windows任务管理器中,它可以显示GPU内存。
那么,有没有办法在 powershell 中获取这些
答案1
Windows 使用性能计数器来跟踪这种事情。您可以通过 powershell 获取值Get-Counter
。根据您的 GPU 和驱动程序,您可能有更具体的计数器可用,但这些示例应始终可用:
# Example to get GPU usage counters for a specific process:
$p = Get-Process dwm
((Get-Counter "\GPU Process Memory(pid_$($p.id)*)\Local Usage").CounterSamples | where CookedValue).CookedValue |
foreach {Write-Output "Process $($P.Name) GPU Process Memory $([math]::Round($_/1MB,2)) MB"}
((Get-Counter "\GPU Engine(pid_$($p.id)*engtype_3D)\Utilization Percentage").CounterSamples | where CookedValue).CookedValue |
foreach {Write-Output "Process $($P.Name) GPU Engine Usage $([math]::Round($_,2))%"}
# Outputs:
Process dwm GPU Process Memory 259.36 MB
Process dwm GPU Engine Usage 0.47%
# Example to get total GPU usage counters:
$GpuMemTotal = (((Get-Counter "\GPU Process Memory(*)\Local Usage").CounterSamples | where CookedValue).CookedValue | measure -sum).sum
Write-Output "Total GPU Process Memory Local Usage: $([math]::Round($GpuMemTotal/1MB,2)) MB"
$GpuUseTotal = (((Get-Counter "\GPU Engine(*engtype_3D)\Utilization Percentage").CounterSamples | where CookedValue).CookedValue | measure -sum).sum
Write-Output "Total GPU Engine Usage: $([math]::Round($GpuUseTotal,2))%"
# Outputs:
Total GPU Process Memory Local Usage: 511.36 MB
Total GPU Engine Usage: 0.77%
我通过运行 找到了这些计数器名称Get-Counter -ListSet 'GPU*'
,并Counter
从中获取值。看来 GPU 引擎的使用情况分为 4 种类型:
engtype_3D
engtype_VideoDecode
engtype_Copy
engtype_VideoProcessing
我只是在示例中求和engtype_3D
,因为它是我系统上唯一有使用率的。这些值都与 Windows 任务管理器中显示的值相匹配,因此应该足够准确。