]> git.proxmox.com Git - mirror_qemu.git/blame - include/block/aio.h
aio: add flag to skip fds to aio_dispatch()
[mirror_qemu.git] / include / block / aio.h
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 *
12 */
13
14#ifndef QEMU_AIO_H
15#define QEMU_AIO_H
16
17#include "qemu-common.h"
1de7afc9
PB
18#include "qemu/queue.h"
19#include "qemu/event_notifier.h"
dcc772e2 20#include "qemu/thread.h"
dae21b98 21#include "qemu/timer.h"
a76bab49 22
7c84b1b8 23typedef struct BlockAIOCB BlockAIOCB;
097310b5 24typedef void BlockCompletionFunc(void *opaque, int ret);
85e8dab1 25
d7331bed 26typedef struct AIOCBInfo {
7c84b1b8
MA
27 void (*cancel_async)(BlockAIOCB *acb);
28 AioContext *(*get_aio_context)(BlockAIOCB *acb);
8c82e9a4 29 size_t aiocb_size;
d7331bed 30} AIOCBInfo;
85e8dab1 31
7c84b1b8 32struct BlockAIOCB {
d7331bed 33 const AIOCBInfo *aiocb_info;
85e8dab1 34 BlockDriverState *bs;
097310b5 35 BlockCompletionFunc *cb;
85e8dab1 36 void *opaque;
f197fe2b 37 int refcnt;
85e8dab1
PB
38};
39
d7331bed 40void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
097310b5 41 BlockCompletionFunc *cb, void *opaque);
8007429a 42void qemu_aio_unref(void *p);
f197fe2b 43void qemu_aio_ref(void *p);
85e8dab1 44
f627aab1
PB
45typedef struct AioHandler AioHandler;
46typedef void QEMUBHFunc(void *opaque);
47typedef void IOHandler(void *opaque);
48
0187f5c9
PB
49struct ThreadPool;
50struct LinuxAioState;
51
6a1751b7 52struct AioContext {
e3713e00
PB
53 GSource source;
54
98563fc3 55 /* Protects all fields from multi-threaded access */
3fe71223 56 QemuRecMutex lock;
98563fc3 57
a915f4bc
PB
58 /* The list of registered AIO handlers */
59 QLIST_HEAD(, AioHandler) aio_handlers;
60
61 /* This is a simple lock used to protect the aio_handlers list.
62 * Specifically, it's used to ensure that no callbacks are removed while
63 * we're walking and dispatching callbacks.
64 */
65 int walking_handlers;
66
eabc9779
PB
67 /* Used to avoid unnecessary event_notifier_set calls in aio_notify;
68 * accessed with atomic primitives. If this field is 0, everything
69 * (file descriptors, bottom halves, timers) will be re-evaluated
70 * before the next blocking poll(), thus the event_notifier_set call
71 * can be skipped. If it is non-zero, you may need to wake up a
72 * concurrent aio_poll or the glib main event loop, making
73 * event_notifier_set necessary.
74 *
75 * Bit 0 is reserved for GSource usage of the AioContext, and is 1
54a16a63 76 * between a call to aio_ctx_prepare and the next call to aio_ctx_check.
eabc9779
PB
77 * Bits 1-31 simply count the number of active calls to aio_poll
78 * that are in the prepare or poll phase.
79 *
80 * The GSource and aio_poll must use a different mechanism because
81 * there is no certainty that a call to GSource's prepare callback
82 * (via g_main_context_prepare) is indeed followed by check and
83 * dispatch. It's not clear whether this would be a bug, but let's
84 * play safe and allow it---it will just cause extra calls to
85 * event_notifier_set until the next call to dispatch.
86 *
87 * Instead, the aio_poll calls include both the prepare and the
88 * dispatch phase, hence a simple counter is enough for them.
0ceb849b 89 */
eabc9779 90 uint32_t notify_me;
0ceb849b 91
dcc772e2
LPF
92 /* lock to protect between bh's adders and deleter */
93 QemuMutex bh_lock;
0ceb849b 94
f627aab1
PB
95 /* Anchor of the list of Bottom Halves belonging to the context */
96 struct QEMUBH *first_bh;
97
98 /* A simple lock used to protect the first_bh list, and ensure that
99 * no callbacks are removed while we're walking and dispatching callbacks.
100 */
101 int walking_bh;
2f4dc3c1 102
05e514b1
PB
103 /* Used by aio_notify.
104 *
105 * "notified" is used to avoid expensive event_notifier_test_and_clear
106 * calls. When it is clear, the EventNotifier is clear, or one thread
107 * is going to clear "notified" before processing more events. False
108 * positives are possible, i.e. "notified" could be set even though the
109 * EventNotifier is clear.
110 *
111 * Note that event_notifier_set *cannot* be optimized the same way. For
112 * more information on the problem that would result, see "#ifdef BUG2"
113 * in the docs/aio_notify_accept.promela formal model.
114 */
115 bool notified;
2f4dc3c1 116 EventNotifier notifier;
6b5f8762 117
9b34277d
SH
118 /* Thread pool for performing work and receiving completion callbacks */
119 struct ThreadPool *thread_pool;
dae21b98 120
0187f5c9
PB
121#ifdef CONFIG_LINUX_AIO
122 /* State for native Linux AIO. Uses aio_context_acquire/release for
123 * locking.
124 */
125 struct LinuxAioState *linux_aio;
126#endif
127
dae21b98
AB
128 /* TimerLists for calling timers - one per clock type */
129 QEMUTimerListGroup tlg;
c1e1e5fa
FZ
130
131 int external_disable_cnt;
fbe3fc5c
FZ
132
133 /* epoll(7) state used when built with CONFIG_EPOLL */
134 int epollfd;
135 bool epoll_enabled;
136 bool epoll_available;
6a1751b7 137};
f627aab1 138
f627aab1
PB
139/**
140 * aio_context_new: Allocate a new AioContext.
141 *
142 * AioContext provide a mini event-loop that can be waited on synchronously.
143 * They also provide bottom halves, a service to execute a piece of code
144 * as soon as possible.
145 */
2f78e491 146AioContext *aio_context_new(Error **errp);
f627aab1 147
e3713e00
PB
148/**
149 * aio_context_ref:
150 * @ctx: The AioContext to operate on.
151 *
152 * Add a reference to an AioContext.
153 */
154void aio_context_ref(AioContext *ctx);
155
156/**
157 * aio_context_unref:
158 * @ctx: The AioContext to operate on.
159 *
160 * Drop a reference to an AioContext.
161 */
162void aio_context_unref(AioContext *ctx);
163
98563fc3 164/* Take ownership of the AioContext. If the AioContext will be shared between
49110174
PB
165 * threads, and a thread does not want to be interrupted, it will have to
166 * take ownership around calls to aio_poll(). Otherwise, aio_poll()
167 * automatically takes care of calling aio_context_acquire and
168 * aio_context_release.
98563fc3 169 *
49110174
PB
170 * Access to timers and BHs from a thread that has not acquired AioContext
171 * is possible. Access to callbacks for now must be done while the AioContext
172 * is owned by the thread (FIXME).
98563fc3
SH
173 */
174void aio_context_acquire(AioContext *ctx);
175
176/* Relinquish ownership of the AioContext. */
177void aio_context_release(AioContext *ctx);
178
5b8bb359
PB
179/**
180 * aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run
181 * only once and as soon as possible.
182 */
183void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
184
f627aab1
PB
185/**
186 * aio_bh_new: Allocate a new bottom half structure.
187 *
188 * Bottom halves are lightweight callbacks whose invocation is guaranteed
189 * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure
190 * is opaque and must be allocated prior to its use.
191 */
192QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
193
2f4dc3c1
PB
194/**
195 * aio_notify: Force processing of pending events.
196 *
197 * Similar to signaling a condition variable, aio_notify forces
722f8d90
YB
198 * aio_poll to exit, so that the next call will re-examine pending events.
199 * The caller of aio_notify will usually call aio_poll again very soon,
2f4dc3c1
PB
200 * or go through another iteration of the GLib main loop. Hence, aio_notify
201 * also has the side effect of recalculating the sets of file descriptors
202 * that the main loop waits for.
203 *
204 * Calling aio_notify is rarely necessary, because for example scheduling
205 * a bottom half calls it already.
206 */
207void aio_notify(AioContext *ctx);
208
05e514b1
PB
209/**
210 * aio_notify_accept: Acknowledge receiving an aio_notify.
211 *
212 * aio_notify() uses an EventNotifier in order to wake up a sleeping
213 * aio_poll() or g_main_context_iteration(). Calls to aio_notify() are
214 * usually rare, but the AioContext has to clear the EventNotifier on
215 * every aio_poll() or g_main_context_iteration() in order to avoid
216 * busy waiting. This event_notifier_test_and_clear() cannot be done
217 * using the usual aio_context_set_event_notifier(), because it must
218 * be done before processing all events (file descriptors, bottom halves,
219 * timers).
220 *
221 * aio_notify_accept() is an optimized event_notifier_test_and_clear()
222 * that is specific to an AioContext's notifier; it is used internally
223 * to clear the EventNotifier only if aio_notify() had been called.
224 */
225void aio_notify_accept(AioContext *ctx);
226
df281b80
PD
227/**
228 * aio_bh_call: Executes callback function of the specified BH.
229 */
230void aio_bh_call(QEMUBH *bh);
231
f627aab1
PB
232/**
233 * aio_bh_poll: Poll bottom halves for an AioContext.
234 *
235 * These are internal functions used by the QEMU main loop.
dcc772e2
LPF
236 * And notice that multiple occurrences of aio_bh_poll cannot
237 * be called concurrently
f627aab1
PB
238 */
239int aio_bh_poll(AioContext *ctx);
f627aab1
PB
240
241/**
242 * qemu_bh_schedule: Schedule a bottom half.
243 *
244 * Scheduling a bottom half interrupts the main loop and causes the
245 * execution of the callback that was passed to qemu_bh_new.
246 *
247 * Bottom halves that are scheduled from a bottom half handler are instantly
248 * invoked. This can create an infinite loop if a bottom half handler
249 * schedules itself.
250 *
251 * @bh: The bottom half to be scheduled.
252 */
253void qemu_bh_schedule(QEMUBH *bh);
254
255/**
256 * qemu_bh_cancel: Cancel execution of a bottom half.
257 *
258 * Canceling execution of a bottom half undoes the effect of calls to
259 * qemu_bh_schedule without freeing its resources yet. While cancellation
260 * itself is also wait-free and thread-safe, it can of course race with the
261 * loop that executes bottom halves unless you are holding the iothread
262 * mutex. This makes it mostly useless if you are not holding the mutex.
263 *
264 * @bh: The bottom half to be canceled.
265 */
266void qemu_bh_cancel(QEMUBH *bh);
267
268/**
269 *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
270 *
271 * Deleting a bottom half frees the memory that was allocated for it by
272 * qemu_bh_new. It also implies canceling the bottom half if it was
273 * scheduled.
dcc772e2
LPF
274 * This func is async. The bottom half will do the delete action at the finial
275 * end.
f627aab1
PB
276 *
277 * @bh: The bottom half to be deleted.
278 */
279void qemu_bh_delete(QEMUBH *bh);
280
cd9ba1eb 281/* Return whether there are any pending callbacks from the GSource
a3462c65
PB
282 * attached to the AioContext, before g_poll is invoked.
283 *
284 * This is used internally in the implementation of the GSource.
285 */
286bool aio_prepare(AioContext *ctx);
287
288/* Return whether there are any pending callbacks from the GSource
289 * attached to the AioContext, after g_poll is invoked.
cd9ba1eb
PB
290 *
291 * This is used internally in the implementation of the GSource.
292 */
293bool aio_pending(AioContext *ctx);
294
e4c7e2d1
PB
295/* Dispatch any pending callbacks from the GSource attached to the AioContext.
296 *
297 * This is used internally in the implementation of the GSource.
721671ad
SH
298 *
299 * @dispatch_fds: true to process fds, false to skip them
300 * (can be used as an optimization by callers that know there
301 * are no fds ready)
e4c7e2d1 302 */
721671ad 303bool aio_dispatch(AioContext *ctx, bool dispatch_fds);
e4c7e2d1 304
7c0628b2
PB
305/* Progress in completing AIO work to occur. This can issue new pending
306 * aio as a result of executing I/O completion or bh callbacks.
bcdc1857 307 *
acfb23ad
PB
308 * Return whether any progress was made by executing AIO or bottom half
309 * handlers. If @blocking == true, this should always be true except
310 * if someone called aio_notify.
7c0628b2
PB
311 *
312 * If there are no pending bottom halves, but there are pending AIO
313 * operations, it may not be possible to make any progress without
314 * blocking. If @blocking is true, this function will wait until one
315 * or more AIO events have completed, to ensure something has moved
316 * before returning.
7c0628b2
PB
317 */
318bool aio_poll(AioContext *ctx, bool blocking);
a76bab49
AL
319
320/* Register a file descriptor and associated callbacks. Behaves very similarly
6484e422 321 * to qemu_set_fd_handler. Unlike qemu_set_fd_handler, these callbacks will
87f68d31 322 * be invoked when using aio_poll().
a76bab49
AL
323 *
324 * Code that invokes AIO completion functions should rely on this function
325 * instead of qemu_set_fd_handler[2].
326 */
a915f4bc
PB
327void aio_set_fd_handler(AioContext *ctx,
328 int fd,
dca21ef2 329 bool is_external,
a915f4bc
PB
330 IOHandler *io_read,
331 IOHandler *io_write,
a915f4bc 332 void *opaque);
9958c351
PB
333
334/* Register an event notifier and associated callbacks. Behaves very similarly
335 * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks
87f68d31 336 * will be invoked when using aio_poll().
9958c351
PB
337 *
338 * Code that invokes AIO completion functions should rely on this function
339 * instead of event_notifier_set_handler.
340 */
a915f4bc
PB
341void aio_set_event_notifier(AioContext *ctx,
342 EventNotifier *notifier,
dca21ef2 343 bool is_external,
f2e5dca4 344 EventNotifierHandler *io_read);
a915f4bc 345
e3713e00
PB
346/* Return a GSource that lets the main loop poll the file descriptors attached
347 * to this AioContext.
348 */
349GSource *aio_get_g_source(AioContext *ctx);
350
9b34277d
SH
351/* Return the ThreadPool bound to this AioContext */
352struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
353
0187f5c9
PB
354/* Return the LinuxAioState bound to this AioContext */
355struct LinuxAioState *aio_get_linux_aio(AioContext *ctx);
356
4e29e831
AB
357/**
358 * aio_timer_new:
359 * @ctx: the aio context
360 * @type: the clock type
361 * @scale: the scale
362 * @cb: the callback to call on timer expiry
363 * @opaque: the opaque pointer to pass to the callback
364 *
365 * Allocate a new timer attached to the context @ctx.
366 * The function is responsible for memory allocation.
367 *
368 * The preferred interface is aio_timer_init. Use that
369 * unless you really need dynamic memory allocation.
370 *
371 * Returns: a pointer to the new timer
372 */
373static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
374 int scale,
375 QEMUTimerCB *cb, void *opaque)
376{
377 return timer_new_tl(ctx->tlg.tl[type], scale, cb, opaque);
378}
379
380/**
381 * aio_timer_init:
382 * @ctx: the aio context
383 * @ts: the timer
384 * @type: the clock type
385 * @scale: the scale
386 * @cb: the callback to call on timer expiry
387 * @opaque: the opaque pointer to pass to the callback
388 *
389 * Initialise a new timer attached to the context @ctx.
390 * The caller is responsible for memory allocation.
391 */
392static inline void aio_timer_init(AioContext *ctx,
393 QEMUTimer *ts, QEMUClockType type,
394 int scale,
395 QEMUTimerCB *cb, void *opaque)
396{
f186aa97 397 timer_init_tl(ts, ctx->tlg.tl[type], scale, cb, opaque);
4e29e831
AB
398}
399
845ca10d
PB
400/**
401 * aio_compute_timeout:
402 * @ctx: the aio context
403 *
404 * Compute the timeout that a blocking aio_poll should use.
405 */
406int64_t aio_compute_timeout(AioContext *ctx);
407
c1e1e5fa
FZ
408/**
409 * aio_disable_external:
410 * @ctx: the aio context
411 *
412 * Disable the further processing of external clients.
413 */
414static inline void aio_disable_external(AioContext *ctx)
415{
416 atomic_inc(&ctx->external_disable_cnt);
417}
418
419/**
420 * aio_enable_external:
421 * @ctx: the aio context
422 *
423 * Enable the processing of external clients.
424 */
425static inline void aio_enable_external(AioContext *ctx)
426{
427 assert(ctx->external_disable_cnt > 0);
428 atomic_dec(&ctx->external_disable_cnt);
429}
430
5ceb9e39
FZ
431/**
432 * aio_external_disabled:
433 * @ctx: the aio context
434 *
435 * Return true if the external clients are disabled.
436 */
437static inline bool aio_external_disabled(AioContext *ctx)
438{
439 return atomic_read(&ctx->external_disable_cnt);
440}
441
c1e1e5fa
FZ
442/**
443 * aio_node_check:
444 * @ctx: the aio context
445 * @is_external: Whether or not the checked node is an external event source.
446 *
447 * Check if the node's is_external flag is okay to be polled by the ctx at this
448 * moment. True means green light.
449 */
450static inline bool aio_node_check(AioContext *ctx, bool is_external)
451{
452 return !is_external || !atomic_read(&ctx->external_disable_cnt);
453}
454
e4370165
PB
455/**
456 * Return the AioContext whose event loop runs in the current thread.
457 *
458 * If called from an IOThread this will be the IOThread's AioContext. If
459 * called from another thread it will be the main loop AioContext.
460 */
461AioContext *qemu_get_current_aio_context(void);
462
463/**
464 * @ctx: the aio context
465 *
466 * Return whether we are running in the I/O thread that manages @ctx.
467 */
468static inline bool aio_context_in_iothread(AioContext *ctx)
469{
470 return ctx == qemu_get_current_aio_context();
471}
472
37fcee5d
FZ
473/**
474 * aio_context_setup:
475 * @ctx: the aio context
476 *
477 * Initialize the aio context.
478 */
7e003465 479void aio_context_setup(AioContext *ctx);
37fcee5d 480
a76bab49 481#endif