Skip to contentSkip to conversation

improve aio-polling efficiency

RFC PATCH Superseded Jaehoon Kim 3 participants 18 messages Mar 23 – Mar 31

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover improve aio-polling efficiency

Dear all,

This is v2 of the RFC patch series to refine aio_poll adaptive polling logic for better CPU efficiency.

v1:
https://lore.kernel.org/qemu-devel/[email protected]/

Changes in v2:
- Patch 2/3: Changed default POLL_WEIGHT_SHIFT from 2 to 3 based on extensive testing. Updated commit message with detailed performance comparison showing weight=3 provides better balance between throughput and CPU savings

- Patch 3/3: Added proper initialization of poll-weight, poll-grow, and poll-shrink defaults in iothread.c

This patch series refines the aio_poll adaptive polling logic to reduce unnecessary busy-waiting and improve CPU efficiency.

The first patch prevents redundant polling time calculation when polling is disabled. The second patch enhances the adaptive polling mechanism by dynamically adjusting the iothread's polling duration based on event intervals measured by individual AioHandlers. The third patch introduces a new 'poll-weight' parameter for runtime control over how much the current interval influences the next polling duration.

We evaluated the patches on s390x hosts with different configurations:

Initial testing (Fedora 42):
Using a single guest with 16 virtio block devices backed by FCP multipath devices, I/O scheduler set to 'none'. Across four FIO workload patterns (sequential R/W, random R/W), averaged over numjobs 1, 4, 8, and 16:

- Throughput: -3% to -8% (one iothread), -2% to -5% (two iothreads)
- CPU usage: -10% to -25% (one iothread), -7% to -12% (two iothreads)

Additional validation (RHEL 10.1 GA + QEMU 10.0.0):
Comparing baseline vs poll-weight=2/3 with FCP and FICON storage, using 1 and 8 iothreads, averaged over numjobs 1, 4, and 8:

Summary of results (% change vs baseline):

- Throughput avg: -2.2% (weight=3), -2.4% (weight=2)
- CPU consumption avg: -9.4% (weight=3), -10.9% (weight=2)

Weight=3 was selected as default for providing slightly better throughput (-2.2% vs -2.4%) while maintaining substantial CPU savings (-9.4%).

Best regards,
Jaehoon Kim

Jaehoon Kim (3):
aio-poll: avoid unnecessary polling time computation
aio-poll: refine iothread polling using weighted handler intervals qapi/iothread: introduce poll-weight parameter for aio-poll

 include/qemu/aio.h                |   8 +-
 include/system/iothread.h         |   1 +
 iothread.c                        |  34 ++++++-
 monitor/hmp-cmds.c                |   1 +
 qapi/misc.json                    |   7 ++
 qapi/qom.json                     |   8 +-
 qemu-options.hx                   |   7 +-
 tests/unit/test-nested-aio-poll.c |   2 +-
 util/aio-posix.c                  | 141 ++++++++++++++++++++----------
 util/aio-win32.c                  |   3 +-
 util/async.c                      |   2 +
 11 files changed, 161 insertions(+), 53 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

RFC PATCH v2 1/3 aio-poll: avoid unnecessary polling time computation

Nodes are no longer added to poll_aio_handlers when adaptive polling is disabled, preventing unnecessary try_poll_mode() calls. Additionally, aio_poll() skips try_poll_mode() when timeout is 0.

This avoids iterating over all nodes to compute max_ns unnecessarily when polling is disabled or timeout is 0.

---
 util/aio-posix.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
util/aio-posix.c +6 -5
index 488d964611..b02beb0505 100644
@@ -307,9 +307,8 @@ static bool aio_dispatch_handler(AioContext *ctx, AioHandler *node)
      * fdmon_supports_polling(), but only until the fd fires for the first
      * time.
      */
