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

82
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -15,7 +16,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/coreos/go-systemd/daemon" "github.com/coreos/go-systemd/daemon"
"github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
) )
@ -30,15 +31,13 @@ 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 (
DaemonStarted =notifyType("READY=1") DaemonStarted = notifyType("READY=1")
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!")
return &Processor{ } else {
Id: processorId, return &Processor{
TempeturePath: matches[0], 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() 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 {
senseChan <- float64(tempetureSense) / 1000.0
} }
tempetureSense, err := strconv.Atoi(strings.TrimSpace(string(dat)))
if err != nil {
senseChan <- 0.0
return
}
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)
@ -132,8 +127,7 @@ 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,19 +172,27 @@ 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)
errorChan = make(chan error, 1) errorChan = make(chan error, 1)
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)
} }
go CpuTempetureScraper(processorCount, tempetureChange,errorChan, 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)
defer waiter.Wait() defer waiter.Wait()
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
NotifyDaemon(DaemonStarted) NotifyDaemon(DaemonStarted)
@ -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())
} }
} }