git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
fee93460c12db4fe60998b012d1267544a33377c blob 14617 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
 
#include "cache.h"
#include "entry.h"
#include "parallel-checkout.h"
#include "pkt-line.h"
#include "progress.h"
#include "run-command.h"
#include "streaming.h"
#include "thread-utils.h"
#include "config.h"

struct parallel_checkout {
	struct checkout_item *items;
	size_t nr, alloc;
	struct progress *progress;
	unsigned int *progress_cnt;
};

static struct parallel_checkout *parallel_checkout = NULL;
static enum pc_status pc_status = PC_UNINITIALIZED;

enum pc_status parallel_checkout_status(void)
{
	return pc_status;
}

#define DEFAULT_WORKERS_THRESHOLD 0

void get_parallel_checkout_configs(int *num_workers, int *threshold)
{
	if (git_config_get_int("checkout.workers", num_workers) ||
	    *num_workers < 1)
		*num_workers = online_cpus();

	if (git_config_get_int("checkout.workersThreshold", threshold) ||
	    *threshold < 0)
		*threshold = DEFAULT_WORKERS_THRESHOLD;
}

void init_parallel_checkout(void)
{
	if (parallel_checkout)
		BUG("parallel checkout already initialized");

	parallel_checkout = xcalloc(1, sizeof(*parallel_checkout));
	pc_status = PC_ACCEPTING_ENTRIES;
}

static void finish_parallel_checkout(void)
{
	if (!parallel_checkout)
		BUG("cannot finish parallel checkout: not initialized yet");

	free(parallel_checkout->items);
	FREE_AND_NULL(parallel_checkout);
	pc_status = PC_UNINITIALIZED;
}

static int is_eligible_for_parallel_checkout(const struct cache_entry *ce,
					     const struct conv_attrs *ca)
{
	enum conv_attrs_classification c;

	if (!S_ISREG(ce->ce_mode))
		return 0;

	c = classify_conv_attrs(ca);
	switch (c) {
	case CA_CLASS_INCORE:
		return 1;

	case CA_CLASS_INCORE_FILTER:
		/*
		 * It would be safe to allow concurrent instances of
		 * single-file smudge filters, like rot13, but we should not
		 * assume that all filters are parallel-process safe. So we
		 * don't allow this.
		 */
		return 0;

	case CA_CLASS_INCORE_PROCESS:
		/*
		 * The parallel queue and the delayed queue are not compatible,
		 * so they must be kept completely separated. And we can't tell
		 * if a long-running process will delay its response without
		 * actually asking it to perform the filtering. Therefore, this
		 * type of filter is not allowed in parallel checkout.
		 *
		 * Furthermore, there should only be one instance of the
		 * long-running process filter as we don't know how it is
		 * managing its own concurrency. So, spreading the entries that
		 * requisite such a filter among the parallel workers would
		 * require a lot more inter-process communication. We would
		 * probably have to designate a single process to interact with
		 * the filter and send all the necessary data to it, for each
		 * entry.
		 */
		return 0;

	case CA_CLASS_STREAMABLE:
		return 1;

	default:
		BUG("unsupported conv_attrs classification '%d'", c);
	}
}

int enqueue_checkout(struct cache_entry *ce, struct conv_attrs *ca)
{
	struct checkout_item *ci;

	if (!parallel_checkout || pc_status != PC_ACCEPTING_ENTRIES ||
	    !is_eligible_for_parallel_checkout(ce, ca))
		return -1;

	ALLOC_GROW(parallel_checkout->items, parallel_checkout->nr + 1,
		   parallel_checkout->alloc);

	ci = &parallel_checkout->items[parallel_checkout->nr];
	ci->ce = ce;
	memcpy(&ci->ca, ca, sizeof(ci->ca));
	ci->id = parallel_checkout->nr;
	parallel_checkout->nr++;

	return 0;
}

size_t pc_queue_size(void)
{
	if (!parallel_checkout)
		return 0;
	return parallel_checkout->nr;
}

