Skip to contentSkip to conversation

block: make aio_poll(ctx, true) block with no fds

PATCH Applied Stefan Hajnoczi 2 participants 4 messages Nov 26, 2013 – Dec 5, 2013

lore
Share & tools
lore
#0 lore raw

v2 cover block: make aio_poll(ctx, true) block with no fds

v2:
* Oops, this patch got lost. Rebased onto qemu.git/master for 1.8

Jan and Alex have expressed that aio_poll(ctx, blocking=true) should block even when there are no file descriptors registered. This can be handy since other threads may still kick the AioContext using aio_notify(ctx).

A concrete example is a thread that has only a timer in its AioContext. aio_poll(ctx, true) should block until the timer expires or another thread invokes aio_notify(ctx).

Alex and Paolo were concerned about bdrv_drain_all() which has the following comment:

  while (busy) {
      /* FIXME: We do not have timer support here, so this is effectively
       * a busy wait.
       */
      QTAILQ_FOREACH(bs, &bdrv_states, list) {
          if (bdrv_start_throttled_reqs(bs)) {
              busy = true;
          }
      }
      busy = bdrv_requests_pending_all();
      busy |= aio_poll(qemu_get_aio_context(), busy);

}

Patch 1 drops this outdated comment. The new I/O throttling code already eliminated the busy wait.

Patch 2 drops the special case which returns immediately from aio_poll(ctx, true) when no file descriptors are registered.

Note that aio_notify(ctx) still causes aio_poll(ctx, true) to return false. I don't see a need to change it so aio_poll(ctx, true) always returns true.

Stefan Hajnoczi (2):
block: clean up bdrv_drain_all() throttling comments
aio: make aio_poll(ctx, true) block with no fds

 aio-posix.c      | 5 -----
 aio-win32.c      | 5 -----
 block.c          | 7 +------
 tests/test-aio.c | 1 -
 4 files changed, 1 insertion(+), 17 deletions(-)
Signature · 1 line
1.8.4.2
#1 lore raw

PATCH v2 1/2 block: clean up bdrv_drain_all() throttling comments

Since cc0681c45430a1f1a4c2d06e9499b7775afc9a18 ("block: Enable the new throttling code in the block layer.") bdrv_drain_all() no longer spins. The code used to look as follows:

  do {
      busy = qemu_aio_wait();
      /* FIXME: We do not have timer support here, so this is effectively
       * a busy wait.
       */
      QTAILQ_FOREACH(bs, &bdrv_states, list) {
          while (qemu_co_enter_next(&bs->throttled_reqs)) {
              busy = true;
          }
      }

} while (busy);

Note that throttle requests are kicked but I/O throttling limits are still in effect. The loop spins until the vm_clock time allows the request to make progress and complete.

The new throttling code introduced bdrv_start_throttled_reqs(). This function not only kicks throttled requests but also temporarily disables throttling so requests can run.

The outdated FIXME comment can be removed. Also drop the busy = true assignment since we overwrite it immediately afterwards.

---
 block.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
block.c +1 -6
index 382ea71..13ad3fd 100644
@@ -1559,13 +1559,8 @@ void bdrv_drain_all(void)
     BlockDriverState *bs;
 
     while (busy) {
-        /* FIXME: We do not have timer support here, so this is effectively
-         * a busy wait.
-         */
         QTAILQ_FOREACH(bs, &bdrv_states, list) {
-            if (bdrv_start_throttled_reqs(bs)) {
-                busy = true;
-            }
+            bdrv_start_throttled_reqs(bs);
         }
 
         busy = bdrv_requests_pending_all();
Signature · 1 line
1.8.4.2
#2 lore raw

PATCH v2 2/2 aio: make aio_poll(ctx, true) block with no fds

In reply to Stefan Hajnoczi

This patch drops a special case where aio_poll(ctx, true) returns false instead of blocking if no file descriptors are waiting on I/O. Now it is possible to block in aio_poll() to wait for aio_notify().

This change eliminates busy waiting. bdrv_drain_all() used to rely on busy waiting to completed throttled I/O requests but this is no longer required so we can simplify aio_poll().

Note that aio_poll() still returns false when aio_notify() was used. In other words, stopping a blocking aio_poll() wait is not considered making progress.

Adjust test-aio /aio/bh/callback-delete/one which assumed aio_poll(ctx, true) would immediately return false instead of blocking.

---
 aio-posix.c      | 5 -----
 aio-win32.c      | 5 -----
 tests/test-aio.c | 1 -
 3 files changed, 11 deletions(-)
aio-posix.c +0 -5
index bd06f33..f921d4f 100644
@@ -217,11 +217,6 @@ bool aio_poll(AioContext *ctx, bool blocking)
 
     ctx->walking_handlers--;
 
-    /* early return if we only have the aio_notify() fd */
-    if (ctx->pollfds->len == 1) {
-        return progress;
-    }
-
     /* wait until next event */
     ret = qemu_poll_ns((GPollFD *)ctx->pollfds->data,
                          ctx->pollfds->len,
aio-win32.c +0 -5
index f9cfbb7..23f4e5b 100644
@@ -161,11 +161,6 @@ bool aio_poll(AioContext *ctx, bool blocking)
 
     ctx->walking_handlers--;
 
-    /* early return if we only have the aio_notify() fd */
-    if (count == 1) {
-        return progress;
-    }
-
     /* wait until next event */
     while (count > 0) {
         int ret;
tests/test-aio.c +0 -1
index c4fe0fc..592721e 100644
@@ -195,7 +195,6 @@ static void test_bh_delete_from_cb(void)
     g_assert(data1.bh == NULL);
 
     g_assert(!aio_poll(ctx, false));
-    g_assert(!aio_poll(ctx, true));
 }
 
 static void test_bh_delete_from_cb_many(void)
Signature · 1 line
1.8.4.2
#3 lore raw
In reply to Stefan Hajnoczi
Quoted 41 lines · Stefan Hajnoczi

v2:
* Oops, this patch got lost. Rebased onto qemu.git/master for 1.8

Jan and Alex have expressed that aio_poll(ctx, blocking=true) should block even when there are no file descriptors registered. This can be handy since other threads may still kick the AioContext using aio_notify(ctx).

A concrete example is a thread that has only a timer in its AioContext. aio_poll(ctx, true) should block until the timer expires or another thread invokes aio_notify(ctx).

Alex and Paolo were concerned about bdrv_drain_all() which has the following comment:

  while (busy) {
      /* FIXME: We do not have timer support here, so this is effectively
       * a busy wait.
       */
      QTAILQ_FOREACH(bs, &bdrv_states, list) {
          if (bdrv_start_throttled_reqs(bs)) {
              busy = true;
          }
      }
      busy = bdrv_requests_pending_all();
      busy |= aio_poll(qemu_get_aio_context(), busy);

}

Patch 1 drops this outdated comment. The new I/O throttling code already eliminated the busy wait.

Patch 2 drops the special case which returns immediately from aio_poll(ctx, true) when no file descriptors are registered.

Note that aio_notify(ctx) still causes aio_poll(ctx, true) to return false. I don't see a need to change it so aio_poll(ctx, true) always returns true.

Stefan Hajnoczi (2):
block: clean up bdrv_drain_all() throttling comments
aio: make aio_poll(ctx, true) block with no fds

Quoted 5 lines
 aio-posix.c      | 5 -----
 aio-win32.c      | 5 -----
 block.c          | 7 +------
 tests/test-aio.c | 1 -
 4 files changed, 1 insertion(+), 17 deletions(-)

Applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan