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 | |
Stefan Hajnoczi | cdd7abf | 2017-01-26 17:01:19 +0000 | [diff] [blame] | 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 | |
Paolo Bonzini | e437016 | 2016-10-27 12:48:59 +0200 | [diff] [blame] | 39 | static __thread IOThread *my_iothread; |
| 40 | |
| 41 | AioContext *qemu_get_current_aio_context(void) |
| 42 | { |
| 43 | return my_iothread ? my_iothread->ctx : qemu_get_aio_context(); |
| 44 | } |
| 45 | |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 46 | static void *iothread_run(void *opaque) |
| 47 | { |
| 48 | IOThread *iothread = opaque; |
| 49 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 50 | rcu_register_thread(); |
| 51 | |
Paolo Bonzini | e437016 | 2016-10-27 12:48:59 +0200 | [diff] [blame] | 52 | my_iothread = iothread; |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 53 | qemu_mutex_lock(&iothread->init_done_lock); |
| 54 | iothread->thread_id = qemu_get_thread_id(); |
| 55 | qemu_cond_signal(&iothread->init_done_cond); |
| 56 | qemu_mutex_unlock(&iothread->init_done_lock); |
| 57 | |
Paolo Bonzini | 65c1b5b | 2016-10-27 12:49:06 +0200 | [diff] [blame] | 58 | while (!atomic_read(&iothread->stopping)) { |
| 59 | aio_poll(iothread->ctx, true); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 60 | } |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 61 | |
| 62 | rcu_unregister_thread(); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
| 65 | |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 66 | static int iothread_stop(Object *object, void *opaque) |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 67 | { |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 68 | IOThread *iothread; |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 69 | |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 70 | iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD); |
| 71 | if (!iothread || !iothread->ctx) { |
| 72 | return 0; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 73 | } |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 74 | iothread->stopping = true; |
| 75 | aio_notify(iothread->ctx); |
| 76 | qemu_thread_join(&iothread->thread); |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Stefan Hajnoczi | cdd7abf | 2017-01-26 17:01:19 +0000 | [diff] [blame] | 80 | static void iothread_instance_init(Object *obj) |
| 81 | { |
| 82 | IOThread *iothread = IOTHREAD(obj); |
| 83 | |
| 84 | iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT; |
| 85 | } |
| 86 | |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 87 | static void iothread_instance_finalize(Object *obj) |
| 88 | { |
| 89 | IOThread *iothread = IOTHREAD(obj); |
| 90 | |
| 91 | iothread_stop(obj, NULL); |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 92 | qemu_cond_destroy(&iothread->init_done_cond); |
| 93 | qemu_mutex_destroy(&iothread->init_done_lock); |
Lin Ma | eb7b5c3 | 2016-09-26 13:29:58 +0800 | [diff] [blame] | 94 | if (!iothread->ctx) { |
| 95 | return; |
| 96 | } |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 97 | aio_context_unref(iothread->ctx); |
| 98 | } |
| 99 | |
| 100 | static void iothread_complete(UserCreatable *obj, Error **errp) |
| 101 | { |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 102 | Error *local_error = NULL; |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 103 | IOThread *iothread = IOTHREAD(obj); |
Paolo Bonzini | d21e877 | 2015-11-24 14:46:44 +0100 | [diff] [blame] | 104 | char *name, *thread_name; |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 105 | |
| 106 | iothread->stopping = false; |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 107 | iothread->thread_id = -1; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 108 | iothread->ctx = aio_context_new(&local_error); |
| 109 | if (!iothread->ctx) { |
| 110 | error_propagate(errp, local_error); |
| 111 | return; |
| 112 | } |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 113 | |
Stefan Hajnoczi | 5e5db49 | 2016-12-01 19:26:52 +0000 | [diff] [blame] | 114 | aio_context_set_poll_params(iothread->ctx, |
| 115 | iothread->poll_max_ns, |
| 116 | iothread->poll_grow, |
| 117 | iothread->poll_shrink, |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame] | 118 | &local_error); |
| 119 | if (local_error) { |
| 120 | error_propagate(errp, local_error); |
| 121 | aio_context_unref(iothread->ctx); |
| 122 | iothread->ctx = NULL; |
| 123 | return; |
| 124 | } |
| 125 | |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 126 | qemu_mutex_init(&iothread->init_done_lock); |
| 127 | qemu_cond_init(&iothread->init_done_cond); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 128 | |
| 129 | /* This assumes we are called from a thread with useful CPU affinity for us |
| 130 | * to inherit. |
| 131 | */ |
Paolo Bonzini | d21e877 | 2015-11-24 14:46:44 +0100 | [diff] [blame] | 132 | name = object_get_canonical_path_component(OBJECT(obj)); |
| 133 | thread_name = g_strdup_printf("IO %s", name); |
| 134 | qemu_thread_create(&iothread->thread, thread_name, iothread_run, |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 135 | iothread, QEMU_THREAD_JOINABLE); |
Paolo Bonzini | d21e877 | 2015-11-24 14:46:44 +0100 | [diff] [blame] | 136 | g_free(thread_name); |
| 137 | g_free(name); |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 138 | |
| 139 | /* Wait for initialization to complete */ |
| 140 | qemu_mutex_lock(&iothread->init_done_lock); |
| 141 | while (iothread->thread_id == -1) { |
| 142 | qemu_cond_wait(&iothread->init_done_cond, |
| 143 | &iothread->init_done_lock); |
| 144 | } |
| 145 | qemu_mutex_unlock(&iothread->init_done_lock); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Stefan Hajnoczi | 5e5db49 | 2016-12-01 19:26:52 +0000 | [diff] [blame] | 148 | typedef struct { |
| 149 | const char *name; |
| 150 | ptrdiff_t offset; /* field's byte offset in IOThread struct */ |
| 151 | } PollParamInfo; |
| 152 | |
| 153 | static PollParamInfo poll_max_ns_info = { |
| 154 | "poll-max-ns", offsetof(IOThread, poll_max_ns), |
| 155 | }; |
| 156 | static PollParamInfo poll_grow_info = { |
| 157 | "poll-grow", offsetof(IOThread, poll_grow), |
| 158 | }; |
| 159 | static PollParamInfo poll_shrink_info = { |
| 160 | "poll-shrink", offsetof(IOThread, poll_shrink), |
| 161 | }; |
| 162 | |
| 163 | static void iothread_get_poll_param(Object *obj, Visitor *v, |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame] | 164 | const char *name, void *opaque, Error **errp) |
| 165 | { |
| 166 | IOThread *iothread = IOTHREAD(obj); |
Stefan Hajnoczi | 5e5db49 | 2016-12-01 19:26:52 +0000 | [diff] [blame] | 167 | PollParamInfo *info = opaque; |
| 168 | int64_t *field = (void *)iothread + info->offset; |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame] | 169 | |
Stefan Hajnoczi | 5e5db49 | 2016-12-01 19:26:52 +0000 | [diff] [blame] | 170 | visit_type_int64(v, name, field, errp); |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Stefan Hajnoczi | 5e5db49 | 2016-12-01 19:26:52 +0000 | [diff] [blame] | 173 | static void iothread_set_poll_param(Object *obj, Visitor *v, |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame] | 174 | const char *name, void *opaque, Error **errp) |
| 175 | { |
| 176 | IOThread *iothread = IOTHREAD(obj); |
Stefan Hajnoczi | 5e5db49 | 2016-12-01 19:26:52 +0000 | [diff] [blame] | 177 | PollParamInfo *info = opaque; |
| 178 | int64_t *field = (void *)iothread + info->offset; |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame] | 179 | Error *local_err = NULL; |
| 180 | int64_t value; |
| 181 | |
| 182 | visit_type_int64(v, name, &value, &local_err); |
| 183 | if (local_err) { |
| 184 | goto out; |
| 185 | } |
| 186 | |
| 187 | if (value < 0) { |
Stefan Hajnoczi | 5e5db49 | 2016-12-01 19:26:52 +0000 | [diff] [blame] | 188 | error_setg(&local_err, "%s value must be in range [0, %"PRId64"]", |
| 189 | info->name, INT64_MAX); |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame] | 190 | goto out; |
| 191 | } |
| 192 | |
Stefan Hajnoczi | 5e5db49 | 2016-12-01 19:26:52 +0000 | [diff] [blame] | 193 | *field = value; |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame] | 194 | |
| 195 | if (iothread->ctx) { |
Stefan Hajnoczi | 5e5db49 | 2016-12-01 19:26:52 +0000 | [diff] [blame] | 196 | aio_context_set_poll_params(iothread->ctx, |
| 197 | iothread->poll_max_ns, |
| 198 | iothread->poll_grow, |
| 199 | iothread->poll_shrink, |
| 200 | &local_err); |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | out: |
| 204 | error_propagate(errp, local_err); |
| 205 | } |
| 206 | |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 207 | static void iothread_class_init(ObjectClass *klass, void *class_data) |
| 208 | { |
| 209 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass); |
| 210 | ucc->complete = iothread_complete; |
Stefan Hajnoczi | 0d9d86f | 2016-12-01 19:26:45 +0000 | [diff] [blame] | 211 | |
| 212 | object_class_property_add(klass, "poll-max-ns", "int", |
Stefan Hajnoczi | 5e5db49 | 2016-12-01 19:26:52 +0000 | [diff] [blame] | 213 | iothread_get_poll_param, |
| 214 | iothread_set_poll_param, |
| 215 | NULL, &poll_max_ns_info, &error_abort); |
| 216 | object_class_property_add(klass, "poll-grow", "int", |
| 217 | iothread_get_poll_param, |
| 218 | iothread_set_poll_param, |
| 219 | NULL, &poll_grow_info, &error_abort); |
| 220 | object_class_property_add(klass, "poll-shrink", "int", |
| 221 | iothread_get_poll_param, |
| 222 | iothread_set_poll_param, |
| 223 | NULL, &poll_shrink_info, &error_abort); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static const TypeInfo iothread_info = { |
| 227 | .name = TYPE_IOTHREAD, |
| 228 | .parent = TYPE_OBJECT, |
| 229 | .class_init = iothread_class_init, |
| 230 | .instance_size = sizeof(IOThread), |
Stefan Hajnoczi | cdd7abf | 2017-01-26 17:01:19 +0000 | [diff] [blame] | 231 | .instance_init = iothread_instance_init, |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 232 | .instance_finalize = iothread_instance_finalize, |
| 233 | .interfaces = (InterfaceInfo[]) { |
| 234 | {TYPE_USER_CREATABLE}, |
| 235 | {} |
| 236 | }, |
| 237 | }; |
| 238 | |
| 239 | static void iothread_register_types(void) |
| 240 | { |
| 241 | type_register_static(&iothread_info); |
| 242 | } |
| 243 | |
| 244 | type_init(iothread_register_types) |
| 245 | |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 246 | char *iothread_get_id(IOThread *iothread) |
| 247 | { |
| 248 | return object_get_canonical_path_component(OBJECT(iothread)); |
| 249 | } |
| 250 | |
| 251 | AioContext *iothread_get_aio_context(IOThread *iothread) |
| 252 | { |
| 253 | return iothread->ctx; |
| 254 | } |
Stefan Hajnoczi | dc3dd0d | 2014-02-27 11:48:42 +0100 | [diff] [blame] | 255 | |
| 256 | static int query_one_iothread(Object *object, void *opaque) |
| 257 | { |
| 258 | IOThreadInfoList ***prev = opaque; |
| 259 | IOThreadInfoList *elem; |
| 260 | IOThreadInfo *info; |
| 261 | IOThread *iothread; |
| 262 | |
| 263 | iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD); |
| 264 | if (!iothread) { |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | info = g_new0(IOThreadInfo, 1); |
| 269 | info->id = iothread_get_id(iothread); |
| 270 | info->thread_id = iothread->thread_id; |
Pavel Hrdina | 5fc0048 | 2017-02-10 10:41:17 +0100 | [diff] [blame] | 271 | info->poll_max_ns = iothread->poll_max_ns; |
| 272 | info->poll_grow = iothread->poll_grow; |
| 273 | info->poll_shrink = iothread->poll_shrink; |
Stefan Hajnoczi | dc3dd0d | 2014-02-27 11:48:42 +0100 | [diff] [blame] | 274 | |
| 275 | elem = g_new0(IOThreadInfoList, 1); |
| 276 | elem->value = info; |
| 277 | elem->next = NULL; |
| 278 | |
| 279 | **prev = elem; |
| 280 | *prev = &elem->next; |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | IOThreadInfoList *qmp_query_iothreads(Error **errp) |
| 285 | { |
| 286 | IOThreadInfoList *head = NULL; |
| 287 | IOThreadInfoList **prev = &head; |
Daniel P. Berrange | bc2256c | 2015-05-13 17:14:05 +0100 | [diff] [blame] | 288 | Object *container = object_get_objects_root(); |
Stefan Hajnoczi | dc3dd0d | 2014-02-27 11:48:42 +0100 | [diff] [blame] | 289 | |
| 290 | object_child_foreach(container, query_one_iothread, &prev); |
| 291 | return head; |
| 292 | } |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 293 | |
| 294 | void iothread_stop_all(void) |
| 295 | { |
| 296 | Object *container = object_get_objects_root(); |
Paolo Bonzini | d16341f | 2016-10-27 12:49:00 +0200 | [diff] [blame] | 297 | BlockDriverState *bs; |
| 298 | BdrvNextIterator it; |
| 299 | |
| 300 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
| 301 | AioContext *ctx = bdrv_get_aio_context(bs); |
| 302 | if (ctx == qemu_get_aio_context()) { |
| 303 | continue; |
| 304 | } |
| 305 | aio_context_acquire(ctx); |
| 306 | bdrv_set_aio_context(bs, qemu_get_aio_context()); |
| 307 | aio_context_release(ctx); |
| 308 | } |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 309 | |
| 310 | object_child_foreach(container, iothread_stop, NULL); |
| 311 | } |