1
0
Fork 0

fan control 작성중

브로드케스터 작성
This commit is contained in:
Sangbum Kim 2017-09-10 19:45:30 +09:00
parent f681f3dd60
commit e820b88e0f
3 changed files with 105 additions and 53 deletions

25
main.go
View File

@ -41,7 +41,31 @@ func setMaxProcs() {
}
}
func broadcastTempeture(sender <-chan float64, handler util.Handler, receivers ...<-chan float64) {
defer handler.DecreaseWait()
defer func() {
if err := recover(); err != nil {
handler.NotifyError(err.(error))
}
}()
for {
select {
case tempeture := <-sender:
for _, receiver := range receivers {
select {
case receiver <- tempeture:
default:
log.Warn("Some Tempeture consumer blocked!")
}
}
case <-handler.Done():
return
}
}
}
func main() {
var (
processorCount = processor.GetProcessorCount()
processors []processor.Processor
@ -64,7 +88,6 @@ func main() {
handler.NotifyError(err)
} else {
processors = append(processors, info)
handler.IncreaseWait()
go info.StartMonitoring()
}
}

View File

@ -21,14 +21,26 @@ type processor struct {
id int
tempeturePath string
tempeture float64
tempetureChanged chan float64
tempetureChanged chan TempetureInfo
sampleDuration time.Duration
fanController pid.Controller
fanSpeed int
fanMaxSpeed int
fanMinSpeed int
fanSpeedChanged chan int
fanSpeedChanged chan FanspeedInfo
}
type TempetureInfo struct {
Id int
Tempeture float64
}
type FanspeedInfo struct {
Id int
FanSpeed int
}
type TempetureChanged = <-chan TempetureInfo
type FanspeedChanged = <-chan FanspeedInfo
type Processor interface {
Id() int
Tempeture() float64
@ -36,8 +48,8 @@ type Processor interface {
FanMaxSpeed() int
FanMinSpeed() int
StartMonitoring()
TempetureNotifier() <-chan float64
FanSpeedNotifier() <-chan int
TempetureNotifier() TempetureChanged
FanSpeedNotifier() FanspeedChanged
getMaxTempetureOnProcessor(string) (float64)
readTempeture(string, chan<- float64, *sync.WaitGroup)
normalizeFanspeed(float64) (int)
@ -89,10 +101,10 @@ func NewProcessorInfo(
handler: handler,
id: processorId,
tempeturePath: matches[0],
tempetureChanged: make(chan float64),
tempetureChanged: make(chan TempetureInfo),
sampleDuration: sampleDuration,
fanController: controller,
fanSpeedChanged: make(chan int),
fanSpeedChanged: make(chan FanspeedInfo),
fanMaxSpeed: maxNoob,
fanMinSpeed: minNoob,
}, nil
@ -103,10 +115,11 @@ func (p *processor) Tempeture() float64 { return p.tempeture }
func (p *processor) FanSpeed() int { return p.fanSpeed }
func (p *processor) FanMaxSpeed() int { return p.fanMaxSpeed }
func (p *processor) FanMinSpeed() int { return p.fanMinSpeed }
func (p *processor) TempetureNotifier() <-chan float64 { return p.tempetureChanged }
func (p *processor) FanSpeedNotifier() <-chan int { return p.fanSpeedChanged }
func (p *processor) TempetureNotifier() TempetureChanged { return p.tempetureChanged }
func (p *processor) FanSpeedNotifier() FanspeedChanged { return p.fanSpeedChanged }
func (p *processor) StartMonitoring() {
p.handler.IncreaseWait()
defer p.handler.DecreaseWait()
defer func() {
if err := recover(); err != nil {
@ -131,13 +144,19 @@ func (p *processor) StartMonitoring() {
highestTemp = p.getMaxTempetureOnProcessor(tempeturePathGlob)
if highestTemp != p.tempeture {
p.tempetureChanged <- highestTemp
p.tempetureChanged <- TempetureInfo{
Id: p.id,
Tempeture: highestTemp,
}
p.tempeture = highestTemp
}
fanspeed = p.normalizeFanspeed(p.fanController.Update(highestTemp))
if fanspeed != p.fanSpeed {
p.fanSpeedChanged <- fanspeed
p.fanSpeedChanged <- FanspeedInfo{
Id:p.id,
FanSpeed:fanspeed,
}
p.fanSpeed = fanspeed
}

View File

@ -1,39 +1,49 @@
package speed_ctrl
package processor
import (
"errors"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"amuz.es/src/infra/cpu_ctrl/daemon"
"amuz.es/src/infra/cpu_ctrl/logger"
"amuz.es/src/infra/cpu_ctrl/processor"
"fmt"
"os/exec"
"amuz.es/src/infra/cpu_ctrl/pid"
"amuz.es/src/infra/cpu_ctrl/util"
"time"
"golang.org/x/tools/go/gcimporter15/testdata"
)
var (
log = logger.NewLogger("speed_ctrl")
)
type fanControl struct {
processorCount int
handler util.Handler
fanSpeedConsumer chan FanspeedInfo
}
type FanControl interface {
}
func NewFanControl(processorCount int, handler util.Handler) FanControl {
return &fanControl{
processorCount: processorCount,
handler: handler,
fanSpeedConsumer: make(chan ),
}
}
func (c *fanControl) StartControl() {
c.handler.IncreaseWait()
defer c.handler.DecreaseWait()
defer func() {
if err := recover(); err != nil {
c.handler.NotifyError(err.(error))
}
}()
defer close(c.fanSpeedConsumer)
ticker := time.Tick(p.sampleDuration)
}
func TempetureControl(
handler util.Handler,
processors []processor.Processor,
) {
defer handler.DecreaseWait()
var (
P, I, D = 1.5, 0.4, 2.0
SetPoint = 38.0
SampleTime = time.Second
maxNoob, minNoob = 0x64, 0x4
WindupGuard = float64(maxNoob - minNoob)
)
noobs := make([]int, processorCount)