From e820b88e0f684b09110ef0064367c810da8d6123 Mon Sep 17 00:00:00 2001 From: sangbum kim Date: Sun, 10 Sep 2017 19:45:30 +0900 Subject: [PATCH] =?UTF-8?q?fan=20control=20=EC=9E=91=EC=84=B1=EC=A4=91=20?= =?UTF-8?q?=20=EB=B8=8C=EB=A1=9C=EB=93=9C=EC=BC=80=EC=8A=A4=ED=84=B0=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 25 +++++- processor/processor.go | 81 ++++++++++++------- {speed_ctrl => processor}/speed_controller.go | 52 +++++++----- 3 files changed, 105 insertions(+), 53 deletions(-) rename {speed_ctrl => processor}/speed_controller.go (67%) diff --git a/main.go b/main.go index 6cf4351..dc91eff 100644 --- a/main.go +++ b/main.go @@ -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() } } diff --git a/processor/processor.go b/processor/processor.go index e963846..c8a6ea3 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -17,18 +17,30 @@ import ( ) type processor struct { - handler util.Handler - id int - tempeturePath string - tempeture float64 - tempetureChanged chan float64 - sampleDuration time.Duration - fanController pid.Controller - fanSpeed int - fanMaxSpeed int - fanMinSpeed int - fanSpeedChanged chan int + handler util.Handler + id int + tempeturePath string + tempeture float64 + tempetureChanged chan TempetureInfo + sampleDuration time.Duration + fanController pid.Controller + fanSpeed int + fanMaxSpeed int + fanMinSpeed 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) @@ -86,27 +98,28 @@ func NewProcessorInfo( controller.SetWindupGuard(windupGuard) return &processor{ - handler: handler, - id: processorId, - tempeturePath: matches[0], - tempetureChanged: make(chan float64), - sampleDuration: sampleDuration, - fanController: controller, - fanSpeedChanged: make(chan int), - fanMaxSpeed: maxNoob, - fanMinSpeed: minNoob, + handler: handler, + id: processorId, + tempeturePath: matches[0], + tempetureChanged: make(chan TempetureInfo), + sampleDuration: sampleDuration, + fanController: controller, + fanSpeedChanged: make(chan FanspeedInfo), + fanMaxSpeed: maxNoob, + fanMinSpeed: minNoob, }, nil } } -func (p *processor) Id() int { return p.id } -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) Id() int { return p.id } +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() 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 } diff --git a/speed_ctrl/speed_controller.go b/processor/speed_controller.go similarity index 67% rename from speed_ctrl/speed_controller.go rename to processor/speed_controller.go index 3664bd6..b78892c 100644 --- a/speed_ctrl/speed_controller.go +++ b/processor/speed_controller.go @@ -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)