1
0
Fork 0

밀리지 않도록 로직 조정

This commit is contained in:
Sangbum Kim 2017-09-13 00:33:34 +09:00
parent 1d55617d68
commit 8e9ffe4808
3 changed files with 85 additions and 69 deletions

View File

@ -81,38 +81,55 @@ func (m *influxMetric) StartLogging() {
panic(err) panic(err)
} }
ticker := time.Tick(time.Second) ticker := time.Tick(time.Second)
pointList := make([]*client.Point, 0, 0) fanspeedList, tempetureList := make([]processor.FanspeedInfo, 0, 0), make([]processor.TempetureInfo, 0, 0)
for {
pointList = pointList[:cap(pointList)]
checker:
for { for {
select { select {
case <-ticker: case <-ticker:
break checker fanspeedListTemp, tempetureListTemp := fanspeedList, tempetureList
fanspeedList, tempetureList = make([]processor.FanspeedInfo, 0, 0), make([]processor.TempetureInfo, 0, 0)
go m.sendPoint(influxDbConn, batchPoint, fanspeedListTemp, tempetureListTemp)
case changedSpeed := <-m.fanSpeedConsumer: case changedSpeed := <-m.fanSpeedConsumer:
if point, err := m.getFanspeedPoint(changedSpeed); err == nil { fanspeedList = append(fanspeedList, changedSpeed)
pointList = append(pointList, point)
} else {
influxLogger.Debugf("id %d speed err %s", changedSpeed.Id, err)
}
case changedTempeture := <-m.tempetureConsumer: case changedTempeture := <-m.tempetureConsumer:
if point, err := m.getTempeturePoint(changedTempeture); err == nil { tempetureList = append(tempetureList, changedTempeture)
pointList = append(pointList, point)
} else {
influxLogger.Debugf("id %d tempeture err %s", changedTempeture.Id, err)
}
case <-m.handler.Done(): case <-m.handler.Done():
return return
} }
} }
batchPoint.AddPoints(pointList) }
func (m *influxMetric) sendPoint(
influxDbConn client.Client,
batchPoint client.BatchPoints,
fanspeeds []processor.FanspeedInfo, tempetures []processor.TempetureInfo) {
pointList := make([]*client.Point, 0, 0)
for _, fanspeed := range fanspeeds {
if point, err := m.getFanspeedPoint(fanspeed); err == nil {
pointList = append(pointList, point)
} else {
influxLogger.Debugf("id %d speed err %s", fanspeed.Id, err)
}
}
for _, tempeture := range tempetures {
if point, err := m.getTempeturePoint(tempeture); err == nil {
pointList = append(pointList, point)
} else {
influxLogger.Debugf("id %d tempeture err %s", tempeture.Id, err)
}
}
if len(pointList) == 0 {
return
}
batchPoint.AddPoints(pointList)
if err := influxDbConn.Write(batchPoint); err != nil { if err := influxDbConn.Write(batchPoint); err != nil {
influxLogger.Warn(err) influxLogger.Warn(err)
} }
pointList = make([]*client.Point, 0, 0)
}
} }
func (m *influxMetric) getTempeturePoint(info processor.TempetureInfo) (*client.Point, error) { func (m *influxMetric) getTempeturePoint(info processor.TempetureInfo) (*client.Point, error) {
@ -123,7 +140,7 @@ func (m *influxMetric) getTempeturePoint(info processor.TempetureInfo) (*client.
"tempeture": info.Tempeture, "tempeture": info.Tempeture,
} }
return client.NewPoint("processor_tempeture", tags, fields, time.Now()) return client.NewPoint("processor_tempeture", tags, fields, info.At)
} }
func (m *influxMetric) getFanspeedPoint(info processor.FanspeedInfo) (*client.Point, error) { func (m *influxMetric) getFanspeedPoint(info processor.FanspeedInfo) (*client.Point, error) {
@ -134,5 +151,5 @@ func (m *influxMetric) getFanspeedPoint(info processor.FanspeedInfo) (*client.Po
"noob": info.FanSpeed, "noob": info.FanSpeed,
} }
return client.NewPoint("processor_cooling_fanspeed", tags, fields, time.Now()) return client.NewPoint("processor_cooling_fanspeed", tags, fields, info.At)
} }

