]> git.proxmox.com Git - mirror_qemu.git/blame - iothread.c
migration: save_compress_page() can take block through pss
[mirror_qemu.git] / iothread.c
CommitLineData
be8d8537
SH
1/*
2 * Event loop thread
3 *
c3033fd3 4 * Copyright Red Hat Inc., 2013, 2020
be8d8537
SH
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 *
12 */
13
d38ea87a 14#include "qemu/osdep.h"
be8d8537
SH
15#include "qom/object.h"
16#include "qom/object_interfaces.h"
17#include "qemu/module.h"
be8d8537 18#include "block/aio.h"
d16341fa 19#include "block/block.h"
7d5983e3 20#include "sysemu/event-loop-base.h"
be8d8537 21#include "sysemu/iothread.h"
e688df6b 22#include "qapi/error.h"
112ed241 23#include "qapi/qapi-commands-misc.h"
2f78e491 24#include "qemu/error-report.h"
ab28bd23 25#include "qemu/rcu.h"
e4370165 26#include "qemu/main-loop.h"
be8d8537 27
be8d8537 28
90c558be 29#ifdef CONFIG_POSIX
cdd7abfd
SH
30/* Benchmark results from 2016 on NVMe SSD drives show max polling times around
31 * 16-32 microseconds yield IOPS improvements for both iodepth=1 and iodepth=32
32 * workloads.
33 */
34#define IOTHREAD_POLL_MAX_NS_DEFAULT 32768ULL
90c558be
PX
35#else
36#define IOTHREAD_POLL_MAX_NS_DEFAULT 0ULL
37#endif
cdd7abfd 38
be8d8537
SH
39static void *iothread_run(void *opaque)
40{
41 IOThread *iothread = opaque;
42
ab28bd23 43 rcu_register_thread();
b60ec76a
PX
44 /*
45 * g_main_context_push_thread_default() must be called before anything
46 * in this new thread uses glib.
47 */
48 g_main_context_push_thread_default(iothread->worker_context);
5f50be9b 49 qemu_set_current_aio_context(iothread->ctx);
88eb7c29 50 iothread->thread_id = qemu_get_thread_id();
21c4d15b 51 qemu_sem_post(&iothread->init_done_sem);
88eb7c29 52
2362a28e 53 while (iothread->running) {
6ca20620
PX
54 /*
55 * Note: from functional-wise the g_main_loop_run() below can
56 * already cover the aio_poll() events, but we can't run the
57 * main loop unconditionally because explicit aio_poll() here
58 * is faster than g_main_loop_run() when we do not need the
59 * gcontext at all (e.g., pure block layer iothreads). In
60 * other words, when we want to run the gcontext with the
61 * iothread we need to pay some performance for functionality.
62 */
65c1b5b6 63 aio_poll(iothread->ctx, true);
329163cb 64
6c95363d
PX
65 /*
66 * We must check the running state again in case it was
67 * changed in previous aio_poll()
68 */
d73415a3 69 if (iothread->running && qatomic_read(&iothread->run_gcontext)) {
329163cb 70 g_main_loop_run(iothread->main_loop);
329163cb 71 }
be8d8537 72 }
ab28bd23 73
b60ec76a 74 g_main_context_pop_thread_default(iothread->worker_context);
ab28bd23 75 rcu_unregister_thread();
be8d8537
SH
76 return NULL;
77}
78
2362a28e
SH
79/* Runs in iothread_run() thread */
80static void iothread_stop_bh(void *opaque)
81{
82 IOThread *iothread = opaque;
83
84 iothread->running = false; /* stop iothread_run() */
85
86 if (iothread->main_loop) {
87 g_main_loop_quit(iothread->main_loop);
88 }
89}
90
82d90705 91void iothread_stop(IOThread *iothread)
be8d8537 92{
82d90705
PX
93 if (!iothread->ctx || iothread->stopping) {
94 return;
2f78e491 95 }
be8d8537 96 iothread->stopping = true;
2362a28e 97 aio_bh_schedule_oneshot(iothread->ctx, iothread_stop_bh, iothread);
be8d8537 98 qemu_thread_join(&iothread->thread);
82d90705
PX
99}
100
cdd7abfd
SH
101static void iothread_instance_init(Object *obj)
102{
103 IOThread *iothread = IOTHREAD(obj);
104
105 iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
14a2d118 106 iothread->thread_id = -1;
21c4d15b 107 qemu_sem_init(&iothread->init_done_sem, 0);
b506e0f1 108 /* By default, we don't run gcontext */
d73415a3 109 qatomic_set(&iothread->run_gcontext, 0);
cdd7abfd
SH
110}
111
dce8921b
FZ
112static void iothread_instance_finalize(Object *obj)
113{
114 IOThread *iothread = IOTHREAD(obj);
115
82d90705 116 iothread_stop(iothread);
14a2d118 117
15544349
PX
118 /*
119 * Before glib2 2.33.10, there is a glib2 bug that GSource context
120 * pointer may not be cleared even if the context has already been
121 * destroyed (while it should). Here let's free the AIO context
122 * earlier to bypass that glib bug.
123 *
124 * We can remove this comment after the minimum supported glib2
125 * version boosts to 2.33.10. Before that, let's free the
126 * GSources first before destroying any GMainContext.
127 */
128 if (iothread->ctx) {
129 aio_context_unref(iothread->ctx);
130 iothread->ctx = NULL;
131 }
5b3ac23f
PX
132 if (iothread->worker_context) {
133 g_main_context_unref(iothread->worker_context);
134 iothread->worker_context = NULL;
0bd2d233
PX
135 g_main_loop_unref(iothread->main_loop);
136 iothread->main_loop = NULL;
5b3ac23f 137 }
21c4d15b 138 qemu_sem_destroy(&iothread->init_done_sem);
be8d8537
SH
139}
140
1f14c914 141static void iothread_init_gcontext(IOThread *iothread, const char *thread_name)
b506e0f1
PX
142{
143 GSource *source;
1f14c914 144 g_autofree char *name = g_strdup_printf("%s aio-context", thread_name);
b506e0f1
PX
145
146 iothread->worker_context = g_main_context_new();
147 source = aio_get_g_source(iothread_get_aio_context(iothread));
1f14c914 148 g_source_set_name(source, name);
b506e0f1
PX
149 g_source_attach(source, iothread->worker_context);
150 g_source_unref(source);
0bd2d233 151 iothread->main_loop = g_main_loop_new(iothread->worker_context, TRUE);
b506e0f1
PX
152}
153
7d5983e3 154static void iothread_set_aio_context_params(EventLoopBase *base, Error **errp)
1793ad02
SG
155{
156 ERRP_GUARD();
05e385d2 157 IOThread *iothread = IOTHREAD(base);
1793ad02 158
7d5983e3
NSJ
159 if (!iothread->ctx) {
160 return;
161 }
162
1793ad02
SG
163 aio_context_set_poll_params(iothread->ctx,
164 iothread->poll_max_ns,
165 iothread->poll_grow,
166 iothread->poll_shrink,
167 errp);
168 if (*errp) {
169 return;
170 }
171
172 aio_context_set_aio_params(iothread->ctx,
7d5983e3 173 iothread->parent_obj.aio_max_batch,
1793ad02 174 errp);
71ad4713
NSJ
175
176 aio_context_set_thread_pool_params(iothread->ctx, base->thread_pool_min,
177 base->thread_pool_max, errp);
1793ad02
SG
178}
179
7d5983e3
NSJ
180
181static void iothread_init(EventLoopBase *base, Error **errp)
be8d8537 182{
2f78e491 183 Error *local_error = NULL;
7d5983e3 184 IOThread *iothread = IOTHREAD(base);
1f14c914 185 g_autofree char *thread_name = NULL;
be8d8537
SH
186
187 iothread->stopping = false;
2362a28e 188 iothread->running = true;
668f62ec 189 iothread->ctx = aio_context_new(errp);
2f78e491 190 if (!iothread->ctx) {
2f78e491
CN
191 return;
192 }
88eb7c29 193
1f14c914
FR
194 thread_name = g_strdup_printf("IO %s",
195 object_get_canonical_path_component(OBJECT(base)));
196
b506e0f1
PX
197 /*
198 * Init one GMainContext for the iothread unconditionally, even if
199 * it's not used
200 */
1f14c914 201 iothread_init_gcontext(iothread, thread_name);
b506e0f1 202
7d5983e3 203 iothread_set_aio_context_params(base, &local_error);
0d9d86fb
SH
204 if (local_error) {
205 error_propagate(errp, local_error);
206 aio_context_unref(iothread->ctx);
207 iothread->ctx = NULL;
208 return;
209 }
210
be8d8537
SH
211 /* This assumes we are called from a thread with useful CPU affinity for us
212 * to inherit.
213 */
d21e8776 214 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
be8d8537 215 iothread, QEMU_THREAD_JOINABLE);
88eb7c29
SH
216
217 /* Wait for initialization to complete */
88eb7c29 218 while (iothread->thread_id == -1) {
21c4d15b 219 qemu_sem_wait(&iothread->init_done_sem);
88eb7c29 220 }
be8d8537
SH
221}
222
5e5db499
SH
223typedef struct {
224 const char *name;
225 ptrdiff_t offset; /* field's byte offset in IOThread struct */
f0ed36a6 226} IOThreadParamInfo;
5e5db499 227
f0ed36a6 228static IOThreadParamInfo poll_max_ns_info = {
5e5db499
SH
229 "poll-max-ns", offsetof(IOThread, poll_max_ns),
230};
f0ed36a6 231static IOThreadParamInfo poll_grow_info = {
5e5db499
SH
232 "poll-grow", offsetof(IOThread, poll_grow),
233};
f0ed36a6 234static IOThreadParamInfo poll_shrink_info = {
5e5db499
SH
235 "poll-shrink", offsetof(IOThread, poll_shrink),
236};
237
0445409d 238static void iothread_get_param(Object *obj, Visitor *v,
1cc7eada 239 const char *name, IOThreadParamInfo *info, Error **errp)
0d9d86fb
SH
240{
241 IOThread *iothread = IOTHREAD(obj);
5e5db499 242 int64_t *field = (void *)iothread + info->offset;
0d9d86fb 243
5e5db499 244 visit_type_int64(v, name, field, errp);
0d9d86fb
SH
245}
246
0445409d 247static bool iothread_set_param(Object *obj, Visitor *v,
1cc7eada 248 const char *name, IOThreadParamInfo *info, Error **errp)
0d9d86fb
SH
249{
250 IOThread *iothread = IOTHREAD(obj);
5e5db499 251 int64_t *field = (void *)iothread + info->offset;
0d9d86fb
SH
252 int64_t value;
253
668f62ec 254 if (!visit_type_int64(v, name, &value, errp)) {
0445409d 255 return false;
0d9d86fb
SH
256 }
257
258 if (value < 0) {
dcfe4805 259 error_setg(errp, "%s value must be in range [0, %" PRId64 "]",
5e5db499 260 info->name, INT64_MAX);
0445409d 261 return false;
0d9d86fb
SH
262 }
263
5e5db499 264 *field = value;
0d9d86fb 265
0445409d
SG
266 return true;
267}
268
269static void iothread_get_poll_param(Object *obj, Visitor *v,
270 const char *name, void *opaque, Error **errp)
271{
1cc7eada 272 IOThreadParamInfo *info = opaque;
0445409d 273
1cc7eada 274 iothread_get_param(obj, v, name, info, errp);
0445409d
SG
275}
276
277static void iothread_set_poll_param(Object *obj, Visitor *v,
278 const char *name, void *opaque, Error **errp)
279{
280 IOThread *iothread = IOTHREAD(obj);
1cc7eada 281 IOThreadParamInfo *info = opaque;
0445409d 282
1cc7eada 283 if (!iothread_set_param(obj, v, name, info, errp)) {
0445409d
SG
284 return;
285 }
286
0d9d86fb 287 if (iothread->ctx) {
5e5db499
SH
288 aio_context_set_poll_params(iothread->ctx,
289 iothread->poll_max_ns,
290 iothread->poll_grow,
291 iothread->poll_shrink,
dcfe4805 292 errp);
0d9d86fb 293 }
0d9d86fb
SH
294}
295
be8d8537
SH
296static void iothread_class_init(ObjectClass *klass, void *class_data)
297{
7d5983e3
NSJ
298 EventLoopBaseClass *bc = EVENT_LOOP_BASE_CLASS(klass);
299
300 bc->init = iothread_init;
301 bc->update_params = iothread_set_aio_context_params;
0d9d86fb
SH
302
303 object_class_property_add(klass, "poll-max-ns", "int",
5e5db499
SH
304 iothread_get_poll_param,
305 iothread_set_poll_param,
d2623129 306 NULL, &poll_max_ns_info);
5e5db499
SH
307 object_class_property_add(klass, "poll-grow", "int",
308 iothread_get_poll_param,
309 iothread_set_poll_param,
d2623129 310 NULL, &poll_grow_info);
5e5db499
SH
311 object_class_property_add(klass, "poll-shrink", "int",
312 iothread_get_poll_param,
313 iothread_set_poll_param,
d2623129 314 NULL, &poll_shrink_info);
be8d8537
SH
315}
316
317static const TypeInfo iothread_info = {
318 .name = TYPE_IOTHREAD,
7d5983e3 319 .parent = TYPE_EVENT_LOOP_BASE,
be8d8537
SH
320 .class_init = iothread_class_init,
321 .instance_size = sizeof(IOThread),
cdd7abfd 322 .instance_init = iothread_instance_init,
be8d8537 323 .instance_finalize = iothread_instance_finalize,
be8d8537
SH
324};
325
326static void iothread_register_types(void)
327{
328 type_register_static(&iothread_info);
329}
330
331type_init(iothread_register_types)
332
be8d8537
SH
333char *iothread_get_id(IOThread *iothread)
334{
7a309cc9 335 return g_strdup(object_get_canonical_path_component(OBJECT(iothread)));
be8d8537
SH
336}
337
338AioContext *iothread_get_aio_context(IOThread *iothread)
339{
340 return iothread->ctx;
341}
dc3dd0d2
SH
342
343static int query_one_iothread(Object *object, void *opaque)
344{
c3033fd3 345 IOThreadInfoList ***tail = opaque;
dc3dd0d2
SH
346 IOThreadInfo *info;
347 IOThread *iothread;
348
349 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
350 if (!iothread) {
351 return 0;
352 }
353
354 info = g_new0(IOThreadInfo, 1);
355 info->id = iothread_get_id(iothread);
356 info->thread_id = iothread->thread_id;
5fc00480
PH
357 info->poll_max_ns = iothread->poll_max_ns;
358 info->poll_grow = iothread->poll_grow;
359 info->poll_shrink = iothread->poll_shrink;
7d5983e3 360 info->aio_max_batch = iothread->parent_obj.aio_max_batch;
dc3dd0d2 361
c3033fd3 362 QAPI_LIST_APPEND(*tail, info);
dc3dd0d2
SH
363 return 0;
364}
365
366IOThreadInfoList *qmp_query_iothreads(Error **errp)
367{
368 IOThreadInfoList *head = NULL;
369 IOThreadInfoList **prev = &head;
bc2256c4 370 Object *container = object_get_objects_root();
dc3dd0d2
SH
371
372 object_child_foreach(container, query_one_iothread, &prev);
373 return head;
374}
dce8921b 375
329163cb
WY
376GMainContext *iothread_get_g_main_context(IOThread *iothread)
377{
d73415a3 378 qatomic_set(&iothread->run_gcontext, 1);
b506e0f1 379 aio_notify(iothread->ctx);
329163cb
WY
380 return iothread->worker_context;
381}
0173e21b
PX
382
383IOThread *iothread_create(const char *id, Error **errp)
384{
385 Object *obj;
386
387 obj = object_new_with_props(TYPE_IOTHREAD,
388 object_get_internal_root(),
389 id, errp, NULL);
390
391 return IOTHREAD(obj);
392}
393
394void iothread_destroy(IOThread *iothread)
395{
396 object_unparent(OBJECT(iothread));
397}
fbcc6923
SH
398
399/* Lookup IOThread by its id. Only finds user-created objects, not internal
400 * iothread_create() objects. */
401IOThread *iothread_by_id(const char *id)
402{
403 return IOTHREAD(object_resolve_path_type(id, TYPE_IOTHREAD, NULL));
404}
ad22c308
EU
405
406bool qemu_in_iothread(void)
407{
408 return qemu_get_current_aio_context() == qemu_get_aio_context() ?
409 false : true;
410}