-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontroller.go
More file actions
104 lines (94 loc) · 2.1 KB
/
controller.go
File metadata and controls
104 lines (94 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"log"
"sync"
"time"
)
type Mode int
const (
MaxMode Mode = 0
MeanMode Mode = 1
)
type Controller struct {
interval int
mode Mode
pwdIds []int
executor Executor
devices GpuDevicesInterface
thresholds Thresholds
lastDutyCycle int
}
func (c *Controller) Execute(dutyCycle int) {
var wg sync.WaitGroup
for _, pwdId := range c.pwdIds {
wg.Add(1)
go func(id int) {
defer wg.Done()
if err := c.executor.Execute(id, dutyCycle); err != nil {
log.Printf("Unable to set fan: id=%v duty-cycle=%v", id, dutyCycle)
}
}(pwdId)
}
wg.Wait()
}
func (c *Controller) Run() {
go func() {
ticker := time.NewTicker(time.Second * time.Duration(c.interval))
for {
select {
case <-ticker.C:
var temp int
if c.mode == MaxMode {
temp = c.devices.GetMaxTemperature()
} else {
temp = c.devices.GetMeanTemperature()
}
dutyCycle := c.thresholds.GetDutyCycleFromTemperature(temp)
if dutyCycle != c.lastDutyCycle {
c.Execute(dutyCycle)
log.Printf("Temperature=%v Celsius and setup duty-cycle=%v%%", temp, dutyCycle)
c.lastDutyCycle = dutyCycle
} else {
log.Printf("DutyCycle did not change")
}
default:
time.Sleep(time.Second * time.Duration(c.interval))
}
}
}()
}
func NewController(interval int, mode Mode, pwdIds []int, executor Executor, devices GpuDevicesInterface, thresholds Thresholds) *Controller {
return &Controller{
interval: interval,
mode: mode,
pwdIds: pwdIds,
executor: executor,
devices: devices,
thresholds: thresholds,
lastDutyCycle: 0,
}
}
func NewControllerFromConfig(c Config) *Controller {
var mode Mode
if c.Mode == "max" {
mode = MaxMode
} else {
mode = MeanMode
}
var executor Executor
if c.IpmiDebug {
executor = &FakeExecutor{}
} else {
executor = &IPMIExecutor{}
}
var devices GpuDevicesInterface
if c.GpuDebug {
devices = &FakeGpuDevices{}
} else {
devices = &GpuDevices{}
}
thresholds := Thresholds{
points: c.Thresholds,
}
return NewController(c.Interval, mode, c.PwdIds, executor, devices, thresholds)
}