6 #include "parallel-checkout.h"
9 #include "run-command.h"
11 #include "streaming.h"
12 #include "thread-utils.h"
16 struct child_process cp
;
17 size_t next_item_to_complete
, nr_items_to_complete
;
20 struct parallel_checkout
{
21 enum pc_status status
;
22 struct parallel_checkout_item
*items
; /* The parallel checkout queue. */
24 struct progress
*progress
;
25 unsigned int *progress_cnt
;
28 static struct parallel_checkout parallel_checkout
;
30 enum pc_status
parallel_checkout_status(void)
32 return parallel_checkout
.status
;
35 static const int DEFAULT_THRESHOLD_FOR_PARALLELISM
= 100;
36 static const int DEFAULT_NUM_WORKERS
= 1;
38 void get_parallel_checkout_configs(int *num_workers
, int *threshold
)
40 char *env_workers
= getenv("GIT_TEST_CHECKOUT_WORKERS");
42 if (env_workers
&& *env_workers
) {
43 if (strtol_i(env_workers
, 10, num_workers
)) {
44 die(_("invalid value for '%s': '%s'"),
45 "GIT_TEST_CHECKOUT_WORKERS", env_workers
);
48 *num_workers
= online_cpus();
54 if (git_config_get_int("checkout.workers", num_workers
))
55 *num_workers
= DEFAULT_NUM_WORKERS
;
56 else if (*num_workers
< 1)
57 *num_workers
= online_cpus();
59 if (git_config_get_int("checkout.thresholdForParallelism", threshold
))
60 *threshold
= DEFAULT_THRESHOLD_FOR_PARALLELISM
;
63 void init_parallel_checkout(void)
65 if (parallel_checkout
.status
!= PC_UNINITIALIZED
)
66 BUG("parallel checkout already initialized");
68 parallel_checkout
.status
= PC_ACCEPTING_ENTRIES
;
71 static void finish_parallel_checkout(void)
73 if (parallel_checkout
.status
== PC_UNINITIALIZED
)
74 BUG("cannot finish parallel checkout: not initialized yet");
76 free(parallel_checkout
.items
);
77 memset(¶llel_checkout
, 0, sizeof(parallel_checkout
));
80 static int is_eligible_for_parallel_checkout(const struct cache_entry
*ce
,
81 const struct conv_attrs
*ca
)
83 enum conv_attrs_classification c
;
84 size_t packed_item_size
;
87 * Symlinks cannot be checked out in parallel as, in case of path
88 * collision, they could racily replace leading directories of other
89 * entries being checked out. Submodules are checked out in child
90 * processes, which have their own parallel checkout queues.
92 if (!S_ISREG(ce
->ce_mode
))
95 packed_item_size
= sizeof(struct pc_item_fixed_portion
) + ce
->ce_namelen
+
96 (ca
->working_tree_encoding
? strlen(ca
->working_tree_encoding
) : 0);
99 * The amount of data we send to the workers per checkout item is
100 * typically small (75~300B). So unless we find an insanely huge path
101 * of 64KB, we should never reach the 65KB limit of one pkt-line. If
102 * that does happen, we let the sequential code handle the item.
104 if (packed_item_size
> LARGE_PACKET_DATA_MAX
)
107 c
= classify_conv_attrs(ca
);
109 case CA_CLASS_INCORE
:
112 case CA_CLASS_INCORE_FILTER
:
114 * It would be safe to allow concurrent instances of
115 * single-file smudge filters, like rot13, but we should not
116 * assume that all filters are parallel-process safe. So we
121 case CA_CLASS_INCORE_PROCESS
:
123 * The parallel queue and the delayed queue are not compatible,
124 * so they must be kept completely separated. And we can't tell
125 * if a long-running process will delay its response without
126 * actually asking it to perform the filtering. Therefore, this
127 * type of filter is not allowed in parallel checkout.
129 * Furthermore, there should only be one instance of the
130 * long-running process filter as we don't know how it is
131 * managing its own concurrency. So, spreading the entries that
132 * requisite such a filter among the parallel workers would
133 * require a lot more inter-process communication. We would
134 * probably have to designate a single process to interact with
135 * the filter and send all the necessary data to it, for each
140 case CA_CLASS_STREAMABLE
:
144 BUG("unsupported conv_attrs classification '%d'", c
);
148 int enqueue_checkout(struct cache_entry
*ce
, struct conv_attrs
*ca
,
149 int *checkout_counter
)
151 struct parallel_checkout_item
*pc_item
;
153 if (parallel_checkout
.status
!= PC_ACCEPTING_ENTRIES
||
154 !is_eligible_for_parallel_checkout(ce
, ca
))
157 ALLOC_GROW(parallel_checkout
.items
, parallel_checkout
.nr
+ 1,
158 parallel_checkout
.alloc
);
160 pc_item
= ¶llel_checkout
.items
[parallel_checkout
.nr
];
162 memcpy(&pc_item
->ca
, ca
, sizeof(pc_item
->ca
));
163 pc_item
->status
= PC_ITEM_PENDING
;
164 pc_item
->id
= parallel_checkout
.nr
;
165 pc_item
->checkout_counter
= checkout_counter
;
166 parallel_checkout
.nr
++;
171 size_t pc_queue_size(void)
173 return parallel_checkout
.nr
;
176 static void advance_progress_meter(void)
178 if (parallel_checkout
.progress
) {
179 (*parallel_checkout
.progress_cnt
)++;
180 display_progress(parallel_checkout
.progress
,
181 *parallel_checkout
.progress_cnt
);
185 static int handle_results(struct checkout
*state
)
189 int have_pending
= 0;
192 * We first update the successfully written entries with the collected
193 * stat() data, so that they can be found by mark_colliding_entries(),
194 * in the next loop, when necessary.
196 for (i
= 0; i
< parallel_checkout
.nr
; i
++) {
197 struct parallel_checkout_item
*pc_item
= ¶llel_checkout
.items
[i
];
198 if (pc_item
->status
== PC_ITEM_WRITTEN
)
199 update_ce_after_write(state
, pc_item
->ce
, &pc_item
->st
);
202 for (i
= 0; i
< parallel_checkout
.nr
; i
++) {
203 struct parallel_checkout_item
*pc_item
= ¶llel_checkout
.items
[i
];
205 switch(pc_item
->status
) {
206 case PC_ITEM_WRITTEN
:
207 if (pc_item
->checkout_counter
)
208 (*pc_item
->checkout_counter
)++;
210 case PC_ITEM_COLLIDED
:
212 * The entry could not be checked out due to a path
213 * collision with another entry. Since there can only
214 * be one entry of each colliding group on the disk, we
215 * could skip trying to check out this one and move on.
216 * However, this would leave the unwritten entries with
217 * null stat() fields on the index, which could
218 * potentially slow down subsequent operations that
219 * require refreshing it: git would not be able to
220 * trust st_size and would have to go to the filesystem
221 * to see if the contents match (see ie_modified()).
223 * Instead, let's pay the overhead only once, now, and
224 * call checkout_entry_ca() again for this file, to
225 * have its stat() data stored in the index. This also
226 * has the benefit of adding this entry and its
227 * colliding pair to the collision report message.
228 * Additionally, this overwriting behavior is consistent
229 * with what the sequential checkout does, so it doesn't
230 * add any extra overhead.
232 ret
|= checkout_entry_ca(pc_item
->ce
, &pc_item
->ca
,
234 pc_item
->checkout_counter
);
235 advance_progress_meter();
237 case PC_ITEM_PENDING
:
244 BUG("unknown checkout item status in parallel checkout");
249 error("parallel checkout finished with pending entries");
254 static int reset_fd(int fd
, const char *path
)
256 if (lseek(fd
, 0, SEEK_SET
) != 0)
257 return error_errno("failed to rewind descriptor of '%s'", path
);
258 if (ftruncate(fd
, 0))
259 return error_errno("failed to truncate file '%s'", path
);
263 static int write_pc_item_to_fd(struct parallel_checkout_item
*pc_item
, int fd
,
267 struct stream_filter
*filter
;
268 struct strbuf buf
= STRBUF_INIT
;
274 assert(is_eligible_for_parallel_checkout(pc_item
->ce
, &pc_item
->ca
));
276 filter
= get_stream_filter_ca(&pc_item
->ca
, &pc_item
->ce
->oid
);
278 if (stream_blob_to_fd(fd
, &pc_item
->ce
->oid
, filter
, 1)) {
279 /* On error, reset fd to try writing without streaming */
280 if (reset_fd(fd
, path
))
287 blob
= read_blob_entry(pc_item
->ce
, &size
);
289 return error("cannot read object %s '%s'",
290 oid_to_hex(&pc_item
->ce
->oid
), pc_item
->ce
->name
);
293 * checkout metadata is used to give context for external process
294 * filters. Files requiring such filters are not eligible for parallel
295 * checkout, so pass NULL. Note: if that changes, the metadata must also
296 * be passed from the main process to the workers.
298 ret
= convert_to_working_tree_ca(&pc_item
->ca
, pc_item
->ce
->name
,
299 blob
, size
, &buf
, NULL
);
304 blob
= strbuf_detach(&buf
, &newsize
);
308 wrote
= write_in_full(fd
, blob
, size
);
311 return error("unable to write file '%s'", path
);
316 static int close_and_clear(int *fd
)
328 void write_pc_item(struct parallel_checkout_item
*pc_item
,
329 struct checkout
*state
)
331 unsigned int mode
= (pc_item
->ce
->ce_mode
& 0100) ? 0777 : 0666;
332 int fd
= -1, fstat_done
= 0;
333 struct strbuf path
= STRBUF_INIT
;
336 strbuf_add(&path
, state
->base_dir
, state
->base_dir_len
);
337 strbuf_add(&path
, pc_item
->ce
->name
, pc_item
->ce
->ce_namelen
);
339 dir_sep
= find_last_dir_sep(path
.buf
);
342 * The leading dirs should have been already created by now. But, in
343 * case of path collisions, one of the dirs could have been replaced by
344 * a symlink (checked out after we enqueued this entry for parallel
345 * checkout). Thus, we must check the leading dirs again.
347 if (dir_sep
&& !has_dirs_only_path(path
.buf
, dir_sep
- path
.buf
,
348 state
->base_dir_len
)) {
349 pc_item
->status
= PC_ITEM_COLLIDED
;
350 trace2_data_string("pcheckout", NULL
, "collision/dirname", path
.buf
);
354 fd
= open(path
.buf
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
);
357 if (errno
== EEXIST
|| errno
== EISDIR
) {
359 * Errors which probably represent a path collision.
360 * Suppress the error message and mark the item to be
361 * retried later, sequentially. ENOTDIR and ENOENT are
362 * also interesting, but the above has_dirs_only_path()
363 * call should have already caught these cases.
365 pc_item
->status
= PC_ITEM_COLLIDED
;
366 trace2_data_string("pcheckout", NULL
,
367 "collision/basename", path
.buf
);
369 error_errno("failed to open file '%s'", path
.buf
);
370 pc_item
->status
= PC_ITEM_FAILED
;
375 if (write_pc_item_to_fd(pc_item
, fd
, path
.buf
)) {
376 /* Error was already reported. */
377 pc_item
->status
= PC_ITEM_FAILED
;
378 close_and_clear(&fd
);
383 fstat_done
= fstat_checkout_output(fd
, state
, &pc_item
->st
);
385 if (close_and_clear(&fd
)) {
386 error_errno("unable to close file '%s'", path
.buf
);
387 pc_item
->status
= PC_ITEM_FAILED
;
391 if (state
->refresh_cache
&& !fstat_done
&& lstat(path
.buf
, &pc_item
->st
) < 0) {
392 error_errno("unable to stat just-written file '%s'", path
.buf
);
393 pc_item
->status
= PC_ITEM_FAILED
;
397 pc_item
->status
= PC_ITEM_WRITTEN
;
400 strbuf_release(&path
);
403 static void send_one_item(int fd
, struct parallel_checkout_item
*pc_item
)
406 char *data
, *variant
;
407 struct pc_item_fixed_portion
*fixed_portion
;
408 const char *working_tree_encoding
= pc_item
->ca
.working_tree_encoding
;
409 size_t name_len
= pc_item
->ce
->ce_namelen
;
410 size_t working_tree_encoding_len
= working_tree_encoding
?
411 strlen(working_tree_encoding
) : 0;
414 * Any changes in the calculation of the message size must also be made
415 * in is_eligible_for_parallel_checkout().
417 len_data
= sizeof(struct pc_item_fixed_portion
) + name_len
+
418 working_tree_encoding_len
;
420 data
= xmalloc(len_data
);
422 fixed_portion
= (struct pc_item_fixed_portion
*)data
;
423 fixed_portion
->id
= pc_item
->id
;
424 fixed_portion
->ce_mode
= pc_item
->ce
->ce_mode
;
425 fixed_portion
->crlf_action
= pc_item
->ca
.crlf_action
;
426 fixed_portion
->ident
= pc_item
->ca
.ident
;
427 fixed_portion
->name_len
= name_len
;
428 fixed_portion
->working_tree_encoding_len
= working_tree_encoding_len
;
430 * We pad the unused bytes in the hash array because, otherwise,
431 * Valgrind would complain about passing uninitialized bytes to a
432 * write() syscall. The warning doesn't represent any real risk here,
433 * but it could hinder the detection of actual errors.
435 oidcpy_with_padding(&fixed_portion
->oid
, &pc_item
->ce
->oid
);
437 variant
= data
+ sizeof(*fixed_portion
);
438 if (working_tree_encoding_len
) {
439 memcpy(variant
, working_tree_encoding
, working_tree_encoding_len
);
440 variant
+= working_tree_encoding_len
;
442 memcpy(variant
, pc_item
->ce
->name
, name_len
);
444 packet_write(fd
, data
, len_data
);
449 static void send_batch(int fd
, size_t start
, size_t nr
)
452 sigchain_push(SIGPIPE
, SIG_IGN
);
453 for (i
= 0; i
< nr
; i
++)
454 send_one_item(fd
, ¶llel_checkout
.items
[start
+ i
]);
456 sigchain_pop(SIGPIPE
);
459 static struct pc_worker
*setup_workers(struct checkout
*state
, int num_workers
)
461 struct pc_worker
*workers
;
462 int i
, workers_with_one_extra_item
;
463 size_t base_batch_size
, batch_beginning
= 0;
465 ALLOC_ARRAY(workers
, num_workers
);
467 for (i
= 0; i
< num_workers
; i
++) {
468 struct child_process
*cp
= &workers
[i
].cp
;
470 child_process_init(cp
);
474 cp
->clean_on_exit
= 1;
475 strvec_push(&cp
->args
, "checkout--worker");
476 if (state
->base_dir_len
)
477 strvec_pushf(&cp
->args
, "--prefix=%s", state
->base_dir
);
478 if (start_command(cp
))
479 die("failed to spawn checkout worker");
482 base_batch_size
= parallel_checkout
.nr
/ num_workers
;
483 workers_with_one_extra_item
= parallel_checkout
.nr
% num_workers
;
485 for (i
= 0; i
< num_workers
; i
++) {
486 struct pc_worker
*worker
= &workers
[i
];
487 size_t batch_size
= base_batch_size
;
489 /* distribute the extra work evenly */
490 if (i
< workers_with_one_extra_item
)
493 send_batch(worker
->cp
.in
, batch_beginning
, batch_size
);
494 worker
->next_item_to_complete
= batch_beginning
;
495 worker
->nr_items_to_complete
= batch_size
;
497 batch_beginning
+= batch_size
;
503 static void finish_workers(struct pc_worker
*workers
, int num_workers
)
508 * Close pipes before calling finish_command() to let the workers
509 * exit asynchronously and avoid spending extra time on wait().
511 for (i
= 0; i
< num_workers
; i
++) {
512 struct child_process
*cp
= &workers
[i
].cp
;
519 for (i
= 0; i
< num_workers
; i
++) {
520 int rc
= finish_command(&workers
[i
].cp
);
523 * For a normal non-zero exit, the worker should have
524 * already printed something useful to stderr. But a
525 * death by signal should be mentioned to the user.
527 error("checkout worker %d died of signal %d", i
, rc
- 128);
534 static inline void assert_pc_item_result_size(int got
, int exp
)
537 BUG("wrong result size from checkout worker (got %dB, exp %dB)",
541 static void parse_and_save_result(const char *buffer
, int len
,
542 struct pc_worker
*worker
)
544 struct pc_item_result
*res
;
545 struct parallel_checkout_item
*pc_item
;
546 struct stat
*st
= NULL
;
548 if (len
< PC_ITEM_RESULT_BASE_SIZE
)
549 BUG("too short result from checkout worker (got %dB, exp >=%dB)",
550 len
, (int)PC_ITEM_RESULT_BASE_SIZE
);
552 res
= (struct pc_item_result
*)buffer
;
555 * Worker should send either the full result struct on success, or
556 * just the base (i.e. no stat data), otherwise.
558 if (res
->status
== PC_ITEM_WRITTEN
) {
559 assert_pc_item_result_size(len
, (int)sizeof(struct pc_item_result
));
562 assert_pc_item_result_size(len
, (int)PC_ITEM_RESULT_BASE_SIZE
);
565 if (!worker
->nr_items_to_complete
)
566 BUG("received result from supposedly finished checkout worker");
567 if (res
->id
!= worker
->next_item_to_complete
)
568 BUG("unexpected item id from checkout worker (got %"PRIuMAX
", exp %"PRIuMAX
")",
569 (uintmax_t)res
->id
, (uintmax_t)worker
->next_item_to_complete
);
571 worker
->next_item_to_complete
++;
572 worker
->nr_items_to_complete
--;
574 pc_item
= ¶llel_checkout
.items
[res
->id
];
575 pc_item
->status
= res
->status
;
579 if (res
->status
!= PC_ITEM_COLLIDED
)
580 advance_progress_meter();
583 static void gather_results_from_workers(struct pc_worker
*workers
,
586 int i
, active_workers
= num_workers
;
589 CALLOC_ARRAY(pfds
, num_workers
);
590 for (i
= 0; i
< num_workers
; i
++) {
591 pfds
[i
].fd
= workers
[i
].cp
.out
;
592 pfds
[i
].events
= POLLIN
;
595 while (active_workers
) {
596 int nr
= poll(pfds
, num_workers
, -1);
601 die_errno("failed to poll checkout workers");
604 for (i
= 0; i
< num_workers
&& nr
> 0; i
++) {
605 struct pc_worker
*worker
= &workers
[i
];
606 struct pollfd
*pfd
= &pfds
[i
];
611 if (pfd
->revents
& POLLIN
) {
612 int len
= packet_read(pfd
->fd
, packet_buffer
,
613 sizeof(packet_buffer
), 0);
616 BUG("packet_read() returned negative value");
621 parse_and_save_result(packet_buffer
,
624 } else if (pfd
->revents
& POLLHUP
) {
627 } else if (pfd
->revents
& (POLLNVAL
| POLLERR
)) {
628 die("error polling from checkout worker");
638 static void write_items_sequentially(struct checkout
*state
)
642 for (i
= 0; i
< parallel_checkout
.nr
; i
++) {
643 struct parallel_checkout_item
*pc_item
= ¶llel_checkout
.items
[i
];
644 write_pc_item(pc_item
, state
);
645 if (pc_item
->status
!= PC_ITEM_COLLIDED
)
646 advance_progress_meter();
650 int run_parallel_checkout(struct checkout
*state
, int num_workers
, int threshold
,
651 struct progress
*progress
, unsigned int *progress_cnt
)
655 if (parallel_checkout
.status
!= PC_ACCEPTING_ENTRIES
)
656 BUG("cannot run parallel checkout: uninitialized or already running");
658 parallel_checkout
.status
= PC_RUNNING
;
659 parallel_checkout
.progress
= progress
;
660 parallel_checkout
.progress_cnt
= progress_cnt
;
662 if (parallel_checkout
.nr
< num_workers
)
663 num_workers
= parallel_checkout
.nr
;
665 if (num_workers
<= 1 || parallel_checkout
.nr
< threshold
) {
666 write_items_sequentially(state
);
668 struct pc_worker
*workers
= setup_workers(state
, num_workers
);
669 gather_results_from_workers(workers
, num_workers
);
670 finish_workers(workers
, num_workers
);
673 ret
= handle_results(state
);
675 finish_parallel_checkout();