cryptospore / rpms / qemu-kvm

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