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