Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 1 | /* |
| 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 Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 15 | #include "qom/object.h" |
| 16 | #include "qom/object_interfaces.h" |
| 17 | #include "qemu/module.h" |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 18 | #include "block/aio.h" |
Paolo Bonzini | d16341f | 2016-10-27 12:49:00 +0200 | [diff] [blame] | 19 | #include "block/block.h" |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 20 | #include "sysemu/iothread.h" |
Stefan Hajnoczi | dc3dd0d | 2014-02-27 11:48:42 +0100 | [diff] [blame] | 21 | #include "qmp-commands.h" |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 22 | #include "qemu/error-report.h" |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 23 | #include "qemu/rcu.h" |
Paolo Bonzini | e437016 | 2016-10-27 12:48:59 +0200 | [diff] [blame] | 24 | #include "qemu/main-loop.h" |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 25 | |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 26 | typedef ObjectClass IOThreadClass; |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 27 | |
| 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) |
| 32 | |
Paolo Bonzini | e437016 | 2016-10-27 12:48:59 +0200 | [diff] [blame] | 33 | static __thread IOThread *my_iothread; |
| 34 | |
| 35 | AioContext *qemu_get_current_aio_context(void) |
| 36 | { |
| 37 | return my_iothread ? my_iothread->ctx : qemu_get_aio_context(); |
| 38 | } |
| 39 | |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 40 | static void *iothread_run(void *opaque) |
| 41 | { |
| 42 | IOThread *iothread = opaque; |
| 43 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 44 | rcu_register_thread(); |
| 45 | |
Paolo Bonzini | e437016 | 2016-10-27 12:48:59 +0200 | [diff] [blame] | 46 | my_iothread = iothread; |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 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); |
| 51 | |
Paolo Bonzini | 65c1b5b | 2016-10-27 12:49:06 +0200 | [diff] [blame] | 52 | while (!atomic_read(&iothread->stopping)) { |
| 53 | aio_poll(iothread->ctx, true); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 54 | } |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 55 | |
| 56 | rcu_unregister_thread(); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 57 | return NULL; |
| 58 | } |
| 59 | |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 60 | static int iothread_stop(Object *object, void *opaque) |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 61 | { |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 62 | IOThread *iothread; |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 63 | |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 64 | iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD); |
| 65 | if (!iothread || !iothread->ctx) { |
| 66 | return 0; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 67 | } |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 68 | iothread->stopping = true; |
| 69 | aio_notify(iothread->ctx); |
| 70 | qemu_thread_join(&iothread->thread); |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static void iothread_instance_finalize(Object *obj) |
| 75 | { |
| 76 | IOThread *iothread = IOTHREAD(obj); |
| 77 | |
| 78 | iothread_stop(obj, NULL); |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 79 | qemu_cond_destroy(&iothread->init_done_cond); |
| 80 | qemu_mutex_destroy(&iothread->init_done_lock); |
Lin Ma | eb7b5c3 | 2016-09-26 13:29:58 +0800 | [diff] [blame] | 81 | if (!iothread->ctx) { |
| 82 | return; |
| 83 | } |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 84 | aio_context_unref(iothread->ctx); |
| 85 | } |
| 86 | |
| 87 | static void iothread_complete(UserCreatable *obj, Error **errp) |
| 88 | { |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 89 | Error *local_error = NULL; |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 90 | IOThread *iothread = IOTHREAD(obj); |
Paolo Bonzini | d21e877 | 2015-11-24 14:46:44 +0100 | [diff] [blame] | 91 | char *name, *thread_name; |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 92 | |
| 93 | iothread->stopping = false; |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 94 | iothread->thread_id = -1; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 95 | iothread->ctx = aio_context_new(&local_error); |
| 96 | if (!iothread->ctx) { |
| 97 | error_propagate(errp, local_error); |
| 98 | return; |
| 99 | } |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 100 | |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame^] | 101 | aio_context_set_poll_params(iothread->ctx, iothread->poll_max_ns, |
| 102 | &local_error); |
| 103 | if (local_error) { |
| 104 | error_propagate(errp, local_error); |
| 105 | aio_context_unref(iothread->ctx); |
| 106 | iothread->ctx = NULL; |
| 107 | return; |
| 108 | } |
| 109 | |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 110 | qemu_mutex_init(&iothread->init_done_lock); |
| 111 | qemu_cond_init(&iothread->init_done_cond); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 112 | |
| 113 | /* This assumes we are called from a thread with useful CPU affinity for us |
| 114 | * to inherit. |
| 115 | */ |
Paolo Bonzini | d21e877 | 2015-11-24 14:46:44 +0100 | [diff] [blame] | 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, |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 119 | iothread, QEMU_THREAD_JOINABLE); |
Paolo Bonzini | d21e877 | 2015-11-24 14:46:44 +0100 | [diff] [blame] | 120 | g_free(thread_name); |
| 121 | g_free(name); |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 122 | |
| 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); |
| 128 | } |
| 129 | qemu_mutex_unlock(&iothread->init_done_lock); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame^] | 132 | static void iothread_get_poll_max_ns(Object *obj, Visitor *v, |
| 133 | const char *name, void *opaque, Error **errp) |
| 134 | { |
| 135 | IOThread *iothread = IOTHREAD(obj); |
| 136 | |
| 137 | visit_type_int64(v, name, &iothread->poll_max_ns, errp); |
| 138 | } |
| 139 | |
| 140 | static void iothread_set_poll_max_ns(Object *obj, Visitor *v, |
| 141 | const char *name, void *opaque, Error **errp) |
| 142 | { |
| 143 | IOThread *iothread = IOTHREAD(obj); |
| 144 | Error *local_err = NULL; |
| 145 | int64_t value; |
| 146 | |
| 147 | visit_type_int64(v, name, &value, &local_err); |
| 148 | if (local_err) { |
| 149 | goto out; |
| 150 | } |
| 151 | |
| 152 | if (value < 0) { |
| 153 | error_setg(&local_err, "poll_max_ns value must be in range " |
| 154 | "[0, %"PRId64"]", INT64_MAX); |
| 155 | goto out; |
| 156 | } |
| 157 | |
| 158 | iothread->poll_max_ns = value; |
| 159 | |
| 160 | if (iothread->ctx) { |
| 161 | aio_context_set_poll_params(iothread->ctx, value, &local_err); |
| 162 | } |
| 163 | |
| 164 | out: |
| 165 | error_propagate(errp, local_err); |
| 166 | } |
| 167 | |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 168 | static void iothread_class_init(ObjectClass *klass, void *class_data) |
| 169 | { |
| 170 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass); |
| 171 | ucc->complete = iothread_complete; |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame^] | 172 | |
| 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); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 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}, |
| 187 | {} |
| 188 | }, |
| 189 | }; |
| 190 | |
| 191 | static void iothread_register_types(void) |
| 192 | { |
| 193 | type_register_static(&iothread_info); |
| 194 | } |
| 195 | |
| 196 | type_init(iothread_register_types) |
| 197 | |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 198 | char *iothread_get_id(IOThread *iothread) |
| 199 | { |
| 200 | return object_get_canonical_path_component(OBJECT(iothread)); |
| 201 | } |
| 202 | |
| 203 | AioContext *iothread_get_aio_context(IOThread *iothread) |
| 204 | { |
| 205 | return iothread->ctx; |
| 206 | } |
Stefan Hajnoczi | dc3dd0d | 2014-02-27 11:48:42 +0100 | [diff] [blame] | 207 | |
| 208 | static int query_one_iothread(Object *object, void *opaque) |
| 209 | { |
| 210 | IOThreadInfoList ***prev = opaque; |
| 211 | IOThreadInfoList *elem; |
| 212 | IOThreadInfo *info; |
| 213 | IOThread *iothread; |
| 214 | |
| 215 | iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD); |
| 216 | if (!iothread) { |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | info = g_new0(IOThreadInfo, 1); |
| 221 | info->id = iothread_get_id(iothread); |
| 222 | info->thread_id = iothread->thread_id; |
| 223 | |
| 224 | elem = g_new0(IOThreadInfoList, 1); |
| 225 | elem->value = info; |
| 226 | elem->next = NULL; |
| 227 | |
| 228 | **prev = elem; |
| 229 | *prev = &elem->next; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | IOThreadInfoList *qmp_query_iothreads(Error **errp) |
| 234 | { |
| 235 | IOThreadInfoList *head = NULL; |
| 236 | IOThreadInfoList **prev = &head; |
Daniel P. Berrange | bc2256c | 2015-05-13 17:14:05 +0100 | [diff] [blame] | 237 | Object *container = object_get_objects_root(); |
Stefan Hajnoczi | dc3dd0d | 2014-02-27 11:48:42 +0100 | [diff] [blame] | 238 | |
| 239 | object_child_foreach(container, query_one_iothread, &prev); |
| 240 | return head; |
| 241 | } |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 242 | |
| 243 | void iothread_stop_all(void) |
| 244 | { |
| 245 | Object *container = object_get_objects_root(); |
Paolo Bonzini | d16341f | 2016-10-27 12:49:00 +0200 | [diff] [blame] | 246 | BlockDriverState *bs; |
| 247 | BdrvNextIterator it; |
| 248 | |
| 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()) { |
| 252 | continue; |
| 253 | } |
| 254 | aio_context_acquire(ctx); |
| 255 | bdrv_set_aio_context(bs, qemu_get_aio_context()); |
| 256 | aio_context_release(ctx); |
| 257 | } |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 258 | |
| 259 | object_child_foreach(container, iothread_stop, NULL); |
| 260 | } |