2 # ex:ts=4:sw=4:sts=4:et
3 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
5 BitBake 'RunQueue' implementation
7 Handles preparation and execution of a queue of tasks
9 Copyright (C) 2006 Richard Purdie
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License version 2 as published by the Free
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along with
22 from bb import msg, data, fetch, event, mkdirhier, utils
26 class TaskFailure(Exception):
27 """Exception raised when a task in a runqueue fails"""
28 def __init__(self, x):
33 BitBake Run Queue implementation
38 def reset_runqueue(self):
41 self.runq_depends = []
42 self.runq_revdeps = []
46 def get_user_idstring(self, task, taskData):
47 fn = taskData.fn_index[self.runq_fnid[task]]
48 taskname = self.runq_task[task]
49 return "%s, %s" % (fn, taskname)
51 def prepare_runqueue(self, cfgData, dataCache, taskData, targets):
53 Turn a set of taskData into a RunQueue and compute data needed
54 to optimise the execution order.
55 targets is list of paired values - a provider name and the task to run
63 bb.msg.note(1, bb.msg.domain.RunQueue, "Preparing Runqueue")
65 for task in range(len(taskData.tasks_name)):
66 fnid = taskData.tasks_fnid[task]
67 fn = taskData.fn_index[fnid]
68 task_deps = dataCache.task_deps[fn]
70 if fnid not in taskData.failed_fnids:
72 depends = taskData.tasks_tdepends[task]
75 if 'deptask' in task_deps and taskData.tasks_name[task] in task_deps['deptask']:
76 taskname = task_deps['deptask'][taskData.tasks_name[task]]
77 for depid in taskData.depids[fnid]:
78 if depid in taskData.build_targets:
79 depdata = taskData.build_targets[depid][0]
81 dep = taskData.fn_index[depdata]
82 depends.append(taskData.gettask_id(dep, taskname))
84 # Resolve Runtime Depends
85 if 'rdeptask' in task_deps and taskData.tasks_name[task] in task_deps['rdeptask']:
86 taskname = task_deps['rdeptask'][taskData.tasks_name[task]]
87 for depid in taskData.rdepids[fnid]:
88 if depid in taskData.run_targets:
89 depdata = taskData.run_targets[depid][0]
91 dep = taskData.fn_index[depdata]
92 depends.append(taskData.gettask_id(dep, taskname))
94 def add_recursive_build(depid):
96 Add build depends of depid to depends
97 (if we've not see it before)
98 (calls itself recursively)
100 if str(depid) in dep_seen:
102 dep_seen.append(depid)
103 if depid in taskData.build_targets:
104 depdata = taskData.build_targets[depid][0]
106 dep = taskData.fn_index[depdata]
107 taskid = taskData.gettask_id(dep, taskname)
108 depends.append(taskid)
109 fnid = taskData.tasks_fnid[taskid]
110 for nextdepid in taskData.depids[fnid]:
111 if nextdepid not in dep_seen:
112 add_recursive_build(nextdepid)
113 for nextdepid in taskData.rdepids[fnid]:
114 if nextdepid not in rdep_seen:
115 add_recursive_run(nextdepid)
117 def add_recursive_run(rdepid):
119 Add runtime depends of rdepid to depends
120 (if we've not see it before)
121 (calls itself recursively)
123 if str(rdepid) in rdep_seen:
125 rdep_seen.append(rdepid)
126 if rdepid in taskData.run_targets:
127 depdata = taskData.run_targets[rdepid][0]
129 dep = taskData.fn_index[depdata]
130 taskid = taskData.gettask_id(dep, taskname)
131 depends.append(taskid)
132 fnid = taskData.tasks_fnid[taskid]
133 for nextdepid in taskData.depids[fnid]:
134 if nextdepid not in dep_seen:
135 add_recursive_build(nextdepid)
136 for nextdepid in taskData.rdepids[fnid]:
137 if nextdepid not in rdep_seen:
138 add_recursive_run(nextdepid)
141 # Resolve Recursive Runtime Depends
142 # Also includes all Build Depends (and their runtime depends)
143 if 'recrdeptask' in task_deps and taskData.tasks_name[task] in task_deps['recrdeptask']:
146 taskname = task_deps['recrdeptask'][taskData.tasks_name[task]]
147 for depid in taskData.depids[fnid]:
148 add_recursive_build(depid)
149 for rdepid in taskData.rdepids[fnid]:
150 add_recursive_run(rdepid)
152 #Prune self references
155 bb.msg.debug(2, bb.msg.domain.RunQueue, "Task %s (%s %s) contains self reference! %s" % (task, taskData.fn_index[taskData.tasks_fnid[task]], taskData.tasks_name[task], depends))
162 self.runq_fnid.append(taskData.tasks_fnid[task])
163 self.runq_task.append(taskData.tasks_name[task])
164 self.runq_depends.append(Set(depends))
165 self.runq_revdeps.append(Set())
166 self.runq_weight.append(0)
168 runq_weight1.append(0)
172 bb.msg.note(2, bb.msg.domain.RunQueue, "Marking Active Tasks")
174 def mark_active(listid, depth):
176 Mark an item as active along with its depends
177 (calls itself recursively)
180 if runq_build[listid] == 1:
183 runq_build[listid] = 1
185 depends = self.runq_depends[listid]
186 for depend in depends:
187 mark_active(depend, depth+1)
189 for target in targets:
190 targetid = taskData.getbuild_id(target[0])
191 if targetid in taskData.failed_deps:
194 if targetid not in taskData.build_targets:
197 fnid = taskData.build_targets[targetid][0]
198 if fnid in taskData.failed_fnids:
201 listid = taskData.tasks_lookup[fnid][target[1]]
203 mark_active(listid, 1)
205 # Prune inactive tasks
208 for listid in range(len(self.runq_fnid)):
209 if runq_build[listid-delcount] == 1:
210 maps.append(listid-delcount)
212 del self.runq_fnid[listid-delcount]
213 del self.runq_task[listid-delcount]
214 del self.runq_depends[listid-delcount]
215 del self.runq_weight[listid-delcount]
216 del runq_weight1[listid-delcount]
217 del runq_build[listid-delcount]
218 del runq_done[listid-delcount]
219 del self.runq_revdeps[listid-delcount]
220 delcount = delcount + 1
223 if len(self.runq_fnid) == 0:
224 if not taskData.abort:
225 bb.msg.note(1, bb.msg.domain.RunQueue, "All possible tasks have been run but build incomplete (--continue mode). See errors above for incomplete tasks.")
227 bb.msg.fatal(bb.msg.domain.RunQueue, "No active tasks and not in --continue mode?! Please report this bug.")
229 bb.msg.note(2, bb.msg.domain.RunQueue, "Pruned %s inactive tasks, %s left" % (delcount, len(self.runq_fnid)))
231 for listid in range(len(self.runq_fnid)):
233 origdeps = self.runq_depends[listid]
234 for origdep in origdeps:
235 if maps[origdep] == -1:
236 bb.msg.fatal(bb.msg.domain.RunQueue, "Invalid mapping - Should never happen!")
237 newdeps.append(maps[origdep])
238 self.runq_depends[listid] = Set(newdeps)
240 bb.msg.note(2, bb.msg.domain.RunQueue, "Assign Weightings")
242 for listid in range(len(self.runq_fnid)):
243 for dep in self.runq_depends[listid]:
244 self.runq_revdeps[dep].add(listid)
247 for listid in range(len(self.runq_fnid)):
248 revdeps = self.runq_revdeps[listid]
249 if len(revdeps) == 0:
250 runq_done[listid] = 1
251 self.runq_weight[listid] = 1
252 endpoints.append(listid)
254 if dep in self.runq_depends[listid]:
255 #self.dump_data(taskData)
256 bb.msg.fatal(bb.msg.domain.RunQueue, "Task %s (%s) has circular dependency on %s (%s)" % (taskData.fn_index[self.runq_fnid[dep]], self.runq_task[dep] , taskData.fn_index[self.runq_fnid[listid]], self.runq_task[listid]))
257 runq_weight1[listid] = len(revdeps)
259 bb.msg.note(2, bb.msg.domain.RunQueue, "Compute totals (have %s endpoint(s))" % len(endpoints))
263 for listid in endpoints:
264 for revdep in self.runq_depends[listid]:
265 self.runq_weight[revdep] = self.runq_weight[revdep] + self.runq_weight[listid]
266 runq_weight1[revdep] = runq_weight1[revdep] - 1
267 if runq_weight1[revdep] == 0:
268 next_points.append(revdep)
269 runq_done[revdep] = 1
270 endpoints = next_points
271 if len(next_points) == 0:
275 for task in range(len(self.runq_fnid)):
276 if runq_done[task] == 0:
279 def print_chain(taskid, finish):
281 for revdep in self.runq_revdeps[taskid]:
282 if runq_done[revdep] == 0 and revdep not in seen and not finish:
283 bb.msg.error(bb.msg.domain.RunQueue, "Task %s (%s) (depends: %s)" % (revdep, self.get_user_idstring(revdep, taskData), self.runq_depends[revdep]))
284 if revdep in deps_seen:
285 bb.msg.error(bb.msg.domain.RunQueue, "Chain ends at Task %s (%s)" % (revdep, self.get_user_idstring(revdep, taskData)))
288 for dep in self.runq_depends[revdep]:
289 deps_seen.append(dep)
290 print_chain(revdep, finish)
291 print_chain(task, False)
292 bb.msg.fatal(bb.msg.domain.RunQueue, "Task %s (%s) not processed!\nThis is probably a circular dependency (the chain might be printed above)." % (task, self.get_user_idstring(task, taskData)))
293 if runq_weight1[task] != 0:
294 bb.msg.fatal(bb.msg.domain.RunQueue, "Task %s (%s) count not zero!" % (task, self.get_user_idstring(task, taskData)))
296 # Make a weight sorted map
297 from copy import deepcopy
299 sortweight = deepcopy(self.runq_weight)
301 copyweight = deepcopy(self.runq_weight)
304 for weight in sortweight:
305 idx = copyweight.index(weight)
306 self.prio_map.append(idx)
308 self.prio_map.reverse()
310 #self.dump_data(taskData)
312 def execute_runqueue(self, cooker, cfgData, dataCache, taskData, runlist):
314 Run the tasks in a queue prepared by prepare_runqueue
315 Upon failure, optionally try to recover the build using any alternate providers
316 (if the abort on failure configuration option isn't set)
321 failed_fnids = self.execute_runqueue_internal(cooker, cfgData, dataCache, taskData)
322 if len(failed_fnids) == 0:
325 raise bb.runqueue.TaskFailure(failed_fnids)
326 for fnid in failed_fnids:
327 #print "Failure: %s %s %s" % (fnid, taskData.fn_index[fnid], self.runq_task[fnid])
328 taskData.fail_fnid(fnid)
329 failures = failures + 1
330 self.reset_runqueue()
331 self.prepare_runqueue(cfgData, dataCache, taskData, runlist)
333 def execute_runqueue_internal(self, cooker, cfgData, dataCache, taskData):
335 Run the tasks in a queue prepared by prepare_runqueue
339 bb.msg.note(1, bb.msg.domain.RunQueue, "Executing runqueue")
348 if len(self.runq_fnid) == 0:
352 def sigint_handler(signum, frame):
353 raise KeyboardInterrupt
355 def get_next_task(data):
357 Return the id of the highest priority task that is buildable
359 for task1 in range(len(data.runq_fnid)):
360 task = data.prio_map[task1]
361 if runq_running[task] == 1:
363 if runq_buildable[task] == 1:
367 def task_complete(data, task):
369 Mark a task as completed
370 Look at the reverse dependencies and mark any task with
371 completed dependencies as buildable
373 runq_complete[task] = 1
374 for revdep in data.runq_revdeps[task]:
375 if runq_running[revdep] == 1:
377 if runq_buildable[revdep] == 1:
380 for dep in data.runq_depends[revdep]:
381 if runq_complete[dep] != 1:
384 runq_buildable[revdep] = 1
385 fn = taskData.fn_index[self.runq_fnid[revdep]]
386 taskname = self.runq_task[revdep]
387 bb.msg.debug(1, bb.msg.domain.RunQueue, "Marking task %s (%s, %s) as buildable" % (revdep, fn, taskname))
389 # Mark initial buildable tasks
390 for task in range(len(self.runq_fnid)):
391 runq_running.append(0)
392 runq_complete.append(0)
393 if len(self.runq_depends[task]) == 0:
394 runq_buildable.append(1)
396 runq_buildable.append(0)
399 number_tasks = int(bb.data.getVar("BB_NUMBER_THREADS", cfgData) or 1)
403 task = get_next_task(self)
405 fn = taskData.fn_index[self.runq_fnid[task]]
406 taskname = self.runq_task[task]
408 if bb.build.stamp_is_current_cache(dataCache, fn, taskname):
409 targetid = taskData.gettask_id(fn, taskname)
410 if not (targetid in taskData.external_targets and cooker.configuration.force):
411 bb.msg.debug(2, bb.msg.domain.RunQueue, "Stamp current task %s (%s)" % (task, self.get_user_idstring(task, taskData)))
412 runq_running[task] = 1
413 task_complete(self, task)
416 bb.msg.debug(1, bb.msg.domain.RunQueue, "Running task %s (%s)" % (task, self.get_user_idstring(task, taskData)))
420 bb.msg.fatal(bb.msg.domain.RunQueue, "fork failed: %d (%s)" % (e.errno, e.strerror))
422 # Bypass finally below
424 # Stop Ctrl+C being sent to children
425 signal.signal(signal.SIGINT, signal.SIG_IGN)
426 sys.stdin = open('/dev/null', 'r')
427 cooker.configuration.cmd = taskname[3:]
429 cooker.tryBuild(fn, False)
430 except bb.build.EventException:
431 bb.msg.error(bb.msg.domain.Build, "Build of " + fn + " " + taskname + " failed")
434 bb.msg.error(bb.msg.domain.Build, "Build of " + fn + " " + taskname + " failed")
437 build_pids[pid] = task
438 runq_running[task] = 1
439 active_builds = active_builds + 1
440 if active_builds < number_tasks:
442 if active_builds > 0:
443 result = os.waitpid(-1, 0)
444 active_builds = active_builds - 1
445 task = build_pids[result[0]]
447 del build_pids[result[0]]
448 bb.msg.error(bb.msg.domain.RunQueue, "Task %s (%s) failed" % (task, self.get_user_idstring(task, taskData)))
449 failed_fnids.append(self.runq_fnid[task])
451 task_complete(self, task)
452 del build_pids[result[0]]
457 orig_builds = active_builds
458 while active_builds > 0:
459 bb.msg.note(1, bb.msg.domain.RunQueue, "Waiting for %s active tasks to finish" % active_builds)
461 for k, v in build_pids.iteritems():
462 bb.msg.note(1, bb.msg.domain.RunQueue, "%s: %s (%s)" % (tasknum, self.get_user_idstring(v, taskData), k))
463 tasknum = tasknum + 1
464 result = os.waitpid(-1, 0)
465 task = build_pids[result[0]]
467 bb.msg.error(bb.msg.domain.RunQueue, "Task %s (%s) failed" % (task, self.get_user_idstring(task, taskData)))
468 failed_fnids.append(self.runq_fnid[task])
469 del build_pids[result[0]]
470 active_builds = active_builds - 1
474 bb.msg.note(1, bb.msg.domain.RunQueue, "Sending SIGTERM to remaining %s tasks" % active_builds)
475 for k, v in build_pids.iteritems():
476 os.kill(k, signal.SIGTERM)
480 for task in range(len(self.runq_fnid)):
481 if runq_buildable[task] == 0:
482 bb.msg.error(bb.msg.domain.RunQueue, "Task %s never buildable!" % task)
483 if runq_running[task] == 0:
484 bb.msg.error(bb.msg.domain.RunQueue, "Task %s never ran!" % task)
485 if runq_complete[task] == 0:
486 bb.msg.error(bb.msg.domain.RunQueue, "Task %s never completed!" % task)
490 def dump_data(self, taskQueue):
492 Dump some debug information on the internal data structures
494 bb.msg.debug(3, bb.msg.domain.RunQueue, "run_tasks:")
495 for task in range(len(self.runq_fnid)):
496 bb.msg.debug(3, bb.msg.domain.RunQueue, " (%s)%s - %s: %s Deps %s RevDeps %s" % (task,
497 taskQueue.fn_index[self.runq_fnid[task]],
498 self.runq_task[task],
499 self.runq_weight[task],
500 self.runq_depends[task],
501 self.runq_revdeps[task]))
503 bb.msg.debug(3, bb.msg.domain.RunQueue, "sorted_tasks:")
504 for task1 in range(len(self.runq_fnid)):
505 if task1 in self.prio_map:
506 task = self.prio_map[task1]
507 bb.msg.debug(3, bb.msg.domain.RunQueue, " (%s)%s - %s: %s Deps %s RevDeps %s" % (task,
508 taskQueue.fn_index[self.runq_fnid[task]],
509 self.runq_task[task],
510 self.runq_weight[task],
511 self.runq_depends[task],
512 self.runq_revdeps[task]))