Skip to contentSkip to conversation

Add git-grep threads param

PATCH In review Victor Leschuk 3 participants 6 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

v8 cover Add git-grep threads param

Introducing v8 of git-grep threads param patch.
Patch is now split in 2 parts - 1/2 is actually renewed v6 version (see list of changes below), 2/2 removes dependency on online_cpus() - as we discussed with Eric this is rather significant change in default behavior and should be placed into separate patch.

Here is list of changes since v6 ($gmane/281160):

* Fixed broken t7811: moved all threads_num setup to 1 place (for -O option it was in wrong place)
* Fixed 'invalid number of threads' message so that it could be translated
* Got rid of grep_threads_config() - its too trivial to be separate function
* Fixed xcalloc() args (sizeof(pthread_t) -> sizeof(*threads)) to correspond to general git style
* Improved commit message (in 2/2) to explain why online_cpus() is now not used in threads_num setup
* The full param documentation was moved into single place (grep.threads description in git-grep.txt) and is referenced from other places. Also made few language improvements in documentation.
* Style improvements: splitted too long lines

Victor Leschuk (2):
"git grep" can now be configured (or told from the command line)
how many threads to use when searching in the working tree files.
Number of threads now doesn't depend on online_cpus(), e.g. if
specific number is not configured GREP_NUM_THREADS_DEFAULT (8) threads will be used even on 1-core CPU.

 Documentation/config.txt               |  4 +++
 Documentation/git-grep.txt             | 12 +++++++++
 builtin/grep.c                         | 49 +++++++++++++++++++++++-----------
 contrib/completion/git-completion.bash |  1 +
 4 files changed, 51 insertions(+), 15 deletions(-)
Signature · 1 line
2.6.3.369.g3e7f205.dirty
#1 lore raw

PATCH v8 1/2 Introduce grep threads param

"git grep" can now be configured (or told from the command line) how many threads to use when searching in the working tree files.

---
 Documentation/config.txt               |  4 +++
 Documentation/git-grep.txt             | 12 +++++++++
 builtin/grep.c                         | 49 +++++++++++++++++++++++-----------
 contrib/completion/git-completion.bash |  1 +
 4 files changed, 51 insertions(+), 15 deletions(-)
Documentation/config.txt +4 -0
index 2d06b11..cbf4071 100644
@@ -1450,6 +1450,10 @@ grep.extendedRegexp::
 	option is ignored when the 'grep.patternType' option is set to a value
 	other than 'default'.
 
+grep.threads::
+	Number of grep worker threads.
+	See `grep.threads` in linkgit:git-grep[1] for more information.
+
 gpg.program::
 	Use this custom program instead of "gpg" found on $PATH when
 	making or verifying a PGP signature. The program must support the
Documentation/git-grep.txt +12 -0
index 4a44d6d..25e6dc5 100644
@@ -23,6 +23,7 @@ SYNOPSIS
 	   [--break] [--heading] [-p | --show-function]
 	   [-A <post-context>] [-B <pre-context>] [-C <context>]
 	   [-W | --function-context]
+	   [--threads <num>]
 	   [-f <file>] [-e] <pattern>
 	   [--and|--or|--not|(|)|-e <pattern>...]
 	   [ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>...]
@@ -53,6 +54,13 @@ grep.extendedRegexp::
 	option is ignored when the 'grep.patternType' option is set to a value
 	other than 'default'.
 
+grep.threads::
+	Number of grep worker threads, use it to tune up performance on
+	your machines. Leave it unset (or set to 0) for default behavior,
+	which is using 8 threads for all systems.
+	Default behavior may change in future versions
+	to better suit hardware and circumstances.
+
 grep.fullName::
 	If set to true, enable '--full-name' option by default.
 
@@ -227,6 +235,10 @@ OPTIONS
 	effectively showing the whole function in which the match was
 	found.
 
+--threads <num>::
+	Number of grep worker threads.
+	See `grep.threads` in 'CONFIGURATION' for more information.
+
 -f <file>::
 	Read patterns from <file>, one per line.
 
builtin/grep.c +34 -15

131 lines not on this page · raw message

contrib/completion/git-completion.bash +1 -0
index 111b053..d5c3e3f 100644
@@ -1311,6 +1311,7 @@ _git_grep ()
 			--full-name --line-number
 			--extended-regexp --basic-regexp --fixed-strings
 			--perl-regexp
+			--threads
 			--files-with-matches --name-only
 			--files-without-match
 			--max-depth