static void advance_progress_meter(void)
{
	if (parallel_checkout && parallel_checkout->progress) {
		(*parallel_checkout->progress_cnt)++;
		display_progress(parallel_checkout->progress,
				 *parallel_checkout->progress_cnt);
	}
}

struct symlink_checkout_item {
	struct cache_entry *ce;
	int *nr_checkouts;
};

static struct symlink_checkout_item *symlink_queue = NULL;
static size_t symlink_queue_nr = 0, symlink_queue_alloc = 0;

void enqueue_symlink_checkout(struct cache_entry *ce, int *nr_checkouts)
{
	assert(S_ISLNK(ce->ce_mode));
	ALLOC_GROW(symlink_queue, symlink_queue_nr + 1, symlink_queue_alloc);
	symlink_queue[symlink_queue_nr].ce = ce;
	symlink_queue[symlink_queue_nr].nr_checkouts = nr_checkouts;
	symlink_queue_nr++;
}

size_t symlink_queue_size(void)
{
	return symlink_queue_nr;
}

static int checkout_symlink_queue(struct checkout *state)
{
	size_t i;
	int ret = 0;

	for (i = 0; i < symlink_queue_nr; ++i) {
		struct symlink_checkout_item *sci = &symlink_queue[i];
		ret |= checkout_entry(sci->ce, state, NULL, sci->nr_checkouts);
		advance_progress_meter();
	}

	FREE_AND_NULL(symlink_queue);
	symlink_queue_nr = symlink_queue_alloc = 0;
	return ret;
}

static int handle_results(struct checkout *state)
{
	int ret = 0;
	size_t i;

	pc_status = PC_HANDLING_RESULTS;

	for (i = 0; i < parallel_checkout->nr; ++i) {
		struct checkout_item *ci = &parallel_checkout->items[i];
		struct stat *st = &ci->st;

		/*
		 * Note: progress meter was already incremented for CI_SUCCESS
		 * and CI_FAILED.
		 */
		switch(ci->status) {
		case CI_SUCCESS:
			update_ce_after_write(state, ci->ce, st);
			break;
		case CI_RETRY:
			/*
			 * The fails for which we set CI_RETRY are the ones
			 * that might have been caused by a path collision. So
			 * we let checkout_entry_ca() retry writing, as it will
			 * properly handle collisions and the creation of
			 * leading dirs in the entry's path.
			 */
			ret |= checkout_entry_ca(ci->ce, &ci->ca, state, NULL, NULL);
			advance_progress_meter();
			break;
		case CI_FAILED:
			ret = -1;
			break;
		case CI_PENDING:
			BUG("parallel checkout finished with pending entries");
		default:
			BUG("unknown checkout item status in parallel checkout");
		}
	}

	return ret;
}

static int reset_fd(int fd, const char *path)
{
	if (lseek(fd, 0, SEEK_SET) != 0)
		return error_errno("failed to rewind descriptor of %s", path);
	if (ftruncate(fd, 0))
		return error_errno("failed to truncate file %s", path);
	return 0;
}

static int write_checkout_item_to_fd(int fd, struct checkout *state,
				     struct checkout_item *ci, const char *path)
{
	int ret;
	struct stream_filter *filter;
	struct strbuf buf = STRBUF_INIT;
	char *new_blob;
	unsigned long size;
	size_t newsize = 0;
	ssize_t wrote;

	/* Sanity check */
	assert(is_eligible_for_parallel_checkout(ci->ce, &ci->ca));

	filter = get_stream_filter_ca(&ci->ca, &ci->ce->oid);
	if (filter) {
		if (stream_blob_to_fd(fd, &ci->ce->oid, filter, 1)) {
			/* On error, reset fd to try writing without streaming */
			if (reset_fd(fd, path))
				return -1;
		} else {
			return 0;
		}
	}

	new_blob = read_blob_entry(ci->ce, &size);
	if (!new_blob)
		return error("unable to read sha1 file of %s (%s)", path,
			     oid_to_hex(&ci->ce->oid));

