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

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