hmp: introduce cmd_available
[qemu.git] / iothread.c
blob69eff9efbc70834f62cd1b009962ee005d88199b
1 /*
2 * Event loop thread
4 * Copyright Red Hat Inc., 2013
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
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.
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"
27 typedef ObjectClass IOThreadClass;
29 DECLARE_CLASS_CHECKERS(IOThreadClass, IOTHREAD,
30 TYPE_IOTHREAD)
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.
37 #define IOTHREAD_POLL_MAX_NS_DEFAULT 32768ULL
38 #else
39 #define IOTHREAD_POLL_MAX_NS_DEFAULT 0ULL
40 #endif
42 static __thread IOThread *my_iothread;
44 AioContext *qemu_get_current_aio_context(void)
46 return my_iothread ? my_iothread->ctx : qemu_get_aio_context();
49 static void *iothread_run(void *opaque)
51 IOThread *iothread = opaque;
53 rcu_register_thread();
55 * g_main_context_push_thread_default() must be called before anything
56 * in this new thread uses glib.
58 g_main_context_push_thread_default(iothread->worker_context);
59 my_iothread = iothread;
60 iothread->thread_id = qemu_get_thread_id();
61 qemu_sem_post(&iothread->init_done_sem);
63 while (iothread->running) {
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.
73 aio_poll(iothread->ctx, true);
76 * We must check the running state again in case it was
77 * changed in previous aio_poll()
79 if (iothread->running && qatomic_read(&iothread->run_gcontext)) {
80 g_main_loop_run(iothread->main_loop);
84 g_main_context_pop_thread_default(iothread->worker_context);
85 rcu_unregister_thread();
86 return NULL;
89 /* Runs in iothread_run() thread */
90 static void iothread_stop_bh(void *opaque)
92 IOThread *iothread = opaque;
94 iothread->running = false; /* stop iothread_run() */
96 if (iothread->main_loop) {
97 g_main_loop_quit(iothread->main_loop);
101 void iothread_stop(IOThread *iothread)
103 if (!iothread->ctx || iothread->stopping) {
104 return;
106 iothread->stopping = true;
107 aio_bh_schedule_oneshot(iothread->ctx, iothread_stop_bh, iothread);
108 qemu_thread_join(&iothread->thread);
111 static void iothread_instance_init(Object *obj)
113 IOThread *iothread = IOTHREAD(obj);
115 iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
116 iothread->thread_id = -1;
117 qemu_sem_init(&iothread->init_done_sem, 0);
118 /* By default, we don't run gcontext */
119 qatomic_set(&iothread->run_gcontext, 0);
122 static void iothread_instance_finalize(Object *obj)
124 IOThread *iothread = IOTHREAD(obj);
126 iothread_stop(iothread);
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.
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.
138 if (iothread->ctx) {
139 aio_context_unref(iothread->ctx);
140 iothread->ctx = NULL;
142 if (iothread->worker_context) {
143 g_main_context_unref(iothread->worker_context);
144 iothread->worker_context = NULL;
145 g_main_loop_unref(iothread->main_loop);
146 iothread->main_loop = NULL;
148 qemu_sem_destroy(&iothread->init_done_sem);
151 static void iothread_init_gcontext(IOThread *iothread)
153 GSource *source;
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);
159 iothread->main_loop = g_main_loop_new(iothread->worker_context, TRUE);
162 static void iothread_complete(UserCreatable *obj, Error **errp)
164 Error *local_error = NULL;
165 IOThread *iothread = IOTHREAD(obj);
166 char *thread_name;
168 iothread->stopping = false;
169 iothread->running = true;
170 iothread->ctx = aio_context_new(errp);
171 if (!iothread->ctx) {
172 return;
176 * Init one GMainContext for the iothread unconditionally, even if
177 * it's not used
179 iothread_init_gcontext(iothread);
181 aio_context_set_poll_params(iothread->ctx,
182 iothread->poll_max_ns,
183 iothread->poll_grow,
184 iothread->poll_shrink,
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;
193 /* This assumes we are called from a thread with useful CPU affinity for us
194 * to inherit.
196 thread_name = g_strdup_printf("IO %s",
197 object_get_canonical_path_component(OBJECT(obj)));
198 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
199 iothread, QEMU_THREAD_JOINABLE);
200 g_free(thread_name);
202 /* Wait for initialization to complete */
203 while (iothread->thread_id == -1) {
204 qemu_sem_wait(&iothread->init_done_sem);
208 typedef struct {
209 const char *name;
210 ptrdiff_t offset; /* field's byte offset in IOThread struct */
211 } PollParamInfo;
213 static PollParamInfo poll_max_ns_info = {
214 "poll-max-ns", offsetof(IOThread, poll_max_ns),
216 static PollParamInfo poll_grow_info = {
217 "poll-grow", offsetof(IOThread, poll_grow),
219 static PollParamInfo poll_shrink_info = {
220 "poll-shrink", offsetof(IOThread, poll_shrink),
223 static void iothread_get_poll_param(Object *obj, Visitor *v,
224 const char *name, void *opaque, Error **errp)
226 IOThread *iothread = IOTHREAD(obj);
227 PollParamInfo *info = opaque;
228 int64_t *field = (void *)iothread + info->offset;
230 visit_type_int64(v, name, field, errp);
233 static void iothread_set_poll_param(Object *obj, Visitor *v,
234 const char *name, void *opaque, Error **errp)
236 IOThread *iothread = IOTHREAD(obj);
237 PollParamInfo *info = opaque;
238 int64_t *field = (void *)iothread + info->offset;
239 int64_t value;
241 if (!visit_type_int64(v, name, &value, errp)) {
242 return;
245 if (value < 0) {
246 error_setg(errp, "%s value must be in range [0, %" PRId64 "]",
247 info->name, INT64_MAX);
248 return;
251 *field = value;
253 if (iothread->ctx) {
254 aio_context_set_poll_params(iothread->ctx,
255 iothread->poll_max_ns,
256 iothread->poll_grow,
257 iothread->poll_shrink,
258 errp);
262 static void iothread_class_init(ObjectClass *klass, void *class_data)
264 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
265 ucc->complete = iothread_complete;
267 object_class_property_add(klass, "poll-max-ns", "int",
268 iothread_get_poll_param,
269 iothread_set_poll_param,
270 NULL, &poll_max_ns_info);
271 object_class_property_add(klass, "poll-grow", "int",
272 iothread_get_poll_param,
273 iothread_set_poll_param,
274 NULL, &poll_grow_info);
275 object_class_property_add(klass, "poll-shrink", "int",
276 iothread_get_poll_param,
277 iothread_set_poll_param,
278 NULL, &poll_shrink_info);
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),
286 .instance_init = iothread_instance_init,
287 .instance_finalize = iothread_instance_finalize,
288 .interfaces = (InterfaceInfo[]) {
289 {TYPE_USER_CREATABLE},
294 static void iothread_register_types(void)
296 type_register_static(&iothread_info);
299 type_init(iothread_register_types)
301 char *iothread_get_id(IOThread *iothread)
303 return g_strdup(object_get_canonical_path_component(OBJECT(iothread)));
306 AioContext *iothread_get_aio_context(IOThread *iothread)
308 return iothread->ctx;
311 static int query_one_iothread(Object *object, void *opaque)
313 IOThreadInfoList ***prev = opaque;
314 IOThreadInfoList *elem;
315 IOThreadInfo *info;
316 IOThread *iothread;
318 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
319 if (!iothread) {
320 return 0;
323 info = g_new0(IOThreadInfo, 1);
324 info->id = iothread_get_id(iothread);
325 info->thread_id = iothread->thread_id;
326 info->poll_max_ns = iothread->poll_max_ns;
327 info->poll_grow = iothread->poll_grow;
328 info->poll_shrink = iothread->poll_shrink;
330 elem = g_new0(IOThreadInfoList, 1);
331 elem->value = info;
332 elem->next = NULL;
334 **prev = elem;
335 *prev = &elem->next;
336 return 0;
339 IOThreadInfoList *qmp_query_iothreads(Error **errp)
341 IOThreadInfoList *head = NULL;
342 IOThreadInfoList **prev = &head;
343 Object *container = object_get_objects_root();
345 object_child_foreach(container, query_one_iothread, &prev);
346 return head;
349 GMainContext *iothread_get_g_main_context(IOThread *iothread)
351 qatomic_set(&iothread->run_gcontext, 1);
352 aio_notify(iothread->ctx);
353 return iothread->worker_context;
356 IOThread *iothread_create(const char *id, Error **errp)
358 Object *obj;
360 obj = object_new_with_props(TYPE_IOTHREAD,
361 object_get_internal_root(),
362 id, errp, NULL);
364 return IOTHREAD(obj);
367 void iothread_destroy(IOThread *iothread)
369 object_unparent(OBJECT(iothread));
372 /* Lookup IOThread by its id. Only finds user-created objects, not internal
373 * iothread_create() objects. */
374 IOThread *iothread_by_id(const char *id)
376 return IOTHREAD(object_resolve_path_type(id, TYPE_IOTHREAD, NULL));