-    if (!QLIST_IS_INSERTED(node, node_deleted) &&
-        !QLIST_IS_INSERTED(node, node_poll) &&
-        node->io_poll) {
+    if (ctx->poll_max_ns && !QLIST_IS_INSERTED(node, node_deleted) &&
+        !QLIST_IS_INSERTED(node, node_poll) && node->io_poll) {
         trace_poll_add(ctx, node, node->pfd.fd, revents);
         if (ctx->poll_started && node->io_poll_begin) {
             node->io_poll_begin(node->opaque);
@@ -631,7 +630,7 @@ static void adjust_polling_time(AioContext *ctx, AioPolledEvent *poll,
 bool aio_poll(AioContext *ctx, bool blocking)
 {
     AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list);
-    bool progress;
+    bool progress = false;
     bool use_notify_me;
     int64_t timeout;
     int64_t start = 0;
@@ -656,7 +655,9 @@ bool aio_poll(AioContext *ctx, bool blocking)
     }
 
     timeout = blocking ? aio_compute_timeout(ctx) : 0;
-    progress = try_poll_mode(ctx, &ready_list, &timeout);
+    if ((ctx->poll_max_ns != 0) && (timeout != 0)) {
+        progress = try_poll_mode(ctx, &ready_list, &timeout);
+    }
     assert(!(timeout && progress));
 
     /*
Signature · 1 line
2.50.1
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH RFC v2 1/3] aio-poll: avoid unnecessary polling time computation

Quoted 3 lines · Jaehoon Kim

Nodes are no longer added to poll_aio_handlers when adaptive polling is disabled, preventing unnecessary try_poll_mode() calls. Additionally, aio_poll() skips try_poll_mode() when timeout is 0.

Skipping when timeout is 0 seems risky to me. VIRTIO devices disable guest kicks when polling mode is started. When aio_poll(ctx, blocking=false) is called, we will skip polling and ctx->fdmon_ops->need_wait(ctx) won't detect an event either. aio_poll() will return without noticing that the VIRTIO device's AioHandler is ready.

Is skipping when timeout 0 necessary for performance or can it be dropped from the patch?

Aside from this:

#3 lore raw

Re: [PATCH RFC v2 1/3] aio-poll: avoid unnecessary polling time computation

Quoted 11 lines · Stefan Hajnoczi
Quoted 3 lines · Jaehoon Kim

Nodes are no longer added to poll_aio_handlers when adaptive polling is disabled, preventing unnecessary try_poll_mode() calls. Additionally, aio_poll() skips try_poll_mode() when timeout is 0.

Skipping when timeout is 0 seems risky to me. VIRTIO devices disable guest kicks when polling mode is started. When aio_poll(ctx, blocking=false) is called, we will skip polling and ctx->fdmon_ops->need_wait(ctx) won't detect an event either. aio_poll() will return without noticing that the VIRTIO device's AioHandler is ready.

Quoted 6 lines

Is skipping when timeout 0 necessary for performance or can it be dropped from the patch?

Aside from this:

Thank you for the review!
Removing the (timeout != 0) check does not cause any issue on my side, so I plan to remove it in the next version.

However, I'd like to clarify: when timeout is 0, is there any concern in the try_poll_mode function below that I might be missing? I just want to make sure I understand correctly.

static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, int64_t *timeout)
{
    AioHandler *node;
    int64_t max_ns;

    if (QLIST_EMPTY_RCU(&ctx->poll_aio_handlers)) { return false;
    }

    max_ns = 0;
    QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) { max_ns = MAX(max_ns, node->poll.ns);
    }
    max_ns = qemu_soonest_timeout(*timeout, max_ns);

    if (max_ns && !ctx->fdmon_ops->need_wait(ctx)) {
        /*
         * Enable poll mode. It pairs with the poll_set_started() in
         * aio_poll() which disables poll mode.
         */
        poll_set_started(ctx, ready_list, true);

    .....

Regards,
Jaehoon Kim.

#4 lore raw

Re: [PATCH RFC v2 1/3] aio-poll: avoid unnecessary polling time computation

Quoted 37 lines · JAEHOON KIM
Quoted 11 lines · Stefan Hajnoczi
Quoted 3 lines · Jaehoon Kim

Nodes are no longer added to poll_aio_handlers when adaptive polling is disabled, preventing unnecessary try_poll_mode() calls. Additionally, aio_poll() skips try_poll_mode() when timeout is 0.

Skipping when timeout is 0 seems risky to me. VIRTIO devices disable guest kicks when polling mode is started. When aio_poll(ctx, blocking=false) is called, we will skip polling and ctx->fdmon_ops->need_wait(ctx) won't detect an event either. aio_poll() will return without noticing that the VIRTIO device's AioHandler is ready.

Quoted 6 lines

Is skipping when timeout 0 necessary for performance or can it be dropped from the patch?

Aside from this:

Thank you for the review!
Removing the (timeout != 0) check does not cause any issue on my side, so I plan to remove it in the next version.

However, I'd like to clarify: when timeout is 0, is there any concern in the try_poll_mode function below that I might be missing? I just want to make sure I understand correctly.

static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, int64_t *timeout)
{
    AioHandler *node;
    int64_t max_ns;

    if (QLIST_EMPTY_RCU(&ctx->poll_aio_handlers)) { return false;
    }

Quoted 7 lines

    max_ns = 0;
    QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) { max_ns = MAX(max_ns, node->poll.ns);
    }
    max_ns = qemu_soonest_timeout(*timeout, max_ns);

    if (max_ns && !ctx->fdmon_ops->need_wait(ctx)) {

I think you are pointing out that max_ns will be 0 and therefore polling will be skipped? At least this is what came to mind when I read this code again.

That is a problem because the exact scenario I described in my reply to you can already happen in the existing code before your patch :(.

Avoiding the timeout != 0 check in your patch would help contain the bug in try_poll_mode() rather than extending it to aio_poll() as well.

Thanks for pointing this out!

Stefan

#5 lore raw

RFC PATCH v2 2/3 aio-poll: refine iothread polling using weighted handler intervals

In reply to Jaehoon Kim

Refine adaptive polling in aio_poll by updating iothread polling duration based on weighted AioHandler event intervals.

Each AioHandler's poll.ns is updated using a weighted factor when an event occurs. Idle handlers accumulate block_ns until poll_max_ns and then reset to 0, preventing sporadically active handlers from unnecessarily prolonging iothread polling.

The iothread polling duration is set based on the largest poll.ns among active handlers. The shrink divider defaults to 2, matching the grow rate, to reduce frequent poll_ns resets for slow devices.

The default weight factor (POLL_WEIGHT_SHIFT=3, meaning the current interval contributes 12.5% to the weighted average) was selected based on extensive testing comparing QEMU 10.0.0 baseline vs poll-weight=2 and poll-weight=3 across various workloads.

The table below shows a comparison between:
-Host: RHEL 10.1 GA + qemu-10.0.0-14.el10_1, Guest: RHEL 9.6GA vs. -Host: RHEL 10.1 GA + qemu-10.0.0-14.el10_1 (w=2/w=3), Guest: RHEL 9.6GA for FIO FCP and FICON with 1 iothread and 8 iothreads.
The values shown are the averages for numjobs 1, 4, and 8.

Summary of results (% change vs baseline):

                    | poll-weight=2      | poll-weight=3
--------------------|--------------------|-----------------
Throughput avg      | -2.4% (all tests)  | -2.2% (all tests)
CPU consumption avg | -10.9% (all tests) | -9.4% (all tests)

Both weight=2 and weight=3 show significant CPU consumption reduction (~10%) compared to baseline, which addresses the CPU utilization regression observed in QEMU 10.0.0. The throughput impact is minimal for both (~2%).

Weight=3 is selected as the default because it provides slightly better throughput (-2.2% vs -2.4%) while still achieving substantial CPU savings (-9.4%). The difference between weight=2 and weight=3 is small, but weight=3 offers a better balance for general-purpose workloads.

---
 include/qemu/aio.h |   4 +-
 util/aio-posix.c   | 135 +++++++++++++++++++++++++++++++--------------
 util/async.c       |   1 +
 3 files changed, 99 insertions(+), 41 deletions(-)
include/qemu/aio.h +3 -1
index 8cca2360d1..6c77a190e9 100644
@@ -195,7 +195,8 @@ struct BHListSlice {
 typedef QSLIST_HEAD(, AioHandler) AioHandlerSList;
 
 typedef struct AioPolledEvent {
-    int64_t ns;        /* current polling time in nanoseconds */
+    bool has_event; /* Flag to indicate if an event has occurred */
+    int64_t ns;     /* estimated block time in nanoseconds */
 } AioPolledEvent;
 
 struct AioContext {
@@ -306,6 +307,7 @@ struct AioContext {
     int poll_disable_cnt;
 
     /* Polling mode parameters */
+    int64_t poll_ns;        /* current polling time in nanoseconds */
     int64_t poll_max_ns;    /* maximum polling time in nanoseconds */
     int64_t poll_grow;      /* polling time growth factor */
     int64_t poll_shrink;    /* polling time shrink factor */
util/aio-posix.c +95 -40

193 lines not on this page · raw message

util/async.c +1 -0
index 80d6b01a8a..9d3627566f 100644
@@ -606,6 +606,7 @@ AioContext *aio_context_new(Error **errp)
     timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
 
     ctx->poll_max_ns = 0;
+    ctx->poll_ns = 0;
     ctx->poll_grow = 0;
     ctx->poll_shrink = 0;
 
Signature · 1 line
2.50.1
#6 lore raw

Re: [PATCH RFC v2 2/3] aio-poll: refine iothread polling using weighted handler intervals

Quoted 88 lines · Jaehoon Kim

Refine adaptive polling in aio_poll by updating iothread polling duration based on weighted AioHandler event intervals.

Each AioHandler's poll.ns is updated using a weighted factor when an event occurs. Idle handlers accumulate block_ns until poll_max_ns and then reset to 0, preventing sporadically active handlers from unnecessarily prolonging iothread polling.

The iothread polling duration is set based on the largest poll.ns among active handlers. The shrink divider defaults to 2, matching the grow rate, to reduce frequent poll_ns resets for slow devices.

The default weight factor (POLL_WEIGHT_SHIFT=3, meaning the current interval contributes 12.5% to the weighted average) was selected based on extensive testing comparing QEMU 10.0.0 baseline vs poll-weight=2 and poll-weight=3 across various workloads.

The table below shows a comparison between:

(quoted diff) +0 -2
-Host: RHEL 10.1 GA + qemu-10.0.0-14.el10_1, Guest: RHEL 9.6GA vs.
-Host: RHEL 10.1 GA + qemu-10.0.0-14.el10_1 (w=2/w=3), Guest: RHEL 9.6GA

for FIO FCP and FICON with 1 iothread and 8 iothreads. The values shown are the averages for numjobs 1, 4, and 8.

Summary of results (% change vs baseline):

                    | poll-weight=2      | poll-weight=3
--------------------|--------------------|-----------------
Throughput avg      | -2.4% (all tests)  | -2.2% (all tests)
CPU consumption avg | -10.9% (all tests) | -9.4% (all tests)

Both weight=2 and weight=3 show significant CPU consumption reduction (~10%) compared to baseline, which addresses the CPU utilization regression observed in QEMU 10.0.0. The throughput impact is minimal for both (~2%).

Weight=3 is selected as the default because it provides slightly better throughput (-2.2% vs -2.4%) while still achieving substantial CPU savings (-9.4%). The difference between weight=2 and weight=3 is small, but weight=3 offers a better balance for general-purpose workloads.

---
 include/qemu/aio.h |   4 +-
 util/aio-posix.c   | 135 +++++++++++++++++++++++++++++++--------------
 util/async.c       |   1 +
 3 files changed, 99 insertions(+), 41 deletions(-)
include/qemu/aio.h +3 -1
index 8cca2360d1..6c77a190e9 100644
@@ -195,7 +195,8 @@ struct BHListSlice {
 typedef QSLIST_HEAD(, AioHandler) AioHandlerSList;
 
 typedef struct AioPolledEvent {
-    int64_t ns;        /* current polling time in nanoseconds */
+    bool has_event; /* Flag to indicate if an event has occurred */
+    int64_t ns;     /* estimated block time in nanoseconds */
 } AioPolledEvent;
 
 struct AioContext {
@@ -306,6 +307,7 @@ struct AioContext {
     int poll_disable_cnt;
 
     /* Polling mode parameters */
+    int64_t poll_ns;        /* current polling time in nanoseconds */
     int64_t poll_max_ns;    /* maximum polling time in nanoseconds */
     int64_t poll_grow;      /* polling time growth factor */
     int64_t poll_shrink;    /* polling time shrink factor */
util/aio-posix.c +4 -2
index b02beb0505..2b3522f2f9 100644
@@ -29,9 +29,11 @@
 
 /* Stop userspace polling on a handler if it isn't active for some time */
 #define POLL_IDLE_INTERVAL_NS (7 * NANOSECONDS_PER_SECOND)
+#define POLL_WEIGHT_SHIFT   (3)
 
-static void adjust_polling_time(AioContext *ctx, AioPolledEvent *poll,
-                                int64_t block_ns);
+static void adjust_block_ns(AioContext *ctx, int64_t block_ns);
+static void grow_polling_time(AioContext *ctx, int64_t block_ns);
+static void shrink_polling_time(AioContext *ctx, int64_t block_ns);
 
 bool aio_poll_disabled(AioContext *ctx)
 {

util/aio-posix.c static bool aio_dispatch_ready_handlers(AioContext *ctx, · line 375

Quoted 1 line
util/aio-posix.c +0 -0
@@ -373,7 +375,7 @@ static bool aio_dispatch_ready_handlers(AioContext *ctx,
          * add the handler to ctx->poll_aio_handlers.

This comment refers to adjusting the polling time. The code no longer does this and the comment should be updated.

util/aio-posix.c static bool aio_dispatch_ready_handlers(AioContext *ctx, · line 376

Quoted 3 lines
util/aio-posix.c +0 -1
          */
         if (ctx->poll_max_ns && QLIST_IS_INSERTED(node, node_poll)) {
-            adjust_polling_time(ctx, &node->poll, block_ns);

aio_dispatch_ready_handlers() no longer uses the block_ns argument. It can be removed.

Quoted 63 lines
util/aio-posix.c +12 -28
+            node->poll.has_event = true;
         }
     }
 
@@ -560,18 +562,13 @@ static bool run_poll_handlers(AioContext *ctx, AioHandlerList *ready_list,
 static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list,
                           int64_t *timeout)
 {
-    AioHandler *node;
     int64_t max_ns;
 
     if (QLIST_EMPTY_RCU(&ctx->poll_aio_handlers)) {
         return false;
     }
 
-    max_ns = 0;
-    QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) {
-        max_ns = MAX(max_ns, node->poll.ns);
-    }
-    max_ns = qemu_soonest_timeout(*timeout, max_ns);
+    max_ns = qemu_soonest_timeout(*timeout, ctx->poll_ns);
 
     if (max_ns && !ctx->fdmon_ops->need_wait(ctx)) {
         /*
@@ -587,46 +584,98 @@ static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list,
     return false;
 }
 
-static void adjust_polling_time(AioContext *ctx, AioPolledEvent *poll,
-                                int64_t block_ns)
+static void shrink_polling_time(AioContext *ctx, int64_t block_ns)
 {
-    if (block_ns <= poll->ns) {
-        /* This is the sweet spot, no adjustment needed */
-    } else if (block_ns > ctx->poll_max_ns) {
-        /* We'd have to poll for too long, poll less */
-        int64_t old = poll->ns;
-
-        if (ctx->poll_shrink) {
-            poll->ns /= ctx->poll_shrink;
-        } else {
-            poll->ns = 0;
-        }
+    /*
+     * Reduce polling time if the block_ns is zero or
+     * less than the current poll_ns.
+     */
+    int64_t old = ctx->poll_ns;
+    int64_t shrink = ctx->poll_shrink;
 
-        trace_poll_shrink(ctx, old, poll->ns);
-    } else if (poll->ns < ctx->poll_max_ns &&
-               block_ns < ctx->poll_max_ns) {
-        /* There is room to grow, poll longer */
-        int64_t old = poll->ns;
-        int64_t grow = ctx->poll_grow;
+    if (shrink == 0) {
+        shrink = 2;
+    }
 
-        if (grow == 0) {
-            grow = 2;
-        }

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 604

Quoted 10 lines
util/aio-posix.c +4 -5
+    if (block_ns < (ctx->poll_ns / shrink)) {
+        ctx->poll_ns /= shrink;
+    }
 
-        if (poll->ns) {
-            poll->ns *= grow;
-        } else {
-            poll->ns = 4000; /* start polling at 4 microseconds */
-        }
+    trace_poll_shrink(ctx, old, ctx->poll_ns);

This trace event should be inside if (block_ns < (ctx->poll_ns / shrink)) like it was before this patch.

Quoted 17 lines
util/aio-posix.c +10 -4
+}
 
-        if (poll->ns > ctx->poll_max_ns) {
-            poll->ns = ctx->poll_max_ns;
-        }
+static void grow_polling_time(AioContext *ctx, int64_t block_ns)
+{
+    /* There is room to grow, poll longer */
+    int64_t old = ctx->poll_ns;
+    int64_t grow = ctx->poll_grow;
 
-        trace_poll_grow(ctx, old, poll->ns);
+    if (grow == 0) {
+        grow = 2;
     }
+
+    if (block_ns > ctx->poll_ns * grow) {

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 627

Quoted 10 lines
util/aio-posix.c +10 -0
+        ctx->poll_ns = block_ns;
+    } else {
+        ctx->poll_ns *= grow;
+    }
+
+    if (ctx->poll_ns > ctx->poll_max_ns) {
+        ctx->poll_ns = ctx->poll_max_ns;
+    }
+
+    trace_poll_grow(ctx, old, ctx->poll_ns);

Same here.

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 636

Quoted 9 lines
util/aio-posix.c +7 -0
 }
 
+static void adjust_block_ns(AioContext *ctx, int64_t block_ns)
+{
+    AioHandler *node;
+    int64_t adj_block_ns = -1;
+
+    QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) {
+        if (node->poll.has_event) {

Did you consider unifying node->poll.has_event with node->poll_idle_timeout, which is assigned now + POLL_IDLE_INTERVAL_NS every time ->io_poll() detects an event?

For instance, rename node->poll_idle_timeout to node->last_event_timestamp and assign now without adding POLL_IDLE_INTERVAL_NS. Then use the field for both idle node removal and adjust_block_ns() (pass in now).

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 648

Quoted 12 lines
util/aio-posix.c +12 -0
+            /*
+             * Update poll.ns for the node with an event.
+             * Uses a weighted average of the current block_ns and the previous
+             * poll.ns to smooth out polling time adjustments.
+             */
+            node->poll.ns = node->poll.ns
+                ? (node->poll.ns - (node->poll.ns >> POLL_WEIGHT_SHIFT))
+                + (block_ns >> POLL_WEIGHT_SHIFT) : block_ns;
+
+            if (node->poll.ns > ctx->poll_max_ns) {
+                node->poll.ns = 0;
+            }

Previously:
- if (poll->ns > ctx->poll_max_ns) {
- poll->ns = ctx->poll_max_ns;
- }

Was this causing excessive CPU consumption in your benchmarks?

Can you explain the rationale for zeroing the poll time? Aside from reducing CPU consumption, it also reduces the chance that polling will succeed and could therefore impact performance.

I'm asking about this because this patch makes several changes at once and I'm not sure how the CPU usage and performance changes are attributed to these multiple changes. I want to make sure the changes merged are minimal and the best set - sometimes when multiple things are changes at the same time, not all of them are beneficial.

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 653

Quoted 5 lines
util/aio-posix.c +5 -0
+            /*
+             * To avoid excessive polling time increase, update adj_block_ns
+             * for nodes with the event flag set to true
+             */
+            adj_block_ns = MAX(adj_block_ns, node->poll.ns);

adj_block_ns is not the blocking time, it's the maximum current poll time across all nodes. It would be clearer to change the variable name.

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 655

Quoted 2 lines
util/aio-posix.c +2 -0
+            node->poll.has_event = false;
+         } else {

4-space indentation should be used.

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 662

Quoted 7 lines
util/aio-posix.c +7 -0
+            /*
+             * No event now, but was active before.
+             * If it waits longer than poll_max_ns, poll.ns will stay 0
+             * until the next event arrives.
+             */
+            if (node->poll.ns != 0) {
+                node->poll.ns += block_ns;

Why is block_ns being added to an recently inactive node's polling time? Here node->poll.ns no longer measures the weighted time until the handler had an event.

If the goal is to get rid of inactive nodes, then maybe the idle handler removal mechanism should be made more aggresive instead?

Quoted 61 lines
util/aio-posix.c +22 -0
+                if (node->poll.ns > ctx->poll_max_ns) {
+                    node->poll.ns = 0;
+                }
+            }
+        }
+    }
+
+    if (adj_block_ns >= 0) {
+        if (adj_block_ns > ctx->poll_ns) {
+            grow_polling_time(ctx, adj_block_ns);
+        } else {
+            shrink_polling_time(ctx, adj_block_ns);
+         }
+     }
+ }
+
 bool aio_poll(AioContext *ctx, bool blocking)
 {
     AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list);
@@ -723,6 +772,10 @@ bool aio_poll(AioContext *ctx, bool blocking)
 
     aio_free_deleted_handlers(ctx);
 
+    if (ctx->poll_max_ns) {
+        adjust_block_ns(ctx, block_ns);
+    }
+
     qemu_lockcnt_dec(&ctx->list_lock);
 
     progress |= timerlistgroup_run_timers(&ctx->tlg);
@@ -784,6 +837,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
 
     qemu_lockcnt_inc(&ctx->list_lock);
     QLIST_FOREACH(node, &ctx->aio_handlers, node) {
+        node->poll.has_event = false;
         node->poll.ns = 0;
     }
     qemu_lockcnt_dec(&ctx->list_lock);
@@ -794,6 +848,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
     ctx->poll_max_ns = max_ns;
     ctx->poll_grow = grow;
     ctx->poll_shrink = shrink;
+    ctx->poll_ns = 0;
 
     aio_notify(ctx);
 }
util/async.c +1 -0
index 80d6b01a8a..9d3627566f 100644
@@ -606,6 +606,7 @@ AioContext *aio_context_new(Error **errp)
     timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
 
     ctx->poll_max_ns = 0;
+    ctx->poll_ns = 0;
     ctx->poll_grow = 0;
     ctx->poll_shrink = 0;
 
Signature · 1 line
2.50.1
#7 lore raw

Re: [PATCH RFC v2 2/3] aio-poll: refine iothread polling using weighted handler intervals

Quoted 86 lines · Stefan Hajnoczi
Quoted 85 lines · Jaehoon Kim

Refine adaptive polling in aio_poll by updating iothread polling duration based on weighted AioHandler event intervals.

Each AioHandler's poll.ns is updated using a weighted factor when an event occurs. Idle handlers accumulate block_ns until poll_max_ns and then reset to 0, preventing sporadically active handlers from unnecessarily prolonging iothread polling.

The iothread polling duration is set based on the largest poll.ns among active handlers. The shrink divider defaults to 2, matching the grow rate, to reduce frequent poll_ns resets for slow devices.

The default weight factor (POLL_WEIGHT_SHIFT=3, meaning the current interval contributes 12.5% to the weighted average) was selected based on extensive testing comparing QEMU 10.0.0 baseline vs poll-weight=2 and poll-weight=3 across various workloads.

The table below shows a comparison between:

(quoted diff) +0 -2
-Host: RHEL 10.1 GA + qemu-10.0.0-14.el10_1, Guest: RHEL 9.6GA vs.
-Host: RHEL 10.1 GA + qemu-10.0.0-14.el10_1 (w=2/w=3), Guest: RHEL 9.6GA

for FIO FCP and FICON with 1 iothread and 8 iothreads. The values shown are the averages for numjobs 1, 4, and 8.

Summary of results (% change vs baseline):

                     | poll-weight=2      | poll-weight=3
--------------------|--------------------|-----------------
Throughput avg      | -2.4% (all tests)  | -2.2% (all tests)
CPU consumption avg | -10.9% (all tests) | -9.4% (all tests)

Both weight=2 and weight=3 show significant CPU consumption reduction (~10%) compared to baseline, which addresses the CPU utilization regression observed in QEMU 10.0.0. The throughput impact is minimal for both (~2%).

Weight=3 is selected as the default because it provides slightly better throughput (-2.2% vs -2.4%) while still achieving substantial CPU savings (-9.4%). The difference between weight=2 and weight=3 is small, but weight=3 offers a better balance for general-purpose workloads.

---
  include/qemu/aio.h |   4 +-
  util/aio-posix.c   | 135 +++++++++++++++++++++++++++++++--------------
  util/async.c       |   1 +
  3 files changed, 99 insertions(+), 41 deletions(-)
include/qemu/aio.h +3 -1
index 8cca2360d1..6c77a190e9 100644
@@ -195,7 +195,8 @@ struct BHListSlice {
  typedef QSLIST_HEAD(, AioHandler) AioHandlerSList;
 
  typedef struct AioPolledEvent {
-    int64_t ns;        /* current polling time in nanoseconds */
+    bool has_event; /* Flag to indicate if an event has occurred */
+    int64_t ns;     /* estimated block time in nanoseconds */
  } AioPolledEvent;
 
  struct AioContext {
@@ -306,6 +307,7 @@ struct AioContext {
      int poll_disable_cnt;
 
      /* Polling mode parameters */
+    int64_t poll_ns;        /* current polling time in nanoseconds */
      int64_t poll_max_ns;    /* maximum polling time in nanoseconds */
      int64_t poll_grow;      /* polling time growth factor */
      int64_t poll_shrink;    /* polling time shrink factor */
util/aio-posix.c +4 -2
index b02beb0505..2b3522f2f9 100644
@@ -29,9 +29,11 @@
 
  /* Stop userspace polling on a handler if it isn't active for some time */
  #define POLL_IDLE_INTERVAL_NS (7 * NANOSECONDS_PER_SECOND)
+#define POLL_WEIGHT_SHIFT   (3)
 
-static void adjust_polling_time(AioContext *ctx, AioPolledEvent *poll,
-                                int64_t block_ns);
+static void adjust_block_ns(AioContext *ctx, int64_t block_ns);
+static void grow_polling_time(AioContext *ctx, int64_t block_ns);
+static void shrink_polling_time(AioContext *ctx, int64_t block_ns);
 
Quoted 6 lines

util/aio-posix.c static bool aio_dispatch_ready_handlers(AioContext *ctx, · line 375

Quoted 4 lines
util/aio-posix.c +0 -0
  bool aio_poll_disabled(AioContext *ctx)
  {
@@ -373,7 +375,7 @@ static bool aio_dispatch_ready_handlers(AioContext *ctx,
           * add the handler to ctx->poll_aio_handlers.

This comment refers to adjusting the polling time. The code no longer does this and the comment should be updated.

The comment about adjusting polling time is no longer accurate. I will update it in the next version.

Quoted 5 lines

util/aio-posix.c static bool aio_dispatch_ready_handlers(AioContext *ctx, · line 376

Quoted 3 lines
util/aio-posix.c +0 -1
           */
          if (ctx->poll_max_ns && QLIST_IS_INSERTED(node, node_poll)) {
-            adjust_polling_time(ctx, &node->poll, block_ns);

aio_dispatch_ready_handlers() no longer uses the block_ns argument. It can be removed.

I will remove the block_ns argument in the next version.

Quoted 96 lines
Quoted 63 lines
util/aio-posix.c +12 -28
+            node->poll.has_event = true;
          }
      }
 
@@ -560,18 +562,13 @@ static bool run_poll_handlers(AioContext *ctx, AioHandlerList *ready_list,
  static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list,
                            int64_t *timeout)
  {
-    AioHandler *node;
      int64_t max_ns;
 
      if (QLIST_EMPTY_RCU(&ctx->poll_aio_handlers)) {
          return false;
      }
 
-    max_ns = 0;
-    QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) {
-        max_ns = MAX(max_ns, node->poll.ns);
-    }
-    max_ns = qemu_soonest_timeout(*timeout, max_ns);
+    max_ns = qemu_soonest_timeout(*timeout, ctx->poll_ns);
 
      if (max_ns && !ctx->fdmon_ops->need_wait(ctx)) {
          /*
@@ -587,46 +584,98 @@ static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list,
      return false;
  }
 
-static void adjust_polling_time(AioContext *ctx, AioPolledEvent *poll,
-                                int64_t block_ns)
+static void shrink_polling_time(AioContext *ctx, int64_t block_ns)
  {
-    if (block_ns <= poll->ns) {
-        /* This is the sweet spot, no adjustment needed */
-    } else if (block_ns > ctx->poll_max_ns) {
-        /* We'd have to poll for too long, poll less */
-        int64_t old = poll->ns;
-
-        if (ctx->poll_shrink) {
-            poll->ns /= ctx->poll_shrink;
-        } else {
-            poll->ns = 0;
-        }
+    /*
+     * Reduce polling time if the block_ns is zero or
+     * less than the current poll_ns.
+     */
+    int64_t old = ctx->poll_ns;
+    int64_t shrink = ctx->poll_shrink;
 
-        trace_poll_shrink(ctx, old, poll->ns);
-    } else if (poll->ns < ctx->poll_max_ns &&
-               block_ns < ctx->poll_max_ns) {
-        /* There is room to grow, poll longer */
-        int64_t old = poll->ns;
-        int64_t grow = ctx->poll_grow;
+    if (shrink == 0) {
+        shrink = 2;
+    }
 
-        if (grow == 0) {
-            grow = 2;
-        }

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 604

Quoted 10 lines
util/aio-posix.c +4 -5
+    if (block_ns < (ctx->poll_ns / shrink)) {
+        ctx->poll_ns /= shrink;
+    }
 
-        if (poll->ns) {
-            poll->ns *= grow;
-        } else {
-            poll->ns = 4000; /* start polling at 4 microseconds */
-        }
+    trace_poll_shrink(ctx, old, ctx->poll_ns);

This trace event should be inside if (block_ns < (ctx->poll_ns / shrink)) like it was before this patch.

Quoted 20 lines
util/aio-posix.c +13 -4
+}
 
-        if (poll->ns > ctx->poll_max_ns) {
-            poll->ns = ctx->poll_max_ns;
-        }
+static void grow_polling_time(AioContext *ctx, int64_t block_ns)
+{
+    /* There is room to grow, poll longer */
+    int64_t old = ctx->poll_ns;
+    int64_t grow = ctx->poll_grow;
 
-        trace_poll_grow(ctx, old, poll->ns);
+    if (grow == 0) {
+        grow = 2;
      }
+
+    if (block_ns > ctx->poll_ns * grow) {
+        ctx->poll_ns = block_ns;
+    } else {
+        ctx->poll_ns *= grow;
Quoted 8 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 627

Quoted 7 lines
util/aio-posix.c +7 -0
+    }
+
+    if (ctx->poll_ns > ctx->poll_max_ns) {
+        ctx->poll_ns = ctx->poll_max_ns;
+    }
+
+    trace_poll_grow(ctx, old, ctx->poll_ns);

Same here.

I will move the trace_poll_xxx functions inside if condition in the next version.

Quoted 14 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 636

Quoted 10 lines
util/aio-posix.c +7 -0
  }
 
+static void adjust_block_ns(AioContext *ctx, int64_t block_ns)
+{
+    AioHandler *node;
+    int64_t adj_block_ns = -1;
+
+    QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) {
+        if (node->poll.has_event) {

Did you consider unifying node->poll.has_event with node->poll_idle_timeout, which is assigned now + POLL_IDLE_INTERVAL_NS every time ->io_poll() detects an event?

Quoted 4 lines

For instance, rename node->poll_idle_timeout to node->last_event_timestamp and assign now without adding POLL_IDLE_INTERVAL_NS. Then use the field for both idle node removal and adjust_block_ns() (pass in now).

Thank you for the suggestion, I think this is a good idea.
After testing, it seems that node->poll_idle_timeout can be reused as you suggested, although a few adjustment are needed.

currently, an event is detected and the AioHandler is added to the ready_list in three cases: run_poll_handler_once(), ctx->fdmon_ops-wait(), and poll_set_started().

To accurately track the last event timestamp of an AioHandler, it seems necessary to update the timestamp in the following two functions:

@@ -45,6 +45,7 @@ void aio_add_ready_handler(AioHandlerList *ready_list, {
     QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's list */ node->pfd.revents = revents;
+    node->poll_idle_timeout = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); QLIST_INSERT_HEAD(ready_list, node, node_ready);
 }

@@ -53,6 +54,7 @@ static void aio_add_poll_ready_handler(AioHandlerList *ready_list, {
     QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's list */ node->poll_ready = true;
+    node->poll_idle_timeout = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); QLIST_INSERT_HEAD(ready_list, node, node_ready);
 }

In addition, remove_idle_poll_handler() would need some adjustments as well. If this approach aligns with you had in mind, I believe it can be incorporated in the next version without any issues.

Quoted 23 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 648

Quoted 12 lines
util/aio-posix.c +12 -0
+            /*
+             * Update poll.ns for the node with an event.
+             * Uses a weighted average of the current block_ns and the previous
+             * poll.ns to smooth out polling time adjustments.
+             */
+            node->poll.ns = node->poll.ns
+                ? (node->poll.ns - (node->poll.ns >> POLL_WEIGHT_SHIFT))
+                + (block_ns >> POLL_WEIGHT_SHIFT) : block_ns;
+
+            if (node->poll.ns > ctx->poll_max_ns) {
+                node->poll.ns = 0;
+            }

Previously:

util/aio-posix.c +0 -3
-        if (poll->ns > ctx->poll_max_ns) {
-            poll->ns = ctx->poll_max_ns;
-        }

Was this causing excessive CPU consumption in your benchmarks?

Can you explain the rationale for zeroing the poll time? Aside from reducing CPU consumption, it also reduces the chance that polling will succeed and could therefore impact performance.

Quoted 5 lines

I'm asking about this because this patch makes several changes at once and I'm not sure how the CPU usage and performance changes are attributed to these multiple changes. I want to make sure the changes merged are minimal and the best set - sometimes when multiple things are changes at the same time, not all of them are beneficial.

This snippet you quoted under "Previously" is also reflected in the logic within grow_polling_time() where a same approach is used.

The difference is that previously. all AioHandler in the reay_list would update their own poll.ns using block_ns. As in the snippet below, if block_ns exceeded poll_max_ns, it would effectively be reset to 0 anyway.

-    } else if (block_ns > ctx->poll_max_ns) { -        /* We'd have to poll for too long, poll less */ -        int64_t old = poll->ns;
-
-        if (ctx->poll_shrink) {
-            poll->ns /= ctx->poll_shrink;
-        } else {
-            poll->ns = 0;
-        }

I think I did not explain this part clearly enough in the commit message. Here’s a more detailed explanation of the current polling logic problem and approach:

Problem:
Starting from QEMU 10.0, poll.ns was introduced per event handler to mitigate excessive fluctuations in IOThread polling times observed in earlier versions (QEMU 9.x).

However, in the current design, poll.ns is updated only when an event occurs, making it difficult to treat block_ns as a reliable event interval. Also, The IOThread’s next polling time is determined by the maximum poll.ns among all AioHandlers, which means idle AioHandlers with high poll.ns can have an outsized impact on the polling duration.

For io_uring, idle AioHandlers are cleared after POLL_IDLE_INTERVAL_NS (7s), but ppoll/epoll, there is no such mechanism, so CPU consumption due to idle nodes can increase even more.

Approach:
To address this, we treat block_ns as an event interval and update each AioHandler’s poll.ns using a weighted factor. This smooths out polling time adjustments, preventing excessive fluctuations and ensuring that recent event intervals are properly reflected, which helps maintain performance while lowering CPU utilization.

To use block_ns as an event interval, we update polling times for both event and non-event AioHandlers in each loop iteration. Non-event AioHandler do not require a weighted factor; this allows for rapid isolation of idle nodes, while ensuring that poll.ns can increase more responsively when an event occurs within a few subsequent loops.

Quoted 7 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 653

Quoted 5 lines
util/aio-posix.c +5 -0
+            /*
+             * To avoid excessive polling time increase, update adj_block_ns
+             * for nodes with the event flag set to true
+             */
+            adj_block_ns = MAX(adj_block_ns, node->poll.ns);

adj_block_ns is not the blocking time, it's the maximum current poll time across all nodes. It would be clearer to change the variable name.

You're right. I will rename it to max_poll_ns to better reflect its purpose.

Quoted 3 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 655

Quoted 2 lines
util/aio-posix.c +2 -0
+            node->poll.has_event = false;
+         } else {

4-space indentation should be used.

I will also fix the indentation.

Quoted 76 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 662

Quoted 8 lines
util/aio-posix.c +7 -0
+            /*
+             * No event now, but was active before.
+             * If it waits longer than poll_max_ns, poll.ns will stay 0
+             * until the next event arrives.
+             */
+            if (node->poll.ns != 0) {
+                node->poll.ns += block_ns;

Why is block_ns being added to an recently inactive node's polling time? Here node->poll.ns no longer measures the weighted time until the handler had an event.

If the goal is to get rid of inactive nodes, then maybe the idle handler removal mechanism should be made more aggresive instead?

Quoted 61 lines
util/aio-posix.c +22 -0
+                if (node->poll.ns > ctx->poll_max_ns) {
+                    node->poll.ns = 0;
+                }
+            }
+        }
+    }
+
+    if (adj_block_ns >= 0) {
+        if (adj_block_ns > ctx->poll_ns) {
+            grow_polling_time(ctx, adj_block_ns);
+        } else {
+            shrink_polling_time(ctx, adj_block_ns);
+         }
+     }
+ }
+
  bool aio_poll(AioContext *ctx, bool blocking)
  {
      AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list);
@@ -723,6 +772,10 @@ bool aio_poll(AioContext *ctx, bool blocking)
 
      aio_free_deleted_handlers(ctx);
 
+    if (ctx->poll_max_ns) {
+        adjust_block_ns(ctx, block_ns);
+    }
+
      qemu_lockcnt_dec(&ctx->list_lock);
 
      progress |= timerlistgroup_run_timers(&ctx->tlg);
@@ -784,6 +837,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
 
      qemu_lockcnt_inc(&ctx->list_lock);
      QLIST_FOREACH(node, &ctx->aio_handlers, node) {
+        node->poll.has_event = false;
          node->poll.ns = 0;
      }
      qemu_lockcnt_dec(&ctx->list_lock);
@@ -794,6 +848,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
      ctx->poll_max_ns = max_ns;
      ctx->poll_grow = grow;
      ctx->poll_shrink = shrink;
+    ctx->poll_ns = 0;
 
      aio_notify(ctx);
  }
util/async.c +1 -0
index 80d6b01a8a..9d3627566f 100644
@@ -606,6 +606,7 @@ AioContext *aio_context_new(Error **errp)
      timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
 
      ctx->poll_max_ns = 0;
+    ctx->poll_ns = 0;
      ctx->poll_grow = 0;
      ctx->poll_shrink = 0;
 
Signature · 1 line
2.50.1
#8 lore raw

Re: [PATCH RFC v2 2/3] aio-poll: refine iothread polling using weighted handler intervals

Quoted 241 lines · JAEHOON KIM
Quoted 78 lines · Stefan Hajnoczi
Quoted 77 lines · Jaehoon Kim

Refine adaptive polling in aio_poll by updating iothread polling duration based on weighted AioHandler event intervals.

Each AioHandler's poll.ns is updated using a weighted factor when an event occurs. Idle handlers accumulate block_ns until poll_max_ns and then reset to 0, preventing sporadically active handlers from unnecessarily prolonging iothread polling.

The iothread polling duration is set based on the largest poll.ns among active handlers. The shrink divider defaults to 2, matching the grow rate, to reduce frequent poll_ns resets for slow devices.

The default weight factor (POLL_WEIGHT_SHIFT=3, meaning the current interval contributes 12.5% to the weighted average) was selected based on extensive testing comparing QEMU 10.0.0 baseline vs poll-weight=2 and poll-weight=3 across various workloads.

The table below shows a comparison between:

(quoted diff) +0 -2
-Host: RHEL 10.1 GA + qemu-10.0.0-14.el10_1, Guest: RHEL 9.6GA vs.
-Host: RHEL 10.1 GA + qemu-10.0.0-14.el10_1 (w=2/w=3), Guest: RHEL 9.6GA

for FIO FCP and FICON with 1 iothread and 8 iothreads. The values shown are the averages for numjobs 1, 4, and 8.

Summary of results (% change vs baseline):

                     | poll-weight=2      | poll-weight=3
--------------------|--------------------|-----------------
Throughput avg      | -2.4% (all tests)  | -2.2% (all tests)
CPU consumption avg | -10.9% (all tests) | -9.4% (all tests)

Both weight=2 and weight=3 show significant CPU consumption reduction (~10%) compared to baseline, which addresses the CPU utilization regression observed in QEMU 10.0.0. The throughput impact is minimal for both (~2%).

Weight=3 is selected as the default because it provides slightly better throughput (-2.2% vs -2.4%) while still achieving substantial CPU savings (-9.4%). The difference between weight=2 and weight=3 is small, but weight=3 offers a better balance for general-purpose workloads.

---
  include/qemu/aio.h |   4 +-
  util/aio-posix.c   | 135 +++++++++++++++++++++++++++++++--------------
  util/async.c       |   1 +
  3 files changed, 99 insertions(+), 41 deletions(-)
include/qemu/aio.h +3 -1
index 8cca2360d1..6c77a190e9 100644
@@ -195,7 +195,8 @@ struct BHListSlice {
  typedef QSLIST_HEAD(, AioHandler) AioHandlerSList;
  typedef struct AioPolledEvent {
-    int64_t ns;        /* current polling time in nanoseconds */
+    bool has_event; /* Flag to indicate if an event has occurred */
+    int64_t ns;     /* estimated block time in nanoseconds */
  } AioPolledEvent;
  struct AioContext {
@@ -306,6 +307,7 @@ struct AioContext {
      int poll_disable_cnt;
      /* Polling mode parameters */
+    int64_t poll_ns;        /* current polling time in nanoseconds */
      int64_t poll_max_ns;    /* maximum polling time in nanoseconds */
      int64_t poll_grow;      /* polling time growth factor */
      int64_t poll_shrink;    /* polling time shrink factor */
util/aio-posix.c +2 -2
index b02beb0505..2b3522f2f9 100644
@@ -29,9 +29,11 @@
  /* Stop userspace polling on a handler if it isn't active for some time */
  #define POLL_IDLE_INTERVAL_NS (7 * NANOSECONDS_PER_SECOND)
+#define POLL_WEIGHT_SHIFT   (3)
-static void adjust_polling_time(AioContext *ctx, AioPolledEvent *poll,
-                                int64_t block_ns);
+static void adjust_block_ns(AioContext *ctx, int64_t block_ns);
Quoted 8 lines

util/aio-posix.c static bool aio_dispatch_ready_handlers(AioContext *ctx, · line 375

Quoted 6 lines
util/aio-posix.c +2 -0
+static void grow_polling_time(AioContext *ctx, int64_t block_ns);
+static void shrink_polling_time(AioContext *ctx, int64_t block_ns);
  bool aio_poll_disabled(AioContext *ctx)
  {
@@ -373,7 +375,7 @@ static bool aio_dispatch_ready_handlers(AioContext *ctx,
           * add the handler to ctx->poll_aio_handlers.

This comment refers to adjusting the polling time. The code no longer does this and the comment should be updated.

The comment about adjusting polling time is no longer accurate. I will update it in the next version.

Quoted 5 lines

util/aio-posix.c static bool aio_dispatch_ready_handlers(AioContext *ctx, · line 376

Quoted 3 lines
util/aio-posix.c +0 -1
           */
          if (ctx->poll_max_ns && QLIST_IS_INSERTED(node, node_poll)) {
-            adjust_polling_time(ctx, &node->poll, block_ns);

aio_dispatch_ready_handlers() no longer uses the block_ns argument. It can be removed.

I will remove the block_ns argument in the next version.

Quoted 86 lines
Quoted 55 lines
util/aio-posix.c +12 -27
+            node->poll.has_event = true;
          }
      }
@@ -560,18 +562,13 @@ static bool run_poll_handlers(AioContext *ctx, AioHandlerList *ready_list,
  static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list,
                            int64_t *timeout)
  {
-    AioHandler *node;
      int64_t max_ns;
      if (QLIST_EMPTY_RCU(&ctx->poll_aio_handlers)) {
          return false;
      }
-    max_ns = 0;
-    QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) {
-        max_ns = MAX(max_ns, node->poll.ns);
-    }
-    max_ns = qemu_soonest_timeout(*timeout, max_ns);
+    max_ns = qemu_soonest_timeout(*timeout, ctx->poll_ns);
      if (max_ns && !ctx->fdmon_ops->need_wait(ctx)) {
          /*
@@ -587,46 +584,98 @@ static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list,
      return false;
  }
-static void adjust_polling_time(AioContext *ctx, AioPolledEvent *poll,
-                                int64_t block_ns)
+static void shrink_polling_time(AioContext *ctx, int64_t block_ns)
  {
-    if (block_ns <= poll->ns) {
-        /* This is the sweet spot, no adjustment needed */
-    } else if (block_ns > ctx->poll_max_ns) {
-        /* We'd have to poll for too long, poll less */
-        int64_t old = poll->ns;
-
-        if (ctx->poll_shrink) {
-            poll->ns /= ctx->poll_shrink;
-        } else {
-            poll->ns = 0;
-        }
+    /*
+     * Reduce polling time if the block_ns is zero or
+     * less than the current poll_ns.
+     */
+    int64_t old = ctx->poll_ns;
+    int64_t shrink = ctx->poll_shrink;
-        trace_poll_shrink(ctx, old, poll->ns);
-    } else if (poll->ns < ctx->poll_max_ns &&
-               block_ns < ctx->poll_max_ns) {
-        /* There is room to grow, poll longer */
-        int64_t old = poll->ns;
-        int64_t grow = ctx->poll_grow;
+    if (shrink == 0) {
+        shrink = 2;
+    }
-        if (grow == 0) {
-            grow = 2;

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 600

Quoted 10 lines
util/aio-posix.c +4 -6
-        }
+    if (block_ns < (ctx->poll_ns / shrink)) {
+        ctx->poll_ns /= shrink;
+    }
-        if (poll->ns) {
-            poll->ns *= grow;
-        } else {
-            poll->ns = 4000; /* start polling at 4 microseconds */
-        }
+    trace_poll_shrink(ctx, old, ctx->poll_ns);

This trace event should be inside if (block_ns < (ctx->poll_ns / shrink)) like it was before this patch.

Quoted 18 lines
util/aio-posix.c +13 -4
+}
-        if (poll->ns > ctx->poll_max_ns) {
-            poll->ns = ctx->poll_max_ns;
-        }
+static void grow_polling_time(AioContext *ctx, int64_t block_ns)
+{
+    /* There is room to grow, poll longer */
+    int64_t old = ctx->poll_ns;
+    int64_t grow = ctx->poll_grow;
-        trace_poll_grow(ctx, old, poll->ns);
+    if (grow == 0) {
+        grow = 2;
      }
+
+    if (block_ns > ctx->poll_ns * grow) {
+        ctx->poll_ns = block_ns;
+    } else {
+        ctx->poll_ns *= grow;
Quoted 8 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 627

Quoted 7 lines
util/aio-posix.c +7 -0
+    }
+
+    if (ctx->poll_ns > ctx->poll_max_ns) {
+        ctx->poll_ns = ctx->poll_max_ns;
+    }
+
+    trace_poll_grow(ctx, old, ctx->poll_ns);

Same here.

I will move the trace_poll_xxx functions inside if condition in the next version.

Quoted 13 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 636

Quoted 9 lines
util/aio-posix.c +7 -0
  }
+static void adjust_block_ns(AioContext *ctx, int64_t block_ns)
+{
+    AioHandler *node;
+    int64_t adj_block_ns = -1;
+
+    QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) {
+        if (node->poll.has_event) {

Did you consider unifying node->poll.has_event with node->poll_idle_timeout, which is assigned now + POLL_IDLE_INTERVAL_NS every time ->io_poll() detects an event?

Quoted 4 lines

For instance, rename node->poll_idle_timeout to node->last_event_timestamp and assign now without adding POLL_IDLE_INTERVAL_NS. Then use the field for both idle node removal and adjust_block_ns() (pass in now).

Thank you for the suggestion, I think this is a good idea. After testing, it seems that node->poll_idle_timeout can be reused as you suggested,
although a few adjustment are needed.

currently, an event is detected and the AioHandler is added to the ready_list in
three cases: run_poll_handler_once(), ctx->fdmon_ops-wait(), and poll_set_started().

To accurately track the last event timestamp of an AioHandler, it seems necessary to
update the timestamp in the following two functions:

(quoted diff) +0 -0
@@ -45,6 +45,7 @@ void aio_add_ready_handler(AioHandlerList *ready_list,
  {

     QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's list */
     node->pfd.revents = revents;
+    node->poll_idle_timeout = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); QLIST_INSERT_HEAD(ready_list, node, node_ready);
 }

(quoted diff) +0 -0
@@ -53,6 +54,7 @@ static void aio_add_poll_ready_handler(AioHandlerList
 *ready_list,
  {

     QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's list */
     node->poll_ready = true;
+    node->poll_idle_timeout = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); QLIST_INSERT_HEAD(ready_list, node, node_ready);
 }

Quoted 4 lines

In addition, remove_idle_poll_handler() would need some adjustments as well. If this approach aligns with you had in mind, I believe it can be incorporated in
the next version without any issues.

That works but I worry a bit about calling qemu_clock_get_ns() for every event. The timestamp does not need to be precise down to the nano-, micro-, or event milli-second. I think you could instead pass 'now' into aio_dispatch_ready_handlers() and assign the new node->last_dispatch_timestamp field there.

Quoted 80 lines
Quoted 23 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 648

Quoted 12 lines
util/aio-posix.c +12 -0
+            /*
+             * Update poll.ns for the node with an event.
+             * Uses a weighted average of the current block_ns and the previous
+             * poll.ns to smooth out polling time adjustments.
+             */
+            node->poll.ns = node->poll.ns
+                ? (node->poll.ns - (node->poll.ns >> POLL_WEIGHT_SHIFT))
+                + (block_ns >> POLL_WEIGHT_SHIFT) : block_ns;
+
+            if (node->poll.ns > ctx->poll_max_ns) {
+                node->poll.ns = 0;
+            }

Previously:

util/aio-posix.c +0 -3
-        if (poll->ns > ctx->poll_max_ns) {
-            poll->ns = ctx->poll_max_ns;
-        }

Was this causing excessive CPU consumption in your benchmarks?

Can you explain the rationale for zeroing the poll time? Aside from reducing CPU consumption, it also reduces the chance that polling will succeed and could therefore impact performance.

Quoted 5 lines

I'm asking about this because this patch makes several changes at once and I'm not sure how the CPU usage and performance changes are attributed to these multiple changes. I want to make sure the changes merged are minimal and the best set - sometimes when multiple things are changes at the same time, not all of them are beneficial.

This snippet you quoted under "Previously" is also reflected in the logic within grow_polling_time() where a same approach is used.

The difference is that previously. all AioHandler in the reay_list would update their own poll.ns using block_ns. As in the snippet below, if block_ns exceeded poll_max_ns, it would effectively be reset to 0 anyway.

-    } else if (block_ns > ctx->poll_max_ns) { -        /* We'd have to poll for too long, poll less */ -        int64_t old = poll->ns;
-
-        if (ctx->poll_shrink) {
-            poll->ns /= ctx->poll_shrink;
-        } else {
-            poll->ns = 0;
-        }

I think I did not explain this part clearly enough in the commit message. Here’s a more detailed explanation of the current polling logic problem and approach:

Problem:
Starting from QEMU 10.0, poll.ns was introduced per event handler to mitigate excessive
fluctuations in IOThread polling times observed in earlier versions (QEMU 9.x).

However, in the current design, poll.ns is updated only when an event occurs, making it
difficult to treat block_ns as a reliable event interval. Also, The IOThread’s next
polling time is determined by the maximum poll.ns among all AioHandlers, which means
idle AioHandlers with high poll.ns can have an outsized impact on the polling duration.

For io_uring, idle AioHandlers are cleared after POLL_IDLE_INTERVAL_NS (7s), but
ppoll/epoll, there is no such mechanism, so CPU consumption due to idle nodes can
increase even more.

Approach:
To address this, we treat block_ns as an event interval and update each AioHandler’s
poll.ns using a weighted factor. This smooths out polling time adjustments, preventing
excessive fluctuations and ensuring that recent event intervals are properly reflected,
which helps maintain performance while lowering CPU utilization.

Quoted 7 lines

To use block_ns as an event interval, we update polling times for both event and non-event AioHandlers in each loop iteration. Non-event AioHandler do not require
a weighted factor; this allows for rapid isolation of idle nodes, while ensuring that
poll.ns can increase more responsively when an event occurs within a few subsequent loops.

Thanks for this information. Please include it in the commit description.

Quoted 82 lines
Quoted 7 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 653

Quoted 5 lines
util/aio-posix.c +5 -0
+            /*
+             * To avoid excessive polling time increase, update adj_block_ns
+             * for nodes with the event flag set to true
+             */
+            adj_block_ns = MAX(adj_block_ns, node->poll.ns);

adj_block_ns is not the blocking time, it's the maximum current poll time across all nodes. It would be clearer to change the variable name.

You're right. I will rename it to max_poll_ns to better reflect its purpose.

Quoted 3 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 648

Quoted 2 lines
util/aio-posix.c +2 -0
+            node->poll.has_event = false;
+         } else {

4-space indentation should be used.

I will also fix the indentation.

Quoted 69 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 662

Quoted 8 lines
util/aio-posix.c +7 -0
+            /*
+             * No event now, but was active before.
+             * If it waits longer than poll_max_ns, poll.ns will stay 0
+             * until the next event arrives.
+             */
+            if (node->poll.ns != 0) {
+                node->poll.ns += block_ns;

Why is block_ns being added to an recently inactive node's polling time? Here node->poll.ns no longer measures the weighted time until the handler had an event.

If the goal is to get rid of inactive nodes, then maybe the idle handler removal mechanism should be made more aggresive instead?

Quoted 54 lines
util/aio-posix.c +22 -0
+                if (node->poll.ns > ctx->poll_max_ns) {
+                    node->poll.ns = 0;
+                }
+            }
+        }
+    }
+
+    if (adj_block_ns >= 0) {
+        if (adj_block_ns > ctx->poll_ns) {
+            grow_polling_time(ctx, adj_block_ns);
+        } else {
+            shrink_polling_time(ctx, adj_block_ns);
+         }
+     }
+ }
+
  bool aio_poll(AioContext *ctx, bool blocking)
  {
      AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list);
@@ -723,6 +772,10 @@ bool aio_poll(AioContext *ctx, bool blocking)
      aio_free_deleted_handlers(ctx);
+    if (ctx->poll_max_ns) {
+        adjust_block_ns(ctx, block_ns);
+    }
+
      qemu_lockcnt_dec(&ctx->list_lock);
      progress |= timerlistgroup_run_timers(&ctx->tlg);
@@ -784,6 +837,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
      qemu_lockcnt_inc(&ctx->list_lock);
      QLIST_FOREACH(node, &ctx->aio_handlers, node) {
+        node->poll.has_event = false;
          node->poll.ns = 0;
      }
      qemu_lockcnt_dec(&ctx->list_lock);
@@ -794,6 +848,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
      ctx->poll_max_ns = max_ns;
      ctx->poll_grow = grow;
      ctx->poll_shrink = shrink;
+    ctx->poll_ns = 0;
      aio_notify(ctx);
  }
util/async.c +1 -0
index 80d6b01a8a..9d3627566f 100644
@@ -606,6 +606,7 @@ AioContext *aio_context_new(Error **errp)
      timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
      ctx->poll_max_ns = 0;
+    ctx->poll_ns = 0;
      ctx->poll_grow = 0;
      ctx->poll_shrink = 0;
Signature · 1 line
2.50.1
#9 lore raw

Re: [PATCH RFC v2 2/3] aio-poll: refine iothread polling using weighted handler intervals

Quoted 241 lines · Stefan Hajnoczi
Quoted 240 lines · JAEHOON KIM
Quoted 78 lines · Stefan Hajnoczi
Quoted 77 lines · Jaehoon Kim

Refine adaptive polling in aio_poll by updating iothread polling duration based on weighted AioHandler event intervals.

Each AioHandler's poll.ns is updated using a weighted factor when an event occurs. Idle handlers accumulate block_ns until poll_max_ns and then reset to 0, preventing sporadically active handlers from unnecessarily prolonging iothread polling.

The iothread polling duration is set based on the largest poll.ns among active handlers. The shrink divider defaults to 2, matching the grow rate, to reduce frequent poll_ns resets for slow devices.

The default weight factor (POLL_WEIGHT_SHIFT=3, meaning the current interval contributes 12.5% to the weighted average) was selected based on extensive testing comparing QEMU 10.0.0 baseline vs poll-weight=2 and poll-weight=3 across various workloads.

The table below shows a comparison between:

(quoted diff) +0 -2
-Host: RHEL 10.1 GA + qemu-10.0.0-14.el10_1, Guest: RHEL 9.6GA vs.
-Host: RHEL 10.1 GA + qemu-10.0.0-14.el10_1 (w=2/w=3), Guest: RHEL 9.6GA

for FIO FCP and FICON with 1 iothread and 8 iothreads. The values shown are the averages for numjobs 1, 4, and 8.

Summary of results (% change vs baseline):

                      | poll-weight=2      | poll-weight=3
--------------------|--------------------|-----------------
Throughput avg      | -2.4% (all tests)  | -2.2% (all tests)
CPU consumption avg | -10.9% (all tests) | -9.4% (all tests)

Both weight=2 and weight=3 show significant CPU consumption reduction (~10%) compared to baseline, which addresses the CPU utilization regression observed in QEMU 10.0.0. The throughput impact is minimal for both (~2%).

Weight=3 is selected as the default because it provides slightly better throughput (-2.2% vs -2.4%) while still achieving substantial CPU savings (-9.4%). The difference between weight=2 and weight=3 is small, but weight=3 offers a better balance for general-purpose workloads.

---
   include/qemu/aio.h |   4 +-
   util/aio-posix.c   | 135 +++++++++++++++++++++++++++++++--------------
   util/async.c       |   1 +
   3 files changed, 99 insertions(+), 41 deletions(-)
include/qemu/aio.h +3 -1
index 8cca2360d1..6c77a190e9 100644
@@ -195,7 +195,8 @@ struct BHListSlice {
   typedef QSLIST_HEAD(, AioHandler) AioHandlerSList;
   typedef struct AioPolledEvent {
-    int64_t ns;        /* current polling time in nanoseconds */
+    bool has_event; /* Flag to indicate if an event has occurred */
+    int64_t ns;     /* estimated block time in nanoseconds */
   } AioPolledEvent;
   struct AioContext {
@@ -306,6 +307,7 @@ struct AioContext {
       int poll_disable_cnt;
       /* Polling mode parameters */
+    int64_t poll_ns;        /* current polling time in nanoseconds */
       int64_t poll_max_ns;    /* maximum polling time in nanoseconds */
       int64_t poll_grow;      /* polling time growth factor */
       int64_t poll_shrink;    /* polling time shrink factor */
util/aio-posix.c +2 -2
index b02beb0505..2b3522f2f9 100644
@@ -29,9 +29,11 @@
   /* Stop userspace polling on a handler if it isn't active for some time */
   #define POLL_IDLE_INTERVAL_NS (7 * NANOSECONDS_PER_SECOND)
+#define POLL_WEIGHT_SHIFT   (3)
-static void adjust_polling_time(AioContext *ctx, AioPolledEvent *poll,
-                                int64_t block_ns);
+static void adjust_block_ns(AioContext *ctx, int64_t block_ns);
Quoted 8 lines

util/aio-posix.c static bool aio_dispatch_ready_handlers(AioContext *ctx, · line 375

Quoted 6 lines
util/aio-posix.c +2 -0
+static void grow_polling_time(AioContext *ctx, int64_t block_ns);
+static void shrink_polling_time(AioContext *ctx, int64_t block_ns);
   bool aio_poll_disabled(AioContext *ctx)
   {
@@ -373,7 +375,7 @@ static bool aio_dispatch_ready_handlers(AioContext *ctx,
            * add the handler to ctx->poll_aio_handlers.

This comment refers to adjusting the polling time. The code no longer does this and the comment should be updated.

The comment about adjusting polling time is no longer accurate. I will update it in the next version.

Quoted 5 lines

util/aio-posix.c static bool aio_dispatch_ready_handlers(AioContext *ctx, · line 376

Quoted 3 lines
util/aio-posix.c +0 -1
            */
           if (ctx->poll_max_ns && QLIST_IS_INSERTED(node, node_poll)) {
-            adjust_polling_time(ctx, &node->poll, block_ns);

aio_dispatch_ready_handlers() no longer uses the block_ns argument. It can be removed.

I will remove the block_ns argument in the next version.

Quoted 86 lines
Quoted 55 lines
util/aio-posix.c +12 -27
+            node->poll.has_event = true;
           }
       }
@@ -560,18 +562,13 @@ static bool run_poll_handlers(AioContext *ctx, AioHandlerList *ready_list,
   static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list,
                             int64_t *timeout)
   {
-    AioHandler *node;
       int64_t max_ns;
       if (QLIST_EMPTY_RCU(&ctx->poll_aio_handlers)) {
           return false;
       }
-    max_ns = 0;
-    QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) {
-        max_ns = MAX(max_ns, node->poll.ns);
-    }
-    max_ns = qemu_soonest_timeout(*timeout, max_ns);
+    max_ns = qemu_soonest_timeout(*timeout, ctx->poll_ns);
       if (max_ns && !ctx->fdmon_ops->need_wait(ctx)) {
           /*
@@ -587,46 +584,98 @@ static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list,
       return false;
   }
-static void adjust_polling_time(AioContext *ctx, AioPolledEvent *poll,
-                                int64_t block_ns)
+static void shrink_polling_time(AioContext *ctx, int64_t block_ns)
   {
-    if (block_ns <= poll->ns) {
-        /* This is the sweet spot, no adjustment needed */
-    } else if (block_ns > ctx->poll_max_ns) {
-        /* We'd have to poll for too long, poll less */
-        int64_t old = poll->ns;
-
-        if (ctx->poll_shrink) {
-            poll->ns /= ctx->poll_shrink;
-        } else {
-            poll->ns = 0;
-        }
+    /*
+     * Reduce polling time if the block_ns is zero or
+     * less than the current poll_ns.
+     */
+    int64_t old = ctx->poll_ns;
+    int64_t shrink = ctx->poll_shrink;
-        trace_poll_shrink(ctx, old, poll->ns);
-    } else if (poll->ns < ctx->poll_max_ns &&
-               block_ns < ctx->poll_max_ns) {
-        /* There is room to grow, poll longer */
-        int64_t old = poll->ns;
-        int64_t grow = ctx->poll_grow;
+    if (shrink == 0) {
+        shrink = 2;
+    }
-        if (grow == 0) {
-            grow = 2;

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 600

Quoted 10 lines
util/aio-posix.c +4 -6
-        }
+    if (block_ns < (ctx->poll_ns / shrink)) {
+        ctx->poll_ns /= shrink;
+    }
-        if (poll->ns) {
-            poll->ns *= grow;
-        } else {
-            poll->ns = 4000; /* start polling at 4 microseconds */
-        }
+    trace_poll_shrink(ctx, old, ctx->poll_ns);

This trace event should be inside if (block_ns < (ctx->poll_ns / shrink)) like it was before this patch.

Quoted 18 lines
util/aio-posix.c +13 -4
+}
-        if (poll->ns > ctx->poll_max_ns) {
-            poll->ns = ctx->poll_max_ns;
-        }
+static void grow_polling_time(AioContext *ctx, int64_t block_ns)
+{
+    /* There is room to grow, poll longer */
+    int64_t old = ctx->poll_ns;
+    int64_t grow = ctx->poll_grow;
-        trace_poll_grow(ctx, old, poll->ns);
+    if (grow == 0) {
+        grow = 2;
       }
+
+    if (block_ns > ctx->poll_ns * grow) {
+        ctx->poll_ns = block_ns;
+    } else {
+        ctx->poll_ns *= grow;
Quoted 8 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 627

Quoted 7 lines
util/aio-posix.c +7 -0
+    }
+
+    if (ctx->poll_ns > ctx->poll_max_ns) {
+        ctx->poll_ns = ctx->poll_max_ns;
+    }
+
+    trace_poll_grow(ctx, old, ctx->poll_ns);

Same here.

I will move the trace_poll_xxx functions inside if condition in the next version.

Quoted 12 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 636

Quoted 8 lines
util/aio-posix.c +7 -0
   }
+static void adjust_block_ns(AioContext *ctx, int64_t block_ns)
+{
+    AioHandler *node;
+    int64_t adj_block_ns = -1;
+
+    QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) {
+        if (node->poll.has_event) {

Did you consider unifying node->poll.has_event with node->poll_idle_timeout, which is assigned now + POLL_IDLE_INTERVAL_NS every time ->io_poll() detects an event?

Quoted 4 lines

For instance, rename node->poll_idle_timeout to node->last_event_timestamp and assign now without adding POLL_IDLE_INTERVAL_NS. Then use the field for both idle node removal and adjust_block_ns() (pass in now).

Thank you for the suggestion, I think this is a good idea. After testing, it seems that node->poll_idle_timeout can be reused as you suggested,
although a few adjustment are needed.

currently, an event is detected and the AioHandler is added to the ready_list in
three cases: run_poll_handler_once(), ctx->fdmon_ops-wait(), and poll_set_started().

To accurately track the last event timestamp of an AioHandler, it seems necessary to
update the timestamp in the following two functions:

(quoted diff) +0 -0
@@ -45,6 +45,7 @@ void aio_add_ready_handler(AioHandlerList *ready_list,
  {
      QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's

list */
     node->pfd.revents = revents;
+    node->poll_idle_timeout = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
     QLIST_INSERT_HEAD(ready_list, node, node_ready);
 }

(quoted diff) +0 -0
@@ -53,6 +54,7 @@ static void aio_add_poll_ready_handler(AioHandlerList
 *ready_list,
  {
      QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's

list */
     node->poll_ready = true;
+    node->poll_idle_timeout = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
     QLIST_INSERT_HEAD(ready_list, node, node_ready);
 }

Quoted 9 lines
Quoted 4 lines

In addition, remove_idle_poll_handler() would need some adjustments as well. If this approach aligns with you had in mind, I believe it can be incorporated in
the next version without any issues.

That works but I worry a bit about calling qemu_clock_get_ns() for every event. The timestamp does not need to be precise down to the nano-, micro-, or event milli-second. I think you could instead pass 'now' into aio_dispatch_ready_handlers() and assign the new node->last_dispatch_timestamp field there.

Thanks for the suggestion, that makes sense.
You're right that calling qemu_clock_get_ns() on every event could introduce unnecessary overhead, especially since we don't require high precision here. I'll update the code accordingly.

Quoted 80 lines
Quoted 80 lines
Quoted 24 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 648

Quoted 13 lines
util/aio-posix.c +12 -0
+            /*
+             * Update poll.ns for the node with an event.
+             * Uses a weighted average of the current block_ns and the previous
+             * poll.ns to smooth out polling time adjustments.
+             */
+            node->poll.ns = node->poll.ns
+                ? (node->poll.ns - (node->poll.ns >> POLL_WEIGHT_SHIFT))
+                + (block_ns >> POLL_WEIGHT_SHIFT) : block_ns;
+
+            if (node->poll.ns > ctx->poll_max_ns) {
+                node->poll.ns = 0;
+            }

Previously:

util/aio-posix.c +0 -3
-        if (poll->ns > ctx->poll_max_ns) {
-            poll->ns = ctx->poll_max_ns;
-        }

Was this causing excessive CPU consumption in your benchmarks?

Can you explain the rationale for zeroing the poll time? Aside from reducing CPU consumption, it also reduces the chance that polling will succeed and could therefore impact performance.

Quoted 5 lines

I'm asking about this because this patch makes several changes at once and I'm not sure how the CPU usage and performance changes are attributed to these multiple changes. I want to make sure the changes merged are minimal and the best set - sometimes when multiple things are changes at the same time, not all of them are beneficial.

This snippet you quoted under "Previously" is also reflected in the logic within grow_polling_time() where a same approach is used.

The difference is that previously. all AioHandler in the reay_list would update their own poll.ns using block_ns. As in the snippet below, if block_ns exceeded poll_max_ns, it would effectively be reset to 0 anyway.

-    } else if (block_ns > ctx->poll_max_ns) { -        /* We'd have to poll for too long, poll less */ -        int64_t old = poll->ns;
-
-        if (ctx->poll_shrink) {
-            poll->ns /= ctx->poll_shrink;
-        } else {
-            poll->ns = 0;
-        }

I think I did not explain this part clearly enough in the commit message. Here’s a more detailed explanation of the current polling logic problem and approach:

Problem:
Starting from QEMU 10.0, poll.ns was introduced per event handler to mitigate excessive
fluctuations in IOThread polling times observed in earlier versions (QEMU 9.x).

However, in the current design, poll.ns is updated only when an event occurs, making it
difficult to treat block_ns as a reliable event interval. Also, The IOThread’s next
polling time is determined by the maximum poll.ns among all AioHandlers, which means
idle AioHandlers with high poll.ns can have an outsized impact on the polling duration.

For io_uring, idle AioHandlers are cleared after POLL_IDLE_INTERVAL_NS (7s), but
ppoll/epoll, there is no such mechanism, so CPU consumption due to idle nodes can
increase even more.

Approach:
To address this, we treat block_ns as an event interval and update each AioHandler’s
poll.ns using a weighted factor. This smooths out polling time adjustments, preventing
excessive fluctuations and ensuring that recent event intervals are properly reflected,
which helps maintain performance while lowering CPU utilization.

Quoted 8 lines
Quoted 7 lines

To use block_ns as an event interval, we update polling times for both event and non-event AioHandlers in each loop iteration. Non-event AioHandler do not require
a weighted factor; this allows for rapid isolation of idle nodes, while ensuring that
poll.ns can increase more responsively when an event occurs within a few subsequent loops.

Thanks for this information. Please include it in the commit description.

Thanks, I'll include this in the commit description in the next version.

Quoted 81 lines
Quoted 81 lines
Quoted 8 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 653

Quoted 6 lines
util/aio-posix.c +5 -0
+            /*
+             * To avoid excessive polling time increase, update adj_block_ns
+             * for nodes with the event flag set to true
+             */
+            adj_block_ns = MAX(adj_block_ns, node->poll.ns);

adj_block_ns is not the blocking time, it's the maximum current poll time across all nodes. It would be clearer to change the variable name.

You're right. I will rename it to max_poll_ns to better reflect its purpose.

Quoted 3 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 648

Quoted 2 lines
util/aio-posix.c +2 -0
+            node->poll.has_event = false;
+         } else {

4-space indentation should be used.

I will also fix the indentation.

Quoted 68 lines

util/aio-posix.c static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, · line 662

Quoted 7 lines
util/aio-posix.c +7 -0
+            /*
+             * No event now, but was active before.
+             * If it waits longer than poll_max_ns, poll.ns will stay 0
+             * until the next event arrives.
+             */
+            if (node->poll.ns != 0) {
+                node->poll.ns += block_ns;

Why is block_ns being added to an recently inactive node's polling time? Here node->poll.ns no longer measures the weighted time until the handler had an event.

If the goal is to get rid of inactive nodes, then maybe the idle handler removal mechanism should be made more aggresive instead?

Quoted 54 lines
util/aio-posix.c +22 -0
+                if (node->poll.ns > ctx->poll_max_ns) {
+                    node->poll.ns = 0;
+                }
+            }
+        }
+    }
+
+    if (adj_block_ns >= 0) {
+        if (adj_block_ns > ctx->poll_ns) {
+            grow_polling_time(ctx, adj_block_ns);
+        } else {
+            shrink_polling_time(ctx, adj_block_ns);
+         }
+     }
+ }
+
   bool aio_poll(AioContext *ctx, bool blocking)
   {
       AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list);
@@ -723,6 +772,10 @@ bool aio_poll(AioContext *ctx, bool blocking)
       aio_free_deleted_handlers(ctx);
+    if (ctx->poll_max_ns) {
+        adjust_block_ns(ctx, block_ns);
+    }
+
       qemu_lockcnt_dec(&ctx->list_lock);
       progress |= timerlistgroup_run_timers(&ctx->tlg);
@@ -784,6 +837,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
       qemu_lockcnt_inc(&ctx->list_lock);
       QLIST_FOREACH(node, &ctx->aio_handlers, node) {
+        node->poll.has_event = false;
           node->poll.ns = 0;
       }
       qemu_lockcnt_dec(&ctx->list_lock);
@@ -794,6 +848,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
       ctx->poll_max_ns = max_ns;
       ctx->poll_grow = grow;
       ctx->poll_shrink = shrink;
+    ctx->poll_ns = 0;
       aio_notify(ctx);
   }
util/async.c +1 -0
index 80d6b01a8a..9d3627566f 100644
@@ -606,6 +606,7 @@ AioContext *aio_context_new(Error **errp)
       timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
       ctx->poll_max_ns = 0;
+    ctx->poll_ns = 0;
       ctx->poll_grow = 0;
       ctx->poll_shrink = 0;
Signature · 1 line
2.50.1
#10 lore raw

RFC PATCH v2 3/3 qapi/iothread: introduce poll-weight parameter for aio-poll

In reply to Jaehoon Kim

Introduce a configurable poll-weight parameter for adaptive polling in IOThread. This parameter replaces the hardcoded POLL_WEIGHT_SHIFT constant, allowing runtime control over how much the most recent event interval affects the next polling duration calculation.

The poll-weight parameter uses a shift value where larger values decrease the weight of the current interval, enabling more gradual adjustments. When set to 0, a default value of 3 is used (meaning the current interval contributes approximately 1/8 to the weighted average).

This patch also removes the hardcoded default values for poll-grow and poll-shrink parameters from the grow_polling_time() and shrink_polling_time() functions, as these defaults are now properly initialized in iothread.c during IOThread creation.

---
 include/qemu/aio.h                |  4 +++-
 include/system/iothread.h         |  1 +
 iothread.c                        | 34 ++++++++++++++++++++++++++++++-
 monitor/hmp-cmds.c                |  1 +
 qapi/misc.json                    |  7 +++++++
 qapi/qom.json                     |  8 +++++++-
 qemu-options.hx                   |  7 ++++++-
 tests/unit/test-nested-aio-poll.c |  2 +-
 util/aio-posix.c                  | 17 +++++-----------
 util/aio-win32.c                  |  3 ++-
 util/async.c                      |  1 +
 11 files changed, 67 insertions(+), 18 deletions(-)
include/qemu/aio.h +3 -1
index 6c77a190e9..50b8db2712 100644
@@ -311,6 +311,7 @@ struct AioContext {
     int64_t poll_max_ns;    /* maximum polling time in nanoseconds */
     int64_t poll_grow;      /* polling time growth factor */
     int64_t poll_shrink;    /* polling time shrink factor */
+    int64_t poll_weight;    /* weight of current interval in calculation */
 
     /* AIO engine parameters */
     int64_t aio_max_batch;  /* maximum number of requests in a batch */
@@ -792,12 +793,13 @@ void aio_context_destroy(AioContext *ctx);
  * @max_ns: how long to busy poll for, in nanoseconds
  * @grow: polling time growth factor
  * @shrink: polling time shrink factor
+ * @weight: weight factor applied to the current polling interval
  *
  * Poll mode can be disabled by setting poll_max_ns to 0.
  */
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
                                  int64_t grow, int64_t shrink,
-                                 Error **errp);
+                                 int64_t weight, Error **errp);
 
 /**
  * aio_context_set_aio_params:
include/system/iothread.h +1 -0
index e26d13c6c7..6ea57ed126 100644
@@ -38,6 +38,7 @@ struct IOThread {
     int64_t poll_max_ns;
     int64_t poll_grow;
     int64_t poll_shrink;
+    int64_t poll_weight;
 };
 typedef struct IOThread IOThread;
 
iothread.c +33 -1
index caf68e0764..0389b8f7a8 100644
@@ -32,8 +32,14 @@
  * workloads.
  */
 #define IOTHREAD_POLL_MAX_NS_DEFAULT 32768ULL
+#define IOTHREAD_POLL_GROW_DEFAULT 2ULL
+#define IOTHREAD_POLL_SHRINK_DEFAULT 2ULL
+#define IOTHREAD_POLL_WEIGHT_DEFAULT 3ULL
 #else
 #define IOTHREAD_POLL_MAX_NS_DEFAULT 0ULL
+#define IOTHREAD_POLL_GROW_DEFAULT 0ULL
+#define IOTHREAD_POLL_SHRINK_DEFAULT 0ULL
+#define IOTHREAD_POLL_WEIGHT_DEFAULT 0ULL
 #endif
 
 static void *iothread_run(void *opaque)
@@ -103,6 +109,10 @@ static void iothread_instance_init(Object *obj)
     IOThread *iothread = IOTHREAD(obj);
 
     iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
+    iothread->poll_grow = IOTHREAD_POLL_GROW_DEFAULT;
+    iothread->poll_shrink = IOTHREAD_POLL_SHRINK_DEFAULT;
+    iothread->poll_weight = IOTHREAD_POLL_WEIGHT_DEFAULT;
+
     iothread->thread_id = -1;
     qemu_sem_init(&iothread->init_done_sem, 0);
     /* By default, we don't run gcontext */
@@ -164,6 +174,7 @@ static void iothread_set_aio_context_params(EventLoopBase *base, Error **errp)
                                 iothread->poll_max_ns,
                                 iothread->poll_grow,
                                 iothread->poll_shrink,
+                                iothread->poll_weight,
                                 errp);
     if (*errp) {
         return;
@@ -233,6 +244,9 @@ static IOThreadParamInfo poll_grow_info = {
 static IOThreadParamInfo poll_shrink_info = {
     "poll-shrink", offsetof(IOThread, poll_shrink),
 };
+static IOThreadParamInfo poll_weight_info = {
+    "poll-weight", offsetof(IOThread, poll_weight),
+};
 
 static void iothread_get_param(Object *obj, Visitor *v,
         const char *name, IOThreadParamInfo *info, Error **errp)
@@ -260,7 +274,19 @@ static bool iothread_set_param(Object *obj, Visitor *v,
         return false;
     }
 
-    *field = value;
+    if (value == 0) {
+        if (info->offset == offsetof(IOThread, poll_grow)) {
+            *field = IOTHREAD_POLL_GROW_DEFAULT;
+        } else if (info->offset == offsetof(IOThread, poll_shrink)) {
+            *field = IOTHREAD_POLL_SHRINK_DEFAULT;
+        } else if (info->offset == offsetof(IOThread, poll_weight)) {
+            *field = IOTHREAD_POLL_WEIGHT_DEFAULT;
+        } else {
+            *field = value;
+        }
+    } else {
+        *field = value;
+    }
 
     return true;
 }
@@ -288,6 +314,7 @@ static void iothread_set_poll_param(Object *obj, Visitor *v,
                                     iothread->poll_max_ns,
                                     iothread->poll_grow,
                                     iothread->poll_shrink,
+                                    iothread->poll_weight,
                                     errp);
     }
 }
@@ -311,6 +338,10 @@ static void iothread_class_init(ObjectClass *klass, const void *class_data)
                               iothread_get_poll_param,
                               iothread_set_poll_param,
                               NULL, &poll_shrink_info);
+    object_class_property_add(klass, "poll-weight", "int",
+                              iothread_get_poll_param,
+                              iothread_set_poll_param,
+                              NULL, &poll_weight_info);
 }
 
 static const TypeInfo iothread_info = {
@@ -356,6 +387,7 @@ static int query_one_iothread(Object *object, void *opaque)
     info->poll_max_ns = iothread->poll_max_ns;
     info->poll_grow = iothread->poll_grow;
     info->poll_shrink = iothread->poll_shrink;
+    info->poll_weight = iothread->poll_weight;
     info->aio_max_batch = iothread->parent_obj.aio_max_batch;
 
     QAPI_LIST_APPEND(*tail, info);
monitor/hmp-cmds.c +1 -0
index bad034937a..75b6e7fa65 100644
@@ -206,6 +206,7 @@ void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
         monitor_printf(mon, "  poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
         monitor_printf(mon, "  poll-grow=%" PRId64 "\n", value->poll_grow);
         monitor_printf(mon, "  poll-shrink=%" PRId64 "\n", value->poll_shrink);
+        monitor_printf(mon, "  poll-weight=%" PRId64 "\n", value->poll_weight);
         monitor_printf(mon, "  aio-max-batch=%" PRId64 "\n",
                        value->aio_max_batch);
     }
qapi/misc.json +7 -0
index 28c641fe2f..39d17010bc 100644
@@ -85,6 +85,12 @@
 # @poll-shrink: how many ns will be removed from polling time, 0 means
 #     that it's not configured (since 2.9)
 #
+# @poll-weight: the weight factor for adaptive polling.
+#     Determines how much the current event interval contributes to
+#     the next polling time calculation.  Valid values are 1 or
+#     greater.  0 selects the system default value which is current 3
+#     (since 10.2)
+#
 # @aio-max-batch: maximum number of requests in a batch for the AIO
 #     engine, 0 means that the engine will use its default (since 6.1)
 #
@@ -96,6 +102,7 @@
            'poll-max-ns': 'int',
            'poll-grow': 'int',
            'poll-shrink': 'int',
+           'poll-weight': 'int',
            'aio-max-batch': 'int' } }
 
 ##
qapi/qom.json +7 -1
index c653248f85..feb80b6cfe 100644
@@ -606,6 +606,11 @@
 #     algorithm detects it is spending too long polling without
 #     encountering events.  0 selects a default behaviour (default: 0)
 #
+# @poll-weight: the weight factor for adaptive polling.
+#     Determines how much the current event interval contributes to
+#     the next polling time calculation.  Valid values are 1 or
+#     greater.  If set to 0, the default value of 3 is used.
+#
 # The @aio-max-batch option is available since 6.1.
 #
 # Since: 2.0
@@ -614,7 +619,8 @@
   'base': 'EventLoopBaseProperties',
   'data': { '*poll-max-ns': 'int',
             '*poll-grow': 'int',
-            '*poll-shrink': 'int' } }
+            '*poll-shrink': 'int',
+            '*poll-weight': 'int' } }
 
 ##
 # @MainLoopProperties:
qemu-options.hx +6 -1
index 69e5a874c1..8ddf6c8d36 100644
@@ -6413,7 +6413,7 @@ SRST
 
             CN=laptop.example.com,O=Example Home,L=London,ST=London,C=GB
 
-    ``-object iothread,id=id,poll-max-ns=poll-max-ns,poll-grow=poll-grow,poll-shrink=poll-shrink,aio-max-batch=aio-max-batch``
+    ``-object iothread,id=id,poll-max-ns=poll-max-ns,poll-grow=poll-grow,poll-shrink=poll-shrink,poll-weight=poll-weight,aio-max-batch=aio-max-batch``
         Creates a dedicated event loop thread that devices can be
         assigned to. This is known as an IOThread. By default device
         emulation happens in vCPU threads or the main event loop thread.
@@ -6449,6 +6449,11 @@ SRST
         the polling time when the algorithm detects it is spending too
         long polling without encountering events.
 
+        The ``poll-weight`` parameter is the weight factor used in the
+        adaptive polling algorithm. It determines how much the most
+        recent event interval affects the calculation of the next
+        polling duration.
+
         The ``aio-max-batch`` parameter is the maximum number of requests
         in a batch for the AIO engine, 0 means that the engine will use
         its default.
tests/unit/test-nested-aio-poll.c +1 -1
index 9ab1ad08a7..4c38f36fd4 100644
@@ -81,7 +81,7 @@ static void test(void)
     qemu_set_current_aio_context(td.ctx);
 
     /* Enable polling */
-    aio_context_set_poll_params(td.ctx, 1000000, 2, 2, &error_abort);
+    aio_context_set_poll_params(td.ctx, 1000000, 2, 2, 3, &error_abort);
 
     /* Make the event notifier active (set) right away */
     event_notifier_init(&td.poll_notifier, 1);
util/aio-posix.c +5 -12
index 2b3522f2f9..13b7f94911 100644
@@ -29,7 +29,6 @@
 
 /* Stop userspace polling on a handler if it isn't active for some time */
 #define POLL_IDLE_INTERVAL_NS (7 * NANOSECONDS_PER_SECOND)
-#define POLL_WEIGHT_SHIFT   (3)
 
 static void adjust_block_ns(AioContext *ctx, int64_t block_ns);
 static void grow_polling_time(AioContext *ctx, int64_t block_ns);
@@ -593,10 +592,6 @@ static void shrink_polling_time(AioContext *ctx, int64_t block_ns)
     int64_t old = ctx->poll_ns;
     int64_t shrink = ctx->poll_shrink;
 
-    if (shrink == 0) {
-        shrink = 2;
-    }
-
     if (block_ns < (ctx->poll_ns / shrink)) {
         ctx->poll_ns /= shrink;
     }
@@ -610,10 +605,6 @@ static void grow_polling_time(AioContext *ctx, int64_t block_ns)
     int64_t old = ctx->poll_ns;
     int64_t grow = ctx->poll_grow;
 
-    if (grow == 0) {
-        grow = 2;
-    }
-
     if (block_ns > ctx->poll_ns * grow) {
         ctx->poll_ns = block_ns;
     } else {
@@ -640,8 +631,8 @@ static void adjust_block_ns(AioContext *ctx, int64_t block_ns)
              * poll.ns to smooth out polling time adjustments.
              */
             node->poll.ns = node->poll.ns
-                ? (node->poll.ns - (node->poll.ns >> POLL_WEIGHT_SHIFT))
-                + (block_ns >> POLL_WEIGHT_SHIFT) : block_ns;
+                ? (node->poll.ns - (node->poll.ns >> ctx->poll_weight))
+                + (block_ns >> ctx->poll_weight) : block_ns;
 
             if (node->poll.ns > ctx->poll_max_ns) {
                 node->poll.ns = 0;
@@ -831,7 +822,8 @@ void aio_context_destroy(AioContext *ctx)
 }
 
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
-                                 int64_t grow, int64_t shrink, Error **errp)
+                                 int64_t grow, int64_t shrink,
+                                 int64_t weight, Error **errp)
 {
     AioHandler *node;
 
@@ -848,6 +840,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
     ctx->poll_max_ns = max_ns;
     ctx->poll_grow = grow;
     ctx->poll_shrink = shrink;
+    ctx->poll_weight = weight;
     ctx->poll_ns = 0;
 
     aio_notify(ctx);
util/aio-win32.c +2 -1
index 6e6f699e4b..1985843233 100644
@@ -429,7 +429,8 @@ void aio_context_destroy(AioContext *ctx)
 }
 
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
-                                 int64_t grow, int64_t shrink, Error **errp)
+                                 int64_t grow, int64_t shrink,
+                                 int64_t weight, Error **errp)
 {
     if (max_ns) {
         error_setg(errp, "AioContext polling is not implemented on Windows");
util/async.c +1 -0
index 9d3627566f..741fcfd6a7 100644
@@ -609,6 +609,7 @@ AioContext *aio_context_new(Error **errp)
     ctx->poll_ns = 0;
     ctx->poll_grow = 0;
     ctx->poll_shrink = 0;
+    ctx->poll_weight = 0;
 
     ctx->aio_max_batch = 0;
 
Signature · 1 line
2.50.1
#11 lore raw

Re: [PATCH RFC v2 3/3] qapi/iothread: introduce poll-weight parameter for aio-poll

Quoted 17 lines · Jaehoon Kim

Introduce a configurable poll-weight parameter for adaptive polling in IOThread. This parameter replaces the hardcoded POLL_WEIGHT_SHIFT constant, allowing runtime control over how much the most recent event interval affects the next polling duration calculation.

The poll-weight parameter uses a shift value where larger values decrease the weight of the current interval, enabling more gradual adjustments. When set to 0, a default value of 3 is used (meaning the current interval contributes approximately 1/8 to the weighted average).

This patch also removes the hardcoded default values for poll-grow and poll-shrink parameters from the grow_polling_time() and shrink_polling_time() functions, as these defaults are now properly initialized in iothread.c during IOThread creation.

[...]

qapi/misc.json

Quoted 5 lines
qapi/misc.json +0 -0
index 28c641fe2f..39d17010bc 100644
@@ -85,6 +85,12 @@

Note: this is IOThreadInfo, used only as return value of query-iothreads.

qapi/misc.json line 91

Quoted 7 lines
qapi/misc.json +4 -0
 # @poll-shrink: how many ns will be removed from polling time, 0 means
 #     that it's not configured (since 2.9)
 #
+# @poll-weight: the weight factor for adaptive polling.
+#     Determines how much the current event interval contributes to
+#     the next polling time calculation.  Valid values are 1 or
+#     greater.  0 selects the system default value which is current 3

Does query-iothreads actually return 0? I'd expect it to return the value that is actually used.

qapi/misc.json line 92

Quoted 1 line
qapi/misc.json +1 -0
+#     (since 10.2)

11.1 most likely.

Quoted 17 lines
qapi/misc.json +2 -0
+#
 # @aio-max-batch: maximum number of requests in a batch for the AIO
 #     engine, 0 means that the engine will use its default (since 6.1)
 #
@@ -96,6 +102,7 @@
            'poll-max-ns': 'int',
            'poll-grow': 'int',
            'poll-shrink': 'int',
+           'poll-weight': 'int',
            'aio-max-batch': 'int' } }
 
 ##
qapi/qom.json +0 -0
index c653248f85..feb80b6cfe 100644

qapi/qom.json line 612

Quoted 7 lines
qapi/qom.json +4 -0
@@ -606,6 +606,11 @@
 #     algorithm detects it is spending too long polling without
 #     encountering events.  0 selects a default behaviour (default: 0)
 #
+# @poll-weight: the weight factor for adaptive polling.
+#     Determines how much the current event interval contributes to
+#     the next polling time calculation.  Valid values are 1 or
+#     greater.  If set to 0, the default value of 3 is used.

The commit message hints what the valid values mean, the doc comment doesn't even that. Do users need to know?

Code [*] below uses it like time >> poll_weight, where @time is int64_t. poll_weight > 63 is undefined behavior, which is a no-no. Please reject such values. poll_weight == 64 results in zero. Is that useful?

Missing: (default: 0) (since 11.1)

Quoted 87 lines
qapi/qom.json +3 -1
+#
 # The @aio-max-batch option is available since 6.1.
 #
 # Since: 2.0
@@ -614,7 +619,8 @@
   'base': 'EventLoopBaseProperties',
   'data': { '*poll-max-ns': 'int',
             '*poll-grow': 'int',
-            '*poll-shrink': 'int' } }
+            '*poll-shrink': 'int',
+            '*poll-weight': 'int' } }
 
 ##
 # @MainLoopProperties:
qemu-options.hx +6 -1
index 69e5a874c1..8ddf6c8d36 100644
@@ -6413,7 +6413,7 @@ SRST
 
             CN=laptop.example.com,O=Example Home,L=London,ST=London,C=GB
 
-    ``-object iothread,id=id,poll-max-ns=poll-max-ns,poll-grow=poll-grow,poll-shrink=poll-shrink,aio-max-batch=aio-max-batch``
+    ``-object iothread,id=id,poll-max-ns=poll-max-ns,poll-grow=poll-grow,poll-shrink=poll-shrink,poll-weight=poll-weight,aio-max-batch=aio-max-batch``
         Creates a dedicated event loop thread that devices can be
         assigned to. This is known as an IOThread. By default device
         emulation happens in vCPU threads or the main event loop thread.
@@ -6449,6 +6449,11 @@ SRST
         the polling time when the algorithm detects it is spending too
         long polling without encountering events.
 
+        The ``poll-weight`` parameter is the weight factor used in the
+        adaptive polling algorithm. It determines how much the most
+        recent event interval affects the calculation of the next
+        polling duration.
+
         The ``aio-max-batch`` parameter is the maximum number of requests
         in a batch for the AIO engine, 0 means that the engine will use
         its default.
tests/unit/test-nested-aio-poll.c +1 -1
index 9ab1ad08a7..4c38f36fd4 100644
@@ -81,7 +81,7 @@ static void test(void)
     qemu_set_current_aio_context(td.ctx);
 
     /* Enable polling */
-    aio_context_set_poll_params(td.ctx, 1000000, 2, 2, &error_abort);
+    aio_context_set_poll_params(td.ctx, 1000000, 2, 2, 3, &error_abort);
 
     /* Make the event notifier active (set) right away */
     event_notifier_init(&td.poll_notifier, 1);
util/aio-posix.c +0 -9
index 2b3522f2f9..13b7f94911 100644
@@ -29,7 +29,6 @@
 
 /* Stop userspace polling on a handler if it isn't active for some time */
 #define POLL_IDLE_INTERVAL_NS (7 * NANOSECONDS_PER_SECOND)
-#define POLL_WEIGHT_SHIFT   (3)
 
 static void adjust_block_ns(AioContext *ctx, int64_t block_ns);
 static void grow_polling_time(AioContext *ctx, int64_t block_ns);
@@ -593,10 +592,6 @@ static void shrink_polling_time(AioContext *ctx, int64_t block_ns)
     int64_t old = ctx->poll_ns;
     int64_t shrink = ctx->poll_shrink;
 
-    if (shrink == 0) {
-        shrink = 2;
-    }
-
     if (block_ns < (ctx->poll_ns / shrink)) {
         ctx->poll_ns /= shrink;
     }
@@ -610,10 +605,6 @@ static void grow_polling_time(AioContext *ctx, int64_t block_ns)
     int64_t old = ctx->poll_ns;
     int64_t grow = ctx->poll_grow;
 
-    if (grow == 0) {
-        grow = 2;
-    }
-
     if (block_ns > ctx->poll_ns * grow) {
         ctx->poll_ns = block_ns;
     } else {

util/aio-posix.c static void adjust_block_ns(AioContext *ctx, int64_t block_ns) · line 635

Quoted 7 lines
util/aio-posix.c +2 -2
@@ -640,8 +631,8 @@ static void adjust_block_ns(AioContext *ctx, int64_t block_ns)
              * poll.ns to smooth out polling time adjustments.
              */
             node->poll.ns = node->poll.ns
-                ? (node->poll.ns - (node->poll.ns >> POLL_WEIGHT_SHIFT))
-                + (block_ns >> POLL_WEIGHT_SHIFT) : block_ns;
+                ? (node->poll.ns - (node->poll.ns >> ctx->poll_weight))
+                + (block_ns >> ctx->poll_weight) : block_ns;

[*] This is the use of @poll-weight referred to above.

Quoted 21 lines
util/aio-posix.c +3 -1
             if (node->poll.ns > ctx->poll_max_ns) {
                 node->poll.ns = 0;
@@ -831,7 +822,8 @@ void aio_context_destroy(AioContext *ctx)
 }
 
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
-                                 int64_t grow, int64_t shrink, Error **errp)
+                                 int64_t grow, int64_t shrink,
+                                 int64_t weight, Error **errp)
 {
     AioHandler *node;
 
@@ -848,6 +840,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
     ctx->poll_max_ns = max_ns;
     ctx->poll_grow = grow;
     ctx->poll_shrink = shrink;
+    ctx->poll_weight = weight;
     ctx->poll_ns = 0;
 
     aio_notify(ctx);

[...]

#12 lore raw

Re: [PATCH RFC v2 3/3] qapi/iothread: introduce poll-weight parameter for aio-poll

Quoted 29 lines · Markus Armbruster
Quoted 17 lines · Jaehoon Kim

Introduce a configurable poll-weight parameter for adaptive polling in IOThread. This parameter replaces the hardcoded POLL_WEIGHT_SHIFT constant, allowing runtime control over how much the most recent event interval affects the next polling duration calculation.

The poll-weight parameter uses a shift value where larger values decrease the weight of the current interval, enabling more gradual adjustments. When set to 0, a default value of 3 is used (meaning the current interval contributes approximately 1/8 to the weighted average).

This patch also removes the hardcoded default values for poll-grow and poll-shrink parameters from the grow_polling_time() and shrink_polling_time() functions, as these defaults are now properly initialized in iothread.c during IOThread creation.

[...]

qapi/misc.json

Quoted 5 lines
qapi/misc.json +0 -0
index 28c641fe2f..39d17010bc 100644
@@ -85,6 +85,12 @@

Note: this is IOThreadInfo, used only as return value of query-iothreads.

Quoted 9 lines

qapi/misc.json line 91

Quoted 7 lines
qapi/misc.json +4 -0
  # @poll-shrink: how many ns will be removed from polling time, 0 means
  #     that it's not configured (since 2.9)
  #
+# @poll-weight: the weight factor for adaptive polling.
+#     Determines how much the current event interval contributes to
+#     the next polling time calculation.  Valid values are 1 or
+#     greater.  0 selects the system default value which is current 3

Does query-iothreads actually return 0? I'd expect it to return the value that is actually used.

Thanks for your feedback.
Expose IOThread default poll parameters in iothread_instance_init() so query shows system-initialized values for grow, shrink, and weight. Also, I modified iothread_set_param() so that when the user input 0, it reverts to the system default values.

Quoted 2 lines

qapi/misc.json line 92

Quoted 1 line
qapi/misc.json +1 -0
+#     (since 10.2)

11.1 most likely.

Thanks for noticing. I'll update the version to 11.1 in the next patch.

Quoted 27 lines
Quoted 17 lines
qapi/misc.json +2 -0
+#
  # @aio-max-batch: maximum number of requests in a batch for the AIO
  #     engine, 0 means that the engine will use its default (since 6.1)
  #
@@ -96,6 +102,7 @@
             'poll-max-ns': 'int',
             'poll-grow': 'int',
             'poll-shrink': 'int',
+           'poll-weight': 'int',
             'aio-max-batch': 'int' } }
 
  ##
qapi/qom.json +0 -0
index c653248f85..feb80b6cfe 100644

qapi/qom.json line 612

Quoted 7 lines
qapi/qom.json +4 -0
@@ -606,6 +606,11 @@
  #     algorithm detects it is spending too long polling without
  #     encountering events.  0 selects a default behaviour (default: 0)
  #
+# @poll-weight: the weight factor for adaptive polling.
+#     Determines how much the current event interval contributes to
+#     the next polling time calculation.  Valid values are 1 or
+#     greater.  If set to 0, the default value of 3 is used.

The commit message hints what the valid values mean, the doc comment doesn't even that. Do users need to know?

Quoted 5 lines

Code [*] below uses it like time >> poll_weight, where @time is int64_t. poll_weight > 63 is undefined behavior, which is a no-no. Please reject such values. poll_weight == 64 results in zero. Is that useful?

Missing: (default: 0) (since 11.1)

I agree. I will update the doc comment to give users a practical hint, for example like this:

# @poll-weight: the weight factor for adaptive polling. #     Determines how much the most recent event interval affects #     the next polling duration calculation.
#     If set to 0, the system default value of 3 is used. #     Typical values: 1 (high weight on recent interval), #     2-4 (moderate weight on recent interval).
#    (default: 0) (since 11.1)

I will also a check in the code so that values exceeding the maximum allowed will revert to the system default.

Quoted 119 lines
Quoted 87 lines
qapi/qom.json +3 -1
+#
  # The @aio-max-batch option is available since 6.1.
  #
  # Since: 2.0
@@ -614,7 +619,8 @@
    'base': 'EventLoopBaseProperties',
    'data': { '*poll-max-ns': 'int',
              '*poll-grow': 'int',
-            '*poll-shrink': 'int' } }
+            '*poll-shrink': 'int',
+            '*poll-weight': 'int' } }
 
  ##
  # @MainLoopProperties:
qemu-options.hx +6 -1
index 69e5a874c1..8ddf6c8d36 100644
@@ -6413,7 +6413,7 @@ SRST
 
              CN=laptop.example.com,O=Example Home,L=London,ST=London,C=GB
 
-    ``-object iothread,id=id,poll-max-ns=poll-max-ns,poll-grow=poll-grow,poll-shrink=poll-shrink,aio-max-batch=aio-max-batch``
+    ``-object iothread,id=id,poll-max-ns=poll-max-ns,poll-grow=poll-grow,poll-shrink=poll-shrink,poll-weight=poll-weight,aio-max-batch=aio-max-batch``
          Creates a dedicated event loop thread that devices can be
          assigned to. This is known as an IOThread. By default device
          emulation happens in vCPU threads or the main event loop thread.
@@ -6449,6 +6449,11 @@ SRST
          the polling time when the algorithm detects it is spending too
          long polling without encountering events.
 
+        The ``poll-weight`` parameter is the weight factor used in the
+        adaptive polling algorithm. It determines how much the most
+        recent event interval affects the calculation of the next
+        polling duration.
+
          The ``aio-max-batch`` parameter is the maximum number of requests
          in a batch for the AIO engine, 0 means that the engine will use
          its default.
tests/unit/test-nested-aio-poll.c +1 -1
index 9ab1ad08a7..4c38f36fd4 100644
@@ -81,7 +81,7 @@ static void test(void)
      qemu_set_current_aio_context(td.ctx);
 
      /* Enable polling */
-    aio_context_set_poll_params(td.ctx, 1000000, 2, 2, &error_abort);
+    aio_context_set_poll_params(td.ctx, 1000000, 2, 2, 3, &error_abort);
 
      /* Make the event notifier active (set) right away */
      event_notifier_init(&td.poll_notifier, 1);
util/aio-posix.c +0 -9
index 2b3522f2f9..13b7f94911 100644
@@ -29,7 +29,6 @@
 
  /* Stop userspace polling on a handler if it isn't active for some time */
  #define POLL_IDLE_INTERVAL_NS (7 * NANOSECONDS_PER_SECOND)
-#define POLL_WEIGHT_SHIFT   (3)
 
  static void adjust_block_ns(AioContext *ctx, int64_t block_ns);
  static void grow_polling_time(AioContext *ctx, int64_t block_ns);
@@ -593,10 +592,6 @@ static void shrink_polling_time(AioContext *ctx, int64_t block_ns)
      int64_t old = ctx->poll_ns;
      int64_t shrink = ctx->poll_shrink;
 
-    if (shrink == 0) {
-        shrink = 2;
-    }
-
      if (block_ns < (ctx->poll_ns / shrink)) {
          ctx->poll_ns /= shrink;
      }
@@ -610,10 +605,6 @@ static void grow_polling_time(AioContext *ctx, int64_t block_ns)
      int64_t old = ctx->poll_ns;
      int64_t grow = ctx->poll_grow;
 
-    if (grow == 0) {
-        grow = 2;
-    }
-
      if (block_ns > ctx->poll_ns * grow) {
          ctx->poll_ns = block_ns;
      } else {

util/aio-posix.c static void adjust_block_ns(AioContext *ctx, int64_t block_ns) · line 635

Quoted 7 lines
util/aio-posix.c +2 -2
@@ -640,8 +631,8 @@ static void adjust_block_ns(AioContext *ctx, int64_t block_ns)
               * poll.ns to smooth out polling time adjustments.
               */
              node->poll.ns = node->poll.ns
-                ? (node->poll.ns - (node->poll.ns >> POLL_WEIGHT_SHIFT))
-                + (block_ns >> POLL_WEIGHT_SHIFT) : block_ns;
+                ? (node->poll.ns - (node->poll.ns >> ctx->poll_weight))
+                + (block_ns >> ctx->poll_weight) : block_ns;

[*] This is the use of @poll-weight referred to above.

Quoted 21 lines
util/aio-posix.c +3 -1
              if (node->poll.ns > ctx->poll_max_ns) {
                  node->poll.ns = 0;
@@ -831,7 +822,8 @@ void aio_context_destroy(AioContext *ctx)
  }
 
  void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
-                                 int64_t grow, int64_t shrink, Error **errp)
+                                 int64_t grow, int64_t shrink,
+                                 int64_t weight, Error **errp)
  {
      AioHandler *node;
 
@@ -848,6 +840,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
      ctx->poll_max_ns = max_ns;
      ctx->poll_grow = grow;
      ctx->poll_shrink = shrink;
+    ctx->poll_weight = weight;
      ctx->poll_ns = 0;
 
      aio_notify(ctx);

[...]

#13 lore raw

Re: [PATCH RFC v2 3/3] qapi/iothread: introduce poll-weight parameter for aio-poll

Quoted 20 lines · JAEHOON KIM
Quoted 19 lines · Markus Armbruster
Quoted 17 lines · Jaehoon Kim

Introduce a configurable poll-weight parameter for adaptive polling in IOThread. This parameter replaces the hardcoded POLL_WEIGHT_SHIFT constant, allowing runtime control over how much the most recent event interval affects the next polling duration calculation.

The poll-weight parameter uses a shift value where larger values decrease the weight of the current interval, enabling more gradual adjustments. When set to 0, a default value of 3 is used (meaning the current interval contributes approximately 1/8 to the weighted average).

This patch also removes the hardcoded default values for poll-grow and poll-shrink parameters from the grow_polling_time() and shrink_polling_time() functions, as these defaults are now properly initialized in iothread.c during IOThread creation.

[...]

Quoted 25 lines
Quoted 16 lines

qapi/qom.json line 612

Quoted 12 lines
qapi/qom.json +4 -0
index c653248f85..feb80b6cfe 100644
@@ -606,6 +606,11 @@
 #     algorithm detects it is spending too long polling without
 #     encountering events.  0 selects a default behaviour (default: 0)
 #
+# @poll-weight: the weight factor for adaptive polling.
+#     Determines how much the current event interval contributes to
+#     the next polling time calculation.  Valid values are 1 or
+#     greater.  If set to 0, the default value of 3 is used.

The commit message hints what the valid values mean, the doc comment doesn't even that. Do users need to know?

Quoted 5 lines

Code [*] below uses it like time >> poll_weight, where @time is int64_t. poll_weight > 63 is undefined behavior, which is a no-no. Please reject such values. poll_weight == 64 results in zero. Is that useful?

Missing: (default: 0) (since 11.1)

I agree. I will update the doc comment to give users a practical hint, for example like this:

Quoted 7 lines

# @poll-weight: the weight factor for adaptive polling. #     Determines how much the most recent event interval affects #     the next polling duration calculation.
#     If set to 0, the system default value of 3 is used. #     Typical values: 1 (high weight on recent interval), #     2-4 (moderate weight on recent interval).
#    (default: 0) (since 11.1)

Better, thanks!

Quoted 2 lines

I will also a check in the code so that values exceeding the maximum allowed will revert to the system default.

Don't silently "correct" invalid input, reject it!

[...]

#14 lore raw

Re: [PATCH RFC v2 3/3] qapi/iothread: introduce poll-weight parameter for aio-poll

Quoted 54 lines · Markus Armbruster
Quoted 20 lines · JAEHOON KIM
Quoted 19 lines · Markus Armbruster
Quoted 17 lines · Jaehoon Kim

Introduce a configurable poll-weight parameter for adaptive polling in IOThread. This parameter replaces the hardcoded POLL_WEIGHT_SHIFT constant, allowing runtime control over how much the most recent event interval affects the next polling duration calculation.

The poll-weight parameter uses a shift value where larger values decrease the weight of the current interval, enabling more gradual adjustments. When set to 0, a default value of 3 is used (meaning the current interval contributes approximately 1/8 to the weighted average).

This patch also removes the hardcoded default values for poll-grow and poll-shrink parameters from the grow_polling_time() and shrink_polling_time() functions, as these defaults are now properly initialized in iothread.c during IOThread creation.

[...]

Quoted 23 lines
Quoted 15 lines

qapi/qom.json line 612

Quoted 12 lines
qapi/qom.json +4 -0
index c653248f85..feb80b6cfe 100644
@@ -606,6 +606,11 @@
  #     algorithm detects it is spending too long polling without
  #     encountering events.  0 selects a default behaviour (default: 0)
  #
+# @poll-weight: the weight factor for adaptive polling.
+#     Determines how much the current event interval contributes to
+#     the next polling time calculation.  Valid values are 1 or
+#     greater.  If set to 0, the default value of 3 is used.

The commit message hints what the valid values mean, the doc comment doesn't even that. Do users need to know?

Quoted 5 lines

Code [*] below uses it like time >> poll_weight, where @time is int64_t. poll_weight > 63 is undefined behavior, which is a no-no. Please reject such values. poll_weight == 64 results in zero. Is that useful?

Missing: (default: 0) (since 11.1)

I agree. I will update the doc comment to give users a practical hint, for example like this:

Quoted 7 lines

# @poll-weight: the weight factor for adaptive polling. #     Determines how much the most recent event interval affects #     the next polling duration calculation.
#     If set to 0, the system default value of 3 is used. #     Typical values: 1 (high weight on recent interval), #     2-4 (moderate weight on recent interval).
#    (default: 0) (since 11.1)

Quoted 7 lines

Better, thanks!

Quoted 2 lines

I will also a check in the code so that values exceeding the maximum allowed will revert to the system default.

Don't silently "correct" invalid input, reject it!

[...]

Hi Markus,

Thank you for the review.
I will change the code to reject invalid values instead of silently correct them in the next version.

Regards,
Jaehoon.

Quoted 1 line
Reviewed-by on v2 3/3
#15 lore raw

Re: [PATCH RFC v2 3/3] qapi/iothread: introduce poll-weight parameter for aio-poll

In reply to Jaehoon Kim

qapi/misc.json line 92

Quoted 13 lines · Jaehoon Kim
qapi/misc.json +5 -0
index 28c641fe2f..39d17010bc 100644
@@ -85,6 +85,12 @@
 # @poll-shrink: how many ns will be removed from polling time, 0 means
 #     that it's not configured (since 2.9)
 #
+# @poll-weight: the weight factor for adaptive polling.
+#     Determines how much the current event interval contributes to
+#     the next polling time calculation.  Valid values are 1 or
+#     greater.  0 selects the system default value which is current 3
+#     (since 10.2)

QEMU 11.0 is already in hard freeze, so this patch will go into 11.1:

(since 11.1)

Quoted 17 lines
qapi/misc.json +2 -0
+#
 # @aio-max-batch: maximum number of requests in a batch for the AIO
 #     engine, 0 means that the engine will use its default (since 6.1)
 #
@@ -96,6 +102,7 @@
            'poll-max-ns': 'int',
            'poll-grow': 'int',
            'poll-shrink': 'int',
+           'poll-weight': 'int',
            'aio-max-batch': 'int' } }
 
 ##
qapi/qom.json +0 -0
index c653248f85..feb80b6cfe 100644

qapi/qom.json line 612

Quoted 7 lines
qapi/qom.json +4 -0
@@ -606,6 +606,11 @@
 #     algorithm detects it is spending too long polling without
 #     encountering events.  0 selects a default behaviour (default: 0)
 #
+# @poll-weight: the weight factor for adaptive polling.
+#     Determines how much the current event interval contributes to
+#     the next polling time calculation.  Valid values are 1 or
+#     greater.  If set to 0, the default value of 3 is used.

(since 11.1)

Other than that:

#16 lore raw

Re: [PATCH RFC v2 3/3] qapi/iothread: introduce poll-weight parameter for aio-poll

In reply to Jaehoon Kim
Quoted 11 lines · Jaehoon Kim
util/aio-posix.c +2 -1
@@ -831,7 +822,8 @@ void aio_context_destroy(AioContext *ctx)
 }
 
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
-                                 int64_t grow, int64_t shrink, Error **errp)
+                                 int64_t grow, int64_t shrink,
+                                 int64_t weight, Error **errp)
 {
     AioHandler *node;
 

util/aio-posix.c void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns, · line 846

Quoted 7 lines
util/aio-posix.c +1 -0
@@ -848,6 +840,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
     ctx->poll_max_ns = max_ns;
     ctx->poll_grow = grow;
     ctx->poll_shrink = shrink;
+    ctx->poll_weight = weight;
     ctx->poll_ns = 0;
 
     aio_notify(ctx);

On second thought, now that the divide-by-0 protection has been removed and these fields are assumed to hold a valid value when poll_max_ns > 0, aio_context_set_poll_params() needs the same 0-protection as iothread_set_param().

#17 lore raw

Re: [PATCH RFC v2 3/3] qapi/iothread: introduce poll-weight parameter for aio-poll

Quoted 18 lines · Stefan Hajnoczi
Quoted 17 lines · Jaehoon Kim
util/aio-posix.c +3 -1
@@ -831,7 +822,8 @@ void aio_context_destroy(AioContext *ctx)
  }
 
  void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
-                                 int64_t grow, int64_t shrink, Error **errp)
+                                 int64_t grow, int64_t shrink,
+                                 int64_t weight, Error **errp)
  {
      AioHandler *node;
 
@@ -848,6 +840,7 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
      ctx->poll_max_ns = max_ns;
      ctx->poll_grow = grow;
      ctx->poll_shrink = shrink;
+    ctx->poll_weight = weight;
      ctx->poll_ns = 0;
 
Quoted 5 lines

util/aio-posix.c void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns, · line 846

Quoted 1 line
util/aio-posix.c +0 -0
      aio_notify(ctx);

On second thought, now that the divide-by-0 protection has been removed and these fields are assumed to hold a valid value when poll_max_ns > 0, aio_context_set_poll_params() needs the same 0-protection as iothread_set_param().

Thanks for pointing that out. I only considered the user settings and overlooked the possibility of this being configured in test code or other areas.

I will add divide-by-zero protection to aio_context_set_poll_params. and update the versioning to 11.1 in the next version.

Regards,
Jaehoon