Skip to contentSkip to conversation

submodule: parallelize diff

PATCH Superseded Calvin Wan 3 participants 11 messages Oct 11, 2022 – Oct 14, 2022 1 message not in the archive Documentation

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Calvin Wan

author
#1 lore raw

v2 cover submodule: parallelize diff

In reply to a message not in the archive

Changes since v1

This series is now rebased on top of Avar's run-command refactor series[1], which allows new functions to be easily added to run_parallel_processes without having to change other callers.

The config option has been renamed to submodule.diffJobs. This name accurately reflects what functionality is affected. While other APIs parse for config options from the initial command, this config option is parsed for in the diff-lib.c library. This is because there are so many entry points into run_diff_files resulting in signicant code changes required to pass in this config option.

I also wanted to pose another question to list regarding defaults for parallel processes. For jobs that clearly scale with the number of processes (aka jobs that are mostly processor bound), it is obvious that setting the default number of processes to the number of available cores is the most optimal option. However, this changes when the job is mostly I/O bound or has a combination of I/O and processing. Looking at my use case for status on a cold cache (see below), we notice that increasing the number of parallel processes speeds up status, but after a certain number, it actually starts slowing down. This is because status spends most of its time reading from the index. While SSDs are able to do a certain amount of reads in parallel, this limit can be significantly smaller than the number of cores and going past that limit causes the read head to constantly swap, slowing down status. With an HDD, parallel reads aren't even possible and while I haven't tested my patch on an HDD, I have a suspicion that adding multiple processes would probably make status slower than the baseline. How should the default be set then? In my case, the safe option would be to default it to 1, but then many users wouldn't be able to discover this optimization. There are also multiple places in the git documentation where we say that setting a config option for a parallel process to 0 will result in a "reasonable amount" of processes, which generally entails the number of available cores. Can this be problematic for other parallel processes that spend a significant time in I/O? Should my option even have the option to set it to 0 given the pitalls?

Original cover letter:

When running 'git status' in a superproject, git spawns a subprocess in series to run status for every submodule. For projects with many large submodules, parallelizing status subprocesses can significantly speed up the runtime for both cold and warm caches. While my initial intention was to speedup status, it turns out that much of the runtime spent on status is in diff-lib.c, resulting in speedups for many different other commands that utilize this library.

Here are some timing tests from running status on the Android Open Source Project (AOSP). My machine has an SSD and 48 cores.
Warm Cache:

    git 2.37

Time (mean ± σ): 17.685 s ± 2.040 s [User: 5.041 s, System: 22.799 s]
Range (min … max): 16.168 s … 22.804 s 10 runs

this patch (status.parallelSubmodules=1)
Time (mean ± σ): 13.102 s ± 0.500 s [User: 4.894 s, System: 19.533 s]
Range (min … max): 12.841 s … 14.447 s 10 runs

this patch (status.parallelSubmodules=5)
Time (mean ± σ): 3.994 s ± 0.152 s [User: 4.998 s, System: 20.805 s]
Range (min … max): 3.744 s … 4.163 s 10 runs

this patch (status.parallelSubmodules=10)
Time (mean ± σ): 3.445 s ± 0.085 s [User: 5.151 s, System: 20.208 s]
Range (min … max): 3.319 s … 3.586 s 10 runs

this patch (status.parallelSubmodules=20)
Time (mean ± σ): 3.626 s ± 0.109 s [User: 5.087 s, System: 20.366 s]
Range (min … max): 3.438 s … 3.763 s 10 runs

We can see that there are diminishing returns and even slightly worse performance after a certain number of max processes, but optimally there is a speed up factor of around 5.

Cold Cache:
git 2.37
mean of 3 runs: 6m32s
this patch (status.parallelSubmodules=1)
mean of 3 runs: 5m34s
this patch (status.parallelSubmodules=5)
mean of 3 runs: 2m23s
this patch (status.parallelSubmodules=10)
mean of 3 runs: 2m45s
this patch (status.parallelSubmodules=20)
mean of 3 runs: 3m23s

We can witness the same phenomenon as above and optimally there is a speed up factor of around 2.7.

Patch 1 adds output piping to run_processes_parallel_opt so the output from each submodule can be parsed. Patches 2 and 3 move preexisting functionality into separate functions and refactor code to prepare for patch 4 to implement parallelization.

Future work: The reason why status is much slower on a cold cache vs warm cache is mainly due to refreshing the index. It is worth investigating whether this is entirely necessary.

[1] https://lore.kernel.org/git/[email protected]/

Calvin Wan (4):
run-command: add pipe_output_fn to run_processes_parallel_opts
submodule: move status parsing into function
diff-lib: refactor match_stat_with_submodule
diff-lib: parallelize run_diff_files for submodules

 Documentation/config/submodule.txt |  12 ++
 diff-lib.c                         | 108 ++++++++++++--
 run-command.c                      |  12 +-
 run-command.h                      |  22 +++
 submodule.c                        | 230 +++++++++++++++++++++++++----
 submodule.h                        |   9 ++
 t/helper/test-run-command.c        |  18 +++
 t/t0061-run-command.sh             |  65 ++++----
 t/t4027-diff-submodule.sh          |  19 +++
 t/t7506-status-submodule.sh        |  19 +++
 10 files changed, 441 insertions(+), 73 deletions(-)
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
author
#4 lore raw

