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; |
Stefan Hajnoczi | da5e1de | 2015-06-03 10:15:33 +0100 | [diff] [blame] | 43 | bool blocking; |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 44 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 45 | rcu_register_thread(); |
| 46 | |
Paolo Bonzini | e437016 | 2016-10-27 12:48:59 +0200 | [diff] [blame] | 47 | my_iothread = iothread; |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 48 | qemu_mutex_lock(&iothread->init_done_lock); |
| 49 | iothread->thread_id = qemu_get_thread_id(); |
| 50 | qemu_cond_signal(&iothread->init_done_cond); |
| 51 | qemu_mutex_unlock(&iothread->init_done_lock); |
| 52 | |
Stefan Hajnoczi | da5e1de | 2015-06-03 10:15:33 +0100 | [diff] [blame] | 53 | while (!iothread->stopping) { |
| 54 | aio_context_acquire(iothread->ctx); |
| 55 | blocking = true; |
| 56 | while (!iothread->stopping && aio_poll(iothread->ctx, blocking)) { |
| 57 | /* Progress was made, keep going */ |
| 58 | blocking = false; |
| 59 | } |
| 60 | aio_context_release(iothread->ctx); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 61 | } |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 62 | |
| 63 | rcu_unregister_thread(); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 64 | return NULL; |
| 65 | } |
| 66 | |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 67 | static int iothread_stop(Object *object, void *opaque) |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 68 | { |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 69 | IOThread *iothread; |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 70 | |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 71 | iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD); |
| 72 | if (!iothread || !iothread->ctx) { |
| 73 | return 0; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 74 | } |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 75 | iothread->stopping = true; |
| 76 | aio_notify(iothread->ctx); |
| 77 | qemu_thread_join(&iothread->thread); |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static void iothread_instance_finalize(Object *obj) |
| 82 | { |
| 83 | IOThread *iothread = IOTHREAD(obj); |
| 84 | |
| 85 | iothread_stop(obj, NULL); |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 86 | qemu_cond_destroy(&iothread->init_done_cond); |
| 87 | qemu_mutex_destroy(&iothread->init_done_lock); |
Lin Ma | eb7b5c3 | 2016-09-26 13:29:58 +0800 | [diff] [blame] | 88 | if (!iothread->ctx) { |
| 89 | return; |
| 90 | } |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 91 | aio_context_unref(iothread->ctx); |
| 92 | } |
| 93 | |
| 94 | static void iothread_complete(UserCreatable *obj, Error **errp) |
| 95 | { |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 96 | Error *local_error = NULL; |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 97 | IOThread *iothread = IOTHREAD(obj); |
Paolo Bonzini | d21e877 | 2015-11-24 14:46:44 +0100 | [diff] [blame] | 98 | char *name, *thread_name; |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 99 | |
| 100 | iothread->stopping = false; |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 101 | iothread->thread_id = -1; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 102 | iothread->ctx = aio_context_new(&local_error); |
| 103 | if (!iothread->ctx) { |
| 104 | error_propagate(errp, local_error); |
| 105 | return; |
| 106 | } |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 107 | |
| 108 | qemu_mutex_init(&iothread->init_done_lock); |
| 109 | qemu_cond_init(&iothread->init_done_cond); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 110 | |
| 111 | /* This assumes we are called from a thread with useful CPU affinity for us |
| 112 | * to inherit. |
| 113 | */ |
Paolo Bonzini | d21e877 | 2015-11-24 14:46:44 +0100 | [diff] [blame] | 114 | name = object_get_canonical_path_component(OBJECT(obj)); |
| 115 | thread_name = g_strdup_printf("IO %s", name); |
| 116 | qemu_thread_create(&iothread->thread, thread_name, iothread_run, |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 117 | iothread, QEMU_THREAD_JOINABLE); |
Paolo Bonzini | d21e877 | 2015-11-24 14:46:44 +0100 | [diff] [blame] | 118 | g_free(thread_name); |
| 119 | g_free(name); |
Stefan Hajnoczi | 88eb7c2 | 2014-02-27 11:48:41 +0100 | [diff] [blame] | 120 | |
| 121 | /* Wait for initialization to complete */ |
| 122 | qemu_mutex_lock(&iothread->init_done_lock); |
| 123 | while (iothread->thread_id == -1) { |
| 124 | qemu_cond_wait(&iothread->init_done_cond, |
| 125 | &iothread->init_done_lock); |
| 126 | } |
| 127 | qemu_mutex_unlock(&iothread->init_done_lock); |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void iothread_class_init(ObjectClass *klass, void *class_data) |
| 131 | { |
| 132 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass); |
| 133 | ucc->complete = iothread_complete; |
| 134 | } |
| 135 | |
| 136 | static const TypeInfo iothread_info = { |
| 137 | .name = TYPE_IOTHREAD, |
| 138 | .parent = TYPE_OBJECT, |
| 139 | .class_init = iothread_class_init, |
| 140 | .instance_size = sizeof(IOThread), |
| 141 | .instance_finalize = iothread_instance_finalize, |
| 142 | .interfaces = (InterfaceInfo[]) { |
| 143 | {TYPE_USER_CREATABLE}, |
| 144 | {} |
| 145 | }, |
| 146 | }; |
| 147 | |
| 148 | static void iothread_register_types(void) |
| 149 | { |
| 150 | type_register_static(&iothread_info); |
| 151 | } |
| 152 | |
| 153 | type_init(iothread_register_types) |
| 154 | |
Stefan Hajnoczi | be8d853 | 2014-03-03 11:30:05 +0100 | [diff] [blame] | 155 | char *iothread_get_id(IOThread *iothread) |
| 156 | { |
| 157 | return object_get_canonical_path_component(OBJECT(iothread)); |
| 158 | } |
| 159 | |
| 160 | AioContext *iothread_get_aio_context(IOThread *iothread) |
| 161 | { |
| 162 | return iothread->ctx; |
| 163 | } |
Stefan Hajnoczi | dc3dd0d | 2014-02-27 11:48:42 +0100 | [diff] [blame] | 164 | |
| 165 | static int query_one_iothread(Object *object, void *opaque) |
| 166 | { |
| 167 | IOThreadInfoList ***prev = opaque; |
| 168 | IOThreadInfoList *elem; |
| 169 | IOThreadInfo *info; |
| 170 | IOThread *iothread; |
| 171 | |
| 172 | iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD); |
| 173 | if (!iothread) { |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | info = g_new0(IOThreadInfo, 1); |
| 178 | info->id = iothread_get_id(iothread); |
| 179 | info->thread_id = iothread->thread_id; |
| 180 | |
| 181 | elem = g_new0(IOThreadInfoList, 1); |
| 182 | elem->value = info; |
| 183 | elem->next = NULL; |
| 184 | |
| 185 | **prev = elem; |
| 186 | *prev = &elem->next; |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | IOThreadInfoList *qmp_query_iothreads(Error **errp) |
| 191 | { |
| 192 | IOThreadInfoList *head = NULL; |
| 193 | IOThreadInfoList **prev = &head; |
Daniel P. Berrange | bc2256c | 2015-05-13 17:14:05 +0100 | [diff] [blame] | 194 | Object *container = object_get_objects_root(); |
Stefan Hajnoczi | dc3dd0d | 2014-02-27 11:48:42 +0100 | [diff] [blame] | 195 | |
| 196 | object_child_foreach(container, query_one_iothread, &prev); |
| 197 | return head; |
| 198 | } |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 199 | |
| 200 | void iothread_stop_all(void) |
| 201 | { |
| 202 | Object *container = object_get_objects_root(); |
Paolo Bonzini | d16341f | 2016-10-27 12:49:00 +0200 | [diff] [blame^] | 203 | BlockDriverState *bs; |
| 204 | BdrvNextIterator it; |
| 205 | |
| 206 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
| 207 | AioContext *ctx = bdrv_get_aio_context(bs); |
| 208 | if (ctx == qemu_get_aio_context()) { |
| 209 | continue; |
| 210 | } |
| 211 | aio_context_acquire(ctx); |
| 212 | bdrv_set_aio_context(bs, qemu_get_aio_context()); |
| 213 | aio_context_release(ctx); |
| 214 | } |
Fam Zheng | dce8921 | 2016-09-08 17:28:51 +0800 | [diff] [blame] | 215 | |
| 216 | object_child_foreach(container, iothread_stop, NULL); |
| 217 | } |