	/*
	 * checkout metadata is used to give context for external process
	 * filters. Files requiring such filters are not eligible for parallel
	 * checkout, so pass NULL. Note: if that changes, the metadata must also
	 * be passed from the main process to the workers.
	 */
	ret = convert_to_working_tree_ca(&ci->ca, ci->ce->name, new_blob, size,
					 &buf, NULL);

	if (ret) {
		free(new_blob);
		new_blob = strbuf_detach(&buf, &newsize);
		size = newsize;
	}

	wrote = write_in_full(fd, new_blob, size);
	free(new_blob);
	if (wrote < 0)
		return error("unable to write file %s", path);

	return 0;
}

static int close_and_clear(int *fd)
{
	int ret = 0;

	if (*fd >= 0) {
		ret = close(*fd);
		*fd = -1;
	}

	return ret;
}

void write_checkout_item(struct checkout *state, struct checkout_item *ci)
{
	unsigned int mode = (ci->ce->ce_mode & 0100) ? 0777 : 0666;
	int fd = -1, fstat_done = 0;
	struct strbuf path = STRBUF_INIT;

	strbuf_add(&path, state->base_dir, state->base_dir_len);
	strbuf_add(&path, ci->ce->name, ci->ce->ce_namelen);

	fd = open(path.buf, O_WRONLY | O_CREAT | O_EXCL, mode);

	if (fd < 0) {
		if (errno == EEXIST || errno == EISDIR || errno == ENOENT ||
		    errno == ENOTDIR) {
			/*
			 * Errors which probably represent a path collision.
			 * Suppress the error message and mark the ci to be
			 * retried later, sequentially.
			 */
			ci->status = CI_RETRY;
		} else {
			error_errno("failed to open file %s", path.buf);
			ci->status = CI_FAILED;
		}
		goto out;
	}

	if (write_checkout_item_to_fd(fd, state, ci, path.buf)) {
		/* Error was already reported. */
		ci->status = CI_FAILED;
		goto out;
	}

	fstat_done = fstat_checkout_output(fd, state, &ci->st);

	if (close_and_clear(&fd)) {
		error_errno("unable to close file %s", path.buf);
		ci->status = CI_FAILED;
		goto out;
	}

	if (state->refresh_cache && !fstat_done && lstat(path.buf, &ci->st) < 0) {
		error_errno("unable to stat just-written file %s",  path.buf);
		ci->status = CI_FAILED;
		goto out;
	}

	ci->status = CI_SUCCESS;

out:
	/*
	 * No need to check close() return. At this point, either fd is already
	 * closed, or we are on an error path, that has already been reported.
	 */
	close_and_clear(&fd);
	strbuf_release(&path);
}

static void send_one_item(int fd, struct checkout_item *ci)
{
	size_t len_data;
	char *data, *variant;
	struct ci_fixed_portion *fixed_portion;
	const char *working_tree_encoding = ci->ca.working_tree_encoding;
	size_t name_len = ci->ce->ce_namelen;
	size_t working_tree_encoding_len = working_tree_encoding ?
					   strlen(working_tree_encoding) : 0;

	len_data = sizeof(struct ci_fixed_portion) + name_len +
		   working_tree_encoding_len;

	data = xcalloc(1, len_data);

	fixed_portion = (struct ci_fixed_portion *)data;
	fixed_portion->id = ci->id;
	oidcpy(&fixed_portion->oid, &ci->ce->oid);
	fixed_portion->ce_mode = ci->ce->ce_mode;
	fixed_portion->attr_action = ci->ca.attr_action;
	fixed_portion->crlf_action = ci->ca.crlf_action;
	fixed_portion->ident = ci->ca.ident;
	fixed_portion->name_len = name_len;
	fixed_portion->working_tree_encoding_len = working_tree_encoding_len;

	variant = data + sizeof(*fixed_portion);
	if (working_tree_encoding_len) {
		memcpy(variant, working_tree_encoding, working_tree_encoding_len);
		variant += working_tree_encoding_len;
	}
	memcpy(variant, ci->ce->name, name_len);

	packet_write(fd, data, len_data);

	free(data);
}

