blob: d8b6c1fb27b9499176da71229d88d2ad91c43265 [file] [log] [blame]
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +01001/*
2 * Event loop thread
3 *
4 * Copyright Red Hat Inc., 2013
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
Peter Maydelld38ea872016-01-29 17:50:05 +000014#include "qemu/osdep.h"
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010015#include "qom/object.h"
16#include "qom/object_interfaces.h"
17#include "qemu/module.h"
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010018#include "block/aio.h"
Paolo Bonzinid16341f2016-10-27 12:49:00 +020019#include "block/block.h"
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010020#include "sysemu/iothread.h"
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +010021#include "qmp-commands.h"
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030022#include "qemu/error-report.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020023#include "qemu/rcu.h"
Paolo Bonzinie4370162016-10-27 12:48:59 +020024#include "qemu/main-loop.h"
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010025
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010026typedef ObjectClass IOThreadClass;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010027
28#define IOTHREAD_GET_CLASS(obj) \
29 OBJECT_GET_CLASS(IOThreadClass, obj, TYPE_IOTHREAD)
30#define IOTHREAD_CLASS(klass) \
31 OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD)
32
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +000033/* Benchmark results from 2016 on NVMe SSD drives show max polling times around
34 * 16-32 microseconds yield IOPS improvements for both iodepth=1 and iodepth=32
35 * workloads.
36 */
37#define IOTHREAD_POLL_MAX_NS_DEFAULT 32768ULL
38
Paolo Bonzinie4370162016-10-27 12:48:59 +020039static __thread IOThread *my_iothread;
40
41AioContext *qemu_get_current_aio_context(void)
42{
43 return my_iothread ? my_iothread->ctx : qemu_get_aio_context();
44}
45
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010046static void *iothread_run(void *opaque)
47{
48 IOThread *iothread = opaque;
49
Paolo Bonziniab28bd22015-07-09 08:55:38 +020050 rcu_register_thread();
51
Paolo Bonzinie4370162016-10-27 12:48:59 +020052 my_iothread = iothread;
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010053 qemu_mutex_lock(&iothread->init_done_lock);
54 iothread->thread_id = qemu_get_thread_id();
55 qemu_cond_signal(&iothread->init_done_cond);
56 qemu_mutex_unlock(&iothread->init_done_lock);
57
Stefan Hajnoczi2362a282017-12-07 20:13:19 +000058 while (iothread->running) {
Paolo Bonzini65c1b5b2016-10-27 12:49:06 +020059 aio_poll(iothread->ctx, true);
Wang Yong329163c2017-08-29 15:22:37 +080060
61 if (atomic_read(&iothread->worker_context)) {
62 GMainLoop *loop;
63
64 g_main_context_push_thread_default(iothread->worker_context);
65 iothread->main_loop =
66 g_main_loop_new(iothread->worker_context, TRUE);
67 loop = iothread->main_loop;
68
69 g_main_loop_run(iothread->main_loop);
70 iothread->main_loop = NULL;
71 g_main_loop_unref(loop);
72
73 g_main_context_pop_thread_default(iothread->worker_context);
Wang Yong329163c2017-08-29 15:22:37 +080074 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010075 }
Paolo Bonziniab28bd22015-07-09 08:55:38 +020076
77 rcu_unregister_thread();
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010078 return NULL;
79}
80
Stefan Hajnoczi2362a282017-12-07 20:13:19 +000081/* Runs in iothread_run() thread */
82static void iothread_stop_bh(void *opaque)
83{
84 IOThread *iothread = opaque;
85
86 iothread->running = false; /* stop iothread_run() */
87
88 if (iothread->main_loop) {
89 g_main_loop_quit(iothread->main_loop);
90 }
91}
92
Peter Xu82d90702017-09-28 10:59:56 +080093void iothread_stop(IOThread *iothread)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010094{
Peter Xu82d90702017-09-28 10:59:56 +080095 if (!iothread->ctx || iothread->stopping) {
96 return;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030097 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010098 iothread->stopping = true;
Stefan Hajnoczi2362a282017-12-07 20:13:19 +000099 aio_bh_schedule_oneshot(iothread->ctx, iothread_stop_bh, iothread);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100100 qemu_thread_join(&iothread->thread);
Peter Xu82d90702017-09-28 10:59:56 +0800101}
102
103static int iothread_stop_iter(Object *object, void *opaque)
104{
105 IOThread *iothread;
106
107 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
108 if (!iothread) {
109 return 0;
110 }
111 iothread_stop(iothread);
Fam Zhengdce89212016-09-08 17:28:51 +0800112 return 0;
113}
114
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000115static void iothread_instance_init(Object *obj)
116{
117 IOThread *iothread = IOTHREAD(obj);
118
119 iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
120}
121
Fam Zhengdce89212016-09-08 17:28:51 +0800122static void iothread_instance_finalize(Object *obj)
123{
124 IOThread *iothread = IOTHREAD(obj);
125
Peter Xu82d90702017-09-28 10:59:56 +0800126 iothread_stop(iothread);
Peter Xu5b3ac232017-09-28 10:59:57 +0800127 if (iothread->worker_context) {
128 g_main_context_unref(iothread->worker_context);
129 iothread->worker_context = NULL;
130 }
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100131 qemu_cond_destroy(&iothread->init_done_cond);
132 qemu_mutex_destroy(&iothread->init_done_lock);
Lin Maeb7b5c32016-09-26 13:29:58 +0800133 if (!iothread->ctx) {
134 return;
135 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100136 aio_context_unref(iothread->ctx);
137}
138
139static void iothread_complete(UserCreatable *obj, Error **errp)
140{
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300141 Error *local_error = NULL;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100142 IOThread *iothread = IOTHREAD(obj);
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100143 char *name, *thread_name;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100144
145 iothread->stopping = false;
Stefan Hajnoczi2362a282017-12-07 20:13:19 +0000146 iothread->running = true;
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100147 iothread->thread_id = -1;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300148 iothread->ctx = aio_context_new(&local_error);
149 if (!iothread->ctx) {
150 error_propagate(errp, local_error);
151 return;
152 }
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100153
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000154 aio_context_set_poll_params(iothread->ctx,
155 iothread->poll_max_ns,
156 iothread->poll_grow,
157 iothread->poll_shrink,
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000158 &local_error);
159 if (local_error) {
160 error_propagate(errp, local_error);
161 aio_context_unref(iothread->ctx);
162 iothread->ctx = NULL;
163 return;
164 }
165
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100166 qemu_mutex_init(&iothread->init_done_lock);
167 qemu_cond_init(&iothread->init_done_cond);
Wang Yong329163c2017-08-29 15:22:37 +0800168 iothread->once = (GOnce) G_ONCE_INIT;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100169
170 /* This assumes we are called from a thread with useful CPU affinity for us
171 * to inherit.
172 */
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100173 name = object_get_canonical_path_component(OBJECT(obj));
174 thread_name = g_strdup_printf("IO %s", name);
175 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100176 iothread, QEMU_THREAD_JOINABLE);
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100177 g_free(thread_name);
178 g_free(name);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100179
180 /* Wait for initialization to complete */
181 qemu_mutex_lock(&iothread->init_done_lock);
182 while (iothread->thread_id == -1) {
183 qemu_cond_wait(&iothread->init_done_cond,
184 &iothread->init_done_lock);
185 }
186 qemu_mutex_unlock(&iothread->init_done_lock);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100187}
188
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000189typedef struct {
190 const char *name;
191 ptrdiff_t offset; /* field's byte offset in IOThread struct */
192} PollParamInfo;
193
194static PollParamInfo poll_max_ns_info = {
195 "poll-max-ns", offsetof(IOThread, poll_max_ns),
196};
197static PollParamInfo poll_grow_info = {
198 "poll-grow", offsetof(IOThread, poll_grow),
199};
200static PollParamInfo poll_shrink_info = {
201 "poll-shrink", offsetof(IOThread, poll_shrink),
202};
203
204static void iothread_get_poll_param(Object *obj, Visitor *v,
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000205 const char *name, void *opaque, Error **errp)
206{
207 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000208 PollParamInfo *info = opaque;
209 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000210
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000211 visit_type_int64(v, name, field, errp);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000212}
213
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000214static void iothread_set_poll_param(Object *obj, Visitor *v,
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000215 const char *name, void *opaque, Error **errp)
216{
217 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000218 PollParamInfo *info = opaque;
219 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000220 Error *local_err = NULL;
221 int64_t value;
222
223 visit_type_int64(v, name, &value, &local_err);
224 if (local_err) {
225 goto out;
226 }
227
228 if (value < 0) {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000229 error_setg(&local_err, "%s value must be in range [0, %"PRId64"]",
230 info->name, INT64_MAX);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000231 goto out;
232 }
233
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000234 *field = value;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000235
236 if (iothread->ctx) {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000237 aio_context_set_poll_params(iothread->ctx,
238 iothread->poll_max_ns,
239 iothread->poll_grow,
240 iothread->poll_shrink,
241 &local_err);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000242 }
243
244out:
245 error_propagate(errp, local_err);
246}
247
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100248static void iothread_class_init(ObjectClass *klass, void *class_data)
249{
250 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
251 ucc->complete = iothread_complete;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000252
253 object_class_property_add(klass, "poll-max-ns", "int",
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000254 iothread_get_poll_param,
255 iothread_set_poll_param,
256 NULL, &poll_max_ns_info, &error_abort);
257 object_class_property_add(klass, "poll-grow", "int",
258 iothread_get_poll_param,
259 iothread_set_poll_param,
260 NULL, &poll_grow_info, &error_abort);
261 object_class_property_add(klass, "poll-shrink", "int",
262 iothread_get_poll_param,
263 iothread_set_poll_param,
264 NULL, &poll_shrink_info, &error_abort);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100265}
266
267static const TypeInfo iothread_info = {
268 .name = TYPE_IOTHREAD,
269 .parent = TYPE_OBJECT,
270 .class_init = iothread_class_init,
271 .instance_size = sizeof(IOThread),
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000272 .instance_init = iothread_instance_init,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100273 .instance_finalize = iothread_instance_finalize,
274 .interfaces = (InterfaceInfo[]) {
275 {TYPE_USER_CREATABLE},
276 {}
277 },
278};
279
280static void iothread_register_types(void)
281{
282 type_register_static(&iothread_info);
283}
284
285type_init(iothread_register_types)
286
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100287char *iothread_get_id(IOThread *iothread)
288{
289 return object_get_canonical_path_component(OBJECT(iothread));
290}
291
292AioContext *iothread_get_aio_context(IOThread *iothread)
293{
294 return iothread->ctx;
295}
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100296
297static int query_one_iothread(Object *object, void *opaque)
298{
299 IOThreadInfoList ***prev = opaque;
300 IOThreadInfoList *elem;
301 IOThreadInfo *info;
302 IOThread *iothread;
303
304 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
305 if (!iothread) {
306 return 0;
307 }
308
309 info = g_new0(IOThreadInfo, 1);
310 info->id = iothread_get_id(iothread);
311 info->thread_id = iothread->thread_id;
Pavel Hrdina5fc00482017-02-10 10:41:17 +0100312 info->poll_max_ns = iothread->poll_max_ns;
313 info->poll_grow = iothread->poll_grow;
314 info->poll_shrink = iothread->poll_shrink;
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100315
316 elem = g_new0(IOThreadInfoList, 1);
317 elem->value = info;
318 elem->next = NULL;
319
320 **prev = elem;
321 *prev = &elem->next;
322 return 0;
323}
324
325IOThreadInfoList *qmp_query_iothreads(Error **errp)
326{
327 IOThreadInfoList *head = NULL;
328 IOThreadInfoList **prev = &head;
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100329 Object *container = object_get_objects_root();
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100330
331 object_child_foreach(container, query_one_iothread, &prev);
332 return head;
333}
Fam Zhengdce89212016-09-08 17:28:51 +0800334
335void iothread_stop_all(void)
336{
337 Object *container = object_get_objects_root();
Paolo Bonzinid16341f2016-10-27 12:49:00 +0200338 BlockDriverState *bs;
339 BdrvNextIterator it;
340
341 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
342 AioContext *ctx = bdrv_get_aio_context(bs);
343 if (ctx == qemu_get_aio_context()) {
344 continue;
345 }
346 aio_context_acquire(ctx);
347 bdrv_set_aio_context(bs, qemu_get_aio_context());
348 aio_context_release(ctx);
349 }
Fam Zhengdce89212016-09-08 17:28:51 +0800350
Peter Xu82d90702017-09-28 10:59:56 +0800351 object_child_foreach(container, iothread_stop_iter, NULL);
Fam Zhengdce89212016-09-08 17:28:51 +0800352}
Wang Yong329163c2017-08-29 15:22:37 +0800353
354static gpointer iothread_g_main_context_init(gpointer opaque)
355{
356 AioContext *ctx;
357 IOThread *iothread = opaque;
358 GSource *source;
359
360 iothread->worker_context = g_main_context_new();
361
362 ctx = iothread_get_aio_context(iothread);
363 source = aio_get_g_source(ctx);
364 g_source_attach(source, iothread->worker_context);
365 g_source_unref(source);
366
367 aio_notify(iothread->ctx);
368 return NULL;
369}
370
371GMainContext *iothread_get_g_main_context(IOThread *iothread)
372{
373 g_once(&iothread->once, iothread_g_main_context_init, iothread);
374
375 return iothread->worker_context;
376}
Peter Xu0173e212017-09-28 10:59:55 +0800377
378IOThread *iothread_create(const char *id, Error **errp)
379{
380 Object *obj;
381
382 obj = object_new_with_props(TYPE_IOTHREAD,
383 object_get_internal_root(),
384 id, errp, NULL);
385
386 return IOTHREAD(obj);
387}
388
389void iothread_destroy(IOThread *iothread)
390{
391 object_unparent(OBJECT(iothread));
392}
Stefan Hajnoczifbcc6922017-12-06 14:45:48 +0000393
394/* Lookup IOThread by its id. Only finds user-created objects, not internal
395 * iothread_create() objects. */
396IOThread *iothread_by_id(const char *id)
397{
398 return IOTHREAD(object_resolve_path_type(id, TYPE_IOTHREAD, NULL));
399}