1
0
Fork 0
cpu_ctrl/main.go

212 lines
4.7 KiB
Go
Raw Normal View History

2017-09-05 01:13:03 +09:00
package main
import (
"context"
2017-09-06 07:53:17 +09:00
"errors"
2017-09-05 01:13:03 +09:00
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
2017-09-06 07:53:17 +09:00
"github.com/coreos/go-systemd/daemon"
2017-09-05 01:13:03 +09:00
"github.com/shirou/gopsutil/cpu"
)
const (
auto = 0x0
min = 0x04
max = 0x64
)
var (
prefix = [...]int{0x3a, 0x01}
suffix = [...]int{auto, auto, auto, auto, auto, auto}
)
2017-09-05 01:32:58 +09:00
type notifyType string
2017-09-05 01:13:03 +09:00
const (
2017-09-06 07:53:17 +09:00
DaemonStarted = notifyType("READY=1")
DaemonStopping = notifyType("STOPPING=1")
2017-09-05 01:13:03 +09:00
)
func NotifyDaemon(status notifyType) {
daemon.SdNotify(false, string(status))
}
func getProcessorCount() int {
var maxcpu int
stat, err := cpu.Info()
if err != nil {
panic(err)
}
for _, info := range stat {
physicalId, err := strconv.Atoi(info.PhysicalID)
if err != nil {
panic(err)
} else if maxcpu < physicalId {
maxcpu = physicalId
}
}
return maxcpu + 1
}
type Processor struct {
Id int
TempeturePath string
}
type TempetureChange struct {
Id int
Tempeture float64
}
2017-09-06 07:53:17 +09:00
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
2017-09-05 01:13:03 +09:00
}
}
2017-09-06 07:53:17 +09:00
func ReadTempeture(path string, senseChan chan<- float64, errorChan chan<- error, waiter *sync.WaitGroup) {
2017-09-05 01:13:03 +09:00
defer waiter.Done()
2017-09-06 07:53:17 +09:00
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
2017-09-05 01:13:03 +09:00
}
}
2017-09-06 07:53:17 +09:00
func CpuTempetureMonitoring(info *Processor, notifier chan<- TempetureChange, errorChan chan<- error, ctx context.Context, waiter *sync.WaitGroup) {
2017-09-05 01:13:03 +09:00
defer waiter.Done()
tempeturePathGlob := path.Join(info.TempeturePath, "temp?_input")
ticker := time.Tick(3 * time.Second)
for {
select {
case <-ticker:
matches, err := filepath.Glob(tempeturePathGlob)
if err != nil {
panic(err)
}
tempetureReadWaiter := &sync.WaitGroup{}
queue := make(chan float64, len(matches))
// exclude package temp
for _, path := range matches[1:] {
tempetureReadWaiter.Add(1)
2017-09-06 07:53:17 +09:00
go ReadTempeture(path, queue, errorChan, tempetureReadWaiter)
2017-09-05 01:13:03 +09:00
}
tempetureReadWaiter.Wait()
close(queue)
var tempeture float64
for sense := range queue {
if tempeture < sense {
tempeture = sense
}
}
notifier <- TempetureChange{
Id: info.Id,
Tempeture: tempeture,
}
case <-ctx.Done():
return
}
}
}
2017-09-06 07:53:17 +09:00
func CpuTempetureScraper(processorCount int, notifier <-chan TempetureChange, errorChan chan<- error, ctx context.Context, waiter *sync.WaitGroup) {
2017-09-05 01:13:03 +09:00
defer waiter.Done()
pastFan := make([]int, processorCount)
for {
select {
case change := <-notifier:
delta := int(change.Tempeture) - 33
fan := 0x4 + (delta - delta%0x4)
if fan < 0x4 {
fan = 0x4
} else if fan > 0x50 {
fan = 0x0
}
if pastFan[change.Id] != fan {
pastFan[change.Id] = fan
} else {
continue
}
fmt.Printf("cpu %d fan 0x%x\n", change.Id, fan)
args := make([]string, 0)
args = append(args,
"raw",
"0x3a", "0x01",
)
for _, item := range pastFan {
args = append(args, fmt.Sprintf("0x%x", item))
}
args = append(args,
"0x0", "0x0", "0x0", "0x0", "0x0", "0x0",
)
cmd := exec.Command("ipmitool", args...)
if err := cmd.Run(); err != nil {
2017-09-05 22:34:17 +09:00
errorChan <- err
return
2017-09-05 01:13:03 +09:00
}
case <-ctx.Done():
return
}
}
}
func main() {
var (
2017-09-06 07:53:17 +09:00
processorCount = 0 //getProcessorCount()
2017-09-05 01:13:03 +09:00
ctx, canceled = context.WithCancel(context.Background())
waiter = &sync.WaitGroup{}
exitSignal = make(chan os.Signal, 1)
2017-09-06 07:53:17 +09:00
errorChan = make(chan error, 1)
2017-09-05 01:13:03 +09:00
tempetureChange = make(chan TempetureChange)
)
2017-09-06 07:53:17 +09:00
if processorCount == 0 {
errorChan <- errors.New("cpu not found!")
}
2017-09-05 01:13:03 +09:00
for i := 0; i < processorCount; i++ {
2017-09-06 07:53:17 +09:00
if info, err := getProcessorInfo(i); err != nil {
errorChan <- err
} else {
waiter.Add(1)
go CpuTempetureMonitoring(info, tempetureChange, errorChan, ctx, waiter)
}
2017-09-05 01:13:03 +09:00
}
2017-09-06 07:53:17 +09:00
waiter.Add(1)
go CpuTempetureScraper(processorCount, tempetureChange, errorChan, ctx, waiter)
2017-09-05 01:13:03 +09:00
defer waiter.Wait()
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
NotifyDaemon(DaemonStarted)
defer NotifyDaemon(DaemonStopping)
select {
case <-ctx.Done():
fmt.Println("Service request to close this application")
2017-09-06 07:53:17 +09:00
case err := <-errorChan:
2017-09-05 22:34:17 +09:00
canceled()
2017-09-06 07:53:17 +09:00
fmt.Printf("error! %s\n", err.Error())
2017-09-05 01:13:03 +09:00
case sysSignal := <-exitSignal:
canceled()
2017-09-06 07:53:17 +09:00
fmt.Printf("SYSCALL! %s\n", sysSignal.String())
2017-09-05 01:13:03 +09:00
}
}