forked from google/kati
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
368 lines (325 loc) · 7.53 KB
/
worker.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright 2015 Google Inc. All rights reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kati
import (
"container/heap"
"errors"
"fmt"
"os"
"os/exec"
"syscall"
"time"
"github.com/golang/glog"
)
var (
errNothingDone = errors.New("nothing done")
)
type job struct {
n *DepNode
ex *Executor
parents []*job
outputTs int64
numDeps int
depsTs int64
id int
runners []runner
}
type jobResult struct {
j *job
w *worker
err error
}
type newDep struct {
j *job
neededBy *job
}
type worker struct {
wm *workerManager
jobChan chan *job
waitChan chan bool
doneChan chan bool
}
type jobQueue []*job
func (jq jobQueue) Len() int { return len(jq) }
func (jq jobQueue) Swap(i, j int) { jq[i], jq[j] = jq[j], jq[i] }
func (jq jobQueue) Less(i, j int) bool {
// First come, first serve, for GNU make compatibility.
return jq[i].id < jq[j].id
}
func (jq *jobQueue) Push(x interface{}) {
item := x.(*job)
*jq = append(*jq, item)
}
func (jq *jobQueue) Pop() interface{} {
old := *jq
n := len(old)
item := old[n-1]
*jq = old[0 : n-1]
return item
}
func newWorker(wm *workerManager) *worker {
w := &worker{
wm: wm,
jobChan: make(chan *job),
waitChan: make(chan bool),
doneChan: make(chan bool),
}
return w
}
func (w *worker) Run() {
done := false
for !done {
select {
case j := <-w.jobChan:
err := j.build()
w.wm.ReportResult(w, j, err)
case done = <-w.waitChan:
}
}
w.doneChan <- true
}
func (w *worker) PostJob(j *job) {
w.jobChan <- j
}
func (w *worker) Wait() {
w.waitChan <- true
<-w.doneChan
}
func (j *job) createRunners() ([]runner, error) {
runners, _, err := createRunners(j.ex.ctx, j.n)
return runners, err
}
// TODO(ukai): use time.Time?
func getTimestamp(filename string) int64 {
st, err := os.Stat(filename)
if err != nil {
return -2
}
return st.ModTime().Unix()
}
func (j *job) build() error {
if j.n.IsPhony {
j.outputTs = -2 // trigger cmd even if all inputs don't exist.
} else {
j.outputTs = getTimestamp(j.n.Output)
}
if !j.n.HasRule {
if j.outputTs >= 0 || j.n.IsPhony {
return errNothingDone
}
if len(j.parents) == 0 {
return fmt.Errorf("*** No rule to make target %q.", j.n.Output)
}
return fmt.Errorf("*** No rule to make target %q, needed by %q.", j.n.Output, j.parents[0].n.Output)
}
if j.outputTs >= j.depsTs {
// TODO: stats.
return errNothingDone
}
rr, err := j.createRunners()
if err != nil {
return err
}
if len(rr) == 0 {
return errNothingDone
}
for _, r := range rr {
err := r.run(j.n.Output)
glog.Warningf("cmd result for %q: %v", j.n.Output, err)
if err != nil {
exit := exitStatus(err)
return fmt.Errorf("*** [%s] Error %d", j.n.Output, exit)
}
}
if j.n.IsPhony {
j.outputTs = time.Now().Unix()
} else {
j.outputTs = getTimestamp(j.n.Output)
if j.outputTs < 0 {
j.outputTs = time.Now().Unix()
}
}
return nil
}
func (wm *workerManager) handleJobs() error {
for {
if len(wm.freeWorkers) == 0 {
return nil
}
if wm.readyQueue.Len() == 0 {
return nil
}
j := heap.Pop(&wm.readyQueue).(*job)
glog.V(1).Infof("run: %s", j.n.Output)
j.numDeps = -1 // Do not let other workers pick this.
w := wm.freeWorkers[0]
wm.freeWorkers = wm.freeWorkers[1:]
wm.busyWorkers[w] = true
w.jobChan <- j
}
}
func (wm *workerManager) updateParents(j *job) {
for _, p := range j.parents {
p.numDeps--
glog.V(1).Infof("child: %s (%d)", p.n.Output, p.numDeps)
if p.depsTs < j.outputTs {
p.depsTs = j.outputTs
}
wm.maybePushToReadyQueue(p)
}
}
type workerManager struct {
maxJobs int
jobs []*job
readyQueue jobQueue
jobChan chan *job
resultChan chan jobResult
newDepChan chan newDep
stopChan chan bool
waitChan chan bool
doneChan chan error
freeWorkers []*worker
busyWorkers map[*worker]bool
ex *Executor
runnings map[string]*job
finishCnt int
skipCnt int
}
func newWorkerManager(numJobs int) (*workerManager, error) {
wm := &workerManager{
maxJobs: numJobs,
jobChan: make(chan *job),
resultChan: make(chan jobResult),
newDepChan: make(chan newDep),
stopChan: make(chan bool),
waitChan: make(chan bool),
doneChan: make(chan error),
busyWorkers: make(map[*worker]bool),
}
wm.busyWorkers = make(map[*worker]bool)
for i := 0; i < numJobs; i++ {
w := newWorker(wm)
wm.freeWorkers = append(wm.freeWorkers, w)
go w.Run()
}
heap.Init(&wm.readyQueue)
go wm.Run()
return wm, nil
}
func exitStatus(err error) int {
if err == nil {
return 0
}
exit := 1
if err, ok := err.(*exec.ExitError); ok {
if w, ok := err.ProcessState.Sys().(syscall.WaitStatus); ok {
return w.ExitStatus()
}
}
return exit
}
func (wm *workerManager) hasTodo() bool {
return wm.finishCnt != len(wm.jobs)
}
func (wm *workerManager) maybePushToReadyQueue(j *job) {
if j.numDeps != 0 {
return
}
heap.Push(&wm.readyQueue, j)
glog.V(1).Infof("ready: %s", j.n.Output)
}
func (wm *workerManager) handleNewDep(j *job, neededBy *job) {
if j.numDeps < 0 {
neededBy.numDeps--
if neededBy.id > 0 {
panic("FIXME: already in WM... can this happen?")
}
} else {
j.parents = append(j.parents, neededBy)
}
}
func (wm *workerManager) Run() {
done := false
var err error
Loop:
for wm.hasTodo() || len(wm.busyWorkers) > 0 || len(wm.runnings) > 0 || !done {
select {
case j := <-wm.jobChan:
glog.V(1).Infof("wait: %s (%d)", j.n.Output, j.numDeps)
j.id = len(wm.jobs) + 1
wm.jobs = append(wm.jobs, j)
wm.maybePushToReadyQueue(j)
case jr := <-wm.resultChan:
glog.V(1).Infof("done: %s", jr.j.n.Output)
delete(wm.busyWorkers, jr.w)
wm.freeWorkers = append(wm.freeWorkers, jr.w)
wm.updateParents(jr.j)
wm.finishCnt++
if jr.err == errNothingDone {
wm.skipCnt++
jr.err = nil
}
if jr.err != nil {
err = jr.err
close(wm.stopChan)
break Loop
}
case af := <-wm.newDepChan:
wm.handleNewDep(af.j, af.neededBy)
glog.V(1).Infof("dep: %s (%d) %s", af.neededBy.n.Output, af.neededBy.numDeps, af.j.n.Output)
case done = <-wm.waitChan:
}
err = wm.handleJobs()
if err != nil {
break Loop
}
glog.V(1).Infof("job=%d ready=%d free=%d busy=%d", len(wm.jobs)-wm.finishCnt, wm.readyQueue.Len(), len(wm.freeWorkers), len(wm.busyWorkers))
}
if !done {
<-wm.waitChan
}
for _, w := range wm.freeWorkers {
w.Wait()
}
for w := range wm.busyWorkers {
w.Wait()
}
wm.doneChan <- err
}
func (wm *workerManager) PostJob(j *job) error {
select {
case wm.jobChan <- j:
return nil
case <-wm.stopChan:
return errors.New("worker manager stopped")
}
}
func (wm *workerManager) ReportResult(w *worker, j *job, err error) {
select {
case wm.resultChan <- jobResult{w: w, j: j, err: err}:
case <-wm.stopChan:
}
}
func (wm *workerManager) ReportNewDep(j *job, neededBy *job) {
select {
case wm.newDepChan <- newDep{j: j, neededBy: neededBy}:
case <-wm.stopChan:
}
}
func (wm *workerManager) Wait() (int, error) {
wm.waitChan <- true
err := <-wm.doneChan
glog.V(2).Infof("finish %d skip %d", wm.finishCnt, wm.skipCnt)
return wm.finishCnt - wm.skipCnt, err
}