static void send_batch(int fd, size_t start, size_t nr)
{
	size_t i;
	for (i = 0; i < nr; ++i)
		send_one_item(fd, &parallel_checkout->items[start + i]);
	packet_flush(fd);
}

static struct child_process *setup_workers(struct checkout *state, int num_workers)
{
	struct child_process *workers;
	int i, workers_with_one_extra_item;
	size_t base_batch_size, next_to_assign = 0;

	base_batch_size = parallel_checkout->nr / num_workers;
	workers_with_one_extra_item = parallel_checkout->nr % num_workers;
	ALLOC_ARRAY(workers, num_workers);

	for (i = 0; i < num_workers; ++i) {
		struct child_process *cp = &workers[i];
		size_t batch_size = base_batch_size;

		child_process_init(cp);
		cp->git_cmd = 1;
		cp->in = -1;
		cp->out = -1;
		strvec_push(&cp->args, "checkout--helper");
		if (state->base_dir_len)
			strvec_pushf(&cp->args, "--prefix=%s", state->base_dir);
		if (start_command(cp))
			die(_("failed to spawn checkout worker"));

		/* distribute the extra work evenly */
		if (i < workers_with_one_extra_item)
			batch_size++;

		send_batch(cp->in, next_to_assign, batch_size);
		next_to_assign += batch_size;
	}

	return workers;
}

static void finish_workers(struct child_process *workers, int num_workers)
{
	int i;
	for (i = 0; i < num_workers; ++i) {
		struct child_process *w = &workers[i];
		if (w->in >= 0)
			close(w->in);
		if (w->out >= 0)
			close(w->out);
		if (finish_command(w))
			die(_("checkout worker finished with error"));
	}
	free(workers);
}

static void parse_and_save_result(const char *line, int len)
{
	struct ci_result *res;
	struct checkout_item *ci;

	/*
	 * Worker should send either the full result struct or just the base
	 * (i.e. no stat data).
	 */
	if (len != ci_result_base_size() && len != sizeof(struct ci_result))
		BUG("received corrupted item from checkout worker");

	res = (struct ci_result *)line;

	if (res->id > parallel_checkout->nr)
		BUG("checkout worker sent unknown item id");

	ci = &parallel_checkout->items[res->id];
	ci->status = res->status;

	/*
	 * Worker only sends stat data on success. Otherwise, we *cannot* access
	 * res->st as that will be an invalid address.
	 */
	if (res->status == CI_SUCCESS)
		ci->st = res->st;

	if (res->status != CI_RETRY)
		advance_progress_meter();
}

static void gather_results_from_workers(struct child_process *workers,
					int num_workers)
{
	int i, active_workers = num_workers;
	struct pollfd *pfds;

	CALLOC_ARRAY(pfds, num_workers);
	for (i = 0; i < num_workers; ++i) {
		pfds[i].fd = workers[i].out;
		pfds[i].events = POLLIN;
	}

	while (active_workers) {
		int nr = poll(pfds, num_workers, -1);

		if (nr < 0) {
			if (errno == EINTR)
				continue;
			die_errno("failed to poll checkout workers");
		}

		for (i = 0; i < num_workers && nr > 0; ++i) {
			struct pollfd *pfd = &pfds[i];

			if (!pfd->revents)
				continue;

			if (pfd->revents & POLLIN) {
				int len;
				const char *line = packet_read_line(pfd->fd, &len);

				if (!line) {
					pfd->fd = -1;
					active_workers--;
				} else {
					parse_and_save_result(line, len);
				}
			} else if (pfd->revents & POLLHUP) {
				pfd->fd = -1;
				active_workers--;
			} else if (pfd->revents & (POLLNVAL | POLLERR)) {
				die(_("error polling from checkout worker"));
			}

			nr--;
		}
	}

	free(pfds);
}

static int run_checkout_sequentially(struct checkout *state)
{
	size_t i;
	for (i = 0; i < parallel_checkout->nr; ++i) {
		struct checkout_item *ci = &parallel_checkout->items[i];
		write_checkout_item(state, ci);
		if (ci->status != CI_RETRY)
			advance_progress_meter();
	}
	return handle_results(state) | checkout_symlink_queue(state);
}

