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
25 class TaskFailure(Exception):
26 """Exception raised when a task in a runqueue fails"""
28 def __init__(self, fnid, taskname):
29 self.args = fnid, taskname
33 BitBake Run Queue implementation
38 def reset_runqueue(self):
41 self.runq_depends = []
42 self.runq_revdeps = []
46 def prepare_runqueue(self, cfgData, dataCache, taskData, targets):
48 Turn a set of taskData into a RunQueue and compute data needed
49 to optimise the execution order.
50 targets is list of paired values - a provider name and the task to run
57 bb.msg.note(1, bb.msg.domain.RunQueue, "Preparing Runqueue")
59 for task in range(len(taskData.tasks_name)):
60 fnid = taskData.tasks_fnid[task]
61 fn = taskData.fn_index[fnid]
62 task_deps = dataCache.task_deps[fn]
64 if fnid not in taskData.failed_fnids:
66 depends = taskData.tasks_tdepends[task]
69 if 'deptask' in task_deps and taskData.tasks_name[task] in task_deps['deptask']:
70 taskname = task_deps['deptask'][taskData.tasks_name[task]]
71 for depid in taskData.depids[fnid]:
72 if depid in taskData.build_targets:
73 depdata = taskData.build_targets[depid][0]
75 dep = taskData.fn_index[depdata]
76 depends.append(taskData.gettask_id(dep, taskname))
78 # Resolve Runtime Depends
79 if 'rdeptask' in task_deps and taskData.tasks_name[task] in task_deps['rdeptask']:
80 taskname = task_deps['rdeptask'][taskData.tasks_name[task]]
81 for depid in taskData.rdepids[fnid]:
82 if depid in taskData.run_targets:
83 depdata = taskData.run_targets[depid][0]
85 dep = taskData.fn_index[depdata]
86 depends.append(taskData.gettask_id(dep, taskname))
88 def add_recursive_run(rdepid):
90 Add runtime depends of rdepid to depends, if
91 we've not see it before
92 (calls itself recursively)
94 if str(rdepid) in rdep_seen:
96 rdep_seen.append(rdepid)
97 if rdepid in taskData.run_targets:
98 depdata = taskData.run_targets[rdepid][0]
100 dep = taskData.fn_index[depdata]
101 taskid = taskData.gettask_id(dep, taskname)
102 depends.append(taskid)
103 fnid = taskData.tasks_fnid[taskid]
104 for nextdepid in taskData.rdepids[fnid]:
105 if nextdepid not in rdep_seen:
106 add_recursive_run(nextdepid)
108 # Resolve Recursive Runtime Depends
109 if 'recrdeptask' in task_deps and taskData.tasks_name[task] in task_deps['recrdeptask']:
111 taskname = task_deps['recrdeptask'][taskData.tasks_name[task]]
112 for rdepid in taskData.rdepids[fnid]:
113 add_recursive_run(rdepid)
115 #Prune self references
118 bb.debug(2, "Task %s (%s %s) contains self reference! %s" % (task, taskData.fn_index[taskData.tasks_fnid[task]], taskData.tasks_name[task], depends))
125 self.runq_fnid.append(taskData.tasks_fnid[task])
126 self.runq_task.append(taskData.tasks_name[task])
127 self.runq_depends.append(depends)
128 self.runq_revdeps.append([])
129 self.runq_weight.append(0)
131 runq_weight1.append(0)
135 bb.msg.note(2, bb.msg.domain.RunQueue, "Marking Active Tasks")
137 def mark_active(listid, depth):
139 Mark an item as active along with its depends
140 (calls itself recursively)
143 if runq_build[listid] == 1:
146 runq_build[listid] = 1
148 depends = self.runq_depends[listid]
149 for depend in depends:
150 mark_active(depend, depth+1)
152 for target in targets:
153 targetid = taskData.getbuild_id(target[0])
154 if targetid in taskData.failed_deps:
157 fnid = taskData.build_targets[targetid][0]
158 if fnid in taskData.failed_fnids:
161 fnids = taskData.matches_in_list(self.runq_fnid, fnid)
162 tasks = taskData.matches_in_list(self.runq_task, target[1])
164 listid = taskData.both_contain(fnids, tasks)
166 mark_active(listid, 1)
168 # Prune inactive tasks
171 for listid in range(len(self.runq_fnid)):
172 if runq_build[listid-delcount] == 1:
173 maps.append(listid-delcount)
175 del self.runq_fnid[listid-delcount]
176 del self.runq_task[listid-delcount]
177 del self.runq_depends[listid-delcount]
178 del self.runq_weight[listid-delcount]
179 del runq_weight1[listid-delcount]
180 del runq_build[listid-delcount]
181 del runq_done[listid-delcount]
182 del self.runq_revdeps[listid-delcount]
183 delcount = delcount + 1
186 bb.msg.note(2, bb.msg.domain.RunQueue, "Pruned %s inactive tasks, %s left" % (delcount, len(self.runq_fnid)))
188 for listid in range(len(self.runq_fnid)):
190 origdeps = self.runq_depends[listid]
191 for origdep in origdeps:
192 if maps[origdep] == -1:
193 bb.fatal(bb.msg.domain.RunQueue, "Invalid mapping - Should never happen!")
194 newdeps.append(maps[origdep])
195 self.runq_depends[listid] = newdeps
197 bb.msg.note(2, bb.msg.domain.RunQueue, "Assign Weightings")
199 for listid in range(len(self.runq_fnid)):
200 for dep in self.runq_depends[listid]:
201 if dep not in self.runq_revdeps[dep]:
202 self.runq_revdeps[dep].append(listid)
205 for listid in range(len(self.runq_fnid)):
206 revdeps = self.runq_revdeps[listid]
207 if len(revdeps) == 0:
208 runq_done[listid] = 1
209 self.runq_weight[listid] = 1
210 endpoints.append(listid)
212 if dep in self.runq_depends[listid]:
213 self.dump_data(taskData)
214 bb.fatal("Task %s has circular dependency on %s" % (dep, listid))
215 runq_weight1[listid] = len(revdeps)
217 bb.msg.note(2, bb.msg.domain.RunQueue, "Compute totals (have %s endpoint(s))" % len(endpoints))
221 for listid in endpoints:
222 for revdep in self.runq_depends[listid]:
223 self.runq_weight[revdep] = self.runq_weight[revdep] + self.runq_weight[listid]
224 runq_weight1[revdep] = runq_weight1[revdep] - 1
225 if runq_weight1[revdep] == 0:
226 next_points.append(revdep)
227 runq_done[revdep] = 1
228 endpoints = next_points
229 if len(next_points) == 0:
233 for task in range(len(self.runq_fnid)):
234 if runq_done[task] == 0:
235 bb.fatal("Task %s not processed!" % task)
236 if runq_weight1[task] != 0:
237 bb.fatal("Task %s count not zero!" % task)
239 # Make a weight sorted map
240 from copy import deepcopy
242 sortweight = deepcopy(self.runq_weight)
244 copyweight = deepcopy(self.runq_weight)
247 for weight in sortweight:
248 idx = copyweight.index(weight)
249 self.prio_map.append(idx)
251 self.prio_map.reverse()
253 def execute_runqueue(self, cooker, cfgData, dataCache, taskData, runlist):
255 Run the tasks in a queue prepared by prepare_runqueue
256 Upon failure, optionally try to recover the build using any alternate providers
257 (if the abort on failure configuration option isn't set)
263 self.execute_runqueue_internal(cooker, cfgData, dataCache, taskData)
265 except bb.runqueue.TaskFailure, (fnid, taskname):
266 taskData.fail_fnid(fnid)
267 self.reset_runqueue()
268 self.prepare_runqueue(cfgData, dataCache, taskData, runlist)
269 failures = failures + 1
271 def execute_runqueue_internal(self, cooker, cfgData, dataCache, taskData):
273 Run the tasks in a queue prepared by prepare_runqueue
276 bb.msg.note(1, bb.msg.domain.RunQueue, "Executing runqueue")
284 def get_next_task(data):
286 Return the id of the highest priority task that is buildable
288 for task1 in range(len(data.runq_fnid)):
289 task = data.prio_map[task1]
290 if runq_running[task] == 1:
292 if runq_buildable[task] == 1:
296 def task_complete(data, task):
298 Mark a task as completed
299 Look at the reverse dependencies and mark any task with
300 completed dependencies as buildable
302 runq_complete[task] = 1
303 for revdep in data.runq_revdeps[task]:
304 if runq_running[revdep] == 1:
306 if runq_buildable[revdep] == 1:
309 for dep in data.runq_depends[revdep]:
310 if runq_complete[dep] != 1:
313 runq_buildable[revdep] = 1
314 fn = taskData.fn_index[self.runq_fnid[revdep]]
315 taskname = self.runq_task[revdep]
316 bb.msg.debug(1, bb.msg.domain.RunQueue, "Marking task %s (%s, %s) as buildable" % (revdep, fn, taskname))
318 # Mark initial buildable tasks
319 for task in range(len(self.runq_fnid)):
320 runq_running.append(0)
321 runq_complete.append(0)
322 if len(self.runq_depends[task]) == 0:
323 runq_buildable.append(1)
325 runq_buildable.append(0)
327 def get_user_idstring(task):
328 fn = taskData.fn_index[self.runq_fnid[task]]
329 taskname = self.runq_task[task]
330 return "%s, %s" % (fn, taskname)
332 number_tasks = bb.data.getVar("BB_NUMBER_THREADS", cfgData)
338 task = get_next_task(self)
340 fn = taskData.fn_index[self.runq_fnid[task]]
341 taskname = self.runq_task[task]
343 if bb.build.stamp_is_current_cache(dataCache, fn, taskname):
344 bb.msg.debug(2, bb.msg.domain.RunQueue, "Stamp current task %s (%s)" % (task, get_user_idstring(task)))
345 runq_running[task] = 1
346 task_complete(self, task)
349 bb.msg.debug(1, bb.msg.domain.RunQueue, "Running task %s (%s)" % (task, get_user_idstring(task)))
353 bb.msg.fatal(bb.msg.domain.RunQueue, "fork failed: %d (%s)" % (e.errno, e.strerror))
355 cooker.configuration.cmd = taskname[3:]
357 cooker.tryBuild(fn, False)
358 except bb.build.EventException, e:
359 bb.msg.error(bb.msg.domain.Build, "Build of " + fn + " " + taskname + " failed")
364 build_pids[pid] = task
365 runq_running[task] = 1
366 active_builds = active_builds + 1
367 if active_builds < number_tasks:
369 if active_builds > 0:
370 result = os.waitpid(-1, 0)
371 active_builds = active_builds - 1
373 bb.msg.error(bb.msg.domain.RunQueue, "Task %s (%s) failed" % (build_pids[result[0]], get_user_idstring(build_pids[result[0]])))
374 raise bb.runqueue.TaskFailure(self.runq_fnid[build_pids[result[0]]], self.runq_task[build_pids[result[0]]])
375 task_complete(self, build_pids[result[0]])
381 bb.error("Exception received")
382 if active_builds > 0:
383 while active_builds > 0:
384 bb.note("Waiting for %s active tasks to finish" % active_builds)
386 active_builds = active_builds - 1
387 if cooker.configuration.abort:
392 for task in range(len(self.runq_fnid)):
393 if runq_buildable[task] == 0:
394 bb.msg.error(bb.msg.domain.RunQueue, "Task %s never buildable!" % task)
395 if runq_running[task] == 0:
396 bb.msg.error(bb.msg.domain.RunQueue, "Task %s never ran!" % task)
397 if runq_complete[task] == 0:
398 bb.msg.error(bb.msg.domain.RunQueue, "Task %s never completed!" % task)
402 def dump_data(self, taskQueue):
404 Dump some debug information on the internal data structures
406 bb.msg.debug(3, bb.msg.domain.RunQueue, "run_tasks:")
407 for task in range(len(self.runq_fnid)):
408 bb.msg.debug(3, bb.msg.domain.RunQueue, " (%s)%s - %s: %s Deps %s RevDeps %s" % (task,
409 taskQueue.fn_index[self.runq_fnid[task]],
410 self.runq_task[task],
411 self.runq_weight[task],
412 self.runq_depends[task],
413 self.runq_revdeps[task]))
415 bb.msg.debug(3, bb.msg.domain.RunQueue, "sorted_tasks:")
416 for task1 in range(len(self.runq_fnid)):
417 task = self.prio_map[task1]
418 bb.msg.debug(3, bb.msg.domain.RunQueue, " (%s)%s - %s: %s Deps %s RevDeps %s" % (task,
419 taskQueue.fn_index[self.runq_fnid[task]],
420 self.runq_task[task],
421 self.runq_weight[task],
422 self.runq_depends[task],
423 self.runq_revdeps[task]))