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() { func main() {
var ( var (
processorCount = processor.GetProcessorCount() processorCount = processor.GetProcessorCount()
processors []processor.Processor processors []processor.Processor
@ -64,7 +88,6 @@ func main() {
handler.NotifyError(err) handler.NotifyError(err)
} else { } else {
processors = append(processors, info) processors = append(processors, info)
handler.IncreaseWait()
go info.StartMonitoring() go info.StartMonitoring()
} }
} }

View File

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

View File

@ -1,39 +1,49 @@
package speed_ctrl package processor
import ( 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" "fmt"
"os/exec" "os/exec"
"amuz.es/src/infra/cpu_ctrl/pid" "amuz.es/src/infra/cpu_ctrl/pid"
"amuz.es/src/infra/cpu_ctrl/util" "amuz.es/src/infra/cpu_ctrl/util"
"time"
"golang.org/x/tools/go/gcimporter15/testdata"
) )
var ( type fanControl struct {
log = logger.NewLogger("speed_ctrl") 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( func TempetureControl(
handler util.Handler, handler util.Handler,
processors []processor.Processor, processors []processor.Processor,
) { ) {
defer handler.DecreaseWait() 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) noobs := make([]int, processorCount)