blob: 233d8f5d7951738e0f043596060d7ff8a86e9866 [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;
Paolo Bonzinib4933172014-07-09 11:53:10 +020025 IOHandler *io_read;
26 IOHandler *io_write;
Paolo Bonzinif42b2202012-06-09 04:01:51 +020027 EventNotifierHandler *io_notify;
Paolo Bonzinif42b2202012-06-09 04:01:51 +020028 GPollFD pfd;
29 int deleted;
Paolo Bonzinib4933172014-07-09 11:53:10 +020030 void *opaque;
Paolo Bonzinif42b2202012-06-09 04:01:51 +020031 QLIST_ENTRY(AioHandler) node;
32};
33
Paolo Bonzinib4933172014-07-09 11:53:10 +020034void aio_set_fd_handler(AioContext *ctx,
35 int fd,
36 IOHandler *io_read,
37 IOHandler *io_write,
38 void *opaque)
39{
40 /* fd is a SOCKET in our case */
41 AioHandler *node;
42
43 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
44 if (node->pfd.fd == fd && !node->deleted) {
45 break;
46 }
47 }
48
49 /* Are we deleting the fd handler? */
50 if (!io_read && !io_write) {
51 if (node) {
52 /* If the lock is held, just mark the node as deleted */
53 if (ctx->walking_handlers) {
54 node->deleted = 1;
55 node->pfd.revents = 0;
56 } else {
57 /* Otherwise, delete it for real. We can't just mark it as
58 * deleted because deleted nodes are only cleaned up after
59 * releasing the walking_handlers lock.
60 */
61 QLIST_REMOVE(node, node);
62 g_free(node);
63 }
64 }
65 } else {
66 HANDLE event;
67
68 if (node == NULL) {
69 /* Alloc and insert if it's not already there */
Markus Armbruster3ba235a2014-12-04 13:55:09 +010070 node = g_new0(AioHandler, 1);
Paolo Bonzinib4933172014-07-09 11:53:10 +020071 node->pfd.fd = fd;
72 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
73 }
74
75 node->pfd.events = 0;
76 if (node->io_read) {
77 node->pfd.events |= G_IO_IN;
78 }
79 if (node->io_write) {
80 node->pfd.events |= G_IO_OUT;
81 }
82
83 node->e = &ctx->notifier;
84
85 /* Update handler with latest information */
86 node->opaque = opaque;
87 node->io_read = io_read;
88 node->io_write = io_write;
89
90 event = event_notifier_get_handle(&ctx->notifier);
91 WSAEventSelect(node->pfd.fd, event,
92 FD_READ | FD_ACCEPT | FD_CLOSE |
93 FD_CONNECT | FD_WRITE | FD_OOB);
94 }
95
96 aio_notify(ctx);
97}
98
Paolo Bonzinif42b2202012-06-09 04:01:51 +020099void aio_set_event_notifier(AioContext *ctx,
100 EventNotifier *e,
Stefan Hajnoczif2e5dca2013-04-11 17:26:25 +0200101 EventNotifierHandler *io_notify)
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200102{
103 AioHandler *node;
104
105 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
106 if (node->e == e && !node->deleted) {
107 break;
108 }
109 }
110
111 /* Are we deleting the fd handler? */
112 if (!io_notify) {
113 if (node) {
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200114 g_source_remove_poll(&ctx->source, &node->pfd);
115
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200116 /* If the lock is held, just mark the node as deleted */
117 if (ctx->walking_handlers) {
118 node->deleted = 1;
119 node->pfd.revents = 0;
120 } else {
121 /* Otherwise, delete it for real. We can't just mark it as
122 * deleted because deleted nodes are only cleaned up after
123 * releasing the walking_handlers lock.
124 */
125 QLIST_REMOVE(node, node);
126 g_free(node);
127 }
128 }
129 } else {
130 if (node == NULL) {
131 /* Alloc and insert if it's not already there */
Markus Armbruster3ba235a2014-12-04 13:55:09 +0100132 node = g_new0(AioHandler, 1);
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200133 node->e = e;
134 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
135 node->pfd.events = G_IO_IN;
136 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200137
138 g_source_add_poll(&ctx->source, &node->pfd);
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200139 }
140 /* Update handler with latest information */
141 node->io_notify = io_notify;
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200142 }
Paolo Bonzini7ed2b242012-09-25 10:22:39 +0200143
144 aio_notify(ctx);
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200145}
146
Paolo Bonzinia3462c62014-07-09 11:53:08 +0200147bool aio_prepare(AioContext *ctx)
148{
Paolo Bonzinib4933172014-07-09 11:53:10 +0200149 static struct timeval tv0;
150 AioHandler *node;
151 bool have_select_revents = false;
152 fd_set rfds, wfds;
153
154 /* fill fd sets */
155 FD_ZERO(&rfds);
156 FD_ZERO(&wfds);
157 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
158 if (node->io_read) {
159 FD_SET ((SOCKET)node->pfd.fd, &rfds);
160 }
161 if (node->io_write) {
162 FD_SET ((SOCKET)node->pfd.fd, &wfds);
163 }
164 }
165
166 if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
167 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
168 node->pfd.revents = 0;
169 if (FD_ISSET(node->pfd.fd, &rfds)) {
170 node->pfd.revents |= G_IO_IN;
171 have_select_revents = true;
172 }
173
174 if (FD_ISSET(node->pfd.fd, &wfds)) {
175 node->pfd.revents |= G_IO_OUT;
176 have_select_revents = true;
177 }
178 }
179 }
180
181 return have_select_revents;
Paolo Bonzinia3462c62014-07-09 11:53:08 +0200182}
183
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200184bool aio_pending(AioContext *ctx)
185{
186 AioHandler *node;
187
188 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
189 if (node->pfd.revents && node->io_notify) {
190 return true;
191 }
Paolo Bonzinib4933172014-07-09 11:53:10 +0200192
193 if ((node->pfd.revents & G_IO_IN) && node->io_read) {
194 return true;
195 }
196 if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
197 return true;
198 }
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200199 }
200
201 return false;
202}
203
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200204static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200205{
206 AioHandler *node;
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200207 bool progress = false;
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200208
209 /*
Paolo Bonzini87f68d32014-07-07 15:18:02 +0200210 * We have to walk very carefully in case aio_set_fd_handler is
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200211 * called while we're walking.
212 */
213 node = QLIST_FIRST(&ctx->aio_handlers);
214 while (node) {
215 AioHandler *tmp;
Paolo Bonzinib4933172014-07-09 11:53:10 +0200216 int revents = node->pfd.revents;
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200217
218 ctx->walking_handlers++;
219
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200220 if (!node->deleted &&
Paolo Bonzinib4933172014-07-09 11:53:10 +0200221 (revents || event_notifier_get_handle(node->e) == event) &&
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200222 node->io_notify) {
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200223 node->pfd.revents = 0;
224 node->io_notify(node->e);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200225
226 /* aio_notify() does not count as progress */
Stefan Hajnoczi8b2d42d2013-08-22 15:28:35 +0200227 if (node->e != &ctx->notifier) {
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200228 progress = true;
229 }
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200230 }
231
Paolo Bonzinib4933172014-07-09 11:53:10 +0200232 if (!node->deleted &&
233 (node->io_read || node->io_write)) {
234 node->pfd.revents = 0;
235 if ((revents & G_IO_IN) && node->io_read) {
236 node->io_read(node->opaque);
237 progress = true;
238 }
239 if ((revents & G_IO_OUT) && node->io_write) {
240 node->io_write(node->opaque);
241 progress = true;
242 }
243
244 /* if the next select() will return an event, we have progressed */
245 if (event == event_notifier_get_handle(&ctx->notifier)) {
246 WSANETWORKEVENTS ev;
247 WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
248 if (ev.lNetworkEvents) {
249 progress = true;
250 }
251 }
252 }
253
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200254 tmp = node;
255 node = QLIST_NEXT(node, node);
256
257 ctx->walking_handlers--;
258
259 if (!ctx->walking_handlers && tmp->deleted) {
260 QLIST_REMOVE(tmp, node);
261 g_free(tmp);
262 }
263 }
264
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200265 return progress;
266}
267
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200268bool aio_dispatch(AioContext *ctx)
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200269{
270 bool progress;
271
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200272 progress = aio_bh_poll(ctx);
273 progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
Paolo Bonzinid397ec992014-07-09 11:53:02 +0200274 progress |= timerlistgroup_run_timers(&ctx->tlg);
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200275 return progress;
276}
277
278bool aio_poll(AioContext *ctx, bool blocking)
279{
280 AioHandler *node;
281 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
Paolo Bonzinib4933172014-07-09 11:53:10 +0200282 bool was_dispatching, progress, have_select_revents, first;
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200283 int count;
284 int timeout;
285
Paolo Bonzini49110172015-02-20 17:26:51 +0100286 aio_context_acquire(ctx);
Paolo Bonzini6698c5b2014-09-12 12:08:49 +0200287 have_select_revents = aio_prepare(ctx);
288 if (have_select_revents) {
Paolo Bonzinib4933172014-07-09 11:53:10 +0200289 blocking = false;
Paolo Bonzinib4933172014-07-09 11:53:10 +0200290 }
291
Paolo Bonzini0a9dd162014-07-09 11:53:07 +0200292 was_dispatching = ctx->dispatching;
Paolo Bonzinia398dea2014-07-09 11:53:03 +0200293 progress = false;
294
Paolo Bonzini0a9dd162014-07-09 11:53:07 +0200295 /* aio_notify can avoid the expensive event_notifier_set if
296 * everything (file descriptors, bottom halves, timers) will
297 * be re-evaluated before the next blocking poll(). This is
298 * already true when aio_poll is called with blocking == false;
299 * if blocking == true, it is only true after poll() returns.
300 *
301 * If we're in a nested event loop, ctx->dispatching might be true.
302 * In that case we can restore it just before returning, but we
303 * have to clear it now.
304 */
305 aio_set_dispatching(ctx, !blocking);
306
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200307 ctx->walking_handlers++;
308
309 /* fill fd sets */
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200310 count = 0;
311 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200312 if (!node->deleted && node->io_notify) {
313 events[count++] = event_notifier_get_handle(node->e);
314 }
315 }
316
317 ctx->walking_handlers--;
Paolo Bonzini3672fa52014-07-09 11:53:04 +0200318 first = true;
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200319
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200320 /* wait until next event */
Paolo Bonzinib022b4a2012-11-23 15:59:43 +0100321 while (count > 0) {
Paolo Bonzinib4933172014-07-09 11:53:10 +0200322 HANDLE event;
Alex Bligh438e1f42013-08-21 16:02:53 +0100323 int ret;
324
Paolo Bonzini845ca102014-07-09 11:53:01 +0200325 timeout = blocking
326 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
Paolo Bonzini49110172015-02-20 17:26:51 +0100327 if (timeout) {
328 aio_context_release(ctx);
329 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100330 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
Paolo Bonzini49110172015-02-20 17:26:51 +0100331 if (timeout) {
332 aio_context_acquire(ctx);
333 }
Paolo Bonzini0a9dd162014-07-09 11:53:07 +0200334 aio_set_dispatching(ctx, true);
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200335
Paolo Bonzini3672fa52014-07-09 11:53:04 +0200336 if (first && aio_bh_poll(ctx)) {
337 progress = true;
338 }
339 first = false;
340
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200341 /* if we have any signaled events, dispatch event */
Paolo Bonzinib4933172014-07-09 11:53:10 +0200342 event = NULL;
343 if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
344 event = events[ret - WAIT_OBJECT_0];
Paolo Bonzinia90d4112014-09-15 14:52:58 +0200345 events[ret - WAIT_OBJECT_0] = events[--count];
Paolo Bonzinib4933172014-07-09 11:53:10 +0200346 } else if (!have_select_revents) {
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200347 break;
348 }
349
Paolo Bonzinib4933172014-07-09 11:53:10 +0200350 have_select_revents = false;
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200351 blocking = false;
352
Paolo Bonzinib4933172014-07-09 11:53:10 +0200353 progress |= aio_dispatch_handlers(ctx, event);
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200354 }
355
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200356 progress |= timerlistgroup_run_timers(&ctx->tlg);
Alex Bligh438e1f42013-08-21 16:02:53 +0100357
Paolo Bonzini0a9dd162014-07-09 11:53:07 +0200358 aio_set_dispatching(ctx, was_dispatching);
Paolo Bonzini49110172015-02-20 17:26:51 +0100359 aio_context_release(ctx);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200360 return progress;
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200361}