Signature · 1 line
2.6.3.369.g3e7f205.dirty
#2 lore raw

Re: [PATCH 1/2] Introduce grep threads param

Quoted 1 line · Victor Leschuk

Subject: Re: [PATCH 1/2] Introduce grep threads param

I'll retitle this to something like

grep: add --threads=<num> option and grep.threads configuration

while queuing (which I did for v7 earlier).

Documentation/config.txt grep.extendedRegexp:: · line 1454

Quoted 8 lines

"git grep" can now be configured (or told from the command line) how many threads to use when searching in the working tree files.

---
...

Documentation/config.txt +2 -0
+grep.threads::
+	Number of grep worker threads.

"Number of grep worker threads to use"?

Documentation/git-grep.txt grep.extendedRegexp:: · line 62

Quoted 8 lines
Documentation/git-grep.txt +7 -0
+	See `grep.threads` in linkgit:git-grep[1] for more information.
...
+grep.threads::
+	Number of grep worker threads, use it to tune up performance on
+	your machines. Leave it unset (or set to 0) for default behavior,
+	which is using 8 threads for all systems.
+	Default behavior may change in future versions
+	to better suit hardware and circumstances.

The last sentence is too noisy. Perhaps drop it and phrase it like this instead?

grep.threads::
Number of grep worker threads to use. If unset (or set to 0), to 0), 8 threads are used by default (for now).

builtin/grep.c static char const * const grep_usage[] = { · line 28

Quoted 11 lines
builtin/grep.c +2 -1
index 4229cae..e9aebab 100644
@@ -24,11 +24,11 @@ static char const * const grep_usage[] = {
 	NULL
 };
 
-static int use_threads = 1;
+#define GREP_NUM_THREADS_DEFAULT 8
+static int num_threads = 0;

Please do not initialize static to 0 (or NULL).

builtin/grep.c static int grep_cmd_config(const char *var, const char *value, void *cb) · line 275

Quoted 7 lines
builtin/grep.c +3 -0
@@ -267,6 +270,12 @@ static int grep_cmd_config(const char *var, const char *value, void *cb)
 	int st = grep_config(var, value, cb);
 	if (git_color_default_config(var, value, cb) < 0)
 		st = -1;
+
+	if (!strcmp(var, "grep.threads")) {
+		/* Sanity check of value will be perfomed later */

Hmm, is that a good design?

A user may hear "invalid number of threads specified (-4)" later, but if that came from "grep.threads", especially when the user did not say "--threads=-4" from the command line, would she know to check her configuration file?

If she had "grep.threads=Yes" in her configuration, we would helpfully tell her that 'Yes' given to grep.threads is not a valid integer. Shouldn't we do the same for '-4' given to grep.threads, too?

	if (!strcmp(var, "grep.threads")) {
		num_threads = git_config_int(var, value);
		if (num_threads < 0)
			die(_("invalid number of threads specified (%d) for %s"),
			    num_threads, var);
	}

perhaps.

builtin/grep.c int cmd_grep(int argc, const char **argv, const char *prefix) · line 832

Quoted 9 lines
builtin/grep.c +3 -2
@@ -817,14 +827,23 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	}
 
 #ifndef NO_PTHREADS
-	if (list.nr || cached || online_cpus() == 1)
-		use_threads = 0;
+	if (list.nr || cached || online_cpus() == 1 || show_in_pager) {
+		/* Can not multi-thread object lookup */
+		num_threads = 0;

Removing 'use_threads = 0' from an earlier part and moving the check to show_in_pager is a good idea, but it invalidates this comment. The earlier three (actually two and a half) are "cannot" cases, i.e. the object layer is not easily threaded without locking, and when you have a single core, you do not truly run multiple operations at the same time, but as [PATCH 2/2] does, threading in "grep" is not about CPU alone, so that is why I am demoting it to just a half ;-). But show_in_pager is "we do not want to", I think.

In any case, this comment and "User didn't specify" below are not telling the reader something very much useful. You probably should remove them.

builtin/grep.c int cmd_grep(int argc, const char **argv, const char *prefix) · line 840

Quoted 8 lines
builtin/grep.c +8 -0
+	}
+	else if (num_threads == 0) {
+		/* User didn't specify value, or just wants default behavior */
+		num_threads = GREP_NUM_THREADS_DEFAULT;
+	}
+	else if (num_threads < 0) {
+		die(_("invalid number of threads specified (%d)"), num_threads);
+	}

