Files
finally-a-monolithic-linux-…/app/cpu/proc/vendor_amd.go
T
2026-08-04 20:12:47 +00:00

81 lines
1.6 KiB
Go

package proc
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type AMDReader struct {
hwmonDir string
driver string
}
type PowerState struct {
lastEnergy uint64
lastTime time.Time
}
func NewAMDReader() (*AMDReader, error) {
// Locate AMD k10temp or zenpower hwmon path
matches, err := filepath.Glob("/sys/class/hwmon/hwmon*")
if err != nil {
return nil, err
}
for _, dir := range matches {
b, err := os.ReadFile(filepath.Join(dir, "name"))
if err != nil {
continue
}
drv := strings.TrimSpace(string(b))
if drv == "k10temp" || drv == "zenpower" {
return &AMDReader{
hwmonDir: dir,
driver: drv,
}, nil
}
}
return nil, fmt.Errorf("k10temp/zenpower driver not found under /sys/class/hwmon/")
}
func (r *AMDReader) ReadTemperatureSensors() ([]TempSensor, error) {
inputs, err := filepath.Glob(filepath.Join(r.hwmonDir, "temp*_input"))
if err != nil || len(inputs) == 0 {
return nil, fmt.Errorf("no temperature inputs found in %s", r.hwmonDir)
}
temps := []TempSensor{}
for _, inputPath := range inputs {
valBytes, err := os.ReadFile(inputPath)
if err != nil {
continue
}
milliC, err := strconv.ParseFloat(strings.TrimSpace(string(valBytes)), 64)
if err != nil {
continue
}
labelPath := strings.TrimSuffix(inputPath, "_input") + "_label"
label := ""
if lBytes, err := os.ReadFile(labelPath); err == nil {
label = strings.TrimSpace(string(lBytes))
} else {
label = filepath.Base(strings.TrimSuffix(inputPath, "_input"))
}
temps = append(temps, TempSensor{
Name: label,
TempC: milliC / 1000.0,
})
}
return temps, nil
}