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