Many unnecessary braces.

I think [2/2] and also moving the code to disable threading when show-in-pager mode should be separate "preparatory clean-up" patches before this main patch. I'll push out what I think this topic should be on 'pu' later today (with fixups suggested above squashed in); please check them and see what you think.

Thanks.

#3 lore raw

Re: [PATCH 1/2] Introduce grep threads param

Hello Junio,

Quoted 8 lines · Junio C Hamano
Quoted 1 line · Victor Leschuk

Subject: Re: [PATCH 1/2] Introduce grep threads param

I'll retitle this to something like

grep: add --threads=<num> option and grep.threads configuration

while queuing (which I did for v7 earlier).

Ok, thanks.

Quoted 10 lines

Documentation/config.txt grep.extendedRegexp:: · line 1454

Quoted 9 lines

"git grep" can now be configured (or told from the command line) how many threads to use when searching in the working tree files.

---
...

Documentation/config.txt +2 -0
+grep.threads::
+	Number of grep worker threads.

"Number of grep worker threads to use"?

Accepted, will change.

Quoted 12 lines

Documentation/git-grep.txt grep.extendedRegexp:: · line 62

Quoted 9 lines
Documentation/git-grep.txt +7 -0
+	See `grep.threads` in linkgit:git-grep[1] for more information.
...
+grep.threads::
+	Number of grep worker threads, use it to tune up performance on
+	your machines. Leave it unset (or set to 0) for default behavior,
+	which is using 8 threads for all systems.
+	Default behavior may change in future versions
+	to better suit hardware and circumstances.

The last sentence is too noisy. Perhaps drop it and phrase it like this instead?

Quoted 3 lines

grep.threads::
Number of grep worker threads to use. If unset (or set to 0), to 0), 8 threads are used by default (for now).

Agree.

Quoted 9 lines
Quoted 9 lines
builtin/grep.c +0 -0
index 4229cae..e9aebab 100644
@@ -24,11 +24,11 @@ static char const * const grep_usage[] = {
  	NULL
  };
 
Quoted 4 lines

builtin/grep.c static char const * const grep_usage[] = { · line 28

Quoted 3 lines
builtin/grep.c +2 -1
-static int use_threads = 1;
+#define GREP_NUM_THREADS_DEFAULT 8
+static int num_threads = 0;

Please do not initialize static to 0 (or NULL).

Ok.

Quoted 20 lines

builtin/grep.c static int grep_cmd_config(const char *var, const char *value, void *cb) · line 275

Quoted 8 lines
builtin/grep.c +3 -0
@@ -267,6 +270,12 @@ static int grep_cmd_config(const char *var, const char *value, void *cb)
  	int st = grep_config(var, value, cb);
  	if (git_color_default_config(var, value, cb) < 0)
  		st = -1;
+
+	if (!strcmp(var, "grep.threads")) {
+		/* Sanity check of value will be perfomed later */

Hmm, is that a good design?

A user may hear "invalid number of threads specified (-4)" later, but if that came from "grep.threads", especially when the user did not say "--threads=-4" from the command line, would she know to check her configuration file?

If she had "grep.threads=Yes" in her configuration, we would helpfully tell her that 'Yes' given to grep.threads is not a valid integer. Shouldn't we do the same for '-4' given to grep.threads, too?

Quoted 8 lines
	if (!strcmp(var, "grep.threads")) {
		num_threads = git_config_int(var, value);
		if (num_threads < 0)
			die(_("invalid number of threads specified (%d) for %s"),
			    num_threads, var);
	}

perhaps.

When I was doing this I looked at other code and saw that exact message "Invalid number of threads..." is used in other parts of git and is present in 'po' translations. Thus I decided to use exactly the same message in order not to create numerous almost similar localizations (which we should do if we use two different messages in different places). Do you think we need to create two more different messages (and translations, I can prepare Russian and French) and create two checks for cmd and config?

Quoted 19 lines

builtin/grep.c int cmd_grep(int argc, const char **argv, const char *prefix) · line 832

Quoted 10 lines
builtin/grep.c +3 -2
@@ -817,14 +827,23 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
  	}
 
  #ifndef NO_PTHREADS
-	if (list.nr || cached || online_cpus() == 1)
-		use_threads = 0;
+	if (list.nr || cached || online_cpus() == 1 || show_in_pager) {
+		/* Can not multi-thread object lookup */
+		num_threads = 0;

Removing 'use_threads = 0' from an earlier part and moving the check to show_in_pager is a good idea, but it invalidates this comment. The earlier three (actually two and a half) are "cannot" cases, i.e. the object layer is not easily threaded without locking, and when you have a single core, you do not truly run multiple operations at the same time, but as [PATCH 2/2] does, threading in "grep" is not about CPU alone, so that is why I am demoting it to just a half ;-). But show_in_pager is "we do not want to", I think.

Quoted 3 lines

In any case, this comment and "User didn't specify" below are not telling the reader something very much useful. You probably should remove them.

Ok, will remove comments.

Quoted 10 lines

builtin/grep.c int cmd_grep(int argc, const char **argv, const char *prefix) · line 840

Quoted 9 lines
builtin/grep.c +8 -0
+	}
+	else if (num_threads == 0) {
+		/* User didn't specify value, or just wants default behavior */
+		num_threads = GREP_NUM_THREADS_DEFAULT;
+	}
+	else if (num_threads < 0) {
+		die(_("invalid number of threads specified (%d)"), num_threads);
+	}

