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