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)
}
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 {
select {
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:
if point, err := m.getFanspeedPoint(changedSpeed); err == nil {
pointList = append(pointList, point)
} else {
influxLogger.Debugf("id %d speed err %s", changedSpeed.Id, err)
}
fanspeedList = append(fanspeedList, changedSpeed)
case changedTempeture := <-m.tempetureConsumer:
if point, err := m.getTempeturePoint(changedTempeture); err == nil {
pointList = append(pointList, point)
} else {
influxLogger.Debugf("id %d tempeture err %s", changedTempeture.Id, err)
}
tempetureList = append(tempetureList, changedTempeture)
case <-m.handler.Done():
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 {
influxLogger.Warn(err)
}
pointList = make([]*client.Point, 0, 0)
}
}
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,
}
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) {
@ -134,5 +151,5 @@ func (m *influxMetric) getFanspeedPoint(info processor.FanspeedInfo) (*client.Po
"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")
ticker := time.Tick(c.sampleDuration)
pastFanSpeedList := make([]int, c.processorCount)
newFanSpeedList := make([]int, c.processorCount)
for {
checker:
pastFanSpeedList, newFanSpeedList := make([]int, c.processorCount), make([]int, c.processorCount)
for {
select {
case <-ticker:
break checker
go c.applyFanspeed(pastFanSpeedList, newFanSpeedList)
pastFanSpeedList, newFanSpeedList = newFanSpeedList, make([]int, c.processorCount)
case changedSpeed := <-c.fanSpeedConsumer:
if changedSpeed.Changed {
newFanSpeedList[changedSpeed.Id] = changedSpeed.FanSpeed
}
case <-c.handler.Done():
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 = append(args, "raw", "0x3a", "0x01", )
for _, item := range newFanSpeedList {
@ -86,8 +86,6 @@ func (c *fanControl) StartControl() {
buf.WriteRune(' ')
}
fanspeedLogger.Infof("Commit fan speed with %s", buf.String())
}
}
}
func compareFanSpeed(old, new []int) bool {

View File

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