Many unnecessary braces.

I put braces to make code look more unified. I had to put braces here:

+ else if (num_threads == 0) {
+ /* User didn't specify value, or just wants default behavior */

In order to be able to place long comment above the line. And code like:

if (a)
do_smth();
else if (b) {
do_smth1();
do_smth2();
}
else
do_smth3();

Looks rather ugly to me. Usually I put braces to all 'ifs' if any part of block requires them. Actually I will remove comments as I said above, and thus remove all braces.

Quoted 8 lines

I think [2/2] and also moving the code to disable threading when show-in-pager mode should be separate "preparatory clean-up" patches before this main patch. I'll push out what I think this topic should be on 'pu' later today (with fixups suggested above squashed in); please check them and see what you think.

Thanks.

Ok, I will prepare v8 as soon as we finish discussion on this one.

Thanks for the review.

--
Victor

#4 lore raw

Re: [PATCH 1/2] Introduce grep threads param

In reply to Junio C Hamano
Quoted 7 lines · Junio C Hamano
Quoted 1 line · Victor Leschuk

Subject: Re: [PATCH 1/2] Introduce grep threads param

I'll retitle this to something like

grep: add --threads=<num> option and grep.threads configuration

Quoted 7 lines

while queuing (which I did for v7 earlier).

I think [2/2] and also moving the code to disable threading when show-in-pager mode should be separate "preparatory clean-up" patches before this main patch. I'll push out what I think this topic should be on 'pu' later today (with fixups suggested above squashed in); please check them and see what you think.

I read over what was pushed to 'pu' and noticed a couple problems.

First, the 'online_cpus() == 1' check, which was removed in patch 1/3, accidentally creeps back in with patch 3/3.

Quoted 7 lines

Documentation/git-grep.txt grep.extendedRegexp:: · line 62

Quoted 6 lines
Documentation/git-grep.txt +6 -0
+grep.threads::
+     Number of grep worker threads, use it to tune up performance on
+     your machines. Leave it unset (or set to 0) for default behavior,
+     which is using 8 threads for all systems.
+     Default behavior may change in future versions
+     to better suit hardware and circumstances.
Quoted 6 lines

The last sentence is too noisy. Perhaps drop it and phrase it like this instead?

grep.threads::
Number of grep worker threads to use. If unset (or set to 0), to 0), 8 threads are used by default (for now).

Second, the stray "to 0)," on the second line needs to be dropped.

Other than that, the series looks reasonable.

#5 lore raw

PATCH v8 2/2 Get rid of online_cpus() when determining grep threads num

In reply to Victor Leschuk

Number of threads now doesn't depend on online_cpus(),
e.g. if specific number is not configured GREP_NUM_THREADS_DEFAULT (8) threads will be used even on 1-core CPU.

Reason: multithreading can improve performance even on single core machines as IO is also a major factor here. Using multiple threads can significantly boost grep performance when working on slow filesystems (or repo isn't cached) or through network (for example repo is located on NFS).

---
 builtin/grep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/grep.c +1 -1
index e9aebab..1315905 100644
@@ -827,7 +827,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	}
 
 #ifndef NO_PTHREADS
-	if (list.nr || cached || online_cpus() == 1 || show_in_pager) {
+	if (list.nr || cached || show_in_pager) {
 		/* Can not multi-thread object lookup */
 		num_threads = 0;
 	}
Signature · 1 line
2.6.3.369.g3e7f205.dirty