Shinichiro Hamaji | b69bf8a | 2015-06-10 14:52:06 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | package kati |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 16 | |
| 17 | import ( |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 18 | "container/heap" |
Fumitoshi Ukai | 531c5d2 | 2015-06-27 00:58:56 +0900 | [diff] [blame] | 19 | "errors" |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 20 | "fmt" |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 21 | "os" |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 22 | "os/exec" |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 23 | "syscall" |
| 24 | "time" |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 25 | |
| 26 | "github.com/golang/glog" |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
Fumitoshi Ukai | a43b96f | 2015-07-17 19:54:21 +0900 | [diff] [blame] | 29 | var ( |
| 30 | errNothingDone = errors.New("nothing done") |
| 31 | ) |
| 32 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 33 | type job struct { |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 34 | n *DepNode |
| 35 | ex *Executor |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 36 | parents []*job |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 37 | outputTs int64 |
| 38 | numDeps int |
| 39 | depsTs int64 |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 40 | id int |
Shinichiro Hamaji | a680842 | 2015-05-13 18:00:50 +0900 | [diff] [blame] | 41 | |
| 42 | runners []runner |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 43 | } |
| 44 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 45 | type jobResult struct { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 46 | j *job |
| 47 | w *worker |
| 48 | err error |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 49 | } |
| 50 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 51 | type newDep struct { |
| 52 | j *job |
| 53 | neededBy *job |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 54 | } |
| 55 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 56 | type worker struct { |
| 57 | wm *workerManager |
| 58 | jobChan chan *job |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 59 | waitChan chan bool |
| 60 | doneChan chan bool |
| 61 | } |
| 62 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 63 | type jobQueue []*job |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 64 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 65 | func (jq jobQueue) Len() int { return len(jq) } |
| 66 | func (jq jobQueue) Swap(i, j int) { jq[i], jq[j] = jq[j], jq[i] } |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 67 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 68 | func (jq jobQueue) Less(i, j int) bool { |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 69 | // First come, first serve, for GNU make compatibility. |
| 70 | return jq[i].id < jq[j].id |
| 71 | } |
| 72 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 73 | func (jq *jobQueue) Push(x interface{}) { |
| 74 | item := x.(*job) |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 75 | *jq = append(*jq, item) |
| 76 | } |
| 77 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 78 | func (jq *jobQueue) Pop() interface{} { |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 79 | old := *jq |
| 80 | n := len(old) |
| 81 | item := old[n-1] |
| 82 | *jq = old[0 : n-1] |
| 83 | return item |
| 84 | } |
| 85 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 86 | func newWorker(wm *workerManager) *worker { |
| 87 | w := &worker{ |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 88 | wm: wm, |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 89 | jobChan: make(chan *job), |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 90 | waitChan: make(chan bool), |
| 91 | doneChan: make(chan bool), |
| 92 | } |
| 93 | return w |
| 94 | } |
| 95 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 96 | func (w *worker) Run() { |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 97 | done := false |
| 98 | for !done { |
| 99 | select { |
| 100 | case j := <-w.jobChan: |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 101 | err := j.build() |
| 102 | w.wm.ReportResult(w, j, err) |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 103 | case done = <-w.waitChan: |
| 104 | } |
| 105 | } |
| 106 | w.doneChan <- true |
| 107 | } |
| 108 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 109 | func (w *worker) PostJob(j *job) { |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 110 | w.jobChan <- j |
| 111 | } |
| 112 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 113 | func (w *worker) Wait() { |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 114 | w.waitChan <- true |
| 115 | <-w.doneChan |
| 116 | } |
| 117 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 118 | func (j *job) createRunners() ([]runner, error) { |
Fumitoshi Ukai | a70b4ea | 2015-06-30 15:31:49 +0900 | [diff] [blame] | 119 | runners, _, err := createRunners(j.ex.ctx, j.n) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 120 | return runners, err |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 121 | } |
| 122 | |
Shinichiro Hamaji | 71fae4c | 2015-05-25 17:48:34 +0900 | [diff] [blame] | 123 | // TODO(ukai): use time.Time? |
| 124 | func getTimestamp(filename string) int64 { |
| 125 | st, err := os.Stat(filename) |
| 126 | if err != nil { |
| 127 | return -2 |
| 128 | } |
| 129 | return st.ModTime().Unix() |
| 130 | } |
| 131 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 132 | func (j *job) build() error { |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 133 | if j.n.IsPhony { |
| 134 | j.outputTs = -2 // trigger cmd even if all inputs don't exist. |
| 135 | } else { |
| 136 | j.outputTs = getTimestamp(j.n.Output) |
| 137 | } |
| 138 | |
| 139 | if !j.n.HasRule { |
| 140 | if j.outputTs >= 0 || j.n.IsPhony { |
Fumitoshi Ukai | a43b96f | 2015-07-17 19:54:21 +0900 | [diff] [blame] | 141 | return errNothingDone |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 142 | } |
| 143 | if len(j.parents) == 0 { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 144 | return fmt.Errorf("*** No rule to make target %q.", j.n.Output) |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 145 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 146 | return fmt.Errorf("*** No rule to make target %q, needed by %q.", j.n.Output, j.parents[0].n.Output) |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if j.outputTs >= j.depsTs { |
| 150 | // TODO: stats. |
Fumitoshi Ukai | a43b96f | 2015-07-17 19:54:21 +0900 | [diff] [blame] | 151 | return errNothingDone |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 152 | } |
| 153 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 154 | rr, err := j.createRunners() |
| 155 | if err != nil { |
| 156 | return err |
| 157 | } |
Fumitoshi Ukai | d1578d5 | 2015-09-04 17:44:15 +0900 | [diff] [blame] | 158 | if len(rr) == 0 { |
| 159 | return errNothingDone |
| 160 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 161 | for _, r := range rr { |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 162 | err := r.run(j.n.Output) |
Fumitoshi Ukai | 60f92c0 | 2015-07-29 10:09:36 +0900 | [diff] [blame] | 163 | glog.Warningf("cmd result for %q: %v", j.n.Output, err) |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 164 | if err != nil { |
| 165 | exit := exitStatus(err) |
Fumitoshi Ukai | 08c1e94 | 2015-07-15 16:38:07 +0900 | [diff] [blame] | 166 | return fmt.Errorf("*** [%s] Error %d", j.n.Output, exit) |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | if j.n.IsPhony { |
| 171 | j.outputTs = time.Now().Unix() |
| 172 | } else { |
| 173 | j.outputTs = getTimestamp(j.n.Output) |
| 174 | if j.outputTs < 0 { |
| 175 | j.outputTs = time.Now().Unix() |
| 176 | } |
| 177 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 178 | return nil |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 179 | } |
| 180 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 181 | func (wm *workerManager) handleJobs() error { |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 182 | for { |
Shinichiro Hamaji | 18287f0 | 2015-07-06 14:48:48 +0900 | [diff] [blame] | 183 | if len(wm.freeWorkers) == 0 { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 184 | return nil |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 185 | } |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 186 | if wm.readyQueue.Len() == 0 { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 187 | return nil |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 188 | } |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 189 | j := heap.Pop(&wm.readyQueue).(*job) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 190 | glog.V(1).Infof("run: %s", j.n.Output) |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 191 | |
Shinichiro Hamaji | 18287f0 | 2015-07-06 14:48:48 +0900 | [diff] [blame] | 192 | j.numDeps = -1 // Do not let other workers pick this. |
| 193 | w := wm.freeWorkers[0] |
| 194 | wm.freeWorkers = wm.freeWorkers[1:] |
| 195 | wm.busyWorkers[w] = true |
| 196 | w.jobChan <- j |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 197 | } |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 198 | } |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 199 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 200 | func (wm *workerManager) updateParents(j *job) { |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 201 | for _, p := range j.parents { |
| 202 | p.numDeps-- |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 203 | glog.V(1).Infof("child: %s (%d)", p.n.Output, p.numDeps) |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 204 | if p.depsTs < j.outputTs { |
| 205 | p.depsTs = j.outputTs |
| 206 | } |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 207 | wm.maybePushToReadyQueue(p) |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 208 | } |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 209 | } |
| 210 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 211 | type workerManager struct { |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 212 | maxJobs int |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 213 | jobs []*job |
| 214 | readyQueue jobQueue |
| 215 | jobChan chan *job |
| 216 | resultChan chan jobResult |
| 217 | newDepChan chan newDep |
Fumitoshi Ukai | 531c5d2 | 2015-06-27 00:58:56 +0900 | [diff] [blame] | 218 | stopChan chan bool |
Shinichiro Hamaji | 55c50bd | 2015-04-27 21:05:47 +0900 | [diff] [blame] | 219 | waitChan chan bool |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 220 | doneChan chan error |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 221 | freeWorkers []*worker |
| 222 | busyWorkers map[*worker]bool |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 223 | ex *Executor |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 224 | runnings map[string]*job |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 225 | |
| 226 | finishCnt int |
Fumitoshi Ukai | a43b96f | 2015-07-17 19:54:21 +0900 | [diff] [blame] | 227 | skipCnt int |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 228 | } |
| 229 | |
Shinichiro Hamaji | 18287f0 | 2015-07-06 14:48:48 +0900 | [diff] [blame] | 230 | func newWorkerManager(numJobs int) (*workerManager, error) { |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 231 | wm := &workerManager{ |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 232 | maxJobs: numJobs, |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 233 | jobChan: make(chan *job), |
| 234 | resultChan: make(chan jobResult), |
| 235 | newDepChan: make(chan newDep), |
Fumitoshi Ukai | 531c5d2 | 2015-06-27 00:58:56 +0900 | [diff] [blame] | 236 | stopChan: make(chan bool), |
Shinichiro Hamaji | 55c50bd | 2015-04-27 21:05:47 +0900 | [diff] [blame] | 237 | waitChan: make(chan bool), |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 238 | doneChan: make(chan error), |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 239 | busyWorkers: make(map[*worker]bool), |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 240 | } |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 241 | |
Shinichiro Hamaji | 18287f0 | 2015-07-06 14:48:48 +0900 | [diff] [blame] | 242 | wm.busyWorkers = make(map[*worker]bool) |
| 243 | for i := 0; i < numJobs; i++ { |
| 244 | w := newWorker(wm) |
| 245 | wm.freeWorkers = append(wm.freeWorkers, w) |
| 246 | go w.Run() |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 247 | } |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 248 | heap.Init(&wm.readyQueue) |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 249 | go wm.Run() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 250 | return wm, nil |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | func exitStatus(err error) int { |
| 254 | if err == nil { |
| 255 | return 0 |
| 256 | } |
| 257 | exit := 1 |
| 258 | if err, ok := err.(*exec.ExitError); ok { |
| 259 | if w, ok := err.ProcessState.Sys().(syscall.WaitStatus); ok { |
| 260 | return w.ExitStatus() |
| 261 | } |
| 262 | } |
| 263 | return exit |
| 264 | } |
| 265 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 266 | func (wm *workerManager) hasTodo() bool { |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 267 | return wm.finishCnt != len(wm.jobs) |
| 268 | } |
| 269 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 270 | func (wm *workerManager) maybePushToReadyQueue(j *job) { |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 271 | if j.numDeps != 0 { |
| 272 | return |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 273 | } |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 274 | heap.Push(&wm.readyQueue, j) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 275 | glog.V(1).Infof("ready: %s", j.n.Output) |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 276 | } |
| 277 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 278 | func (wm *workerManager) handleNewDep(j *job, neededBy *job) { |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 279 | if j.numDeps < 0 { |
| 280 | neededBy.numDeps-- |
Shinichiro Hamaji | 5180c97 | 2015-04-28 20:14:38 +0900 | [diff] [blame] | 281 | if neededBy.id > 0 { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 282 | panic("FIXME: already in WM... can this happen?") |
Shinichiro Hamaji | 5180c97 | 2015-04-28 20:14:38 +0900 | [diff] [blame] | 283 | } |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 284 | } else { |
| 285 | j.parents = append(j.parents, neededBy) |
| 286 | } |
| 287 | } |
| 288 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 289 | func (wm *workerManager) Run() { |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 290 | done := false |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 291 | var err error |
| 292 | Loop: |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 293 | for wm.hasTodo() || len(wm.busyWorkers) > 0 || len(wm.runnings) > 0 || !done { |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 294 | select { |
| 295 | case j := <-wm.jobChan: |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 296 | glog.V(1).Infof("wait: %s (%d)", j.n.Output, j.numDeps) |
Shinichiro Hamaji | 5180c97 | 2015-04-28 20:14:38 +0900 | [diff] [blame] | 297 | j.id = len(wm.jobs) + 1 |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 298 | wm.jobs = append(wm.jobs, j) |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 299 | wm.maybePushToReadyQueue(j) |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 300 | case jr := <-wm.resultChan: |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 301 | glog.V(1).Infof("done: %s", jr.j.n.Output) |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 302 | delete(wm.busyWorkers, jr.w) |
| 303 | wm.freeWorkers = append(wm.freeWorkers, jr.w) |
| 304 | wm.updateParents(jr.j) |
Shinichiro Hamaji | dbc6c13 | 2015-04-28 18:26:36 +0900 | [diff] [blame] | 305 | wm.finishCnt++ |
Fumitoshi Ukai | a43b96f | 2015-07-17 19:54:21 +0900 | [diff] [blame] | 306 | if jr.err == errNothingDone { |
| 307 | wm.skipCnt++ |
| 308 | jr.err = nil |
| 309 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 310 | if jr.err != nil { |
| 311 | err = jr.err |
Fumitoshi Ukai | 531c5d2 | 2015-06-27 00:58:56 +0900 | [diff] [blame] | 312 | close(wm.stopChan) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 313 | break Loop |
| 314 | } |
Shinichiro Hamaji | 55c50bd | 2015-04-27 21:05:47 +0900 | [diff] [blame] | 315 | case af := <-wm.newDepChan: |
| 316 | wm.handleNewDep(af.j, af.neededBy) |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 317 | glog.V(1).Infof("dep: %s (%d) %s", af.neededBy.n.Output, af.neededBy.numDeps, af.j.n.Output) |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 318 | case done = <-wm.waitChan: |
| 319 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 320 | err = wm.handleJobs() |
| 321 | if err != nil { |
| 322 | break Loop |
| 323 | } |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 324 | |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 325 | 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)) |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 326 | } |
Fumitoshi Ukai | 531c5d2 | 2015-06-27 00:58:56 +0900 | [diff] [blame] | 327 | if !done { |
| 328 | <-wm.waitChan |
| 329 | } |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 330 | |
Shinichiro Hamaji | 18287f0 | 2015-07-06 14:48:48 +0900 | [diff] [blame] | 331 | for _, w := range wm.freeWorkers { |
| 332 | w.Wait() |
| 333 | } |
| 334 | for w := range wm.busyWorkers { |
| 335 | w.Wait() |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 336 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 337 | wm.doneChan <- err |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 338 | } |
| 339 | |
Fumitoshi Ukai | 531c5d2 | 2015-06-27 00:58:56 +0900 | [diff] [blame] | 340 | func (wm *workerManager) PostJob(j *job) error { |
| 341 | select { |
| 342 | case wm.jobChan <- j: |
| 343 | return nil |
| 344 | case <-wm.stopChan: |
| 345 | return errors.New("worker manager stopped") |
| 346 | } |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 347 | } |
| 348 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 349 | func (wm *workerManager) ReportResult(w *worker, j *job, err error) { |
Fumitoshi Ukai | 531c5d2 | 2015-06-27 00:58:56 +0900 | [diff] [blame] | 350 | select { |
| 351 | case wm.resultChan <- jobResult{w: w, j: j, err: err}: |
| 352 | case <-wm.stopChan: |
| 353 | } |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 354 | } |
| 355 | |
Fumitoshi Ukai | dfb518b | 2015-06-25 13:19:55 +0900 | [diff] [blame] | 356 | func (wm *workerManager) ReportNewDep(j *job, neededBy *job) { |
Fumitoshi Ukai | 531c5d2 | 2015-06-27 00:58:56 +0900 | [diff] [blame] | 357 | select { |
| 358 | case wm.newDepChan <- newDep{j: j, neededBy: neededBy}: |
| 359 | case <-wm.stopChan: |
| 360 | } |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 361 | } |
| 362 | |
Fumitoshi Ukai | a43b96f | 2015-07-17 19:54:21 +0900 | [diff] [blame] | 363 | func (wm *workerManager) Wait() (int, error) { |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 364 | wm.waitChan <- true |
Fumitoshi Ukai | a43b96f | 2015-07-17 19:54:21 +0900 | [diff] [blame] | 365 | err := <-wm.doneChan |
Fumitoshi Ukai | d1578d5 | 2015-09-04 17:44:15 +0900 | [diff] [blame] | 366 | glog.V(2).Infof("finish %d skip %d", wm.finishCnt, wm.skipCnt) |
Fumitoshi Ukai | a43b96f | 2015-07-17 19:54:21 +0900 | [diff] [blame] | 367 | return wm.finishCnt - wm.skipCnt, err |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 368 | } |