Testing for linux cpuinfo

This commit is contained in:
lin committed 2026-10-05 18:19:36 +02:00
1 parent cbc9e4a0b4
commit ea5e79a46a
8 files changed
+172 -20

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
package processor
import "time"
type ProcessorInformation struct {
IdleTimeSinceBoot uint64
KernelTimeSinceBoot uint64
UserTimeSinceBoot uint64
}
func (procInfo ProcessorInformation) Usage() (
TotalCPUUsage float64,
KernelCPUUsage float64,
UserCPUUsage float64,
IdleCPUUsage float64,
) {
first := GetCPUInformation()
time.Sleep(time.Second)
second := GetCPUInformation()
deltaIdleTime := second.IdleTimeSinceBoot - first.IdleTimeSinceBoot
deltaKernelTime := second.KernelTimeSinceBoot - first.KernelTimeSinceBoot
deltaUserTime := second.UserTimeSinceBoot - first.UserTimeSinceBoot
deltaTotalTime := deltaIdleTime + deltaKernelTime + deltaUserTime
if deltaTotalTime > 0 {
TotalCPUUsage = ((float64(deltaKernelTime) + float64(deltaUserTime)) / float64(deltaTotalTime)) * 100
UserCPUUsage = (float64(deltaUserTime) / float64(deltaTotalTime)) * 100
KernelCPUUsage = (float64(deltaKernelTime) / float64(deltaTotalTime)) * 100
IdleCPUUsage = (float64(deltaIdleTime) / float64(deltaTotalTime)) * 100
}
return TotalCPUUsage, KernelCPUUsage, UserCPUUsage, IdleCPUUsage
}
+50
View File
@@ -0,0 +1,50 @@
//go:build linux
package processor
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strings"
)
var numberRegex *regexp.Regexp = regexp.MustCompile(`\d+`)
func getLinuxCpuInfo() (ProcessorInformation, error) {
procFile, err := os.Open("/proc/meminfo")
if err != nil {
return ProcessorInformation{}, fmt.Errorf("Could not find /proc/meminfo. Are you sure you are on linux?")
}
defer procFile.Close()
fileReader := bufio.NewScanner(procFile)
//var idleTime uint64
//var kernelTime uint64
//var userTime uint64
for fileReader.Scan() {
line := fileReader.Text()
if strings.HasPrefix(line, "cpu ") {
fields := strings.Split(line, " ")
for _, field := range fields {
fmt.Print(field)
}
}
}
return ProcessorInformation{}, nil
}
func GetCPUInformation() ProcessorInformation {
info, err := getLinuxCpuInfo()
if err != nil {
log.Printf("Could not read processor info. Error: %v", err)
return ProcessorInformation{}
}
return info
}
+48
View File
@@ -0,0 +1,48 @@
//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)
}