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