sophuwu.site > quickstat
updates
sophuwu sophie@skisiel.com
Thu, 11 Apr 2024 08:59:20 +0200
commit

30a16be75733f3315208ae3aed8d88a2c3174b6e

parent

8d50746b77be881b981ca446bb3e331f1723c487

3 files changed, 47 insertions(+), 82 deletions(-)

jump to
M cpu.gocpu.go

@@ -1,6 +1,7 @@

package main import ( + "bytes" "encoding/json" "fmt" "os"

@@ -66,44 +67,37 @@ cpu.Temp = (int(b[0])-48)*10 + int(b[1]) - 48

} func (c *CPU) loadMHz() { b, err := os.ReadFile("/proc/cpuinfo") - - if errCheck(err, func() string { - cpuMHz = -1 - return "Unable to read cpu MHz.\n" - }) { + if Is(err) { return } - - var tmp, j int - c.MHz = 0 - for i := 0; i < len(b); i++ { - if string(b[i:i+7]) == "cpu MHz" { - tmp = 0 - for ; i < len(b) && b[i] != 10 && b[i] != '.'; i++ { - if b[i] >= 48 && b[i] <= 57 { - tmp = tmp*10 + int(b[i]) - 48 - } + var ns numSeeker + var Ipart, Fpart, n float64 + for _, v := range bytes.Split(b, []byte("\n")) { + Ipart = 0 + Fpart = 0 + if bytes.HasPrefix(v, []byte("cpu MHz")) { + ns.Init(v) + Ipart = float64(ns.GetNum()) + Fpart = float64(ns.GetNum()) + for Fpart >= 1 { + Fpart /= 10 } - cpuMHz += tmp - j++ + c.MHz += Ipart + Fpart + n++ } } - c.MHz /= j + c.MHz /= n } func (c *CPU) loadUsage() { readStat := func(n *[4]float64) bool { - errFunc := func() string { - cpuLoad = -1 - return "Unable to read cpu load.\n" - } b := make([]byte, 100) f, err := os.Open("/proc/stat") - if errCheck(err, errFunc) { + if Is(err) { return true } _, err = f.Read(b) - if errCheck(err, errFunc) { + if Is(err) { return true } f.Close()

@@ -126,10 +120,11 @@ return

} c.Load = ((b[0] + b[1] + b[2]) - (a[0] + a[1] + a[2])) / ((b[0] + b[1] + b[2] + b[3]) - (a[0] + a[1] + a[2] + a[3])) } -func (c *CPU) update() { +func (c *CPU) update(done chan bool) { go c.loadMHz() go c.loadTemp() c.loadUsage() + done <- true } func (c *CPU) GHz() float64 { return c.MHz / 1000

@@ -146,9 +141,4 @@ b, _ := json.Marshal(c)

return string(b) } return fmt.Sprintf("%.1f %c %.2f GHz %s\n", c.Load*100, '%', c.GHz(), c.TempStr()) -} -func Cpu() CPU { - var cpu CPU - cpu.update() - return cpu }
M main.gomain.go

@@ -64,18 +64,27 @@ func (b Bytes) String() string {

if CONFIG.Unit == 0 { } + return fmt.Sprint(b) } -type NetInfo struct { +type Net struct { Tx Bytes Rx Bytes } type HWInfo struct { CPU CPU - MEM MEMInfo - NET NetInfo + MEM MEM + NET Net } +func (this *HWInfo) update() { + done := make(chan bool) + go this.CPU.update(done) + go this.MEM.update(done) + <-done + <-done + // i.NET.update() +} func (i *HWInfo) String() string { if CONFIG.Json { b, _ := json.Marshal(i)

@@ -85,53 +94,16 @@ return fmt.Sprintf("%s %s %s\n", i.CPU, i.MEM, i.NET)

} -func round(f float64) string { - var n int = int(f*1000) % 1000 - var s string = "" - - if n != 0 { - for i := 0; i < 3; i++ { - if n%10 >= 5 { - n += 10 - } - n /= 10 - } - } - - n = int(f) + n - if n == 0 { - return "0" - } - for n > 0 { - s = string(n%10+48) + s - n /= 10 - } - - return s -} - -func printNWithErr(in interface{}) string { - var n float64 - switch v := in.(type) { - case int: - n = float64(v) - break - case float64: - n = v - break - default: - return "Err" - } - if n < 0 { - return "Err" - } - return round(n) +func Is(err error) bool { + return err != nil } func main() { - go getMem() - go getCPUTemp() - go getMHz() - getCPU() - println(" Mem: ", printNWithErr(prctMem), "% USED ", printNWithErr(prctBuff), "% BUFF ", printNWithErr(prctFree), "% FREE CPU: ", printNWithErr(cpuLoad), "% ", printNWithErr(cpuTemp), "C ", printNWithErr(cpuMHz), "MHz") + var HW HWInfo + HW.update() + fmt.Println(HW) + // go getCPUTemp() + // go getMHz() + // getCPU() + // println(" Mem: ", printNWithErr(prctMem), "% USED ", printNWithErr(prctBuff), "% BUFF ", printNWithErr(prctFree), "% FREE CPU: ", printNWithErr(cpuLoad), "% ", printNWithErr(cpuTemp), "C ", printNWithErr(cpuMHz), "MHz") }
M mem.gomem.go

@@ -1,7 +1,6 @@

package main import ( - "fmt" "os" )

@@ -59,7 +58,7 @@ }

return nums } -func (m *MEM) update() { +func (m *MEM) update(done chan bool) { b := make([]byte, 140) f, _ := os.Open("/proc/meminfo")

@@ -70,5 +69,9 @@ }

f.Close() var seeker numSeeker seeker.Init(b) - fmt.Println(seeker.GetNums()) + m.Total = Bytes(seeker.GetNum()) + m.Free = Bytes(seeker.GetNum()) + m.Avail = Bytes(seeker.GetNum()) + m.Buffer = Bytes(seeker.GetNum() + seeker.GetNum()) + done <- true }