pwsh/Get-CpuCache.ps1
function Get-CpuCache { [CmdletBinding()]param() process { New-Delegate kernel32 { bool GetLogicalProcessorInformation([buf, uint_]) } $sz = 0 # first pass is requierd to retrieve real buffer size if (!$kernel32.GetLogicalProcessorInformation.Invoke($null, [ref]$sz) -and !$sz) { throw [InvalidOperationException]::new('Cannot retrieve info buffer size.') } $buf = [Byte[]]::new($sz) if (!$kernel32.GetLogicalProcessorInformation.Invoke($buf, [ref]$sz)) { throw [InvalidOperationException]::new('Internal error.') } # sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) x86 - 0x18, x64 - 0x20 $(for ($pct, $total, $i = ('CacheUnified', 'CacheInstruction', 'CacheData', 'CacheTrace' ), ($buf.Length / ($sz = (24, 32)[[IntPtr]::Size / 4 - 1])), 0; $i -lt $total; $i++) { $slpi = Read-DataValues $buf[0..($sz - 1)] -Map p2b2SI2 if ($slpi[1] -eq 2) { # RelationCache = 0x02 [PSCustomObject]@{ Type = $pct[$slpi[5]] Level = ($x = [Int32[]]($slpi[2] -replace '\\', ',0' -split ','))[1] Associativity = $x[2] LineSize = $slpi[3] Size = $slpi[4] / 1Kb } } $buf = $buf[$sz..$buf.Length] }) | Format-Table -AutoSize } } Export-ModuleMember -Function Get-CpuCache |