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