int run_parallel_checkout(struct checkout *state, int num_workers, int threshold,
			  struct progress *progress, unsigned int *progress_cnt)
{
	int ret = 0;
	struct child_process *workers;

	if (!parallel_checkout)
		BUG("cannot run parallel checkout: not initialized yet");

	if (num_workers < 1)
		BUG("invalid number of workers for run_parallel_checkout: %d",
		    num_workers);

	pc_status = PC_RUNNING;
	parallel_checkout->progress = progress;
	parallel_checkout->progress_cnt = progress_cnt;

	if (parallel_checkout->nr == 0) {
		goto done;
	} else if (parallel_checkout->nr < threshold || num_workers == 1) {
		ret = run_checkout_sequentially(state);
		goto done;
	}

	workers = setup_workers(state, num_workers);
	gather_results_from_workers(workers, num_workers);
	finish_workers(workers, num_workers);
	ret |= handle_results(state);
	ret |= checkout_symlink_queue(state);

done:
	finish_parallel_checkout();
	return ret;
}
debug log:

solving fee93460c1 ...
found fee93460c1 in https://public-inbox.org/git/79de52b6952441e77d7276243b4b2ebe7ca16a1f.1597093021.git.matheus.bernardino@usp.br/
found 78bf2de5ea in https://public-inbox.org/git/e271d73091de42b30123ae1fffaadb61ad23de44.1597093021.git.matheus.bernardino@usp.br/
found e0fca4d380 in https://public-inbox.org/git/1263342110bfc96480146df0f876a5c936b61ce1.1597093021.git.matheus.bernardino@usp.br/
found ec42342bc8 in https://public-inbox.org/git/7e7527ef3e8a9e71a012f1623e9642c47f7f741c.1597093021.git.matheus.bernardino@usp.br/
found e3b44eeb34 in https://public-inbox.org/git/1b39a4099a69f2c42211d46d615055c783703fea.1597093021.git.matheus.bernardino@usp.br/

applying [1/5] https://public-inbox.org/git/1b39a4099a69f2c42211d46d615055c783703fea.1597093021.git.matheus.bernardino@usp.br/
diff --git a/parallel-checkout.c b/parallel-checkout.c
new file mode 100644
index 0000000000..e3b44eeb34


applying [2/5] https://public-inbox.org/git/7e7527ef3e8a9e71a012f1623e9642c47f7f741c.1597093021.git.matheus.bernardino@usp.br/
diff --git a/parallel-checkout.c b/parallel-checkout.c
index e3b44eeb34..ec42342bc8 100644


applying [3/5] https://public-inbox.org/git/1263342110bfc96480146df0f876a5c936b61ce1.1597093021.git.matheus.bernardino@usp.br/
diff --git a/parallel-checkout.c b/parallel-checkout.c
index ec42342bc8..e0fca4d380 100644


applying [4/5] https://public-inbox.org/git/e271d73091de42b30123ae1fffaadb61ad23de44.1597093021.git.matheus.bernardino@usp.br/
diff --git a/parallel-checkout.c b/parallel-checkout.c
index e0fca4d380..78bf2de5ea 100644


applying [5/5] https://public-inbox.org/git/79de52b6952441e77d7276243b4b2ebe7ca16a1f.1597093021.git.matheus.bernardino@usp.br/
diff --git a/parallel-checkout.c b/parallel-checkout.c
index 78bf2de5ea..fee93460c1 100644

Checking patch parallel-checkout.c...
Applied patch parallel-checkout.c cleanly.
Checking patch parallel-checkout.c...
Applied patch parallel-checkout.c cleanly.
Checking patch parallel-checkout.c...
Applied patch parallel-checkout.c cleanly.
Checking patch parallel-checkout.c...
Applied patch parallel-checkout.c cleanly.
Checking patch parallel-checkout.c...
Applied patch parallel-checkout.c cleanly.

index at:
100644 fee93460c12db4fe60998b012d1267544a33377c	parallel-checkout.c

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).