Skip to content

Commit 6e5ba0b

Browse files
avargitster
authored andcommitted
run-command API: have run_process_parallel() take an "opts" struct
As noted in fd3aaf5 (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 fd3aaf5 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/[email protected]/ Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c333e6f commit 6e5ba0b

File tree

4 files changed

+121
-59
lines changed

4 files changed

+121
-59
lines changed

hook.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,20 @@ int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options)
114114
.options = options,
115115
};
116116
const char *const hook_path = find_hook(hook_name);
117-
int jobs = 1;
118117
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+
};
119131

120132
if (!options)
121133
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)
137149
cb_data.hook_path = abs_path.buf;
138150
}
139151

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);
148153
ret = cb_data.rc;
149154
cleanup:
150155
strbuf_release(&abs_path);

run-command.c

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,6 @@ enum child_state {
14961496
GIT_CP_WAIT_CLEANUP,
14971497
};
14981498

1499-
int run_processes_parallel_ungroup;
15001499
struct parallel_processes {
15011500
void *const data;
15021501

@@ -1558,11 +1557,12 @@ static void handle_children_on_signal(int signo)
15581557
}
15591558

15601559
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)
15641561
{
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;
15661566

15671567
if (!n)
15681568
BUG("you must provide a non-zero number of processes!");
@@ -1769,27 +1769,27 @@ static int pp_collect_finished(struct parallel_processes *pp)
17691769
return result;
17701770
}
17711771

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)
17771773
{
17781774
int i, code;
17791775
int output_timeout = 100;
17801776
int spawn_cap = 4;
1781-
int ungroup = run_processes_parallel_ungroup;
17821777
struct parallel_processes pp = {
1783-
.max_processes = n,
1784-
.data = pp_cb,
1778+
.max_processes = opts->processes,
1779+
.data = opts->data,
17851780
.buffered_output = STRBUF_INIT,
1786-
.ungroup = ungroup,
1781+
.ungroup = opts->ungroup,
17871782
};
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;
17881787

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);
17911791

1792-
pp_init(&pp, get_next_task, start_failure, task_finished);
1792+
pp_init(&pp, opts);
17931793
while (1) {
17941794
for (i = 0;
17951795
i < spawn_cap && !pp.shutdown &&
@@ -1806,7 +1806,7 @@ void run_processes_parallel(size_t n,
18061806
}
18071807
if (!pp.nr_processes)
18081808
break;
1809-
if (ungroup) {
1809+
if (opts->ungroup) {
18101810
for (size_t i = 0; i < pp.max_processes; i++)
18111811
pp.children[i].state = GIT_CP_WAIT_CLEANUP;
18121812
} else {
@@ -1822,19 +1822,27 @@ void run_processes_parallel(size_t n,
18221822
}
18231823

18241824
pp_cleanup(&pp);
1825+
1826+
if (do_trace2)
1827+
trace2_region_leave(tr2_category, tr2_label, NULL);
18251828
}
18261829

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,
18281831
start_failure_fn start_failure,
18291832
task_finished_fn task_finished, void *pp_cb,
18301833
const char *tr2_category, const char *tr2_label)
18311834
{
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,
18331839

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+
};
18361844

1837-
trace2_region_leave(tr2_category, tr2_label, NULL);
1845+
run_processes_parallel(&opts);
18381846
}
18391847

18401848
int run_auto_maintenance(int quiet)

run-command.h

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -459,17 +459,64 @@ typedef int (*task_finished_fn)(int result,
459459
void *pp_task_cb);
460460

461461
/**
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
464514
* required to start another child process.
465515
*
466516
* The children started via this function run in parallel. Their output
467517
* (both stdout and stderr) is routed to stderr in a manner that output
468518
* from different tasks does not interleave (but see "ungroup" below).
469519
*
470-
* start_failure_fn and task_finished_fn can be NULL to omit any
471-
* special handling.
472-
*
473520
* If the "ungroup" option isn't specified, the API will set the
474521
* "stdout_to_stderr" parameter in "struct child_process" and provide
475522
* the callbacks with a "struct strbuf *out" parameter to write output
@@ -479,19 +526,10 @@ typedef int (*task_finished_fn)(int result,
479526
* NULL "struct strbuf *out" parameter, and are responsible for
480527
* emitting their own output, including dealing with any race
481528
* 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.
486529
*/
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,
495533
const char *tr2_category, const char *tr2_label);
496534

497535
/**

t/helper/test-run-command.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static const char * const testsuite_usage[] = {
136136
static int testsuite(int argc, const char **argv)
137137
{
138138
struct testsuite suite = TESTSUITE_INIT;
139-
int max_jobs = 1, i, ret;
139+
int max_jobs = 1, i, ret = 0;
140140
DIR *dir;
141141
struct dirent *d;
142142
struct option options[] = {
@@ -152,6 +152,12 @@ static int testsuite(int argc, const char **argv)
152152
"write JUnit-style XML files"),
153153
OPT_END()
154154
};
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+
};
155161

156162
argc = parse_options(argc, argv, NULL, options,
157163
testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
@@ -192,8 +198,8 @@ static int testsuite(int argc, const char **argv)
192198
fprintf(stderr, "Running %"PRIuMAX" tests (%d at a time)\n",
193199
(uintmax_t)suite.tests.nr, max_jobs);
194200

195-
run_processes_parallel(max_jobs, next_test, test_failed,
196-
test_finished, &suite);
201+
opts.processes = max_jobs;
202+
run_processes_parallel(&opts);
197203

198204
if (suite.failed.nr > 0) {
199205
ret = 1;
@@ -206,7 +212,7 @@ static int testsuite(int argc, const char **argv)
206212
string_list_clear(&suite.tests, 0);
207213
string_list_clear(&suite.failed, 0);
208214

209-
return !!ret;
215+
return ret;
210216
}
211217

212218
static uint64_t my_random_next = 1234;
@@ -382,6 +388,9 @@ int cmd__run_command(int argc, const char **argv)
382388
struct child_process proc = CHILD_PROCESS_INIT;
383389
int jobs;
384390
int ret;
391+
struct run_process_parallel_opts opts = {
392+
.data = &proc,
393+
};
385394

386395
if (argc > 1 && !strcmp(argv[1], "testsuite"))
387396
return testsuite(argc - 1, argv + 1);
@@ -427,26 +436,28 @@ int cmd__run_command(int argc, const char **argv)
427436
if (!strcmp(argv[1], "--ungroup")) {
428437
argv += 1;
429438
argc -= 1;
430-
run_processes_parallel_ungroup = 1;
439+
opts.ungroup = 1;
431440
}
432441

433442
jobs = atoi(argv[2]);
434443
strvec_clear(&proc.args);
435444
strvec_pushv(&proc.args, (const char **)argv + 3);
436445

437446
if (!strcmp(argv[1], "run-command-parallel")) {
438-
run_processes_parallel(jobs, parallel_next, NULL, NULL, &proc);
447+
opts.get_next_task = parallel_next;
439448
} 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;
442451
} 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;
445454
} else {
446455
ret = 1;
447456
fprintf(stderr, "check usage\n");
448457
goto cleanup;
449458
}
459+
opts.processes = jobs;
460+
run_processes_parallel(&opts);
450461
ret = 0;
451462
cleanup:
452463
child_process_clear(&proc);

0 commit comments

Comments
 (0)