Blame SOURCES/kvm-aio-posix-compute-timeout-before-polling.patch

383d26
From 451a04350505ac3078620cacc1c89389c848e6c5 Mon Sep 17 00:00:00 2001
383d26
From: Fam Zheng <famz@redhat.com>
383d26
Date: Tue, 18 Sep 2018 09:07:13 +0200
383d26
Subject: [PATCH 2/3] aio-posix: compute timeout before polling
383d26
383d26
RH-Author: Fam Zheng <famz@redhat.com>
383d26
Message-id: <20180918090714.18069-3-famz@redhat.com>
383d26
Patchwork-id: 82213
383d26
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 2/3] aio-posix: compute timeout before polling
383d26
Bugzilla: 1628191
383d26
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
383d26
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
383d26
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
383d26
383d26
From: Paolo Bonzini <pbonzini@redhat.com>
383d26
383d26
This is a preparation for the next patch, and also a very small
383d26
optimization.  Compute the timeout only once, before invoking
383d26
try_poll_mode, and adjust it in run_poll_handlers.  The adjustment
383d26
is the polling time when polling fails, or zero (non-blocking) if
383d26
polling succeeds.
383d26
383d26
Fixes: 70232b5253a3c4e03ed1ac47ef9246a8ac66c6fa
383d26
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
383d26
Message-Id: <20180912171040.1732-3-pbonzini@redhat.com>
383d26
Reviewed-by: Fam Zheng <famz@redhat.com>
383d26
Signed-off-by: Fam Zheng <famz@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 util/aio-posix.c  | 59 ++++++++++++++++++++++++++++++++-----------------------
383d26
 util/trace-events |  4 ++--
383d26
 2 files changed, 36 insertions(+), 27 deletions(-)
383d26
383d26
diff --git a/util/aio-posix.c b/util/aio-posix.c
383d26
index 1d7cc53..1b17597 100644
383d26
--- a/util/aio-posix.c
383d26
+++ b/util/aio-posix.c
383d26
@@ -490,7 +490,7 @@ static void add_pollfd(AioHandler *node)
383d26
     npfd++;
383d26
 }
383d26
 
383d26
-static bool run_poll_handlers_once(AioContext *ctx)
383d26
+static bool run_poll_handlers_once(AioContext *ctx, int64_t *timeout)
383d26
 {
383d26
     bool progress = false;
383d26
     AioHandler *node;
383d26
@@ -500,6 +500,7 @@ static bool run_poll_handlers_once(AioContext *ctx)
383d26
             aio_node_check(ctx, node->is_external) &&
383d26
             node->io_poll(node->opaque) &&
383d26
             node->opaque != &ctx->notifier) {
383d26
+            *timeout = 0;
383d26
             progress = true;
383d26
         }
383d26
 
383d26
@@ -522,31 +523,38 @@ static bool run_poll_handlers_once(AioContext *ctx)
383d26
  *
383d26
  * Returns: true if progress was made, false otherwise
383d26
  */
383d26
-static bool run_poll_handlers(AioContext *ctx, int64_t max_ns)
383d26
+static bool run_poll_handlers(AioContext *ctx, int64_t max_ns, int64_t *timeout)
383d26
 {
383d26
     bool progress;
383d26
-    int64_t end_time;
383d26
+    int64_t start_time, elapsed_time;
383d26
 
383d26
     assert(ctx->notify_me);
383d26
     assert(qemu_lockcnt_count(&ctx->list_lock) > 0);
383d26
 
383d26
-    trace_run_poll_handlers_begin(ctx, max_ns);
383d26
-
383d26
-    end_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + max_ns;
383d26
+    trace_run_poll_handlers_begin(ctx, max_ns, *timeout);
383d26
 
383d26
+    start_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
383d26
     do {
383d26
-        progress = run_poll_handlers_once(ctx);
383d26
-    } while (!progress && qemu_clock_get_ns(QEMU_CLOCK_REALTIME) < end_time
383d26
+        progress = run_poll_handlers_once(ctx, timeout);
383d26
+        elapsed_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time;
383d26
+    } while (!progress && elapsed_time < max_ns
383d26
              && !atomic_read(&ctx->poll_disable_cnt));
383d26
 
383d26
-    trace_run_poll_handlers_end(ctx, progress);
383d26
+    /* If time has passed with no successful polling, adjust *timeout to
383d26
+     * keep the same ending time.
383d26
+     */
383d26
+    if (*timeout != -1) {
383d26
+        *timeout -= MIN(*timeout, elapsed_time);
383d26
+    }
383d26
 
383d26
+    trace_run_poll_handlers_end(ctx, progress, *timeout);
383d26
     return progress;
383d26
 }
383d26
 
