/src/git/parallel-checkout.c
Line | Count | Source (jump to first uncovered line) |
1 | | #define USE_THE_REPOSITORY_VARIABLE |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "config.h" |
5 | | #include "entry.h" |
6 | | #include "gettext.h" |
7 | | #include "hash.h" |
8 | | #include "hex.h" |
9 | | #include "parallel-checkout.h" |
10 | | #include "pkt-line.h" |
11 | | #include "progress.h" |
12 | | #include "read-cache-ll.h" |
13 | | #include "run-command.h" |
14 | | #include "sigchain.h" |
15 | | #include "streaming.h" |
16 | | #include "symlinks.h" |
17 | | #include "thread-utils.h" |
18 | | #include "trace2.h" |
19 | | |
20 | | struct pc_worker { |
21 | | struct child_process cp; |
22 | | size_t next_item_to_complete, nr_items_to_complete; |
23 | | }; |
24 | | |
25 | | struct parallel_checkout { |
26 | | enum pc_status status; |
27 | | struct parallel_checkout_item *items; /* The parallel checkout queue. */ |
28 | | size_t nr, alloc; |
29 | | struct progress *progress; |
30 | | unsigned int *progress_cnt; |
31 | | }; |
32 | | |
33 | | static struct parallel_checkout parallel_checkout; |
34 | | |
35 | | enum pc_status parallel_checkout_status(void) |
36 | 0 | { |
37 | 0 | return parallel_checkout.status; |
38 | 0 | } |
39 | | |
40 | | static const int DEFAULT_THRESHOLD_FOR_PARALLELISM = 100; |
41 | | static const int DEFAULT_NUM_WORKERS = 1; |
42 | | |
43 | | void get_parallel_checkout_configs(int *num_workers, int *threshold) |
44 | 0 | { |
45 | 0 | char *env_workers = getenv("GIT_TEST_CHECKOUT_WORKERS"); |
46 | |
|
47 | 0 | if (env_workers && *env_workers) { |
48 | 0 | if (strtol_i(env_workers, 10, num_workers)) { |
49 | 0 | die(_("invalid value for '%s': '%s'"), |
50 | 0 | "GIT_TEST_CHECKOUT_WORKERS", env_workers); |
51 | 0 | } |
52 | 0 | if (*num_workers < 1) |
53 | 0 | *num_workers = online_cpus(); |
54 | |
|
55 | 0 | *threshold = 0; |
56 | 0 | return; |
57 | 0 | } |
58 | | |
59 | 0 | if (git_config_get_int("checkout.workers", num_workers)) |
60 | 0 | *num_workers = DEFAULT_NUM_WORKERS; |
61 | 0 | else if (*num_workers < 1) |
62 | 0 | *num_workers = online_cpus(); |
63 | |
|
64 | 0 | if (git_config_get_int("checkout.thresholdForParallelism", threshold)) |
65 | 0 | *threshold = DEFAULT_THRESHOLD_FOR_PARALLELISM; |
66 | 0 | } |
67 | | |
68 | | void init_parallel_checkout(void) |
69 | 0 | { |
70 | 0 | if (parallel_checkout.status != PC_UNINITIALIZED) |
71 | 0 | BUG("parallel checkout already initialized"); |
72 | | |
73 | 0 | parallel_checkout.status = PC_ACCEPTING_ENTRIES; |
74 | 0 | } |
75 | | |
76 | | static void finish_parallel_checkout(void) |
77 | 0 | { |
78 | 0 | if (parallel_checkout.status == PC_UNINITIALIZED) |
79 | 0 | BUG("cannot finish parallel checkout: not initialized yet"); |
80 | | |
81 | 0 | free(parallel_checkout.items); |
82 | 0 | memset(¶llel_checkout, 0, sizeof(parallel_checkout)); |
83 | 0 | } |
84 | | |
85 | | static int is_eligible_for_parallel_checkout(const struct cache_entry *ce, |
86 | | const struct conv_attrs *ca) |
87 | 0 | { |
88 | 0 | enum conv_attrs_classification c; |
89 | 0 | size_t packed_item_size; |
90 | | |
91 | | /* |
92 | | * Symlinks cannot be checked out in parallel as, in case of path |
93 | | * collision, they could racily replace leading directories of other |
94 | | * entries being checked out. Submodules are checked out in child |
95 | | * processes, which have their own parallel checkout queues. |
96 | | */ |
97 | 0 | if (!S_ISREG(ce->ce_mode)) |
98 | 0 | return 0; |
99 | | |
100 | 0 | packed_item_size = sizeof(struct pc_item_fixed_portion) + ce->ce_namelen + |
101 | 0 | (ca->working_tree_encoding ? strlen(ca->working_tree_encoding) : 0); |
102 | | |
103 | | /* |
104 | | * The amount of data we send to the workers per checkout item is |
105 | | * typically small (75~300B). So unless we find an insanely huge path |
106 | | * of 64KB, we should never reach the 65KB limit of one pkt-line. If |
107 | | * that does happen, we let the sequential code handle the item. |
108 | | */ |
109 | 0 | if (packed_item_size > LARGE_PACKET_DATA_MAX) |
110 | 0 | return 0; |
111 | | |
112 | 0 | c = classify_conv_attrs(ca); |
113 | 0 | switch (c) { |
114 | 0 | case CA_CLASS_INCORE: |
115 | 0 | return 1; |
116 | | |
117 | 0 | case CA_CLASS_INCORE_FILTER: |
118 | | /* |
119 | | * It would be safe to allow concurrent instances of |
120 | | * single-file smudge filters, like rot13, but we should not |
121 | | * assume that all filters are parallel-process safe. So we |
122 | | * don't allow this. |
123 | | */ |
124 | 0 | return 0; |
125 | | |
126 | 0 | case CA_CLASS_INCORE_PROCESS: |
127 | | /* |
128 | | * The parallel queue and the delayed queue are not compatible, |
129 | | * so they must be kept completely separated. And we can't tell |
130 | | * if a long-running process will delay its response without |
131 | | * actually asking it to perform the filtering. Therefore, this |
132 | | * type of filter is not allowed in parallel checkout. |
133 | | * |
134 | | * Furthermore, there should only be one instance of the |
135 | | * long-running process filter as we don't know how it is |
136 | | * managing its own concurrency. So, spreading the entries that |
137 | | * requisite such a filter among the parallel workers would |
138 | | * require a lot more inter-process communication. We would |
139 | | * probably have to designate a single process to interact with |
140 | | * the filter and send all the necessary data to it, for each |
141 | | * entry. |
142 | | */ |
143 | 0 | return 0; |
144 | | |
145 | 0 | case CA_CLASS_STREAMABLE: |
146 | 0 | return 1; |
147 | | |
148 | 0 | default: |
149 | 0 | BUG("unsupported conv_attrs classification '%d'", c); |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | | int enqueue_checkout(struct cache_entry *ce, struct conv_attrs *ca, |
154 | | int *checkout_counter) |
155 | 0 | { |
156 | 0 | struct parallel_checkout_item *pc_item; |
157 | |
|
158 | 0 | if (parallel_checkout.status != PC_ACCEPTING_ENTRIES || |
159 | 0 | !is_eligible_for_parallel_checkout(ce, ca)) |
160 | 0 | return -1; |
161 | | |
162 | 0 | ALLOC_GROW(parallel_checkout.items, parallel_checkout.nr + 1, |
163 | 0 | parallel_checkout.alloc); |
164 | |
|
165 | 0 | pc_item = ¶llel_checkout.items[parallel_checkout.nr]; |
166 | 0 | pc_item->ce = ce; |
167 | 0 | memcpy(&pc_item->ca, ca, sizeof(pc_item->ca)); |
168 | 0 | pc_item->status = PC_ITEM_PENDING; |
169 | 0 | pc_item->id = parallel_checkout.nr; |
170 | 0 | pc_item->checkout_counter = checkout_counter; |
171 | 0 | parallel_checkout.nr++; |
172 | |
|
173 | 0 | return 0; |
174 | 0 | } |
175 | | |
176 | | size_t pc_queue_size(void) |
177 | 0 | { |
178 | 0 | return parallel_checkout.nr; |
179 | 0 | } |
180 | | |
181 | | static void advance_progress_meter(void) |
182 | 0 | { |
183 | 0 | if (parallel_checkout.progress) { |
184 | 0 | (*parallel_checkout.progress_cnt)++; |
185 | 0 | display_progress(parallel_checkout.progress, |
186 | 0 | *parallel_checkout.progress_cnt); |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | | static int handle_results(struct checkout *state) |
191 | 0 | { |
192 | 0 | int ret = 0; |
193 | 0 | size_t i; |
194 | 0 | int have_pending = 0; |
195 | | |
196 | | /* |
197 | | * We first update the successfully written entries with the collected |
198 | | * stat() data, so that they can be found by mark_colliding_entries(), |
199 | | * in the next loop, when necessary. |
200 | | */ |
201 | 0 | for (i = 0; i < parallel_checkout.nr; i++) { |
202 | 0 | struct parallel_checkout_item *pc_item = ¶llel_checkout.items[i]; |
203 | 0 | if (pc_item->status == PC_ITEM_WRITTEN) |
204 | 0 | update_ce_after_write(state, pc_item->ce, &pc_item->st); |
205 | 0 | } |
206 | |
|
207 | 0 | for (i = 0; i < parallel_checkout.nr; i++) { |
208 | 0 | struct parallel_checkout_item *pc_item = ¶llel_checkout.items[i]; |
209 | |
|
210 | 0 | switch(pc_item->status) { |
211 | 0 | case PC_ITEM_WRITTEN: |
212 | 0 | if (pc_item->checkout_counter) |
213 | 0 | (*pc_item->checkout_counter)++; |
214 | 0 | break; |
215 | 0 | case PC_ITEM_COLLIDED: |
216 | | /* |
217 | | * The entry could not be checked out due to a path |
218 | | * collision with another entry. Since there can only |
219 | | * be one entry of each colliding group on the disk, we |
220 | | * could skip trying to check out this one and move on. |
221 | | * However, this would leave the unwritten entries with |
222 | | * null stat() fields on the index, which could |
223 | | * potentially slow down subsequent operations that |
224 | | * require refreshing it: git would not be able to |
225 | | * trust st_size and would have to go to the filesystem |
226 | | * to see if the contents match (see ie_modified()). |
227 | | * |
228 | | * Instead, let's pay the overhead only once, now, and |
229 | | * call checkout_entry_ca() again for this file, to |
230 | | * have its stat() data stored in the index. This also |
231 | | * has the benefit of adding this entry and its |
232 | | * colliding pair to the collision report message. |
233 | | * Additionally, this overwriting behavior is consistent |
234 | | * with what the sequential checkout does, so it doesn't |
235 | | * add any extra overhead. |
236 | | */ |
237 | 0 | ret |= checkout_entry_ca(pc_item->ce, &pc_item->ca, |
238 | 0 | state, NULL, |
239 | 0 | pc_item->checkout_counter); |
240 | 0 | advance_progress_meter(); |
241 | 0 | break; |
242 | 0 | case PC_ITEM_PENDING: |
243 | 0 | have_pending = 1; |
244 | | /* fall through */ |
245 | 0 | case PC_ITEM_FAILED: |
246 | 0 | ret = -1; |
247 | 0 | break; |
248 | 0 | default: |
249 | 0 | BUG("unknown checkout item status in parallel checkout"); |
250 | 0 | } |
251 | 0 | } |
252 | | |
253 | 0 | if (have_pending) |
254 | 0 | error("parallel checkout finished with pending entries"); |
255 | |
|
256 | 0 | return ret; |
257 | 0 | } |
258 | | |
259 | | static int reset_fd(int fd, const char *path) |
260 | 0 | { |
261 | 0 | if (lseek(fd, 0, SEEK_SET) != 0) |
262 | 0 | return error_errno("failed to rewind descriptor of '%s'", path); |
263 | 0 | if (ftruncate(fd, 0)) |
264 | 0 | return error_errno("failed to truncate file '%s'", path); |
265 | 0 | return 0; |
266 | 0 | } |
267 | | |
268 | | static int write_pc_item_to_fd(struct parallel_checkout_item *pc_item, int fd, |
269 | | const char *path) |
270 | 0 | { |
271 | 0 | int ret; |
272 | 0 | struct stream_filter *filter; |
273 | 0 | struct strbuf buf = STRBUF_INIT; |
274 | 0 | char *blob; |
275 | 0 | size_t size; |
276 | 0 | ssize_t wrote; |
277 | | |
278 | | /* Sanity check */ |
279 | 0 | assert(is_eligible_for_parallel_checkout(pc_item->ce, &pc_item->ca)); |
280 | | |
281 | 0 | filter = get_stream_filter_ca(&pc_item->ca, &pc_item->ce->oid); |
282 | 0 | if (filter) { |
283 | 0 | if (stream_blob_to_fd(fd, &pc_item->ce->oid, filter, 1)) { |
284 | | /* On error, reset fd to try writing without streaming */ |
285 | 0 | if (reset_fd(fd, path)) |
286 | 0 | return -1; |
287 | 0 | } else { |
288 | 0 | return 0; |
289 | 0 | } |
290 | 0 | } |
291 | | |
292 | 0 | blob = read_blob_entry(pc_item->ce, &size); |
293 | 0 | if (!blob) |
294 | 0 | return error("cannot read object %s '%s'", |
295 | 0 | oid_to_hex(&pc_item->ce->oid), pc_item->ce->name); |
296 | | |
297 | | /* |
298 | | * checkout metadata is used to give context for external process |
299 | | * filters. Files requiring such filters are not eligible for parallel |
300 | | * checkout, so pass NULL. Note: if that changes, the metadata must also |
301 | | * be passed from the main process to the workers. |
302 | | */ |
303 | 0 | ret = convert_to_working_tree_ca(&pc_item->ca, pc_item->ce->name, |
304 | 0 | blob, size, &buf, NULL); |
305 | |
|
306 | 0 | if (ret) { |
307 | 0 | size_t newsize; |
308 | 0 | free(blob); |
309 | 0 | blob = strbuf_detach(&buf, &newsize); |
310 | 0 | size = newsize; |
311 | 0 | } |
312 | |
|
313 | 0 | wrote = write_in_full(fd, blob, size); |
314 | 0 | free(blob); |
315 | 0 | if (wrote < 0) |
316 | 0 | return error("unable to write file '%s'", path); |
317 | | |
318 | 0 | return 0; |
319 | 0 | } |
320 | | |
321 | | static int close_and_clear(int *fd) |
322 | 0 | { |
323 | 0 | int ret = 0; |
324 | |
|
325 | 0 | if (*fd >= 0) { |
326 | 0 | ret = close(*fd); |
327 | 0 | *fd = -1; |
328 | 0 | } |
329 | |
|
330 | 0 | return ret; |
331 | 0 | } |
332 | | |
333 | | void write_pc_item(struct parallel_checkout_item *pc_item, |
334 | | struct checkout *state) |
335 | 0 | { |
336 | 0 | unsigned int mode = (pc_item->ce->ce_mode & 0100) ? 0777 : 0666; |
337 | 0 | int fd = -1, fstat_done = 0; |
338 | 0 | struct strbuf path = STRBUF_INIT; |
339 | 0 | const char *dir_sep; |
340 | |
|
341 | 0 | strbuf_add(&path, state->base_dir, state->base_dir_len); |
342 | 0 | strbuf_add(&path, pc_item->ce->name, pc_item->ce->ce_namelen); |
343 | |
|
344 | 0 | dir_sep = find_last_dir_sep(path.buf); |
345 | | |
346 | | /* |
347 | | * The leading dirs should have been already created by now. But, in |
348 | | * case of path collisions, one of the dirs could have been replaced by |
349 | | * a symlink (checked out after we enqueued this entry for parallel |
350 | | * checkout). Thus, we must check the leading dirs again. |
351 | | */ |
352 | 0 | if (dir_sep && !has_dirs_only_path(path.buf, dir_sep - path.buf, |
353 | 0 | state->base_dir_len)) { |
354 | 0 | pc_item->status = PC_ITEM_COLLIDED; |
355 | 0 | trace2_data_string("pcheckout", NULL, "collision/dirname", path.buf); |
356 | 0 | goto out; |
357 | 0 | } |
358 | | |
359 | 0 | fd = open(path.buf, O_WRONLY | O_CREAT | O_EXCL, mode); |
360 | |
|
361 | 0 | if (fd < 0) { |
362 | 0 | if (errno == EEXIST || errno == EISDIR) { |
363 | | /* |
364 | | * Errors which probably represent a path collision. |
365 | | * Suppress the error message and mark the item to be |
366 | | * retried later, sequentially. ENOTDIR and ENOENT are |
367 | | * also interesting, but the above has_dirs_only_path() |
368 | | * call should have already caught these cases. |
369 | | */ |
370 | 0 | pc_item->status = PC_ITEM_COLLIDED; |
371 | 0 | trace2_data_string("pcheckout", NULL, |
372 | 0 | "collision/basename", path.buf); |
373 | 0 | } else { |
374 | 0 | error_errno("failed to open file '%s'", path.buf); |
375 | 0 | pc_item->status = PC_ITEM_FAILED; |
376 | 0 | } |
377 | 0 | goto out; |
378 | 0 | } |
379 | | |
380 | 0 | if (write_pc_item_to_fd(pc_item, fd, path.buf)) { |
381 | | /* Error was already reported. */ |
382 | 0 | pc_item->status = PC_ITEM_FAILED; |
383 | 0 | close_and_clear(&fd); |
384 | 0 | unlink(path.buf); |
385 | 0 | goto out; |
386 | 0 | } |
387 | | |
388 | 0 | fstat_done = fstat_checkout_output(fd, state, &pc_item->st); |
389 | |
|
390 | 0 | if (close_and_clear(&fd)) { |
391 | 0 | error_errno("unable to close file '%s'", path.buf); |
392 | 0 | pc_item->status = PC_ITEM_FAILED; |
393 | 0 | goto out; |
394 | 0 | } |
395 | | |
396 | 0 | if (state->refresh_cache && !fstat_done && lstat(path.buf, &pc_item->st) < 0) { |
397 | 0 | error_errno("unable to stat just-written file '%s'", path.buf); |
398 | 0 | pc_item->status = PC_ITEM_FAILED; |
399 | 0 | goto out; |
400 | 0 | } |
401 | | |
402 | 0 | pc_item->status = PC_ITEM_WRITTEN; |
403 | |
|
404 | 0 | out: |
405 | 0 | strbuf_release(&path); |
406 | 0 | } |
407 | | |
408 | | static void send_one_item(int fd, struct parallel_checkout_item *pc_item) |
409 | 0 | { |
410 | 0 | size_t len_data; |
411 | 0 | char *data, *variant; |
412 | 0 | struct pc_item_fixed_portion *fixed_portion; |
413 | 0 | const char *working_tree_encoding = pc_item->ca.working_tree_encoding; |
414 | 0 | size_t name_len = pc_item->ce->ce_namelen; |
415 | 0 | size_t working_tree_encoding_len = working_tree_encoding ? |
416 | 0 | strlen(working_tree_encoding) : 0; |
417 | | |
418 | | /* |
419 | | * Any changes in the calculation of the message size must also be made |
420 | | * in is_eligible_for_parallel_checkout(). |
421 | | */ |
422 | 0 | len_data = sizeof(struct pc_item_fixed_portion) + name_len + |
423 | 0 | working_tree_encoding_len; |
424 | |
|
425 | 0 | data = xmalloc(len_data); |
426 | |
|
427 | 0 | fixed_portion = (struct pc_item_fixed_portion *)data; |
428 | 0 | fixed_portion->id = pc_item->id; |
429 | 0 | fixed_portion->ce_mode = pc_item->ce->ce_mode; |
430 | 0 | fixed_portion->crlf_action = pc_item->ca.crlf_action; |
431 | 0 | fixed_portion->ident = pc_item->ca.ident; |
432 | 0 | fixed_portion->name_len = name_len; |
433 | 0 | fixed_portion->working_tree_encoding_len = working_tree_encoding_len; |
434 | 0 | oidcpy(&fixed_portion->oid, &pc_item->ce->oid); |
435 | |
|
436 | 0 | variant = data + sizeof(*fixed_portion); |
437 | 0 | if (working_tree_encoding_len) { |
438 | 0 | memcpy(variant, working_tree_encoding, working_tree_encoding_len); |
439 | 0 | variant += working_tree_encoding_len; |
440 | 0 | } |
441 | 0 | memcpy(variant, pc_item->ce->name, name_len); |
442 | |
|
443 | 0 | packet_write(fd, data, len_data); |
444 | |
|
445 | 0 | free(data); |
446 | 0 | } |
447 | | |
448 | | static void send_batch(int fd, size_t start, size_t nr) |
449 | 0 | { |
450 | 0 | size_t i; |
451 | 0 | sigchain_push(SIGPIPE, SIG_IGN); |
452 | 0 | for (i = 0; i < nr; i++) |
453 | 0 | send_one_item(fd, ¶llel_checkout.items[start + i]); |
454 | 0 | packet_flush(fd); |
455 | 0 | sigchain_pop(SIGPIPE); |
456 | 0 | } |
457 | | |
458 | | static struct pc_worker *setup_workers(struct checkout *state, int num_workers) |
459 | 0 | { |
460 | 0 | struct pc_worker *workers; |
461 | 0 | int i, workers_with_one_extra_item; |
462 | 0 | size_t base_batch_size, batch_beginning = 0; |
463 | |
|
464 | 0 | ALLOC_ARRAY(workers, num_workers); |
465 | |
|
466 | 0 | for (i = 0; i < num_workers; i++) { |
467 | 0 | struct child_process *cp = &workers[i].cp; |
468 | |
|
469 | 0 | child_process_init(cp); |
470 | 0 | cp->git_cmd = 1; |
471 | 0 | cp->in = -1; |
472 | 0 | cp->out = -1; |
473 | 0 | cp->clean_on_exit = 1; |
474 | 0 | strvec_push(&cp->args, "checkout--worker"); |
475 | 0 | if (state->base_dir_len) |
476 | 0 | strvec_pushf(&cp->args, "--prefix=%s", state->base_dir); |
477 | 0 | if (start_command(cp)) |
478 | 0 | die("failed to spawn checkout worker"); |
479 | 0 | } |
480 | | |
481 | 0 | base_batch_size = parallel_checkout.nr / num_workers; |
482 | 0 | workers_with_one_extra_item = parallel_checkout.nr % num_workers; |
483 | |
|
484 | 0 | for (i = 0; i < num_workers; i++) { |
485 | 0 | struct pc_worker *worker = &workers[i]; |
486 | 0 | size_t batch_size = base_batch_size; |
487 | | |
488 | | /* distribute the extra work evenly */ |
489 | 0 | if (i < workers_with_one_extra_item) |
490 | 0 | batch_size++; |
491 | |
|
492 | 0 | send_batch(worker->cp.in, batch_beginning, batch_size); |
493 | 0 | worker->next_item_to_complete = batch_beginning; |
494 | 0 | worker->nr_items_to_complete = batch_size; |
495 | |
|
496 | 0 | batch_beginning += batch_size; |
497 | 0 | } |
498 | |
|
499 | 0 | return workers; |
500 | 0 | } |
501 | | |
502 | | static void finish_workers(struct pc_worker *workers, int num_workers) |
503 | 0 | { |
504 | 0 | int i; |
505 | | |
506 | | /* |
507 | | * Close pipes before calling finish_command() to let the workers |
508 | | * exit asynchronously and avoid spending extra time on wait(). |
509 | | */ |
510 | 0 | for (i = 0; i < num_workers; i++) { |
511 | 0 | struct child_process *cp = &workers[i].cp; |
512 | 0 | if (cp->in >= 0) |
513 | 0 | close(cp->in); |
514 | 0 | if (cp->out >= 0) |
515 | 0 | close(cp->out); |
516 | 0 | } |
517 | |
|
518 | 0 | for (i = 0; i < num_workers; i++) { |
519 | 0 | int rc = finish_command(&workers[i].cp); |
520 | 0 | if (rc > 128) { |
521 | | /* |
522 | | * For a normal non-zero exit, the worker should have |
523 | | * already printed something useful to stderr. But a |
524 | | * death by signal should be mentioned to the user. |
525 | | */ |
526 | 0 | error("checkout worker %d died of signal %d", i, rc - 128); |
527 | 0 | } |
528 | 0 | } |
529 | |
|
530 | 0 | free(workers); |
531 | 0 | } |
532 | | |
533 | | static inline void assert_pc_item_result_size(int got, int exp) |
534 | 0 | { |
535 | 0 | if (got != exp) |
536 | 0 | BUG("wrong result size from checkout worker (got %dB, exp %dB)", |
537 | 0 | got, exp); |
538 | 0 | } |
539 | | |
540 | | static void parse_and_save_result(const char *buffer, int len, |
541 | | struct pc_worker *worker) |
542 | 0 | { |
543 | 0 | struct pc_item_result *res; |
544 | 0 | struct parallel_checkout_item *pc_item; |
545 | 0 | struct stat *st = NULL; |
546 | |
|
547 | 0 | if (len < PC_ITEM_RESULT_BASE_SIZE) |
548 | 0 | BUG("too short result from checkout worker (got %dB, exp >=%dB)", |
549 | 0 | len, (int)PC_ITEM_RESULT_BASE_SIZE); |
550 | | |
551 | 0 | res = (struct pc_item_result *)buffer; |
552 | | |
553 | | /* |
554 | | * Worker should send either the full result struct on success, or |
555 | | * just the base (i.e. no stat data), otherwise. |
556 | | */ |
557 | 0 | if (res->status == PC_ITEM_WRITTEN) { |
558 | 0 | assert_pc_item_result_size(len, (int)sizeof(struct pc_item_result)); |
559 | 0 | st = &res->st; |
560 | 0 | } else { |
561 | 0 | assert_pc_item_result_size(len, (int)PC_ITEM_RESULT_BASE_SIZE); |
562 | 0 | } |
563 | |
|
564 | 0 | if (!worker->nr_items_to_complete) |
565 | 0 | BUG("received result from supposedly finished checkout worker"); |
566 | 0 | if (res->id != worker->next_item_to_complete) |
567 | 0 | BUG("unexpected item id from checkout worker (got %"PRIuMAX", exp %"PRIuMAX")", |
568 | 0 | (uintmax_t)res->id, (uintmax_t)worker->next_item_to_complete); |
569 | | |
570 | 0 | worker->next_item_to_complete++; |
571 | 0 | worker->nr_items_to_complete--; |
572 | |
|
573 | 0 | pc_item = ¶llel_checkout.items[res->id]; |
574 | 0 | pc_item->status = res->status; |
575 | 0 | if (st) |
576 | 0 | pc_item->st = *st; |
577 | |
|
578 | 0 | if (res->status != PC_ITEM_COLLIDED) |
579 | 0 | advance_progress_meter(); |
580 | 0 | } |
581 | | |
582 | | static void gather_results_from_workers(struct pc_worker *workers, |
583 | | int num_workers) |
584 | 0 | { |
585 | 0 | int i, active_workers = num_workers; |
586 | 0 | struct pollfd *pfds; |
587 | |
|
588 | 0 | CALLOC_ARRAY(pfds, num_workers); |
589 | 0 | for (i = 0; i < num_workers; i++) { |
590 | 0 | pfds[i].fd = workers[i].cp.out; |
591 | 0 | pfds[i].events = POLLIN; |
592 | 0 | } |
593 | |
|
594 | 0 | while (active_workers) { |
595 | 0 | int nr = poll(pfds, num_workers, -1); |
596 | |
|
597 | 0 | if (nr < 0) { |
598 | 0 | if (errno == EINTR) |
599 | 0 | continue; |
600 | 0 | die_errno("failed to poll checkout workers"); |
601 | 0 | } |
602 | | |
603 | 0 | for (i = 0; i < num_workers && nr > 0; i++) { |
604 | 0 | struct pc_worker *worker = &workers[i]; |
605 | 0 | struct pollfd *pfd = &pfds[i]; |
606 | |
|
607 | 0 | if (!pfd->revents) |
608 | 0 | continue; |
609 | | |
610 | 0 | if (pfd->revents & POLLIN) { |
611 | 0 | int len = packet_read(pfd->fd, packet_buffer, |
612 | 0 | sizeof(packet_buffer), 0); |
613 | |
|
614 | 0 | if (len < 0) { |
615 | 0 | BUG("packet_read() returned negative value"); |
616 | 0 | } else if (!len) { |
617 | 0 | pfd->fd = -1; |
618 | 0 | active_workers--; |
619 | 0 | } else { |
620 | 0 | parse_and_save_result(packet_buffer, |
621 | 0 | len, worker); |
622 | 0 | } |
623 | 0 | } else if (pfd->revents & POLLHUP) { |
624 | 0 | pfd->fd = -1; |
625 | 0 | active_workers--; |
626 | 0 | } else if (pfd->revents & (POLLNVAL | POLLERR)) { |
627 | 0 | die("error polling from checkout worker"); |
628 | 0 | } |
629 | | |
630 | 0 | nr--; |
631 | 0 | } |
632 | 0 | } |
633 | | |
634 | 0 | free(pfds); |
635 | 0 | } |
636 | | |
637 | | static void write_items_sequentially(struct checkout *state) |
638 | 0 | { |
639 | 0 | size_t i; |
640 | |
|
641 | 0 | for (i = 0; i < parallel_checkout.nr; i++) { |
642 | 0 | struct parallel_checkout_item *pc_item = ¶llel_checkout.items[i]; |
643 | 0 | write_pc_item(pc_item, state); |
644 | 0 | if (pc_item->status != PC_ITEM_COLLIDED) |
645 | 0 | advance_progress_meter(); |
646 | 0 | } |
647 | 0 | } |
648 | | |
649 | | int run_parallel_checkout(struct checkout *state, int num_workers, int threshold, |
650 | | struct progress *progress, unsigned int *progress_cnt) |
651 | 0 | { |
652 | 0 | int ret; |
653 | |
|
654 | 0 | if (parallel_checkout.status != PC_ACCEPTING_ENTRIES) |
655 | 0 | BUG("cannot run parallel checkout: uninitialized or already running"); |
656 | | |
657 | 0 | parallel_checkout.status = PC_RUNNING; |
658 | 0 | parallel_checkout.progress = progress; |
659 | 0 | parallel_checkout.progress_cnt = progress_cnt; |
660 | |
|
661 | 0 | if (parallel_checkout.nr < num_workers) |
662 | 0 | num_workers = parallel_checkout.nr; |
663 | |
|
664 | 0 | if (num_workers <= 1 || parallel_checkout.nr < threshold) { |
665 | 0 | write_items_sequentially(state); |
666 | 0 | } else { |
667 | 0 | struct pc_worker *workers = setup_workers(state, num_workers); |
668 | 0 | gather_results_from_workers(workers, num_workers); |
669 | 0 | finish_workers(workers, num_workers); |
670 | 0 | } |
671 | |
|
672 | 0 | ret = handle_results(state); |
673 | |
|
674 | 0 | finish_parallel_checkout(); |
675 | 0 | return ret; |
676 | 0 | } |