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/queue.h"
22 #include "qemu/sockets.h"
23 #include "qapi/error.h"
24 #include "qemu/rcu_queue.h"
25 #include "qemu/error-report.h"
31 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
,
71 IOHandler
*io_poll_ready
,
75 AioHandler
*node
= NULL
;
78 if (!fd_is_socket(fd
)) {
79 error_report("fd=%d is not a socket, AIO implementation is missing", fd
);
83 s
= _get_osfhandle(fd
);
85 qemu_lockcnt_lock(&ctx
->list_lock
);
86 QLIST_FOREACH(old_node
, &ctx
->aio_handlers
, node
) {
87 if (old_node
->pfd
.fd
== s
&& !old_node
->deleted
) {
92 if (io_read
|| io_write
) {
96 /* Alloc and insert if it's not already there */
97 node
= g_new0(AioHandler
, 1);
100 node
->pfd
.events
= 0;
102 node
->pfd
.events
|= G_IO_IN
;
104 if (node
->io_write
) {
105 node
->pfd
.events
|= G_IO_OUT
;
108 node
->e
= &ctx
->notifier
;
110 /* Update handler with latest information */
111 node
->opaque
= opaque
;
112 node
->io_read
= io_read
;
113 node
->io_write
= io_write
;
114 node
->is_external
= is_external
;
117 bitmask
|= FD_READ
| FD_ACCEPT
| FD_CLOSE
;
121 bitmask
|= FD_WRITE
| FD_CONNECT
;
124 QLIST_INSERT_HEAD_RCU(&ctx
->aio_handlers
, node
, node
);
125 event
= event_notifier_get_handle(&ctx
->notifier
);
126 qemu_socket_select(fd
, event
, bitmask
, NULL
);
129 aio_remove_fd_handler(ctx
, old_node
);
132 qemu_lockcnt_unlock(&ctx
->list_lock
);
136 void aio_set_event_notifier(AioContext
*ctx
,
139 EventNotifierHandler
*io_notify
,
141 EventNotifierHandler
*io_poll_ready
)
145 qemu_lockcnt_lock(&ctx
->list_lock
);
146 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
147 if (node
->e
== e
&& !node
->deleted
) {
152 /* Are we deleting the fd handler? */
155 aio_remove_fd_handler(ctx
, node
);
159 /* Alloc and insert if it's not already there */
160 node
= g_new0(AioHandler
, 1);
162 node
->pfd
.fd
= (uintptr_t)event_notifier_get_handle(e
);
163 node
->pfd
.events
= G_IO_IN
;
164 node
->is_external
= is_external
;
165 QLIST_INSERT_HEAD_RCU(&ctx
->aio_handlers
, node
, node
);
167 g_source_add_poll(&ctx
->source
, &node
->pfd
);
169 /* Update handler with latest information */
170 node
->io_notify
= io_notify
;
173 qemu_lockcnt_unlock(&ctx
->list_lock
);
177 void aio_set_event_notifier_poll(AioContext
*ctx
,
178 EventNotifier
*notifier
,
179 EventNotifierHandler
*io_poll_begin
,
180 EventNotifierHandler
*io_poll_end
)
182 /* Not implemented */
185 bool aio_prepare(AioContext
*ctx
)
187 static struct timeval tv0
;
189 bool have_select_revents
= false;
193 * We have to walk very carefully in case aio_set_fd_handler is
194 * called while we're walking.
196 qemu_lockcnt_inc(&ctx
->list_lock
);
201 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
203 FD_SET ((SOCKET
)node
->pfd
.fd
, &rfds
);
205 if (node
->io_write
) {
206 FD_SET ((SOCKET
)node
->pfd
.fd
, &wfds
);
210 if (select(0, &rfds
, &wfds
, NULL
, &tv0
) > 0) {
211 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
212 node
->pfd
.revents
= 0;
213 if (FD_ISSET(node
->pfd
.fd
, &rfds
)) {
214 node
->pfd
.revents
|= G_IO_IN
;
215 have_select_revents
= true;
218 if (FD_ISSET(node
->pfd
.fd
, &wfds
)) {
219 node
->pfd
.revents
|= G_IO_OUT
;
220 have_select_revents
= true;
225 qemu_lockcnt_dec(&ctx
->list_lock
);
226 return have_select_revents
;
229 bool aio_pending(AioContext
*ctx
)
235 * We have to walk very carefully in case aio_set_fd_handler is
236 * called while we're walking.
238 qemu_lockcnt_inc(&ctx
->list_lock
);
239 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
240 if (node
->pfd
.revents
&& node
->io_notify
) {
245 if ((node
->pfd
.revents
& G_IO_IN
) && node
->io_read
) {
249 if ((node
->pfd
.revents
& G_IO_OUT
) && node
->io_write
) {
255 qemu_lockcnt_dec(&ctx
->list_lock
);
259 static bool aio_dispatch_handlers(AioContext
*ctx
, HANDLE event
)
262 bool progress
= false;
266 * We have to walk very carefully in case aio_set_fd_handler is
267 * called while we're walking.
269 QLIST_FOREACH_SAFE_RCU(node
, &ctx
->aio_handlers
, node
, tmp
) {
270 int revents
= node
->pfd
.revents
;
272 if (!node
->deleted
&&
273 (revents
|| event_notifier_get_handle(node
->e
) == event
) &&
275 node
->pfd
.revents
= 0;
276 node
->io_notify(node
->e
);
278 /* aio_notify() does not count as progress */
279 if (node
->e
!= &ctx
->notifier
) {
284 if (!node
->deleted
&&
285 (node
->io_read
|| node
->io_write
)) {
286 node
->pfd
.revents
= 0;
287 if ((revents
& G_IO_IN
) && node
->io_read
) {
288 node
->io_read(node
->opaque
);
291 if ((revents
& G_IO_OUT
) && node
->io_write
) {
292 node
->io_write(node
->opaque
);
296 /* if the next select() will return an event, we have progressed */
297 if (event
== event_notifier_get_handle(&ctx
->notifier
)) {
299 WSAEnumNetworkEvents(node
->pfd
.fd
, event
, &ev
);
300 if (ev
.lNetworkEvents
) {
307 if (qemu_lockcnt_dec_if_lock(&ctx
->list_lock
)) {
308 QLIST_REMOVE(node
, node
);
310 qemu_lockcnt_inc_and_unlock(&ctx
->list_lock
);
318 void aio_dispatch(AioContext
*ctx
)
320 qemu_lockcnt_inc(&ctx
->list_lock
);
322 aio_dispatch_handlers(ctx
, INVALID_HANDLE_VALUE
);
323 qemu_lockcnt_dec(&ctx
->list_lock
);
324 timerlistgroup_run_timers(&ctx
->tlg
);
327 bool aio_poll(AioContext
*ctx
, bool blocking
)
330 HANDLE events
[MAXIMUM_WAIT_OBJECTS
];
331 bool progress
, have_select_revents
, first
;
336 * There cannot be two concurrent aio_poll calls for the same AioContext (or
337 * an aio_poll concurrent with a GSource prepare/check/dispatch callback).
338 * We rely on this below to avoid slow locked accesses to ctx->notify_me.
340 * aio_poll() may only be called in the AioContext's thread. iohandler_ctx
341 * is special in that it runs in the main thread, but that thread's context
342 * is qemu_aio_context.
344 assert(in_aio_context_home_thread(ctx
== iohandler_get_aio_context() ?
345 qemu_get_aio_context() : ctx
));
348 /* aio_notify can avoid the expensive event_notifier_set if
349 * everything (file descriptors, bottom halves, timers) will
350 * be re-evaluated before the next blocking poll(). This is
351 * already true when aio_poll is called with blocking == false;
352 * if blocking == true, it is only true after poll() returns,
353 * so disable the optimization now.
356 qatomic_set(&ctx
->notify_me
, qatomic_read(&ctx
->notify_me
) + 2);
358 * Write ctx->notify_me before computing the timeout
359 * (reading bottom half flags, etc.). Pairs with
360 * smp_mb in aio_notify().
365 qemu_lockcnt_inc(&ctx
->list_lock
);
366 have_select_revents
= aio_prepare(ctx
);
370 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
371 if (!node
->deleted
&& node
->io_notify
372 && aio_node_check(ctx
, node
->is_external
)) {
373 assert(count
< MAXIMUM_WAIT_OBJECTS
);
374 events
[count
++] = event_notifier_get_handle(node
->e
);
380 /* ctx->notifier is always registered. */
383 /* Multiple iterations, all of them non-blocking except the first,
384 * may be necessary to process all pending events. After the first
385 * WaitForMultipleObjects call ctx->notify_me will be decremented.
391 timeout
= blocking
&& !have_select_revents
392 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx
)) : 0;
393 ret
= WaitForMultipleObjects(count
, events
, FALSE
, timeout
);
396 qatomic_store_release(&ctx
->notify_me
,
397 qatomic_read(&ctx
->notify_me
) - 2);
398 aio_notify_accept(ctx
);
402 progress
|= aio_bh_poll(ctx
);
406 /* if we have any signaled events, dispatch event */
408 if ((DWORD
) (ret
- WAIT_OBJECT_0
) < count
) {
409 event
= events
[ret
- WAIT_OBJECT_0
];
410 events
[ret
- WAIT_OBJECT_0
] = events
[--count
];
411 } else if (!have_select_revents
) {
415 have_select_revents
= false;
418 progress
|= aio_dispatch_handlers(ctx
, event
);
421 qemu_lockcnt_dec(&ctx
->list_lock
);
423 progress
|= timerlistgroup_run_timers(&ctx
->tlg
);
427 void aio_context_setup(AioContext
*ctx
)
431 void aio_context_destroy(AioContext
*ctx
)
435 void aio_context_use_g_source(AioContext
*ctx
)
439 void aio_context_set_poll_params(AioContext
*ctx
, int64_t max_ns
,
440 int64_t grow
, int64_t shrink
, Error
**errp
)
443 error_setg(errp
, "AioContext polling is not implemented on Windows");
447 void aio_context_set_aio_params(AioContext
*ctx
, int64_t max_batch
,