reformat
This commit is contained in:
parent
b3197b61e9
commit
ec0c7112b1
62
main.go
62
main.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
@ -30,7 +31,6 @@ var (
|
|||
suffix = [...]int{auto, auto, auto, auto, auto, auto}
|
||||
)
|
||||
|
||||
|
||||
type notifyType string
|
||||
|
||||
const (
|
||||
|
@ -38,7 +38,6 @@ const (
|
|||
DaemonStopping = notifyType("STOPPING=1")
|
||||
)
|
||||
|
||||
|
||||
func NotifyDaemon(status notifyType) {
|
||||
daemon.SdNotify(false, string(status))
|
||||
}
|
||||
|
@ -70,34 +69,30 @@ type TempetureChange struct {
|
|||
Tempeture float64
|
||||
}
|
||||
|
||||
func getProcessorInfo(processorId int) *Processor {
|
||||
matches, err := filepath.Glob(fmt.Sprintf("/sys/devices/platform/coretemp.%d/hwmon/hwmon?", processorId))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
func getProcessorInfo(processorId int) (*Processor, error) {
|
||||
if matches, err := filepath.Glob(fmt.Sprintf("/sys/devices/platform/coretemp.%d/hwmon/hwmon?", processorId)); err != nil {
|
||||
return nil, err
|
||||
} else if matches == nil {
|
||||
return nil, errors.New("hwmon not found!")
|
||||
} else {
|
||||
return &Processor{
|
||||
Id: processorId,
|
||||
TempeturePath: matches[0],
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
func ReadTempeture(path string, senseChan chan<- float64, waiter *sync.WaitGroup) {
|
||||
func ReadTempeture(path string, senseChan chan<- float64, errorChan chan<- error, waiter *sync.WaitGroup) {
|
||||
defer waiter.Done()
|
||||
dat, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
senseChan <- 0.0
|
||||
return
|
||||
}
|
||||
tempetureSense, err := strconv.Atoi(strings.TrimSpace(string(dat)))
|
||||
if err != nil {
|
||||
senseChan <- 0.0
|
||||
return
|
||||
}
|
||||
if dat, err := ioutil.ReadFile(path); err != nil {
|
||||
errorChan <- err
|
||||
} else if tempetureSense, err := strconv.Atoi(strings.TrimSpace(string(dat))); err != nil {
|
||||
errorChan <- err
|
||||
} else {
|
||||
senseChan <- float64(tempetureSense) / 1000.0
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func CpuTempetureMonitoring(info *Processor, notifier chan<- TempetureChange, ctx context.Context, waiter *sync.WaitGroup) {
|
||||
waiter.Add(1)
|
||||
func CpuTempetureMonitoring(info *Processor, notifier chan<- TempetureChange, errorChan chan<- error, ctx context.Context, waiter *sync.WaitGroup) {
|
||||
defer waiter.Done()
|
||||
tempeturePathGlob := path.Join(info.TempeturePath, "temp?_input")
|
||||
ticker := time.Tick(3 * time.Second)
|
||||
|
@ -113,7 +108,7 @@ func CpuTempetureMonitoring(info *Processor, notifier chan<- TempetureChange, ct
|
|||
// exclude package temp
|
||||
for _, path := range matches[1:] {
|
||||
tempetureReadWaiter.Add(1)
|
||||
go ReadTempeture(path, queue, tempetureReadWaiter)
|
||||
go ReadTempeture(path, queue, errorChan, tempetureReadWaiter)
|
||||
}
|
||||
tempetureReadWaiter.Wait()
|
||||
close(queue)
|
||||
|
@ -133,7 +128,6 @@ func CpuTempetureMonitoring(info *Processor, notifier chan<- TempetureChange, ct
|
|||
}
|
||||
}
|
||||
func CpuTempetureScraper(processorCount int, notifier <-chan TempetureChange, errorChan chan<- error, ctx context.Context, waiter *sync.WaitGroup) {
|
||||
waiter.Add(1)
|
||||
defer waiter.Done()
|
||||
pastFan := make([]int, processorCount)
|
||||
for {
|
||||
|
@ -178,7 +172,7 @@ func CpuTempetureScraper(processorCount int, notifier <-chan TempetureChange,err
|
|||
|
||||
func main() {
|
||||
var (
|
||||
processorCount = getProcessorCount()
|
||||
processorCount = 0 //getProcessorCount()
|
||||
ctx, canceled = context.WithCancel(context.Background())
|
||||
waiter = &sync.WaitGroup{}
|
||||
exitSignal = make(chan os.Signal, 1)
|
||||
|
@ -186,10 +180,18 @@ func main() {
|
|||
tempetureChange = make(chan TempetureChange)
|
||||
)
|
||||
|
||||
for i := 0; i < processorCount; i++ {
|
||||
info := getProcessorInfo(i)
|
||||
go CpuTempetureMonitoring(info, tempetureChange, ctx, waiter)
|
||||
if processorCount == 0 {
|
||||
errorChan <- errors.New("cpu not found!")
|
||||
}
|
||||
for i := 0; i < processorCount; i++ {
|
||||
if info, err := getProcessorInfo(i); err != nil {
|
||||
errorChan <- err
|
||||
} else {
|
||||
waiter.Add(1)
|
||||
go CpuTempetureMonitoring(info, tempetureChange, errorChan, ctx, waiter)
|
||||
}
|
||||
}
|
||||
waiter.Add(1)
|
||||
go CpuTempetureScraper(processorCount, tempetureChange, errorChan, ctx, waiter)
|
||||
defer waiter.Wait()
|
||||
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
||||
|
@ -199,13 +201,11 @@ func main() {
|
|||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println("Service request to close this application")
|
||||
return
|
||||
case err := <-errorChan:
|
||||
canceled()
|
||||
fmt.Printf("error! %s", err.Error())
|
||||
fmt.Printf("error! %s\n", err.Error())
|
||||
case sysSignal := <-exitSignal:
|
||||
canceled()
|
||||
fmt.Printf("SYSCALL! %s", sysSignal.String())
|
||||
fmt.Printf("SYSCALL! %s\n", sysSignal.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue