diff options
author | Stefan Hajnoczi <stefanha@redhat.com> | 2023-09-13 16:00:43 -0400 |
---|---|---|
committer | Kevin Wolf <kwolf@redhat.com> | 2023-10-31 15:41:42 +0100 |
commit | 433fcea40c31ff355f84da22a46977c2a1b596c3 (patch) | |
tree | 47a995db30935c88634c46402b26d736a10d8b73 /block | |
parent | ccee48aa736b97f0ce4ab04ad41815f0e575d526 (diff) |
util/defer-call: move defer_call() to util/
The networking subsystem may wish to use defer_call(), so move the code
to util/ where it can be reused.
As a reminder of what defer_call() does:
This API defers a function call within a defer_call_begin()/defer_call_end()
section, allowing multiple calls to batch up. This is a performance
optimization that is used in the block layer to submit several I/O requests
at once instead of individually:
defer_call_begin(); <-- start of section
...
defer_call(my_func, my_obj); <-- deferred my_func(my_obj) call
defer_call(my_func, my_obj); <-- another
defer_call(my_func, my_obj); <-- another
...
defer_call_end(); <-- end of section, my_func(my_obj) is called once
Suggested-by: Ilya Maximets <i.maximets@ovn.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-ID: <20230913200045.1024233-3-stefanha@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'block')
-rw-r--r-- | block/blkio.c | 1 | ||||
-rw-r--r-- | block/io_uring.c | 1 | ||||
-rw-r--r-- | block/linux-aio.c | 1 | ||||
-rw-r--r-- | block/meson.build | 1 | ||||
-rw-r--r-- | block/nvme.c | 1 | ||||
-rw-r--r-- | block/plug.c | 156 |
6 files changed, 4 insertions, 157 deletions
diff --git a/block/blkio.c b/block/blkio.c index 7cf6d61f47..0a0a6c0f5f 100644 --- a/block/blkio.c +++ b/block/blkio.c @@ -13,6 +13,7 @@ #include "block/block_int.h" #include "exec/memory.h" #include "exec/cpu-common.h" /* for qemu_ram_get_fd() */ +#include "qemu/defer-call.h" #include "qapi/error.h" #include "qemu/error-report.h" #include "qapi/qmp/qdict.h" diff --git a/block/io_uring.c b/block/io_uring.c index 8429f341be..3a1e1f45b3 100644 --- a/block/io_uring.c +++ b/block/io_uring.c @@ -15,6 +15,7 @@ #include "block/block.h" #include "block/raw-aio.h" #include "qemu/coroutine.h" +#include "qemu/defer-call.h" #include "qapi/error.h" #include "sysemu/block-backend.h" #include "trace.h" diff --git a/block/linux-aio.c b/block/linux-aio.c index 49a37174c2..a2670b3e46 100644 --- a/block/linux-aio.c +++ b/block/linux-aio.c @@ -14,6 +14,7 @@ #include "block/raw-aio.h" #include "qemu/event_notifier.h" #include "qemu/coroutine.h" +#include "qemu/defer-call.h" #include "qapi/error.h" #include "sysemu/block-backend.h" diff --git a/block/meson.build b/block/meson.build index f351b9d0d3..59ff6d380c 100644 --- a/block/meson.build +++ b/block/meson.build @@ -21,7 +21,6 @@ block_ss.add(files( 'mirror.c', 'nbd.c', 'null.c', - 'plug.c', 'preallocate.c', 'progress_meter.c', 'qapi.c', diff --git a/block/nvme.c b/block/nvme.c index dfbd1085fd..96b3f8f2fa 100644 --- a/block/nvme.c +++ b/block/nvme.c @@ -16,6 +16,7 @@ #include "qapi/error.h" #include "qapi/qmp/qdict.h" #include "qapi/qmp/qstring.h" +#include "qemu/defer-call.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" #include "qemu/module.h" diff --git a/block/plug.c b/block/plug.c deleted file mode 100644 index f26173559c..0000000000 --- a/block/plug.c +++ /dev/null @@ -1,156 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Deferred calls - * - * Copyright Red Hat. - * - * This API defers a function call within a defer_call_begin()/defer_call_end() - * section, allowing multiple calls to batch up. This is a performance - * optimization that is used in the block layer to submit several I/O requests - * at once instead of individually: - * - * defer_call_begin(); <-- start of section - * ... - * defer_call(my_func, my_obj); <-- deferred my_func(my_obj) call - * defer_call(my_func, my_obj); <-- another - * defer_call(my_func, my_obj); <-- another - * ... - * defer_call_end(); <-- end of section, my_func(my_obj) is called once - */ - -#include "qemu/osdep.h" -#include "qemu/coroutine-tls.h" -#include "qemu/notify.h" -#include "qemu/thread.h" -#include "sysemu/block-backend.h" - -/* A function call that has been deferred until defer_call_end() */ -typedef struct { - void (*fn)(void *); - void *opaque; -} DeferredCall; - -/* Per-thread state */ -typedef struct { - unsigned nesting_level; - GArray *deferred_call_array; -} DeferCallThreadState; - -/* Use get_ptr_defer_call_thread_state() to fetch this thread-local value */ -QEMU_DEFINE_STATIC_CO_TLS(DeferCallThreadState, defer_call_thread_state); - -/* Called at thread cleanup time */ -static void defer_call_atexit(Notifier *n, void *value) -{ - DeferCallThreadState *thread_state = get_ptr_defer_call_thread_state(); - g_array_free(thread_state->deferred_call_array, TRUE); -} - -/* This won't involve coroutines, so use __thread */ -static __thread Notifier defer_call_atexit_notifier; - -/** - * defer_call: - * @fn: a function pointer to be invoked - * @opaque: a user-defined argument to @fn() - * - * Call @fn(@opaque) immediately if not within a - * defer_call_begin()/defer_call_end() section. - * - * Otherwise defer the call until the end of the outermost - * defer_call_begin()/defer_call_end() section in this thread. If the same - * @fn/@opaque pair has already been deferred, it will only be called once upon - * defer_call_end() so that accumulated calls are batched into a single call. - * - * The caller must ensure that @opaque is not freed before @fn() is invoked. - */ -void defer_call(void (*fn)(void *), void *opaque) -{ - DeferCallThreadState *thread_state = get_ptr_defer_call_thread_state(); - - /* Call immediately if we're not deferring calls */ - if (thread_state->nesting_level == 0) { - fn(opaque); - return; - } - - GArray *array = thread_state->deferred_call_array; - if (!array) { - array = g_array_new(FALSE, FALSE, sizeof(DeferredCall)); - thread_state->deferred_call_array = array; - defer_call_atexit_notifier.notify = defer_call_atexit; - qemu_thread_atexit_add(&defer_call_atexit_notifier); - } - - DeferredCall *fns = (DeferredCall *)array->data; - DeferredCall new_fn = { - .fn = fn, - .opaque = opaque, - }; - - /* - * There won't be many, so do a linear search. If this becomes a bottleneck - * then a binary search (glib 2.62+) or different data structure could be - * used. - */ - for (guint i = 0; i < array->len; i++) { - if (memcmp(&fns[i], &new_fn, sizeof(new_fn)) == 0) { - return; /* already exists */ - } - } - - g_array_append_val(array, new_fn); -} - -/** - * defer_call_begin: Defer defer_call() functions until defer_call_end() - * - * defer_call_begin() and defer_call_end() are thread-local operations. The - * caller must ensure that each defer_call_begin() has a matching - * defer_call_end() in the same thread. - * - * Nesting is supported. defer_call() functions are only called at the - * outermost defer_call_end(). - */ -void defer_call_begin(void) -{ - DeferCallThreadState *thread_state = get_ptr_defer_call_thread_state(); - - assert(thread_state->nesting_level < UINT32_MAX); - - thread_state->nesting_level++; -} - -/** - * defer_call_end: Run any pending defer_call() functions - * - * There must have been a matching defer_call_begin() call in the same thread - * prior to this defer_call_end() call. - */ -void defer_call_end(void) -{ - DeferCallThreadState *thread_state = get_ptr_defer_call_thread_state(); - - assert(thread_state->nesting_level > 0); - - if (--thread_state->nesting_level > 0) { - return; - } - - GArray *array = thread_state->deferred_call_array; - if (!array) { - return; - } - - DeferredCall *fns = (DeferredCall *)array->data; - - for (guint i = 0; i < array->len; i++) { - fns[i].fn(fns[i].opaque); - } - - /* - * This resets the array without freeing memory so that appending is cheap - * in the future. - */ - g_array_set_size(array, 0); -} |