On 6/29/19 8:28 AM, Eric Blake wrote:
Some clients need to know when it is safe to issue NBD_CMD_DISC, or to decide whether calling poll(POLLIN) will block indefinitely because the server isn't expected to respond. Make this easier to learn by tracking the count of commands we have queued up to send, as well as the count of commands where we are waiting on the server's response.
Update tests/aio-parallel* and examples/batched-read-write to use nbd's own in-flight counter instead of reimplementing it ourselves.
Note that h->in_flight is only ever updated while the lock is held; but we may want to consider also making it atomic and therefore readable as a lock-less function. ---
+++ b/lib/aio.c @@ -23,6 +23,7 @@ #include <stdbool.h> #include <errno.h> #include <inttypes.h> +#include <assert.h>
#include "internal.h"
@@ -84,6 +85,8 @@ nbd_unlocked_aio_command_completed (struct nbd_handle *h, prev_cmd->next = cmd->next; else h->cmds_done = cmd->next; + h->in_flight--; + assert (h->in_flight >= 0);
We guard against underflow...
+++ b/lib/rw.c
@@ -236,6 +241,7 @@ nbd_internal_command_common (struct nbd_handle *h, nbd_internal_run (h, cmd_issue) == -1) return -1; }
+ h->in_flight++;
...but even though we inserted in the list, we fail to increment if nbd_internal_run() encountered an error. Also, the count is too low if the server managed to get a reply to us without us blocking. Obvious fix pushed. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org