Runner.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2005-2018 (ita)
  4. """
  5. Runner.py: Task scheduling and execution
  6. """
  7. import heapq, traceback
  8. try:
  9. from queue import Queue, PriorityQueue
  10. except ImportError:
  11. from Queue import Queue
  12. try:
  13. from Queue import PriorityQueue
  14. except ImportError:
  15. class PriorityQueue(Queue):
  16. def _init(self, maxsize):
  17. self.maxsize = maxsize
  18. self.queue = []
  19. def _put(self, item):
  20. heapq.heappush(self.queue, item)
  21. def _get(self):
  22. return heapq.heappop(self.queue)
  23. from waflib import Utils, Task, Errors, Logs
  24. GAP = 5
  25. """
  26. Wait for at least ``GAP * njobs`` before trying to enqueue more tasks to run
  27. """
  28. class PriorityTasks(object):
  29. def __init__(self):
  30. self.lst = []
  31. def __len__(self):
  32. return len(self.lst)
  33. def __iter__(self):
  34. return iter(self.lst)
  35. def clear(self):
  36. self.lst = []
  37. def append(self, task):
  38. heapq.heappush(self.lst, task)
  39. def appendleft(self, task):
  40. "Deprecated, do not use"
  41. heapq.heappush(self.lst, task)
  42. def pop(self):
  43. return heapq.heappop(self.lst)
  44. def extend(self, lst):
  45. if self.lst:
  46. for x in lst:
  47. self.append(x)
  48. else:
  49. if isinstance(lst, list):
  50. self.lst = lst
  51. heapq.heapify(lst)
  52. else:
  53. self.lst = lst.lst
  54. class Consumer(Utils.threading.Thread):
  55. """
  56. Daemon thread object that executes a task. It shares a semaphore with
  57. the coordinator :py:class:`waflib.Runner.Spawner`. There is one
  58. instance per task to consume.
  59. """
  60. def __init__(self, spawner, task):
  61. Utils.threading.Thread.__init__(self)
  62. self.task = task
  63. """Task to execute"""
  64. self.spawner = spawner
  65. """Coordinator object"""
  66. self.setDaemon(1)
  67. self.start()
  68. def run(self):
  69. """
  70. Processes a single task
  71. """
  72. try:
  73. if not self.spawner.master.stop:
  74. self.spawner.master.process_task(self.task)
  75. finally:
  76. self.spawner.sem.release()
  77. self.spawner.master.out.put(self.task)
  78. self.task = None
  79. self.spawner = None
  80. class Spawner(Utils.threading.Thread):
  81. """
  82. Daemon thread that consumes tasks from :py:class:`waflib.Runner.Parallel` producer and
  83. spawns a consuming thread :py:class:`waflib.Runner.Consumer` for each
  84. :py:class:`waflib.Task.Task` instance.
  85. """
  86. def __init__(self, master):
  87. Utils.threading.Thread.__init__(self)
  88. self.master = master
  89. """:py:class:`waflib.Runner.Parallel` producer instance"""
  90. self.sem = Utils.threading.Semaphore(master.numjobs)
  91. """Bounded semaphore that prevents spawning more than *n* concurrent consumers"""
  92. self.setDaemon(1)
  93. self.start()
  94. def run(self):
  95. """
  96. Spawns new consumers to execute tasks by delegating to :py:meth:`waflib.Runner.Spawner.loop`
  97. """
  98. try:
  99. self.loop()
  100. except Exception:
  101. # Python 2 prints unnecessary messages when shutting down
  102. # we also want to stop the thread properly
  103. pass
  104. def loop(self):
  105. """
  106. Consumes task objects from the producer; ends when the producer has no more
  107. task to provide.
  108. """
  109. master = self.master
  110. while 1:
  111. task = master.ready.get()
  112. self.sem.acquire()
  113. if not master.stop:
  114. task.log_display(task.generator.bld)
  115. Consumer(self, task)
  116. class Parallel(object):
  117. """
  118. Schedule the tasks obtained from the build context for execution.
  119. """
  120. def __init__(self, bld, j=2):
  121. """
  122. The initialization requires a build context reference
  123. for computing the total number of jobs.
  124. """
  125. self.numjobs = j
  126. """
  127. Amount of parallel consumers to use
  128. """
  129. self.bld = bld
  130. """
  131. Instance of :py:class:`waflib.Build.BuildContext`
  132. """
  133. self.outstanding = PriorityTasks()
  134. """Heap of :py:class:`waflib.Task.Task` that may be ready to be executed"""
  135. self.postponed = PriorityTasks()
  136. """Heap of :py:class:`waflib.Task.Task` which are not ready to run for non-DAG reasons"""
  137. self.incomplete = set()
  138. """List of :py:class:`waflib.Task.Task` waiting for dependent tasks to complete (DAG)"""
  139. self.ready = PriorityQueue(0)
  140. """List of :py:class:`waflib.Task.Task` ready to be executed by consumers"""
  141. self.out = Queue(0)
  142. """List of :py:class:`waflib.Task.Task` returned by the task consumers"""
  143. self.count = 0
  144. """Amount of tasks that may be processed by :py:class:`waflib.Runner.TaskConsumer`"""
  145. self.processed = 0
  146. """Amount of tasks processed"""
  147. self.stop = False
  148. """Error flag to stop the build"""
  149. self.error = []
  150. """Tasks that could not be executed"""
  151. self.biter = None
  152. """Task iterator which must give groups of parallelizable tasks when calling ``next()``"""
  153. self.dirty = False
  154. """
  155. Flag that indicates that the build cache must be saved when a task was executed
  156. (calls :py:meth:`waflib.Build.BuildContext.store`)"""
  157. self.revdeps = Utils.defaultdict(set)
  158. """
  159. The reverse dependency graph of dependencies obtained from Task.run_after
  160. """
  161. self.spawner = Spawner(self)
  162. """
  163. Coordinating daemon thread that spawns thread consumers
  164. """
  165. def get_next_task(self):
  166. """
  167. Obtains the next Task instance to run
  168. :rtype: :py:class:`waflib.Task.Task`
  169. """
  170. if not self.outstanding:
  171. return None
  172. return self.outstanding.pop()
  173. def postpone(self, tsk):
  174. """
  175. Adds the task to the list :py:attr:`waflib.Runner.Parallel.postponed`.
  176. The order is scrambled so as to consume as many tasks in parallel as possible.
  177. :param tsk: task instance
  178. :type tsk: :py:class:`waflib.Task.Task`
  179. """
  180. self.postponed.append(tsk)
  181. def refill_task_list(self):
  182. """
  183. Pulls a next group of tasks to execute in :py:attr:`waflib.Runner.Parallel.outstanding`.
  184. Ensures that all tasks in the current build group are complete before processing the next one.
  185. """
  186. while self.count > self.numjobs * GAP:
  187. self.get_out()
  188. while not self.outstanding:
  189. if self.count:
  190. self.get_out()
  191. if self.outstanding:
  192. break
  193. elif self.postponed:
  194. try:
  195. cond = self.deadlock == self.processed
  196. except AttributeError:
  197. pass
  198. else:
  199. if cond:
  200. lst = []
  201. for tsk in self.postponed:
  202. deps = [id(x) for x in tsk.run_after if not x.hasrun]
  203. lst.append('%s\t-> %r' % (repr(tsk), deps))
  204. if not deps:
  205. lst.append('\n task %r dependencies are done, check its *runnable_status*?' % id(tsk))
  206. raise Errors.WafError('Deadlock detected: check the task build order%s' % ''.join(lst))
  207. self.deadlock = self.processed
  208. if self.postponed:
  209. self.outstanding.extend(self.postponed)
  210. self.postponed.clear()
  211. elif not self.count:
  212. if self.incomplete:
  213. for x in self.incomplete:
  214. for k in x.run_after:
  215. if not k.hasrun:
  216. break
  217. else:
  218. # dependency added after the build started without updating revdeps
  219. self.incomplete.remove(x)
  220. self.outstanding.append(x)
  221. break
  222. else:
  223. raise Errors.WafError('Broken revdeps detected on %r' % self.incomplete)
  224. else:
  225. tasks = next(self.biter)
  226. ready, waiting = self.prio_and_split(tasks)
  227. self.outstanding.extend(ready)
  228. self.incomplete.update(waiting)
  229. self.total = self.bld.total()
  230. break
  231. def add_more_tasks(self, tsk):
  232. """
  233. If a task provides :py:attr:`waflib.Task.Task.more_tasks`, then the tasks contained
  234. in that list are added to the current build and will be processed before the next build group.
  235. The priorities for dependent tasks are not re-calculated globally
  236. :param tsk: task instance
  237. :type tsk: :py:attr:`waflib.Task.Task`
  238. """
  239. if getattr(tsk, 'more_tasks', None):
  240. more = set(tsk.more_tasks)
  241. groups_done = set()
  242. def iteri(a, b):
  243. for x in a:
  244. yield x
  245. for x in b:
  246. yield x
  247. # Update the dependency tree
  248. # this assumes that task.run_after values were updated
  249. for x in iteri(self.outstanding, self.incomplete):
  250. for k in x.run_after:
  251. if isinstance(k, Task.TaskGroup):
  252. if k not in groups_done:
  253. groups_done.add(k)
  254. for j in k.prev & more:
  255. self.revdeps[j].add(k)
  256. elif k in more:
  257. self.revdeps[k].add(x)
  258. ready, waiting = self.prio_and_split(tsk.more_tasks)
  259. self.outstanding.extend(ready)
  260. self.incomplete.update(waiting)
  261. self.total += len(tsk.more_tasks)
  262. def mark_finished(self, tsk):
  263. def try_unfreeze(x):
  264. # DAG ancestors are likely to be in the incomplete set
  265. if x in self.incomplete:
  266. # TODO remove dependencies to free some memory?
  267. # x.run_after.remove(tsk)
  268. for k in x.run_after:
  269. if not k.hasrun:
  270. break
  271. else:
  272. self.incomplete.remove(x)
  273. self.outstanding.append(x)
  274. if tsk in self.revdeps:
  275. for x in self.revdeps[tsk]:
  276. if isinstance(x, Task.TaskGroup):
  277. x.prev.remove(tsk)
  278. if not x.prev:
  279. for k in x.next:
  280. # TODO necessary optimization?
  281. k.run_after.remove(x)
  282. try_unfreeze(k)
  283. # TODO necessary optimization?
  284. x.next = []
  285. else:
  286. try_unfreeze(x)
  287. del self.revdeps[tsk]
  288. def get_out(self):
  289. """
  290. Waits for a Task that task consumers add to :py:attr:`waflib.Runner.Parallel.out` after execution.
  291. Adds more Tasks if necessary through :py:attr:`waflib.Runner.Parallel.add_more_tasks`.
  292. :rtype: :py:attr:`waflib.Task.Task`
  293. """
  294. tsk = self.out.get()
  295. if not self.stop:
  296. self.add_more_tasks(tsk)
  297. self.mark_finished(tsk)
  298. self.count -= 1
  299. self.dirty = True
  300. return tsk
  301. def add_task(self, tsk):
  302. """
  303. Enqueue a Task to :py:attr:`waflib.Runner.Parallel.ready` so that consumers can run them.
  304. :param tsk: task instance
  305. :type tsk: :py:attr:`waflib.Task.Task`
  306. """
  307. self.ready.put(tsk)
  308. def process_task(self, tsk):
  309. """
  310. Processes a task and attempts to stop the build in case of errors
  311. """
  312. tsk.process()
  313. if tsk.hasrun != Task.SUCCESS:
  314. self.error_handler(tsk)
  315. def skip(self, tsk):
  316. """
  317. Mark a task as skipped/up-to-date
  318. """
  319. tsk.hasrun = Task.SKIPPED
  320. self.mark_finished(tsk)
  321. def cancel(self, tsk):
  322. """
  323. Mark a task as failed because of unsatisfiable dependencies
  324. """
  325. tsk.hasrun = Task.CANCELED
  326. self.mark_finished(tsk)
  327. def error_handler(self, tsk):
  328. """
  329. Called when a task cannot be executed. The flag :py:attr:`waflib.Runner.Parallel.stop` is set,
  330. unless the build is executed with::
  331. $ waf build -k
  332. :param tsk: task instance
  333. :type tsk: :py:attr:`waflib.Task.Task`
  334. """
  335. if not self.bld.keep:
  336. self.stop = True
  337. self.error.append(tsk)
  338. def task_status(self, tsk):
  339. """
  340. Obtains the task status to decide whether to run it immediately or not.
  341. :return: the exit status, for example :py:attr:`waflib.Task.ASK_LATER`
  342. :rtype: integer
  343. """
  344. try:
  345. return tsk.runnable_status()
  346. except Exception:
  347. self.processed += 1
  348. tsk.err_msg = traceback.format_exc()
  349. if not self.stop and self.bld.keep:
  350. self.skip(tsk)
  351. if self.bld.keep == 1:
  352. # if -k stop on the first exception, if -kk try to go as far as possible
  353. if Logs.verbose > 1 or not self.error:
  354. self.error.append(tsk)
  355. self.stop = True
  356. else:
  357. if Logs.verbose > 1:
  358. self.error.append(tsk)
  359. return Task.EXCEPTION
  360. tsk.hasrun = Task.EXCEPTION
  361. self.error_handler(tsk)
  362. return Task.EXCEPTION
  363. def start(self):
  364. """
  365. Obtains Task instances from the BuildContext instance and adds the ones that need to be executed to
  366. :py:class:`waflib.Runner.Parallel.ready` so that the :py:class:`waflib.Runner.Spawner` consumer thread
  367. has them executed. Obtains the executed Tasks back from :py:class:`waflib.Runner.Parallel.out`
  368. and marks the build as failed by setting the ``stop`` flag.
  369. If only one job is used, then executes the tasks one by one, without consumers.
  370. """
  371. self.total = self.bld.total()
  372. while not self.stop:
  373. self.refill_task_list()
  374. # consider the next task
  375. tsk = self.get_next_task()
  376. if not tsk:
  377. if self.count:
  378. # tasks may add new ones after they are run
  379. continue
  380. else:
  381. # no tasks to run, no tasks running, time to exit
  382. break
  383. if tsk.hasrun:
  384. # if the task is marked as "run", just skip it
  385. self.processed += 1
  386. continue
  387. if self.stop: # stop immediately after a failure is detected
  388. break
  389. st = self.task_status(tsk)
  390. if st == Task.RUN_ME:
  391. self.count += 1
  392. self.processed += 1
  393. if self.numjobs == 1:
  394. tsk.log_display(tsk.generator.bld)
  395. try:
  396. self.process_task(tsk)
  397. finally:
  398. self.out.put(tsk)
  399. else:
  400. self.add_task(tsk)
  401. elif st == Task.ASK_LATER:
  402. self.postpone(tsk)
  403. elif st == Task.SKIP_ME:
  404. self.processed += 1
  405. self.skip(tsk)
  406. self.add_more_tasks(tsk)
  407. elif st == Task.CANCEL_ME:
  408. # A dependency problem has occurred, and the
  409. # build is most likely run with `waf -k`
  410. if Logs.verbose > 1:
  411. self.error.append(tsk)
  412. self.processed += 1
  413. self.cancel(tsk)
  414. # self.count represents the tasks that have been made available to the consumer threads
  415. # collect all the tasks after an error else the message may be incomplete
  416. while self.error and self.count:
  417. self.get_out()
  418. self.ready.put(None)
  419. if not self.stop:
  420. assert not self.count
  421. assert not self.postponed
  422. assert not self.incomplete
  423. def prio_and_split(self, tasks):
  424. """
  425. Label input tasks with priority values, and return a pair containing
  426. the tasks that are ready to run and the tasks that are necessarily
  427. waiting for other tasks to complete.
  428. The priority system is really meant as an optional layer for optimization:
  429. dependency cycles are found quickly, and builds should be more efficient.
  430. A high priority number means that a task is processed first.
  431. This method can be overridden to disable the priority system::
  432. def prio_and_split(self, tasks):
  433. return tasks, []
  434. :return: A pair of task lists
  435. :rtype: tuple
  436. """
  437. # to disable:
  438. #return tasks, []
  439. for x in tasks:
  440. x.visited = 0
  441. reverse = self.revdeps
  442. groups_done = set()
  443. for x in tasks:
  444. for k in x.run_after:
  445. if isinstance(k, Task.TaskGroup):
  446. if k not in groups_done:
  447. groups_done.add(k)
  448. for j in k.prev:
  449. reverse[j].add(k)
  450. else:
  451. reverse[k].add(x)
  452. # the priority number is not the tree depth
  453. def visit(n):
  454. if isinstance(n, Task.TaskGroup):
  455. return sum(visit(k) for k in n.next)
  456. if n.visited == 0:
  457. n.visited = 1
  458. if n in reverse:
  459. rev = reverse[n]
  460. n.prio_order = n.tree_weight + len(rev) + sum(visit(k) for k in rev)
  461. else:
  462. n.prio_order = n.tree_weight
  463. n.visited = 2
  464. elif n.visited == 1:
  465. raise Errors.WafError('Dependency cycle found!')
  466. return n.prio_order
  467. for x in tasks:
  468. if x.visited != 0:
  469. # must visit all to detect cycles
  470. continue
  471. try:
  472. visit(x)
  473. except Errors.WafError:
  474. self.debug_cycles(tasks, reverse)
  475. ready = []
  476. waiting = []
  477. for x in tasks:
  478. for k in x.run_after:
  479. if not k.hasrun:
  480. waiting.append(x)
  481. break
  482. else:
  483. ready.append(x)
  484. return (ready, waiting)
  485. def debug_cycles(self, tasks, reverse):
  486. tmp = {}
  487. for x in tasks:
  488. tmp[x] = 0
  489. def visit(n, acc):
  490. if isinstance(n, Task.TaskGroup):
  491. for k in n.next:
  492. visit(k, acc)
  493. return
  494. if tmp[n] == 0:
  495. tmp[n] = 1
  496. for k in reverse.get(n, []):
  497. visit(k, [n] + acc)
  498. tmp[n] = 2
  499. elif tmp[n] == 1:
  500. lst = []
  501. for tsk in acc:
  502. lst.append(repr(tsk))
  503. if tsk is n:
  504. # exclude prior nodes, we want the minimum cycle
  505. break
  506. raise Errors.WafError('Task dependency cycle in "run_after" constraints: %s' % ''.join(lst))
  507. for x in tasks:
  508. visit(x, [])