blob: fbeb8deb385fea8d7d5199e9a2199415951c54c4 [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"
19#include "sysemu/iothread.h"
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +010020#include "qmp-commands.h"
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030021#include "qemu/error-report.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020022#include "qemu/rcu.h"
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010023
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010024typedef ObjectClass IOThreadClass;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010025
26#define IOTHREAD_GET_CLASS(obj) \
27 OBJECT_GET_CLASS(IOThreadClass, obj, TYPE_IOTHREAD)
28#define IOTHREAD_CLASS(klass) \
29 OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD)
30
31static void *iothread_run(void *opaque)
32{
33 IOThread *iothread = opaque;
Stefan Hajnoczida5e1de2015-06-03 10:15:33 +010034 bool blocking;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010035
Paolo Bonziniab28bd22015-07-09 08:55:38 +020036 rcu_register_thread();
37
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010038 qemu_mutex_lock(&iothread->init_done_lock);
39 iothread->thread_id = qemu_get_thread_id();
40 qemu_cond_signal(&iothread->init_done_cond);
41 qemu_mutex_unlock(&iothread->init_done_lock);
42
Stefan Hajnoczida5e1de2015-06-03 10:15:33 +010043 while (!iothread->stopping) {
44 aio_context_acquire(iothread->ctx);
45 blocking = true;
46 while (!iothread->stopping && aio_poll(iothread->ctx, blocking)) {
47 /* Progress was made, keep going */
48 blocking = false;
49 }
50 aio_context_release(iothread->ctx);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010051 }
Paolo Bonziniab28bd22015-07-09 08:55:38 +020052
53 rcu_unregister_thread();
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010054 return NULL;
55}
56
Fam Zhengdce89212016-09-08 17:28:51 +080057static int iothread_stop(Object *object, void *opaque)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010058{
Fam Zhengdce89212016-09-08 17:28:51 +080059 IOThread *iothread;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010060
Fam Zhengdce89212016-09-08 17:28:51 +080061 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
62 if (!iothread || !iothread->ctx) {
63 return 0;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030064 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010065 iothread->stopping = true;
66 aio_notify(iothread->ctx);
67 qemu_thread_join(&iothread->thread);
Fam Zhengdce89212016-09-08 17:28:51 +080068 return 0;
69}
70
71static void iothread_instance_finalize(Object *obj)
72{
73 IOThread *iothread = IOTHREAD(obj);
74
75 iothread_stop(obj, NULL);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010076 qemu_cond_destroy(&iothread->init_done_cond);
77 qemu_mutex_destroy(&iothread->init_done_lock);
Lin Maeb7b5c32016-09-26 13:29:58 +080078 if (!iothread->ctx) {
79 return;
80 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010081 aio_context_unref(iothread->ctx);
82}
83
84static void iothread_complete(UserCreatable *obj, Error **errp)
85{
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030086 Error *local_error = NULL;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010087 IOThread *iothread = IOTHREAD(obj);
Paolo Bonzinid21e8772015-11-24 14:46:44 +010088 char *name, *thread_name;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010089
90 iothread->stopping = false;
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010091 iothread->thread_id = -1;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030092 iothread->ctx = aio_context_new(&local_error);
93 if (!iothread->ctx) {
94 error_propagate(errp, local_error);
95 return;
96 }
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010097
98 qemu_mutex_init(&iothread->init_done_lock);
99 qemu_cond_init(&iothread->init_done_cond);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100100
101 /* This assumes we are called from a thread with useful CPU affinity for us
102 * to inherit.
103 */
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100104 name = object_get_canonical_path_component(OBJECT(obj));
105 thread_name = g_strdup_printf("IO %s", name);
106 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100107 iothread, QEMU_THREAD_JOINABLE);
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100108 g_free(thread_name);
109 g_free(name);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100110
111 /* Wait for initialization to complete */
112 qemu_mutex_lock(&iothread->init_done_lock);
113 while (iothread->thread_id == -1) {
114 qemu_cond_wait(&iothread->init_done_cond,
115 &iothread->init_done_lock);
116 }
117 qemu_mutex_unlock(&iothread->init_done_lock);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100118}
119
120static void iothread_class_init(ObjectClass *klass, void *class_data)
121{
122 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
123 ucc->complete = iothread_complete;
124}
125
126static const TypeInfo iothread_info = {
127 .name = TYPE_IOTHREAD,
128 .parent = TYPE_OBJECT,
129 .class_init = iothread_class_init,
130 .instance_size = sizeof(IOThread),
131 .instance_finalize = iothread_instance_finalize,
132 .interfaces = (InterfaceInfo[]) {
133 {TYPE_USER_CREATABLE},
134 {}
135 },
136};
137
138static void iothread_register_types(void)
139{
140 type_register_static(&iothread_info);
141}
142
143type_init(iothread_register_types)
144
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100145char *iothread_get_id(IOThread *iothread)
146{
147 return object_get_canonical_path_component(OBJECT(iothread));
148}
149
150AioContext *iothread_get_aio_context(IOThread *iothread)
151{
152 return iothread->ctx;
153}
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100154
155static int query_one_iothread(Object *object, void *opaque)
156{
157 IOThreadInfoList ***prev = opaque;
158 IOThreadInfoList *elem;
159 IOThreadInfo *info;
160 IOThread *iothread;
161
162 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
163 if (!iothread) {
164 return 0;
165 }
166
167 info = g_new0(IOThreadInfo, 1);
168 info->id = iothread_get_id(iothread);
169 info->thread_id = iothread->thread_id;
170
171 elem = g_new0(IOThreadInfoList, 1);
172 elem->value = info;
173 elem->next = NULL;
174
175 **prev = elem;
176 *prev = &elem->next;
177 return 0;
178}
179
180IOThreadInfoList *qmp_query_iothreads(Error **errp)
181{
182 IOThreadInfoList *head = NULL;
183 IOThreadInfoList **prev = &head;
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100184 Object *container = object_get_objects_root();
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100185
186 object_child_foreach(container, query_one_iothread, &prev);
187 return head;
188}
Fam Zhengdce89212016-09-08 17:28:51 +0800189
190void iothread_stop_all(void)
191{
192 Object *container = object_get_objects_root();
193
194 object_child_foreach(container, iothread_stop, NULL);
195}