From c321a1d02755a77ae790d6e8976b2ff15edba89d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 14 Oct 2013 21:33:07 +1030 Subject: [PATCH] ccan/io: flatten debug callchain further. Don't call through io_loop, but have it pass the connection back and call manually. Signed-off-by: Rusty Russell --- ccan/io/backend.h | 12 ++++- ccan/io/io.c | 90 ++++++++++++++++++++++++------- ccan/io/io_plan.h | 13 +++++ ccan/io/poll.c | 39 +++++++++++--- ccan/io/test/run-17-homemade-io.c | 8 +-- 5 files changed, 130 insertions(+), 32 deletions(-) diff --git a/ccan/io/backend.h b/ccan/io/backend.h index df168d9f..30a338f7 100644 --- a/ccan/io/backend.h +++ b/ccan/io/backend.h @@ -53,14 +53,22 @@ static inline void set_current(struct io_conn *conn) { current = conn; } +static inline bool doing_debug_on(struct io_conn *conn) +{ + return io_debug_conn && io_debug_conn(conn); +} static inline bool doing_debug(void) { - return io_debug_conn != NULL; + return io_debug_conn; } #else static inline void set_current(struct io_conn *conn) { } +static inline bool doing_debug_on(struct io_conn *conn) +{ + return false; +} static inline bool doing_debug(void) { return false; @@ -74,6 +82,8 @@ void del_listener(struct io_listener *l); void backend_plan_changed(struct io_conn *conn); void backend_add_timeout(struct io_conn *conn, struct timespec ts); void backend_del_timeout(struct io_conn *conn); +void backend_del_conn(struct io_conn *conn); void io_ready(struct io_conn *conn); +void *do_io_loop(struct io_conn **ready); #endif /* CCAN_IO_BACKEND_H */ diff --git a/ccan/io/io.c b/ccan/io/io.c index a4489ee8..ddcd502f 100644 --- a/ccan/io/io.c +++ b/ccan/io/io.c @@ -24,24 +24,78 @@ bool io_debug_wakeup; struct io_plan io_debug(struct io_plan plan) { + struct io_conn *ready = NULL; + if (io_plan_nodebug) { io_plan_nodebug = false; return plan; } - if (!io_debug_conn || !current) - return plan; - - if (!io_debug_conn(current) && !io_debug_wakeup) - return plan; + if (!current || !doing_debug_on(current)) { + if (!io_debug_wakeup) + return plan; + } io_debug_wakeup = false; current->plan = plan; backend_plan_changed(current); /* Call back into the loop immediately. */ - io_loop_return = io_loop(); - return plan; + io_loop_return = do_io_loop(&ready); + + if (ready) { + set_current(ready); + if (!ready->plan.next) { + /* Call finish function immediately. */ + if (ready->finish) { + errno = ready->plan.u.close.saved_errno; + ready->finish(ready, ready->finish_arg); + ready->finish = NULL; + } + backend_del_conn(ready); + } else { + /* Calls back in itself, via io_debug_io(). */ + if (ready->plan.io(ready->fd.fd, &ready->plan) != 2) + abort(); + } + set_current(NULL); + } + + /* Return a do-nothing plan, so backend_plan_changed in + * io_ready doesn't do anything (it's already been called). */ + return io_idle_(); +} + +int io_debug_io(int ret) +{ + /* Cache it for debugging; current changes. */ + struct io_conn *conn = current; + int saved_errno = errno; + + if (!doing_debug_on(conn)) + return ret; + + /* These will all go linearly through the io_debug() path above. */ + switch (ret) { + case -1: + /* This will call io_debug above. */ + errno = saved_errno; + io_close(); + break; + case 0: /* Keep going with plan. */ + io_debug(conn->plan); + break; + case 1: /* Done: get next plan. */ + if (timeout_active(conn)) + backend_del_timeout(conn); + conn->plan.next(conn, conn->plan.next_arg); + break; + default: + abort(); + } + + /* Normally-invalid value, used for sanity check. */ + return 2; } static void debug_io_wake(struct io_conn *conn) @@ -173,11 +227,11 @@ static int do_write(int fd, struct io_plan *plan) { ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len); if (ret < 0) - return -1; + return io_debug_io(-1); plan->u.write.buf += ret; plan->u.write.len -= ret; - return (plan->u.write.len == 0); + return io_debug_io(plan->u.write.len == 0); } /* Queue some data to be written. */ @@ -202,11 +256,11 @@ static int do_read(int fd, struct io_plan *plan) { ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len); if (ret <= 0) - return -1; + return io_debug_io(-1); plan->u.read.buf += ret; plan->u.read.len -= ret; - return (plan->u.read.len == 0); + return io_debug_io(plan->u.read.len == 0); } /* Queue a request to read into a buffer. */ @@ -231,10 +285,10 @@ static int do_read_partial(int fd, struct io_plan *plan) { ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp); if (ret <= 0) - return -1; + return io_debug_io(-1); *plan->u.readpart.lenp = ret; - return 1; + return io_debug_io(1); } /* Queue a partial request to read into a buffer. */ @@ -259,10 +313,10 @@ static int do_write_partial(int fd, struct io_plan *plan) { ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp); if (ret < 0) - return -1; + return io_debug_io(-1); *plan->u.writepart.lenp = ret; - return 1; + return io_debug_io(1); } /* Queue a partial write request. */ @@ -313,23 +367,21 @@ void io_wake_(struct io_conn *conn, struct io_plan plan) void io_ready(struct io_conn *conn) { + set_current(conn); switch (conn->plan.io(conn->fd.fd, &conn->plan)) { case -1: /* Failure means a new plan: close up. */ - set_current(conn); conn->plan = io_close(); backend_plan_changed(conn); - set_current(NULL); break; case 0: /* Keep going with plan. */ break; case 1: /* Done: get next plan. */ - set_current(conn); if (timeout_active(conn)) backend_del_timeout(conn); conn->plan = conn->plan.next(conn, conn->plan.next_arg); backend_plan_changed(conn); - set_current(NULL); } + set_current(NULL); } /* Close the connection, we're done. */ diff --git a/ccan/io/io_plan.h b/ccan/io/io_plan.h index 86be1d70..b855e717 100644 --- a/ccan/io/io_plan.h +++ b/ccan/io/io_plan.h @@ -80,6 +80,15 @@ extern bool (*io_debug_conn)(struct io_conn *conn); */ struct io_plan io_debug(struct io_plan plan); +/** + * io_debug_io - return from function which actually does I/O. + * + * This determines if we are debugging the current connection: if so, + * it immediately sets the next function and calls into io_loop() to + * create a linear call chain. + */ +int io_debug_io(int ret); + /** * io_plan_no_debug - mark the next plan not to be called immediately. * @@ -99,6 +108,10 @@ static inline struct io_plan io_debug(struct io_plan plan) { return plan; } +static inline int io_debug_io(int ret) +{ + return ret; +} #define io_plan_no_debug() (void)0 #endif diff --git a/ccan/io/poll.c b/ccan/io/poll.c index 3ed6321b..31a56600 100644 --- a/ccan/io/poll.c +++ b/ccan/io/poll.c @@ -172,7 +172,7 @@ bool add_duplex(struct io_conn *c) return true; } -static void del_conn(struct io_conn *conn) +void backend_del_conn(struct io_conn *conn) { if (conn->finish) { errno = conn->plan.u.close.saved_errno; @@ -189,6 +189,7 @@ static void del_conn(struct io_conn *conn) } else del_fd(&conn->fd); num_closing--; + free_conn(conn); } void del_listener(struct io_listener *l) @@ -213,7 +214,7 @@ static void accept_conn(struct io_listener *l) } /* It's OK to miss some, as long as we make progress. */ -static void finish_conns(void) +static bool finish_conns(struct io_conn **ready) { unsigned int i; @@ -228,12 +229,16 @@ static void finish_conns(void) c = (void *)fds[i]; for (duplex = c->duplex; c; c = duplex, duplex = NULL) { if (!c->plan.next) { - del_conn(c); - free_conn(c); + if (doing_debug_on(c) && ready) { + *ready = c; + return true; + } + backend_del_conn(c); i--; } } } + return false; } void backend_add_timeout(struct io_conn *conn, struct timespec duration) @@ -253,7 +258,7 @@ void backend_del_timeout(struct io_conn *conn) } /* This is the main loop. */ -void *io_loop(void) +void *do_io_loop(struct io_conn **ready) { void *ret; @@ -291,7 +296,9 @@ void *io_loop(void) } if (num_closing) { - finish_conns(); + /* If this finishes a debugging con, return now. */ + if (finish_conns(ready)) + return NULL; /* Could have started/finished more. */ continue; } @@ -327,6 +334,11 @@ void *io_loop(void) if (c->duplex) { int mask = c->duplex->plan.pollflag; if (events & mask) { + if (doing_debug_on(c->duplex) + && ready) { + *ready = c->duplex; + return NULL; + } io_ready(c->duplex); events &= ~mask; /* debug can recurse; @@ -337,6 +349,10 @@ void *io_loop(void) continue; } } + if (doing_debug_on(c) && ready) { + *ready = c; + return NULL; + } io_ready(c); /* debug can recurse; anything can change. */ if (doing_debug()) @@ -354,8 +370,10 @@ void *io_loop(void) } } - while (num_closing) - finish_conns(); + while (num_closing && !io_loop_return) { + if (finish_conns(ready)) + return NULL; + } ret = io_loop_return; io_loop_return = NULL; @@ -363,3 +381,8 @@ void *io_loop(void) io_loop_exit(); return ret; } + +void *io_loop(void) +{ + return do_io_loop(NULL); +} diff --git a/ccan/io/test/run-17-homemade-io.c b/ccan/io/test/run-17-homemade-io.c index 00fbf223..b827713a 100644 --- a/ccan/io/test/run-17-homemade-io.c +++ b/ccan/io/test/run-17-homemade-io.c @@ -41,7 +41,7 @@ static int do_read_packet(int fd, struct io_plan *plan) ok1(pkt->state == 2); pkt->state++; if (pkt->len == 0) - return 1; + return io_debug_io(1); if (!pkt->contents && !(pkt->contents = malloc(pkt->len))) goto fail; else { @@ -58,12 +58,12 @@ static int do_read_packet(int fd, struct io_plan *plan) plan->u.ptr_len.len += ret; /* Finished? */ - return (plan->u.ptr_len.len >= sizeof(pkt->len) - && plan->u.ptr_len.len == pkt->len + sizeof(pkt->len)); + return io_debug_io(plan->u.ptr_len.len >= sizeof(pkt->len) + && plan->u.ptr_len.len == pkt->len + sizeof(pkt->len)); fail: free(pkt->contents); - return -1; + return io_debug_io(-1); } static struct io_plan io_read_packet(struct packet *pkt, -- 2.39.2