2
0

aio-posix.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. *
  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.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "block/block.h"
  17. #include "qemu/rcu_queue.h"
  18. #include "qemu/sockets.h"
  19. #include "qemu/cutils.h"
  20. #include "trace.h"
  21. #ifdef CONFIG_EPOLL_CREATE1
  22. #include <sys/epoll.h>
  23. #endif
  24. struct AioHandler
  25. {
  26. GPollFD pfd;
  27. IOHandler *io_read;
  28. IOHandler *io_write;
  29. AioPollFn *io_poll;
  30. IOHandler *io_poll_begin;
  31. IOHandler *io_poll_end;
  32. int deleted;
  33. void *opaque;
  34. bool is_external;
  35. QLIST_ENTRY(AioHandler) node;
  36. };
  37. #ifdef CONFIG_EPOLL_CREATE1
  38. /* The fd number threshold to switch to epoll */
  39. #define EPOLL_ENABLE_THRESHOLD 64
  40. static void aio_epoll_disable(AioContext *ctx)
  41. {
  42. ctx->epoll_enabled = false;
  43. if (!ctx->epoll_available) {
  44. return;
  45. }
  46. ctx->epoll_available = false;
  47. close(ctx->epollfd);
  48. }
  49. static inline int epoll_events_from_pfd(int pfd_events)
  50. {
  51. return (pfd_events & G_IO_IN ? EPOLLIN : 0) |
  52. (pfd_events & G_IO_OUT ? EPOLLOUT : 0) |
  53. (pfd_events & G_IO_HUP ? EPOLLHUP : 0) |
  54. (pfd_events & G_IO_ERR ? EPOLLERR : 0);
  55. }
  56. static bool aio_epoll_try_enable(AioContext *ctx)
  57. {
  58. AioHandler *node;
  59. struct epoll_event event;
  60. QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
  61. int r;
  62. if (node->deleted || !node->pfd.events) {
  63. continue;
  64. }
  65. event.events = epoll_events_from_pfd(node->pfd.events);
  66. event.data.ptr = node;
  67. r = epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, node->pfd.fd, &event);
  68. if (r) {
  69. return false;
  70. }
  71. }
  72. ctx->epoll_enabled = true;
  73. return true;
  74. }
  75. static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
  76. {
  77. struct epoll_event event;
  78. int r;
  79. int ctl;
  80. if (!ctx->epoll_enabled) {
  81. return;
  82. }
  83. if (!node->pfd.events) {
  84. ctl = EPOLL_CTL_DEL;
  85. } else {
  86. event.data.ptr = node;
  87. event.events = epoll_events_from_pfd(node->pfd.events);
  88. ctl = is_new ? EPOLL_CTL_ADD : EPOLL_CTL_MOD;
  89. }
  90. r = epoll_ctl(ctx->epollfd, ctl, node->pfd.fd, &event);
  91. if (r) {
  92. aio_epoll_disable(ctx);
  93. }
  94. }
  95. static int aio_epoll(AioContext *ctx, GPollFD *pfds,
  96. unsigned npfd, int64_t timeout)
  97. {
  98. AioHandler *node;
  99. int i, ret = 0;
  100. struct epoll_event events[128];
  101. assert(npfd == 1);
  102. assert(pfds[0].fd == ctx->epollfd);
  103. if (timeout > 0) {
  104. ret = qemu_poll_ns(pfds, npfd, timeout);
  105. }
  106. if (timeout <= 0 || ret > 0) {
  107. ret = epoll_wait(ctx->epollfd, events,
  108. ARRAY_SIZE(events),
  109. timeout);
  110. if (ret <= 0) {
  111. goto out;
  112. }
  113. for (i = 0; i < ret; i++) {
  114. int ev = events[i].events;
  115. node = events[i].data.ptr;
  116. node->pfd.revents = (ev & EPOLLIN ? G_IO_IN : 0) |
  117. (ev & EPOLLOUT ? G_IO_OUT : 0) |
  118. (ev & EPOLLHUP ? G_IO_HUP : 0) |
  119. (ev & EPOLLERR ? G_IO_ERR : 0);
  120. }
  121. }
  122. out:
  123. return ret;
  124. }
  125. static bool aio_epoll_enabled(AioContext *ctx)
  126. {
  127. /* Fall back to ppoll when external clients are disabled. */
  128. return !aio_external_disabled(ctx) && ctx->epoll_enabled;
  129. }
  130. static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
  131. unsigned npfd, int64_t timeout)
  132. {
  133. if (!ctx->epoll_available) {
  134. return false;
  135. }
  136. if (aio_epoll_enabled(ctx)) {
  137. return true;
  138. }
  139. if (npfd >= EPOLL_ENABLE_THRESHOLD) {
  140. if (aio_epoll_try_enable(ctx)) {
  141. return true;
  142. } else {
  143. aio_epoll_disable(ctx);
  144. }
  145. }
  146. return false;
  147. }
  148. #else
  149. static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
  150. {
  151. }
  152. static int aio_epoll(AioContext *ctx, GPollFD *pfds,
  153. unsigned npfd, int64_t timeout)
  154. {
  155. assert(false);
  156. }
  157. static bool aio_epoll_enabled(AioContext *ctx)
  158. {
  159. return false;
  160. }
  161. static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
  162. unsigned npfd, int64_t timeout)
  163. {
  164. return false;
  165. }
  166. #endif
  167. static AioHandler *find_aio_handler(AioContext *ctx, int fd)
  168. {
  169. AioHandler *node;
  170. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  171. if (node->pfd.fd == fd)
  172. if (!node->deleted)
  173. return node;
  174. }
  175. return NULL;
  176. }
  177. static bool aio_remove_fd_handler(AioContext *ctx, AioHandler *node)
  178. {
  179. /* If the GSource is in the process of being destroyed then
  180. * g_source_remove_poll() causes an assertion failure. Skip
  181. * removal in that case, because glib cleans up its state during
  182. * destruction anyway.
  183. */
  184. if (!g_source_is_destroyed(&ctx->source)) {
  185. g_source_remove_poll(&ctx->source, &node->pfd);
  186. }
  187. /* If a read is in progress, just mark the node as deleted */
  188. if (qemu_lockcnt_count(&ctx->list_lock)) {
  189. node->deleted = 1;
  190. node->pfd.revents = 0;
  191. return false;
  192. }
  193. /* Otherwise, delete it for real. We can't just mark it as
  194. * deleted because deleted nodes are only cleaned up while
  195. * no one is walking the handlers list.
  196. */
  197. QLIST_REMOVE(node, node);
  198. return true;
  199. }
  200. void aio_set_fd_handler(AioContext *ctx,
  201. int fd,
  202. bool is_external,
  203. IOHandler *io_read,
  204. IOHandler *io_write,
  205. AioPollFn *io_poll,
  206. void *opaque)
  207. {
  208. AioHandler *node;
  209. AioHandler *new_node = NULL;
  210. bool is_new = false;
  211. bool deleted = false;
  212. int poll_disable_change;
  213. qemu_lockcnt_lock(&ctx->list_lock);
  214. node = find_aio_handler(ctx, fd);
  215. /* Are we deleting the fd handler? */
  216. if (!io_read && !io_write && !io_poll) {
  217. if (node == NULL) {
  218. qemu_lockcnt_unlock(&ctx->list_lock);
  219. return;
  220. }
  221. /* Clean events in order to unregister fd from the ctx epoll. */
  222. node->pfd.events = 0;
  223. poll_disable_change = -!node->io_poll;
  224. } else {
  225. poll_disable_change = !io_poll - (node && !node->io_poll);
  226. if (node == NULL) {
  227. is_new = true;
  228. }
  229. /* Alloc and insert if it's not already there */
  230. new_node = g_new0(AioHandler, 1);
  231. /* Update handler with latest information */
  232. new_node->io_read = io_read;
  233. new_node->io_write = io_write;
  234. new_node->io_poll = io_poll;
  235. new_node->opaque = opaque;
  236. new_node->is_external = is_external;
  237. if (is_new) {
  238. new_node->pfd.fd = fd;
  239. } else {
  240. new_node->pfd = node->pfd;
  241. }
  242. g_source_add_poll(&ctx->source, &new_node->pfd);
  243. new_node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
  244. new_node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
  245. QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, new_node, node);
  246. }
  247. if (node) {
  248. deleted = aio_remove_fd_handler(ctx, node);
  249. }
  250. /* No need to order poll_disable_cnt writes against other updates;
  251. * the counter is only used to avoid wasting time and latency on
  252. * iterated polling when the system call will be ultimately necessary.
  253. * Changing handlers is a rare event, and a little wasted polling until
  254. * the aio_notify below is not an issue.
  255. */
  256. atomic_set(&ctx->poll_disable_cnt,
  257. atomic_read(&ctx->poll_disable_cnt) + poll_disable_change);
  258. if (new_node) {
  259. aio_epoll_update(ctx, new_node, is_new);
  260. } else if (node) {
  261. /* Unregister deleted fd_handler */
  262. aio_epoll_update(ctx, node, false);
  263. }
  264. qemu_lockcnt_unlock(&ctx->list_lock);
  265. aio_notify(ctx);
  266. if (deleted) {
  267. g_free(node);
  268. }
  269. }
  270. void aio_set_fd_poll(AioContext *ctx, int fd,
  271. IOHandler *io_poll_begin,
  272. IOHandler *io_poll_end)
  273. {
  274. AioHandler *node = find_aio_handler(ctx, fd);
  275. if (!node) {
  276. return;
  277. }
  278. node->io_poll_begin = io_poll_begin;
  279. node->io_poll_end = io_poll_end;
  280. }
  281. void aio_set_event_notifier(AioContext *ctx,
  282. EventNotifier *notifier,
  283. bool is_external,
  284. EventNotifierHandler *io_read,
  285. AioPollFn *io_poll)
  286. {
  287. aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), is_external,
  288. (IOHandler *)io_read, NULL, io_poll, notifier);
  289. }
  290. void aio_set_event_notifier_poll(AioContext *ctx,
  291. EventNotifier *notifier,
  292. EventNotifierHandler *io_poll_begin,
  293. EventNotifierHandler *io_poll_end)
  294. {
  295. aio_set_fd_poll(ctx, event_notifier_get_fd(notifier),
  296. (IOHandler *)io_poll_begin,
  297. (IOHandler *)io_poll_end);
  298. }
  299. static void poll_set_started(AioContext *ctx, bool started)
  300. {
  301. AioHandler *node;
  302. if (started == ctx->poll_started) {
  303. return;
  304. }
  305. ctx->poll_started = started;
  306. qemu_lockcnt_inc(&ctx->list_lock);
  307. QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
  308. IOHandler *fn;
  309. if (node->deleted) {
  310. continue;
  311. }
  312. if (started) {
  313. fn = node->io_poll_begin;
  314. } else {
  315. fn = node->io_poll_end;
  316. }
  317. if (fn) {
  318. fn(node->opaque);
  319. }
  320. }
  321. qemu_lockcnt_dec(&ctx->list_lock);
  322. }
  323. bool aio_prepare(AioContext *ctx)
  324. {
  325. /* Poll mode cannot be used with glib's event loop, disable it. */
  326. poll_set_started(ctx, false);
  327. return false;
  328. }
  329. bool aio_pending(AioContext *ctx)
  330. {
  331. AioHandler *node;
  332. bool result = false;
  333. /*
  334. * We have to walk very carefully in case aio_set_fd_handler is
  335. * called while we're walking.
  336. */
  337. qemu_lockcnt_inc(&ctx->list_lock);
  338. QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
  339. int revents;
  340. revents = node->pfd.revents & node->pfd.events;
  341. if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read &&
  342. aio_node_check(ctx, node->is_external)) {
  343. result = true;
  344. break;
  345. }
  346. if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write &&
  347. aio_node_check(ctx, node->is_external)) {
  348. result = true;
  349. break;
  350. }
  351. }
  352. qemu_lockcnt_dec(&ctx->list_lock);
  353. return result;
  354. }
  355. static bool aio_dispatch_handlers(AioContext *ctx)
  356. {
  357. AioHandler *node, *tmp;
  358. bool progress = false;
  359. QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
  360. int revents;
  361. revents = node->pfd.revents & node->pfd.events;
  362. node->pfd.revents = 0;
  363. if (!node->deleted &&
  364. (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
  365. aio_node_check(ctx, node->is_external) &&
  366. node->io_read) {
  367. node->io_read(node->opaque);
  368. /* aio_notify() does not count as progress */
  369. if (node->opaque != &ctx->notifier) {
  370. progress = true;
  371. }
  372. }
  373. if (!node->deleted &&
  374. (revents & (G_IO_OUT | G_IO_ERR)) &&
  375. aio_node_check(ctx, node->is_external) &&
  376. node->io_write) {
  377. node->io_write(node->opaque);
  378. progress = true;
  379. }
  380. if (node->deleted) {
  381. if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
  382. QLIST_REMOVE(node, node);
  383. g_free(node);
  384. qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
  385. }
  386. }
  387. }
  388. return progress;
  389. }
  390. void aio_dispatch(AioContext *ctx)
  391. {
  392. qemu_lockcnt_inc(&ctx->list_lock);
  393. aio_bh_poll(ctx);
  394. aio_dispatch_handlers(ctx);
  395. qemu_lockcnt_dec(&ctx->list_lock);
  396. timerlistgroup_run_timers(&ctx->tlg);
  397. }
  398. /* These thread-local variables are used only in a small part of aio_poll
  399. * around the call to the poll() system call. In particular they are not
  400. * used while aio_poll is performing callbacks, which makes it much easier
  401. * to think about reentrancy!
  402. *
  403. * Stack-allocated arrays would be perfect but they have size limitations;
  404. * heap allocation is expensive enough that we want to reuse arrays across
  405. * calls to aio_poll(). And because poll() has to be called without holding
  406. * any lock, the arrays cannot be stored in AioContext. Thread-local data
  407. * has none of the disadvantages of these three options.
  408. */
  409. static __thread GPollFD *pollfds;
  410. static __thread AioHandler **nodes;
  411. static __thread unsigned npfd, nalloc;
  412. static __thread Notifier pollfds_cleanup_notifier;
  413. static void pollfds_cleanup(Notifier *n, void *unused)
  414. {
  415. g_assert(npfd == 0);
  416. g_free(pollfds);
  417. g_free(nodes);
  418. nalloc = 0;
  419. }
  420. static void add_pollfd(AioHandler *node)
  421. {
  422. if (npfd == nalloc) {
  423. if (nalloc == 0) {
  424. pollfds_cleanup_notifier.notify = pollfds_cleanup;
  425. qemu_thread_atexit_add(&pollfds_cleanup_notifier);
  426. nalloc = 8;
  427. } else {
  428. g_assert(nalloc <= INT_MAX);
  429. nalloc *= 2;
  430. }
  431. pollfds = g_renew(GPollFD, pollfds, nalloc);
  432. nodes = g_renew(AioHandler *, nodes, nalloc);
  433. }
  434. nodes[npfd] = node;
  435. pollfds[npfd] = (GPollFD) {
  436. .fd = node->pfd.fd,
  437. .events = node->pfd.events,
  438. };
  439. npfd++;
  440. }
  441. static bool run_poll_handlers_once(AioContext *ctx, int64_t *timeout)
  442. {
  443. bool progress = false;
  444. AioHandler *node;
  445. QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
  446. if (!node->deleted && node->io_poll &&
  447. aio_node_check(ctx, node->is_external) &&
  448. node->io_poll(node->opaque)) {
  449. /*
  450. * Polling was successful, exit try_poll_mode immediately
  451. * to adjust the next polling time.
  452. */
  453. *timeout = 0;
  454. if (node->opaque != &ctx->notifier) {
  455. progress = true;
  456. }
  457. }
  458. /* Caller handles freeing deleted nodes. Don't do it here. */
  459. }
  460. return progress;
  461. }
  462. /* run_poll_handlers:
  463. * @ctx: the AioContext
  464. * @max_ns: maximum time to poll for, in nanoseconds
  465. *
  466. * Polls for a given time.
  467. *
  468. * Note that ctx->notify_me must be non-zero so this function can detect
  469. * aio_notify().
  470. *
  471. * Note that the caller must have incremented ctx->list_lock.
  472. *
  473. * Returns: true if progress was made, false otherwise
  474. */
  475. static bool run_poll_handlers(AioContext *ctx, int64_t max_ns, int64_t *timeout)
  476. {
  477. bool progress;
  478. int64_t start_time, elapsed_time;
  479. assert(ctx->notify_me);
  480. assert(qemu_lockcnt_count(&ctx->list_lock) > 0);
  481. trace_run_poll_handlers_begin(ctx, max_ns, *timeout);
  482. start_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
  483. do {
  484. progress = run_poll_handlers_once(ctx, timeout);
  485. elapsed_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time;
  486. max_ns = qemu_soonest_timeout(*timeout, max_ns);
  487. assert(!(max_ns && progress));
  488. } while (elapsed_time < max_ns && !atomic_read(&ctx->poll_disable_cnt));
  489. /* If time has passed with no successful polling, adjust *timeout to
  490. * keep the same ending time.
  491. */
  492. if (*timeout != -1) {
  493. *timeout -= MIN(*timeout, elapsed_time);
  494. }
  495. trace_run_poll_handlers_end(ctx, progress, *timeout);
  496. return progress;
  497. }
  498. /* try_poll_mode:
  499. * @ctx: the AioContext
  500. * @timeout: timeout for blocking wait, computed by the caller and updated if
  501. * polling succeeds.
  502. *
  503. * ctx->notify_me must be non-zero so this function can detect aio_notify().
  504. *
  505. * Note that the caller must have incremented ctx->list_lock.
  506. *
  507. * Returns: true if progress was made, false otherwise
  508. */
  509. static bool try_poll_mode(AioContext *ctx, int64_t *timeout)
  510. {
  511. int64_t max_ns = qemu_soonest_timeout(*timeout, ctx->poll_ns);
  512. if (max_ns && !atomic_read(&ctx->poll_disable_cnt)) {
  513. poll_set_started(ctx, true);
  514. if (run_poll_handlers(ctx, max_ns, timeout)) {
  515. return true;
  516. }
  517. }
  518. poll_set_started(ctx, false);
  519. /* Even if we don't run busy polling, try polling once in case it can make
  520. * progress and the caller will be able to avoid ppoll(2)/epoll_wait(2).
  521. */
  522. return run_poll_handlers_once(ctx, timeout);
  523. }
  524. bool aio_poll(AioContext *ctx, bool blocking)
  525. {
  526. AioHandler *node;
  527. int i;
  528. int ret = 0;
  529. bool progress;
  530. int64_t timeout;
  531. int64_t start = 0;
  532. assert(in_aio_context_home_thread(ctx));
  533. /* aio_notify can avoid the expensive event_notifier_set if
  534. * everything (file descriptors, bottom halves, timers) will
  535. * be re-evaluated before the next blocking poll(). This is
  536. * already true when aio_poll is called with blocking == false;
  537. * if blocking == true, it is only true after poll() returns,
  538. * so disable the optimization now.
  539. */
  540. if (blocking) {
  541. atomic_add(&ctx->notify_me, 2);
  542. }
  543. qemu_lockcnt_inc(&ctx->list_lock);
  544. if (ctx->poll_max_ns) {
  545. start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
  546. }
  547. timeout = blocking ? aio_compute_timeout(ctx) : 0;
  548. progress = try_poll_mode(ctx, &timeout);
  549. assert(!(timeout && progress));
  550. /* If polling is allowed, non-blocking aio_poll does not need the
  551. * system call---a single round of run_poll_handlers_once suffices.
  552. */
  553. if (timeout || atomic_read(&ctx->poll_disable_cnt)) {
  554. assert(npfd == 0);
  555. /* fill pollfds */
  556. if (!aio_epoll_enabled(ctx)) {
  557. QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
  558. if (!node->deleted && node->pfd.events
  559. && aio_node_check(ctx, node->is_external)) {
  560. add_pollfd(node);
  561. }
  562. }
  563. }
  564. /* wait until next event */
  565. if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) {
  566. AioHandler epoll_handler;
  567. epoll_handler.pfd.fd = ctx->epollfd;
  568. epoll_handler.pfd.events = G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR;
  569. npfd = 0;
  570. add_pollfd(&epoll_handler);
  571. ret = aio_epoll(ctx, pollfds, npfd, timeout);
  572. } else {
  573. ret = qemu_poll_ns(pollfds, npfd, timeout);
  574. }
  575. }
  576. if (blocking) {
  577. atomic_sub(&ctx->notify_me, 2);
  578. aio_notify_accept(ctx);
  579. }
  580. /* Adjust polling time */
  581. if (ctx->poll_max_ns) {
  582. int64_t block_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start;
  583. if (block_ns <= ctx->poll_ns) {
  584. /* This is the sweet spot, no adjustment needed */
  585. } else if (block_ns > ctx->poll_max_ns) {
  586. /* We'd have to poll for too long, poll less */
  587. int64_t old = ctx->poll_ns;
  588. if (ctx->poll_shrink) {
  589. ctx->poll_ns /= ctx->poll_shrink;
  590. } else {
  591. ctx->poll_ns = 0;
  592. }
  593. trace_poll_shrink(ctx, old, ctx->poll_ns);
  594. } else if (ctx->poll_ns < ctx->poll_max_ns &&
  595. block_ns < ctx->poll_max_ns) {
  596. /* There is room to grow, poll longer */
  597. int64_t old = ctx->poll_ns;
  598. int64_t grow = ctx->poll_grow;
  599. if (grow == 0) {
  600. grow = 2;
  601. }
  602. if (ctx->poll_ns) {
  603. ctx->poll_ns *= grow;
  604. } else {
  605. ctx->poll_ns = 4000; /* start polling at 4 microseconds */
  606. }
  607. if (ctx->poll_ns > ctx->poll_max_ns) {
  608. ctx->poll_ns = ctx->poll_max_ns;
  609. }
  610. trace_poll_grow(ctx, old, ctx->poll_ns);
  611. }
  612. }
  613. /* if we have any readable fds, dispatch event */
  614. if (ret > 0) {
  615. for (i = 0; i < npfd; i++) {
  616. nodes[i]->pfd.revents = pollfds[i].revents;
  617. }
  618. }
  619. npfd = 0;
  620. progress |= aio_bh_poll(ctx);
  621. if (ret > 0) {
  622. progress |= aio_dispatch_handlers(ctx);
  623. }
  624. qemu_lockcnt_dec(&ctx->list_lock);
  625. progress |= timerlistgroup_run_timers(&ctx->tlg);
  626. return progress;
  627. }
  628. void aio_context_setup(AioContext *ctx)
  629. {
  630. #ifdef CONFIG_EPOLL_CREATE1
  631. assert(!ctx->epollfd);
  632. ctx->epollfd = epoll_create1(EPOLL_CLOEXEC);
  633. if (ctx->epollfd == -1) {
  634. fprintf(stderr, "Failed to create epoll instance: %s", strerror(errno));
  635. ctx->epoll_available = false;
  636. } else {
  637. ctx->epoll_available = true;
  638. }
  639. #endif
  640. }
  641. void aio_context_destroy(AioContext *ctx)
  642. {
  643. #ifdef CONFIG_EPOLL_CREATE1
  644. aio_epoll_disable(ctx);
  645. #endif
  646. }
  647. void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
  648. int64_t grow, int64_t shrink, Error **errp)
  649. {
  650. /* No thread synchronization here, it doesn't matter if an incorrect value
  651. * is used once.
  652. */
  653. ctx->poll_max_ns = max_ns;
  654. ctx->poll_ns = 0;
  655. ctx->poll_grow = grow;
  656. ctx->poll_shrink = shrink;
  657. aio_notify(ctx);
  658. }