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