PATCH v2 1/4 run-command: add pipe_output_fn to run_processes_parallel_opts

In reply to a message not in the archive

Add pipe_output_fn as an optionally set function in run_process_parallel_opts. If set, output from each child process is piped to the callback function to allow for separate parsing.

---
 run-command.c               | 12 +++++--
 run-command.h               | 22 +++++++++++++
 t/helper/test-run-command.c | 18 ++++++++++
 t/t0061-run-command.sh      | 65 +++++++++++++++++++++++--------------
 4 files changed, 90 insertions(+), 27 deletions(-)
run-command.c +10 -2
index da02631933..c6090e4cb8 100644
@@ -1689,12 +1689,16 @@ static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
 	}
 }
 
-static void pp_output(struct parallel_processes *pp)
+static void pp_output(struct parallel_processes *pp,
+			const struct run_process_parallel_opts *opts)
 {
 	int i = pp->output_owner;
 
 	if (pp->children[i].state == GIT_CP_WORKING &&
 	    pp->children[i].err.len) {
+		if (opts->pipe_output)
+			opts->pipe_output(&pp->children[i].err, pp->data,
+						pp->children[i].data);
 		strbuf_write(&pp->children[i].err, stderr);
 		strbuf_reset(&pp->children[i].err);
 	}
@@ -1716,6 +1720,10 @@ static int pp_collect_finished(struct parallel_processes *pp,
 
 		code = finish_command(&pp->children[i].process);
 
+		if (opts->pipe_output)
+			opts->pipe_output(&pp->children[i].err, pp->data,
+						pp->children[i].data);
+
 		if (opts->task_finished)
 			code = opts->task_finished(code, opts->ungroup ? NULL :
 						   &pp->children[i].err, pp->data,
@@ -1803,7 +1811,7 @@ void run_processes_parallel(const struct run_process_parallel_opts *opts)
 				pp.children[i].state = GIT_CP_WAIT_CLEANUP;
 		} else {
 			pp_buffer_stderr(&pp, output_timeout);
-			pp_output(&pp);
+			pp_output(&pp, opts);
 		}
 		code = pp_collect_finished(&pp, opts);
 		if (code) {
run-command.h +22 -0
index 075bd9b9de..cb51c56ea6 100644
@@ -440,6 +440,22 @@ typedef int (*start_failure_fn)(struct strbuf *out,
 				void *pp_cb,
 				void *pp_task_cb);
 
+/**
+ * This callback is periodically called while child processes are
+ * running and also when a child process finishes.
+ *
+ * "struct strbuf *out" contains the output collected from pp_task_cb
+ * since the last call of this function.
+ *
+ * pp_cb is the callback cookie as passed into run_processes_parallel,
+ * pp_task_cb is the callback cookie as passed into get_next_task_fn.
+ *
+ * This function is incompatible with "ungroup"
+ */
+typedef void (*pipe_output_fn)(struct strbuf *out,
+				void *pp_cb,
+				void *pp_task_cb);
+
 /**
  * This callback is called on every child process that finished processing.
  *
@@ -493,6 +509,12 @@ struct run_process_parallel_opts
 	 */
 	start_failure_fn start_failure;
 
+	/**
+	 * pipe_output: See pipe_output_fn() above. This should be
+	 * NULL unless process specific output is needed
+	 */
+	pipe_output_fn pipe_output;
+
 	/**
 	 * task_finished: See task_finished_fn() above. This can be
 	 * NULL to omit any special handling.
t/helper/test-run-command.c +18 -0
index 46bac2bb70..d3c3df7960 100644
@@ -52,6 +52,18 @@ static int no_job(struct child_process *cp,
 	return 0;
 }
 
+static void pipe_output(struct strbuf *out,
+				void *pp_cb,
+				void *pp_task_cb)
+{
+	fprintf(stderr, "%s", out->buf);
+	/*
+	 * Resetting output to show that piped output would print the
+	 * same as other tests without the pipe_output() function set
+	 */
+	strbuf_reset(out);
+}
+
 static int task_finished(int result,
 			 struct strbuf *err,
 			 void *pp_cb,
@@ -439,6 +451,12 @@ int cmd__run_command(int argc, const char **argv)
 		opts.ungroup = 1;
 	}
 
+	if (!strcmp(argv[1], "--pipe-output")) {
+		argv += 1;
+		argc -= 1;
+		opts.pipe_output = pipe_output;
+	}
+
 	jobs = atoi(argv[2]);
 	strvec_clear(&proc.args);
 	strvec_pushv(&proc.args, (const char **)argv + 3);
t/t0061-run-command.sh +40 -25

95 lines not on this page · raw message

Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
author
#6 lore raw

PATCH v2 2/4 submodule: move status parsing into function

In reply to a message not in the archive

A future patch requires the ability to parse the output of git status --porcelain=2. Move parsing code from is_submodule_modified to parse_status_porcelain.

---
 submodule.c | 71 +++++++++++++++++++++++++++++------------------------
 1 file changed, 39 insertions(+), 32 deletions(-)
submodule.c +39 -32
index 72b295b87b..a3410ed8f0 100644
@@ -1864,6 +1864,43 @@ int fetch_submodules(struct repository *r,
 	return spf.result;
 }
 
+static int parse_status_porcelain(char *buf, unsigned *dirty_submodule, int ignore_untracked)
+{
+	/* regular untracked files */
+	if (buf[0] == '?')
+		*dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
+
+	if (buf[0] == 'u' ||
+		buf[0] == '1' ||
+		buf[0] == '2') {
+		/* T = line type, XY = status, SSSS = submodule state */
+		if (strlen(buf) < strlen("T XY SSSS"))
+			BUG("invalid status --porcelain=2 line %s",
+				buf);
+
+		if (buf[5] == 'S' && buf[8] == 'U')
+			/* nested untracked file */
+			*dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
+
+		if (buf[0] == 'u' ||
+			buf[0] == '2' ||
+			memcmp(buf + 5, "S..U", 4))
+			/* other change */
+			*dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
+	}
+
+	if ((*dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
+		((*dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
+		ignore_untracked)) {
+		/*
+		* We're not interested in any further information from
+		* the child any more, neither output nor its exit code.
+		*/
+		return 1;
+	}
+	return 0;
+}
+
 unsigned is_submodule_modified(const char *path, int ignore_untracked)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
@@ -1900,39 +1937,9 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)
 
 	fp = xfdopen(cp.out, "r");
 	while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
-		/* regular untracked files */
-		if (buf.buf[0] == '?')
-			dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
-
-		if (buf.buf[0] == 'u' ||
-		    buf.buf[0] == '1' ||
-		    buf.buf[0] == '2') {
-			/* T = line type, XY = status, SSSS = submodule state */
-			if (buf.len < strlen("T XY SSSS"))
-				BUG("invalid status --porcelain=2 line %s",
-				    buf.buf);
-
-			if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
-				/* nested untracked file */
-				dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
-
-			if (buf.buf[0] == 'u' ||
-			    buf.buf[0] == '2' ||
-			    memcmp(buf.buf + 5, "S..U", 4))
-				/* other change */
-				dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
-		}
-
-		if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
-		    ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
-		     ignore_untracked)) {
-			/*
-			 * We're not interested in any further information from
-			 * the child any more, neither output nor its exit code.
-			 */
-			ignore_cp_exit_code = 1;
+		ignore_cp_exit_code = parse_status_porcelain(buf.buf, &dirty_submodule, ignore_untracked);
+		if (ignore_cp_exit_code)
 			break;
-		}
 	}
 	fclose(fp);
 
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
author
#9 lore raw

