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"""
29 def __init__(self, fnid, fn, taskname):
30 self.args = fnid, fn, taskname
34 BitBake Run Queue implementation
39 def reset_runqueue(self):
42 self.runq_depends = []
43 self.runq_revdeps = []
47 def get_user_idstring(self, task, taskData):
48 fn = taskData.fn_index[self.runq_fnid[task]]
49 taskname = self.runq_task[task]
50 return "%s, %s" % (fn, taskname)
52 def prepare_runqueue(self, cfgData, dataCache, taskData, targets):
54 Turn a set of taskData into a RunQueue and compute data needed
55 to optimise the execution order.
56 targets is list of paired values - a provider name and the task to run
64 bb.msg.note(1, bb.msg.domain.RunQueue, "Preparing Runqueue")
66 for task in range(len(taskData.tasks_name)):
67 fnid = taskData.tasks_fnid[task]
68 fn = taskData.fn_index[fnid]
69 task_deps = dataCache.task_deps[fn]
71 if fnid not in taskData.failed_fnids:
73 depends = taskData.tasks_tdepends[task]
76 if 'deptask' in task_deps and taskData.tasks_name[task] in task_deps['deptask']:
77 taskname = task_deps['deptask'][taskData.tasks_name[task]]
78 for depid in taskData.depids[fnid]:
79 if depid in taskData.build_targets:
80 depdata = taskData.build_targets[depid][0]
82 dep = taskData.fn_index[depdata]
83 depends.append(taskData.gettask_id(dep, taskname))
85 # Resolve Runtime Depends
86 if 'rdeptask' in task_deps and taskData.tasks_name[task] in task_deps['rdeptask']:
87 taskname = task_deps['rdeptask'][taskData.tasks_name[task]]
88 for depid in taskData.rdepids[fnid]:
89 if depid in taskData.run_targets:
90 depdata = taskData.run_targets[depid][0]
92 dep = taskData.fn_index[depdata]
93 depends.append(taskData.gettask_id(dep, taskname))
95 def add_recursive_build(depid):
97 Add build depends of depid to depends
98 (if we've not see it before)
99 (calls itself recursively)
101 if str(depid) in dep_seen:
103 dep_seen.append(depid)
104 if depid in taskData.build_targets:
105 depdata = taskData.build_targets[depid][0]
107 dep = taskData.fn_index[depdata]
108 taskid = taskData.gettask_id(dep, taskname)
109 depends.append(taskid)
110 fnid = taskData.tasks_fnid[taskid]
111 for nextdepid in taskData.depids[fnid]:
112 if nextdepid not in dep_seen:
113 add_recursive_build(nextdepid)
114 for nextdepid in taskData.rdepids[fnid]:
115 if nextdepid not in rdep_seen:
116 add_recursive_run(nextdepid)
118 def add_recursive_run(rdepid):
120 Add runtime depends of rdepid to depends
121 (if we've not see it before)
122 (calls itself recursively)
124 if str(rdepid) in rdep_seen:
126 rdep_seen.append(rdepid)
127 if rdepid in taskData.run_targets:
128 depdata = taskData.run_targets[rdepid][0]
130 dep = taskData.fn_index[depdata]
131 taskid = taskData.gettask_id(dep, taskname)
132 depends.append(taskid)
133 fnid = taskData.tasks_fnid[taskid]
134 for nextdepid in taskData.depids[fnid]:
135 if nextdepid not in dep_seen:
136 add_recursive_build(nextdepid)
137 for nextdepid in taskData.rdepids[fnid]:
138 if nextdepid not in rdep_seen:
139 add_recursive_run(nextdepid)
142 # Resolve Recursive Runtime Depends
143 # Also includes all Build Depends (and their runtime depends)
144 if 'recrdeptask' in task_deps and taskData.tasks_name[task] in task_deps['recrdeptask']:
147 taskname = task_deps['recrdeptask'][taskData.tasks_name[task]]
148 for depid in taskData.depids[fnid]:
149 add_recursive_build(depid)
150 for rdepid in taskData.rdepids[fnid]:
151 add_recursive_run(rdepid)
153 #Prune self references
156 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))
163 self.runq_fnid.append(taskData.tasks_fnid[task])
164 self.runq_task.append(taskData.tasks_name[task])
165 self.runq_depends.append(Set(depends))
166 self.runq_revdeps.append(Set())
167 self.runq_weight.append(0)
169 runq_weight1.append(0)
173 bb.msg.note(2, bb.msg.domain.RunQueue, "Marking Active Tasks")
175 def mark_active(listid, depth):
177 Mark an item as active along with its depends
178 (calls itself recursively)
181 if runq_build[listid] == 1:
184 runq_build[listid] = 1
186 depends = self.runq_depends[listid]
187 for depend in depends:
188 mark_active(depend, depth+1)
190 for target in targets:
191 targetid = taskData.getbuild_id(target[0])
192 if targetid in taskData.failed_deps:
195 if targetid not in taskData.build_targets:
198 fnid = taskData.build_targets[targetid][0]
199 if fnid in taskData.failed_fnids:
202 fnids = taskData.matches_in_list(self.runq_fnid, fnid)
203 tasks = taskData.matches_in_list(self.runq_task, target[1])
205 listid = taskData.both_contain(fnids, tasks)
207 mark_active(listid, 1)
209 # Prune inactive tasks
212 for listid in range(len(self.runq_fnid)):
213 if runq_build[listid-delcount] == 1:
214 maps.append(listid-delcount)
216 del self.runq_fnid[listid-delcount]
217 del self.runq_task[listid-delcount]
218 del self.runq_depends[listid-delcount]
219 del self.runq_weight[listid-delcount]
220 del runq_weight1[listid-delcount]
221 del runq_build[listid-delcount]
222 del runq_done[listid-delcount]
223 del self.runq_revdeps[listid-delcount]
224 delcount = delcount + 1
227 if len(self.runq_fnid) == 0:
228 if not taskData.abort:
229 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.")
231 bb.msg.fatal(bb.msg.domain.RunQueue, "No active tasks and not in --continue mode?! Please report this bug.")
233 bb.msg.note(2, bb.msg.domain.RunQueue, "Pruned %s inactive tasks, %s left" % (delcount, len(self.runq_fnid)))
235 for listid in range(len(self.runq_fnid)):
237 origdeps = self.runq_depends[listid]
238 for origdep in origdeps:
239 if maps[origdep] == -1:
240 bb.msg.fatal(bb.msg.domain.RunQueue, "Invalid mapping - Should never happen!")
241 newdeps.append(maps[origdep])
242 self.runq_depends[listid] = Set(newdeps)
244 bb.msg.note(2, bb.msg.domain.RunQueue, "Assign Weightings")
246 for listid in range(len(self.runq_fnid)):
247 for dep in self.runq_depends[listid]:
248 self.runq_revdeps[dep].add(listid)
251 for listid in range(len(self.runq_fnid)):
252 revdeps = self.runq_revdeps[listid]
253 if len(revdeps) == 0:
254 runq_done[listid] = 1
255 self.runq_weight[listid] = 1
256 endpoints.append(listid)
258 if dep in self.runq_depends[listid]:
259 #self.dump_data(taskData)
260 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]))
261 runq_weight1[listid] = len(revdeps)
263 bb.msg.note(2, bb.msg.domain.RunQueue, "Compute totals (have %s endpoint(s))" % len(endpoints))
267 for listid in endpoints:
268 for revdep in self.runq_depends[listid]:
269 self.runq_weight[revdep] = self.runq_weight[revdep] + self.runq_weight[listid]
270 runq_weight1[revdep] = runq_weight1[revdep] - 1
271 if runq_weight1[revdep] == 0:
272 next_points.append(revdep)
273 runq_done[revdep] = 1
274 endpoints = next_points
275 if len(next_points) == 0:
279 for task in range(len(self.runq_fnid)):
280 if runq_done[task] == 0:
282 def print_chain(taskid):
284 for revdep in self.runq_revdeps[taskid]:
285 if runq_done[revdep] == 0:
286 bb.msg.error(bb.msg.domain.RunQueue, "Task %s (%s) (depends: %s)" % (revdep, self.get_user_idstring(revdep, taskData), self.runq_depends[revdep]))
287 if revdep not in seen:
290 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)))
291 if runq_weight1[task] != 0:
292 bb.msg.fatal(bb.msg.domain.RunQueue, "Task %s (%s) count not zero!" % (task, self.get_user_idstring(task, taskData)))
294 # Make a weight sorted map
295 from copy import deepcopy
297 sortweight = deepcopy(self.runq_weight)
299 copyweight = deepcopy(self.runq_weight)
302 for weight in sortweight:
303 idx = copyweight.index(weight)
304 self.prio_map.append(idx)
306 self.prio_map.reverse()
308 def execute_runqueue(self, cooker, cfgData, dataCache, taskData, runlist):
310 Run the tasks in a queue prepared by prepare_runqueue
311 Upon failure, optionally try to recover the build using any alternate providers
312 (if the abort on failure configuration option isn't set)
318 self.execute_runqueue_internal(cooker, cfgData, dataCache, taskData)
320 except bb.runqueue.TaskFailure, (fnid, taskData.fn_index[fnid], taskname):
323 taskData.fail_fnid(fnid)
324 self.reset_runqueue()
325 self.prepare_runqueue(cfgData, dataCache, taskData, runlist)
326 failures = failures + 1
328 def execute_runqueue_internal(self, cooker, cfgData, dataCache, taskData):
330 Run the tasks in a queue prepared by prepare_runqueue
333 bb.msg.note(1, bb.msg.domain.RunQueue, "Executing runqueue")
341 if len(self.runq_fnid) == 0:
345 def get_next_task(data):
347 Return the id of the highest priority task that is buildable
349 for task1 in range(len(data.runq_fnid)):
350 task = data.prio_map[task1]
351 if runq_running[task] == 1:
353 if runq_buildable[task] == 1:
357 def task_complete(data, task):
359 Mark a task as completed
360 Look at the reverse dependencies and mark any task with
361 completed dependencies as buildable
363 runq_complete[task] = 1
364 for revdep in data.runq_revdeps[task]:
365 if runq_running[revdep] == 1:
367 if runq_buildable[revdep] == 1:
370 for dep in data.runq_depends[revdep]:
371 if runq_complete[dep] != 1:
374 runq_buildable[revdep] = 1
375 fn = taskData.fn_index[self.runq_fnid[revdep]]
376 taskname = self.runq_task[revdep]
377 bb.msg.debug(1, bb.msg.domain.RunQueue, "Marking task %s (%s, %s) as buildable" % (revdep, fn, taskname))
379 # Mark initial buildable tasks
380 for task in range(len(self.runq_fnid)):
381 runq_running.append(0)
382 runq_complete.append(0)
383 if len(self.runq_depends[task]) == 0:
384 runq_buildable.append(1)
386 runq_buildable.append(0)
389 number_tasks = int(bb.data.getVar("BB_NUMBER_THREADS", cfgData) or 1)
393 task = get_next_task(self)
395 fn = taskData.fn_index[self.runq_fnid[task]]
396 taskname = self.runq_task[task]
398 if bb.build.stamp_is_current_cache(dataCache, fn, taskname):
399 targetid = taskData.gettask_id(fn, taskname)
400 if not (targetid in taskData.external_targets and cooker.configuration.force):
401 bb.msg.debug(2, bb.msg.domain.RunQueue, "Stamp current task %s (%s)" % (task, self.get_user_idstring(task, taskData)))
402 runq_running[task] = 1
403 task_complete(self, task)
406 bb.msg.debug(1, bb.msg.domain.RunQueue, "Running task %s (%s)" % (task, self.get_user_idstring(task, taskData)))
410 bb.msg.fatal(bb.msg.domain.RunQueue, "fork failed: %d (%s)" % (e.errno, e.strerror))
412 cooker.configuration.cmd = taskname[3:]
414 cooker.tryBuild(fn, False)
415 except bb.build.EventException:
416 bb.msg.error(bb.msg.domain.Build, "Build of " + fn + " " + taskname + " failed")
419 bb.msg.error(bb.msg.domain.Build, "Build of " + fn + " " + taskname + " failed")
422 build_pids[pid] = task
423 runq_running[task] = 1
424 active_builds = active_builds + 1
425 if active_builds < number_tasks:
427 if active_builds > 0:
428 result = os.waitpid(-1, 0)
429 active_builds = active_builds - 1
431 bb.msg.error(bb.msg.domain.RunQueue, "Task %s (%s) failed" % (build_pids[result[0]], self.get_user_idstring(build_pids[result[0]], taskData)))
432 raise bb.runqueue.TaskFailure(self.runq_fnid[build_pids[result[0]]], taskData.fn_index[self.runq_fnid[build_pids[result[0]]]], self.runq_task[build_pids[result[0]]])
433 task_complete(self, build_pids[result[0]])
434 del build_pids[result[0]]
440 bb.msg.error(bb.msg.domain.RunQueue, "Exception received")
441 if active_builds > 0:
442 while active_builds > 0:
443 bb.msg.note(1, bb.msg.domain.RunQueue, "Waiting for %s active tasks to finish" % active_builds)
445 for k, v in build_pids.iteritems():
446 bb.msg.note(1, bb.msg.domain.RunQueue, "%s: %s (%s)" % (tasknum, self.get_user_idstring(v, taskData), k))
447 tasknum = tasknum + 1
448 result = os.waitpid(-1, 0)
449 del build_pids[result[0]]
450 active_builds = active_builds - 1
454 for task in range(len(self.runq_fnid)):
455 if runq_buildable[task] == 0:
456 bb.msg.error(bb.msg.domain.RunQueue, "Task %s never buildable!" % task)
457 if runq_running[task] == 0:
458 bb.msg.error(bb.msg.domain.RunQueue, "Task %s never ran!" % task)
459 if runq_complete[task] == 0:
460 bb.msg.error(bb.msg.domain.RunQueue, "Task %s never completed!" % task)
464 def dump_data(self, taskQueue):
466 Dump some debug information on the internal data structures
468 bb.msg.debug(3, bb.msg.domain.RunQueue, "run_tasks:")
469 for task in range(len(self.runq_fnid)):
470 bb.msg.debug(3, bb.msg.domain.RunQueue, " (%s)%s - %s: %s Deps %s RevDeps %s" % (task,
471 taskQueue.fn_index[self.runq_fnid[task]],
472 self.runq_task[task],
473 self.runq_weight[task],
474 self.runq_depends[task],
475 self.runq_revdeps[task]))
477 bb.msg.debug(3, bb.msg.domain.RunQueue, "sorted_tasks:")
478 for task1 in range(len(self.runq_fnid)):
479 if task1 in self.prio_map:
480 task = self.prio_map[task1]
481 bb.msg.debug(3, bb.msg.domain.RunQueue, " (%s)%s - %s: %s Deps %s RevDeps %s" % (task,
482 taskQueue.fn_index[self.runq_fnid[task]],
483 self.runq_task[task],
484 self.runq_weight[task],
485 self.runq_depends[task],
486 self.runq_revdeps[task]))