Last active
September 6, 2019 09:28
-
-
Save hizumi/905d9038e52e2d4bc9c09ba2c9b35110 to your computer and use it in GitHub Desktop.
nvidia-smiを使ってGPU使用率と温度と消費電力を取得してくるPowerShellスクリプト mackerel-agent出力準拠
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
cd %~dp0 | |
powershell -ExecutionPolicy RemoteSigned -File .\Gpu.ps1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define | |
$path = "C:\Program Files\NVIDIA Corporation\NVSMI\nvidia-smi.exe" | |
Function GetNvidiaSMIList($p) | |
{ | |
$ninfo = New-Object System.Diagnostics.ProcessStartInfo | |
$ninfo.filename = $p | |
$ninfo.RedirectStandardError = $true | |
$ninfo.RedirectStandardOutput = $true | |
$ninfo.UseShellExecute = $false | |
$ninfo.Arguments = "-L" | |
$n = New-Object System.Diagnostics.Process | |
$n.StartInfo = $ninfo | |
$n.Start() | Out-Null | |
$n.WaitForExit() | |
$stdout = $n.StandardOutput.ReadToEnd() -split "`r`n" | |
return $stdout | |
} | |
Function GetNvidiaSMIStatus($p,$cmd,$i) | |
{ | |
echo $args | |
$ninfo = New-Object System.Diagnostics.ProcessStartInfo | |
$ninfo.filename = $p | |
$ninfo.RedirectStandardError = $true | |
$ninfo.RedirectStandardOutput = $true | |
$ninfo.UseShellExecute = $false | |
$ninfo.Arguments = "-q -i $i -d $cmd" | |
$n = New-Object System.Diagnostics.Process | |
$n.StartInfo = $ninfo | |
$n.Start() | Out-Null | |
$n.WaitForExit() | |
return $n.StandardOutput.ReadToEnd() | tee -Variable text | |
} | |
Function Epoch_utc | |
{ | |
$date1 = Get-Date -Date "01/01/1970" | |
$date2 = Get-Date | |
$date = (New-TimeSpan -Start $date1 -End $date2).TotalSeconds | |
$epoch_jst = [math]::Truncate($date) | |
$epoch_utc = $epoch_jst - 32400 | |
return [string]$epoch_utc | tee -Variable text | |
} | |
#GPUList | |
$gpulist = @() | |
foreach($g in GetNvidiaSMIList $path ){ | |
if ($g -ne ""){ | |
$garray = @() | |
$g -match "GPU (?<Gnum>.*?): (?<Gname>.*?) \(UUID: (?<Guuid>.*?)\)" | Out-Null | |
$garray = @($Matches.Gnum,$Matches.Gname.Replace(' ', '_'),$Matches.Guuid) | |
$gpulist += ,@($garray) | |
} | |
} | |
$time = Epoch_utc | |
#GPUUtilization | |
foreach($gg in $gpulist){ | |
$cmd = "UTILIZATION" | |
$out = GetNvidiaSMIStatus $path $cmd $gg[0] | |
if ($out -notmatch "Gpu +: (?<Gpuutil>.*?)\%\r\n.*?Memory +: (?<Gmemutil>.*?)\%\r\n.*?Encoder +: (?<Gencutil>.*?)\%\r\n.*?Decoder +: (?<Gdecutil>.*?)\%") | |
{ | |
$stat = @(0,0,0,0) | |
} | |
else | |
{ | |
$stat = @($Matches.Gpuutil,$Matches.Gmemutil,$Matches.Gencutil,$Matches.Gdecutil) | |
} | |
Write-output "GPU.Utilization.$($gpulist[$gg[0]][1]).GPU`t$($stat[0])`t$time" | |
Write-output "GPU.Utilization.$($gpulist[$gg[0]][1]).Memory`t$($stat[1])`t$time" | |
Write-output "GPU.Utilization.$($gpulist[$gg[0]][1]).Encoder`t$($stat[2])`t$time" | |
Write-output "GPU.Utilization.$($gpulist[$gg[0]][1]).Decoder`t$($stat[3])`t$time" | |
} | |
#GPUTemp | |
foreach($gg in $gpulist){ | |
$cmd = "TEMPERATURE" | |
$out = GetNvidiaSMIStatus $path $cmd $gg[0] | |
$out -match "GPU Current Temp +: (?<Gtemp>.*?) C" | Out-Null | |
$stat = @($Matches.Gtemp) | |
Write-output "GPU.Temp.$($gpulist[$gg[0]][1]).Current`t$stat`t$time" | |
} | |
#GPUWatt | |
foreach($gg in $gpulist) | |
{ | |
$cmd = "POWER" | |
$out = GetNvidiaSMIStatus $path $cmd $gg[0] | |
if ($out -notmatch "Power Management +: N/A") | |
{ | |
$out -match "Power Draw +: (?<Gwatt>.*?) W" | Out-Null | |
$stat = @($Matches.Gwatt) | |
} | |
else | |
{ | |
$stat = "0" | |
} | |
Write-output "GPU.Power.$($gpulist[$gg[0]][1]).Current`t$stat`t$time" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment