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-07 01:26:04 +09:00
|
|
|
|
2017-09-07 01:21:33 +09:00
|
|
|
"amuz.es/src/infra/cpu_ctrl/pid"
|
2017-09-07 01:26:04 +09:00
|
|
|
|
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-07 01:26:04 +09:00
|
|
|
func CpuTempetureMonitoring(info *Processor, sampleDuration time.Duration, 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")
|
2017-09-07 01:21:33 +09:00
|
|
|
ticker := time.Tick(sampleDuration)
|
2017-09-05 01:13:03 +09:00
|
|
|
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()
|
2017-09-07 01:21:33 +09:00
|
|
|
var (
|
2017-09-07 02:02:48 +09:00
|
|
|
P, I, D = 1.5, 0.4, 2.0
|
|
|
|
SetPoint = 37.0
|
2017-09-07 01:26:04 +09:00
|
|
|
SampleTime = time.Second
|
|
|
|
maxNoob, minNoob = 0x64, 0x4
|
2017-09-07 01:49:56 +09:00
|
|
|
WindupGuard = float64(maxNoob - minNoob)
|
2017-09-07 01:21:33 +09:00
|
|
|
)
|
2017-09-07 01:26:04 +09:00
|
|
|
noobs := make([]int, processorCount)
|
|
|
|
controllers := make([]pid.Controller, 0, processorCount)
|
|
|
|
for i := 0; i < processorCount; i++ {
|
|
|
|
controller := pid.New(P, I, D)
|
2017-09-07 01:21:33 +09:00
|
|
|
controller.SetSetPoint(SetPoint)
|
|
|
|
controller.SetSampleTime(SampleTime)
|
|
|
|
controller.SetWindupGuard(WindupGuard)
|
2017-09-07 01:27:40 +09:00
|
|
|
controllers = append(controllers, controller)
|
2017-09-07 01:21:33 +09:00
|
|
|
}
|
2017-09-07 01:26:04 +09:00
|
|
|
|
2017-09-05 01:13:03 +09:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case change := <-notifier:
|
2017-09-07 01:26:04 +09:00
|
|
|
controller := controllers[change.Id]
|
2017-09-07 01:49:56 +09:00
|
|
|
adj_noob := int(-controller.Update(change.Tempeture))
|
|
|
|
if adj_noob < minNoob {
|
|
|
|
adj_noob = minNoob
|
|
|
|
} else if adj_noob > maxNoob {
|
2017-09-07 01:26:04 +09:00
|
|
|
adj_noob = maxNoob
|
2017-09-05 01:13:03 +09:00
|
|
|
}
|
2017-09-07 01:26:04 +09:00
|
|
|
if noobs[change.Id] == adj_noob {
|
2017-09-05 01:13:03 +09:00
|
|
|
continue
|
|
|
|
}
|
2017-09-07 01:26:04 +09:00
|
|
|
noobs[change.Id] = adj_noob
|
2017-09-07 01:21:33 +09:00
|
|
|
fmt.Printf("cpu %d fan 0x%x\n", change.Id, adj_noob)
|
2017-09-05 01:13:03 +09:00
|
|
|
|
|
|
|
args := make([]string, 0)
|
|
|
|
args = append(args,
|
|
|
|
"raw",
|
|
|
|
"0x3a", "0x01",
|
|
|
|
)
|
2017-09-07 01:21:33 +09:00
|
|
|
for _, item := range noobs {
|
2017-09-05 01:13:03 +09:00
|
|
|
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-07 01:21:33 +09:00
|
|
|
processorCount = 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-07 01:26:04 +09:00
|
|
|
sampleDuration = time.Second
|
2017-09-05 01:13:03 +09:00
|
|
|
)
|
|
|
|
|
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)
|
2017-09-07 01:26:04 +09:00
|
|
|
go CpuTempetureMonitoring(info, sampleDuration, tempetureChange, errorChan, ctx, waiter)
|
2017-09-06 07:53:17 +09:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|