View File

@ -49,24 +49,24 @@ func (c *fanControl) StartControl() {
fanspeedLogger.Info("Fan control started") fanspeedLogger.Info("Fan control started")
ticker := time.Tick(c.sampleDuration) ticker := time.Tick(c.sampleDuration)
pastFanSpeedList := make([]int, c.processorCount) pastFanSpeedList, newFanSpeedList := make([]int, c.processorCount), make([]int, c.processorCount)
newFanSpeedList := make([]int, c.processorCount)
for {
checker:
for { for {
select { select {
case <-ticker: case <-ticker:
break checker go c.applyFanspeed(pastFanSpeedList, newFanSpeedList)
pastFanSpeedList, newFanSpeedList = newFanSpeedList, make([]int, c.processorCount)
case changedSpeed := <-c.fanSpeedConsumer: case changedSpeed := <-c.fanSpeedConsumer:
if changedSpeed.Changed {
newFanSpeedList[changedSpeed.Id] = changedSpeed.FanSpeed newFanSpeedList[changedSpeed.Id] = changedSpeed.FanSpeed
}
case <-c.handler.Done(): case <-c.handler.Done():
return return
} }
} }
if (!compareFanSpeed(pastFanSpeedList, newFanSpeedList)) { }
copy(pastFanSpeedList, newFanSpeedList) func (c *fanControl) applyFanspeed(pastFanSpeedList, newFanSpeedList []int) {
if !compareFanSpeed(pastFanSpeedList, newFanSpeedList) {
return
}
args := make([]string, 0) args := make([]string, 0)
args = append(args, "raw", "0x3a", "0x01", ) args = append(args, "raw", "0x3a", "0x01", )
for _, item := range newFanSpeedList { for _, item := range newFanSpeedList {
@ -86,8 +86,6 @@ func (c *fanControl) StartControl() {
buf.WriteRune(' ') buf.WriteRune(' ')
} }
fanspeedLogger.Infof("Commit fan speed with %s", buf.String()) fanspeedLogger.Infof("Commit fan speed with %s", buf.String())
}
}
} }
func compareFanSpeed(old, new []int) bool { func compareFanSpeed(old, new []int) bool {

View File

@ -33,13 +33,13 @@ type processor struct {
type TempetureInfo struct { type TempetureInfo struct {
Id int Id int
Tempeture float64 Tempeture float64
Changed bool At time.Time
} }
type FanspeedInfo struct { type FanspeedInfo struct {
Id int Id int
FanSpeed int FanSpeed int
Changed bool At time.Time
} }
type Processor interface { type Processor interface {
@ -132,7 +132,7 @@ func (p *processor) StartMonitoring() {
for { for {
select { select {
case <-ticker: case now := <-ticker:
var ( var (
highestTemp float64 highestTemp float64
fanspeed int fanspeed int
@ -147,7 +147,7 @@ func (p *processor) StartMonitoring() {
case p.tempetureChanged <- TempetureInfo{ case p.tempetureChanged <- TempetureInfo{
Id: p.id, Id: p.id,
Tempeture: highestTemp, Tempeture: highestTemp,
Changed: highestTemp != p.tempeture, At: now,
}: }:
default: default:
} }
@ -165,6 +165,7 @@ func (p *processor) StartMonitoring() {
Id: p.id, Id: p.id,
FanSpeed: fanspeed, FanSpeed: fanspeed,
Changed: fanspeed != p.fanSpeed, Changed: fanspeed != p.fanSpeed,
At: now,
}: }:
default: default:
} }