initial proof of concept
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package cpu
|
||||
|
||||
import (
|
||||
"finally-a-monolithic-linux-hw-monitor/app/cpu/proc"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func RenderCPU(console *strings.Builder) {
|
||||
proc, err := proc.GetProc()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Move cursor to top-left (0,0) without clearing full buffer (flicker-free)
|
||||
console.WriteString("\033[H")
|
||||
console.WriteString(fmt.Sprintf("=== %s ===\n", proc.Model()))
|
||||
|
||||
RenderTemperatures(console, proc)
|
||||
}
|
||||
|
||||
func RenderTemperatures(console *strings.Builder, proc proc.Proc) {
|
||||
temps, _ := proc.SensorReader.ReadTemperatureSensors()
|
||||
for _, sensor := range temps {
|
||||
tempC := sensor.TempC
|
||||
tempF := (tempC * 9 / 5) + 32
|
||||
// \033[K clears from cursor to end of line (prevents lingering chars)
|
||||
console.WriteString(fmt.Sprintf("Sensor: %-25s | %5.1f°C (%5.1f°F)\033[K\n", sensor.Name, tempC, tempF))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package proc
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Proc struct {
|
||||
Info map[string]string
|
||||
SensorReader CPUSensorReader
|
||||
}
|
||||
|
||||
type CPUSensorReader interface {
|
||||
ReadTemperatureSensors() ([]TempSensor, error)
|
||||
}
|
||||
|
||||
type TempSensor struct {
|
||||
Name string
|
||||
TempC float64
|
||||
}
|
||||
|
||||
const (
|
||||
VendorIntel string = "GenuineIntel"
|
||||
VendorAMD string = "AuthenticAMD"
|
||||
VendorUnknown string = "Unknown"
|
||||
)
|
||||
|
||||
func GetProc() (Proc, error) {
|
||||
// open and parse cpuinfo
|
||||
proc := Proc{}
|
||||
procFile, err := os.Open("/proc/cpuinfo")
|
||||
if err != nil {
|
||||
return proc, err
|
||||
}
|
||||
defer procFile.Close()
|
||||
|
||||
proc.Info = map[string]string{}
|
||||
|
||||
scanner := bufio.NewScanner(procFile)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
line = strings.ReplaceAll(line, "\t", "")
|
||||
if line != "" {
|
||||
x := strings.Split(line, ": ")
|
||||
if len(x) != 2 {
|
||||
continue
|
||||
}
|
||||
key := x[0]
|
||||
value := x[1]
|
||||
proc.Info[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
return proc, proc.getSensorReader()
|
||||
}
|
||||
|
||||
func (proc *Proc) getSensorReader() error {
|
||||
// get vendor string and initialize sensor reader
|
||||
|
||||
vendor := proc.Vendor()
|
||||
|
||||
if vendor == VendorIntel {
|
||||
intelReader, err := NewIntelReader()
|
||||
proc.SensorReader = intelReader
|
||||
return err
|
||||
}
|
||||
|
||||
if vendor == VendorAMD {
|
||||
amdReader, err := NewAMDReader()
|
||||
proc.SensorReader = amdReader
|
||||
return err
|
||||
}
|
||||
|
||||
return fmt.Errorf("proc type %s is not supported", vendor)
|
||||
}
|
||||
|
||||
func (proc *Proc) Vendor() string {
|
||||
return proc.Info["vendor_id"]
|
||||
}
|
||||
|
||||
func (proc *Proc) Model() string {
|
||||
return proc.Info["model name"]
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package proc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type IntelReader struct {
|
||||
hwmonDir string
|
||||
driver string
|
||||
}
|
||||
|
||||
func NewIntelReader() (*IntelReader, error) {
|
||||
// Locate Intel coretemp hwmon path
|
||||
dirs, err := filepath.Glob("/sys/class/hwmon/hwmon*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hwmonDir := ""
|
||||
for _, d := range dirs {
|
||||
nameData, err := os.ReadFile(filepath.Join(d, "name"))
|
||||
if err == nil && strings.TrimSpace(string(nameData)) == "coretemp" {
|
||||
hwmonDir = d
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if hwmonDir == "" {
|
||||
return nil, fmt.Errorf("coretemp driver not found under /sys/class/hwmon/")
|
||||
}
|
||||
|
||||
return &IntelReader{
|
||||
hwmonDir: hwmonDir,
|
||||
driver: "coretemp",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *IntelReader) 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
|
||||
}
|
||||
Reference in New Issue
Block a user