run-command API: have run_process_parallel() take an "opts" struct

As noted in fd3aaf53f71 (run-command: add an "ungroup" option to run_process_parallel(), 2022-06-07) which added the "ungroup" passing it to "run_process_parallel()" via the global "run_processes_parallel_ungroup" variable was a compromise to get the smallest possible regression fix for "maint" at the time. This follow-up to that is a start at passing that parameter and others via a new "struct run_process_parallel_opts", as the earlier version[1] of what became fd3aaf53f71 did. Since we need to change all of the occurrences of "n" to "opt->SOMETHING" let's take the opportunity and rename the terse "n" to "processes". We could also have picked "max_processes", "jobs", "threads" etc., but as the API is named "run_processes_parallel()" let's go with "processes". Since the new "run_processes_parallel()" function is able to take an optional "tr2_category" and "tr2_label" via the struct we can at this point migrate all of the users of "run_processes_parallel_tr2()" over to it. But let's not migrate all the API users yet, only the two users that passed the "ungroup" parameter via the "run_processes_parallel_ungroup" global 1. https://lore.kernel.org/git/cover-v2-0.8-00000000000-20220518T195858Z-avarab@gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 12, 2022 at 23:02 UTC 6e5ba0bae447dba2adabea588b248b5fc6f59cd6
4 files changed +121 -59
hook.c
+14 -9
@@ -114,8 +114,20 @@ int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
114 .options = options,
115 };
116 const char *const hook_path = find_hook(hook_name);
117 - int jobs = 1;
117 int ret = 0;
118 + const struct run_process_parallel_opts opts = {
119 + .tr2_category = "hook",
120 + .tr2_label = hook_name,
121 +
122 + .processes = 1,
123 + .ungroup = 1,
124 +
125 + .get_next_task = pick_next_hook,
126 + .start_failure = notify_start_failure,
127 + .task_finished = notify_hook_finished,
128 +
129 + .data = &cb_data,
130 + };
131
132 if (!options)
133 BUG("a struct run_hooks_opt must be provided to run_hooks");
@@ -137,14 +149,7 @@ int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
149 cb_data.hook_path = abs_path.buf;
150 }
151
140 - run_processes_parallel_ungroup = 1;
141 - run_processes_parallel_tr2(jobs,
142 - pick_next_hook,
143 - notify_start_failure,
144 - notify_hook_finished,
145 - &cb_data,
146 - "hook",
147 - hook_name);
152 + run_processes_parallel(&opts);
153 ret = cb_data.rc;
154 cleanup:
155 strbuf_release(&abs_path);
run-command.c
+31 -23
@@ -1496,7 +1496,6 @@ enum child_state {
1496 GIT_CP_WAIT_CLEANUP,
1497 };
1498
1499 -int run_processes_parallel_ungroup;
1499 struct parallel_processes {
1500 void *const data;
1501
@@ -1558,11 +1557,12 @@ static void handle_children_on_signal(int signo)
1557 }
1558
1559 static void pp_init(struct parallel_processes *pp,
1561 - get_next_task_fn get_next_task,
1562 - start_failure_fn start_failure,
1563 - task_finished_fn task_finished)
1560 + const struct run_process_parallel_opts *opts)
1561 {
1565 - const size_t n = pp->max_processes;
1562 + const size_t n = opts->processes;
1563 + get_next_task_fn get_next_task = opts->get_next_task;
1564 + start_failure_fn start_failure = opts->start_failure;
1565 + task_finished_fn task_finished = opts->task_finished;
1566
1567 if (!n)
1568 BUG("you must provide a non-zero number of processes!");
@@ -1769,27 +1769,27 @@ static int pp_collect_finished(struct parallel_processes *pp)
1769 return result;
1770 }
1771
1772 -void run_processes_parallel(size_t n,
1773 - get_next_task_fn get_next_task,
1774 - start_failure_fn start_failure,
1775 - task_finished_fn task_finished,
1776 - void *pp_cb)
1772 +void run_processes_parallel(const struct run_process_parallel_opts *opts)
1773 {
1774 int i, code;
1775 int output_timeout = 100;
1776 int spawn_cap = 4;
1781 - int ungroup = run_processes_parallel_ungroup;
1777 struct parallel_processes pp = {
1783 - .max_processes = n,
1784 - .data = pp_cb,
1778 + .max_processes = opts->processes,
1779 + .data = opts->data,
1780 .buffered_output = STRBUF_INIT,
1786 - .ungroup = ungroup,
1781 + .ungroup = opts->ungroup,
1782 };
1783 + /* options */
1784 + const char *tr2_category = opts->tr2_category;
1785 + const char *tr2_label = opts->tr2_label;
1786 + const int do_trace2 = tr2_category && tr2_label;
1787
1789 - /* unset for the next API user */
1790 - run_processes_parallel_ungroup = 0;
1788 + if (do_trace2)
1789 + trace2_region_enter_printf(tr2_category, tr2_label, NULL,
1790 + "max:%d", opts->processes);
1791
1792 - pp_init(&pp, get_next_task, start_failure, task_finished);
1792 + pp_init(&pp, opts);
1793 while (1) {
1794 for (i = 0;
1795 i < spawn_cap && !pp.shutdown &&
@@ -1806,7 +1806,7 @@ void run_processes_parallel(size_t n,
1806 }
1807 if (!pp.nr_processes)
1808 break;
1809 - if (ungroup) {
1809 + if (opts->ungroup) {
1810 for (size_t i = 0; i < pp.max_processes; i++)
1811 pp.children[i].state = GIT_CP_WAIT_CLEANUP;
1812 } else {
@@ -1822,19 +1822,27 @@ void run_processes_parallel(size_t n,
1822 }
1823
1824 pp_cleanup(&pp);
1825 +
1826 + if (do_trace2)
1827 + trace2_region_leave(tr2_category, tr2_label, NULL);
1828 }
1829
1827 -void run_processes_parallel_tr2(size_t n, get_next_task_fn get_next_task,
1830 +void run_processes_parallel_tr2(size_t processes, get_next_task_fn get_next_task,
1831 start_failure_fn start_failure,
1832 task_finished_fn task_finished, void *pp_cb,
1833 const char *tr2_category, const char *tr2_label)
1834 {
1832 - trace2_region_enter_printf(tr2_category, tr2_label, NULL, "max:%d", n);
1835 + const struct run_process_parallel_opts opts = {
1836 + .tr2_category = tr2_category,
1837 + .tr2_label = tr2_label,
1838 + .processes = processes,
1839
1834 - run_processes_parallel(n, get_next_task, start_failure,
1835 - task_finished, pp_cb);
1840 + .get_next_task = get_next_task,
1841 + .start_failure = start_failure,
1842 + .task_finished = task_finished,
1843 + };
1844
1837 - trace2_region_leave(tr2_category, tr2_label, NULL);
1845 + run_processes_parallel(&opts);
1846 }
1847
1848 int run_auto_maintenance(int quiet)
run-command.h
+55 -17
@@ -459,17 +459,64 @@ typedef int (*task_finished_fn)(int result,
459 void *pp_task_cb);
460
461 /**
462 - * Runs up to n processes at the same time. Whenever a process can be
463 - * started, the callback get_next_task_fn is called to obtain the data
462 + * Option used by run_processes_parallel(), { 0 }-initialized means no
463 + * options.
464 + */
465 +struct run_process_parallel_opts
466 +{
467 + /**
468 + * tr2_category & tr2_label: sets the trace2 category and label for
469 + * logging. These must either be unset, or both of them must be set.
470 + */
471 + const char *tr2_category;
472 + const char *tr2_label;
473 +
474 + /**
475 + * processes: see 'processes' in run_processes_parallel() below.
476 + */
477 + size_t processes;
478 +
479 + /**
480 + * ungroup: see 'ungroup' in run_processes_parallel() below.
481 + */
482 + unsigned int ungroup:1;
483 +
484 + /**
485 + * get_next_task: See get_next_task_fn() above. This must be
486 + * specified.
487 + */
488 + get_next_task_fn get_next_task;
489 +
490 + /**
491 + * start_failure: See start_failure_fn() above. This can be
492 + * NULL to omit any special handling.
493 + */
494 + start_failure_fn start_failure;
495 +
496 + /**
497 + * task_finished: See task_finished_fn() above. This can be
498 + * NULL to omit any special handling.
499 + */
500 + task_finished_fn task_finished;
501 +
502 + /**
503 + * data: user data, will be passed as "pp_cb" to the callback
504 + * parameters.
505 + */
506 + void *data;
507 +};
508 +
509 +/**
510 + * Options are passed via the "struct run_process_parallel_opts" above.
511 + *
512 + * Runs N 'processes' at the same time. Whenever a process can be
513 + * started, the callback opts.get_next_task is called to obtain the data
514 * required to start another child process.
515 *
516 * The children started via this function run in parallel. Their output
517 * (both stdout and stderr) is routed to stderr in a manner that output
518 * from different tasks does not interleave (but see "ungroup" below).
519 *
470 - * start_failure_fn and task_finished_fn can be NULL to omit any
471 - * special handling.
472 - *
520 * If the "ungroup" option isn't specified, the API will set the
521 * "stdout_to_stderr" parameter in "struct child_process" and provide
522 * the callbacks with a "struct strbuf *out" parameter to write output
@@ -479,19 +526,10 @@ typedef int (*task_finished_fn)(int result,
526 * NULL "struct strbuf *out" parameter, and are responsible for
527 * emitting their own output, including dealing with any race
528 * conditions due to writing in parallel to stdout and stderr.
482 - * The "ungroup" option can be enabled by setting the global
483 - * "run_processes_parallel_ungroup" to "1" before invoking
484 - * run_processes_parallel(), it will be set back to "0" as soon as the
485 - * API reads that setting.
529 */
487 -extern int run_processes_parallel_ungroup;
488 -void run_processes_parallel(size_t n,
489 - get_next_task_fn,
490 - start_failure_fn,
491 - task_finished_fn,
492 - void *pp_cb);
493 -void run_processes_parallel_tr2(size_t n, get_next_task_fn, start_failure_fn,
494 - task_finished_fn, void *pp_cb,
530 +void run_processes_parallel(const struct run_process_parallel_opts *opts);
531 +void run_processes_parallel_tr2(size_t processes, get_next_task_fn,
532 + start_failure_fn, task_finished_fn, void *pp_cb,
533 const char *tr2_category, const char *tr2_label);
534
535 /**
t/helper/test-run-command.c
+21 -10
@@ -136,7 +136,7 @@ static const char * const testsuite_usage[] = {
136 static int testsuite(int argc, const char **argv)
137 {
138 struct testsuite suite = TESTSUITE_INIT;
139 - int max_jobs = 1, i, ret;
139 + int max_jobs = 1, i, ret = 0;
140 DIR *dir;
141 struct dirent *d;
142 struct option options[] = {
@@ -152,6 +152,12 @@ static int testsuite(int argc, const char **argv)
152 "write JUnit-style XML files"),
153 OPT_END()
154 };
155 + struct run_process_parallel_opts opts = {
156 + .get_next_task = next_test,
157 + .start_failure = test_failed,
158 + .task_finished = test_finished,
159 + .data = &suite,
160 + };
161
162 argc = parse_options(argc, argv, NULL, options,
163 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
@@ -192,8 +198,8 @@ static int testsuite(int argc, const char **argv)
198 fprintf(stderr, "Running %"PRIuMAX" tests (%d at a time)\n",
199 (uintmax_t)suite.tests.nr, max_jobs);
200
195 - run_processes_parallel(max_jobs, next_test, test_failed,
196 - test_finished, &suite);
201 + opts.processes = max_jobs;
202 + run_processes_parallel(&opts);
203
204 if (suite.failed.nr > 0) {
205 ret = 1;
@@ -206,7 +212,7 @@ static int testsuite(int argc, const char **argv)
212 string_list_clear(&suite.tests, 0);
213 string_list_clear(&suite.failed, 0);
214
209 - return !!ret;
215 + return ret;
216 }
217
218 static uint64_t my_random_next = 1234;
@@ -382,6 +388,9 @@ int cmd__run_command(int argc, const char **argv)
388 struct child_process proc = CHILD_PROCESS_INIT;
389 int jobs;
390 int ret;
391 + struct run_process_parallel_opts opts = {
392 + .data = &proc,
393 + };
394
395 if (argc > 1 && !strcmp(argv[1], "testsuite"))
396 return testsuite(argc - 1, argv + 1);
@@ -427,7 +436,7 @@ int cmd__run_command(int argc, const char **argv)
436 if (!strcmp(argv[1], "--ungroup")) {
437 argv += 1;
438 argc -= 1;
430 - run_processes_parallel_ungroup = 1;
439 + opts.ungroup = 1;
440 }
441
442 jobs = atoi(argv[2]);
@@ -435,18 +444,20 @@ int cmd__run_command(int argc, const char **argv)
444 strvec_pushv(&proc.args, (const char **)argv + 3);
445
446 if (!strcmp(argv[1], "run-command-parallel")) {
438 - run_processes_parallel(jobs, parallel_next, NULL, NULL, &proc);
447 + opts.get_next_task = parallel_next;
448 } else if (!strcmp(argv[1], "run-command-abort")) {
440 - run_processes_parallel(jobs, parallel_next, NULL,
441 - task_finished, &proc);
449 + opts.get_next_task = parallel_next;
450 + opts.task_finished = task_finished;
451 } else if (!strcmp(argv[1], "run-command-no-jobs")) {
443 - run_processes_parallel(jobs, no_job, NULL, task_finished,
444 - &proc);
452 + opts.get_next_task = no_job;
453 + opts.task_finished = task_finished;
454 } else {
455 ret = 1;
456 fprintf(stderr, "check usage\n");
457 goto cleanup;
458 }
459 + opts.processes = jobs;
460 + run_processes_parallel(&opts);
461 ret = 0;
462 cleanup:
463 child_process_clear(&proc);