runqueue.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. #!/usr/bin/env python
  2. # ex:ts=4:sw=4:sts=4:et
  3. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  4. """
  5. BitBake 'RunQueue' implementation
  6. Handles preparation and execution of a queue of tasks
  7. """
  8. # Copyright (C) 2006 Richard Purdie
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License version 2 as
  12. # published by the Free Software Foundation.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. from bb import msg, data, fetch, event, mkdirhier, utils
  23. from sets import Set
  24. import bb, os, sys
  25. class TaskFailure(Exception):
  26. """Exception raised when a task in a runqueue fails"""
  27. def __init__(self, x):
  28. self.args = x
  29. class RunQueue:
  30. """
  31. BitBake Run Queue implementation
  32. """
  33. def __init__(self):
  34. self.reset_runqueue()
  35. def reset_runqueue(self):
  36. self.runq_fnid = []
  37. self.runq_task = []
  38. self.runq_depends = []
  39. self.runq_revdeps = []
  40. self.runq_weight = []
  41. self.prio_map = []
  42. def get_user_idstring(self, task, taskData):
  43. fn = taskData.fn_index[self.runq_fnid[task]]
  44. taskname = self.runq_task[task]
  45. return "%s, %s" % (fn, taskname)
  46. def prepare_runqueue(self, cooker, cfgData, dataCache, taskData, targets):
  47. """
  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
  51. """
  52. depends = []
  53. runq_weight1 = []
  54. runq_build = []
  55. runq_done = []
  56. bb.msg.note(1, bb.msg.domain.RunQueue, "Preparing Runqueue")
  57. for task in range(len(taskData.tasks_name)):
  58. fnid = taskData.tasks_fnid[task]
  59. fn = taskData.fn_index[fnid]
  60. task_deps = dataCache.task_deps[fn]
  61. if fnid not in taskData.failed_fnids:
  62. depends = taskData.tasks_tdepends[task]
  63. # Resolve Depends
  64. if 'deptask' in task_deps and taskData.tasks_name[task] in task_deps['deptask']:
  65. taskname = task_deps['deptask'][taskData.tasks_name[task]]
  66. for depid in taskData.depids[fnid]:
  67. if depid in taskData.build_targets:
  68. depdata = taskData.build_targets[depid][0]
  69. if depdata:
  70. dep = taskData.fn_index[depdata]
  71. depends.append(taskData.gettask_id(dep, taskname))
  72. # Resolve Runtime Depends
  73. if 'rdeptask' in task_deps and taskData.tasks_name[task] in task_deps['rdeptask']:
  74. taskname = task_deps['rdeptask'][taskData.tasks_name[task]]
  75. for depid in taskData.rdepids[fnid]:
  76. if depid in taskData.run_targets:
  77. depdata = taskData.run_targets[depid][0]
  78. if depdata:
  79. dep = taskData.fn_index[depdata]
  80. depends.append(taskData.gettask_id(dep, taskname))
  81. def add_recursive_build(depid):
  82. """
  83. Add build depends of depid to depends
  84. (if we've not see it before)
  85. (calls itself recursively)
  86. """
  87. if str(depid) in dep_seen:
  88. return
  89. dep_seen.append(depid)
  90. if depid in taskData.build_targets:
  91. depdata = taskData.build_targets[depid][0]
  92. if depdata:
  93. dep = taskData.fn_index[depdata]
  94. # Need to avoid creating new tasks here
  95. taskid = taskData.gettask_id(dep, taskname, False)
  96. if taskid:
  97. depends.append(taskid)
  98. fnid = taskData.tasks_fnid[taskid]
  99. else:
  100. fnid = taskData.getfn_id(dep)
  101. for nextdepid in taskData.depids[fnid]:
  102. if nextdepid not in dep_seen:
  103. add_recursive_build(nextdepid)
  104. for nextdepid in taskData.rdepids[fnid]:
  105. if nextdepid not in rdep_seen:
  106. add_recursive_run(nextdepid)
  107. def add_recursive_run(rdepid):
  108. """
  109. Add runtime depends of rdepid to depends
  110. (if we've not see it before)
  111. (calls itself recursively)
  112. """
  113. if str(rdepid) in rdep_seen:
  114. return
  115. rdep_seen.append(rdepid)
  116. if rdepid in taskData.run_targets:
  117. depdata = taskData.run_targets[rdepid][0]
  118. if depdata:
  119. dep = taskData.fn_index[depdata]
  120. # Need to avoid creating new tasks here
  121. taskid = taskData.gettask_id(dep, taskname, False)
  122. if taskid:
  123. depends.append(taskid)
  124. fnid = taskData.tasks_fnid[taskid]
  125. else:
  126. fnid = taskData.getfn_id(dep)
  127. for nextdepid in taskData.depids[fnid]:
  128. if nextdepid not in dep_seen:
  129. add_recursive_build(nextdepid)
  130. for nextdepid in taskData.rdepids[fnid]:
  131. if nextdepid not in rdep_seen:
  132. add_recursive_run(nextdepid)
  133. # Resolve Recursive Runtime Depends
  134. # Also includes all Build Depends (and their runtime depends)
  135. if 'recrdeptask' in task_deps and taskData.tasks_name[task] in task_deps['recrdeptask']:
  136. dep_seen = []
  137. rdep_seen = []
  138. for taskname in task_deps['recrdeptask'][taskData.tasks_name[task]].split():
  139. for depid in taskData.depids[fnid]:
  140. add_recursive_build(depid)
  141. for rdepid in taskData.rdepids[fnid]:
  142. add_recursive_run(rdepid)
  143. #Prune self references
  144. if task in depends:
  145. newdep = []
  146. 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))
  147. for dep in depends:
  148. if task != dep:
  149. newdep.append(dep)
  150. depends = newdep
  151. self.runq_fnid.append(taskData.tasks_fnid[task])
  152. self.runq_task.append(taskData.tasks_name[task])
  153. self.runq_depends.append(Set(depends))
  154. self.runq_revdeps.append(Set())
  155. self.runq_weight.append(0)
  156. runq_weight1.append(0)
  157. runq_build.append(0)
  158. runq_done.append(0)
  159. bb.msg.note(2, bb.msg.domain.RunQueue, "Marking Active Tasks")
  160. def mark_active(listid, depth):
  161. """
  162. Mark an item as active along with its depends
  163. (calls itself recursively)
  164. """
  165. if runq_build[listid] == 1:
  166. return
  167. runq_build[listid] = 1
  168. depends = self.runq_depends[listid]
  169. for depend in depends:
  170. mark_active(depend, depth+1)
  171. for target in targets:
  172. targetid = taskData.getbuild_id(target[0])
  173. if targetid not in taskData.build_targets:
  174. continue
  175. fnid = taskData.build_targets[targetid][0]
  176. # Remove stamps for targets if force mode active
  177. if cooker.configuration.force:
  178. fn = taskData.fn_index[fnid]
  179. bb.msg.note(2, bb.msg.domain.RunQueue, "Remove stamp %s, %s" % (target[1], fn))
  180. bb.build.del_stamp(target[1], dataCache, fn)
  181. if targetid in taskData.failed_deps:
  182. continue
  183. if fnid in taskData.failed_fnids:
  184. continue
  185. listid = taskData.tasks_lookup[fnid][target[1]]
  186. mark_active(listid, 1)
  187. # Prune inactive tasks
  188. maps = []
  189. delcount = 0
  190. for listid in range(len(self.runq_fnid)):
  191. if runq_build[listid-delcount] == 1:
  192. maps.append(listid-delcount)
  193. else:
  194. del self.runq_fnid[listid-delcount]
  195. del self.runq_task[listid-delcount]
  196. del self.runq_depends[listid-delcount]
  197. del self.runq_weight[listid-delcount]
  198. del runq_weight1[listid-delcount]
  199. del runq_build[listid-delcount]
  200. del runq_done[listid-delcount]
  201. del self.runq_revdeps[listid-delcount]
  202. delcount = delcount + 1
  203. maps.append(-1)
  204. if len(self.runq_fnid) == 0:
  205. if not taskData.abort:
  206. 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.")
  207. return
  208. bb.msg.fatal(bb.msg.domain.RunQueue, "No active tasks and not in --continue mode?! Please report this bug.")
  209. bb.msg.note(2, bb.msg.domain.RunQueue, "Pruned %s inactive tasks, %s left" % (delcount, len(self.runq_fnid)))
  210. for listid in range(len(self.runq_fnid)):
  211. newdeps = []
  212. origdeps = self.runq_depends[listid]
  213. for origdep in origdeps:
  214. if maps[origdep] == -1:
  215. bb.msg.fatal(bb.msg.domain.RunQueue, "Invalid mapping - Should never happen!")
  216. newdeps.append(maps[origdep])
  217. self.runq_depends[listid] = Set(newdeps)
  218. bb.msg.note(2, bb.msg.domain.RunQueue, "Assign Weightings")
  219. for listid in range(len(self.runq_fnid)):
  220. for dep in self.runq_depends[listid]:
  221. self.runq_revdeps[dep].add(listid)
  222. endpoints = []
  223. for listid in range(len(self.runq_fnid)):
  224. revdeps = self.runq_revdeps[listid]
  225. if len(revdeps) == 0:
  226. runq_done[listid] = 1
  227. self.runq_weight[listid] = 1
  228. endpoints.append(listid)
  229. for dep in revdeps:
  230. if dep in self.runq_depends[listid]:
  231. #self.dump_data(taskData)
  232. 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]))
  233. runq_weight1[listid] = len(revdeps)
  234. bb.msg.note(2, bb.msg.domain.RunQueue, "Compute totals (have %s endpoint(s))" % len(endpoints))
  235. while 1:
  236. next_points = []
  237. for listid in endpoints:
  238. for revdep in self.runq_depends[listid]:
  239. self.runq_weight[revdep] = self.runq_weight[revdep] + self.runq_weight[listid]
  240. runq_weight1[revdep] = runq_weight1[revdep] - 1
  241. if runq_weight1[revdep] == 0:
  242. next_points.append(revdep)
  243. runq_done[revdep] = 1
  244. endpoints = next_points
  245. if len(next_points) == 0:
  246. break
  247. # Sanity Checks
  248. for task in range(len(self.runq_fnid)):
  249. if runq_done[task] == 0:
  250. seen = []
  251. deps_seen = []
  252. def print_chain(taskid, finish):
  253. seen.append(taskid)
  254. for revdep in self.runq_revdeps[taskid]:
  255. if runq_done[revdep] == 0 and revdep not in seen and not finish:
  256. bb.msg.error(bb.msg.domain.RunQueue, "Task %s (%s) (depends: %s)" % (revdep, self.get_user_idstring(revdep, taskData), self.runq_depends[revdep]))
  257. if revdep in deps_seen:
  258. bb.msg.error(bb.msg.domain.RunQueue, "Chain ends at Task %s (%s)" % (revdep, self.get_user_idstring(revdep, taskData)))
  259. finish = True
  260. return
  261. for dep in self.runq_depends[revdep]:
  262. deps_seen.append(dep)
  263. print_chain(revdep, finish)
  264. print_chain(task, False)
  265. 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)))
  266. if runq_weight1[task] != 0:
  267. bb.msg.fatal(bb.msg.domain.RunQueue, "Task %s (%s) count not zero!" % (task, self.get_user_idstring(task, taskData)))
  268. # Make a weight sorted map
  269. from copy import deepcopy
  270. sortweight = deepcopy(self.runq_weight)
  271. sortweight.sort()
  272. copyweight = deepcopy(self.runq_weight)
  273. self.prio_map = []
  274. for weight in sortweight:
  275. idx = copyweight.index(weight)
  276. self.prio_map.append(idx)
  277. copyweight[idx] = -1
  278. self.prio_map.reverse()
  279. #self.dump_data(taskData)
  280. def execute_runqueue(self, cooker, cfgData, dataCache, taskData, runlist):
  281. """
  282. Run the tasks in a queue prepared by prepare_runqueue
  283. Upon failure, optionally try to recover the build using any alternate providers
  284. (if the abort on failure configuration option isn't set)
  285. """
  286. failures = 0
  287. while 1:
  288. failed_fnids = self.execute_runqueue_internal(cooker, cfgData, dataCache, taskData)
  289. if len(failed_fnids) == 0:
  290. return failures
  291. if taskData.abort:
  292. raise bb.runqueue.TaskFailure(failed_fnids)
  293. for fnid in failed_fnids:
  294. #print "Failure: %s %s %s" % (fnid, taskData.fn_index[fnid], self.runq_task[fnid])
  295. taskData.fail_fnid(fnid)
  296. failures = failures + 1
  297. self.reset_runqueue()
  298. self.prepare_runqueue(cfgData, dataCache, taskData, runlist)
  299. def execute_runqueue_internal(self, cooker, cfgData, dataCache, taskData):
  300. """
  301. Run the tasks in a queue prepared by prepare_runqueue
  302. """
  303. import signal
  304. bb.msg.note(1, bb.msg.domain.RunQueue, "Executing runqueue")
  305. active_builds = 0
  306. tasks_completed = 0
  307. tasks_skipped = 0
  308. runq_buildable = []
  309. runq_running = []
  310. runq_complete = []
  311. build_pids = {}
  312. failed_fnids = []
  313. if len(self.runq_fnid) == 0:
  314. # nothing to do
  315. return
  316. def sigint_handler(signum, frame):
  317. raise KeyboardInterrupt
  318. def get_next_task(data):
  319. """
  320. Return the id of the highest priority task that is buildable
  321. """
  322. for task1 in range(len(data.runq_fnid)):
  323. task = data.prio_map[task1]
  324. if runq_running[task] == 1:
  325. continue
  326. if runq_buildable[task] == 1:
  327. return task
  328. return None
  329. def task_complete(data, task):
  330. """
  331. Mark a task as completed
  332. Look at the reverse dependencies and mark any task with
  333. completed dependencies as buildable
  334. """
  335. runq_complete[task] = 1
  336. for revdep in data.runq_revdeps[task]:
  337. if runq_running[revdep] == 1:
  338. continue
  339. if runq_buildable[revdep] == 1:
  340. continue
  341. alldeps = 1
  342. for dep in data.runq_depends[revdep]:
  343. if runq_complete[dep] != 1:
  344. alldeps = 0
  345. if alldeps == 1:
  346. runq_buildable[revdep] = 1
  347. fn = taskData.fn_index[self.runq_fnid[revdep]]
  348. taskname = self.runq_task[revdep]
  349. bb.msg.debug(1, bb.msg.domain.RunQueue, "Marking task %s (%s, %s) as buildable" % (revdep, fn, taskname))
  350. # Mark initial buildable tasks
  351. for task in range(len(self.runq_fnid)):
  352. runq_running.append(0)
  353. runq_complete.append(0)
  354. if len(self.runq_depends[task]) == 0:
  355. runq_buildable.append(1)
  356. else:
  357. runq_buildable.append(0)
  358. number_tasks = int(bb.data.getVar("BB_NUMBER_THREADS", cfgData) or 1)
  359. try:
  360. while 1:
  361. task = get_next_task(self)
  362. if task is not None:
  363. fn = taskData.fn_index[self.runq_fnid[task]]
  364. taskname = self.runq_task[task]
  365. if bb.build.stamp_is_current(taskname, dataCache, fn):
  366. bb.msg.debug(2, bb.msg.domain.RunQueue, "Stamp current task %s (%s)" % (task, self.get_user_idstring(task, taskData)))
  367. runq_running[task] = 1
  368. task_complete(self, task)
  369. tasks_completed = tasks_completed + 1
  370. tasks_skipped = tasks_skipped + 1
  371. continue
  372. bb.msg.note(1, bb.msg.domain.RunQueue, "Running task %d of %d (ID: %s, %s)" % (tasks_completed + active_builds + 1, len(self.runq_fnid), task, self.get_user_idstring(task, taskData)))
  373. try:
  374. pid = os.fork()
  375. except OSError, e:
  376. bb.msg.fatal(bb.msg.domain.RunQueue, "fork failed: %d (%s)" % (e.errno, e.strerror))
  377. if pid == 0:
  378. # Bypass finally below
  379. active_builds = 0
  380. # Stop Ctrl+C being sent to children
  381. # signal.signal(signal.SIGINT, signal.SIG_IGN)
  382. # Make the child the process group leader
  383. os.setpgid(0, 0)
  384. sys.stdin = open('/dev/null', 'r')
  385. cooker.configuration.cmd = taskname[3:]
  386. try:
  387. cooker.tryBuild(fn, False)
  388. except bb.build.EventException:
  389. bb.msg.error(bb.msg.domain.Build, "Build of " + fn + " " + taskname + " failed")
  390. sys.exit(1)
  391. except:
  392. bb.msg.error(bb.msg.domain.Build, "Build of " + fn + " " + taskname + " failed")
  393. raise
  394. sys.exit(0)
  395. build_pids[pid] = task
  396. runq_running[task] = 1
  397. active_builds = active_builds + 1
  398. if active_builds < number_tasks:
  399. continue
  400. if active_builds > 0:
  401. result = os.waitpid(-1, 0)
  402. active_builds = active_builds - 1
  403. task = build_pids[result[0]]
  404. if result[1] != 0:
  405. del build_pids[result[0]]
  406. bb.msg.error(bb.msg.domain.RunQueue, "Task %s (%s) failed" % (task, self.get_user_idstring(task, taskData)))
  407. failed_fnids.append(self.runq_fnid[task])
  408. break
  409. task_complete(self, task)
  410. tasks_completed = tasks_completed + 1
  411. del build_pids[result[0]]
  412. continue
  413. break
  414. finally:
  415. try:
  416. while active_builds > 0:
  417. bb.msg.note(1, bb.msg.domain.RunQueue, "Waiting for %s active tasks to finish" % active_builds)
  418. tasknum = 1
  419. for k, v in build_pids.iteritems():
  420. bb.msg.note(1, bb.msg.domain.RunQueue, "%s: %s (%s)" % (tasknum, self.get_user_idstring(v, taskData), k))
  421. tasknum = tasknum + 1
  422. result = os.waitpid(-1, 0)
  423. task = build_pids[result[0]]
  424. if result[1] != 0:
  425. bb.msg.error(bb.msg.domain.RunQueue, "Task %s (%s) failed" % (task, self.get_user_idstring(task, taskData)))
  426. failed_fnids.append(self.runq_fnid[task])
  427. del build_pids[result[0]]
  428. active_builds = active_builds - 1
  429. if len(failed_fnids) > 0:
  430. return failed_fnids
  431. except:
  432. bb.msg.note(1, bb.msg.domain.RunQueue, "Sending SIGINT to remaining %s tasks" % active_builds)
  433. for k, v in build_pids.iteritems():
  434. os.kill(-k, signal.SIGINT)
  435. raise
  436. # Sanity Checks
  437. for task in range(len(self.runq_fnid)):
  438. if runq_buildable[task] == 0:
  439. bb.msg.error(bb.msg.domain.RunQueue, "Task %s never buildable!" % task)
  440. if runq_running[task] == 0:
  441. bb.msg.error(bb.msg.domain.RunQueue, "Task %s never ran!" % task)
  442. if runq_complete[task] == 0:
  443. bb.msg.error(bb.msg.domain.RunQueue, "Task %s never completed!" % task)
  444. bb.msg.note(1, bb.msg.domain.RunQueue, "Tasks Summary: Attempted %d tasks of which %d didn't need to be rerun and %d failed." % (tasks_completed, tasks_skipped, len(failed_fnids)))
  445. return failed_fnids
  446. def dump_data(self, taskQueue):
  447. """
  448. Dump some debug information on the internal data structures
  449. """
  450. bb.msg.debug(3, bb.msg.domain.RunQueue, "run_tasks:")
  451. for task in range(len(self.runq_fnid)):
  452. bb.msg.debug(3, bb.msg.domain.RunQueue, " (%s)%s - %s: %s Deps %s RevDeps %s" % (task,
  453. taskQueue.fn_index[self.runq_fnid[task]],
  454. self.runq_task[task],
  455. self.runq_weight[task],
  456. self.runq_depends[task],
  457. self.runq_revdeps[task]))
  458. bb.msg.debug(3, bb.msg.domain.RunQueue, "sorted_tasks:")
  459. for task1 in range(len(self.runq_fnid)):
  460. if task1 in self.prio_map:
  461. task = self.prio_map[task1]
  462. bb.msg.debug(3, bb.msg.domain.RunQueue, " (%s)%s - %s: %s Deps %s RevDeps %s" % (task,
  463. taskQueue.fn_index[self.runq_fnid[task]],
  464. self.runq_task[task],
  465. self.runq_weight[task],
  466. self.runq_depends[task],
  467. self.runq_revdeps[task]))