-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathloop.go
304 lines (260 loc) · 8.71 KB
/
loop.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"reflect"
"sync"
"time"
"github.com/sirupsen/logrus"
)
var (
maintainedChargingInProgress = false
maintainLoopLock = &sync.Mutex{}
// mg is used to skip several loops when system woke up or before sleep
wg = &sync.WaitGroup{}
loopInterval = time.Duration(10) * time.Second
loopRecorder = NewMaintainLoopRecorder(60)
)
// MaintainLoopRecorder records the last N maintain loop times.
type MaintainLoopRecorder struct {
MaxRecordCount int
LastMaintainLoopTimes []time.Time
mu *sync.Mutex
}
// NewMaintainLoopRecorder returns a new MaintainLoopRecorder.
func NewMaintainLoopRecorder(maxRecordCount int) *MaintainLoopRecorder {
return &MaintainLoopRecorder{
MaxRecordCount: maxRecordCount,
LastMaintainLoopTimes: make([]time.Time, 0),
mu: &sync.Mutex{},
}
}
// AddRecord adds a new record.
func (r *MaintainLoopRecorder) AddRecord() {
r.mu.Lock()
defer r.mu.Unlock()
if len(r.LastMaintainLoopTimes) >= r.MaxRecordCount {
r.LastMaintainLoopTimes = r.LastMaintainLoopTimes[1:]
}
r.LastMaintainLoopTimes = append(r.LastMaintainLoopTimes, time.Now())
}
// GetRecordsIn returns the number of continuous records in the last duration.
func (r *MaintainLoopRecorder) GetRecordsIn(last time.Duration) int {
r.mu.Lock()
defer r.mu.Unlock()
// Find continuous records from the end of the list.
// Continuous records are defined as the time difference between
// two adjacent records is less than loopInterval+1 second.
count := 0
for i := len(r.LastMaintainLoopTimes) - 1; i >= 0; i-- {
record := r.LastMaintainLoopTimes[i]
if time.Since(record) > last {
break
}
theRecordAfter := record
if i+1 < len(r.LastMaintainLoopTimes) {
theRecordAfter = r.LastMaintainLoopTimes[i+1]
}
if theRecordAfter.Sub(record) >= loopInterval+time.Second {
break
}
count++
}
return count
}
// GetLastRecord returns the last record.
func (r *MaintainLoopRecorder) GetLastRecord() time.Time {
r.mu.Lock()
defer r.mu.Unlock()
if len(r.LastMaintainLoopTimes) == 0 {
return time.Time{}
}
return r.LastMaintainLoopTimes[len(r.LastMaintainLoopTimes)-1]
}
// infiniteLoop runs forever and maintains the battery charge,
// which is called by the daemon.
func infiniteLoop() {
for {
maintainLoop()
time.Sleep(loopInterval)
}
}
// maintainLoop maintains the battery charge. It has the logic to
// prevent parallel runs. So if one maintain loop is already running,
// the next one will need to wait until the first one finishes.
func maintainLoop() bool {
maintainLoopLock.Lock()
defer maintainLoopLock.Unlock()
// See wg.Add() in sleepcallback.go for why we need to wait.
tsBeforeWait := time.Now()
wg.Wait()
tsAfterWait := time.Now()
if tsAfterWait.Sub(tsBeforeWait) > time.Second*1 {
logrus.Debugf("this maintain loop waited %d seconds after being initiated, now ready to execute", int(tsAfterWait.Sub(tsBeforeWait).Seconds()))
}
lastRecord := loopRecorder.GetLastRecord()
diff := time.Since(lastRecord)
if diff > loopInterval+time.Second {
logrus.WithFields(logrus.Fields{
"lastRecord": loopRecorder.GetLastRecord(),
"interval": loopInterval.String(),
"actual": diff.String(),
}).Warn("possible missed maintain loop")
}
loopRecorder.AddRecord()
return maintainLoopInner()
}
// maintainLoopForced maintains the battery charge. It runs as soon as
// it is called, without waiting for the previous maintain loop to finish.
// It is mainly called by the HTTP APIs.
func maintainLoopForced() bool {
return maintainLoopInner()
}
func maintainLoopInner() bool {
upper := config.Limit
delta := config.LowerLimitDelta
lower := upper - delta
maintain := upper < 100
isChargingEnabled, err := smcConn.IsChargingEnabled()
if err != nil {
logrus.Errorf("IsChargingEnabled failed: %v", err)
return false
}
// If maintain is disabled, we don't care about the battery charge, enable charging anyway.
if !maintain {
logrus.Debug("limit set to 100%, maintain loop disabled")
if !isChargingEnabled {
logrus.Debug("charging disabled, enabling")
err = smcConn.EnableCharging()
if err != nil {
logrus.Errorf("EnableCharging failed: %v", err)
return false
}
if config.ControlMagSafeLED {
batteryCharge, err := smcConn.GetBatteryCharge()
if err == nil {
_ = smcConn.SetMagSafeCharging(batteryCharge < 100)
}
}
}
maintainedChargingInProgress = false
return true
}
batteryCharge, err := smcConn.GetBatteryCharge()
if err != nil {
logrus.Errorf("GetBatteryCharge failed: %v", err)
return false
}
isPluggedIn, err := smcConn.IsPluggedIn()
if err != nil {
logrus.Errorf("IsPluggedIn failed: %v", err)
return false
}
maintainedChargingInProgress = isChargingEnabled && isPluggedIn
printStatus(batteryCharge, lower, upper, isChargingEnabled, isPluggedIn, maintainedChargingInProgress)
if batteryCharge < lower && !isChargingEnabled {
maintainLoopCount := loopRecorder.GetRecordsIn(time.Minute * 2)
expectedMaintainLoopCount := int(time.Minute * 2 / loopInterval)
minMaintainLoopCount := expectedMaintainLoopCount - 1
// If maintain loop is missed too many times, we assume the system is in a rapid sleep/wake loop, or macOS
// haven't sent the sleep notification but the system is actually sleep/waking up. In either case, we should
// not enable charging, which will cause unexpected charging.
//
// This is a workaround for the issue that macOS sometimes doesn't send the sleep notification.
//
// We allow at most 1 missed maintain loop.
if maintainLoopCount < minMaintainLoopCount {
logrus.WithFields(logrus.Fields{
"batteryCharge": batteryCharge,
"lower": lower,
"upper": upper,
"delta": delta,
"maintainLoopCount": maintainLoopCount,
"expectedMaintainLoopCount": expectedMaintainLoopCount,
"minMaintainLoopCount": minMaintainLoopCount,
}).Infof("Battery charge is below lower limit, but too many missed maintain loops are missed. Will wait until maintain loops are stable")
return true
}
logrus.WithFields(logrus.Fields{
"batteryCharge": batteryCharge,
"lower": lower,
"upper": upper,
"delta": delta,
}).Infof("Battery charge is below lower limit, enabling charging")
err = smcConn.EnableCharging()
if err != nil {
logrus.Errorf("EnableCharging failed: %v", err)
return false
}
isChargingEnabled = true
maintainedChargingInProgress = true
}
if batteryCharge >= upper && isChargingEnabled {
logrus.WithFields(logrus.Fields{
"batteryCharge": batteryCharge,
"lower": lower,
"upper": upper,
"delta": delta,
}).Infof("Battery charge is above upper limit, disabling charging")
err = smcConn.DisableCharging()
if err != nil {
logrus.Errorf("DisableCharging failed: %v", err)
return false
}
isChargingEnabled = false
maintainedChargingInProgress = false
}
if config.ControlMagSafeLED {
updateMagSafeLed(isChargingEnabled)
}
// batteryCharge >= upper - delta && batteryCharge < upper
// do nothing, keep as-is
return true
}
func updateMagSafeLed(isChargingEnabled bool) {
err := smcConn.SetMagSafeCharging(isChargingEnabled)
if err != nil {
logrus.Errorf("SetMagSafeCharging failed: %v", err)
}
}
var lastPrintTime time.Time
type loopStatus struct {
batteryCharge int
lower int
upper int
isChargingEnabled bool
isPluggedIn bool
maintainedChargingInProgress bool
}
var lastStatus loopStatus
func printStatus(
batteryCharge int,
lower int,
upper int,
isChargingEnabled bool,
isPluggedIn bool,
maintainedChargingInProgress bool,
) {
currentStatus := loopStatus{
batteryCharge: batteryCharge,
lower: lower,
upper: upper,
isChargingEnabled: isChargingEnabled,
isPluggedIn: isPluggedIn,
maintainedChargingInProgress: maintainedChargingInProgress,
}
fields := logrus.Fields{
"batteryCharge": batteryCharge,
"lower": lower,
"upper": upper,
"chargingEnabled": isChargingEnabled,
"isPluggedIn": isPluggedIn,
"maintainedChargingInProgress": maintainedChargingInProgress,
}
defer func() { lastPrintTime = time.Now() }()
// Skip printing if the last print was less than loopInterval+1 seconds ago and everything is the same.
if time.Since(lastPrintTime) < loopInterval+time.Second && reflect.DeepEqual(lastStatus, currentStatus) {
logrus.WithFields(fields).Trace("maintain loop status")
return
}
logrus.WithFields(fields).Debug("maintain loop status")
lastStatus = currentStatus
}