PATCH v2 3/4 diff-lib: refactor match_stat_with_submodule

In reply to a message not in the archive

Flatten out the if statements in match_stat_with_submodule so the logic is more readable and easier for future patches to add to. orig_flags didn't need to be set if the cache entry wasn't a GITLINK so defer setting it.

---
 diff-lib.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
diff-lib.c +17 -10
index 2edea41a23..e249322141 100644
@@ -73,18 +73,25 @@ static int match_stat_with_submodule(struct diff_options *diffopt,
 				     unsigned *dirty_submodule)
 {
 	int changed = ie_match_stat(diffopt->repo->index, ce, st, ce_option);
-	if (S_ISGITLINK(ce->ce_mode)) {
-		struct diff_flags orig_flags = diffopt->flags;
-		if (!diffopt->flags.override_submodule_config)
-			set_diffopt_flags_from_submodule_config(diffopt, ce->name);
-		if (diffopt->flags.ignore_submodules)
-			changed = 0;
-		else if (!diffopt->flags.ignore_dirty_submodules &&
+	struct diff_flags orig_flags;
+
+	if (!S_ISGITLINK(ce->ce_mode))
+		goto ret;
+
+	orig_flags = diffopt->flags;
+	if (!diffopt->flags.override_submodule_config)
+		set_diffopt_flags_from_submodule_config(diffopt, ce->name);
+	if (diffopt->flags.ignore_submodules) {
+		changed = 0;
+		goto cleanup;
+	}
+	if (!diffopt->flags.ignore_dirty_submodules &&
 			 (!changed || diffopt->flags.dirty_submodules))
-			*dirty_submodule = is_submodule_modified(ce->name,
-								 diffopt->flags.ignore_untracked_in_submodules);
+		*dirty_submodule = is_submodule_modified(ce->name,
+					diffopt->flags.ignore_untracked_in_submodules);
+cleanup:
 		diffopt->flags = orig_flags;
-	}
+ret:
 	return changed;
 }
 
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
author
#10 lore raw

PATCH v2 4/4 diff-lib: parallelize run_diff_files for submodules

In reply to a message not in the archive

During the iteration of the index entries in run_diff_files, whenever a submodule is found and needs its status checked, a subprocess is spawned for it. Instead of spawning the subprocess immediately and waiting for its completion to continue, hold onto all submodules and relevant information in a list. Then use that list to create tasks for run_processes_parallel. Subprocess output is piped to status_pipe_output which parses it.

Add config option submodule.diffJobs to set the maximum number of parallel jobs. The option defaults to 1 if unset. If set to 0, the number of jobs is set to online_cpus(). I added a TODO here, regarding defaults -- please see the cover letter for the discussion.

Since run_diff_files is called from many different commands, I chose to grab the config option in the function rather than adding variables to every git command and then figuring out how to pass them all in.

---
 Documentation/config/submodule.txt |  12 +++
 diff-lib.c                         |  89 ++++++++++++++--
 submodule.c                        | 159 +++++++++++++++++++++++++++++
 submodule.h                        |   9 ++
 t/t4027-diff-submodule.sh          |  19 ++++
 t/t7506-status-submodule.sh        |  19 ++++
 6 files changed, 299 insertions(+), 8 deletions(-)
Documentation/config/submodule.txt +12 -0
index 6490527b45..3209eb8117 100644
@@ -93,6 +93,18 @@ submodule.fetchJobs::
 	in parallel. A value of 0 will give some reasonable default.
 	If unset, it defaults to 1.
 
+submodule.diffJobs::
+	Specifies how many submodules are diffed at the same time. A
+	positive integer allows up to that number of submodules diffed
+	in parallel. A value of 0 will give some reasonable default.
+	If unset, it defaults to 1. The diff operation is used by many
+	other git commands such as add, merge, diff, status, stash and
+	more. Note that the expensive part of the diff operation is
+	reading the index from cache or memory. Therefore multiple jobs
+	may be detrimental to performance if your hardware does not
+	support parallel reads or if the number of jobs greatly exceeds
+	the amount of supported reads.
+
 submodule.alternateLocation::
 	Specifies how the submodules obtain alternates when submodules are
 	cloned. Possible values are `no`, `superproject`.
diff-lib.c +81 -8

147 lines not on this page · raw message

submodule.c +159 -0

177 lines not on this page · raw message

submodule.h +9 -0
index 6a9fec6de1..c829d37b9f 100644
@@ -41,6 +41,12 @@ struct submodule_update_strategy {
 	.type = SM_UPDATE_UNSPECIFIED, \
 }
 
+struct submodule_status_util {
+	int changed, ignore_untracked;
+	unsigned dirty_submodule, newmode;
+	struct cache_entry *ce;
+};
+
 int is_gitmodules_unmerged(struct index_state *istate);
 int is_writing_gitmodules_ok(void);
 int is_staging_gitmodules_ok(struct index_state *istate);
@@ -94,6 +100,9 @@ int fetch_submodules(struct repository *r,
 		     int command_line_option,
 		     int default_option,
 		     int quiet, int max_parallel_jobs);
+int get_submodules_status(struct repository *r,
+			 struct string_list *submodules,
+			 int max_parallel_jobs);
 unsigned is_submodule_modified(const char *path, int ignore_untracked);
 int submodule_uses_gitfile(const char *path);
 
t/t4027-diff-submodule.sh +19 -0
index 40164ae07d..e08ee315a7 100755
@@ -34,6 +34,25 @@ test_expect_success setup '
 	subtip=$3 subprev=$2
 '
 
+test_expect_success 'diff in superproject with submodules respects parallel settings' '
+	test_when_finished "rm -f trace.out" &&
+	(
+		GIT_TRACE=$(pwd)/trace.out git diff &&
+		grep "1 tasks" trace.out &&
+		>trace.out &&
+
+		git config submodule.diffJobs 8 &&
+		GIT_TRACE=$(pwd)/trace.out git diff &&
+		grep "8 tasks" trace.out &&
+		>trace.out &&
+
+		GIT_TRACE=$(pwd)/trace.out git -c submodule.diffJobs=0 diff &&
+		grep "preparing to run up to [0-9]* tasks" trace.out &&
+		! grep "up to 0 tasks" trace.out &&
+		>trace.out
+	)
+'
+
 test_expect_success 'git diff --raw HEAD' '
 	hexsz=$(test_oid hexsz) &&
 	git diff --raw --abbrev=$hexsz HEAD >actual &&
t/t7506-status-submodule.sh +19 -0
index f5426a8e58..dfdfb58bfc 100755
@@ -410,4 +410,23 @@ test_expect_success 'status with added file in nested submodule (short)' '
 	EOF
 '
 
+test_expect_success 'status in superproject with submodules respects parallel settings' '
+	test_when_finished "rm -f trace.out" &&
+	(
+		GIT_TRACE=$(pwd)/trace.out git status &&
+		grep "1 tasks" trace.out &&
+		>trace.out &&
+
+		git config submodule.diffJobs 8 &&
+		GIT_TRACE=$(pwd)/trace.out git status &&
+		grep "8 tasks" trace.out &&
+		>trace.out &&
+
+		GIT_TRACE=$(pwd)/trace.out git -c submodule.diffJobs=0 status &&
+		grep "preparing to run up to [0-9]* tasks" trace.out &&
+		! grep "up to 0 tasks" trace.out &&
+		>trace.out
+	)
+'
+
 test_done
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
#2 lore raw
In reply to Calvin Wan
Quoted 9 lines · Calvin Wan

I also wanted to pose another question to list regarding defaults for parallel processes. For jobs that clearly scale with the number of processes (aka jobs that are mostly processor bound), it is obvious that setting the default number of processes to the number of available cores is the most optimal option. However, this changes when the job is mostly I/O bound or has a combination of I/O and processing. Looking at my use case for status on a cold cache (see below), we notice that increasing the number of parallel processes speeds up status, but after a certain number, it actually starts slowing down.

I do not offhand recall how the default parallelism is computed there, but if I am correct to suspect that "git grep" has a similar scaling pattern, i.e. the threads all need to compete for I/O to read from the filesystem to find needles from the haystack, perhaps it would give us a precedent to model the behaviour of this part of the code, too, hopefully?

#7 lore raw

Re: [PATCH v2 2/4] submodule: move status parsing into function

In reply to Calvin Wan
Quoted 88 lines · Calvin Wan

A future patch requires the ability to parse the output of git status --porcelain=2. Move parsing code from is_submodule_modified to parse_status_porcelain.

---
 submodule.c | 71 +++++++++++++++++++++++++++++------------------------
 1 file changed, 39 insertions(+), 32 deletions(-)
submodule.c +37 -27
index 72b295b87b..a3410ed8f0 100644
@@ -1864,6 +1864,43 @@ int fetch_submodules(struct repository *r,
 	return spf.result;
 }
 
+static int parse_status_porcelain(char *buf, unsigned *dirty_submodule, int ignore_untracked)
+{
+	/* regular untracked files */
+	if (buf[0] == '?')
+		*dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
+
+	if (buf[0] == 'u' ||
+		buf[0] == '1' ||
+		buf[0] == '2') {
+		/* T = line type, XY = status, SSSS = submodule state */
+		if (strlen(buf) < strlen("T XY SSSS"))
+			BUG("invalid status --porcelain=2 line %s",
+				buf);
+
+		if (buf[5] == 'S' && buf[8] == 'U')
+			/* nested untracked file */
+			*dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
+
+		if (buf[0] == 'u' ||
+			buf[0] == '2' ||
+			memcmp(buf + 5, "S..U", 4))
+			/* other change */
+			*dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
+	}
+
+	if ((*dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
+		((*dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
+		ignore_untracked)) {
+		/*
+		* We're not interested in any further information from
+		* the child any more, neither output nor its exit code.
+		*/
+		return 1;
+	}
+	return 0;
+}
+
 unsigned is_submodule_modified(const char *path, int ignore_untracked)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
@@ -1900,39 +1937,9 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)
 
 	fp = xfdopen(cp.out, "r");
 	while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
-		/* regular untracked files */
-		if (buf.buf[0] == '?')
-			dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
-
-		if (buf.buf[0] == 'u' ||
-		    buf.buf[0] == '1' ||
-		    buf.buf[0] == '2') {
-			/* T = line type, XY = status, SSSS = submodule state */
-			if (buf.len < strlen("T XY SSSS"))
-				BUG("invalid status --porcelain=2 line %s",
-				    buf.buf);
-
-			if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
-				/* nested untracked file */
-				dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
-
-			if (buf.buf[0] == 'u' ||
-			    buf.buf[0] == '2' ||
-			    memcmp(buf.buf + 5, "S..U", 4))
-				/* other change */
-				dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
-		}
-
-		if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
-		    ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
-		     ignore_untracked)) {
-			/*

submodule.c unsigned is_submodule_modified(const char *path, int ignore_untracked) · line 1944

Quoted 10 lines
submodule.c +2 -5
-			 * We're not interested in any further information from
-			 * the child any more, neither output nor its exit code.
-			 */
-			ignore_cp_exit_code = 1;
+		ignore_cp_exit_code = parse_status_porcelain(buf.buf, &dirty_submodule, ignore_untracked);
+		if (ignore_cp_exit_code)
 			break;
-		}
 	}
 	fclose(fp);

