1
0
Fork 0
This commit is contained in:
Sangbum Kim 2017-09-06 07:53:17 +09:00
parent b3197b61e9
commit ec0c7112b1
1 changed files with 42 additions and 42 deletions

62
main.go
View File

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