2 * QEMU aio implementation
4 * Copyright IBM Corp., 2008
5 * Copyright Red Hat Inc., 2012
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
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.
18 #include "qemu/osdep.h"
19 #include "block/block.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/lockcnt.h"
22 #include "qemu/queue.h"
23 #include "qemu/sockets.h"
24 #include "qapi/error.h"
25 #include "qemu/rcu_queue.h"
26 #include "qemu/error-report.h"
32 EventNotifierHandler
*io_notify
;
36 QLIST_ENTRY(AioHandler
) node
;
39 static void aio_remove_fd_handler(AioContext
*ctx
, AioHandler
*node
)
42 * If the GSource is in the process of being destroyed then
43 * g_source_remove_poll() causes an assertion failure. Skip
44 * removal in that case, because glib cleans up its state during
47 if (!g_source_is_destroyed(&ctx
->source
)) {
48 g_source_remove_poll(&ctx
->source
, &node
->pfd
);
51 /* If aio_poll is in progress, just mark the node as deleted */
52 if (qemu_lockcnt_count(&ctx
->list_lock
)) {
54 node
->pfd
.revents
= 0;
56 /* Otherwise, delete it for real. We can't just mark it as
57 * deleted because deleted nodes are only cleaned up after
58 * releasing the list_lock.
60 QLIST_REMOVE(node
, node
);
65 void aio_set_fd_handler(AioContext
*ctx
,
70 IOHandler
*io_poll_ready
,
74 AioHandler
*node
= NULL
;
77 if (!fd_is_socket(fd
)) {
78 error_report("fd=%d is not a socket, AIO implementation is missing", fd
);
82 s
= _get_osfhandle(fd
);
84 qemu_lockcnt_lock(&ctx
->list_lock
);
85 QLIST_FOREACH(old_node
, &ctx
->aio_handlers
, node
) {
86 if (old_node
->pfd
.fd
== s
&& !old_node
->deleted
) {
91 if (io_read
|| io_write
) {
95 /* Alloc and insert if it's not already there */
96 node
= g_new0(AioHandler
, 1);
101 node
->pfd
.events
|= G_IO_IN
;
103 if (node
->io_write
) {
104 node
->pfd
.events
|= G_IO_OUT
;
107 node
->e
= &ctx
->notifier
;
109 /* Update handler with latest information */
110 node
->opaque
= opaque
;
111 node
->io_read
= io_read
;
112 node
->io_write
= io_write
;
115 bitmask
|= FD_READ
| FD_ACCEPT
| FD_CLOSE
;
119 bitmask
|= FD_WRITE
| FD_CONNECT
;
122 QLIST_INSERT_HEAD_RCU(&ctx
->aio_handlers
, node
, node
);
123 event
= event_notifier_get_handle(&ctx
->notifier
);
124 qemu_socket_select(fd
, event
, bitmask
, NULL
);
127 aio_remove_fd_handler(ctx
, old_node
);
130 qemu_lockcnt_unlock(&ctx
->list_lock
);
134 void aio_set_event_notifier(AioContext
*ctx
,
136 EventNotifierHandler
*io_notify
,
138 EventNotifierHandler
*io_poll_ready
)
142 qemu_lockcnt_lock(&ctx
->list_lock
);
143 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
144 if (node
->e
== e
&& !node
->deleted
) {
149 /* Are we deleting the fd handler? */
152 aio_remove_fd_handler(ctx
, node
);
156 /* Alloc and insert if it's not already there */
157 node
= g_new0(AioHandler
, 1);
159 node
->pfd
.fd
= (uintptr_t)event_notifier_get_handle(e
);
160 node
->pfd
.events
= G_IO_IN
;
161 QLIST_INSERT_HEAD_RCU(&ctx
->aio_handlers
, node
, node
);
163 g_source_add_poll(&ctx
->source
, &node
->pfd
);
165 /* Update handler with latest information */
166 node
->io_notify
= io_notify
;
169 qemu_lockcnt_unlock(&ctx
->list_lock
);
173 void aio_set_event_notifier_poll(AioContext
*ctx
,
174 EventNotifier
*notifier
,
175 EventNotifierHandler
*io_poll_begin
,
176 EventNotifierHandler
*io_poll_end
)
178 /* Not implemented */
181 bool aio_prepare(AioContext
*ctx
)
183 static struct timeval tv0
;
185 bool have_select_revents
= false;
189 * We have to walk very carefully in case aio_set_fd_handler is
190 * called while we're walking.
192 qemu_lockcnt_inc(&ctx
->list_lock
);
197 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
199 FD_SET ((SOCKET
)node
->pfd
.fd
, &rfds
);
201 if (node
->io_write
) {
202 FD_SET ((SOCKET
)node
->pfd
.fd
, &wfds
);
206 if (select(0, &rfds
, &wfds
, NULL
, &tv0
) > 0) {
207 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
208 node
->pfd
.revents
= 0;
209 if (FD_ISSET(node
->pfd
.fd
, &rfds
)) {
210 node
->pfd
.revents
|= G_IO_IN
;
211 have_select_revents
= true;
214 if (FD_ISSET(node
->pfd
.fd
, &wfds
)) {
215 node
->pfd
.revents
|= G_IO_OUT
;
216 have_select_revents
= true;
221 qemu_lockcnt_dec(&ctx
->list_lock
);
222 return have_select_revents
;
225 bool aio_pending(AioContext
*ctx
)
231 * We have to walk very carefully in case aio_set_fd_handler is
232 * called while we're walking.
234 qemu_lockcnt_inc(&ctx
->list_lock
);
235 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
236 if (node
->pfd
.revents
&& node
->io_notify
) {
241 if ((node
->pfd
.revents
& G_IO_IN
) && node
->io_read
) {
245 if ((node
->pfd
.revents
& G_IO_OUT
) && node
->io_write
) {
251 qemu_lockcnt_dec(&ctx
->list_lock
);
255 static bool aio_dispatch_handlers(AioContext
*ctx
, HANDLE event
)
258 bool progress
= false;
262 * We have to walk very carefully in case aio_set_fd_handler is
263 * called while we're walking.
265 QLIST_FOREACH_SAFE_RCU(node
, &ctx
->aio_handlers
, node
, tmp
) {
266 int revents
= node
->pfd
.revents
;
268 if (!node
->deleted
&&
269 (revents
|| event_notifier_get_handle(node
->e
) == event
) &&
271 node
->pfd
.revents
= 0;
272 node
->io_notify(node
->e
);
274 /* aio_notify() does not count as progress */
275 if (node
->e
!= &ctx
->notifier
) {
280 if (!node
->deleted
&&
281 (node
->io_read
|| node
->io_write
)) {
282 node
->pfd
.revents
= 0;
283 if ((revents
& G_IO_IN
) && node
->io_read
) {
284 node
->io_read(node
->opaque
);
287 if ((revents
& G_IO_OUT
) && node
->io_write
) {
288 node
->io_write(node
->opaque
);
292 /* if the next select() will return an event, we have progressed */
293 if (event
== event_notifier_get_handle(&ctx
->notifier
)) {
295 WSAEnumNetworkEvents(node
->pfd
.fd
, event
, &ev
);
296 if (ev
.lNetworkEvents
) {
303 if (qemu_lockcnt_dec_if_lock(&ctx
->list_lock
)) {
304 QLIST_REMOVE(node
, node
);
306 qemu_lockcnt_inc_and_unlock(&ctx
->list_lock
);
314 void aio_dispatch(AioContext
*ctx
)
316 qemu_lockcnt_inc(&ctx
->list_lock
);
318 aio_dispatch_handlers(ctx
, INVALID_HANDLE_VALUE
);
319 qemu_lockcnt_dec(&ctx
->list_lock
);
320 timerlistgroup_run_timers(&ctx
->tlg
);
323 bool aio_poll(AioContext
*ctx
, bool blocking
)
326 HANDLE events
[MAXIMUM_WAIT_OBJECTS
];
327 bool progress
, have_select_revents
, first
;
332 * There cannot be two concurrent aio_poll calls for the same AioContext (or
333 * an aio_poll concurrent with a GSource prepare/check/dispatch callback).
334 * We rely on this below to avoid slow locked accesses to ctx->notify_me.
336 * aio_poll() may only be called in the AioContext's thread. iohandler_ctx
337 * is special in that it runs in the main thread, but that thread's context
338 * is qemu_aio_context.
340 assert(in_aio_context_home_thread(ctx
== iohandler_get_aio_context() ?
341 qemu_get_aio_context() : ctx
));
344 /* aio_notify can avoid the expensive event_notifier_set if
345 * everything (file descriptors, bottom halves, timers) will
346 * be re-evaluated before the next blocking poll(). This is
347 * already true when aio_poll is called with blocking == false;
348 * if blocking == true, it is only true after poll() returns,
349 * so disable the optimization now.
352 qatomic_set(&ctx
->notify_me
, qatomic_read(&ctx
->notify_me
) + 2);
354 * Write ctx->notify_me before computing the timeout
355 * (reading bottom half flags, etc.). Pairs with
356 * smp_mb in aio_notify().
361 qemu_lockcnt_inc(&ctx
->list_lock
);
362 have_select_revents
= aio_prepare(ctx
);
366 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
367 if (!node
->deleted
&& node
->io_notify
) {
368 assert(count
< MAXIMUM_WAIT_OBJECTS
);
369 events
[count
++] = event_notifier_get_handle(node
->e
);
375 /* ctx->notifier is always registered. */
378 /* Multiple iterations, all of them non-blocking except the first,
379 * may be necessary to process all pending events. After the first
380 * WaitForMultipleObjects call ctx->notify_me will be decremented.
386 timeout
= blocking
&& !have_select_revents
387 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx
)) : 0;
388 ret
= WaitForMultipleObjects(count
, events
, FALSE
, timeout
);
391 qatomic_store_release(&ctx
->notify_me
,
392 qatomic_read(&ctx
->notify_me
) - 2);
393 aio_notify_accept(ctx
);
397 progress
|= aio_bh_poll(ctx
);
401 /* if we have any signaled events, dispatch event */
403 if ((DWORD
) (ret
- WAIT_OBJECT_0
) < count
) {
404 event
= events
[ret
- WAIT_OBJECT_0
];
405 events
[ret
- WAIT_OBJECT_0
] = events
[--count
];
406 } else if (!have_select_revents
) {
410 have_select_revents
= false;
413 progress
|= aio_dispatch_handlers(ctx
, event
);
416 qemu_lockcnt_dec(&ctx
->list_lock
);
418 progress
|= timerlistgroup_run_timers(&ctx
->tlg
);
422 void aio_context_setup(AioContext
*ctx
)
426 void aio_context_destroy(AioContext
*ctx
)
430 void aio_context_use_g_source(AioContext
*ctx
)
434 void aio_context_set_poll_params(AioContext
*ctx
, int64_t max_ns
,
435 int64_t grow
, int64_t shrink
, Error
**errp
)
438 error_setg(errp
, "AioContext polling is not implemented on Windows");
442 void aio_context_set_aio_params(AioContext
*ctx
, int64_t max_batch
)