Rerolling sb/submodule-parallel-fetch for the time after 2.7
v2 cover Rerolling sb/submodule-parallel-fetch for the time after 2.7
I am sending out a new version for replacing sb/submodule-parallel-fetch for the time after the 2.7 release.
* Dropped the patch, which introduces xread_nonblock
* strbuf_read_once uses xread now. This is safe as we poll before using strbuf_read_once, so we know we won't stall.
* have the commit message reworded for "run-command: add an asynchronous parallel child processor" with Johannes' suggestion.
Thanks,
Stefan
Jonathan Nieder (1):
submodule.c: write "Fetching submodule <foo>" to stderr
Stefan Beller (6):
xread: poll on non blocking fds
strbuf: add strbuf_read_once to read without blocking
sigchain: add command to pop all common signals
run-command: add an asynchronous parallel child processor fetch_populated_submodules: use new parallel job processing
submodules: allow parallel fetching, add tests and documentation
Documentation/fetch-options.txt | 7 + builtin/fetch.c | 6 +- builtin/pull.c | 6 + run-command.c | 335 ++++++++++++++++++++++++++++++++++++++++ run-command.h | 80 ++++++++++ sigchain.c | 9 ++ sigchain.h | 1 + strbuf.c | 11 ++ strbuf.h | 8 + submodule.c | 141 +++++++++++------ submodule.h | 2 +- t/t0061-run-command.sh | 53 +++++++ t/t5526-fetch-submodules.sh | 71 ++++++--- test-run-command.c | 55 ++++++- wrapper.c | 20 ++- 15 files changed, 731 insertions(+), 74 deletions(-)
Signature · 1 line
2.6.4.443.ge094245.dirty
PATCH v2 1/7 submodule.c: write "Fetching submodule <foo>" to stderr
From: Jonathan Nieder <[email protected]>
The "Pushing submodule <foo>" progress output correctly goes to stderr, but "Fetching submodule <foo>" is going to stdout by mistake. Fix it to write to stderr.
Noticed while trying to implement a parallel submodule fetch. When this particular output line went to a different file descriptor, it was buffered separately, resulting in wrongly interleaved output if we copied it to the terminal naively.
--- submodule.c | 2 +- t/t5526-fetch-submodules.sh | 51 +++++++++++++++++++++++---------------------- 2 files changed, 27 insertions(+), 26 deletions(-)
submodule.c +1 -1
@@ -689,7 +689,7 @@ int fetch_populated_submodules(const struct argv_array *options, git_dir = submodule_git_dir.buf;if (is_directory (git_dir)) {if (!quiet) -printf ("Fetching submodule %s%s\n" , prefix, ce->name); +fprintf (stderr,"Fetching submodule %s%s\n" , prefix, ce->name); cp.dir = submodule_path.buf;argv_array_push (&argv, default_argv);argv_array_push (&argv,"--submodule-prefix" );
t/t5526-fetch-submodules.sh +26 -25
189 lines not on this page · raw message
Signature · 1 line
2.6.4.443.ge094245.dirty
PATCH v2 2/7 xread: poll on non blocking fds
In reply to Stefan BellerThe man page of read(2) says:
EAGAIN The file descriptor fd refers to a file other than a socket
and has been marked nonblocking (O_NONBLOCK), and the read would block.
EAGAIN or EWOULDBLOCK
The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.
If we get an EAGAIN or EWOULDBLOCK the fd must have set O_NONBLOCK. As the intent of xread is to read as much as possible either until the fd is EOF or an actual error occurs, we can ease the feeder of the fd by not spinning the whole time, but rather wait for it politely by not busy waiting.
We should not care if the call to poll failed, as we're in an infinite loop and can only get out with the correct read().
--- wrapper.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
wrapper.c +18 -2
@@ -236,8 +236,24 @@ ssize_t xread(int fd, void *buf, size_t len) len =MAX_IO_SIZE ;while (1 ) { nr =read (fd, buf, len); -if ((nr <0 ) && (errno ==EAGAIN || errno ==EINTR )) -continue ; +if (nr <0 ) { +if (errno ==EINTR ) +continue ; +if (errno ==EAGAIN || errno ==EWOULDBLOCK ) { +struct pollfd pfd; + pfd.events =POLLIN ; + pfd.fd = fd; +/* +* it is OK if this poll() failed; we +* want to leave this infinite loop +* only when read() returns with +* success, or an expected failure, +* which would be checked by the next +* call to read(2). +*/ +poll (&pfd,1 ,-1 ); + } + }return nr; } }
Signature · 1 line
2.6.4.443.ge094245.dirty
PATCH v2 3/7 strbuf: add strbuf_read_once to read without blocking
In reply to Stefan BellerThe new call will read from a file descriptor into a strbuf once. The underlying call xread is just run once. xread only reattempts reading in case of EINTR, which makes it suitable to use for a nonblocking read.
--- strbuf.c | 11 +++++++++++ strbuf.h | 8 ++++++++ 2 files changed, 19 insertions(+)
strbuf.c +11 -0
@@ -384,6 +384,17 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)return sb->len - oldlen; } +ssize_t strbuf_read_once (struct strbuf *sb,int fd,size_t hint) +{ +ssize_t cnt; + +strbuf_grow (sb, hint ? hint :8192 ); + cnt =xread (fd, sb->buf + sb->len, sb->alloc - sb->len -1 ); +if (cnt >0 ) +strbuf_setlen (sb, sb->len + cnt); +return cnt; +} +#define STRBUF_MAXLINK (2*PATH_MAX)int strbuf_readlink (struct strbuf *sb,const char *path,size_t hint)
strbuf.h +8 -0
@@ -367,6 +367,14 @@ extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);extern ssize_t strbuf_read (struct strbuf *,int fd,size_t hint);/** +* Read the contents of a given file descriptor partially by using only one +* attempt of xread. The third argument can be used to give a hint about the +* file size, to avoid reallocs. Returns the number of new bytes appended to +* the sb. +*/ +extern ssize_t strbuf_read_once (struct strbuf *,int fd,size_t hint); + +/** * Read the contents of a file, specified by its path. The third argument * can be used to give a hint about the file size, to avoid reallocs. */
Signature · 1 line
2.6.4.443.ge094245.dirty
PATCH v2 4/7 sigchain: add command to pop all common signals
In reply to Stefan BellerThe new method removes all common signal handlers that were installed by sigchain_push.
--- sigchain.c | 9 +++++++++ sigchain.h | 1 + 2 files changed, 10 insertions(+)
sigchain.c +9 -0
@@ -50,3 +50,12 @@ void sigchain_push_common(sigchain_fun f)sigchain_push (SIGQUIT , f);sigchain_push (SIGPIPE , f); } + +void sigchain_pop_common (void ) +{ +sigchain_pop (SIGPIPE ); +sigchain_pop (SIGQUIT ); +sigchain_pop (SIGTERM ); +sigchain_pop (SIGHUP ); +sigchain_pop (SIGINT ); +}
sigchain.h +1 -0
@@ -7,5 +7,6 @@ int sigchain_push(int sig, sigchain_fun f);int sigchain_pop (int sig);void sigchain_push_common (sigchain_fun f); +void sigchain_pop_common (void );#endif /* SIGCHAIN_H */
Signature · 1 line
2.6.4.443.ge094245.dirty
PATCH v2 5/7 run-command: add an asynchronous parallel child processor
In reply to Stefan BellerThis allows to run external commands in parallel with ordered output on stderr.
If we run external commands in parallel we cannot pipe the output directly to the our stdout/err as it would mix up. So each process's output will flow through a pipe, which we buffer. One subprocess can be directly piped to out stdout/err for a low latency feedback to the user.
Example:
Let's assume we have 5 submodules A,B,C,D,E and each fetch takes a different amount of time as the different submodules vary in size, then the output of fetches in sequential order might look like this:
time -->
output: |---A---| |-B-| |-------C-------| |-D-| |-E-|
When we schedule these submodules into maximal two parallel processes, a schedule and sample output over time may look like this:
process 1: |---A---| |-D-| |-E-|
process 2: |-B-| |-------C-------|
output: |---A---|B|---C-------|DE
So A will be perceived as it would run normally in the single child version. As B has finished by the time A is done, we can dump its whole progress buffer on stderr, such that it looks like it finished in no time. Once that is done, C is determined to be the visible child and its progress will be reported in real time.
So this way of output is really good for human consumption, as it only changes the timing, not the actual output.
For machine consumption the output needs to be prepared in the tasks, by either having a prefix per line or per block to indicate whose tasks output is displayed, because the output order may not follow the original sequential ordering:
|----A----| |--B--| |-C-|
will be scheduled to be all parallel:
process 1: |----A----| process 2: |--B--| process 3: |-C-| output: |----A----|CB
This happens because C finished before B did, so it will be queued for output before B.
To detect when a child has finished executing, we check interleaved with other actions (such as checking the liveliness of children or starting new processes) whether the stderr pipe still exists. Once a child closed its stderr stream, we assume it is terminating very soon, and use finish_command() from the single external process execution interface to collect the exit status.
By maintaining the strong assumption of stderr being open until the very end of a child process, we can avoid other hassle such as an implementation using waitpid(-1), which is not implemented in Windows.
--- run-command.c | 335 +++++++++++++++++++++++++++++++++++++++++++++++++ run-command.h | 80 ++++++++++++ t/t0061-run-command.sh | 53 ++++++++ test-run-command.c | 55 +++++++- 4 files changed, 522 insertions(+), 1 deletion(-)
run-command.c +335 -0
@@ -3,6 +3,8 @@#include "exec_cmd.h" #include "sigchain.h" #include "argv-array.h" +#include "thread-utils.h" +#include "strbuf.h" void child_process_init (struct child_process *child) {
@@ -865,3 +867,336 @@ int capture_command(struct child_process *cmd, struct strbuf *buf, size_t hint)close (cmd->out);return finish_command (cmd); } + +enum child_state { +GIT_CP_FREE , +GIT_CP_WORKING , +GIT_CP_WAIT_CLEANUP , +}; + +struct parallel_processes { +void *data; + +int max_processes; +int nr_processes; + +get_next_task_fn get_next_task; +start_failure_fn start_failure; +task_finished_fn task_finished; + +struct { +enum child_state state; +struct child_process process; +struct strbuf err; +void *data; + } *children; +/* +* The struct pollfd is logically part of *children, +* but the system call expects it as its own array. +*/ +struct pollfd *pfd; + +unsigned shutdown :1 ; + +int output_owner; +struct strbuf buffered_output;/* of finished children */ +}; + +static int default_start_failure (struct child_process *cp, +struct strbuf *err, +void *pp_cb, +void *pp_task_cb) +{ +int i; + +strbuf_addstr (err,"Starting a child failed:" ); +for (i =0 ; cp->argv[i]; i++) +strbuf_addf (err," %s" , cp->argv[i]); + +return 0 ; +} + +static int default_task_finished (int result, +struct child_process *cp, +struct strbuf *err, +void *pp_cb, +void *pp_task_cb) +{ +int i; + +if (!result) +return 0 ; + +strbuf_addf (err,"A child failed with return code %d:" , result); +for (i =0 ; cp->argv[i]; i++) +strbuf_addf (err," %s" , cp->argv[i]); + +return 0 ; +} + +static void kill_children (struct parallel_processes *pp,int signo) +{ +int i, n = pp->max_processes; + +for (i =0 ; i < n; i++) +if (pp->children[i].state ==GIT_CP_WORKING ) +kill (pp->children[i].process.pid, signo); +} + +static struct parallel_processes *pp_for_signal; + +static void handle_children_on_signal (int signo) +{ +kill_children (pp_for_signal, signo); +sigchain_pop (signo); +raise (signo); +} + +static void pp_init (struct parallel_processes *pp, +int n, +get_next_task_fn get_next_task, +start_failure_fn start_failure, +task_finished_fn task_finished, +void *data) +{ +int i; + +if (n <1 ) + n =online_cpus (); + + pp->max_processes = n; + +trace_printf ("run_processes_parallel: preparing to run up to %d tasks" , n); + + pp->data = data; +if (!get_next_task) +die ("BUG: you need to specify a get_next_task function" ); + pp->get_next_task = get_next_task; + + pp->start_failure = start_failure ? start_failure : default_start_failure; + pp->task_finished = task_finished ? task_finished : default_task_finished; + + pp->nr_processes =0 ; + pp->output_owner =0 ; + pp->shutdown =0 ; + pp->children =xcalloc (n,sizeof (*pp->children)); + pp->pfd =xcalloc (n,sizeof (*pp->pfd)); +strbuf_init (&pp->buffered_output,0 ); + +for (i =0 ; i < n; i++) { +strbuf_init (&pp->children[i].err,0 ); +child_process_init (&pp->children[i].process); + pp->pfd[i].events =POLLIN |POLLHUP ; + pp->pfd[i].fd =-1 ; + } + + pp_for_signal = pp; +sigchain_push_common (handle_children_on_signal); +} + +static void pp_cleanup (struct parallel_processes *pp) +{ +int i; + +trace_printf ("run_processes_parallel: done" ); +for (i =0 ; i < pp->max_processes; i++) { +strbuf_release (&pp->children[i].err); +child_process_clear (&pp->children[i].process); + } + +free (pp->children); +free (pp->pfd); + +/* +* When get_next_task added messages to the buffer in its last +* iteration, the buffered output is non empty. +*/ +fputs (pp->buffered_output.buf, stderr); +strbuf_release (&pp->buffered_output); + +sigchain_pop_common (); +} + +/* returns +* 0 if a new task was started. +* 1 if no new jobs was started (get_next_task ran out of work, non critical +* problem with starting a new command) +* <0 no new job was started, user wishes to shutdown early. Use negative code +* to signal the children. +*/ +static int pp_start_one (struct parallel_processes *pp) +{ +int i, code; + +for (i =0 ; i < pp->max_processes; i++) +if (pp->children[i].state ==GIT_CP_FREE ) +break ; +if (i == pp->max_processes) +die ("BUG: bookkeeping is hard" ); + + code = pp->get_next_task (&pp->children[i].process, + &pp->children[i].err, + pp->data, + &pp->children[i].data); +if (!code) { +strbuf_addbuf (&pp->buffered_output, &pp->children[i].err); +strbuf_reset (&pp->children[i].err); +return 1 ; + } + pp->children[i].process.err =-1 ; + pp->children[i].process.stdout_to_stderr =1 ; + pp->children[i].process.no_stdin =1 ; + +if (start_command (&pp->children[i].process)) { + code = pp->start_failure (&pp->children[i].process, + &pp->children[i].err, + pp->data, + &pp->children[i].data); +strbuf_addbuf (&pp->buffered_output, &pp->children[i].err); +strbuf_reset (&pp->children[i].err); +if (code) + pp->shutdown =1 ; +return code; + } + + pp->nr_processes++; + pp->children[i].state =GIT_CP_WORKING ; + pp->pfd[i].fd = pp->children[i].process.err; +return 0 ; +} + +static void pp_buffer_stderr (struct parallel_processes *pp,int output_timeout) +{ +int i; + +while ((i =poll (pp->pfd, pp->max_processes, output_timeout)) <0 ) { +if (errno ==EINTR ) +continue ; +pp_cleanup (pp); +die_errno ("poll" ); + } + +/* Buffer output from all pipes. */ +for (i =0 ; i < pp->max_processes; i++) { +if (pp->children[i].state ==GIT_CP_WORKING && + pp->pfd[i].revents & (POLLIN |POLLHUP )) { +int n =strbuf_read_once (&pp->children[i].err, + pp->children[i].process.err,0 ); +if (n ==0 ) { +close (pp->children[i].process.err); + pp->children[i].state =GIT_CP_WAIT_CLEANUP ; + }else if (n <0 ) +if (errno !=EAGAIN ) +die_errno ("read" ); + } + } +} + +static void pp_output (struct parallel_processes *pp) +{ +int i = pp->output_owner; +if (pp->children[i].state ==GIT_CP_WORKING && + pp->children[i].err.len) { +fputs (pp->children[i].err.buf, stderr); +strbuf_reset (&pp->children[i].err); + } +} + +static int pp_collect_finished (struct parallel_processes *pp) +{ +int i, code; +int n = pp->max_processes; +int result =0 ; + +while (pp->nr_processes >0 ) { +for (i =0 ; i < pp->max_processes; i++) +if (pp->children[i].state ==GIT_CP_WAIT_CLEANUP ) +break ; +if (i == pp->max_processes) +break ; + + code =finish_command (&pp->children[i].process); + + code = pp->task_finished (code, &pp->children[i].process, + &pp->children[i].err, pp->data, + &pp->children[i].data); + +if (code) + result = code; +if (code <0 ) +break ; + + pp->nr_processes--; + pp->children[i].state =GIT_CP_FREE ; + pp->pfd[i].fd =-1 ; +child_process_init (&pp->children[i].process); + +if (i != pp->output_owner) { +strbuf_addbuf (&pp->buffered_output, &pp->children[i].err); +strbuf_reset (&pp->children[i].err); + }else { +fputs (pp->children[i].err.buf, stderr); +strbuf_reset (&pp->children[i].err); + +/* Output all other finished child processes */ +fputs (pp->buffered_output.buf, stderr); +strbuf_reset (&pp->buffered_output); + +/* +* Pick next process to output live. +* NEEDSWORK: +* For now we pick it randomly by doing a round +* robin. Later we may want to pick the one with +* the most output or the longest or shortest +* running process time. +*/ +for (i =0 ; i < n; i++) +if (pp->children[(pp->output_owner + i) % n].state ==GIT_CP_WORKING ) +break ; + pp->output_owner = (pp->output_owner + i) % n; + } + } +return result; +} + +int run_processes_parallel (int n, +get_next_task_fn get_next_task, +start_failure_fn start_failure, +task_finished_fn task_finished, +void *pp_cb) +{ +int i, code; +int output_timeout =100 ; +int spawn_cap =4 ; +struct parallel_processes pp; + +pp_init (&pp, n, get_next_task, start_failure, task_finished, pp_cb); +while (1 ) { +for (i =0 ; + i < spawn_cap && !pp.shutdown && + pp.nr_processes < pp.max_processes; + i++) { + code =pp_start_one (&pp); +if (!code) +continue ; +if (code <0 ) { + pp.shutdown =1 ; +kill_children (&pp, -code); + } +break ; + } +if (!pp.nr_processes) +break ; +pp_buffer_stderr (&pp, output_timeout); +pp_output (&pp); + code =pp_collect_finished (&pp); +if (code) { + pp.shutdown =1 ; +if (code <0 ) +kill_children (&pp, -code); + } + } + +pp_cleanup (&pp); +return 0 ; +}
run-command.h +80 -0
84 lines not on this page · raw message
t/t0061-run-command.sh +53 -0
57 lines not on this page · raw message
test-run-command.c +54 -1
76 lines not on this page · raw message
Signature · 1 line
2.6.4.443.ge094245.dirty
PATCH v2 6/7 fetch_populated_submodules: use new parallel job processing
In reply to Stefan BellerIn a later patch we enable parallel processing of submodules, this only adds the possibility for it. So this change should not change any user facing behavior.
--- submodule.c | 142 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 98 insertions(+), 44 deletions(-)
submodule.c +98 -44
@@ -12,6 +12,7 @@#include "sha1-array.h" #include "argv-array.h" #include "blob.h" +#include "thread-utils.h" static int config_fetch_recurse_submodules =RECURSE_SUBMODULES_ON_DEMAND ;static struct string_list changed_submodule_paths;
@@ -610,37 +611,28 @@ static void calculate_changed_submodule_paths(void) initialized_fetch_ref_tips =0 ; } -int fetch_populated_submodules (const struct argv_array *options, -const char *prefix,int command_line_option, -int quiet) +struct submodule_parallel_fetch { +int count; +struct argv_array args; +const char *work_tree; +const char *prefix; +int command_line_option; +int quiet; +int result; +}; +#define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0} + +static int get_next_submodule (struct child_process *cp, +struct strbuf *err,void *data,void **task_cb) { -int i, result =0 ; -struct child_process cp =CHILD_PROCESS_INIT ; -struct argv_array argv =ARGV_ARRAY_INIT ; -const char *work_tree =get_git_work_tree (); -if (!work_tree) - goto out; - -if (read_cache () <0 ) -die ("index file corrupt" ); - -argv_array_push (&argv,"fetch" ); -for (i =0 ; i < options->argc; i++) -argv_array_push (&argv, options->argv[i]); -argv_array_push (&argv,"--recurse-submodules-default" ); -/* default value, "--submodule-prefix" and its value are added later */ - - cp.env = local_repo_env; - cp.git_cmd =1 ; - cp.no_stdin =1 ; - -calculate_changed_submodule_paths (); +int ret =0 ; +struct submodule_parallel_fetch *spf = data; -for (i =0 ; i < active_nr; i++) { +for ( ; spf->count < active_nr; spf->count++) {struct strbuf submodule_path =STRBUF_INIT ;struct strbuf submodule_git_dir =STRBUF_INIT ;struct strbuf submodule_prefix =STRBUF_INIT ; -const struct cache_entry *ce = active_cache[i]; +const struct cache_entry *ce = active_cache[spf->count];const char *git_dir, *default_argv;const struct submodule *submodule;
@@ -652,7 +644,7 @@ int fetch_populated_submodules(const struct argv_array *options, submodule =submodule_from_name (null_sha1, ce->name); default_argv ="yes" ; -if (command_line_option ==RECURSE_SUBMODULES_DEFAULT ) { +if (spf->command_line_option ==RECURSE_SUBMODULES_DEFAULT ) {if (submodule && submodule->fetch_recurse !=RECURSE_SUBMODULES_NONE ) {
@@ -675,40 +667,102 @@ int fetch_populated_submodules(const struct argv_array *options, default_argv ="on-demand" ; } } - }else if (command_line_option ==RECURSE_SUBMODULES_ON_DEMAND ) { + }else if (spf->command_line_option ==RECURSE_SUBMODULES_ON_DEMAND ) {if (!unsorted_string_list_lookup (&changed_submodule_paths, ce->name))continue ; default_argv ="on-demand" ; } -strbuf_addf (&submodule_path,"%s/%s" , work_tree, ce->name); +strbuf_addf (&submodule_path,"%s/%s" , spf->work_tree, ce->name);strbuf_addf (&submodule_git_dir,"%s/.git" , submodule_path.buf); -strbuf_addf (&submodule_prefix,"%s%s/" , prefix, ce->name); +strbuf_addf (&submodule_prefix,"%s%s/" , spf->prefix, ce->name); git_dir =read_gitfile (submodule_git_dir.buf);if (!git_dir) git_dir = submodule_git_dir.buf;if (is_directory (git_dir)) { -if (!quiet) -fprintf (stderr,"Fetching submodule %s%s\n" , prefix, ce->name); - cp.dir = submodule_path.buf; -argv_array_push (&argv, default_argv); -argv_array_push (&argv,"--submodule-prefix" ); -argv_array_push (&argv, submodule_prefix.buf); - cp.argv = argv.argv; -if (run_command (&cp)) - result =1 ; -argv_array_pop (&argv); -argv_array_pop (&argv); -argv_array_pop (&argv); +child_process_init (cp); + cp->dir =strbuf_detach (&submodule_path,NULL ); + cp->env = local_repo_env; + cp->git_cmd =1 ; +if (!spf->quiet) +strbuf_addf (err,"Fetching submodule %s%s\n" , + spf->prefix, ce->name); +argv_array_init (&cp->args); +argv_array_pushv (&cp->args, spf->args.argv); +argv_array_push (&cp->args, default_argv); +argv_array_push (&cp->args,"--submodule-prefix" ); +argv_array_push (&cp->args, submodule_prefix.buf); + ret =1 ; }strbuf_release (&submodule_path);strbuf_release (&submodule_git_dir);strbuf_release (&submodule_prefix); +if (ret) { + spf->count++; +return 1 ; + } } -argv_array_clear (&argv); +return 0 ; +} + +static int fetch_start_failure (struct child_process *cp, +struct strbuf *err, +void *cb,void *task_cb) +{ +struct submodule_parallel_fetch *spf = cb; + + spf->result =1 ; + +return 0 ; +} + +static int fetch_finish (int retvalue,struct child_process *cp, +struct strbuf *err,void *cb,void *task_cb) +{ +struct submodule_parallel_fetch *spf = cb; + +if (retvalue) + spf->result =1 ; + +return 0 ; +} + +int fetch_populated_submodules (const struct argv_array *options, +const char *prefix,int command_line_option, +int quiet) +{ +int i; +int max_parallel_jobs =1 ; +struct submodule_parallel_fetch spf =SPF_INIT ; + + spf.work_tree =get_git_work_tree (); + spf.command_line_option = command_line_option; + spf.quiet = quiet; + spf.prefix = prefix; + +if (!spf.work_tree) + goto out; + +if (read_cache () <0 ) +die ("index file corrupt" ); + +argv_array_push (&spf.args,"fetch" ); +for (i =0 ; i < options->argc; i++) +argv_array_push (&spf.args, options->argv[i]); +argv_array_push (&spf.args,"--recurse-submodules-default" ); +/* default value, "--submodule-prefix" and its value are added later */ + +calculate_changed_submodule_paths (); +run_processes_parallel (max_parallel_jobs, + get_next_submodule, + fetch_start_failure, + fetch_finish, + &spf); + +argv_array_clear (&spf.args); out:string_list_clear (&changed_submodule_paths,1 ); -return result; +return spf.result; }unsigned is_submodule_modified (const char *path,int ignore_untracked)
Signature · 1 line
2.6.4.443.ge094245.dirty
PATCH v2 7/7 submodules: allow parallel fetching, add tests and documentation
In reply to Stefan BellerThis enables the work of the previous patches.
--- Documentation/fetch-options.txt | 7 +++++++ builtin/fetch.c | 6 +++++- builtin/pull.c | 6 ++++++ submodule.c | 3 +-- submodule.h | 2 +- t/t5526-fetch-submodules.sh | 20 ++++++++++++++++++++ 6 files changed, 40 insertions(+), 4 deletions(-)
Documentation/fetch-options.txt +7 -0
@@ -100,6 +100,13 @@ ifndef::git-pull[] reference to a commit that isn't already in the local submodule clone. +-j:: +--jobs=<n>:: + Number of parallel children to be used for fetching submodules. + Each will fetch from different submodules, such that fetching many + submodules will be faster. By default submodules will be fetched + one at a time. + --no-recurse-submodules:: Disable recursive fetching of submodules (this has the same effect as using the '--recurse-submodules=no' option).
builtin/fetch.c +5 -1
@@ -37,6 +37,7 @@ static int prune = -1; /* unspecified */static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity;static int progress =-1 , recurse_submodules =RECURSE_SUBMODULES_DEFAULT ;static int tags =TAGS_DEFAULT , unshallow, update_shallow; +static int max_children =1 ;static const char *depth;static const char *upload_pack;static struct strbuf default_rla =STRBUF_INIT ;
@@ -99,6 +100,8 @@ static struct option builtin_fetch_options[] = { N_("fetch all tags and associated objects"), TAGS_SET),OPT_SET_INT ('n' ,NULL , &tags,N_ ("do not fetch all tags (--no-tags)" ),TAGS_UNSET ), +OPT_INTEGER ('j' ,"jobs" , &max_children, +N_ ("number of submodules fetched in parallel" )),OPT_BOOL ('p' ,"prune" , &prune,N_ ("prune remote-tracking branches no longer on remote" )), { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
@@ -1213,7 +1216,8 @@ int cmd_fetch(int argc, const char **argv, const char *prefix) result =fetch_populated_submodules (&options, submodule_prefix, recurse_submodules, - verbosity <0 ); + verbosity <0 , + max_children);argv_array_clear (&options); }
builtin/pull.c +6 -0
@@ -95,6 +95,7 @@ static int opt_force;static char *opt_tags;static char *opt_prune;static char *opt_recurse_submodules; +static char *max_children;static int opt_dry_run;static char *opt_keep;static char *opt_depth;
@@ -178,6 +179,9 @@ static struct option pull_options[] = { N_("on-demand"), N_("control recursive fetching of submodules"), PARSE_OPT_OPTARG), +OPT_PASSTHRU ('j' ,"jobs" , &max_children,N_ ("n" ), +N_ ("number of submodules pulled in parallel" ), +PARSE_OPT_OPTARG ),OPT_BOOL (0 ,"dry-run" , &opt_dry_run,N_ ("dry run" )), OPT_PASSTHRU('k', "keep", &opt_keep, NULL,
@@ -525,6 +529,8 @@ static int run_fetch(const char *repo, const char **refspecs)argv_array_push (&args, opt_prune);if (opt_recurse_submodules)argv_array_push (&args, opt_recurse_submodules); +if (max_children) +argv_array_push (&args, max_children);if (opt_dry_run)argv_array_push (&args,"--dry-run" );if (opt_keep)
submodule.c +1 -2
@@ -729,10 +729,9 @@ static int fetch_finish(int retvalue, struct child_process *cp,int fetch_populated_submodules (const struct argv_array *options,const char *prefix,int command_line_option, -int quiet) +int quiet,int max_parallel_jobs) {int i; -int max_parallel_jobs =1 ;struct submodule_parallel_fetch spf =SPF_INIT ; spf.work_tree =get_git_work_tree ();
submodule.h +1 -1
@@ -31,7 +31,7 @@ void set_config_fetch_recurse_submodules(int value);void check_for_new_submodule_commits (unsigned char new_sha1[20 ]);int fetch_populated_submodules (const struct argv_array *options,const char *prefix,int command_line_option, -int quiet); +int quiet,int max_parallel_jobs);unsigned is_submodule_modified (const char *path,int ignore_untracked);int submodule_uses_gitfile (const char *path);int ok_to_remove_submodule (const char *path);
t/t5526-fetch-submodules.sh +20 -0
@@ -71,6 +71,17 @@ test_expect_success "fetch --recurse-submodules recurses into submodules" ' test_i18ncmp expect.err actual.err ' +test_expect_success "fetch --recurse-submodules -j2 has the same output behaviour" ' + add_upstream_commit && + ( + cd downstream && + GIT_TRACE=$(pwd)/../trace.out git fetch --recurse-submodules -j2 2>../actual.err + ) && + test_must_be_empty actual.out && + test_i18ncmp expect.err actual.err && + grep "2 tasks" trace.out +' + test_expect_success "fetch alone only fetches superproject" ' add_upstream_commit && (
@@ -140,6 +151,15 @@ test_expect_success "--quiet propagates to submodules" ' ! test -s actual.err ' +test_expect_success "--quiet propagates to parallel submodules" ' + ( + cd downstream && + git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err + ) && + ! test -s actual.out && + ! test -s actual.err +' + test_expect_success "--dry-run propagates to submodules" ' add_upstream_commit && (
Signature · 1 line
2.6.4.443.ge094245.dirty
v2 cover Rerolling sb/submodule-parallel-fetch for the time after 2.7
In reply to Stefan Bellerwith the interdiff below:
git-compat-util.h +0 -1
@@ -723,7 +723,6 @@ extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_extern void *xmmap_gently (void *start,size_t length,int prot,int flags,int fd,off_t offset);extern int xopen (const char *path,int flags, ...);extern ssize_t xread (int fd,void *buf,size_t len); -extern ssize_t xread_nonblock (int fd,void *buf,size_t len);extern ssize_t xwrite (int fd,const void *buf,size_t len);extern ssize_t xpread (int fd,void *buf,size_t len,off_t offset);extern int xdup (int fd);
strbuf.c +1 -1
@@ -389,7 +389,7 @@ ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)ssize_t cnt;strbuf_grow (sb, hint ? hint :8192 ); - cnt =xread_nonblock (fd, sb->buf + sb->len, sb->alloc - sb->len -1 ); + cnt =xread (fd, sb->buf + sb->len, sb->alloc - sb->len -1 );if (cnt >0 )strbuf_setlen (sb, sb->len + cnt);return cnt;
strbuf.h +4 -4
@@ -367,10 +367,10 @@ extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);extern ssize_t strbuf_read (struct strbuf *,int fd,size_t hint);/** -* Returns the number of new bytes appended to the sb. -* Negative return value signals there was an error returned from -* underlying read(2), in which case the caller should check errno. -* e.g. errno == EAGAIN when the read may have blocked. +* Read the contents of a given file descriptor partially by using only one +* attempt of xread. The third argument can be used to give a hint about the +* file size, to avoid reallocs. Returns the number of new bytes appended to +* the sb. */ extern ssize_t strbuf_read_once (struct strbuf *,int fd,size_t hint);
wrapper.c +8 -23
@@ -243,7 +243,14 @@ ssize_t xread(int fd, void *buf, size_t len)struct pollfd pfd; pfd.events =POLLIN ; pfd.fd = fd; -/* We deliberately ignore the return value */ +/* +* it is OK if this poll() failed; we +* want to leave this infinite loop +* only when read() returns with +* success, or an expected failure, +* which would be checked by the next +* call to read(2). +*/ poll (&pfd,1 ,-1 ); } }
@@ -252,28 +259,6 @@ ssize_t xread(int fd, void *buf, size_t len) }/* -* xread_nonblock() is the same a read(), but it automatically restarts read() -* interrupted operations (EINTR). xread_nonblock() DOES NOT GUARANTEE that -* "len" bytes is read. EWOULDBLOCK is turned into EAGAIN. -*/ -ssize_t xread_nonblock (int fd,void *buf,size_t len) -{ -ssize_t nr; -if (len >MAX_IO_SIZE ) - len =MAX_IO_SIZE ; -while (1 ) { - nr =read (fd, buf, len); -if (nr <0 ) { -if (errno ==EINTR ) -continue ; -if (errno ==EWOULDBLOCK ) - errno =EAGAIN ; - } -return nr; - } -} - -/* * xwrite() is the same a write(), but it automatically restarts write() * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT * GUARANTEE that "len" bytes is written even if the operation is successful.
Quoted 6 lines · Stefan Beller
I am sending out a new version for replacing sb/submodule-parallel-fetch for the time after the 2.7 release.
* Dropped the patch, which introduces
xread_nonblock
*strbuf_read_onceuses xread now. This is safe as we poll before usingstrbuf_read_once, so we know we won't stall.
"That is only true for the current callers" was my first reaction, but it is safe and sensible even for future callers. If they have something better to do than getting stuck before reading some, they ought to be checking for POLLIN before calling this, and if not, they do not have to be forced to spin for themselves until this returns some new payload.
I like that.
Quoted 2 lines
* have the commit message reworded for "run-command: add an asynchronous parallel child processor" with Johannes' suggestion.
Thanks.
Re: [PATCHv2 2/7] xread: poll on non blocking fds
In reply to Stefan BellerQuoted 5 lines · Stefan Beller
If we get an EAGAIN or EWOULDBLOCK the fd must have set
O_NONBLOCK. As the intent of xread is to read as much as possible either until the fd is EOF or an actual error occurs, we can ease the feeder of the fd by not spinning the whole time, but rather wait for it politely by not busy waiting.
I'm not sure this second sentence is true. The point of xread() is to do a single read that makes forward progress (or returns a real error), not to read as much as we can.
The rationale seems more to me like:
The point of xread() is to perform read() until we either get some data, or encounter a "real" error. We do not count EINTR or EAGAIN as "real" errors, as a reader trying to make forward progress would generally just repeat the read, so we loop for them as a convenience.
In the case of EINTR, trying the read() again immediately is fine; a signal interrupted us, but there is no reason to think the read() would not succeed if we tried it again.
For EAGAIN, however, we know that the fd must have set O_NONBLOCK. In this case reading again immediately is not likely to produce results, and we will chew up CPU spinning on read() calls. Instead, let's insert a poll() to block until we actually get data.
I also don't know that this patch necessarily needs to be part of the parallel-fetch series anymore. We are not setting O_NONBLOCK ourselves, and I don't think the correctness of the new strbuf_read_once() is impacted by this. I.e., I think this optimization applies equally well to the hundreds of existing xread() calls, should they unexpectedly be fed an O_NONBLOCK descriptor.
That said, I do not mind it here, but I think it could be its own separate topic if you wanted to reduce the complexity of the parallel-fetch topic.
Quoted 2 lines
We should not care if the call to poll failed, as we're in an infinite loop and can only get out with the correct
read().
You might want to expand on "correct" here. I think you mean "can only get out with a read() that returns data or a "real" error" (I am reusing the terminology I introduced above; I am fine with other terminology, depending on how you word the rest of it).
-Peff
Re: [PATCHv2 2/7] xread: poll on non blocking fds
In reply to Stefan BellerQuoted 13 lines · Stefan Beller
The man page of read(2) says:
EAGAIN The file descriptor fd refers to a file other than a socket
and has been marked nonblocking (O_NONBLOCK), and the read would block.EAGAIN or EWOULDBLOCK
The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.
Quoted 8 lines
If we get an EAGAIN or EWOULDBLOCK the fd must have set
O_NONBLOCK. As the intent of xread is to read as much as possible either until the fd is EOF or an actual error occurs, we can ease the feeder of the fd by not spinning the whole time, but rather wait for it politely by not busy waiting.We should not care if the call to poll failed, as we're in an infinite loop and can only get out with the correct
read().
I'm not sure if this is valid under all circumstances:
This is what "man poll" says under Linux:
[]
ENOMEM There was no space to allocate file descriptor tables.
[]
And this is from Mac OS, ("BSD System Calls Manual") ERRORS
Poll() will fail if:
[EAGAIN] Allocation of internal data structures fails. A sub-
sequent request may succeed.
And this is opengroup:
http://pubs.opengroup.org/onlinepubs/9699919799//functions/poll.html:
[EAGAIN]
The allocation of internal data structures failed but a subsequent request may succeed.
read() may return EAGAIN, but poll() may fail to allocate memory, and fail. Is it always guaranteed that the loop is terminated?
Quoted 40 lines
Signed-off-by Stefan Beller <[email protected]>Acked-by Johannes Sixt <[email protected]>Signed-off-by Junio C Hamano <[email protected]>--- wrapper.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)wrapper.c +18
-2@@ -236,8 +236,24 @@ ssize_t xread(int fd, void *buf, size_t len) len =MAX_IO_SIZE ;while (1 ) { nr =read (fd, buf, len); -if ((nr <0 ) && (errno ==EAGAIN || errno ==EINTR )) -continue ; +if (nr <0 ) { +if (errno ==EINTR ) +continue ; +if (errno ==EAGAIN || errno ==EWOULDBLOCK ) { +struct pollfd pfd; + pfd.events =POLLIN ; + pfd.fd = fd; +/* +* it is OK if this poll() failed; we +* want to leave this infinite loop +* only when read() returns with +* success, or an expected failure, +* which would be checked by the next +* call to read(2). +*/ +poll (&pfd,1 ,-1 ); + } + }return nr; } }
Re: [PATCHv2 2/7] xread: poll on non blocking fds
Quoted 38 lines · Torsten Bögershausen
Quoted 13 lines · Stefan Beller
The man page of read(2) says:
EAGAIN The file descriptor fd refers to a file other than a socket
and has been marked nonblocking (O_NONBLOCK), and the read would block.EAGAIN or EWOULDBLOCK
The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.Quoted 8 lines
If we get an EAGAIN or EWOULDBLOCK the fd must have set
O_NONBLOCK. As the intent of xread is to read as much as possible either until the fd is EOF or an actual error occurs, we can ease the feeder of the fd by not spinning the whole time, but rather wait for it politely by not busy waiting.We should not care if the call to poll failed, as we're in an infinite loop and can only get out with the correct
read().I'm not sure if this is valid under all circumstances:
This is what "man poll" says under Linux:
[]
ENOMEM There was no space to allocate file descriptor tables.
[]
And this is from Mac OS, ("BSD System Calls Manual") ERRORS
Poll()will fail if:[EAGAIN] Allocation of internal data structures fails. A sub-
sequent request may succeed.
And this is opengroup:
http://pubs.opengroup.org/onlinepubs/9699919799//functions/poll.html:
[EAGAIN]
The allocation of internal data structures failed but a subsequent request may succeed.
Quoted 2 lines
read()may return EAGAIN, butpoll()may fail to allocate memory, and fail. Is it always guaranteed that the loop is terminated?
In case poll fails (assume a no op for it), the logic should not have changed by this patch?
Looking closely:
Quoted 12 lines
Quoted 12 lines
while (1) { nr = read(fd, buf, len);wrapper.c +8
-2-if ((nr <0 ) && (errno ==EAGAIN || errno ==EINTR )) -continue ; +if (nr <0 ) { +if (errno ==EINTR ) +continue ; +if (errno ==EAGAIN || errno ==EWOULDBLOCK ) { +struct pollfd pfd; + pfd.events =POLLIN ; + pfd.fd = fd; +/*
Quoted 8 lines
wrapper.c ssize_t xread(int fd, void *buf, size_t len) · line 254
Quoted 8 lines
wrapper.c +8
-0+* it is OK if this poll() failed; we +* want to leave this infinite loop +* only when read() returns with +* success, or an expected failure, +* which would be checked by the next +* call to read(2). +*/ +poll (&pfd,1 ,-1 );
Or do you mean to insert another continue in here?
Quoted 7 lines
Quoted 6 lines
wrapper.c +2
-0+ } + }return nr; } }
Re: [PATCHv2 2/7] xread: poll on non blocking fds
Quoted 68 lines · Stefan Beller
Quoted 38 lines · Torsten Bögershausen
Quoted 13 lines · Stefan Beller
The man page of read(2) says:
EAGAIN The file descriptor fd refers to a file other than a socket
and has been marked nonblocking (O_NONBLOCK), and the read would block.EAGAIN or EWOULDBLOCK
The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.Quoted 8 lines
If we get an EAGAIN or EWOULDBLOCK the fd must have set
O_NONBLOCK. As the intent of xread is to read as much as possible either until the fd is EOF or an actual error occurs, we can ease the feeder of the fd by not spinning the whole time, but rather wait for it politely by not busy waiting.We should not care if the call to poll failed, as we're in an infinite loop and can only get out with the correct
read().I'm not sure if this is valid under all circumstances:
This is what "man poll" says under Linux:
[]
ENOMEM There was no space to allocate file descriptor tables.
[]
And this is from Mac OS, ("BSD System Calls Manual") ERRORS
Poll()will fail if:[EAGAIN] Allocation of internal data structures fails. A sub-
sequent request may succeed.
And this is opengroup:
http://pubs.opengroup.org/onlinepubs/9699919799//functions/poll.html:
[EAGAIN]
The allocation of internal data structures failed but a subsequent request may succeed.Quoted 2 lines
read()may return EAGAIN, butpoll()may fail to allocate memory, and fail. Is it always guaranteed that the loop is terminated?In case poll fails (assume a no op for it), the logic should not have changed by this patch?
Looking closely:
Quoted 12 lines
Quoted 12 lines
while (1) { nr = read(fd, buf, len);wrapper.c +8
-2-if ((nr <0 ) && (errno ==EAGAIN || errno ==EINTR )) -continue ; +if (nr <0 ) { +if (errno ==EINTR ) +continue ; +if (errno ==EAGAIN || errno ==EWOULDBLOCK ) { +struct pollfd pfd; + pfd.events =POLLIN ; + pfd.fd = fd; +/* Quoted 8 lines
wrapper.c ssize_t xread(int fd, void *buf, size_t len) · line 254
Quoted 8 lines
wrapper.c +8
-0+* it is OK if this poll() failed; we +* want to leave this infinite loop +* only when read() returns with +* success, or an expected failure, +* which would be checked by the next +* call to read(2). +*/ +poll (&pfd,1 ,-1 );
Quoted 1 line
Or do you mean to insert another continue in here?
I was thinking that we run into similar loop as before:read() returns -1; errno = EAGAIN /* No data to read */poll() returns -1; errno = EAGAIN /* poll failed. If the fd was OK, the failure may be temporaly,
as much as poll() can see.
But most probably we run out ouf memory */
So the code would look like this:
if (!poll(&pfd, 1, -1))
return -1;
Quoted 14 lines
Quoted 9 lines
wrapper.c ssize_t xread(int fd, void *buf, size_t len) · line 259
Quoted 8 lines
wrapper.c +2
-0+ } + }return nr; } }--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCHv2 2/7] xread: poll on non blocking fds
Quoted 11 lines · Torsten Bögershausen
Quoted 1 line
Or do you mean to insert another continue in here?
I was thinking that we run into similar loop as before:
read()returns -1; errno = EAGAIN /* No data to read */poll()returns -1; errno = EAGAIN /* poll failed. If the fd was OK, the failure may be temporaly,
as much aspoll()can see.
But most probably we run out ouf memory */So the code would look like this:
if (!poll(&pfd, 1, -1)) return -1;
That changes the semantics of the function. The poll() is just a convenience to avoid spinning. If it fails, with Stefan's patch[1] the worst case is that we would spin on read() and poll(), instead of actually blocking in the poll().
But if we return on poll() failure, now the caller will see errors from poll() even though they don't know or care that we called poll() in the first place. Consider what would happen with your code if read got EAGAIN and then poll got EINTR. We would report an error, even though the whole point of xread() is to loop on these conditions.
-Peff
[1] Stefan's patch does have a bug. It forgets to "continue" after calling poll, which means we will block with poll and _then_ exit with -1, instead of restarting the loop.
Re: [PATCHv2 2/7] xread: poll on non blocking fds
In reply to Torsten BögershausenQuoted 69 lines · Torsten Bögershausen
Quoted 68 lines · Stefan Beller
Quoted 38 lines · Torsten Bögershausen
Quoted 13 lines · Stefan Beller
The man page of read(2) says:
EAGAIN The file descriptor fd refers to a file other than a socket
and has been marked nonblocking (O_NONBLOCK), and the read would block.EAGAIN or EWOULDBLOCK
The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.Quoted 8 lines
If we get an EAGAIN or EWOULDBLOCK the fd must have set
O_NONBLOCK. As the intent of xread is to read as much as possible either until the fd is EOF or an actual error occurs, we can ease the feeder of the fd by not spinning the whole time, but rather wait for it politely by not busy waiting.We should not care if the call to poll failed, as we're in an infinite loop and can only get out with the correct
read().I'm not sure if this is valid under all circumstances:
This is what "man poll" says under Linux:
[]
ENOMEM There was no space to allocate file descriptor tables.
[]
And this is from Mac OS, ("BSD System Calls Manual") ERRORS
Poll()will fail if:[EAGAIN] Allocation of internal data structures fails. A sub-
sequent request may succeed.
And this is opengroup:
http://pubs.opengroup.org/onlinepubs/9699919799//functions/poll.html:
[EAGAIN]
The allocation of internal data structures failed but a subsequent request may succeed.Quoted 2 lines
read()may return EAGAIN, butpoll()may fail to allocate memory, and fail. Is it always guaranteed that the loop is terminated?In case poll fails (assume a no op for it), the logic should not have changed by this patch?
Looking closely:
Quoted 20 lines
Quoted 20 lines
while (1) { nr = read(fd, buf, len);wrapper.c +16
-2-if ((nr <0 ) && (errno ==EAGAIN || errno ==EINTR )) -continue ; +if (nr <0 ) { +if (errno ==EINTR ) +continue ; +if (errno ==EAGAIN || errno ==EWOULDBLOCK ) { +struct pollfd pfd; + pfd.events =POLLIN ; + pfd.fd = fd; +/* +* it is OK if this poll() failed; we +* want to leave this infinite loop +* only when read() returns with +* success, or an expected failure, +* which would be checked by the next +* call to read(2). +*/ +poll (&pfd,1 ,-1 );
Quoted 3 lines
Quoted 1 line
Or do you mean to insert another continue in here?
I was thinking that we run into similar loop as before:
read()returns -1; errno = EAGAIN /* No data to read */
which is expected for non blocking fds,
Quoted 3 lines
poll()returns -1; errno = EAGAIN /* poll failed. If the fd was OK, the failure may be temporaly,
as much aspoll()can see.
But most probably we run out ouf memory */
Before this patch we would not have asked poll, but had just a continue here, so I think we need to have it here again no matter of the return code of the poll.
If poll determines it is low on memory, this should not make this function fail, we can still do as good as we did before by just asking read repeatedly again, though?
So I'd be convinced now we'd want to have:
poll(&pfd, 1, -1); /* this is only buying time
for the fd to deliver data, in case it fails we don't care but just fall back to old behavior before this patch with busy spinning*/
continue;
Quoted 22 lines
So the code would look like this:
if (!poll(&pfd, 1, -1)) return -1;Quoted 14 lines
Quoted 9 lines
wrapper.c ssize_t xread(int fd, void *buf, size_t len) · line 259
Quoted 8 lines
wrapper.c +2
-0+ } + }return nr; } }--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCHv2 2/7] xread: poll on non blocking fds
Quoted 12 lines · Stefan Beller
Quoted 3 lines
poll()returns -1; errno = EAGAIN /* poll failed. If the fd was OK, the failure may be temporaly,
as much aspoll()can see.
But most probably we run out ouf memory */Before this patch we would not have asked poll, but had just a continue here, so I think we need to have it here again no matter of the return code of the poll.
If poll determines it is low on memory, this should not make this function fail, we can still do as good as we did before by just asking read repeatedly again, though?
Quoted 7 lines
So I'd be convinced now we'd want to have:
poll(&pfd, 1, -1); /* this is only buying time
for the fd to deliver data, in case it fails we don't care but just fall back to old behavior before this patch with busy spinning*/
continue;
Right. I think that is the only sensible thing, and your comment explains perfectly what is going on.
-Peff
Re: [PATCHv2 2/7] xread: poll on non blocking fds
In reply to Jeff KingQuoted 25 lines · Jeff King
Quoted 11 lines · Torsten Bögershausen
Quoted 1 line
Or do you mean to insert another continue in here?
I was thinking that we run into similar loop as before:
read()returns -1; errno = EAGAIN /* No data to read */poll()returns -1; errno = EAGAIN /* poll failed. If the fd was OK, the failure may be temporaly,
as much aspoll()can see.
But most probably we run out ouf memory */So the code would look like this:
if (!poll(&pfd, 1, -1)) return -1;That changes the semantics of the function. The
poll()is just a convenience to avoid spinning. If it fails, with Stefan's patch[1] the worst case is that we would spin onread()andpoll(), instead of actually blocking in thepoll().But if we return on
poll()failure, now the caller will see errors frompoll()even though they don't know or care that we calledpoll()in the first place. Consider what would happen with your code if read got EAGAIN and then poll got EINTR. We would report an error, even though the whole point ofxread()is to loop on these conditions.
Quoted 6 lines
-Peff
[1] Stefan's patch does have a bug. It forgets to "continue" after calling poll, which means we will block with poll and _then_ exit with -1, instead of restarting the loop.
--
I love this group: Curing one bug with another doesn't work :-)
/* So the code v2 would look like this: */
if (!poll(&pfd, 1, -1)) {
if (errno == EINTR)
continue;
return -1; /* poll() failed, this is serious. */
}Re: [PATCHv2 2/7] xread: poll on non blocking fds
Quoted 17 lines · Torsten Bögershausen
Quoted 15 lines
Quoted 4 lines
So the code would look like this:
if (!poll(&pfd, 1, -1)) return -1;That changes the semantics of the function. The
poll()is just a convenience to avoid spinning. If it fails, with Stefan's patch[1] the worst case is that we would spin onread()andpoll(), instead of actually blocking in thepoll().But if we return on
poll()failure, now the caller will see errors frompoll()even though they don't know or care that we calledpoll()in the first place. Consider what would happen with your code if read got EAGAIN and then poll got EINTR. We would report an error, even though the whole point ofxread()is to loop on these conditions.[...]
Quoted 7 lines
/* So the code v2 would look like this: */
if (!poll(&pfd, 1, -1)) { if (errno == EINTR) continue; return -1; /* poll() failed, this is serious. */ }
That solves the EINTR problem, but I still don't see why we want to return -1. The caller asked us to read(). We know that read() did not fail with an actual error. Yet we are going to return an error to the user, with errno set to something related only to poll(). I think we are better off to keep the same semantics from the caller's point of view:
we loop until read() returns forward progress or a real error, and anything else we do is a behind-the-scenes optimization.
BTW, I am assuming you mean:
if (poll(&pfd, 1, -1) < 0)
...
in your examples. Returning "0" means that poll timed out, but of course we are not providing a timeout.
-Peff