Skip to content

Commit

Permalink
Add the usage of SYSTEM_CPU_SET_INFORMATION
Browse files Browse the repository at this point in the history
  • Loading branch information
spddl committed Jun 11, 2024
1 parent 3c3ab08 commit a0a80b3
Show file tree
Hide file tree
Showing 9 changed files with 732 additions and 63 deletions.
40 changes: 34 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import (
. "github.com/tailscale/walk/declarative"
)

var cs CpuSets

func main() {
cs.Init()

var devices []Device
devices, handle = FindAllDevices()

Expand Down Expand Up @@ -144,10 +148,12 @@ func main() {
Children: []Widget{

TableView{
OnItemActivated: mw.lb_ItemActivated,
Name: "tableView", // Name is needed for settings persistence
AlternatingRowBG: true,
ColumnsOrderable: true,
OnItemActivated: mw.lb_ItemActivated,
Name: "tableView", // Name is needed for settings persistence
AlternatingRowBG: true,
ColumnsOrderable: true,
ColumnsSizable: true,
LastColumnStretched: true,
Columns: []TableViewColumn{
{
Name: "DeviceDesc",
Expand All @@ -166,7 +172,7 @@ func main() {
{
Name: "MsiSupported",
Title: "MSI Mode",
Width: 50,
Width: 60,
Alignment: AlignCenter,
FormatFunc: func(value interface{}) string {
if value.(uint32) == 0 {
Expand Down Expand Up @@ -214,7 +220,11 @@ func main() {
result = append(result, cpu)
}
}
sort.Strings(result)

result, err := sortNumbers(result)
if err != nil {
log.Println(err)
}
return strings.Join(result, ",")
},
LessFunc: func(i, j int) bool {
Expand Down Expand Up @@ -434,3 +444,21 @@ type Model struct {
func (m *Model) Items() interface{} {
return m.items
}

func sortNumbers(data []string) ([]string, error) {
var lastErr error
sort.Slice(data, func(i, j int) bool {
a, err := strconv.ParseInt(data[i], 10, 64)
if err != nil {
lastErr = err
return false
}
b, err := strconv.ParseInt(data[j], 10, 64)
if err != nil {
lastErr = err
return false
}
return a < b
})
return data, lastErr
}
7 changes: 2 additions & 5 deletions build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
SET GOOS=windows
SET GOARCH=amd64
SET filename=GoInterruptPolicy
@REM for %%I in (.) do set "filename=%%~nxI"

:loop
CLS

@REM gocritic check -enable="#performance" ./...
gocritic check -enableAll -disable="#experimental,#opinionated,#commentedOutCode" ./...

IF exist %filename%.exe (
Expand All @@ -18,7 +16,7 @@ IF exist %filename%.exe (
)

: Build https://golang.org/cmd/go/
:: go build -v -ldflags="-w -s" -o %filename%.exe
go build -tags debug -buildvcs=false -o %filename%_debug.exe
go build -ldflags="-w -s -H windowsgui" -o %filename%.exe

FOR /F "usebackq" %%A IN ('%filename%.exe') DO SET /A size=%%~zA
Expand All @@ -35,8 +33,7 @@ IF %diffSize% EQU 0 (
)

: Run
@REM IF %ERRORLEVEL% EQU 0 start /B /wait build/%filename%.exe
IF %ERRORLEVEL% EQU 0 %filename%.exe
@REM IF %ERRORLEVEL% EQU 0 %filename%.exe

PAUSE
GOTO loop
11 changes: 11 additions & 0 deletions cpuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import "github.com/intel-go/cpuid"

func isAMD() bool {
return cpuid.VendorIdentificatorString == "AuthenticAMD"
}

func isIntel() bool {
return cpuid.VendorIdentificatorString == "GenuineIntel"
}
150 changes: 150 additions & 0 deletions cpusetinformation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package main

import (
"log"

"golang.org/x/sys/windows"
)

// AMD 5900x
func Fake5900x() {
size := 0x20 * 64
cpuSet := make([]SYSTEM_CPU_SET_INFORMATION, size)
cpuSet[0].Size = 32
var lastCoreIndex byte
var count = 768
var index = 0x100
for i := 0; i < count; i++ {
cs := cpuSet[i].CpuSet()
cs.Id = uint32(index + i)
cs.LogicalProcessorIndex = byte(i)
if i%2 != 0 {
cs.CoreIndex = lastCoreIndex
} else {
cs.CoreIndex = byte(i)
lastCoreIndex = byte(i)
}
if i > 11 {
cs.LastLevelCacheIndex = 12
}
}
SystemCpuSets = cpuSet[:count]
}

// Intel i9 13900
func Fake13900() {
size := 0x20 * 64
cpuSet := make([]SYSTEM_CPU_SET_INFORMATION, size)
cpuSet[0].Size = 32
var lastCoreIndex byte
var count = 1024
var index = 0x100
for i := 0; i < count; i++ {
cs := cpuSet[i].CpuSet()
cs.Id = uint32(index + i)
cs.LogicalProcessorIndex = byte(i)

if i < 16 && i%2 != 0 {
cs.CoreIndex = lastCoreIndex
} else {
cs.CoreIndex = byte(i)
lastCoreIndex = byte(i)
}
if i < 16 {
cs.EfficiencyClass = 1
}

}
SystemCpuSets = cpuSet[:count]
}

var SystemCpuSets = []SYSTEM_CPU_SET_INFORMATION{}

const (
ToolTipTextNumaNode = "A group-relative value indicating which NUMA node a CPU Set is on. All CPU Sets in a given group that are on the same NUMA node will have the same value for this field."
ToolTipTextLastLevelCache = "A group-relative value indicating which CPU Sets share at least one level of cache with each other. This value is the same for all CPU Sets in a group that are on processors that share cache with each other."
ToolTipTextEfficiencyClass = "A value indicating the intrinsic energy efficiency of a processor for systems that support heterogeneous processors (such as ARM big.LITTLE systems). CPU Sets with higher numerical values of this field have home processors that are faster but less power-efficient than ones with lower values."
)

type CpuSets struct {
HyperThreading bool
CoreCount int
MaxThreadsPerCore int
NumaNode bool // A group-relative value indicating which NUMA node a CPU Set is on. All CPU Sets in a given group that are on the same NUMA node will have the same value for this field.
LastLevelCache bool // A group-relative value indicating which CPU Sets share at least one level of cache with each other. This value is the same for all CPU Sets in a group that are on processors that share cache with each other.
EfficiencyClass bool // A value indicating the intrinsic energy efficiency of a processor for systems that support heterogeneous processors (such as ARM big.LITTLE systems). CPU Sets with higher numerical values of this field have home processors that are faster but less power-efficient than ones with lower values.
CPU []CpuSet
}

type CpuSet struct {
Id uint32
CoreIndex byte
LogicalProcessorIndex byte
LastLevelCacheIndex byte // A group-relative value indicating which CPU Sets share at least one level of cache with each other. This value is the same for all CPU Sets in a group that are on processors that share cache with each other.
EfficiencyClass byte // A value indicating the intrinsic energy efficiency of a processor for systems that support heterogeneous processors (such as ARM big.LITTLE systems). CPU Sets with higher numerical values of this field have home processors that are faster but less power-efficient than ones with lower values.
NumaNodeIndex byte // A group-relative value indicating which NUMA node a CPU Set is on. All CPU Sets in a given group that are on the same NUMA node will have the same value for this field.
}

func (cs *CpuSets) Init() {
size := 0x20 * 64
SystemCpuSets = make([]SYSTEM_CPU_SET_INFORMATION, size)

var length uint32
var hProcess windows.Handle
success := GetSystemCpuSetInformation(&SystemCpuSets[0], uint32(size), &length, uintptr(hProcess), 0)
if success != 1 {
log.Println("err")
} else {
SystemCpuSets = SystemCpuSets[:length]
}

/// debug
// Fake13900()
// Fake5900x()

cs.CoreCount = int(uint32(len(SystemCpuSets)) / SystemCpuSets[0].Size)
var lastEfficiencyClass, lastLevelCache, lastNumaNodeIndex byte

for i := 0; i < cs.CoreCount; i++ {
cpu := SystemCpuSets[i].CpuSet()
if i == 0 { // The EfficiencyClass starts with 1 on the Intel Gen12+
lastEfficiencyClass = cpu.EfficiencyClass
}

cs.CPU = append(cs.CPU, CpuSet{
Id: cpu.Id,
CoreIndex: cpu.CoreIndex,
LogicalProcessorIndex: cpu.LogicalProcessorIndex,
EfficiencyClass: cpu.EfficiencyClass,
LastLevelCacheIndex: cpu.LastLevelCacheIndex,
NumaNodeIndex: cpu.NumaNodeIndex,
})

// fmt.Printf("(%02d) [%d/%x] %02d/%02d Eff%d CCD%d NUMA%d\n", i, cpu.Id, cpu.Id, cpu.CoreIndex, cpu.LogicalProcessorIndex, cpu.EfficiencyClass, cpu.LastLevelCacheIndex, cpu.NumaNodeIndex)

if cpu.CoreIndex != cpu.LogicalProcessorIndex {
if !cs.HyperThreading {
cs.HyperThreading = true
}
if cs.MaxThreadsPerCore < int(cpu.LogicalProcessorIndex-cpu.CoreIndex) {
cs.MaxThreadsPerCore = int(cpu.LogicalProcessorIndex - cpu.CoreIndex)
}
}

if !cs.EfficiencyClass && lastEfficiencyClass != cpu.EfficiencyClass {
cs.EfficiencyClass = true
}

if !cs.LastLevelCache && lastLevelCache != cpu.LastLevelCacheIndex {
cs.LastLevelCache = true
}

if !cs.NumaNode && lastNumaNodeIndex != cpu.NumaNodeIndex {
cs.NumaNode = true
}

lastEfficiencyClass = cpu.EfficiencyClass
lastLevelCache = cpu.LastLevelCacheIndex
lastNumaNodeIndex = cpu.NumaNodeIndex
}
}
Loading

0 comments on commit a0a80b3

Please sign in to comment.