usr/lib/Get-CpuCache.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function Get-CpuCache {
  [CmdletBinding()]param()

  begin {
    New-Enum LOGICAL_PROCESSOR_RELATIONSHIP {
      RelationProcessorCore
      RelationNumaNode
      RelationCache
      RelationProcessorPackage
      RelationGroup
      RelationAll = 0xffff
    }

    New-Enum PROCESSOR_CACHE_TYPE {
      CacheUnified
      CacheInstruction
      CacheData
      CacheTrace
    }

    New-Structure PROCESSORCORE {
      Byte Flags
    }

    New-Structure NUMANODE {
      UInt32 NodeNumber
    }

    New-Structure CACHE_DESCRIPTOR {
      Byte   Level
      Byte   Associativity
      UInt16 LineSize
      UInt32 Size
      PROCESSOR_CACHE_TYPE Type
    }

    New-Structure SYSTEM_LOGICAL_PROCESSOR_INFORMATION_UNION {
      PROCESSORCORE 'ProcessorCore 0'
      NUMANODE 'NumaNode 0'
      CACHE_DESCRIPTOR 'Cache 0'
      UInt64 'Reserved 8'
    } -Explicit

    New-Structure SYSTEM_LOGICAL_PROCESSOR_INFORMATION {
      UIntPtr ProcessorMask
      LOGICAL_PROCESSOR_RELATIONSHIP Relationship
      SYSTEM_LOGICAL_PROCESSOR_INFORMATION_UNION ProcessorInformation
    }

    New-Delegate kernel32 {
      bool GetLogicalProcessorInformation([buf, uint_])
    }
  }
  process {}
  end {
    $bsz = 0 # first pass is required to retrieve real buffer size
    if (!$kernel32.GetLogicalProcessorInformation.Invoke($null, [ref]$bsz) -and !$bsz) {
      throw [InvalidOperationException]::new('Cannot retrieve buffer size')
    }

    $buf = [Byte[]]::new($bsz)
    if (!$kernel32.GetLogicalProcessorInformation.Invoke($buf, [ref]$bsz)) {
      throw [InvalidOperationException]::new('Internal error.')
    }

    (0..($bsz / ($sz = [SYSTEM_LOGICAL_PROCESSOR_INFORMATION]::GetSize()) - 1)).ForEach{
      $slpi = ConvertTo-PointerOrStructure $buf[0..($sz - 1)] (
        [SYSTEM_LOGICAL_PROCESSOR_INFORMATION]
      )
      if ($slpi.Relationship -eq 2) { $slpi.ProcessorInformation.Cache }
      $buf = $buf[$sz..$buf.Length]
    } | Format-Table -AutoSize
  }
}

Export-ModuleMember -Function Get-CpuCache