blob: 7bedde87e98fc61c1a2214145995fe8c542c60fa [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
Paolo Bonzinie4370162016-10-27 12:48:59 +020033static __thread IOThread *my_iothread;
34
35AioContext *qemu_get_current_aio_context(void)
36{
37 return my_iothread ? my_iothread->ctx : qemu_get_aio_context();
38}
39
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010040static void *iothread_run(void *opaque)
41{
42 IOThread *iothread = opaque;
43
Paolo Bonziniab28bd22015-07-09 08:55:38 +020044 rcu_register_thread();
45
Paolo Bonzinie4370162016-10-27 12:48:59 +020046 my_iothread = iothread;
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010047 qemu_mutex_lock(&iothread->init_done_lock);
48 iothread->thread_id = qemu_get_thread_id();
49 qemu_cond_signal(&iothread->init_done_cond);
50 qemu_mutex_unlock(&iothread->init_done_lock);
51
Paolo Bonzini65c1b5b2016-10-27 12:49:06 +020052 while (!atomic_read(&iothread->stopping)) {
53 aio_poll(iothread->ctx, true);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010054 }
Paolo Bonziniab28bd22015-07-09 08:55:38 +020055
56 rcu_unregister_thread();
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010057 return NULL;
58}
59
Fam Zhengdce89212016-09-08 17:28:51 +080060static int iothread_stop(Object *object, void *opaque)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010061{
Fam Zhengdce89212016-09-08 17:28:51 +080062 IOThread *iothread;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010063
Fam Zhengdce89212016-09-08 17:28:51 +080064 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
65 if (!iothread || !iothread->ctx) {
66 return 0;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030067 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010068 iothread->stopping = true;
69 aio_notify(iothread->ctx);
70 qemu_thread_join(&iothread->thread);
Fam Zhengdce89212016-09-08 17:28:51 +080071 return 0;
72}
73
74static void iothread_instance_finalize(Object *obj)
75{
76 IOThread *iothread = IOTHREAD(obj);
77
78 iothread_stop(obj, NULL);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010079 qemu_cond_destroy(&iothread->init_done_cond);
80 qemu_mutex_destroy(&iothread->init_done_lock);
Lin Maeb7b5c32016-09-26 13:29:58 +080081 if (!iothread->ctx) {
82 return;
83 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010084 aio_context_unref(iothread->ctx);
85}
86
87static void iothread_complete(UserCreatable *obj, Error **errp)
88{
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030089 Error *local_error = NULL;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010090 IOThread *iothread = IOTHREAD(obj);
Paolo Bonzinid21e8772015-11-24 14:46:44 +010091 char *name, *thread_name;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010092
93 iothread->stopping = false;
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010094 iothread->thread_id = -1;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030095 iothread->ctx = aio_context_new(&local_error);
96 if (!iothread->ctx) {
97 error_propagate(errp, local_error);
98 return;
99 }
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100100
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000101 aio_context_set_poll_params(iothread->ctx,
102 iothread->poll_max_ns,
103 iothread->poll_grow,
104 iothread->poll_shrink,
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000105 &local_error);
106 if (local_error) {
107 error_propagate(errp, local_error);
108 aio_context_unref(iothread->ctx);
109 iothread->ctx = NULL;
110 return;
111 }
112
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100113 qemu_mutex_init(&iothread->init_done_lock);
114 qemu_cond_init(&iothread->init_done_cond);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100115
116 /* This assumes we are called from a thread with useful CPU affinity for us
117 * to inherit.
118 */
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100119 name = object_get_canonical_path_component(OBJECT(obj));
120 thread_name = g_strdup_printf("IO %s", name);
121 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100122 iothread, QEMU_THREAD_JOINABLE);
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100123 g_free(thread_name);
124 g_free(name);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100125
126 /* Wait for initialization to complete */
127 qemu_mutex_lock(&iothread->init_done_lock);
128 while (iothread->thread_id == -1) {
129 qemu_cond_wait(&iothread->init_done_cond,
130 &iothread->init_done_lock);
131 }
132 qemu_mutex_unlock(&iothread->init_done_lock);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100133}
134
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000135typedef struct {
136 const char *name;
137 ptrdiff_t offset; /* field's byte offset in IOThread struct */
138} PollParamInfo;
139
140static PollParamInfo poll_max_ns_info = {
141 "poll-max-ns", offsetof(IOThread, poll_max_ns),
142};
143static PollParamInfo poll_grow_info = {
144 "poll-grow", offsetof(IOThread, poll_grow),
145};
146static PollParamInfo poll_shrink_info = {
147 "poll-shrink", offsetof(IOThread, poll_shrink),
148};
149
150static void iothread_get_poll_param(Object *obj, Visitor *v,
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000151 const char *name, void *opaque, Error **errp)
152{
153 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000154 PollParamInfo *info = opaque;
155 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000156
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000157 visit_type_int64(v, name, field, errp);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000158}
159
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000160static void iothread_set_poll_param(Object *obj, Visitor *v,
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000161 const char *name, void *opaque, Error **errp)
162{
163 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000164 PollParamInfo *info = opaque;
165 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000166 Error *local_err = NULL;
167 int64_t value;
168
169 visit_type_int64(v, name, &value, &local_err);
170 if (local_err) {
171 goto out;
172 }
173
174 if (value < 0) {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000175 error_setg(&local_err, "%s value must be in range [0, %"PRId64"]",
176 info->name, INT64_MAX);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000177 goto out;
178 }
179
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000180 *field = value;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000181
182 if (iothread->ctx) {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000183 aio_context_set_poll_params(iothread->ctx,
184 iothread->poll_max_ns,
185 iothread->poll_grow,
186 iothread->poll_shrink,
187 &local_err);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000188 }
189
190out:
191 error_propagate(errp, local_err);
192}
193
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100194static void iothread_class_init(ObjectClass *klass, void *class_data)
195{
196 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
197 ucc->complete = iothread_complete;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000198
199 object_class_property_add(klass, "poll-max-ns", "int",
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000200 iothread_get_poll_param,
201 iothread_set_poll_param,
202 NULL, &poll_max_ns_info, &error_abort);
203 object_class_property_add(klass, "poll-grow", "int",
204 iothread_get_poll_param,
205 iothread_set_poll_param,
206 NULL, &poll_grow_info, &error_abort);
207 object_class_property_add(klass, "poll-shrink", "int",
208 iothread_get_poll_param,
209 iothread_set_poll_param,
210 NULL, &poll_shrink_info, &error_abort);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100211}
212
213static const TypeInfo iothread_info = {
214 .name = TYPE_IOTHREAD,
215 .parent = TYPE_OBJECT,
216 .class_init = iothread_class_init,
217 .instance_size = sizeof(IOThread),
218 .instance_finalize = iothread_instance_finalize,
219 .interfaces = (InterfaceInfo[]) {
220 {TYPE_USER_CREATABLE},
221 {}
222 },
223};
224
225static void iothread_register_types(void)
226{
227 type_register_static(&iothread_info);
228}
229
230type_init(iothread_register_types)
231
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100232char *iothread_get_id(IOThread *iothread)
233{
234 return object_get_canonical_path_component(OBJECT(iothread));
235}
236
237AioContext *iothread_get_aio_context(IOThread *iothread)
238{
239 return iothread->ctx;
240}
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100241
242static int query_one_iothread(Object *object, void *opaque)
243{
244 IOThreadInfoList ***prev = opaque;
245 IOThreadInfoList *elem;
246 IOThreadInfo *info;
247 IOThread *iothread;
248
249 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
250 if (!iothread) {
251 return 0;
252 }
253
254 info = g_new0(IOThreadInfo, 1);
255 info->id = iothread_get_id(iothread);
256 info->thread_id = iothread->thread_id;
257
258 elem = g_new0(IOThreadInfoList, 1);
259 elem->value = info;
260 elem->next = NULL;
261
262 **prev = elem;
263 *prev = &elem->next;
264 return 0;
265}
266
267IOThreadInfoList *qmp_query_iothreads(Error **errp)
268{
269 IOThreadInfoList *head = NULL;
270 IOThreadInfoList **prev = &head;
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100271 Object *container = object_get_objects_root();
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100272
273 object_child_foreach(container, query_one_iothread, &prev);
274 return head;
275}
Fam Zhengdce89212016-09-08 17:28:51 +0800276
277void iothread_stop_all(void)
278{
279 Object *container = object_get_objects_root();
Paolo Bonzinid16341f2016-10-27 12:49:00 +0200280 BlockDriverState *bs;
281 BdrvNextIterator it;
282
283 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
284 AioContext *ctx = bdrv_get_aio_context(bs);
285 if (ctx == qemu_get_aio_context()) {
286 continue;
287 }
288 aio_context_acquire(ctx);
289 bdrv_set_aio_context(bs, qemu_get_aio_context());
290 aio_context_release(ctx);
291 }
Fam Zhengdce89212016-09-08 17:28:51 +0800292
293 object_child_foreach(container, iothread_stop, NULL);
294}