Merge branch 'vl/grep-configurable-threads'
"git grep" can now be configured (or told from the command line) how many threads to use when searching in the working tree files. * vl/grep-configurable-threads: grep: add --threads=<num> option and grep.threads configuration grep: slight refactoring to the code that disables threading grep: allow threading even on a single-core machine
This commit is contained in:
commit
bdd1cc2092
@ -1450,6 +1450,10 @@ grep.extendedRegexp::
|
|||||||
option is ignored when the 'grep.patternType' option is set to a value
|
option is ignored when the 'grep.patternType' option is set to a value
|
||||||
other than 'default'.
|
other than 'default'.
|
||||||
|
|
||||||
|
grep.threads::
|
||||||
|
Number of grep worker threads to use.
|
||||||
|
See `grep.threads` in linkgit:git-grep[1] for more information.
|
||||||
|
|
||||||
gpg.program::
|
gpg.program::
|
||||||
Use this custom program instead of "gpg" found on $PATH when
|
Use this custom program instead of "gpg" found on $PATH when
|
||||||
making or verifying a PGP signature. The program must support the
|
making or verifying a PGP signature. The program must support the
|
||||||
|
@ -23,6 +23,7 @@ SYNOPSIS
|
|||||||
[--break] [--heading] [-p | --show-function]
|
[--break] [--heading] [-p | --show-function]
|
||||||
[-A <post-context>] [-B <pre-context>] [-C <context>]
|
[-A <post-context>] [-B <pre-context>] [-C <context>]
|
||||||
[-W | --function-context]
|
[-W | --function-context]
|
||||||
|
[--threads <num>]
|
||||||
[-f <file>] [-e] <pattern>
|
[-f <file>] [-e] <pattern>
|
||||||
[--and|--or|--not|(|)|-e <pattern>...]
|
[--and|--or|--not|(|)|-e <pattern>...]
|
||||||
[ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>...]
|
[ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>...]
|
||||||
@ -53,6 +54,10 @@ grep.extendedRegexp::
|
|||||||
option is ignored when the 'grep.patternType' option is set to a value
|
option is ignored when the 'grep.patternType' option is set to a value
|
||||||
other than 'default'.
|
other than 'default'.
|
||||||
|
|
||||||
|
grep.threads::
|
||||||
|
Number of grep worker threads to use. If unset (or set to 0),
|
||||||
|
8 threads are used by default (for now).
|
||||||
|
|
||||||
grep.fullName::
|
grep.fullName::
|
||||||
If set to true, enable '--full-name' option by default.
|
If set to true, enable '--full-name' option by default.
|
||||||
|
|
||||||
@ -227,6 +232,10 @@ OPTIONS
|
|||||||
effectively showing the whole function in which the match was
|
effectively showing the whole function in which the match was
|
||||||
found.
|
found.
|
||||||
|
|
||||||
|
--threads <num>::
|
||||||
|
Number of grep worker threads to use.
|
||||||
|
See `grep.threads` in 'CONFIGURATION' for more information.
|
||||||
|
|
||||||
-f <file>::
|
-f <file>::
|
||||||
Read patterns from <file>, one per line.
|
Read patterns from <file>, one per line.
|
||||||
|
|
||||||
|
@ -24,11 +24,11 @@ static char const * const grep_usage[] = {
|
|||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
static int use_threads = 1;
|
#define GREP_NUM_THREADS_DEFAULT 8
|
||||||
|
static int num_threads;
|
||||||
|
|
||||||
#ifndef NO_PTHREADS
|
#ifndef NO_PTHREADS
|
||||||
#define THREADS 8
|
static pthread_t *threads;
|
||||||
static pthread_t threads[THREADS];
|
|
||||||
|
|
||||||
/* We use one producer thread and THREADS consumer
|
/* We use one producer thread and THREADS consumer
|
||||||
* threads. The producer adds struct work_items to 'todo' and the
|
* threads. The producer adds struct work_items to 'todo' and the
|
||||||
@ -63,13 +63,13 @@ static pthread_mutex_t grep_mutex;
|
|||||||
|
|
||||||
static inline void grep_lock(void)
|
static inline void grep_lock(void)
|
||||||
{
|
{
|
||||||
if (use_threads)
|
if (num_threads)
|
||||||
pthread_mutex_lock(&grep_mutex);
|
pthread_mutex_lock(&grep_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void grep_unlock(void)
|
static inline void grep_unlock(void)
|
||||||
{
|
{
|
||||||
if (use_threads)
|
if (num_threads)
|
||||||
pthread_mutex_unlock(&grep_mutex);
|
pthread_mutex_unlock(&grep_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,7 +206,8 @@ static void start_threads(struct grep_opt *opt)
|
|||||||
strbuf_init(&todo[i].out, 0);
|
strbuf_init(&todo[i].out, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_SIZE(threads); i++) {
|
threads = xcalloc(num_threads, sizeof(*threads));
|
||||||
|
for (i = 0; i < num_threads; i++) {
|
||||||
int err;
|
int err;
|
||||||
struct grep_opt *o = grep_opt_dup(opt);
|
struct grep_opt *o = grep_opt_dup(opt);
|
||||||
o->output = strbuf_out;
|
o->output = strbuf_out;
|
||||||
@ -238,12 +239,14 @@ static int wait_all(void)
|
|||||||
pthread_cond_broadcast(&cond_add);
|
pthread_cond_broadcast(&cond_add);
|
||||||
grep_unlock();
|
grep_unlock();
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_SIZE(threads); i++) {
|
for (i = 0; i < num_threads; i++) {
|
||||||
void *h;
|
void *h;
|
||||||
pthread_join(threads[i], &h);
|
pthread_join(threads[i], &h);
|
||||||
hit |= (int) (intptr_t) h;
|
hit |= (int) (intptr_t) h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(threads);
|
||||||
|
|
||||||
pthread_mutex_destroy(&grep_mutex);
|
pthread_mutex_destroy(&grep_mutex);
|
||||||
pthread_mutex_destroy(&grep_read_mutex);
|
pthread_mutex_destroy(&grep_read_mutex);
|
||||||
pthread_mutex_destroy(&grep_attr_mutex);
|
pthread_mutex_destroy(&grep_attr_mutex);
|
||||||
@ -267,6 +270,14 @@ static int grep_cmd_config(const char *var, const char *value, void *cb)
|
|||||||
int st = grep_config(var, value, cb);
|
int st = grep_config(var, value, cb);
|
||||||
if (git_color_default_config(var, value, cb) < 0)
|
if (git_color_default_config(var, value, cb) < 0)
|
||||||
st = -1;
|
st = -1;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
return st;
|
return st;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,7 +305,7 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifndef NO_PTHREADS
|
#ifndef NO_PTHREADS
|
||||||
if (use_threads) {
|
if (num_threads) {
|
||||||
add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, sha1);
|
add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, sha1);
|
||||||
strbuf_release(&pathbuf);
|
strbuf_release(&pathbuf);
|
||||||
return 0;
|
return 0;
|
||||||
@ -323,7 +334,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
|
|||||||
strbuf_addstr(&buf, filename);
|
strbuf_addstr(&buf, filename);
|
||||||
|
|
||||||
#ifndef NO_PTHREADS
|
#ifndef NO_PTHREADS
|
||||||
if (use_threads) {
|
if (num_threads) {
|
||||||
add_work(opt, GREP_SOURCE_FILE, buf.buf, filename, filename);
|
add_work(opt, GREP_SOURCE_FILE, buf.buf, filename, filename);
|
||||||
strbuf_release(&buf);
|
strbuf_release(&buf);
|
||||||
return 0;
|
return 0;
|
||||||
@ -697,6 +708,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
|
|||||||
N_("show <n> context lines before matches")),
|
N_("show <n> context lines before matches")),
|
||||||
OPT_INTEGER('A', "after-context", &opt.post_context,
|
OPT_INTEGER('A', "after-context", &opt.post_context,
|
||||||
N_("show <n> context lines after matches")),
|
N_("show <n> context lines after matches")),
|
||||||
|
OPT_INTEGER(0, "threads", &num_threads,
|
||||||
|
N_("use <n> worker threads")),
|
||||||
OPT_NUMBER_CALLBACK(&opt, N_("shortcut for -C NUM"),
|
OPT_NUMBER_CALLBACK(&opt, N_("shortcut for -C NUM"),
|
||||||
context_callback),
|
context_callback),
|
||||||
OPT_BOOL('p', "show-function", &opt.funcname,
|
OPT_BOOL('p', "show-function", &opt.funcname,
|
||||||
@ -786,7 +799,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
|
|||||||
opt.output_priv = &path_list;
|
opt.output_priv = &path_list;
|
||||||
opt.output = append_path;
|
opt.output = append_path;
|
||||||
string_list_append(&path_list, show_in_pager);
|
string_list_append(&path_list, show_in_pager);
|
||||||
use_threads = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!opt.pattern_list)
|
if (!opt.pattern_list)
|
||||||
@ -817,14 +829,18 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifndef NO_PTHREADS
|
#ifndef NO_PTHREADS
|
||||||
if (list.nr || cached || online_cpus() == 1)
|
if (list.nr || cached || show_in_pager)
|
||||||
use_threads = 0;
|
num_threads = 0;
|
||||||
|
else if (num_threads == 0)
|
||||||
|
num_threads = GREP_NUM_THREADS_DEFAULT;
|
||||||
|
else if (num_threads < 0)
|
||||||
|
die(_("invalid number of threads specified (%d)"), num_threads);
|
||||||
#else
|
#else
|
||||||
use_threads = 0;
|
num_threads = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef NO_PTHREADS
|
#ifndef NO_PTHREADS
|
||||||
if (use_threads) {
|
if (num_threads) {
|
||||||
if (!(opt.name_only || opt.unmatch_name_only || opt.count)
|
if (!(opt.name_only || opt.unmatch_name_only || opt.count)
|
||||||
&& (opt.pre_context || opt.post_context ||
|
&& (opt.pre_context || opt.post_context ||
|
||||||
opt.file_break || opt.funcbody))
|
opt.file_break || opt.funcbody))
|
||||||
@ -894,7 +910,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
|
|||||||
hit = grep_objects(&opt, &pathspec, &list);
|
hit = grep_objects(&opt, &pathspec, &list);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (use_threads)
|
if (num_threads)
|
||||||
hit |= wait_all();
|
hit |= wait_all();
|
||||||
if (hit && show_in_pager)
|
if (hit && show_in_pager)
|
||||||
run_pager(&opt, prefix);
|
run_pager(&opt, prefix);
|
||||||
|
@ -1312,6 +1312,7 @@ _git_grep ()
|
|||||||
--full-name --line-number
|
--full-name --line-number
|
||||||
--extended-regexp --basic-regexp --fixed-strings
|
--extended-regexp --basic-regexp --fixed-strings
|
||||||
--perl-regexp
|
--perl-regexp
|
||||||
|
--threads
|
||||||
--files-with-matches --name-only
|
--files-with-matches --name-only
|
||||||
--files-without-match
|
--files-without-match
|
||||||
--max-depth
|
--max-depth
|
||||||
|
Loading…
x
Reference in New Issue
Block a user