Created
December 20, 2023 08:08
-
-
Save ZeekoZhu/9832a4e296b08a328f79cc350fa28096 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/pwsh | |
# meminfo -sort <name|value> -list | |
param( | |
[Parameter(Mandatory = $false)] [switch] $list = $false, | |
[parameter(Mandatory = $false)] [switch] $human = $false, | |
[Parameter(Mandatory = $false)] [ValidateSet('name', 'value')] [string] $sort = 'value' | |
) | |
# read /proc/meminfo | |
function Format-ByteSize( | |
[Parameter(Mandatory, ValueFromPipeline)] [int64] $bytes | |
) | |
{ | |
$KiB = 1000 | |
$MiB = $KiB * 1000 | |
$GiB = $MiB * 1000 | |
$TiB = $GiB * 1000 | |
if($human -eq $False) | |
{ | |
return $bytes | |
} | |
if ($bytes -ge $TiB) | |
{ | |
"{0:0.00} TiB" -f ($bytes / $TiB) | |
} elseif ($bytes -ge $GiB) | |
{ | |
"{0:0.00} GiB" -f ($bytes / $GiB) | |
} elseif ($bytes -ge $MiB) | |
{ | |
"{0:0.00} MiB" -f ($bytes / $MiB) | |
} elseif ($bytes -ge $KiB) | |
{ | |
"{0:0.00} KiB" -f ($bytes / $KiB) | |
} else | |
{ | |
"{0:0.00} B" -f $bytes | |
} | |
} | |
function Get-GpuMemory() | |
{ | |
$vramOutput = (sudo cat /sys/kernel/debug/dri/0/amdgpu_vram_mm | grep 'usage').Split('`n')[0].Trim(); | |
$vram = $vramOutput.Substring(7).Trim() -as [int64]; | |
$gtt = (sudo cat /sys/kernel/debug/dri/0/amdgpu_gtt_mm | grep 'usage').Split('`n')[0].Trim().Substring(7).Trim() -as [int64]; | |
return $vram + $gtt; | |
} | |
$meminfo = Get-Content /proc/meminfo | |
# parse result into a hashtable | |
$meminfoRaw = $meminfo | ForEach-Object { | |
$key, $value = $_ -split ':' | |
$key = $key.Trim() | |
# remove kB from the value | |
$value = ($value.Trim() -replace ' kB$' -as [int]) * 1KB | |
[PSCustomObject]@{ | |
Name = $key | |
Value = $value | |
} | |
} | |
$meminfoDisplay = $meminfoRaw | Sort-Object $sort | ForEach-Object { | |
[PSCustomObject]@{ | |
Name = $_.Name | |
Value = $_.Value | Format-ByteSize | |
} | |
} | |
# output the result as a table | |
if ($list) | |
{ | |
$meminfoDisplay | Format-Table -AutoSize | |
exit | |
} | |
# sum the following values | |
$kernelDynamicMemItems = $meminfoRaw | Where-Object { | |
$_.Name -in 'Slab', 'PageTables', 'KernelStack', 'VmallocUsed', 'Bounce', 'HardwareCorrupted', 'Zswap' | |
} | |
# add Gpu memory to the list | |
$kernelDynamicMemItems += [PSCustomObject]@{ | |
Name = 'Gpu' | |
Value = (Get-GpuMemory) -as [int64] | |
} | |
$kernelDynamicMemItems | ForEach-Object { | |
[PSCustomObject]@{ | |
Name = $_.Name | |
Value = $_.Value | Format-ByteSize | |
} | |
} | Format-Table -AutoSize | |
$kernelDynamicMem = $kernelDynamicMemItems | Measure-Object -Property Value -Sum | |
Write-Host "Kernel Dynamic Memory: $($kernelDynamicMem.Sum | Format-ByteSize)" | |
$totalUsedMem = $meminfoRaw | Where-Object { | |
$_.Name -in 'Active', 'Inactive' | |
} | Measure-Object -Property Value -Sum | |
Write-Host "Total Used Memory: $($totalUsedMem.Sum | Format-ByteSize)" | |
Write-Host "used: $($totalUsedMem.Sum + $kernelDynamicMem.Sum | Format-ByteSize)" | |
function show-MemoryPressure () | |
{ | |
function get-usage( | |
[parameter(Mandatory, ValueFromPipeline)] | |
[string]$line | |
) | |
{ | |
return $line -split '\s' | Select-Object -Skip 1 | Select-Object -SkipLast 1 | ForEach-Object { | |
$key, $value = $_ -split '=' | |
[PSCustomObject]@{ | |
Recent = $key.Substring(3) + 's' | |
WaitingTasks = ([float]$value).ToString("P2") | |
} | |
} | |
} | |
# memory pressure | |
Write-Host "`nHow many task are blocked waiting for memory" | |
$content = Get-Content /sys/fs/cgroup/system.slice/memory.pressure | |
# some avg10=0.00 avg60=0.00 avg300=0.00 total=4009 | |
# full avg10=0.00 avg60=0.00 avg300=0.00 total=3958 | |
# parse the resule into { avg10 = 0.00, avg60 = 0.00, avg300 = 0.00 } | |
Write-Host "Some" | |
$content[0] | get-usage | Format-Table | |
Write-Host "Full" | |
$content[1] | get-usage | Format-Table | |
} | |
show-MemoryPressure | |
# vim:ft=ps1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment