blob: 6ab965e27869c43cc027c21d232e6d9dd80b0b41 [file] [log] [blame]
Yuheng Long761748d2013-06-28 09:32:43 -07001"""The pipeline_worker functions of the build and test stage of the framework.
2
3This module defines the helper and the worker. If there are duplicate tasks, for
4example t1 and t2, needs to be built/tested, one of them, for example t1, will
5be built/tested and the helper waits for the result of t1 and set the results of
6the other task, t2 here, to be the same as that of t1. Setting the result of t2
7to be the same as t1 is referred to as resolving the result of t2.
8The worker invokes the work method of the tasks that are not duplicate.
9"""
10
11__author__ = 'yuhenglong@google.com (Yuheng Long)'
12
13import pipeline_process
14
15
16def helper(stage, done_dict, helper_queue, completed_queue, result_queue):
17 """Helper that filters duplicate tasks.
18
19 This method Continuously pulls duplicate tasks from the helper_queue. The
20 duplicate tasks need not be compiled/tested. This method also pulls completed
21 tasks from the worker queue and let the results of the duplicate tasks be the
22 same as their corresponding finished task.
23
24 Args:
25 stage: The current stage of the pipeline, for example, build stage or test
26 stage.
27 done_dict: A dictionary of tasks that are done. The key of the dictionary is
28 the identifier of the task. The value of the dictionary is the results of
29 performing the corresponding task.
30 helper_queue: A queue of duplicate tasks whose results need to be resolved.
31 This is a communication channel between the pipeline_process and this
32 helper process.
33 completed_queue: A queue of tasks that have been built/tested. The results
34 of these tasks are needed to resolve the results of the duplicate tasks.
35 This is the communication channel between the workers and this helper
36 process.
37 result_queue: After the results of the duplicate tasks have been resolved,
38 the duplicate tasks will be sent to the next stage via this queue.
39 """
40
41 # The list of duplicate tasks, the results of which need to be resolved.
42 waiting_list = []
43
44 while True:
45 # Pull duplicate task from the helper queue.
46 if not helper_queue.empty():
47 task = helper_queue.get()
48
49 if task == pipeline_process.POISONPILL:
50 # Poison pill means no more duplicate task from the helper queue.
51 break
52
53 # The task has not been performed before.
54 assert not task.done(stage)
55
56 # The identifier of this task.
57 identifier = task.get_identifier(stage)
58
59 # If a duplicate task comes before the corresponding resolved results from
60 # the completed_queue, it will be put in the waiting list. If the result
61 # arrives before the duplicate task, the duplicate task will be resolved
62 # right away.
63 if identifier in done_dict:
64 # This task has been encountered before and the result is available. The
65 # result can be resolved right away.
66 task.set_result(stage, done_dict[identifier])
67 result_queue.put(task)
68 else:
69 waiting_list.append(task)
70
71 # Check and get completed tasks from completed_queue.
72 get_result_from_completed_queue(stage, completed_queue, done_dict,
73 waiting_list, result_queue)
74
75 # Wait to resolve the results of the remaining duplicate tasks.
76 while waiting_list:
77 get_result_from_completed_queue(stage, completed_queue, done_dict,
78 waiting_list, result_queue)
79
80
81def get_result_from_completed_queue(stage, completed_queue, done_dict,
82 waiting_list, result_queue):
83 """Pull results from the completed queue and resolves duplicate tasks.
84
85 Args:
86 stage: The current stage of the pipeline, for example, build stage or test
87 stage.
88 completed_queue: A queue of tasks that have been performed. The results of
89 these tasks are needed to resolve the results of the duplicate tasks. This
90 is the communication channel between the workers and this method.
91 done_dict: A dictionary of tasks that are done. The key of the dictionary is
92 the optimization flags of the task. The value of the dictionary is the
93 compilation results of the corresponding task.
94 waiting_list: The list of duplicate tasks, the results of which need to be
95 resolved.
96 result_queue: After the results of the duplicate tasks have been resolved,
97 the duplicate tasks will be sent to the next stage via this queue.
98
99 This helper method tries to pull a completed task from the completed queue.
100 If it gets a task from the queue, it resolves the results of all the relevant
101 duplicate tasks in the waiting list. Relevant tasks are the tasks that have
102 the same flags as the currently received results from the completed_queue.
103 """
104 # Pull completed task from the worker queue.
105 if not completed_queue.empty():
106 (identifier, result) = completed_queue.get()
107 done_dict[identifier] = result
108
109 tasks = [t for t in waiting_list if t.get_identifier(stage) == identifier]
110 for duplicate_task in tasks:
111 duplicate_task.set_result(stage, result)
112 result_queue.put(duplicate_task)
113 waiting_list.remove(duplicate_task)
114
115
116def worker(stage, task, helper_queue, result_queue):
117 """Worker that performs the task.
118
119 This method calls the work method of the input task and distribute the result
120 to the helper and the next stage.
121
122 Args:
123 stage: The current stage of the pipeline, for example, build stage or test
124 stage.
125 task: Input task that needs to be performed.
126 helper_queue: Queue that holds the completed tasks and the results. This is
127 the communication channel between the worker and the helper.
128 result_queue: Queue that holds the completed tasks and the results. This is
129 the communication channel between the worker and the next stage.
130 """
131
132 # The task has not been completed before.
133 assert not task.done(stage)
134
135 task.work(stage)
136 helper_queue.put((task.get_identifier(stage), task.get_result(stage)))
137 result_queue.put(task)