4 * Copyright Red Hat Inc., 2013
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 "qmp-commands.h"
22 #include "qemu/error-report.h"
24 #include "qemu/main-loop.h"
26 typedef ObjectClass IOThreadClass
;
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)
33 static __thread IOThread
*my_iothread
;
35 AioContext
*qemu_get_current_aio_context(void)
37 return my_iothread
? my_iothread
->ctx
: qemu_get_aio_context();
40 static void *iothread_run(void *opaque
)
42 IOThread
*iothread
= opaque
;
44 rcu_register_thread();
46 my_iothread
= iothread
;
47 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
);
52 while (!atomic_read(&iothread
->stopping
)) {
53 aio_poll(iothread
->ctx
, true);
56 rcu_unregister_thread();
60 static int iothread_stop(Object
*object
, void *opaque
)
64 iothread
= (IOThread
*)object_dynamic_cast(object
, TYPE_IOTHREAD
);
65 if (!iothread
|| !iothread
->ctx
) {
68 iothread
->stopping
= true;
69 aio_notify(iothread
->ctx
);
70 qemu_thread_join(&iothread
->thread
);
74 static void iothread_instance_finalize(Object
*obj
)
76 IOThread
*iothread
= IOTHREAD(obj
);
78 iothread_stop(obj
, NULL
);
79 qemu_cond_destroy(&iothread
->init_done_cond
);
80 qemu_mutex_destroy(&iothread
->init_done_lock
);
84 aio_context_unref(iothread
->ctx
);
87 static void iothread_complete(UserCreatable
*obj
, Error
**errp
)
89 Error
*local_error
= NULL
;
90 IOThread
*iothread
= IOTHREAD(obj
);
91 char *name
, *thread_name
;
93 iothread
->stopping
= false;
94 iothread
->thread_id
= -1;
95 iothread
->ctx
= aio_context_new(&local_error
);
97 error_propagate(errp
, local_error
);
101 aio_context_set_poll_params(iothread
->ctx
, iothread
->poll_max_ns
,
104 error_propagate(errp
, local_error
);
105 aio_context_unref(iothread
->ctx
);
106 iothread
->ctx
= NULL
;
110 qemu_mutex_init(&iothread
->init_done_lock
);
111 qemu_cond_init(&iothread
->init_done_cond
);
113 /* This assumes we are called from a thread with useful CPU affinity for us
116 name
= object_get_canonical_path_component(OBJECT(obj
));
117 thread_name
= g_strdup_printf("IO %s", name
);
118 qemu_thread_create(&iothread
->thread
, thread_name
, iothread_run
,
119 iothread
, QEMU_THREAD_JOINABLE
);
123 /* Wait for initialization to complete */
124 qemu_mutex_lock(&iothread
->init_done_lock
);
125 while (iothread
->thread_id
== -1) {
126 qemu_cond_wait(&iothread
->init_done_cond
,
127 &iothread
->init_done_lock
);
129 qemu_mutex_unlock(&iothread
->init_done_lock
);
132 static void iothread_get_poll_max_ns(Object
*obj
, Visitor
*v
,
133 const char *name
, void *opaque
, Error
**errp
)
135 IOThread
*iothread
= IOTHREAD(obj
);
137 visit_type_int64(v
, name
, &iothread
->poll_max_ns
, errp
);
140 static void iothread_set_poll_max_ns(Object
*obj
, Visitor
*v
,
141 const char *name
, void *opaque
, Error
**errp
)
143 IOThread
*iothread
= IOTHREAD(obj
);
144 Error
*local_err
= NULL
;
147 visit_type_int64(v
, name
, &value
, &local_err
);
153 error_setg(&local_err
, "poll_max_ns value must be in range "
154 "[0, %"PRId64
"]", INT64_MAX
);
158 iothread
->poll_max_ns
= value
;
161 aio_context_set_poll_params(iothread
->ctx
, value
, &local_err
);
165 error_propagate(errp
, local_err
);
168 static void iothread_class_init(ObjectClass
*klass
, void *class_data
)
170 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(klass
);
171 ucc
->complete
= iothread_complete
;
173 object_class_property_add(klass
, "poll-max-ns", "int",
174 iothread_get_poll_max_ns
,
175 iothread_set_poll_max_ns
,
176 NULL
, NULL
, &error_abort
);
179 static const TypeInfo iothread_info
= {
180 .name
= TYPE_IOTHREAD
,
181 .parent
= TYPE_OBJECT
,
182 .class_init
= iothread_class_init
,
183 .instance_size
= sizeof(IOThread
),
184 .instance_finalize
= iothread_instance_finalize
,
185 .interfaces
= (InterfaceInfo
[]) {
186 {TYPE_USER_CREATABLE
},
191 static void iothread_register_types(void)
193 type_register_static(&iothread_info
);
196 type_init(iothread_register_types
)
198 char *iothread_get_id(IOThread
*iothread
)
200 return object_get_canonical_path_component(OBJECT(iothread
));
203 AioContext
*iothread_get_aio_context(IOThread
*iothread
)
205 return iothread
->ctx
;
208 static int query_one_iothread(Object
*object
, void *opaque
)
210 IOThreadInfoList
***prev
= opaque
;
211 IOThreadInfoList
*elem
;
215 iothread
= (IOThread
*)object_dynamic_cast(object
, TYPE_IOTHREAD
);
220 info
= g_new0(IOThreadInfo
, 1);
221 info
->id
= iothread_get_id(iothread
);
222 info
->thread_id
= iothread
->thread_id
;
224 elem
= g_new0(IOThreadInfoList
, 1);
233 IOThreadInfoList
*qmp_query_iothreads(Error
**errp
)
235 IOThreadInfoList
*head
= NULL
;
236 IOThreadInfoList
**prev
= &head
;
237 Object
*container
= object_get_objects_root();
239 object_child_foreach(container
, query_one_iothread
, &prev
);
243 void iothread_stop_all(void)
245 Object
*container
= object_get_objects_root();
246 BlockDriverState
*bs
;
249 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
250 AioContext
*ctx
= bdrv_get_aio_context(bs
);
251 if (ctx
== qemu_get_aio_context()) {
254 aio_context_acquire(ctx
);
255 bdrv_set_aio_context(bs
, qemu_get_aio_context());
256 aio_context_release(ctx
);
259 object_child_foreach(container
, iothread_stop
, NULL
);