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