This isn't just a code move, but a rewrite of much of the code to accept a "char *buf" rather than a "struct strbuf buf".

I can see in a later commit that you'd like to change this to accept a .string from a string_list.

Without changing anything else, if you lead with a commit that does (in the initial loop):

	char *str = buf.buf;
        const size_t len = buf.len;

And then make it use "buf" and "len" instead you could follow with the move commit, which at that ponit would benefit from the rename detection more more than it does now.

Also: If we have a strbuf in this caller let's pass a "len", and not here make it need to do a strlen() on every line when we've computed it already, the other caller could just pass another strlen([...].string).

#5 lore raw

Re: [PATCH v2 1/4] run-command: add pipe_output_fn to run_processes_parallel_opts

In reply to Calvin Wan

On Tue, Oct 11 2022, Calvin Wan wrote:

(re-arranging the diff a bit for discussion)

Quoted 10 lines
t/helper/test-run-command.c +2 -0
index 46bac2bb70..d3c3df7960 100644
@@ -52,6 +52,18 @@ static int no_job(struct child_process *cp,
 	return 0;
 }
 
+static void pipe_output(struct strbuf *out,
+				void *pp_cb,

t/helper/test-run-command.c static int no_job(struct child_process *cp, · line 66

Quoted 10 lines
t/helper/test-run-command.c +10 -0
+				void *pp_task_cb)
+{
+	fprintf(stderr, "%s", out->buf);
+	/*
+	 * Resetting output to show that piped output would print the
+	 * same as other tests without the pipe_output() function set
+	 */
+	strbuf_reset(out);
+}
+

This is still unaddressed from the last round:
https://lore.kernel.org/git/[email protected]/

Or well, it changed a bit, but the "reset" is still there.

Whatever you're aiming for here, this is apparently doing nothing, because if I comment out the strbuf_reset() then all your tests pass, which...

t/helper/test-run-command.c

Quoted 10 lines
t/helper/test-run-command.c +0 -0
 	int i = pp->output_owner;
 
 	if (pp->children[i].state == GIT_CP_WORKING &&
t/helper/test-run-command.c +3 -0
 	    pp->children[i].err.len) {
+		if (opts->pipe_output)
+			opts->pipe_output(&pp->children[i].err, pp->data,
+						pp->children[i].data);
 		strbuf_write(&pp->children[i].err, stderr);
 		strbuf_reset(&pp->children[i].err);
 	}

...we can see from here...

t/helper/test-run-command.c static int pp_collect_finished(struct parallel_processes *pp, · line 1729

Quoted 11 lines
t/helper/test-run-command.c +4 -0
@@ -1716,6 +1720,10 @@ static int pp_collect_finished(struct parallel_processes *pp,
 
 		code = finish_command(&pp->children[i].process);
 
+		if (opts->pipe_output)
+			opts->pipe_output(&pp->children[i].err, pp->data,
+						pp->children[i].data);
+
 		if (opts->task_finished)
 			code = opts->task_finished(code, opts->ungroup ? NULL :
 						   &pp->children[i].err, pp->data,

...and in particular here, where the "err" is subsequently passed to "task_finished" *would* actually have an impact, but (again, re-arranging he diff a bit) ...

t/helper/test-run-command.c int cmd__run_command(int argc, const char **argv) · line 462

Quoted 13 lines
t/helper/test-run-command.c +6 -0
@@ -439,6 +451,12 @@ int cmd__run_command(int argc, const char **argv)
 		opts.ungroup = 1;
 	}
 
+	if (!strcmp(argv[1], "--pipe-output")) {
+		argv += 1;
+		argc -= 1;
+		opts.pipe_output = pipe_output;
+	}
+
 	jobs = atoi(argv[2]);
 	strvec_clear(&proc.args);
 	strvec_pushv(&proc.args, (const char **)argv + 3);

...here to test that we'd need our tests to combine "pipe_output" with "task_finished".

Which we should do in either case, e.g. if you define both and append to "err" in what order do we call them, and does "err" accumulate? (I think it should), in any case...

t/helper/test-run-command.c

Quoted 5 lines
t/helper/test-run-command.c +5 -0
+	/**
+	 * pipe_output: See pipe_output_fn() above. This should be
+	 * NULL unless process specific output is needed
+	 */
+	pipe_output_fn pipe_output;

...since we're on the topic we really should document what the behavior is. My understanding of this API has been that the "err" is not "const" because the callback is supposed to *append errors*, and there's an implicit contract to do nothing else than that. But looking at your new API docs:

Quoted 2 lines

+ * This callback is periodically called while child processes are
+ * running and also when a child process finishes.

Whatever you think this should be doing isn't documented precisely, i.e. is it called before/after some other callbacks, should the user rely on that at all, and...

Quoted 2 lines

+ * "struct strbuf *out" contains the output collected from pp_task_cb
+ * since the last call of this function.

...this just seems wrong. We don't collect outptu "from pp_task_cb", do you mean to say something, well, I was about to put words in your mouth, but I honestly don't know what you expect, so ...

*I'd* this to be documented as something like (which every other
callback does):

See run_processes_parallel() below for a discussion of the "struct strbuf *out" parameter.

Now *those* docs could be improved a bit, but how we handle "err/out" really needs to be documented holistically, as it's shared betwene callbacks.

Quoted 5 lines

+ *
+ * pp_cb is the callback cookie as passed into run_processes_parallel,
+ * pp_task_cb is the callback cookie as passed into get_next_task_fn.
+ *
+ * This function is incompatible with "ungroup"

I'll re-roll the base series, which has a good place to add a BUG() if incompatible options are combined, which would be good to add, i.e.:

	if (opts->pipe_output && opts->ungroup)
		BUG(...);

t/helper/test-run-command.c

Quoted 8 lines
t/helper/test-run-command.c +8 -0
+for opt in '' '--pipe-output'
+do
+	test_expect_success "run_command runs in parallel with more jobs available than tasks $opt" '
+		test-tool run-command run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
+		test_must_be_empty out &&
+		test_cmp expect err
+	'
+done

I take an earlier comment back, you do have tests that would change if that strbuf_reset() was removed, you're just not running them. You forgot to put an "$opt" into the "test-tool" invocations:

	diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh
	index feabb3717b0..19a674480cd 100755
	--- a/t/t0061-run-command.sh
	+++ b/t/t0061-run-command.sh
	@@ -162,7 +162,7 @@ test_expect_success 'run_command runs ungrouped in parallel with as many jobs as
	 for opt in '' '--pipe-output'
	 do
	 	test_expect_success "run_command runs in parallel with more tasks than jobs available $opt" '
	-		test-tool run-command run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
	+		test-tool run-command $opt run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
	 		test_must_be_empty out &&
	 		test_cmp expect err
	 	'
	@@ -186,7 +186,7 @@ EOF
	 for opt in '' '--pipe-output'
	 do
	 	test_expect_success "run_command is asked to abort gracefully $opt" '
	-		test-tool run-command run-command-abort 3 false >out 2>err &&
	+		test-tool run-command $opt run-command-abort 3 false >out 2>err &&
	 		test_must_be_empty out &&
	 		test_cmp expect err
	 	'
	@@ -205,7 +205,7 @@ EOF
	 for opt in '' '--pipe-output'
	 do
	 	test_expect_success "run_command outputs $opt" '
	-		test-tool run-command run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
	+		test-tool run-command $opt run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
	 		test_must_be_empty out &&
	 		test_cmp expect err
	 	'

With that all except 1 of your tests pass, now if I remove strbuf_reset() and tweak the test so we can see what output comes from what, then:

	diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c
	index ce246dfa4bc..690467e75c0 100644
	--- a/t/helper/test-run-command.c
	+++ b/t/helper/test-run-command.c
	@@ -56,12 +56,11 @@ static void pipe_output(struct strbuf *out,
	 				void *pp_cb,
	 				void *pp_task_cb)
	 {
	-	fprintf(stderr, "%s", out->buf);
	-	/*
	-	 * Resetting output to show that piped output would print the
	-	 * same as other tests without the pipe_output() function set
	-	 */
	-	strbuf_reset(out);
	+	struct strbuf sb = STRBUF_INIT;
	+
	+	strbuf_add_lines(&sb, "pipe_output: ", out->buf, out->len);
	+	fputs(sb.buf, stderr);
	+	strbuf_release(&sb);
	 }
	 
	 static int task_finished(int result,

The first test we fail (but with the reset we don't fail this one) is:

+ diff -u expect err
--- expect 2022-10-12 08:22:58.032264828 +0000
+++ err 2022-10-12 08:22:58.068264515 +0000 @@ -1,12 +1,24 @@
+pipe_output: preloaded output of a child +pipe_output: Hello
+pipe_output: World
preloaded output of a child
Hello
World
+pipe_output: preloaded output of a child +pipe_output: Hello
+pipe_output: World
+pipe_output: preloaded output of a child +pipe_output: Hello
+pipe_output: World
preloaded output of a child
Hello
World
preloaded output of a child
Hello
World
+pipe_output: preloaded output of a child +pipe_output: Hello
+pipe_output: World
preloaded output of a child
Hello
World

So, the apparent reason for the strbuf_reset() is that at some point in the past you passed $out, but wanted to "reset" it so that you could re-use the existing test_cmp.

But that's just wrong, because we'd be sweeping under the rug exactly what we want to be testing here. Instead of hiding this you should be test_cmp-ing the full thing.

#8 lore raw

Re: [PATCH v2 2/4] submodule: move status parsing into function

In reply to Calvin Wan

On Tue, Oct 11 2022, Calvin Wan wrote:

Just commenting on one case, but this is in other parts of the series (e.g. your additions in submodule.c):

submodule.c int fetch_submodules(struct repository *r,

Quoted 5 lines
submodule.c +2 -2
+			BUG("invalid status --porcelain=2 line %s",
+				buf);
[...]
-				BUG("invalid status --porcelain=2 line %s",
-				    buf.buf);

Your editor's indentation settings need fixing. Arguments should align with the opening "(", here you replaced 4 spaces with a "\t".

A "\t" is == 8 spaces for the purposes of our identation, if you need 7 spaces to align with the "7" you insert 7 spaces, if it's 8 a "\t", then for one more a "\t" and one space etc.

#11 lore raw

Re: [PATCH v2 4/4] diff-lib: parallelize run_diff_files for submodules

In reply to Calvin Wan

On Tue, Oct 11 2022, Calvin Wan wrote:

Mostly style comments, but also others.:

diff-lib.c static int check_removed(const struct index_state *istate, const struct cache_en · line 78

Quoted 2 lines
diff-lib.c +2 -0
+				     unsigned *dirty_submodule, int *defer_submodule_status,
+					 int *ignore_untracked_in_submodules)

If you can think of a (much) shorter name for this new paremeter, then...

diff-lib.c static int match_stat_with_submodule(struct diff_options *diffopt, · line 102

Quoted 7 lines
diff-lib.c +7 -0
+		if (defer_submodule_status && *defer_submodule_status) {
+			defer = 1;
+			*ignore_untracked_in_submodules =
+							diffopt->flags.ignore_untracked_in_submodules;
+		} else {
+			*dirty_submodule = is_submodule_modified(ce->name,
+							diffopt->flags.ignore_untracked_in_submodules);

...code like this becomes a lot less verbose and doesn't need this much indentation...

diff-lib.c int run_diff_files(struct rev_info *revs, unsigned int option) · line 275

Quoted 3 lines
diff-lib.c +3 -0
+			if (defer_submodule_status) {
+				struct string_list_item *item =
+								string_list_append(&submodules, ce->name);

And e.g. here splittng up the two:

	struct string_list_item *item;

	item = ...

Makes for easier reading IMO.

diff-lib.c int run_diff_files(struct rev_info *revs, unsigned int option) · line 281

Quoted 6 lines
diff-lib.c +6 -0
+				struct submodule_status_util *util = xmalloc(sizeof(*util));
+				util->changed = changed;
+				util->dirty_submodule = 0;
+				util->ignore_untracked = ignore_untracked_in_submodules;
+				util->newmode = newmode;
+				util->ce = ce;

Maybe easier to read as:

	struct submodule_status_util tmp = {
		.changed = ...,
		.dirty_submodule =...,
	};

Then a single memcpy() to copy that data over to the malloc'd region.

diff-lib.c int run_diff_files(struct rev_info *revs, unsigned int option) · line 282

Quoted 1 line
diff-lib.c +1 -0
+				item->util = util;

And you can also do this idiomatically as:

	string_list_append(...)->util = util;

diff-lib.c int run_diff_files(struct rev_info *revs, unsigned int option) · line 308

Quoted 6 lines
diff-lib.c +5 -0
+				continue;
 
+		int parallel_jobs = 1;
+		git_config_get_int("submodule.diffjobs", &parallel_jobs);
+		if (parallel_jobs < 0) {
+			die(_("submodule.diffjobs cannot be negative"));

Maybe we want something that uses git_parse_unsigned()?

Review

Quoted 1 line
(quoted diff) +1 -0
+		}

This should be cuddled with the "else if"

diff-lib.c int run_diff_files(struct rev_info *revs, unsigned int option) · line 315

Quoted 6 lines
diff-lib.c +6 -0
+		else if (!parallel_jobs) {
+			/*
+			 * TODO: Decide what a reasonable default for parallel_jobs
+			 * is. Currently mimics what other parallel config options
+			 * default to.
+			 */

It's OK to just drop this comment IMO.

Review

Quoted 2 lines
(quoted diff) +2 -0
+			parallel_jobs = online_cpus();
+		}

And as these are both one statement you can drop the {}'s altogether.

I think this would probably be more idiomatic as (untested):

	if (git_config_get_int(..., &v) || !v)
		jobs = online_cpus();
	else if (v < 0) /* or some API checks it for us? */
		die(...);
	else
		jobs = v;

I.e. we'd be explicitly using the "does the key exist" return value.

diff-lib.c int run_diff_files(struct rev_info *revs, unsigned int option) · line 320

Quoted 3 lines
diff-lib.c +3 -0
+
+		if (get_submodules_status(revs->repo, &submodules, parallel_jobs))
+			BUG("Submodule status failed");

s/Sub/sub/, lower-case for bug(), die() etc.

diff-lib.c int run_diff_files(struct rev_info *revs, unsigned int option) · line 321

Quoted 1 line
diff-lib.c +1 -0
+		for (i = 0; i < submodules.nr; i++) {

You're iterating a string_list, so that "i" earlier should be size_t, but better yet I think you can use for_each_string_list_item() here

submodule.c int submodule_touches_in_range(struct repository *r, · line 1367

Quoted 2 lines
submodule.c +2 -0
+struct submodule_parallel_status {
+	int index_count;

Here you have an int...

submodule.c int submodule_touches_in_range(struct repository *r, · line 1370

Quoted 3 lines
submodule.c +3 -0
+	int result;
+
+	struct string_list *submodule_names;

..that'll be tracking the "nr" of this, which is size_t, let's have them match.

Also, can we remove the "*" there and just have submodule.c populate this struct directly, maybe not worth making it public, just a thought...

..I didn't read further than this, ran out of time..

author
#3 lore raw
In reply to Junio C Hamano
Quoted 12 lines
Quoted 9 lines · Calvin Wan

I also wanted to pose another question to list regarding defaults for parallel processes. For jobs that clearly scale with the number of processes (aka jobs that are mostly processor bound), it is obvious that setting the default number of processes to the number of available cores is the most optimal option. However, this changes when the job is mostly I/O bound or has a combination of I/O and processing. Looking at my use case for status on a cold cache (see below), we notice that increasing the number of parallel processes speeds up status, but after a certain number, it actually starts slowing down.

Quoted 6 lines

I do not offhand recall how the default parallelism is computed there, but if I am correct to suspect that "git grep" has a similar scaling pattern, i.e. the threads all need to compete for I/O to read from the filesystem to find needles from the haystack, perhaps it would give us a precedent to model the behaviour of this part of the code, too, hopefully?

Setting grep.threads=0 does default it to the number of available cores (at least the documentation is clear about this). I tested "git grep" on my machine and found that it started slowing down after 4 threads -- this is most likely because my NVMe SSD uses 4 PCIe lanes aka it can at most do 4 reads in parallel. AFAIK, there is no way to tell how many reads a disk can do in parallel. This coupled with the fact that other commands have varying levels of IO requirements makes it impossible to set a "reasonable" amount of threads.