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