]> git.proxmox.com Git - mirror_qemu.git/blame - aio-posix.c
Drop duplicate display option documentation
[mirror_qemu.git] / aio-posix.c
CommitLineData
a76bab49
AL
1/*
2 * QEMU aio implementation
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
a76bab49
AL
14 */
15
d38ea87a 16#include "qemu/osdep.h"
a76bab49 17#include "qemu-common.h"
737e150e 18#include "block/block.h"
2bbf11d7 19#include "qemu/rcu_queue.h"
1de7afc9 20#include "qemu/sockets.h"
4a1cba38
SH
21#include "qemu/cutils.h"
22#include "trace.h"
147dfab7 23#ifdef CONFIG_EPOLL_CREATE1
fbe3fc5c
FZ
24#include <sys/epoll.h>
25#endif
a76bab49 26
a76bab49
AL
27struct AioHandler
28{
cd9ba1eb 29 GPollFD pfd;
a76bab49
AL
30 IOHandler *io_read;
31 IOHandler *io_write;
4a1cba38 32 AioPollFn *io_poll;
684e508c
SH
33 IOHandler *io_poll_begin;
34 IOHandler *io_poll_end;
a76bab49
AL
35 int deleted;
36 void *opaque;
dca21ef2 37 bool is_external;
72cf2d4f 38 QLIST_ENTRY(AioHandler) node;
a76bab49
AL
39};
40
147dfab7 41#ifdef CONFIG_EPOLL_CREATE1
fbe3fc5c
FZ
42
43/* The fd number threashold to switch to epoll */
44#define EPOLL_ENABLE_THRESHOLD 64
45
46static void aio_epoll_disable(AioContext *ctx)
47{
48 ctx->epoll_available = false;
49 if (!ctx->epoll_enabled) {
50 return;
51 }
52 ctx->epoll_enabled = false;
53 close(ctx->epollfd);
54}
55
56static inline int epoll_events_from_pfd(int pfd_events)
57{
58 return (pfd_events & G_IO_IN ? EPOLLIN : 0) |
59 (pfd_events & G_IO_OUT ? EPOLLOUT : 0) |
60 (pfd_events & G_IO_HUP ? EPOLLHUP : 0) |
61 (pfd_events & G_IO_ERR ? EPOLLERR : 0);
62}
63
64static bool aio_epoll_try_enable(AioContext *ctx)
65{
66 AioHandler *node;
67 struct epoll_event event;
68
2bbf11d7 69 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
fbe3fc5c
FZ
70 int r;
71 if (node->deleted || !node->pfd.events) {
72 continue;
73 }
74 event.events = epoll_events_from_pfd(node->pfd.events);
75 event.data.ptr = node;
76 r = epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, node->pfd.fd, &event);
77 if (r) {
78 return false;
79 }
80 }
81 ctx->epoll_enabled = true;
82 return true;
83}
84
85static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
86{
87 struct epoll_event event;
88 int r;
35dd66e2 89 int ctl;
fbe3fc5c
FZ
90
91 if (!ctx->epoll_enabled) {
92 return;
93 }
94 if (!node->pfd.events) {
35dd66e2 95 ctl = EPOLL_CTL_DEL;
fbe3fc5c
FZ
96 } else {
97 event.data.ptr = node;
98 event.events = epoll_events_from_pfd(node->pfd.events);
35dd66e2
PB
99 ctl = is_new ? EPOLL_CTL_ADD : EPOLL_CTL_MOD;
100 }
101
102 r = epoll_ctl(ctx->epollfd, ctl, node->pfd.fd, &event);
103 if (r) {
104 aio_epoll_disable(ctx);
fbe3fc5c
FZ
105 }
106}
107
108static int aio_epoll(AioContext *ctx, GPollFD *pfds,
109 unsigned npfd, int64_t timeout)
110{
111 AioHandler *node;
112 int i, ret = 0;
113 struct epoll_event events[128];
114
115 assert(npfd == 1);
116 assert(pfds[0].fd == ctx->epollfd);
117 if (timeout > 0) {
118 ret = qemu_poll_ns(pfds, npfd, timeout);
119 }
120 if (timeout <= 0 || ret > 0) {
121 ret = epoll_wait(ctx->epollfd, events,
122 sizeof(events) / sizeof(events[0]),
123 timeout);
124 if (ret <= 0) {
125 goto out;
126 }
127 for (i = 0; i < ret; i++) {
128 int ev = events[i].events;
129 node = events[i].data.ptr;
130 node->pfd.revents = (ev & EPOLLIN ? G_IO_IN : 0) |
131 (ev & EPOLLOUT ? G_IO_OUT : 0) |
132 (ev & EPOLLHUP ? G_IO_HUP : 0) |
133 (ev & EPOLLERR ? G_IO_ERR : 0);
134 }
135 }
136out:
137 return ret;
138}
139
140static bool aio_epoll_enabled(AioContext *ctx)
141{
142 /* Fall back to ppoll when external clients are disabled. */
143 return !aio_external_disabled(ctx) && ctx->epoll_enabled;
144}
145
146static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
147 unsigned npfd, int64_t timeout)
148{
149 if (!ctx->epoll_available) {
150 return false;
151 }
152 if (aio_epoll_enabled(ctx)) {
153 return true;
154 }
155 if (npfd >= EPOLL_ENABLE_THRESHOLD) {
156 if (aio_epoll_try_enable(ctx)) {
157 return true;
158 } else {
159 aio_epoll_disable(ctx);
160 }
161 }
162 return false;
163}
164
165#else
166
167static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
168{
169}
170
171static int aio_epoll(AioContext *ctx, GPollFD *pfds,
172 unsigned npfd, int64_t timeout)
173{
174 assert(false);
175}
176
177static bool aio_epoll_enabled(AioContext *ctx)
178{
179 return false;
180}
181
182static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
183 unsigned npfd, int64_t timeout)
184{
185 return false;
186}
187
188#endif
189
a915f4bc 190static AioHandler *find_aio_handler(AioContext *ctx, int fd)
a76bab49
AL
191{
192 AioHandler *node;
193
a915f4bc 194 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
cd9ba1eb 195 if (node->pfd.fd == fd)
79d5ca56
AG
196 if (!node->deleted)
197 return node;
a76bab49
AL
198 }
199
200 return NULL;
201}
202
a915f4bc
PB
203void aio_set_fd_handler(AioContext *ctx,
204 int fd,
dca21ef2 205 bool is_external,
a915f4bc
PB
206 IOHandler *io_read,
207 IOHandler *io_write,
f6a51c84 208 AioPollFn *io_poll,
a915f4bc 209 void *opaque)
a76bab49
AL
210{
211 AioHandler *node;
fbe3fc5c 212 bool is_new = false;
0ed39f3d 213 bool deleted = false;
a76bab49 214
2bbf11d7
PB
215 qemu_lockcnt_lock(&ctx->list_lock);
216
a915f4bc 217 node = find_aio_handler(ctx, fd);
a76bab49
AL
218
219 /* Are we deleting the fd handler? */
4a1cba38 220 if (!io_read && !io_write && !io_poll) {
36173ec5 221 if (node == NULL) {
2bbf11d7 222 qemu_lockcnt_unlock(&ctx->list_lock);
36173ec5
PB
223 return;
224 }
225
226 g_source_remove_poll(&ctx->source, &node->pfd);
227
228 /* If the lock is held, just mark the node as deleted */
2bbf11d7 229 if (qemu_lockcnt_count(&ctx->list_lock)) {
36173ec5
PB
230 node->deleted = 1;
231 node->pfd.revents = 0;
232 } else {
233 /* Otherwise, delete it for real. We can't just mark it as
2bbf11d7
PB
234 * deleted because deleted nodes are only cleaned up while
235 * no one is walking the handlers list.
36173ec5
PB
236 */
237 QLIST_REMOVE(node, node);
238 deleted = true;
a76bab49 239 }
4a1cba38
SH
240
241 if (!node->io_poll) {
242 ctx->poll_disable_cnt--;
243 }
a76bab49
AL
244 } else {
245 if (node == NULL) {
246 /* Alloc and insert if it's not already there */
3ba235a0 247 node = g_new0(AioHandler, 1);
cd9ba1eb 248 node->pfd.fd = fd;
2bbf11d7 249 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
e3713e00
PB
250
251 g_source_add_poll(&ctx->source, &node->pfd);
fbe3fc5c 252 is_new = true;
4a1cba38
SH
253
254 ctx->poll_disable_cnt += !io_poll;
255 } else {
256 ctx->poll_disable_cnt += !io_poll - !node->io_poll;
a76bab49 257 }
4a1cba38 258
a76bab49
AL
259 /* Update handler with latest information */
260 node->io_read = io_read;
261 node->io_write = io_write;
4a1cba38 262 node->io_poll = io_poll;
a76bab49 263 node->opaque = opaque;
dca21ef2 264 node->is_external = is_external;
cd9ba1eb 265
b5a01a70
SH
266 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
267 node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
a76bab49 268 }
7ed2b24c 269
fbe3fc5c 270 aio_epoll_update(ctx, node, is_new);
2bbf11d7 271 qemu_lockcnt_unlock(&ctx->list_lock);
7ed2b24c 272 aio_notify(ctx);
4a1cba38 273
0ed39f3d
FZ
274 if (deleted) {
275 g_free(node);
276 }
9958c351
PB
277}
278
684e508c
SH
279void aio_set_fd_poll(AioContext *ctx, int fd,
280 IOHandler *io_poll_begin,
281 IOHandler *io_poll_end)
282{
283 AioHandler *node = find_aio_handler(ctx, fd);
284
285 if (!node) {
286 return;
287 }
288
289 node->io_poll_begin = io_poll_begin;
290 node->io_poll_end = io_poll_end;
291}
292
a915f4bc
PB
293void aio_set_event_notifier(AioContext *ctx,
294 EventNotifier *notifier,
dca21ef2 295 bool is_external,
f6a51c84
SH
296 EventNotifierHandler *io_read,
297 AioPollFn *io_poll)
a76bab49 298{
f6a51c84
SH
299 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), is_external,
300 (IOHandler *)io_read, NULL, io_poll, notifier);
a76bab49
AL
301}
302
684e508c
SH
303void aio_set_event_notifier_poll(AioContext *ctx,
304 EventNotifier *notifier,
305 EventNotifierHandler *io_poll_begin,
306 EventNotifierHandler *io_poll_end)
307{
308 aio_set_fd_poll(ctx, event_notifier_get_fd(notifier),
309 (IOHandler *)io_poll_begin,
310 (IOHandler *)io_poll_end);
311}
312
313static void poll_set_started(AioContext *ctx, bool started)
314{
315 AioHandler *node;
316
317 if (started == ctx->poll_started) {
318 return;
319 }
320
321 ctx->poll_started = started;
322
2bbf11d7
PB
323 qemu_lockcnt_inc(&ctx->list_lock);
324 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
684e508c
SH
325 IOHandler *fn;
326
327 if (node->deleted) {
328 continue;
329 }
330
331 if (started) {
332 fn = node->io_poll_begin;
333 } else {
334 fn = node->io_poll_end;
335 }
336
337 if (fn) {
338 fn(node->opaque);
339 }
340 }
2bbf11d7 341 qemu_lockcnt_dec(&ctx->list_lock);
684e508c
SH
342}
343
344
a3462c65
PB
345bool aio_prepare(AioContext *ctx)
346{
684e508c
SH
347 /* Poll mode cannot be used with glib's event loop, disable it. */
348 poll_set_started(ctx, false);
349
a3462c65
PB
350 return false;
351}
352
cd9ba1eb
PB
353bool aio_pending(AioContext *ctx)
354{
355 AioHandler *node;
2bbf11d7 356 bool result = false;
cd9ba1eb 357
2bbf11d7
PB
358 /*
359 * We have to walk very carefully in case aio_set_fd_handler is
360 * called while we're walking.
361 */
362 qemu_lockcnt_inc(&ctx->list_lock);
363
364 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
cd9ba1eb
PB
365 int revents;
366
cd9ba1eb 367 revents = node->pfd.revents & node->pfd.events;
37989ced
FZ
368 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read &&
369 aio_node_check(ctx, node->is_external)) {
2bbf11d7
PB
370 result = true;
371 break;
cd9ba1eb 372 }
37989ced
FZ
373 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write &&
374 aio_node_check(ctx, node->is_external)) {
2bbf11d7
PB
375 result = true;
376 break;
cd9ba1eb
PB
377 }
378 }
2bbf11d7 379 qemu_lockcnt_dec(&ctx->list_lock);
cd9ba1eb 380
2bbf11d7 381 return result;
cd9ba1eb
PB
382}
383
56d2c3c6 384static bool aio_dispatch_handlers(AioContext *ctx)
a76bab49 385{
abf90d39 386 AioHandler *node, *tmp;
d0c8d2c0 387 bool progress = false;
7c0628b2 388
cd9ba1eb 389 /*
87f68d31 390 * We have to walk very carefully in case aio_set_fd_handler is
cd9ba1eb
PB
391 * called while we're walking.
392 */
2bbf11d7 393 qemu_lockcnt_inc(&ctx->list_lock);
cd9ba1eb 394
2bbf11d7 395 QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
abf90d39 396 int revents;
cd9ba1eb
PB
397
398 revents = node->pfd.revents & node->pfd.events;
399 node->pfd.revents = 0;
400
d0c8d2c0
SH
401 if (!node->deleted &&
402 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
37989ced 403 aio_node_check(ctx, node->is_external) &&
d0c8d2c0 404 node->io_read) {
cd9ba1eb 405 node->io_read(node->opaque);
164a101f
SH
406
407 /* aio_notify() does not count as progress */
408 if (node->opaque != &ctx->notifier) {
409 progress = true;
410 }
cd9ba1eb 411 }
d0c8d2c0
SH
412 if (!node->deleted &&
413 (revents & (G_IO_OUT | G_IO_ERR)) &&
37989ced 414 aio_node_check(ctx, node->is_external) &&
d0c8d2c0 415 node->io_write) {
cd9ba1eb
PB
416 node->io_write(node->opaque);
417 progress = true;
418 }
419
abf90d39 420 if (node->deleted) {
2bbf11d7 421 if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
abf90d39
PB
422 QLIST_REMOVE(node, node);
423 g_free(node);
2bbf11d7 424 qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
abf90d39 425 }
cd9ba1eb
PB
426 }
427 }
438e1f47 428
2bbf11d7 429 qemu_lockcnt_dec(&ctx->list_lock);
56d2c3c6
PB
430 return progress;
431}
432
433/*
434 * Note that dispatch_fds == false has the side-effect of post-poning the
435 * freeing of deleted handlers.
436 */
437bool aio_dispatch(AioContext *ctx, bool dispatch_fds)
438{
439 bool progress;
440
441 /*
442 * If there are callbacks left that have been queued, we need to call them.
443 * Do not call select in this case, because it is possible that the caller
444 * does not need a complete flush (as is the case for aio_poll loops).
445 */
446 progress = aio_bh_poll(ctx);
447
448 if (dispatch_fds) {
449 progress |= aio_dispatch_handlers(ctx);
450 }
451
438e1f47
AB
452 /* Run our timers */
453 progress |= timerlistgroup_run_timers(&ctx->tlg);
454
d0c8d2c0
SH
455 return progress;
456}
457
e98ab097
PB
458/* These thread-local variables are used only in a small part of aio_poll
459 * around the call to the poll() system call. In particular they are not
460 * used while aio_poll is performing callbacks, which makes it much easier
461 * to think about reentrancy!
462 *
463 * Stack-allocated arrays would be perfect but they have size limitations;
464 * heap allocation is expensive enough that we want to reuse arrays across
465 * calls to aio_poll(). And because poll() has to be called without holding
466 * any lock, the arrays cannot be stored in AioContext. Thread-local data
467 * has none of the disadvantages of these three options.
468 */
469static __thread GPollFD *pollfds;
470static __thread AioHandler **nodes;
471static __thread unsigned npfd, nalloc;
472static __thread Notifier pollfds_cleanup_notifier;
473
474static void pollfds_cleanup(Notifier *n, void *unused)
475{
476 g_assert(npfd == 0);
477 g_free(pollfds);
478 g_free(nodes);
479 nalloc = 0;
480}
481
482static void add_pollfd(AioHandler *node)
483{
484 if (npfd == nalloc) {
485 if (nalloc == 0) {
486 pollfds_cleanup_notifier.notify = pollfds_cleanup;
487 qemu_thread_atexit_add(&pollfds_cleanup_notifier);
488 nalloc = 8;
489 } else {
490 g_assert(nalloc <= INT_MAX);
491 nalloc *= 2;
492 }
493 pollfds = g_renew(GPollFD, pollfds, nalloc);
494 nodes = g_renew(AioHandler *, nodes, nalloc);
495 }
496 nodes[npfd] = node;
497 pollfds[npfd] = (GPollFD) {
498 .fd = node->pfd.fd,
499 .events = node->pfd.events,
500 };
501 npfd++;
502}
503
684e508c
SH
504static bool run_poll_handlers_once(AioContext *ctx)
505{
506 bool progress = false;
507 AioHandler *node;
508
2bbf11d7 509 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
684e508c
SH
510 if (!node->deleted && node->io_poll &&
511 node->io_poll(node->opaque)) {
512 progress = true;
513 }
514
515 /* Caller handles freeing deleted nodes. Don't do it here. */
516 }
517
518 return progress;
519}
520
4a1cba38
SH
521/* run_poll_handlers:
522 * @ctx: the AioContext
523 * @max_ns: maximum time to poll for, in nanoseconds
524 *
525 * Polls for a given time.
526 *
527 * Note that ctx->notify_me must be non-zero so this function can detect
528 * aio_notify().
529 *
2bbf11d7 530 * Note that the caller must have incremented ctx->list_lock.
4a1cba38
SH
531 *
532 * Returns: true if progress was made, false otherwise
533 */
534static bool run_poll_handlers(AioContext *ctx, int64_t max_ns)
535{
684e508c 536 bool progress;
4a1cba38
SH
537 int64_t end_time;
538
539 assert(ctx->notify_me);
2bbf11d7 540 assert(qemu_lockcnt_count(&ctx->list_lock) > 0);
4a1cba38
SH
541 assert(ctx->poll_disable_cnt == 0);
542
543 trace_run_poll_handlers_begin(ctx, max_ns);
544
545 end_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + max_ns;
546
547 do {
684e508c 548 progress = run_poll_handlers_once(ctx);
4a1cba38
SH
549 } while (!progress && qemu_clock_get_ns(QEMU_CLOCK_REALTIME) < end_time);
550
551 trace_run_poll_handlers_end(ctx, progress);
552
553 return progress;
554}
555
556/* try_poll_mode:
557 * @ctx: the AioContext
684e508c 558 * @blocking: busy polling is only attempted when blocking is true
4a1cba38 559 *
684e508c 560 * ctx->notify_me must be non-zero so this function can detect aio_notify().
4a1cba38 561 *
2bbf11d7 562 * Note that the caller must have incremented ctx->list_lock.
4a1cba38
SH
563 *
564 * Returns: true if progress was made, false otherwise
565 */
566static bool try_poll_mode(AioContext *ctx, bool blocking)
567{
568 if (blocking && ctx->poll_max_ns && ctx->poll_disable_cnt == 0) {
569 /* See qemu_soonest_timeout() uint64_t hack */
570 int64_t max_ns = MIN((uint64_t)aio_compute_timeout(ctx),
82a41186 571 (uint64_t)ctx->poll_ns);
4a1cba38
SH
572
573 if (max_ns) {
684e508c
SH
574 poll_set_started(ctx, true);
575
4a1cba38
SH
576 if (run_poll_handlers(ctx, max_ns)) {
577 return true;
578 }
579 }
580 }
581
684e508c
SH
582 poll_set_started(ctx, false);
583
584 /* Even if we don't run busy polling, try polling once in case it can make
585 * progress and the caller will be able to avoid ppoll(2)/epoll_wait(2).
586 */
587 return run_poll_handlers_once(ctx);
4a1cba38
SH
588}
589
d0c8d2c0
SH
590bool aio_poll(AioContext *ctx, bool blocking)
591{
d0c8d2c0 592 AioHandler *node;
4a1cba38
SH
593 int i;
594 int ret = 0;
164a101f 595 bool progress;
e98ab097 596 int64_t timeout;
82a41186 597 int64_t start = 0;
d0c8d2c0 598
49110174 599 aio_context_acquire(ctx);
d0c8d2c0
SH
600 progress = false;
601
0ceb849b
PB
602 /* aio_notify can avoid the expensive event_notifier_set if
603 * everything (file descriptors, bottom halves, timers) will
e4c7e2d1
PB
604 * be re-evaluated before the next blocking poll(). This is
605 * already true when aio_poll is called with blocking == false;
eabc9779
PB
606 * if blocking == true, it is only true after poll() returns,
607 * so disable the optimization now.
0ceb849b 608 */
eabc9779
PB
609 if (blocking) {
610 atomic_add(&ctx->notify_me, 2);
611 }
0ceb849b 612
2bbf11d7 613 qemu_lockcnt_inc(&ctx->list_lock);
a76bab49 614
82a41186
SH
615 if (ctx->poll_max_ns) {
616 start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
617 }
618
4a1cba38
SH
619 if (try_poll_mode(ctx, blocking)) {
620 progress = true;
621 } else {
622 assert(npfd == 0);
a76bab49 623
4a1cba38 624 /* fill pollfds */
6b942468 625
4a1cba38 626 if (!aio_epoll_enabled(ctx)) {
2bbf11d7 627 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
4a1cba38
SH
628 if (!node->deleted && node->pfd.events
629 && aio_node_check(ctx, node->is_external)) {
630 add_pollfd(node);
631 }
6b942468 632 }
9eb0bfca 633 }
a76bab49 634
4a1cba38 635 timeout = blocking ? aio_compute_timeout(ctx) : 0;
a76bab49 636
4a1cba38
SH
637 /* wait until next event */
638 if (timeout) {
639 aio_context_release(ctx);
640 }
641 if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) {
642 AioHandler epoll_handler;
643
644 epoll_handler.pfd.fd = ctx->epollfd;
645 epoll_handler.pfd.events = G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR;
646 npfd = 0;
647 add_pollfd(&epoll_handler);
648 ret = aio_epoll(ctx, pollfds, npfd, timeout);
649 } else {
650 ret = qemu_poll_ns(pollfds, npfd, timeout);
651 }
652 if (timeout) {
653 aio_context_acquire(ctx);
654 }
fbe3fc5c 655 }
4a1cba38 656
eabc9779
PB
657 if (blocking) {
658 atomic_sub(&ctx->notify_me, 2);
659 }
9eb0bfca 660
82a41186
SH
661 /* Adjust polling time */
662 if (ctx->poll_max_ns) {
663 int64_t block_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start;
664
665 if (block_ns <= ctx->poll_ns) {
666 /* This is the sweet spot, no adjustment needed */
667 } else if (block_ns > ctx->poll_max_ns) {
668 /* We'd have to poll for too long, poll less */
669 int64_t old = ctx->poll_ns;
670
671 if (ctx->poll_shrink) {
672 ctx->poll_ns /= ctx->poll_shrink;
673 } else {
674 ctx->poll_ns = 0;
675 }
676
677 trace_poll_shrink(ctx, old, ctx->poll_ns);
678 } else if (ctx->poll_ns < ctx->poll_max_ns &&
679 block_ns < ctx->poll_max_ns) {
680 /* There is room to grow, poll longer */
681 int64_t old = ctx->poll_ns;
682 int64_t grow = ctx->poll_grow;
683
684 if (grow == 0) {
685 grow = 2;
686 }
687
688 if (ctx->poll_ns) {
689 ctx->poll_ns *= grow;
690 } else {
691 ctx->poll_ns = 4000; /* start polling at 4 microseconds */
692 }
693
694 if (ctx->poll_ns > ctx->poll_max_ns) {
695 ctx->poll_ns = ctx->poll_max_ns;
696 }
697
698 trace_poll_grow(ctx, old, ctx->poll_ns);
699 }
700 }
701
05e514b1 702 aio_notify_accept(ctx);
21a03d17 703
9eb0bfca
PB
704 /* if we have any readable fds, dispatch event */
705 if (ret > 0) {
e98ab097
PB
706 for (i = 0; i < npfd; i++) {
707 nodes[i]->pfd.revents = pollfds[i].revents;
a76bab49 708 }
438e1f47
AB
709 }
710
e98ab097 711 npfd = 0;
2bbf11d7 712 qemu_lockcnt_dec(&ctx->list_lock);
e98ab097 713
438e1f47 714 /* Run dispatch even if there were no readable fds to run timers */
721671ad 715 if (aio_dispatch(ctx, ret > 0)) {
438e1f47 716 progress = true;
9eb0bfca 717 }
bcdc1857 718
49110174
PB
719 aio_context_release(ctx);
720
164a101f 721 return progress;
a76bab49 722}
37fcee5d 723
7e003465 724void aio_context_setup(AioContext *ctx)
37fcee5d 725{
4a1cba38
SH
726 /* TODO remove this in final patch submission */
727 if (getenv("QEMU_AIO_POLL_MAX_NS")) {
728 fprintf(stderr, "The QEMU_AIO_POLL_MAX_NS environment variable has "
729 "been replaced with -object iothread,poll-max-ns=NUM\n");
730 exit(1);
731 }
732
147dfab7 733#ifdef CONFIG_EPOLL_CREATE1
fbe3fc5c
FZ
734 assert(!ctx->epollfd);
735 ctx->epollfd = epoll_create1(EPOLL_CLOEXEC);
736 if (ctx->epollfd == -1) {
7e003465 737 fprintf(stderr, "Failed to create epoll instance: %s", strerror(errno));
fbe3fc5c
FZ
738 ctx->epoll_available = false;
739 } else {
740 ctx->epoll_available = true;
741 }
742#endif
37fcee5d 743}
4a1cba38 744
82a41186
SH
745void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
746 int64_t grow, int64_t shrink, Error **errp)
4a1cba38 747{
82a41186
SH
748 /* No thread synchronization here, it doesn't matter if an incorrect value
749 * is used once.
4a1cba38
SH
750 */
751 ctx->poll_max_ns = max_ns;
82a41186
SH
752 ctx->poll_ns = 0;
753 ctx->poll_grow = grow;
754 ctx->poll_shrink = shrink;
4a1cba38
SH
755
756 aio_notify(ctx);
757}