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,39 +81,56 @@ 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 { for {
pointList = pointList[:cap(pointList)] select {
checker: case <-ticker:
for { fanspeedListTemp, tempetureListTemp := fanspeedList, tempetureList
select { fanspeedList, tempetureList = make([]processor.FanspeedInfo, 0, 0), make([]processor.TempetureInfo, 0, 0)
case <-ticker: go m.sendPoint(influxDbConn, batchPoint, fanspeedListTemp, tempetureListTemp)
break checker case changedSpeed := <-m.fanSpeedConsumer:
case changedSpeed := <-m.fanSpeedConsumer: fanspeedList = append(fanspeedList, changedSpeed)
if point, err := m.getFanspeedPoint(changedSpeed); err == nil {
pointList = append(pointList, point)
} else {
influxLogger.Debugf("id %d speed err %s", changedSpeed.Id, err)
}
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)
}
case <-m.handler.Done():
return
}
}
batchPoint.AddPoints(pointList)
if err := influxDbConn.Write(batchPoint); err != nil { case changedTempeture := <-m.tempetureConsumer:
influxLogger.Warn(err) tempetureList = append(tempetureList, changedTempeture)
case <-m.handler.Done():
return
} }
pointList = make([]*client.Point, 0, 0)
} }
} }
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)
}
}
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,46 +49,44 @@ 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 { for {
checker: select {
for { case <-ticker:
select { go c.applyFanspeed(pastFanSpeedList, newFanSpeedList)
case <-ticker: pastFanSpeedList, newFanSpeedList = newFanSpeedList, make([]int, c.processorCount)
break checker case changedSpeed := <-c.fanSpeedConsumer:
case changedSpeed := <-c.fanSpeedConsumer: newFanSpeedList[changedSpeed.Id] = changedSpeed.FanSpeed
if changedSpeed.Changed { case <-c.handler.Done():
newFanSpeedList[changedSpeed.Id] = changedSpeed.FanSpeed return
}
case <-c.handler.Done():
return
}
}
if (!compareFanSpeed(pastFanSpeedList, newFanSpeedList)) {
copy(pastFanSpeedList, newFanSpeedList)
args := make([]string, 0)
args = append(args, "raw", "0x3a", "0x01", )
for _, item := range newFanSpeedList {
args = append(args, fmt.Sprintf("0x%x", item))
}
args = append(args,
"0x0", "0x0", "0x0", "0x0", "0x0", "0x0",
)
cmd := exec.Command("ipmitool", args...)
if err := cmd.Run(); err != nil {
c.handler.NotifyError(err)
return
}
buf := bytes.NewBufferString("")
for _, item := range newFanSpeedList {
buf.WriteString(fmt.Sprintf("0x%x", item))
buf.WriteRune(' ')
}
fanspeedLogger.Infof("Commit fan speed with %s", buf.String())
} }
} }
} }
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 {
args = append(args, fmt.Sprintf("0x%x", item))
}
args = append(args,
"0x0", "0x0", "0x0", "0x0", "0x0", "0x0",
)
cmd := exec.Command("ipmitool", args...)
if err := cmd.Run(); err != nil {
c.handler.NotifyError(err)
return
}
buf := bytes.NewBufferString("")
for _, item := range newFanSpeedList {
buf.WriteString(fmt.Sprintf("0x%x", item))
buf.WriteRune(' ')
}
fanspeedLogger.Infof("Commit fan speed with %s", buf.String())
}
func compareFanSpeed(old, new []int) bool { func compareFanSpeed(old, new []int) bool {
new = new[:len(old)] // this line is the key new = new[:len(old)] // this line is the key

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:
} }