blob: b3c092b2d73a599d5660aa2d14b1f50094e466c7 [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
Paolo Bonzini65c1b5b2016-10-27 12:49:06 +020058 while (!atomic_read(&iothread->stopping)) {
59 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);
74 g_main_context_unref(iothread->worker_context);
75 iothread->worker_context = NULL;
76 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010077 }
Paolo Bonziniab28bd22015-07-09 08:55:38 +020078
79 rcu_unregister_thread();
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010080 return NULL;
81}
82
Peter Xu82d90702017-09-28 10:59:56 +080083void iothread_stop(IOThread *iothread)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010084{
Peter Xu82d90702017-09-28 10:59:56 +080085 if (!iothread->ctx || iothread->stopping) {
86 return;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030087 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010088 iothread->stopping = true;
89 aio_notify(iothread->ctx);
Wang Yong329163c2017-08-29 15:22:37 +080090 if (atomic_read(&iothread->main_loop)) {
91 g_main_loop_quit(iothread->main_loop);
92 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010093 qemu_thread_join(&iothread->thread);
Peter Xu82d90702017-09-28 10:59:56 +080094}
95
96static int iothread_stop_iter(Object *object, void *opaque)
97{
98 IOThread *iothread;
99
100 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
101 if (!iothread) {
102 return 0;
103 }
104 iothread_stop(iothread);
Fam Zhengdce89212016-09-08 17:28:51 +0800105 return 0;
106}
107
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000108static void iothread_instance_init(Object *obj)
109{
110 IOThread *iothread = IOTHREAD(obj);
111
112 iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
113}
114
Fam Zhengdce89212016-09-08 17:28:51 +0800115static void iothread_instance_finalize(Object *obj)
116{
117 IOThread *iothread = IOTHREAD(obj);
118
Peter Xu82d90702017-09-28 10:59:56 +0800119 iothread_stop(iothread);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100120 qemu_cond_destroy(&iothread->init_done_cond);
121 qemu_mutex_destroy(&iothread->init_done_lock);
Lin Maeb7b5c32016-09-26 13:29:58 +0800122 if (!iothread->ctx) {
123 return;
124 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100125 aio_context_unref(iothread->ctx);
126}
127
128static void iothread_complete(UserCreatable *obj, Error **errp)
129{
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300130 Error *local_error = NULL;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100131 IOThread *iothread = IOTHREAD(obj);
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100132 char *name, *thread_name;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100133
134 iothread->stopping = false;
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100135 iothread->thread_id = -1;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300136 iothread->ctx = aio_context_new(&local_error);
137 if (!iothread->ctx) {
138 error_propagate(errp, local_error);
139 return;
140 }
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100141
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000142 aio_context_set_poll_params(iothread->ctx,
143 iothread->poll_max_ns,
144 iothread->poll_grow,
145 iothread->poll_shrink,
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000146 &local_error);
147 if (local_error) {
148 error_propagate(errp, local_error);
149 aio_context_unref(iothread->ctx);
150 iothread->ctx = NULL;
151 return;
152 }
153
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100154 qemu_mutex_init(&iothread->init_done_lock);
155 qemu_cond_init(&iothread->init_done_cond);
Wang Yong329163c2017-08-29 15:22:37 +0800156 iothread->once = (GOnce) G_ONCE_INIT;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100157
158 /* This assumes we are called from a thread with useful CPU affinity for us
159 * to inherit.
160 */
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100161 name = object_get_canonical_path_component(OBJECT(obj));
162 thread_name = g_strdup_printf("IO %s", name);
163 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100164 iothread, QEMU_THREAD_JOINABLE);
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100165 g_free(thread_name);
166 g_free(name);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100167
168 /* Wait for initialization to complete */
169 qemu_mutex_lock(&iothread->init_done_lock);
170 while (iothread->thread_id == -1) {
171 qemu_cond_wait(&iothread->init_done_cond,
172 &iothread->init_done_lock);
173 }
174 qemu_mutex_unlock(&iothread->init_done_lock);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100175}
176
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000177typedef struct {
178 const char *name;
179 ptrdiff_t offset; /* field's byte offset in IOThread struct */
180} PollParamInfo;
181
182static PollParamInfo poll_max_ns_info = {
183 "poll-max-ns", offsetof(IOThread, poll_max_ns),
184};
185static PollParamInfo poll_grow_info = {
186 "poll-grow", offsetof(IOThread, poll_grow),
187};
188static PollParamInfo poll_shrink_info = {
189 "poll-shrink", offsetof(IOThread, poll_shrink),
190};
191
192static void iothread_get_poll_param(Object *obj, Visitor *v,
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000193 const char *name, void *opaque, Error **errp)
194{
195 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000196 PollParamInfo *info = opaque;
197 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000198
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000199 visit_type_int64(v, name, field, errp);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000200}
201
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000202static void iothread_set_poll_param(Object *obj, Visitor *v,
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000203 const char *name, void *opaque, Error **errp)
204{
205 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000206 PollParamInfo *info = opaque;
207 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000208 Error *local_err = NULL;
209 int64_t value;
210
211 visit_type_int64(v, name, &value, &local_err);
212 if (local_err) {
213 goto out;
214 }
215
216 if (value < 0) {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000217 error_setg(&local_err, "%s value must be in range [0, %"PRId64"]",
218 info->name, INT64_MAX);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000219 goto out;
220 }
221
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000222 *field = value;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000223
224 if (iothread->ctx) {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000225 aio_context_set_poll_params(iothread->ctx,
226 iothread->poll_max_ns,
227 iothread->poll_grow,
228 iothread->poll_shrink,
229 &local_err);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000230 }
231
232out:
233 error_propagate(errp, local_err);
234}
235
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100236static void iothread_class_init(ObjectClass *klass, void *class_data)
237{
238 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
239 ucc->complete = iothread_complete;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000240
241 object_class_property_add(klass, "poll-max-ns", "int",
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000242 iothread_get_poll_param,
243 iothread_set_poll_param,
244 NULL, &poll_max_ns_info, &error_abort);
245 object_class_property_add(klass, "poll-grow", "int",
246 iothread_get_poll_param,
247 iothread_set_poll_param,
248 NULL, &poll_grow_info, &error_abort);
249 object_class_property_add(klass, "poll-shrink", "int",
250 iothread_get_poll_param,
251 iothread_set_poll_param,
252 NULL, &poll_shrink_info, &error_abort);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100253}
254
255static const TypeInfo iothread_info = {
256 .name = TYPE_IOTHREAD,
257 .parent = TYPE_OBJECT,
258 .class_init = iothread_class_init,
259 .instance_size = sizeof(IOThread),
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000260 .instance_init = iothread_instance_init,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100261 .instance_finalize = iothread_instance_finalize,
262 .interfaces = (InterfaceInfo[]) {
263 {TYPE_USER_CREATABLE},
264 {}
265 },
266};
267
268static void iothread_register_types(void)
269{
270 type_register_static(&iothread_info);
271}
272
273type_init(iothread_register_types)
274
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100275char *iothread_get_id(IOThread *iothread)
276{
277 return object_get_canonical_path_component(OBJECT(iothread));
278}
279
280AioContext *iothread_get_aio_context(IOThread *iothread)
281{
282 return iothread->ctx;
283}
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100284
285static int query_one_iothread(Object *object, void *opaque)
286{
287 IOThreadInfoList ***prev = opaque;
288 IOThreadInfoList *elem;
289 IOThreadInfo *info;
290 IOThread *iothread;
291
292 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
293 if (!iothread) {
294 return 0;
295 }
296
297 info = g_new0(IOThreadInfo, 1);
298 info->id = iothread_get_id(iothread);
299 info->thread_id = iothread->thread_id;
Pavel Hrdina5fc00482017-02-10 10:41:17 +0100300 info->poll_max_ns = iothread->poll_max_ns;
301 info->poll_grow = iothread->poll_grow;
302 info->poll_shrink = iothread->poll_shrink;
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100303
304 elem = g_new0(IOThreadInfoList, 1);
305 elem->value = info;
306 elem->next = NULL;
307
308 **prev = elem;
309 *prev = &elem->next;
310 return 0;
311}
312
313IOThreadInfoList *qmp_query_iothreads(Error **errp)
314{
315 IOThreadInfoList *head = NULL;
316 IOThreadInfoList **prev = &head;
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100317 Object *container = object_get_objects_root();
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100318
319 object_child_foreach(container, query_one_iothread, &prev);
320 return head;
321}
Fam Zhengdce89212016-09-08 17:28:51 +0800322
323void iothread_stop_all(void)
324{
325 Object *container = object_get_objects_root();
Paolo Bonzinid16341f2016-10-27 12:49:00 +0200326 BlockDriverState *bs;
327 BdrvNextIterator it;
328
329 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
330 AioContext *ctx = bdrv_get_aio_context(bs);
331 if (ctx == qemu_get_aio_context()) {
332 continue;
333 }
334 aio_context_acquire(ctx);
335 bdrv_set_aio_context(bs, qemu_get_aio_context());
336 aio_context_release(ctx);
337 }
Fam Zhengdce89212016-09-08 17:28:51 +0800338
Peter Xu82d90702017-09-28 10:59:56 +0800339 object_child_foreach(container, iothread_stop_iter, NULL);
Fam Zhengdce89212016-09-08 17:28:51 +0800340}
Wang Yong329163c2017-08-29 15:22:37 +0800341
342static gpointer iothread_g_main_context_init(gpointer opaque)
343{
344 AioContext *ctx;
345 IOThread *iothread = opaque;
346 GSource *source;
347
348 iothread->worker_context = g_main_context_new();
349
350 ctx = iothread_get_aio_context(iothread);
351 source = aio_get_g_source(ctx);
352 g_source_attach(source, iothread->worker_context);
353 g_source_unref(source);
354
355 aio_notify(iothread->ctx);
356 return NULL;
357}
358
359GMainContext *iothread_get_g_main_context(IOThread *iothread)
360{
361 g_once(&iothread->once, iothread_g_main_context_init, iothread);
362
363 return iothread->worker_context;
364}
Peter Xu0173e212017-09-28 10:59:55 +0800365
366IOThread *iothread_create(const char *id, Error **errp)
367{
368 Object *obj;
369
370 obj = object_new_with_props(TYPE_IOTHREAD,
371 object_get_internal_root(),
372 id, errp, NULL);
373
374 return IOTHREAD(obj);
375}
376
377void iothread_destroy(IOThread *iothread)
378{
379 object_unparent(OBJECT(iothread));
380}