383d26
 /* try_poll_mode:
383d26
  * @ctx: the AioContext
383d26
- * @blocking: busy polling is only attempted when blocking is true
383d26
+ * @timeout: timeout for blocking wait, computed by the caller and updated if
383d26
+ *    polling succeeds.
383d26
  *
383d26
  * ctx->notify_me must be non-zero so this function can detect aio_notify().
383d26
  *
383d26
@@ -554,19 +562,16 @@ static bool run_poll_handlers(AioContext *ctx, int64_t max_ns)
383d26
  *
383d26
  * Returns: true if progress was made, false otherwise
383d26
  */
383d26
-static bool try_poll_mode(AioContext *ctx, bool blocking)
383d26
+static bool try_poll_mode(AioContext *ctx, int64_t *timeout)
383d26
 {
383d26
-    if (blocking && ctx->poll_max_ns && !atomic_read(&ctx->poll_disable_cnt)) {
383d26
-        /* See qemu_soonest_timeout() uint64_t hack */
383d26
-        int64_t max_ns = MIN((uint64_t)aio_compute_timeout(ctx),
383d26
-                             (uint64_t)ctx->poll_ns);
383d26
+    /* See qemu_soonest_timeout() uint64_t hack */
383d26
+    int64_t max_ns = MIN((uint64_t)*timeout, (uint64_t)ctx->poll_ns);
383d26
 
383d26
-        if (max_ns) {
383d26
-            poll_set_started(ctx, true);
383d26
+    if (max_ns && !atomic_read(&ctx->poll_disable_cnt)) {
383d26
+        poll_set_started(ctx, true);
383d26
 
383d26
-            if (run_poll_handlers(ctx, max_ns)) {
383d26
-                return true;
383d26
-            }
383d26
+        if (run_poll_handlers(ctx, max_ns, timeout)) {
383d26
+            return true;
383d26
         }
383d26
     }
383d26
 
383d26
@@ -575,7 +580,7 @@ static bool try_poll_mode(AioContext *ctx, bool blocking)
383d26
     /* Even if we don't run busy polling, try polling once in case it can make
383d26
      * progress and the caller will be able to avoid ppoll(2)/epoll_wait(2).
383d26
      */
383d26
-    return run_poll_handlers_once(ctx);
383d26
+    return run_poll_handlers_once(ctx, timeout);
383d26
 }
383d26
 
383d26
 bool aio_poll(AioContext *ctx, bool blocking)
383d26
@@ -605,8 +610,14 @@ bool aio_poll(AioContext *ctx, bool blocking)
383d26
         start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
383d26
     }
383d26
 
383d26
-    progress = try_poll_mode(ctx, blocking);
383d26
-    if (!progress) {
383d26
+    timeout = blocking ? aio_compute_timeout(ctx) : 0;
383d26
+    progress = try_poll_mode(ctx, &timeout);
383d26
+    assert(!(timeout && progress));
383d26
+
383d26
+    /* If polling is allowed, non-blocking aio_poll does not need the
383d26
+     * system call---a single round of run_poll_handlers_once suffices.
383d26
+     */
383d26
+    if (timeout || atomic_read(&ctx->poll_disable_cnt)) {
383d26
         assert(npfd == 0);
383d26
 
383d26
         /* fill pollfds */
383d26
@@ -620,8 +631,6 @@ bool aio_poll(AioContext *ctx, bool blocking)
383d26
             }
383d26
         }
383d26
 
383d26
-        timeout = blocking ? aio_compute_timeout(ctx) : 0;
383d26
-
383d26
         /* wait until next event */
383d26
         if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) {
383d26
             AioHandler epoll_handler;
383d26
diff --git a/util/trace-events b/util/trace-events
383d26
index 4822434..79569b7 100644
383d26
--- a/util/trace-events
383d26
+++ b/util/trace-events
383d26
@@ -1,8 +1,8 @@
383d26
 # See docs/devel/tracing.txt for syntax documentation.
383d26
 
383d26
 # util/aio-posix.c
383d26
-run_poll_handlers_begin(void *ctx, int64_t max_ns) "ctx %p max_ns %"PRId64
383d26
-run_poll_handlers_end(void *ctx, bool progress) "ctx %p progress %d"
383d26
+run_poll_handlers_begin(void *ctx, int64_t max_ns, int64_t timeout) "ctx %p max_ns %"PRId64 " timeout %"PRId64
383d26
+run_poll_handlers_end(void *ctx, bool progress, int64_t timeout) "ctx %p progress %d new timeout %"PRId64
383d26
 poll_shrink(void *ctx, int64_t old, int64_t new) "ctx %p old %"PRId64" new %"PRId64
383d26
 poll_grow(void *ctx, int64_t old, int64_t new) "ctx %p old %"PRId64" new %"PRId64
383d26
 
383d26
-- 
383d26
1.8.3.1
383d26