git/t/helper
Ævar Arnfjörð Bjarmason fd3aaf53f7 run-command: add an "ungroup" option to run_process_parallel()
Extend the parallel execution API added in c553c72eed (run-command:
add an asynchronous parallel child processor, 2015-12-15) to support a
mode where the stdout and stderr of the processes isn't captured and
output in a deterministic order, instead we'll leave it to the kernel
and stdio to sort it out.

This gives the API same functionality as GNU parallel's --ungroup
option. As we'll see in a subsequent commit the main reason to want
this is to support stdout and stderr being connected to the TTY in the
case of jobs=1, demonstrated here with GNU parallel:

	$ parallel --ungroup 'test -t {} && echo TTY || echo NTTY' ::: 1 2
	TTY
	TTY
	$ parallel 'test -t {} && echo TTY || echo NTTY' ::: 1 2
	NTTY
	NTTY

Another is as GNU parallel's documentation notes a potential for
optimization. As demonstrated in next commit our results with "git
hook run" will be similar, but generally speaking this shows that if
you want to run processes in parallel where the exact order isn't
important this can be a lot faster:

	$ hyperfine -r 3 -L o ,--ungroup 'parallel {o} seq ::: 10000000 >/dev/null '
	Benchmark 1: parallel  seq ::: 10000000 >/dev/null
	  Time (mean ± σ):     220.2 ms ±   9.3 ms    [User: 124.9 ms, System: 96.1 ms]
	  Range (min … max):   212.3 ms … 230.5 ms    3 runs

	Benchmark 2: parallel --ungroup seq ::: 10000000 >/dev/null
	  Time (mean ± σ):     154.7 ms ±   0.9 ms    [User: 136.2 ms, System: 25.1 ms]
	  Range (min … max):   153.9 ms … 155.7 ms    3 runs

	Summary
	  'parallel --ungroup seq ::: 10000000 >/dev/null ' ran
	    1.42 ± 0.06 times faster than 'parallel  seq ::: 10000000 >/dev/null '

A large part of the juggling in the API is to make the API safer for
its maintenance and consumers alike.

For the maintenance of the API we e.g. avoid malloc()-ing the
"pp->pfd", ensuring that SANITIZE=address and other similar tools will
catch any unexpected misuse.

For API consumers we take pains to never pass the non-NULL "out"
buffer to an API user that provided the "ungroup" option. The
resulting code in t/helper/test-run-command.c isn't typical of such a
user, i.e. they'd typically use one mode or the other, and would know
whether they'd provided "ungroup" or not.

We could also avoid the strbuf_init() for "buffered_output" by having
"struct parallel_processes" use a static PARALLEL_PROCESSES_INIT
initializer, but let's leave that cleanup for later.

Using a global "run_processes_parallel_ungroup" variable to enable
this option is rather nasty, but is being done here to produce as
minimal of a change as possible for a subsequent regression fix. This
change is extracted from a larger initial version[1] which ends up
with a better end-state for the API, but in doing so needed to modify
all existing callers of the API. Let's defer that for now, and
narrowly focus on what we need for fixing the regression in the
subsequent commit.

It's safe to do this with a global variable because:

 A) hook.c is the only user of it that sets it to non-zero, and before
    we'll get any other API users we'll refactor away this method of
    passing in the option, i.e. re-roll [1].

 B) Even if hook.c wasn't the only user we don't have callers of this
    API that concurrently invoke this parallel process starting API
    itself in parallel.

As noted above "A" && "B" are rather nasty, and we don't want to live
with those caveats long-term, but for now they should be an acceptable
compromise.

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>
2022-06-07 10:01:41 -07:00
..
.gitignore drop vcs-svn experiment 2020-08-13 11:02:15 -07:00
test-advise.c advice.h: add missing __attribute__((format)) & fix usage 2021-07-13 15:20:20 -07:00
test-bitmap.c t/helper/test-bitmap.c: add 'dump-hashes' mode 2021-09-14 16:34:17 -07:00
test-bloom.c Merge branch 'cc/test-helper-bloom-usage-fix' 2021-04-13 15:28:52 -07:00
test-chmtime.c t/helper/test-chmtime: skip directories on Windows 2022-03-25 16:04:17 -07:00
test-config.c config: fix leaks from git_config_get_string_const() 2020-08-14 10:52:04 -07:00
test-crontab.c maintenance: add start/stop subcommands 2020-09-25 10:59:44 -07:00
test-csprng.c wrapper: add a helper to generate numbers from a CSPRNG 2022-01-17 14:17:48 -08:00
test-ctype.c
test-date.c date API: add and use a date_mode_release() 2022-02-16 09:40:00 -08:00
test-delta.c test-delta: read input into a heap buffer 2018-08-30 10:30:21 -07:00
test-dir-iterator.c Merge branch 'mt/dir-iterator-updates' 2019-08-09 10:13:14 -07:00
test-drop-caches.c lazyload: use correct calling conventions 2022-01-09 10:34:53 -08:00
test-dump-cache-tree.c convert "oidcmp() != 0" to "!oideq()" 2018-08-29 11:32:49 -07:00
test-dump-fsmonitor.c fsmonitor: change last update timestamp on the index_state to opaque token 2020-01-13 14:58:43 -08:00
test-dump-split-index.c t/helper/test-dump-split-index: initialize git repository 2020-02-24 09:33:24 -08:00
test-dump-untracked-cache.c environment.c: remove test-specific "ignore_untracked..." variable 2021-09-22 13:15:00 -07:00
test-example-decorate.c lookup_unknown_object(): take a repository argument 2021-04-13 13:18:46 -07:00
test-fake-ssh.c
test-fast-rebase.c fast-rebase: write conflict state to working tree, index, and HEAD 2021-05-20 15:40:39 +09:00
test-fsmonitor-client.c t/helper/fsmonitor-client: create IPC client to talk to FSMonitor Daemon 2022-03-25 16:04:16 -07:00
test-genrandom.c
test-genzeros.c test-tool genzeros: generate large amounts of data more efficiently 2021-11-03 11:22:26 -07:00
test-getcwd.c t0001: fix broken not-quite getcwd(3) test in bed67874e2 2021-07-30 10:18:27 -07:00
test-hash-speed.c builtins + test helpers: use return instead of exit() in cmd_* 2021-06-09 09:15:58 +09:00
test-hash.c builtins + test helpers: use return instead of exit() in cmd_* 2021-06-09 09:15:58 +09:00
test-hashmap.c Use new HASHMAP_INIT macro to simplify hashmap initialization 2020-11-11 12:55:27 -08:00
test-index-version.c
test-json-writer.c json_writer: new routines to create JSON data 2018-07-16 13:55:39 -07:00
test-lazy-init-name-hash.c OFFSETOF_VAR macro to simplify hashmap iterators 2019-10-07 10:20:11 +09:00
test-match-trees.c builtins + test helpers: use return instead of exit() in cmd_* 2021-06-09 09:15:58 +09:00
test-mergesort.c test-mergesort: use repeatable random numbers 2021-10-08 10:04:56 -07:00
test-mktemp.c
test-oid-array.c tests: fix test-oid-array leak, test in SANITIZE=leak 2021-10-07 15:40:15 -07:00
test-oidmap.c test-oidmap: remove 'add' subcommand 2019-07-01 10:26:28 -07:00
test-oidtree.c tests: fix a memory leak in test-oidtree.c 2021-10-07 15:40:15 -07:00
test-online-cpus.c
test-parse-options.c tests: fix a memory leak in test-parse-options.c 2021-10-07 15:40:15 -07:00
test-parse-pathspec-file.c test-parse-pathspec-file.c: s/0/NULL/ for pointer type 2020-04-27 11:21:12 -07:00
test-partial-clone.c promisor-remote: teach lazy-fetch in any repo 2021-06-28 09:58:01 -07:00
test-path-utils.c t0060: test ntfs/hfs-obscured dotfiles 2021-05-04 11:52:02 +09:00
test-pcre2-config.c grep/pcre2: better support invalid UTF-8 haystacks 2021-01-24 16:09:17 -08:00
test-pkt-line.c test-lib-functions: use test-tool for [de]packetize() 2021-07-19 11:53:50 -07:00
test-prio-queue.c tests: fix a memory leak in test-prio-queue.c 2021-10-07 15:40:15 -07:00
test-proc-receive.c receive-pack: use default version 0 for proc-receive 2020-11-11 12:46:56 -08:00
test-progress.c progress.c tests: make start/stop commands on stdin 2022-02-03 15:39:55 -08:00
test-reach.c builtins + test helpers: use return instead of exit() in cmd_* 2021-06-09 09:15:58 +09:00
test-read-cache.c test-read-cache: remove --table, --expand options 2021-12-22 11:42:40 -08:00
test-read-graph.c test-read-graph: include extra post-parse info 2022-03-01 12:09:55 -08:00
test-read-midx.c t/helper/test-read-midx.c: free MIDX within read_midx_file() 2021-10-27 16:26:37 -07:00
test-ref-store.c refs API: remove "failure_errno" from refs_resolve_ref_unsafe() 2022-01-26 15:58:41 -08:00
test-reftable.c reftable: order unittests by complexity 2022-01-20 11:31:53 -08:00
test-regex.c t/helper: teach test-regex to report pattern errors (like REG_ILLSEQ) 2020-05-18 13:03:35 -07:00
test-repository.c t/helper: make repository tests hash independent 2020-02-24 09:33:27 -08:00
test-revision-walking.c revision.c: remove implicit dependency on the_index 2018-09-21 09:51:19 -07:00
test-run-command.c run-command: add an "ungroup" option to run_process_parallel() 2022-06-07 10:01:41 -07:00
test-scrap-cache-tree.c Merge branch 'ma/lockfile-cleanup' 2018-05-30 14:04:05 +09:00
test-serve-v2.c serve.[ch]: remove "serve_options", split up --advertise-refs code 2021-08-05 08:59:37 -07:00
test-sha1.c t: make the sha1 test-tool helper generic 2018-11-14 16:54:52 +09:00
test-sha1.sh
test-sha256.c Add a base implementation of SHA-256 support 2018-11-14 16:54:53 +09:00
test-sigchain.c style: the opening '{' of a function is in a separate line 2018-12-10 15:41:09 +09:00
test-simple-ipc.c t/helper/simple-ipc: convert test-simple-ipc to use start_bg_command 2021-09-20 08:57:58 -07:00
test-strcmp-offset.c
test-string-list.c
test-submodule-config.c
test-submodule-nested-repo-config.c repository: support unabsorbed in repo_submodule_init 2021-09-09 14:09:30 -07:00
test-subprocess.c run-command API users: use strvec_pushv(), not argv assignment 2021-11-25 22:15:07 -08:00
test-tool.c t/helper/fsmonitor-client: create IPC client to talk to FSMonitor Daemon 2022-03-25 16:04:16 -07:00
test-tool.h t/helper/fsmonitor-client: create IPC client to talk to FSMonitor Daemon 2022-03-25 16:04:16 -07:00
test-trace2.c common-main.c: call exit(), don't return 2021-12-07 12:29:57 -08:00
test-urlmatch-normalization.c
test-userdiff.c userdiff tests: list builtin drivers via test-tool 2021-04-08 12:19:10 -07:00
test-wildmatch.c
test-windows-named-pipe.c use strpbrk(3) to search for characters from a given set 2020-02-24 09:30:31 -08:00
test-write-cache.c Merge branch 'ma/lockfile-cleanup' 2018-05-30 14:04:05 +09:00
test-xml-encode.c tests: optionally write results as JUnit-style .xml 2019-01-29 09:26:46 -08:00