util/thread-pool: Expose minimun and maximum size
cover util/thread-pool: Expose minimun and maximum size
As discussed on the previous RFC[1] the thread-pool's dynamic thread management doesn't play well with real-time and latency sensitive systems. This series introduces a set of controls that'll permit achieving more deterministic behaviours, for example by fixing the pool's size.
We first introduce a new common interface to event loop configuration by moving iothread's already available properties into an abstract class called 'EventLooopBackend' and have both 'IOThread' and the newly created 'MainLoop' inherit the properties from that class.
With this new configuration interface in place it's relatively simple to introduce new options to fix the even loop's thread pool sizes. The resulting QAPI looks like this:
-object main-loop,id=main-loop,thread-pool-min=1,thread-pool-max=1
Note that all patches are bisect friendly and pass all the tests.
[1] https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/
---
Nicolas Saenz Julienne (3):
util & iothread: Introduce event-loop abstract class util/main-loop: Introduce the main loop into QOM util/event-loop: Introduce options to set the thread pool size
MAINTAINERS | 1 + include/block/aio.h | 11 +++ include/qemu/main-loop.h | 11 +++ include/sysemu/iothread.h | 11 +-- iothread.c | 171 ++++---------------------------------- qapi/qom.json | 14 ++-- qga/meson.build | 2 +- qom/meson.build | 1 + tests/unit/meson.build | 10 +-- util/async.c | 3 + util/event-loop.c | 168 +++++++++++++++++++++++++++++++++++++ util/event-loop.h | 45 ++++++++++ util/main-loop.c | 56 +++++++++++++ util/thread-pool.c | 41 ++++++++- 14 files changed, 368 insertions(+), 177 deletions(-) create mode 100644 util/event-loop.c create mode 100644 util/event-loop.h
Signature · 1 line
2.35.1
PATCH 1/3 util & iothread: Introduce event-loop abstract class
Introduce the 'event-loop-backend' abstract class, it'll hold the properties common to all event loops and provide the necessary hooks for their creation. Then have 'IOThread' inherit from it. The class is defined as user creatable and provides a hook for its children to attach themselves to the user creatable class 'complete' function.
The new 'event-loop-backend' class will live in the util directory, and will be packed into the qom static library.
No functional changes intended.
--- MAINTAINERS | 1 + include/sysemu/iothread.h | 11 +-- iothread.c | 171 ++++---------------------------------- qom/meson.build | 1 + util/event-loop.c | 142 +++++++++++++++++++++++++++++++ util/event-loop.h | 40 +++++++++ 6 files changed, 204 insertions(+), 162 deletions(-) create mode 100644 util/event-loop.c create mode 100644 util/event-loop.h
MAINTAINERS +1 -0
@@ -2670,6 +2670,7 @@ F: include/sysemu/runstate.h F: include/sysemu/runstate-action.h F: util/main-loop.c F: util/qemu-timer.c +F: util/event-loop* F: softmmu/vl.c F: softmmu/main.c F: softmmu/cpus.c
include/sysemu/iothread.h +2 -9
@@ -17,11 +17,12 @@#include "block/aio.h" #include "qemu/thread.h" #include "qom/object.h" +#include "util/event-loop.h" #define TYPE_IOTHREAD "iothread"struct IOThread { -Object parent_obj; +EventLoopBackend parent_obj;QemuThread thread;AioContext *ctx;
@@ -32,14 +33,6 @@ struct IOThread {bool stopping;/* has iothread_stop() been called? */ bool running;/* should iothread_run() continue? */ int thread_id; - -/* AioContext poll parameters */ -int64_t poll_max_ns; -int64_t poll_grow; -int64_t poll_shrink; - -/* AioContext AIO engine parameters */ -int64_t aio_max_batch; };typedef struct IOThread IOThread ;
iothread.c +18 -153
240 lines not on this page · raw message
qom/meson.build +1 -0
@@ -4,6 +4,7 @@ qom_ss.add(files( 'object.c', 'object_interfaces.c', 'qom-qobject.c', + '../util/event-loop.c', )) qmp_ss.add(files('qom-qmp-cmds.c'))
util/event-loop.c new file +142 -0
142 lines not on this page · raw message
util/event-loop.h new file +40 -0
@@ -0,0 +1,40 @@ +/* +* QEMU event-loop backend +* +* Copyright (C) 2022 Red Hat Inc +* +* Authors: +* Nicolas Saenz Julienne <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ +#ifndef QEMU_EVENT_LOOP_H +#define QEMU_EVENT_LOOP_H + +#include "qom/object.h" +#include "block/aio.h" +#include "qemu/typedefs.h" + +#define TYPE_EVENT_LOOP_BACKEND "event-loop-backend" +OBJECT_DECLARE_TYPE (EventLoopBackend, EventLoopBackendClass, + EVENT_LOOP_BACKEND) + +struct EventLoopBackendClass { +ObjectClass parent_class; + +void (*init)(EventLoopBackend *backend,Error **errp); +}; + +struct EventLoopBackend { +Object parent; + +/* AioContext poll parameters */ +int64_t poll_max_ns; +int64_t poll_grow; +int64_t poll_shrink; + +/* AioContext AIO engine parameters */ +int64_t aio_max_batch; +}; +#endif
Signature · 1 line
2.35.1
Re: [PATCH 1/3] util & iothread: Introduce event-loop abstract class
qom/meson.build qom_ss.add(files( · line 7
Quoted 9 lines · Nicolas Saenz Julienne
qom/meson.build +1
-0@@ -4,6 +4,7 @@ qom_ss.add(files( 'object.c', 'object_interfaces.c', 'qom-qobject.c', + '../util/event-loop.c',
This looks strange. I expected util/event-loop.c to be in util/meson.build and added to the util_ss SourceSet instead of qom_ss.
What is the reason for this?
util/event-loop.c
Quoted 8 lines
qom/meson.build +0
-0)) qmp_ss.add(files('qom-qmp-cmds.c'))util/event-loop.c new file +0
-0
The naming is a little inconsistent. The filename "event-loop.c" does match the QOM type or typedef name event-loop-backend/EventLoopBackend.
I suggest calling the source file event-loop-base.c and the QOM type "event-loop-base".
util/event-loop.c line 7
Quoted 8 lines
util/event-loop.c new file +7
-0@@ -0,0 +1,142 @@ +/* +* QEMU event-loop backend +* +* Copyright (C) 2022 Red Hat Inc +* +* Authors: +* Nicolas Saenz Julienne <[email protected]>
Most of the code is cut and pasted. It would be nice to carry over the authorship information too.
util/event-loop.c line 14
Quoted 7 lines
util/event-loop.c new file +7
-0+struct EventLoopBackend { +Object parent; + +/* AioContext poll parameters */ +int64_t poll_max_ns; +int64_t poll_grow; +int64_t poll_shrink;
These parameters do not affect the main loop because it cannot poll. If you decide to keep them in the base class, please document that they have no effect on the main loop so users aren't confused. I would keep them unique to IOThread for now.
Re: [PATCH 1/3] util & iothread: Introduce event-loop abstract class
Quoted 13 lines · Stefan Hajnoczi
qom/meson.build qom_ss.add(files( · line 7
Quoted 9 lines · Nicolas Saenz Julienne
qom/meson.build +1
-0@@ -4,6 +4,7 @@ qom_ss.add(files( 'object.c', 'object_interfaces.c', 'qom-qobject.c', + '../util/event-loop.c',This looks strange. I expected
util/event-loop.cto be in util/meson.build and added to theutil_ssSourceSet instead ofqom_ss.
Or alternatively, to be in the root just like iothread.c.
Paolo
Quoted 41 lines
What is the reason for this?
util/event-loop.c
Quoted 8 lines
qom/meson.build +0
-0)) qmp_ss.add(files('qom-qmp-cmds.c'))util/event-loop.c new file +0
-0The naming is a little inconsistent. The filename "
event-loop.c" does match the QOM type or typedef name event-loop-backend/EventLoopBackend.I suggest calling the source file
event-loop-base.cand the QOM type "event-loop-base".util/event-loop.c line 7
Quoted 8 lines
util/event-loop.c new file +7
-0@@ -0,0 +1,142 @@ +/* +* QEMU event-loop backend +* +* Copyright (C) 2022 Red Hat Inc +* +* Authors: +* Nicolas Saenz Julienne <[email protected]> Most of the code is cut and pasted. It would be nice to carry over the authorship information too.
util/event-loop.c line 14
Quoted 7 lines
util/event-loop.c new file +7
-0+struct EventLoopBackend { +Object parent; + +/* AioContext poll parameters */ +int64_t poll_max_ns; +int64_t poll_grow; +int64_t poll_shrink;These parameters do not affect the main loop because it cannot poll. If you decide to keep them in the base class, please document that they have no effect on the main loop so users aren't confused. I would keep them unique to IOThread for now.
Re: [PATCH 1/3] util & iothread: Introduce event-loop abstract class
In reply to Stefan HajnocziHi Stefan, thanks for the review.
Quoted 14 lines · Stefan Hajnoczi
qom/meson.build qom_ss.add(files( · line 7
Quoted 9 lines · Nicolas Saenz Julienne
qom/meson.build +1
-0@@ -4,6 +4,7 @@ qom_ss.add(files( 'object.c', 'object_interfaces.c', 'qom-qobject.c', + '../util/event-loop.c',This looks strange. I expected
util/event-loop.cto be in util/meson.build and added to theutil_ssSourceSet instead ofqom_ss.
Quoted 1 line
What is the reason for this?
Sorry I meant to move it into the qom directory while cleaning up the series but forgot about it.
That said, I can see how moving 'event-loop-backend' in qom_ss isn't the cleanest.
So I tried moving it into util_ss, but for some reason nobody is calling 'type_init(even_loop_register_type)'. My guess is there's some compilation quirk I'm missing.
Any suggestions? I wonder if util_ss is the right spot for 'event-loop-backend' anyway, but I don't have a better idea.
Quoted 12 lines
util/event-loop.c
Quoted 8 lines
qom/meson.build +0
-0)) qmp_ss.add(files('qom-qmp-cmds.c'))util/event-loop.c new file +0
-0The naming is a little inconsistent. The filename "
event-loop.c" does match the QOM type or typedef name event-loop-backend/EventLoopBackend.
Quoted 2 lines
I suggest calling the source file
event-loop-base.cand the QOM type "event-loop-base".
Agree.
Quoted 11 lines
util/event-loop.c line 7
Quoted 8 lines
util/event-loop.c new file +7
-0@@ -0,0 +1,142 @@ +/* +* QEMU event-loop backend +* +* Copyright (C) 2022 Red Hat Inc +* +* Authors: +* Nicolas Saenz Julienne <[email protected]> Most of the code is cut and pasted. It would be nice to carry over the authorship information too.
Yes, of course.
Quoted 4 lines
Quoted 4 lines
util/event-loop.c new file +4
-0+struct EventLoopBackend { +Object parent; + +/* AioContext poll parameters */
Quoted 8 lines
util/event-loop.c line 14
Quoted 3 lines
util/event-loop.c new file +3
-0+int64_t poll_max_ns; +int64_t poll_grow; +int64_t poll_shrink;These parameters do not affect the main loop because it cannot poll. If you decide to keep them in the base class, please document that they have no effect on the main loop so users aren't confused. I would keep them unique to IOThread for now.
OK, I'll keep them unique then.
Thanks!
Signature · 1 line
Nicolás Sáenz
Re: [PATCH 1/3] util & iothread: Introduce event-loop abstract class
Quoted 15 lines · Nicolas Saenz Julienne
Quoted 14 lines · Stefan Hajnoczi
qom/meson.build qom_ss.add(files( · line 7
Quoted 9 lines · Nicolas Saenz Julienne
qom/meson.build +1
-0@@ -4,6 +4,7 @@ qom_ss.add(files( 'object.c', 'object_interfaces.c', 'qom-qobject.c', + '../util/event-loop.c',This looks strange. I expected
util/event-loop.cto be in util/meson.build and added to theutil_ssSourceSet instead ofqom_ss.
Quoted 7 lines
Quoted 1 line
What is the reason for this?
Sorry I meant to move it into the qom directory while cleaning up the series but forgot about it.
That said, I can see how moving 'event-loop-backend' in
qom_ssisn't the cleanest.
Yes, qom/ is meant for the QEMU Object Model infrastructure itself, not for all the QOM classes that rely on it.
Quoted 3 lines
So I tried moving it into
util_ss, but for some reason nobody is calling 'type_init(even_loop_register_type)'. My guess is there's some compilation quirk I'm missing.
Maybe the issue is that libqemuutil.a (util_ss) object files are linked on demand. If there are no symbol dependencies in the main QEMU code to event-loop.o then it won't be linked into the executable. That may be why event_loop_register_type() isn't being called (it's set up by an __attribute__((constructor)) function in event-loop.o so it doesn't help create a symbol dependency).
Quoted 2 lines
Any suggestions? I wonder if
util_ssis the right spot for 'event-loop-backend' anyway, but I don't have a better idea.
What Paolo suggested sounds good: move event-loop.c next to iothread.c in the top-level source directory.
Stefan
PATCH 2/3 util/main-loop: Introduce the main loop into QOM
In reply to Nicolas Saenz Julienne'event-loop-backend' provides basic property handling for all 'AioContext' based event loops. So let's define a new 'MainLoopClass' that inherits from it. This will permit tweaking the main loop's properties through qapi as well as through the command line using the '-object' keyword[1]. Only one instance of 'MainLoopClass' might be created at any time.
'EventLoopBackendClass' learns a new callback, 'can_be_deleted()' so as to mark 'MainLoop' as non-deletable.
[1] For example:
-object main-loop,id=main-loop,poll-max-ns=<value>
--- include/qemu/main-loop.h | 11 ++++++++++ qapi/qom.json | 10 ++++++---- qga/meson.build | 2 +- tests/unit/meson.build | 10 +++++----- util/event-loop.c | 13 ++++++++++++ util/event-loop.h | 1 + util/main-loop.c | 43 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 80 insertions(+), 10 deletions(-)
include/qemu/main-loop.h +11 -0
@@ -26,9 +26,20 @@#define QEMU_MAIN_LOOP_H #include "block/aio.h" +#include "qom/object.h" +#include "util/event-loop.h" #define SIG_IPI SIGUSR1 +#define TYPE_MAIN_LOOP "main-loop" + +struct MainLoop { +EventLoopBackend parent_obj; +}; +typedef struct MainLoop MainLoop ; + +DECLARE_INSTANCE_CHECKER (MainLoop,MAIN_LOOP ,TYPE_MAIN_LOOP ) +/** * qemu_init_main_loop: Set up the process so that it can run the main loop. *
qapi/qom.json +6 -4
@@ -500,9 +500,9 @@ '*grab-toggle': 'GrabToggleKeys' } } ## -# @IothreadProperties: +# @EventLoopBackendProperties: # -# Properties for iothread objects. +# Properties for iothread and main-loop objects. # # @poll-max-ns: the maximum number of nanoseconds to busy wait for events. # 0 means polling is disabled (default: 32768 on POSIX hosts,
@@ -522,7 +522,7 @@ # # Since: 2.0 ## -{ 'struct': 'IothreadProperties', +{ 'struct': 'EventLoopBackendProperties', 'data': { '*poll-max-ns': 'int', '*poll-grow': 'int', '*poll-shrink': 'int',
@@ -818,6 +818,7 @@ { 'name': 'input-linux', 'if': 'CONFIG_LINUX' }, 'iothread', + 'main-loop', { 'name': 'memory-backend-epc', 'if': 'CONFIG_LINUX' }, 'memory-backend-file',
@@ -882,7 +883,8 @@ 'input-barrier': 'InputBarrierProperties', 'input-linux': { 'type': 'InputLinuxProperties', 'if': 'CONFIG_LINUX' }, - 'iothread': 'IothreadProperties', + 'iothread': 'EventLoopBackendProperties', + 'main-loop': 'EventLoopBackendProperties', 'memory-backend-epc': { 'type': 'MemoryBackendEpcProperties', 'if': 'CONFIG_LINUX' }, 'memory-backend-file': 'MemoryBackendFileProperties',
qga/meson.build +1 -1
@@ -52,7 +52,7 @@ qga_ss = qga_ss.apply(config_host, strict: false) qga = executable('qemu-ga', qga_ss.sources(), link_args: config_host['LIBS_QGA'].split(), - dependencies: [qemuutil, libudev], + dependencies: [qemuutil, libudev, qom], install: true) all_qga = [qga]
tests/unit/meson.build +5 -5
@@ -51,7 +51,7 @@ tests = { if have_system or have_tools tests += { - 'test-qmp-event': [testqapi], + 'test-qmp-event': [testqapi, qom], } endif
@@ -120,17 +120,17 @@ endif if have_system tests += { 'test-iov': [], - 'test-qmp-cmds': [testqapi], + 'test-qmp-cmds': [testqapi, qom], 'test-xbzrle': [migration], - 'test-timed-average': [], - 'test-util-sockets': ['socket-helpers.c'], + 'test-timed-average': [qom], + 'test-util-sockets': ['socket-helpers.c', qom], 'test-base64': [], 'test-bufferiszero': [], 'test-vmstate': [migration, io], 'test-yank': ['socket-helpers.c', qom, io, chardev] } if config_host_data.get('CONFIG_INOTIFY1') - tests += {'test-util-filemonitor': []} + tests += {'test-util-filemonitor': [qom]} endif # Some tests: test-char, test-qdev-global-props, and test-qga,
util/event-loop.c +13 -0
@@ -98,10 +98,23 @@ event_loop_backend_complete(UserCreatable *uc, Error **errp) } } +static bool event_loop_backend_can_be_deleted (UserCreatable *uc) +{ +EventLoopBackendClass *bc =EVENT_LOOP_BACKEND_GET_CLASS (uc); +EventLoopBackend *backend =EVENT_LOOP_BACKEND (uc); + +if (bc->can_be_deleted) { +return bc->can_be_deleted (backend); + } + +return true; +} +static void event_loop_backend_class_init (ObjectClass *klass,void *class_data) {UserCreatableClass *ucc =USER_CREATABLE_CLASS (klass); ucc->complete = event_loop_backend_complete; + ucc->can_be_deleted = event_loop_backend_can_be_deleted; object_class_property_add(klass, "poll-max-ns", "int", event_loop_backend_get_param,
util/event-loop.h +1 -0
@@ -24,6 +24,7 @@ struct EventLoopBackendClass {ObjectClass parent_class;void (*init)(EventLoopBackend *backend,Error **errp); +bool (*can_be_deleted)(EventLoopBackend *backend); };struct EventLoopBackend {
util/main-loop.c +43 -0
55 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [PATCH 2/3] util/main-loop: Introduce the main loop into QOM
Quoted 7 lines · Nicolas Saenz Julienne
'event-loop-backend' provides basic property handling for all 'AioContext' based event loops. So let's define a new 'MainLoopClass' that inherits from it. This will permit tweaking the main loop's properties through qapi as well as through the command line using the '-object' keyword[1]. Only one instance of 'MainLoopClass' might be created at any time.
Quoted 7 lines
'EventLoopBackendClass' learns a new callback, '
can_be_deleted()' so as to mark 'MainLoop' as non-deletable.Signed-off-by Nicolas Saenz Julienne <[email protected]>[1] For example:
-object main-loop,id=main-loop,poll-max-ns=<value>
QAPI schema
Re: [PATCH 2/3] util/main-loop: Introduce the main loop into QOM
In reply to Nicolas Saenz JulienneQuoted 11 lines · Nicolas Saenz Julienne
include/qemu/main-loop.h +2
-0@@ -26,9 +26,20 @@#define QEMU_MAIN_LOOP_H #include "block/aio.h" +#include "qom/object.h" +#include "util/event-loop.h"
include/qemu/main-loop.h line 41
Quoted 10 lines
include/qemu/main-loop.h +8
-0#define SIG_IPI SIGUSR1 +#define TYPE_MAIN_LOOP "main-loop" + +struct MainLoop { +EventLoopBackend parent_obj; +}; +typedef struct MainLoop MainLoop ; + +DECLARE_INSTANCE_CHECKER (MainLoop,MAIN_LOOP ,TYPE_MAIN_LOOP )
* Direct usage of this macro should be avoided, and the complete
* OBJECT_DECLARE_TYPE macro is recommended instead.
Is there a reason for using DECLARE_INSTANCE_CHECKER() instead of OBJECT_DECLARE_TYPE()?
include/qemu/main-loop.h line 890
Quoted 10 lines
include/qemu/main-loop.h +2
-1@@ -882,7 +883,8 @@ 'input-barrier': 'InputBarrierProperties', 'input-linux': { 'type': 'InputLinuxProperties', 'if': 'CONFIG_LINUX' }, - 'iothread': 'IothreadProperties', + 'iothread': 'EventLoopBackendProperties', + 'main-loop': 'EventLoopBackendProperties', 'memory-backend-epc': { 'type': 'MemoryBackendEpcProperties', 'if': 'CONFIG_LINUX' }, 'memory-backend-file': 'MemoryBackendFileProperties',
Does this commit the QAPI schema to keeping iothread and main-loop properties identical or can they diverge over time, if necessary?
I think we have the freedom to switch the QAPI schema to different structs for iothread and main-loop in the future because QMP clients aren't supposed to rely on the exact type, but I wanted to double-check.
qga/meson.build qga_ss = qga_ss.apply(config_host, strict: false) · line 55
Quoted 10 lines
qga/meson.build +1
-1@@ -52,7 +52,7 @@ qga_ss = qga_ss.apply(config_host, strict: false) qga = executable('qemu-ga', qga_ss.sources(), link_args: config_host['LIBS_QGA'].split(), - dependencies: [qemuutil, libudev], + dependencies: [qemuutil, libudev, qom],
Looks like a change because the first patch added the base class to qom instead of qemuutil. Maybe this can be undone if the base class is added to qemuutil instead.
Re: [PATCH 2/3] util/main-loop: Introduce the main loop into QOM
Quoted 26 lines · Stefan Hajnoczi
Quoted 11 lines · Nicolas Saenz Julienne
include/qemu/main-loop.h +2
-0@@ -26,9 +26,20 @@#define QEMU_MAIN_LOOP_H #include "block/aio.h" +#include "qom/object.h" +#include "util/event-loop.h" include/qemu/main-loop.h line 41
Quoted 10 lines
include/qemu/main-loop.h +8
-0#define SIG_IPI SIGUSR1 +#define TYPE_MAIN_LOOP "main-loop" + +struct MainLoop { +EventLoopBackend parent_obj; +}; +typedef struct MainLoop MainLoop ; + +DECLARE_INSTANCE_CHECKER (MainLoop,MAIN_LOOP ,TYPE_MAIN_LOOP )* Direct usage of this macro should be avoided, and the complete
*OBJECT_DECLARE_TYPEmacro is recommended instead.
Quoted 2 lines
Is there a reason for using
DECLARE_INSTANCE_CHECKER()instead ofOBJECT_DECLARE_TYPE()?
No, bad copying on my part, I'll change it.
[...]
Quoted 6 lines
Quoted 6 lines
qga/meson.build +0
-0@@ -52,7 +52,7 @@ qga_ss = qga_ss.apply(config_host, strict: false)
Quoted 8 lines
qga/meson.build qga_ss = qga_ss.apply(config_host, strict: false) · line 55
Quoted 4 lines
qga/meson.build +1
-1qga = executable('qemu-ga', qga_ss.sources(), link_args: config_host['LIBS_QGA'].split(), - dependencies: [qemuutil, libudev], + dependencies: [qemuutil, libudev, qom],Looks like a change because the first patch added the base class to qom instead of qemuutil. Maybe this can be undone if the base class is added to qemuutil instead.
I'm looking into it.
Signature · 1 line
Nicolás Sáenz
PATCH 3/3 util/event-loop: Introduce options to set the thread pool size
In reply to Nicolas Saenz JulienneThe thread pool regulates itself: when idle, it kills threads until empty, when in demand, it creates new threads until full. This behaviour doesn't play well with latency sensitive workloads where the price of creating a new thread is too high. For example, when paired with qemu's '-mlock', or using safety features like SafeStack, creating a new thread has been measured take multiple milliseconds.
In order to mitigate this let's introduce a new 'EventLoopBackend' property to set the thread pool size. The threads will be created during the pool's initialization, remain available during its lifetime regardless of demand, and destroyed upon freeing it. A properly characterized workload will then be able to configure the pool to avoid any latency spike.
--- include/block/aio.h | 11 +++++++++++ qapi/qom.json | 4 +++- util/async.c | 3 +++ util/event-loop.c | 15 ++++++++++++++- util/event-loop.h | 4 ++++ util/main-loop.c | 13 +++++++++++++ util/thread-pool.c | 41 +++++++++++++++++++++++++++++++++++++---- 7 files changed, 85 insertions(+), 6 deletions(-)
include/block/aio.h +11 -0
@@ -192,6 +192,8 @@ struct AioContext { QSLIST_HEAD(, Coroutine) scheduled_coroutines;QEMUBH *co_schedule_bh; +int pool_min; +int pool_max;/* Thread pool for performing work and receiving completion callbacks. * Has its own locking. */
@@ -769,4 +771,13 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,void aio_context_set_aio_params (AioContext *ctx,int64_t max_batch,Error **errp); +/** +* aio_context_set_thread_pool_params: +* @ctx: the aio context +* @min: Thread pool's min size, 0 by default +* @max: Thread pool's max size, 64 by default +*/ +void aio_context_set_thread_pool_params (AioContext *ctx,uint64_t min, +uint64_t max,Error **errp); +#endif
qapi/qom.json +3 -1
@@ -526,7 +526,9 @@ 'data': { '*poll-max-ns': 'int', '*poll-grow': 'int', '*poll-shrink': 'int', - '*aio-max-batch': 'int' } } + '*aio-max-batch': 'int', + '*thread-pool-min': 'int', + '*thread-pool-max': 'int' } } ## # @MemoryBackendProperties:
util/async.c +3 -0
@@ -562,6 +562,9 @@ AioContext *aio_context_new(Error **errp) ctx->aio_max_batch =0 ; + ctx->pool_min =0 ; + ctx->pool_max =64 ; +return ctx; fail:g_source_destroy (&ctx->source);
util/event-loop.c +14 -1
@@ -51,6 +51,12 @@ static EventLoopBackendParamInfo poll_shrink_info = {static EventLoopBackendParamInfo aio_max_batch_info = {"aio-max-batch" , offsetof(EventLoopBackend , aio_max_batch), }; +static EventLoopBackendParamInfo thread_pool_min_info = { +"thread-pool-min" , offsetof(EventLoopBackend , thread_pool_min), +}; +static EventLoopBackendParamInfo thread_pool_max_info = { +"thread-pool-max" , offsetof(EventLoopBackend , thread_pool_max), +};static void event_loop_backend_get_param (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp)
@@ -84,7 +90,6 @@ static void event_loop_backend_set_param(Object *obj, Visitor *v, *field = value;return ; - } static void
@@ -132,6 +137,14 @@ static void event_loop_backend_class_init(ObjectClass *klass, void *class_data) event_loop_backend_get_param, event_loop_backend_set_param, NULL, &aio_max_batch_info); +object_class_property_add (klass,"thread-pool-min" ,"int" , + event_loop_backend_get_param, + event_loop_backend_set_param, +NULL , &thread_pool_min_info); +object_class_property_add (klass,"thread-pool-max" ,"int" , + event_loop_backend_get_param, + event_loop_backend_set_param, +NULL , &thread_pool_max_info); }static const TypeInfo event_loop_backend_info = {
util/event-loop.h +4 -0
@@ -37,5 +37,9 @@ struct EventLoopBackend {/* AioContext AIO engine parameters */ int64_t aio_max_batch; + +/* AioContext thread pool parameters */ +int64_t thread_pool_min; + int64_t thread_pool_max; };#endif
util/main-loop.c +13 -0
@@ -190,12 +190,25 @@ MainLoop *mloop;static void main_loop_init (EventLoopBackend *bc,Error **errp) {MainLoop *m =MAIN_LOOP (bc); +Error *local_error =NULL ; + +if (!qemu_aio_context) { +error_setg (errp,"qemu aio context not ready" ); +return ; + }if (mloop) {error_setg (errp,"only one main-loop instance allowed" );return ; } +aio_context_set_thread_pool_params (qemu_aio_context, bc->thread_pool_min, + bc->thread_pool_max, &local_error); +if (local_error) { +error_propagate (errp, local_error); +return ; + } + mloop = m;return ; }
util/thread-pool.c +37 -4
90 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [PATCH 3/3] util/event-loop: Introduce options to set the thread pool size
Quoted 30 lines · Nicolas Saenz Julienne
The thread pool regulates itself: when idle, it kills threads until empty, when in demand, it creates new threads until full. This behaviour doesn't play well with latency sensitive workloads where the price of creating a new thread is too high. For example, when paired with qemu's '-mlock', or using safety features like SafeStack, creating a new thread has been measured take multiple milliseconds.
In order to mitigate this let's introduce a new 'EventLoopBackend' property to set the thread pool size. The threads will be created during the pool's initialization, remain available during its lifetime regardless of demand, and destroyed upon freeing it. A properly characterized workload will then be able to configure the pool to avoid any latency spike.
Signed-off-by Nicolas Saenz Julienne <[email protected]>--- include/block/aio.h | 11 +++++++++++ qapi/qom.json | 4 +++- util/async.c | 3 +++ util/event-loop.c | 15 ++++++++++++++- util/event-loop.h | 4 ++++ util/main-loop.c | 13 +++++++++++++ util/thread-pool.c | 41 +++++++++++++++++++++++++++++++++++++---- 7 files changed, 85 insertions(+), 6 deletions(-)include/block/aio.h +0
-0
include/block/aio.h struct AioContext { · line 196
Quoted 5 lines
include/block/aio.h +2
-0@@ -192,6 +192,8 @@ struct AioContext { QSLIST_HEAD(, Coroutine) scheduled_coroutines;QEMUBH *co_schedule_bh; +int pool_min; +int pool_max;
Are these fields protected by ThreadPool->lock? Please document. This is a clue that maybe these fields belong in ThreadPool.
Regarding the field names: the AioContext thread pool field is called thread_pool and the user-visible parameters are thread-pool-min/max. I suggest calling the fields thread_pool_min/max too so it's clear which pool we're talking about and there is a correspondence to user-visible parameters.
include/block/aio.h void thread_pool_free(ThreadPool *pool) · line 367
Quoted 11 lines
include/block/aio.h +7
-0@@ -350,3 +358,28 @@ void thread_pool_free(ThreadPool *pool)qemu_mutex_destroy (&pool->lock);g_free (pool); } + +void aio_context_set_thread_pool_params (AioContext *ctx,uint64_t min, +uint64_t max,Error **errp) +{ +ThreadPool *pool = ctx->thread_pool; + +if (min > max || !max) {
ctx->pool_min/max are int while the min/max arguments are uint64_t. Please add an INT_MAX check to detect overflow.
include/block/aio.h void thread_pool_free(ThreadPool *pool) · line 374
Quoted 7 lines
include/block/aio.h +7
-0+error_setg (errp,"bad thread-pool-min/thread-pool-max values" ); +return ; + } + +if (pool) { +qemu_mutex_lock (&pool->lock); + }
This code belongs in util/thread-pool.c. I guess the reason for keeping the fields in AioContext instead of ThreadPool is because the ThreadPool is created on demand and we'd have nowhere to store the parameter value. I suggest we bite the bullet and keep an extra copy of the variables in AioContext with a clean ThreadPool interface (thread_pool_set_params()) instead of letting AioContext and ThreadPool access each other's internals.
include/block/aio.h void thread_pool_free(ThreadPool *pool) · line 382
Quoted 8 lines
include/block/aio.h +8
-0+ + ctx->pool_min = min; + ctx->pool_max = max; + +if (pool) { +for (int i = pool->cur_threads; i < ctx->pool_min; i++) { +spawn_thread (pool); + }
What about the reverse: when min is lowered and there are a bunch of idle worker threads we could wake them up so they terminate until ->pool_min is reached again?
Re: [PATCH 3/3] util/event-loop: Introduce options to set the thread pool size
Quoted 34 lines · Stefan Hajnoczi
Quoted 33 lines · Nicolas Saenz Julienne
The thread pool regulates itself: when idle, it kills threads until empty, when in demand, it creates new threads until full. This behaviour doesn't play well with latency sensitive workloads where the price of creating a new thread is too high. For example, when paired with qemu's '-mlock', or using safety features like SafeStack, creating a new thread has been measured take multiple milliseconds.
In order to mitigate this let's introduce a new 'EventLoopBackend' property to set the thread pool size. The threads will be created during the pool's initialization, remain available during its lifetime regardless of demand, and destroyed upon freeing it. A properly characterized workload will then be able to configure the pool to avoid any latency spike.
Signed-off-by Nicolas Saenz Julienne <[email protected]>--- include/block/aio.h | 11 +++++++++++ qapi/qom.json | 4 +++- util/async.c | 3 +++ util/event-loop.c | 15 ++++++++++++++- util/event-loop.h | 4 ++++ util/main-loop.c | 13 +++++++++++++ util/thread-pool.c | 41 +++++++++++++++++++++++++++++++++++++---- 7 files changed, 85 insertions(+), 6 deletions(-)include/block/aio.h +0
-0@@ -192,6 +192,8 @@ struct AioContext { QSLIST_HEAD(, Coroutine) scheduled_coroutines;QEMUBH *co_schedule_bh;
Quoted 5 lines
include/block/aio.h struct AioContext { · line 196
Quoted 2 lines
include/block/aio.h +2
-0+int pool_min; +int pool_max;Are these fields protected by
ThreadPool->lock? Please document. This is a clue that maybe these fields belong in ThreadPool.
Yes they are. I'll document it properly.
Quoted 5 lines
Regarding the field names: the AioContext thread pool field is called
thread_pooland the user-visible parameters are thread-pool-min/max. I suggest calling the fields thread_pool_min/max too so it's clear which pool we're talking about and there is a correspondence to user-visible parameters.
Noted.
Quoted 6 lines
Quoted 6 lines
include/block/aio.h +2
-0@@ -350,3 +358,28 @@ void thread_pool_free(ThreadPool *pool)qemu_mutex_destroy (&pool->lock);g_free (pool); } + +void aio_context_set_thread_pool_params(AioContext *ctx, uint64_t min,
Quoted 8 lines
include/block/aio.h void thread_pool_free(ThreadPool *pool) · line 367
Quoted 5 lines
include/block/aio.h +5
-0+ uint64_t max, Error **errp) +{ + ThreadPool *pool = ctx->thread_pool; + +if (min > max || !max) {ctx->pool_min/max are int while the min/max arguments are
uint64_t. Please add anINT_MAXcheck to detect overflow.
Noted.
Quoted 11 lines
include/block/aio.h void thread_pool_free(ThreadPool *pool) · line 374
Quoted 7 lines
include/block/aio.h +7
-0+error_setg (errp,"bad thread-pool-min/thread-pool-max values" ); +return ; + } + +if (pool) { +qemu_mutex_lock (&pool->lock); + }This code belongs in
util/thread-pool.c. I guess the reason for keeping the fields in AioContext instead of ThreadPool is because the ThreadPool is created on demand and we'd have nowhere to store the parameter value.
Indeed.
Quoted 4 lines
I suggest we bite the bullet and keep an extra copy of the variables in AioContext with a clean ThreadPool interface (
thread_pool_set_params()) instead of letting AioContext and ThreadPool access each other's internals.
OK!
Quoted 4 lines
Quoted 4 lines
include/block/aio.h +4
-0+ + ctx->pool_min = min; + ctx->pool_max = max; +
Quoted 8 lines
include/block/aio.h void thread_pool_free(ThreadPool *pool) · line 382
Quoted 4 lines
include/block/aio.h +4
-0+if (pool) { +for (int i = pool->cur_threads; i < ctx->pool_min; i++) { +spawn_thread (pool); + }What about the reverse: when min is lowered and there are a bunch of idle worker threads we could wake them up so they terminate until ->pool_min is reached again?
Makes sense, I'll look into it.
Signature · 1 line
Nicolás Sáenz