Files
rpi5-health/internal/processor/win_cpu.go
T
2026-10-05 18:19:36 +02:00

49 lines
1.2 KiB
Go

//go:build windows
package processor
import (
"rpi5-health/internal/windows_system"
"unsafe"
"golang.org/x/sys/windows"
)
type DWORD uint32
type PFILETIME struct {
dwLowDateTime DWORD
dwHighDateTime DWORD
}
func getWinCpuInfo(getSystemTimes *windows.LazyProc) ProcessorInformation {
var lpIdleTime PFILETIME
var lpKernelTime PFILETIME
var lpUserTime PFILETIME
getSystemTimes.Call(
uintptr(unsafe.Pointer(&lpIdleTime)),
uintptr(unsafe.Pointer(&lpKernelTime)),
uintptr(unsafe.Pointer(&lpUserTime)),
)
timeSpentIdlingTicks := fileTimeToTicks(lpIdleTime)
timeSpentByKernelTicks := fileTimeToTicks(lpKernelTime) - timeSpentIdlingTicks // kernel time includes idling by default
timeSpentByUserTicks := fileTimeToTicks(lpUserTime)
return ProcessorInformation{
IdleTimeSinceBoot: timeSpentIdlingTicks,
KernelTimeSinceBoot: timeSpentByKernelTicks,
UserTimeSinceBoot: timeSpentByUserTicks,
}
}
func fileTimeToTicks(filetime PFILETIME) uint64 {
// 100 nanosecond intervals since 1601, january 1
return (uint64(filetime.dwHighDateTime) << 32) | uint64(filetime.dwLowDateTime)
}
func GetCPUInformation() ProcessorInformation {
return getWinCpuInfo(windows_system.GetWindowsTools().SystemTimes)
}