blob: fd526864be79ea47bea6a27b409d66f2aec73fc7 [file] [log] [blame]
Paolo Bonzinif42b2202012-06-09 04:01:51 +02001/*
2 * QEMU aio implementation
3 *
4 * Copyright IBM Corp., 2008
5 * Copyright Red Hat Inc., 2012
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 *
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
16 */
17
18#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010019#include "block/block.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010020#include "qemu/queue.h"
21#include "qemu/sockets.h"
Paolo Bonzinif42b2202012-06-09 04:01:51 +020022
23struct AioHandler {
24 EventNotifier *e;
25 EventNotifierHandler *io_notify;
Paolo Bonzinif42b2202012-06-09 04:01:51 +020026 GPollFD pfd;
27 int deleted;
28 QLIST_ENTRY(AioHandler) node;
29};
30
31void aio_set_event_notifier(AioContext *ctx,
32 EventNotifier *e,
Stefan Hajnoczif2e5dca2013-04-11 17:26:25 +020033 EventNotifierHandler *io_notify)
Paolo Bonzinif42b2202012-06-09 04:01:51 +020034{
35 AioHandler *node;
36
37 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
38 if (node->e == e && !node->deleted) {
39 break;
40 }
41 }
42
43 /* Are we deleting the fd handler? */
44 if (!io_notify) {
45 if (node) {
Paolo Bonzinie3713e02012-09-24 14:57:41 +020046 g_source_remove_poll(&ctx->source, &node->pfd);
47
Paolo Bonzinif42b2202012-06-09 04:01:51 +020048 /* If the lock is held, just mark the node as deleted */
49 if (ctx->walking_handlers) {
50 node->deleted = 1;
51 node->pfd.revents = 0;
52 } else {
53 /* Otherwise, delete it for real. We can't just mark it as
54 * deleted because deleted nodes are only cleaned up after
55 * releasing the walking_handlers lock.
56 */
57 QLIST_REMOVE(node, node);
58 g_free(node);
59 }
60 }
61 } else {
62 if (node == NULL) {
63 /* Alloc and insert if it's not already there */
64 node = g_malloc0(sizeof(AioHandler));
65 node->e = e;
66 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
67 node->pfd.events = G_IO_IN;
68 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
Paolo Bonzinie3713e02012-09-24 14:57:41 +020069
70 g_source_add_poll(&ctx->source, &node->pfd);
Paolo Bonzinif42b2202012-06-09 04:01:51 +020071 }
72 /* Update handler with latest information */
73 node->io_notify = io_notify;
Paolo Bonzinif42b2202012-06-09 04:01:51 +020074 }
Paolo Bonzini7ed2b242012-09-25 10:22:39 +020075
76 aio_notify(ctx);
Paolo Bonzinif42b2202012-06-09 04:01:51 +020077}
78
79bool aio_pending(AioContext *ctx)
80{
81 AioHandler *node;
82
83 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
84 if (node->pfd.revents && node->io_notify) {
85 return true;
86 }
87 }
88
89 return false;
90}
91
Paolo Bonzinia398dea2014-07-09 11:53:03 +020092static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
Paolo Bonzinif42b2202012-06-09 04:01:51 +020093{
94 AioHandler *node;
Paolo Bonzinia398dea2014-07-09 11:53:03 +020095 bool progress = false;
Paolo Bonzinif42b2202012-06-09 04:01:51 +020096
97 /*
Paolo Bonzini87f68d32014-07-07 15:18:02 +020098 * We have to walk very carefully in case aio_set_fd_handler is
Paolo Bonzinif42b2202012-06-09 04:01:51 +020099 * called while we're walking.
100 */
101 node = QLIST_FIRST(&ctx->aio_handlers);
102 while (node) {
103 AioHandler *tmp;
104
105 ctx->walking_handlers++;
106
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200107 if (!node->deleted &&
108 (node->pfd.revents || event_notifier_get_handle(node->e) == event) &&
109 node->io_notify) {
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200110 node->pfd.revents = 0;
111 node->io_notify(node->e);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200112
113 /* aio_notify() does not count as progress */
Stefan Hajnoczi8b2d42d2013-08-22 15:28:35 +0200114 if (node->e != &ctx->notifier) {
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200115 progress = true;
116 }
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200117 }
118
119 tmp = node;
120 node = QLIST_NEXT(node, node);
121
122 ctx->walking_handlers--;
123
124 if (!ctx->walking_handlers && tmp->deleted) {
125 QLIST_REMOVE(tmp, node);
126 g_free(tmp);
127 }
128 }
129
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200130 return progress;
131}
132
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200133bool aio_dispatch(AioContext *ctx)
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200134{
135 bool progress;
136
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200137 progress = aio_bh_poll(ctx);
138 progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
Paolo Bonzinid397ec992014-07-09 11:53:02 +0200139 progress |= timerlistgroup_run_timers(&ctx->tlg);
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200140 return progress;
141}
142
143bool aio_poll(AioContext *ctx, bool blocking)
144{
145 AioHandler *node;
146 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
Paolo Bonzini0a9dd162014-07-09 11:53:07 +0200147 bool was_dispatching, progress, first;
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200148 int count;
149 int timeout;
150
Paolo Bonzini0a9dd162014-07-09 11:53:07 +0200151 was_dispatching = ctx->dispatching;
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200152 progress = false;
153
Paolo Bonzini0a9dd162014-07-09 11:53:07 +0200154 /* aio_notify can avoid the expensive event_notifier_set if
155 * everything (file descriptors, bottom halves, timers) will
156 * be re-evaluated before the next blocking poll(). This is
157 * already true when aio_poll is called with blocking == false;
158 * if blocking == true, it is only true after poll() returns.
159 *
160 * If we're in a nested event loop, ctx->dispatching might be true.
161 * In that case we can restore it just before returning, but we
162 * have to clear it now.
163 */
164 aio_set_dispatching(ctx, !blocking);
165
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200166 ctx->walking_handlers++;
167
168 /* fill fd sets */
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200169 count = 0;
170 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200171 if (!node->deleted && node->io_notify) {
172 events[count++] = event_notifier_get_handle(node->e);
173 }
174 }
175
176 ctx->walking_handlers--;
Paolo Bonzini3672fa52014-07-09 11:53:04 +0200177 first = true;
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200178
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200179 /* wait until next event */
Paolo Bonzinib022b4a2012-11-23 15:59:43 +0100180 while (count > 0) {
Alex Bligh438e1f42013-08-21 16:02:53 +0100181 int ret;
182
Paolo Bonzini845ca102014-07-09 11:53:01 +0200183 timeout = blocking
184 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
Alex Bligh438e1f42013-08-21 16:02:53 +0100185 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
Paolo Bonzini0a9dd162014-07-09 11:53:07 +0200186 aio_set_dispatching(ctx, true);
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200187
Paolo Bonzini3672fa52014-07-09 11:53:04 +0200188 if (first && aio_bh_poll(ctx)) {
189 progress = true;
190 }
191 first = false;
192
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200193 /* if we have any signaled events, dispatch event */
194 if ((DWORD) (ret - WAIT_OBJECT_0) >= count) {
195 break;
196 }
197
198 blocking = false;
199
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200200 progress |= aio_dispatch_handlers(ctx, events[ret - WAIT_OBJECT_0]);
Paolo Bonzinib022b4a2012-11-23 15:59:43 +0100201
202 /* Try again, but only call each handler once. */
203 events[ret - WAIT_OBJECT_0] = events[--count];
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200204 }
205
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200206 progress |= timerlistgroup_run_timers(&ctx->tlg);
Alex Bligh438e1f42013-08-21 16:02:53 +0100207
Paolo Bonzini0a9dd162014-07-09 11:53:07 +0200208 aio_set_dispatching(ctx, was_dispatching);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200209 return progress;
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200210}