Skip to content

Commit

Permalink
Merge pull request #10 from itslightmind/main
Browse files Browse the repository at this point in the history
Added a Generate CPU Table script
  • Loading branch information
Gobidev authored Dec 8, 2023
2 parents 8f990e8 + c4a4a85 commit 4c7c65e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
56 changes: 56 additions & 0 deletions CPU Table Examples/Generate CPU Table.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# PowerShell script to generate a CPU Affinity table for Windows environments.
# Can be run directly in a Powershell Shell. So no need to run it as .ps1, just copy and paste it.
# Utilizes Win32_Processor class to fetch CPU details and calculates thread affinity.

# Retrieve CPU information.
$cpuInfo = Get-WmiObject -Class Win32_Processor
$totalCores = $cpuInfo.NumberOfCores
$totalThreads = $cpuInfo.NumberOfLogicalProcessors

# Initialize StringBuilder for output formatting.
$outputBuilder = New-Object System.Text.StringBuilder
$outputBuilder.AppendLine(":: Affinity table for $($cpuInfo.Name) with Hyperthreading")

# Define a function to format affinity values into bitmasks.
Function Format-BitMask ($affinityValue) {
$bitMask = [Convert]::ToString($affinityValue, 2).PadLeft(8, '0')
return $bitMask.Length -gt 8 ? $bitMask.Insert($bitMask.Length - 8, ' ') : $bitMask
}

# Initialize variables for data collection and formatting.
$maxCoreThreadLength = $maxValueLength = $maxBitMaskLength = 0
$coreThreadInfo = @()
$valueInfo = @()
$bitMaskInfo = @()

# Determine the starting index for hyperthreaded cores.
$hyperthreadedStartIndex = $totalCores / 2

# Collect and format data for each thread.
for ($i = 0; $i -lt $totalThreads; $i++) {
$affinityValue = 1 -shl $i
$formattedBitMask = Format-BitMask -affinityValue $affinityValue
$coreType = if ($i -lt $totalCores) { "P-Core" } else { "E-Core" }
$threadID = if ($coreType -eq "P-Core" -and $i -ge $hyperthreadedStartIndex) { "$i`T" } else { "$i" }

$coreThreadInfo += "$coreType $threadID"
$valueInfo += $affinityValue.ToString()
$bitMaskInfo += $formattedBitMask
}

# Compute maximum lengths for table formatting.
$maxCoreThreadLength = ($coreThreadInfo | Measure-Object -Property Length -Maximum).Maximum
$maxValueLength = ($valueInfo | Measure-Object -Property Length -Maximum).Maximum
$maxBitMaskLength = ($bitMaskInfo | Measure-Object -Property Length -Maximum).Maximum

# Construct the header for the affinity table.
$header = "Thread #".PadRight($maxCoreThreadLength) + " = " + "Value".PadRight($maxValueLength) + " = BitMask"
$outputBuilder.AppendLine(":: $header")

# Build the table with aligned data.
for ($i = 0; $i -lt $totalThreads; $i++) {
$outputBuilder.AppendLine(":: $($coreThreadInfo[$i].PadRight($maxCoreThreadLength)) = $($valueInfo[$i].PadRight($maxValueLength)) = $($bitMaskInfo[$i].PadRight($maxBitMaskLength))")
}

# Output the formatted table.
$outputBuilder.ToString()
26 changes: 26 additions & 0 deletions CPU Table Examples/i7 13700k ( 8-16P and 8E cores )
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
:: Affinity table for Intel i7-13700k Processor with Hyperthreading
:: Thread # = Value = BitMask
:: P-Core 0 = 1 = 00000001
:: P-Core 1 = 2 = 00000010
:: P-Core 2 = 4 = 00000100
:: P-Core 3 = 8 = 00001000
:: P-Core 4 = 16 = 00010000
:: P-Core 5 = 32 = 00100000
:: P-Core 6 = 64 = 01000000
:: P-Core 7 = 128 = 10000000
:: P-Core 8T = 256 = 1 00000000
:: P-Core 9T = 512 = 10 00000000
:: P-Core 10T = 1024 = 100 00000000
:: P-Core 11T = 2048 = 1000 00000000
:: P-Core 12T = 4096 = 10000 00000000
:: P-Core 13T = 8192 = 100000 00000000
:: P-Core 14T = 16384 = 1000000 00000000
:: P-Core 15T = 32768 = 10000000 00000000
:: E-Core 16 = 65536 = 100000000 00000000
:: E-Core 17 = 131072 = 1000000000 00000000
:: E-Core 18 = 262144 = 10000000000 00000000
:: E-Core 19 = 524288 = 100000000000 00000000
:: E-Core 20 = 1048576 = 1000000000000 00000000
:: E-Core 21 = 2097152 = 10000000000000 00000000
:: E-Core 22 = 4194304 = 100000000000000 00000000
:: E-Core 23 = 8388608 = 1000000000000000 00000000

0 comments on commit 4c7c65e

Please sign in to comment.