]> git.proxmox.com Git - mirror_qemu.git/blame - aio-posix.c
block/parallels: improve image reading performance
[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
16#include "qemu-common.h"
737e150e 17#include "block/block.h"
1de7afc9
PB
18#include "qemu/queue.h"
19#include "qemu/sockets.h"
a76bab49 20
a76bab49
AL
21struct AioHandler
22{
cd9ba1eb 23 GPollFD pfd;
a76bab49
AL
24 IOHandler *io_read;
25 IOHandler *io_write;
a76bab49
AL
26 int deleted;
27 void *opaque;
72cf2d4f 28 QLIST_ENTRY(AioHandler) node;
a76bab49
AL
29};
30
a915f4bc 31static AioHandler *find_aio_handler(AioContext *ctx, int fd)
a76bab49
AL
32{
33 AioHandler *node;
34
a915f4bc 35 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
cd9ba1eb 36 if (node->pfd.fd == fd)
79d5ca56
AG
37 if (!node->deleted)
38 return node;
a76bab49
AL
39 }
40
41 return NULL;
42}
43
a915f4bc
PB
44void aio_set_fd_handler(AioContext *ctx,
45 int fd,
46 IOHandler *io_read,
47 IOHandler *io_write,
a915f4bc 48 void *opaque)
a76bab49
AL
49{
50 AioHandler *node;
51
a915f4bc 52 node = find_aio_handler(ctx, fd);
a76bab49
AL
53
54 /* Are we deleting the fd handler? */
55 if (!io_read && !io_write) {
56 if (node) {
e3713e00
PB
57 g_source_remove_poll(&ctx->source, &node->pfd);
58
a76bab49 59 /* If the lock is held, just mark the node as deleted */
cd9ba1eb 60 if (ctx->walking_handlers) {
a76bab49 61 node->deleted = 1;
cd9ba1eb
PB
62 node->pfd.revents = 0;
63 } else {
a76bab49
AL
64 /* Otherwise, delete it for real. We can't just mark it as
65 * deleted because deleted nodes are only cleaned up after
66 * releasing the walking_handlers lock.
67 */
72cf2d4f 68 QLIST_REMOVE(node, node);
7267c094 69 g_free(node);
a76bab49
AL
70 }
71 }
72 } else {
73 if (node == NULL) {
74 /* Alloc and insert if it's not already there */
3ba235a0 75 node = g_new0(AioHandler, 1);
cd9ba1eb 76 node->pfd.fd = fd;
a915f4bc 77 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
e3713e00
PB
78
79 g_source_add_poll(&ctx->source, &node->pfd);
a76bab49
AL
80 }
81 /* Update handler with latest information */
82 node->io_read = io_read;
83 node->io_write = io_write;
a76bab49 84 node->opaque = opaque;
cd9ba1eb 85
b5a01a70
SH
86 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
87 node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
a76bab49 88 }
7ed2b24c
PB
89
90 aio_notify(ctx);
9958c351
PB
91}
92
a915f4bc
PB
93void aio_set_event_notifier(AioContext *ctx,
94 EventNotifier *notifier,
f2e5dca4 95 EventNotifierHandler *io_read)
a76bab49 96{
a915f4bc 97 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
f2e5dca4 98 (IOHandler *)io_read, NULL, notifier);
a76bab49
AL
99}
100
a3462c65
PB
101bool aio_prepare(AioContext *ctx)
102{
103 return false;
104}
105
cd9ba1eb
PB
106bool aio_pending(AioContext *ctx)
107{
108 AioHandler *node;
109
110 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
111 int revents;
112
cd9ba1eb
PB
113 revents = node->pfd.revents & node->pfd.events;
114 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
115 return true;
116 }
117 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
118 return true;
119 }
120 }
121
122 return false;
123}
124
e4c7e2d1 125bool aio_dispatch(AioContext *ctx)
a76bab49 126{
9eb0bfca 127 AioHandler *node;
d0c8d2c0 128 bool progress = false;
7c0628b2 129
e4c7e2d1
PB
130 /*
131 * If there are callbacks left that have been queued, we need to call them.
132 * Do not call select in this case, because it is possible that the caller
133 * does not need a complete flush (as is the case for aio_poll loops).
134 */
135 if (aio_bh_poll(ctx)) {
136 progress = true;
137 }
138
cd9ba1eb 139 /*
87f68d31 140 * We have to walk very carefully in case aio_set_fd_handler is
cd9ba1eb
PB
141 * called while we're walking.
142 */
143 node = QLIST_FIRST(&ctx->aio_handlers);
144 while (node) {
145 AioHandler *tmp;
146 int revents;
147
148 ctx->walking_handlers++;
149
150 revents = node->pfd.revents & node->pfd.events;
151 node->pfd.revents = 0;
152
d0c8d2c0
SH
153 if (!node->deleted &&
154 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
155 node->io_read) {
cd9ba1eb 156 node->io_read(node->opaque);
164a101f
SH
157
158 /* aio_notify() does not count as progress */
159 if (node->opaque != &ctx->notifier) {
160 progress = true;
161 }
cd9ba1eb 162 }
d0c8d2c0
SH
163 if (!node->deleted &&
164 (revents & (G_IO_OUT | G_IO_ERR)) &&
165 node->io_write) {
cd9ba1eb
PB
166 node->io_write(node->opaque);
167 progress = true;
168 }
169
170 tmp = node;
171 node = QLIST_NEXT(node, node);
172
173 ctx->walking_handlers--;
174
175 if (!ctx->walking_handlers && tmp->deleted) {
176 QLIST_REMOVE(tmp, node);
177 g_free(tmp);
178 }
179 }
438e1f47
AB
180
181 /* Run our timers */
182 progress |= timerlistgroup_run_timers(&ctx->tlg);
183
d0c8d2c0
SH
184 return progress;
185}
186
e98ab097
PB
187/* These thread-local variables are used only in a small part of aio_poll
188 * around the call to the poll() system call. In particular they are not
189 * used while aio_poll is performing callbacks, which makes it much easier
190 * to think about reentrancy!
191 *
192 * Stack-allocated arrays would be perfect but they have size limitations;
193 * heap allocation is expensive enough that we want to reuse arrays across
194 * calls to aio_poll(). And because poll() has to be called without holding
195 * any lock, the arrays cannot be stored in AioContext. Thread-local data
196 * has none of the disadvantages of these three options.
197 */
198static __thread GPollFD *pollfds;
199static __thread AioHandler **nodes;
200static __thread unsigned npfd, nalloc;
201static __thread Notifier pollfds_cleanup_notifier;
202
203static void pollfds_cleanup(Notifier *n, void *unused)
204{
205 g_assert(npfd == 0);
206 g_free(pollfds);
207 g_free(nodes);
208 nalloc = 0;
209}
210
211static void add_pollfd(AioHandler *node)
212{
213 if (npfd == nalloc) {
214 if (nalloc == 0) {
215 pollfds_cleanup_notifier.notify = pollfds_cleanup;
216 qemu_thread_atexit_add(&pollfds_cleanup_notifier);
217 nalloc = 8;
218 } else {
219 g_assert(nalloc <= INT_MAX);
220 nalloc *= 2;
221 }
222 pollfds = g_renew(GPollFD, pollfds, nalloc);
223 nodes = g_renew(AioHandler *, nodes, nalloc);
224 }
225 nodes[npfd] = node;
226 pollfds[npfd] = (GPollFD) {
227 .fd = node->pfd.fd,
228 .events = node->pfd.events,
229 };
230 npfd++;
231}
232
d0c8d2c0
SH
233bool aio_poll(AioContext *ctx, bool blocking)
234{
d0c8d2c0 235 AioHandler *node;
0ceb849b 236 bool was_dispatching;
e98ab097 237 int i, ret;
164a101f 238 bool progress;
e98ab097 239 int64_t timeout;
d0c8d2c0 240
49110174 241 aio_context_acquire(ctx);
0ceb849b 242 was_dispatching = ctx->dispatching;
d0c8d2c0
SH
243 progress = false;
244
0ceb849b
PB
245 /* aio_notify can avoid the expensive event_notifier_set if
246 * everything (file descriptors, bottom halves, timers) will
e4c7e2d1
PB
247 * be re-evaluated before the next blocking poll(). This is
248 * already true when aio_poll is called with blocking == false;
249 * if blocking == true, it is only true after poll() returns.
0ceb849b
PB
250 *
251 * If we're in a nested event loop, ctx->dispatching might be true.
252 * In that case we can restore it just before returning, but we
253 * have to clear it now.
254 */
255 aio_set_dispatching(ctx, !blocking);
256
a915f4bc 257 ctx->walking_handlers++;
a76bab49 258
e98ab097 259 assert(npfd == 0);
a76bab49 260
6b5f8762 261 /* fill pollfds */
a915f4bc 262 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
6b5f8762 263 if (!node->deleted && node->pfd.events) {
e98ab097 264 add_pollfd(node);
9eb0bfca
PB
265 }
266 }
a76bab49 267
e98ab097 268 timeout = blocking ? aio_compute_timeout(ctx) : 0;
a76bab49 269
9eb0bfca 270 /* wait until next event */
49110174
PB
271 if (timeout) {
272 aio_context_release(ctx);
273 }
e98ab097 274 ret = qemu_poll_ns((GPollFD *)pollfds, npfd, timeout);
49110174
PB
275 if (timeout) {
276 aio_context_acquire(ctx);
277 }
9eb0bfca
PB
278
279 /* if we have any readable fds, dispatch event */
280 if (ret > 0) {
e98ab097
PB
281 for (i = 0; i < npfd; i++) {
282 nodes[i]->pfd.revents = pollfds[i].revents;
a76bab49 283 }
438e1f47
AB
284 }
285
e98ab097
PB
286 npfd = 0;
287 ctx->walking_handlers--;
288
438e1f47 289 /* Run dispatch even if there were no readable fds to run timers */
0ceb849b 290 aio_set_dispatching(ctx, true);
438e1f47
AB
291 if (aio_dispatch(ctx)) {
292 progress = true;
9eb0bfca 293 }
bcdc1857 294
0ceb849b 295 aio_set_dispatching(ctx, was_dispatching);
49110174
PB
296 aio_context_release(ctx);
297
164a101f 298 return progress;
a76bab49 299}