1737e150eSPaolo Bonzini /* 2737e150eSPaolo Bonzini * QEMU aio implementation 3737e150eSPaolo Bonzini * 4737e150eSPaolo Bonzini * Copyright IBM, Corp. 2008 5737e150eSPaolo Bonzini * 6737e150eSPaolo Bonzini * Authors: 7737e150eSPaolo Bonzini * Anthony Liguori <aliguori@us.ibm.com> 8737e150eSPaolo Bonzini * 9737e150eSPaolo Bonzini * This work is licensed under the terms of the GNU GPL, version 2. See 10737e150eSPaolo Bonzini * the COPYING file in the top-level directory. 11737e150eSPaolo Bonzini * 12737e150eSPaolo Bonzini */ 13737e150eSPaolo Bonzini 14737e150eSPaolo Bonzini #ifndef QEMU_AIO_H 15737e150eSPaolo Bonzini #define QEMU_AIO_H 16737e150eSPaolo Bonzini 17737e150eSPaolo Bonzini #include "qemu-common.h" 181de7afc9SPaolo Bonzini #include "qemu/queue.h" 191de7afc9SPaolo Bonzini #include "qemu/event_notifier.h" 20dcc772e2SLiu Ping Fan #include "qemu/thread.h" 21dae21b98SAlex Bligh #include "qemu/timer.h" 22737e150eSPaolo Bonzini 237c84b1b8SMarkus Armbruster typedef struct BlockAIOCB BlockAIOCB; 24097310b5SMarkus Armbruster typedef void BlockCompletionFunc(void *opaque, int ret); 25737e150eSPaolo Bonzini 26737e150eSPaolo Bonzini typedef struct AIOCBInfo { 277c84b1b8SMarkus Armbruster void (*cancel_async)(BlockAIOCB *acb); 287c84b1b8SMarkus Armbruster AioContext *(*get_aio_context)(BlockAIOCB *acb); 29737e150eSPaolo Bonzini size_t aiocb_size; 30737e150eSPaolo Bonzini } AIOCBInfo; 31737e150eSPaolo Bonzini 327c84b1b8SMarkus Armbruster struct BlockAIOCB { 33737e150eSPaolo Bonzini const AIOCBInfo *aiocb_info; 34737e150eSPaolo Bonzini BlockDriverState *bs; 35097310b5SMarkus Armbruster BlockCompletionFunc *cb; 36737e150eSPaolo Bonzini void *opaque; 37f197fe2bSFam Zheng int refcnt; 38737e150eSPaolo Bonzini }; 39737e150eSPaolo Bonzini 40737e150eSPaolo Bonzini void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, 41097310b5SMarkus Armbruster BlockCompletionFunc *cb, void *opaque); 428007429aSFam Zheng void qemu_aio_unref(void *p); 43f197fe2bSFam Zheng void qemu_aio_ref(void *p); 44737e150eSPaolo Bonzini 45737e150eSPaolo Bonzini typedef struct AioHandler AioHandler; 46737e150eSPaolo Bonzini typedef void QEMUBHFunc(void *opaque); 47f6a51c84SStefan Hajnoczi typedef bool AioPollFn(void *opaque); 48737e150eSPaolo Bonzini typedef void IOHandler(void *opaque); 49737e150eSPaolo Bonzini 500c330a73SPaolo Bonzini struct Coroutine; 510187f5c9SPaolo Bonzini struct ThreadPool; 520187f5c9SPaolo Bonzini struct LinuxAioState; 530187f5c9SPaolo Bonzini 546a1751b7SAlex Bligh struct AioContext { 55737e150eSPaolo Bonzini GSource source; 56737e150eSPaolo Bonzini 577c690fd1SPaolo Bonzini /* Used by AioContext users to protect from multi-threaded access. */ 583fe71223SPaolo Bonzini QemuRecMutex lock; 5998563fc3SStefan Hajnoczi 607c690fd1SPaolo Bonzini /* The list of registered AIO handlers. Protected by ctx->list_lock. */ 61737e150eSPaolo Bonzini QLIST_HEAD(, AioHandler) aio_handlers; 62737e150eSPaolo Bonzini 63eabc9779SPaolo Bonzini /* Used to avoid unnecessary event_notifier_set calls in aio_notify; 64eabc9779SPaolo Bonzini * accessed with atomic primitives. If this field is 0, everything 65eabc9779SPaolo Bonzini * (file descriptors, bottom halves, timers) will be re-evaluated 66eabc9779SPaolo Bonzini * before the next blocking poll(), thus the event_notifier_set call 67eabc9779SPaolo Bonzini * can be skipped. If it is non-zero, you may need to wake up a 68eabc9779SPaolo Bonzini * concurrent aio_poll or the glib main event loop, making 69eabc9779SPaolo Bonzini * event_notifier_set necessary. 70eabc9779SPaolo Bonzini * 71eabc9779SPaolo Bonzini * Bit 0 is reserved for GSource usage of the AioContext, and is 1 7254a16a63SCao jin * between a call to aio_ctx_prepare and the next call to aio_ctx_check. 73eabc9779SPaolo Bonzini * Bits 1-31 simply count the number of active calls to aio_poll 74eabc9779SPaolo Bonzini * that are in the prepare or poll phase. 75eabc9779SPaolo Bonzini * 76eabc9779SPaolo Bonzini * The GSource and aio_poll must use a different mechanism because 77eabc9779SPaolo Bonzini * there is no certainty that a call to GSource's prepare callback 78eabc9779SPaolo Bonzini * (via g_main_context_prepare) is indeed followed by check and 79eabc9779SPaolo Bonzini * dispatch. It's not clear whether this would be a bug, but let's 80eabc9779SPaolo Bonzini * play safe and allow it---it will just cause extra calls to 81eabc9779SPaolo Bonzini * event_notifier_set until the next call to dispatch. 82eabc9779SPaolo Bonzini * 83eabc9779SPaolo Bonzini * Instead, the aio_poll calls include both the prepare and the 84eabc9779SPaolo Bonzini * dispatch phase, hence a simple counter is enough for them. 850ceb849bSPaolo Bonzini */ 86eabc9779SPaolo Bonzini uint32_t notify_me; 870ceb849bSPaolo Bonzini 887c690fd1SPaolo Bonzini /* A lock to protect between QEMUBH and AioHandler adders and deleter, 897c690fd1SPaolo Bonzini * and to ensure that no callbacks are removed while we're walking and 907c690fd1SPaolo Bonzini * dispatching them. 91d7c99a12SPaolo Bonzini */ 92d7c99a12SPaolo Bonzini QemuLockCnt list_lock; 930ceb849bSPaolo Bonzini 94737e150eSPaolo Bonzini /* Anchor of the list of Bottom Halves belonging to the context */ 95737e150eSPaolo Bonzini struct QEMUBH *first_bh; 96737e150eSPaolo Bonzini 9705e514b1SPaolo Bonzini /* Used by aio_notify. 9805e514b1SPaolo Bonzini * 9905e514b1SPaolo Bonzini * "notified" is used to avoid expensive event_notifier_test_and_clear 10005e514b1SPaolo Bonzini * calls. When it is clear, the EventNotifier is clear, or one thread 10105e514b1SPaolo Bonzini * is going to clear "notified" before processing more events. False 10205e514b1SPaolo Bonzini * positives are possible, i.e. "notified" could be set even though the 10305e514b1SPaolo Bonzini * EventNotifier is clear. 10405e514b1SPaolo Bonzini * 10505e514b1SPaolo Bonzini * Note that event_notifier_set *cannot* be optimized the same way. For 10605e514b1SPaolo Bonzini * more information on the problem that would result, see "#ifdef BUG2" 10705e514b1SPaolo Bonzini * in the docs/aio_notify_accept.promela formal model. 10805e514b1SPaolo Bonzini */ 10905e514b1SPaolo Bonzini bool notified; 110737e150eSPaolo Bonzini EventNotifier notifier; 1116b5f8762SStefan Hajnoczi 1120c330a73SPaolo Bonzini QSLIST_HEAD(, Coroutine) scheduled_coroutines; 1130c330a73SPaolo Bonzini QEMUBH *co_schedule_bh; 1140c330a73SPaolo Bonzini 1157c690fd1SPaolo Bonzini /* Thread pool for performing work and receiving completion callbacks. 1167c690fd1SPaolo Bonzini * Has its own locking. 1177c690fd1SPaolo Bonzini */ 1189b34277dSStefan Hajnoczi struct ThreadPool *thread_pool; 119dae21b98SAlex Bligh 1200187f5c9SPaolo Bonzini #ifdef CONFIG_LINUX_AIO 1210187f5c9SPaolo Bonzini /* State for native Linux AIO. Uses aio_context_acquire/release for 1220187f5c9SPaolo Bonzini * locking. 1230187f5c9SPaolo Bonzini */ 1240187f5c9SPaolo Bonzini struct LinuxAioState *linux_aio; 1250187f5c9SPaolo Bonzini #endif 1260187f5c9SPaolo Bonzini 1277c690fd1SPaolo Bonzini /* TimerLists for calling timers - one per clock type. Has its own 1287c690fd1SPaolo Bonzini * locking. 1297c690fd1SPaolo Bonzini */ 130dae21b98SAlex Bligh QEMUTimerListGroup tlg; 131c1e1e5faSFam Zheng 132c1e1e5faSFam Zheng int external_disable_cnt; 133fbe3fc5cSFam Zheng 1344a1cba38SStefan Hajnoczi /* Number of AioHandlers without .io_poll() */ 1354a1cba38SStefan Hajnoczi int poll_disable_cnt; 1364a1cba38SStefan Hajnoczi 13782a41186SStefan Hajnoczi /* Polling mode parameters */ 13882a41186SStefan Hajnoczi int64_t poll_ns; /* current polling time in nanoseconds */ 13982a41186SStefan Hajnoczi int64_t poll_max_ns; /* maximum polling time in nanoseconds */ 14082a41186SStefan Hajnoczi int64_t poll_grow; /* polling time growth factor */ 14182a41186SStefan Hajnoczi int64_t poll_shrink; /* polling time shrink factor */ 1424a1cba38SStefan Hajnoczi 143684e508cSStefan Hajnoczi /* Are we in polling mode or monitoring file descriptors? */ 144684e508cSStefan Hajnoczi bool poll_started; 145684e508cSStefan Hajnoczi 146fbe3fc5cSFam Zheng /* epoll(7) state used when built with CONFIG_EPOLL */ 147fbe3fc5cSFam Zheng int epollfd; 148fbe3fc5cSFam Zheng bool epoll_enabled; 149fbe3fc5cSFam Zheng bool epoll_available; 1506a1751b7SAlex Bligh }; 151737e150eSPaolo Bonzini 152737e150eSPaolo Bonzini /** 153737e150eSPaolo Bonzini * aio_context_new: Allocate a new AioContext. 154737e150eSPaolo Bonzini * 155737e150eSPaolo Bonzini * AioContext provide a mini event-loop that can be waited on synchronously. 156737e150eSPaolo Bonzini * They also provide bottom halves, a service to execute a piece of code 157737e150eSPaolo Bonzini * as soon as possible. 158737e150eSPaolo Bonzini */ 1592f78e491SChrysostomos Nanakos AioContext *aio_context_new(Error **errp); 160737e150eSPaolo Bonzini 161737e150eSPaolo Bonzini /** 162737e150eSPaolo Bonzini * aio_context_ref: 163737e150eSPaolo Bonzini * @ctx: The AioContext to operate on. 164737e150eSPaolo Bonzini * 165737e150eSPaolo Bonzini * Add a reference to an AioContext. 166737e150eSPaolo Bonzini */ 167737e150eSPaolo Bonzini void aio_context_ref(AioContext *ctx); 168737e150eSPaolo Bonzini 169737e150eSPaolo Bonzini /** 170737e150eSPaolo Bonzini * aio_context_unref: 171737e150eSPaolo Bonzini * @ctx: The AioContext to operate on. 172737e150eSPaolo Bonzini * 173737e150eSPaolo Bonzini * Drop a reference to an AioContext. 174737e150eSPaolo Bonzini */ 175737e150eSPaolo Bonzini void aio_context_unref(AioContext *ctx); 176737e150eSPaolo Bonzini 17798563fc3SStefan Hajnoczi /* Take ownership of the AioContext. If the AioContext will be shared between 17849110174SPaolo Bonzini * threads, and a thread does not want to be interrupted, it will have to 17949110174SPaolo Bonzini * take ownership around calls to aio_poll(). Otherwise, aio_poll() 18049110174SPaolo Bonzini * automatically takes care of calling aio_context_acquire and 18149110174SPaolo Bonzini * aio_context_release. 18298563fc3SStefan Hajnoczi * 1837c690fd1SPaolo Bonzini * Note that this is separate from bdrv_drained_begin/bdrv_drained_end. A 1847c690fd1SPaolo Bonzini * thread still has to call those to avoid being interrupted by the guest. 1857c690fd1SPaolo Bonzini * 1867c690fd1SPaolo Bonzini * Bottom halves, timers and callbacks can be created or removed without 1877c690fd1SPaolo Bonzini * acquiring the AioContext. 18898563fc3SStefan Hajnoczi */ 18998563fc3SStefan Hajnoczi void aio_context_acquire(AioContext *ctx); 19098563fc3SStefan Hajnoczi 19198563fc3SStefan Hajnoczi /* Relinquish ownership of the AioContext. */ 19298563fc3SStefan Hajnoczi void aio_context_release(AioContext *ctx); 19398563fc3SStefan Hajnoczi 194737e150eSPaolo Bonzini /** 1955b8bb359SPaolo Bonzini * aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run 1965b8bb359SPaolo Bonzini * only once and as soon as possible. 1975b8bb359SPaolo Bonzini */ 1985b8bb359SPaolo Bonzini void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque); 1995b8bb359SPaolo Bonzini 2005b8bb359SPaolo Bonzini /** 201737e150eSPaolo Bonzini * aio_bh_new: Allocate a new bottom half structure. 202737e150eSPaolo Bonzini * 203737e150eSPaolo Bonzini * Bottom halves are lightweight callbacks whose invocation is guaranteed 204737e150eSPaolo Bonzini * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure 205737e150eSPaolo Bonzini * is opaque and must be allocated prior to its use. 206737e150eSPaolo Bonzini */ 207737e150eSPaolo Bonzini QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque); 208737e150eSPaolo Bonzini 209737e150eSPaolo Bonzini /** 210737e150eSPaolo Bonzini * aio_notify: Force processing of pending events. 211737e150eSPaolo Bonzini * 212737e150eSPaolo Bonzini * Similar to signaling a condition variable, aio_notify forces 213722f8d90SYaowei Bai * aio_poll to exit, so that the next call will re-examine pending events. 214722f8d90SYaowei Bai * The caller of aio_notify will usually call aio_poll again very soon, 215737e150eSPaolo Bonzini * or go through another iteration of the GLib main loop. Hence, aio_notify 216737e150eSPaolo Bonzini * also has the side effect of recalculating the sets of file descriptors 217737e150eSPaolo Bonzini * that the main loop waits for. 218737e150eSPaolo Bonzini * 219737e150eSPaolo Bonzini * Calling aio_notify is rarely necessary, because for example scheduling 220737e150eSPaolo Bonzini * a bottom half calls it already. 221737e150eSPaolo Bonzini */ 222737e150eSPaolo Bonzini void aio_notify(AioContext *ctx); 223737e150eSPaolo Bonzini 224737e150eSPaolo Bonzini /** 22505e514b1SPaolo Bonzini * aio_notify_accept: Acknowledge receiving an aio_notify. 22605e514b1SPaolo Bonzini * 22705e514b1SPaolo Bonzini * aio_notify() uses an EventNotifier in order to wake up a sleeping 22805e514b1SPaolo Bonzini * aio_poll() or g_main_context_iteration(). Calls to aio_notify() are 22905e514b1SPaolo Bonzini * usually rare, but the AioContext has to clear the EventNotifier on 23005e514b1SPaolo Bonzini * every aio_poll() or g_main_context_iteration() in order to avoid 23105e514b1SPaolo Bonzini * busy waiting. This event_notifier_test_and_clear() cannot be done 23205e514b1SPaolo Bonzini * using the usual aio_context_set_event_notifier(), because it must 23305e514b1SPaolo Bonzini * be done before processing all events (file descriptors, bottom halves, 23405e514b1SPaolo Bonzini * timers). 23505e514b1SPaolo Bonzini * 23605e514b1SPaolo Bonzini * aio_notify_accept() is an optimized event_notifier_test_and_clear() 23705e514b1SPaolo Bonzini * that is specific to an AioContext's notifier; it is used internally 23805e514b1SPaolo Bonzini * to clear the EventNotifier only if aio_notify() had been called. 23905e514b1SPaolo Bonzini */ 24005e514b1SPaolo Bonzini void aio_notify_accept(AioContext *ctx); 24105e514b1SPaolo Bonzini 24205e514b1SPaolo Bonzini /** 243df281b80SPavel Dovgalyuk * aio_bh_call: Executes callback function of the specified BH. 244df281b80SPavel Dovgalyuk */ 245df281b80SPavel Dovgalyuk void aio_bh_call(QEMUBH *bh); 246df281b80SPavel Dovgalyuk 247df281b80SPavel Dovgalyuk /** 248737e150eSPaolo Bonzini * aio_bh_poll: Poll bottom halves for an AioContext. 249737e150eSPaolo Bonzini * 250737e150eSPaolo Bonzini * These are internal functions used by the QEMU main loop. 251dcc772e2SLiu Ping Fan * And notice that multiple occurrences of aio_bh_poll cannot 252dcc772e2SLiu Ping Fan * be called concurrently 253737e150eSPaolo Bonzini */ 254737e150eSPaolo Bonzini int aio_bh_poll(AioContext *ctx); 255737e150eSPaolo Bonzini 256737e150eSPaolo Bonzini /** 257737e150eSPaolo Bonzini * qemu_bh_schedule: Schedule a bottom half. 258737e150eSPaolo Bonzini * 259737e150eSPaolo Bonzini * Scheduling a bottom half interrupts the main loop and causes the 260737e150eSPaolo Bonzini * execution of the callback that was passed to qemu_bh_new. 261737e150eSPaolo Bonzini * 262737e150eSPaolo Bonzini * Bottom halves that are scheduled from a bottom half handler are instantly 263737e150eSPaolo Bonzini * invoked. This can create an infinite loop if a bottom half handler 264737e150eSPaolo Bonzini * schedules itself. 265737e150eSPaolo Bonzini * 266737e150eSPaolo Bonzini * @bh: The bottom half to be scheduled. 267737e150eSPaolo Bonzini */ 268737e150eSPaolo Bonzini void qemu_bh_schedule(QEMUBH *bh); 269737e150eSPaolo Bonzini 270737e150eSPaolo Bonzini /** 271737e150eSPaolo Bonzini * qemu_bh_cancel: Cancel execution of a bottom half. 272737e150eSPaolo Bonzini * 273737e150eSPaolo Bonzini * Canceling execution of a bottom half undoes the effect of calls to 274737e150eSPaolo Bonzini * qemu_bh_schedule without freeing its resources yet. While cancellation 275737e150eSPaolo Bonzini * itself is also wait-free and thread-safe, it can of course race with the 276737e150eSPaolo Bonzini * loop that executes bottom halves unless you are holding the iothread 277737e150eSPaolo Bonzini * mutex. This makes it mostly useless if you are not holding the mutex. 278737e150eSPaolo Bonzini * 279737e150eSPaolo Bonzini * @bh: The bottom half to be canceled. 280737e150eSPaolo Bonzini */ 281737e150eSPaolo Bonzini void qemu_bh_cancel(QEMUBH *bh); 282737e150eSPaolo Bonzini 283737e150eSPaolo Bonzini /** 284737e150eSPaolo Bonzini *qemu_bh_delete: Cancel execution of a bottom half and free its resources. 285737e150eSPaolo Bonzini * 286737e150eSPaolo Bonzini * Deleting a bottom half frees the memory that was allocated for it by 287737e150eSPaolo Bonzini * qemu_bh_new. It also implies canceling the bottom half if it was 288737e150eSPaolo Bonzini * scheduled. 289dcc772e2SLiu Ping Fan * This func is async. The bottom half will do the delete action at the finial 290dcc772e2SLiu Ping Fan * end. 291737e150eSPaolo Bonzini * 292737e150eSPaolo Bonzini * @bh: The bottom half to be deleted. 293737e150eSPaolo Bonzini */ 294737e150eSPaolo Bonzini void qemu_bh_delete(QEMUBH *bh); 295737e150eSPaolo Bonzini 296737e150eSPaolo Bonzini /* Return whether there are any pending callbacks from the GSource 297a3462c65SPaolo Bonzini * attached to the AioContext, before g_poll is invoked. 298a3462c65SPaolo Bonzini * 299a3462c65SPaolo Bonzini * This is used internally in the implementation of the GSource. 300a3462c65SPaolo Bonzini */ 301a3462c65SPaolo Bonzini bool aio_prepare(AioContext *ctx); 302a3462c65SPaolo Bonzini 303a3462c65SPaolo Bonzini /* Return whether there are any pending callbacks from the GSource 304a3462c65SPaolo Bonzini * attached to the AioContext, after g_poll is invoked. 305737e150eSPaolo Bonzini * 306737e150eSPaolo Bonzini * This is used internally in the implementation of the GSource. 307737e150eSPaolo Bonzini */ 308737e150eSPaolo Bonzini bool aio_pending(AioContext *ctx); 309737e150eSPaolo Bonzini 310e4c7e2d1SPaolo Bonzini /* Dispatch any pending callbacks from the GSource attached to the AioContext. 311e4c7e2d1SPaolo Bonzini * 312e4c7e2d1SPaolo Bonzini * This is used internally in the implementation of the GSource. 313e4c7e2d1SPaolo Bonzini */ 314a153bf52SPaolo Bonzini void aio_dispatch(AioContext *ctx); 315e4c7e2d1SPaolo Bonzini 316737e150eSPaolo Bonzini /* Progress in completing AIO work to occur. This can issue new pending 317737e150eSPaolo Bonzini * aio as a result of executing I/O completion or bh callbacks. 318737e150eSPaolo Bonzini * 319acfb23adSPaolo Bonzini * Return whether any progress was made by executing AIO or bottom half 320acfb23adSPaolo Bonzini * handlers. If @blocking == true, this should always be true except 321acfb23adSPaolo Bonzini * if someone called aio_notify. 322737e150eSPaolo Bonzini * 323737e150eSPaolo Bonzini * If there are no pending bottom halves, but there are pending AIO 324737e150eSPaolo Bonzini * operations, it may not be possible to make any progress without 325737e150eSPaolo Bonzini * blocking. If @blocking is true, this function will wait until one 326737e150eSPaolo Bonzini * or more AIO events have completed, to ensure something has moved 327737e150eSPaolo Bonzini * before returning. 328737e150eSPaolo Bonzini */ 329737e150eSPaolo Bonzini bool aio_poll(AioContext *ctx, bool blocking); 330737e150eSPaolo Bonzini 331737e150eSPaolo Bonzini /* Register a file descriptor and associated callbacks. Behaves very similarly 3326484e422SFam Zheng * to qemu_set_fd_handler. Unlike qemu_set_fd_handler, these callbacks will 33387f68d31SPaolo Bonzini * be invoked when using aio_poll(). 334737e150eSPaolo Bonzini * 335737e150eSPaolo Bonzini * Code that invokes AIO completion functions should rely on this function 336737e150eSPaolo Bonzini * instead of qemu_set_fd_handler[2]. 337737e150eSPaolo Bonzini */ 338737e150eSPaolo Bonzini void aio_set_fd_handler(AioContext *ctx, 339737e150eSPaolo Bonzini int fd, 340dca21ef2SFam Zheng bool is_external, 341737e150eSPaolo Bonzini IOHandler *io_read, 342737e150eSPaolo Bonzini IOHandler *io_write, 343f6a51c84SStefan Hajnoczi AioPollFn *io_poll, 344737e150eSPaolo Bonzini void *opaque); 345737e150eSPaolo Bonzini 346684e508cSStefan Hajnoczi /* Set polling begin/end callbacks for a file descriptor that has already been 347684e508cSStefan Hajnoczi * registered with aio_set_fd_handler. Do nothing if the file descriptor is 348684e508cSStefan Hajnoczi * not registered. 349684e508cSStefan Hajnoczi */ 350684e508cSStefan Hajnoczi void aio_set_fd_poll(AioContext *ctx, int fd, 351684e508cSStefan Hajnoczi IOHandler *io_poll_begin, 352684e508cSStefan Hajnoczi IOHandler *io_poll_end); 353684e508cSStefan Hajnoczi 354737e150eSPaolo Bonzini /* Register an event notifier and associated callbacks. Behaves very similarly 355737e150eSPaolo Bonzini * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks 35687f68d31SPaolo Bonzini * will be invoked when using aio_poll(). 357737e150eSPaolo Bonzini * 358737e150eSPaolo Bonzini * Code that invokes AIO completion functions should rely on this function 359737e150eSPaolo Bonzini * instead of event_notifier_set_handler. 360737e150eSPaolo Bonzini */ 361737e150eSPaolo Bonzini void aio_set_event_notifier(AioContext *ctx, 362737e150eSPaolo Bonzini EventNotifier *notifier, 363dca21ef2SFam Zheng bool is_external, 364f6a51c84SStefan Hajnoczi EventNotifierHandler *io_read, 365f6a51c84SStefan Hajnoczi AioPollFn *io_poll); 366737e150eSPaolo Bonzini 367684e508cSStefan Hajnoczi /* Set polling begin/end callbacks for an event notifier that has already been 368684e508cSStefan Hajnoczi * registered with aio_set_event_notifier. Do nothing if the event notifier is 369684e508cSStefan Hajnoczi * not registered. 370684e508cSStefan Hajnoczi */ 371684e508cSStefan Hajnoczi void aio_set_event_notifier_poll(AioContext *ctx, 372684e508cSStefan Hajnoczi EventNotifier *notifier, 373684e508cSStefan Hajnoczi EventNotifierHandler *io_poll_begin, 374684e508cSStefan Hajnoczi EventNotifierHandler *io_poll_end); 375684e508cSStefan Hajnoczi 376737e150eSPaolo Bonzini /* Return a GSource that lets the main loop poll the file descriptors attached 377737e150eSPaolo Bonzini * to this AioContext. 378737e150eSPaolo Bonzini */ 379737e150eSPaolo Bonzini GSource *aio_get_g_source(AioContext *ctx); 380737e150eSPaolo Bonzini 3819b34277dSStefan Hajnoczi /* Return the ThreadPool bound to this AioContext */ 3829b34277dSStefan Hajnoczi struct ThreadPool *aio_get_thread_pool(AioContext *ctx); 3839b34277dSStefan Hajnoczi 384*ed6e2161SNishanth Aravamudan /* Setup the LinuxAioState bound to this AioContext */ 385*ed6e2161SNishanth Aravamudan struct LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp); 386*ed6e2161SNishanth Aravamudan 3870187f5c9SPaolo Bonzini /* Return the LinuxAioState bound to this AioContext */ 3880187f5c9SPaolo Bonzini struct LinuxAioState *aio_get_linux_aio(AioContext *ctx); 3890187f5c9SPaolo Bonzini 3904e29e831SAlex Bligh /** 3914e29e831SAlex Bligh * aio_timer_new: 3924e29e831SAlex Bligh * @ctx: the aio context 3934e29e831SAlex Bligh * @type: the clock type 3944e29e831SAlex Bligh * @scale: the scale 3954e29e831SAlex Bligh * @cb: the callback to call on timer expiry 3964e29e831SAlex Bligh * @opaque: the opaque pointer to pass to the callback 3974e29e831SAlex Bligh * 3984e29e831SAlex Bligh * Allocate a new timer attached to the context @ctx. 3994e29e831SAlex Bligh * The function is responsible for memory allocation. 4004e29e831SAlex Bligh * 4014e29e831SAlex Bligh * The preferred interface is aio_timer_init. Use that 4024e29e831SAlex Bligh * unless you really need dynamic memory allocation. 4034e29e831SAlex Bligh * 4044e29e831SAlex Bligh * Returns: a pointer to the new timer 4054e29e831SAlex Bligh */ 4064e29e831SAlex Bligh static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type, 4074e29e831SAlex Bligh int scale, 4084e29e831SAlex Bligh QEMUTimerCB *cb, void *opaque) 4094e29e831SAlex Bligh { 4104e29e831SAlex Bligh return timer_new_tl(ctx->tlg.tl[type], scale, cb, opaque); 4114e29e831SAlex Bligh } 4124e29e831SAlex Bligh 4134e29e831SAlex Bligh /** 4144e29e831SAlex Bligh * aio_timer_init: 4154e29e831SAlex Bligh * @ctx: the aio context 4164e29e831SAlex Bligh * @ts: the timer 4174e29e831SAlex Bligh * @type: the clock type 4184e29e831SAlex Bligh * @scale: the scale 4194e29e831SAlex Bligh * @cb: the callback to call on timer expiry 4204e29e831SAlex Bligh * @opaque: the opaque pointer to pass to the callback 4214e29e831SAlex Bligh * 4224e29e831SAlex Bligh * Initialise a new timer attached to the context @ctx. 4234e29e831SAlex Bligh * The caller is responsible for memory allocation. 4244e29e831SAlex Bligh */ 4254e29e831SAlex Bligh static inline void aio_timer_init(AioContext *ctx, 4264e29e831SAlex Bligh QEMUTimer *ts, QEMUClockType type, 4274e29e831SAlex Bligh int scale, 4284e29e831SAlex Bligh QEMUTimerCB *cb, void *opaque) 4294e29e831SAlex Bligh { 430f186aa97SPaolo Bonzini timer_init_tl(ts, ctx->tlg.tl[type], scale, cb, opaque); 4314e29e831SAlex Bligh } 4324e29e831SAlex Bligh 433845ca10dSPaolo Bonzini /** 434845ca10dSPaolo Bonzini * aio_compute_timeout: 435845ca10dSPaolo Bonzini * @ctx: the aio context 436845ca10dSPaolo Bonzini * 437845ca10dSPaolo Bonzini * Compute the timeout that a blocking aio_poll should use. 438845ca10dSPaolo Bonzini */ 439845ca10dSPaolo Bonzini int64_t aio_compute_timeout(AioContext *ctx); 440845ca10dSPaolo Bonzini 441c1e1e5faSFam Zheng /** 442c1e1e5faSFam Zheng * aio_disable_external: 443c1e1e5faSFam Zheng * @ctx: the aio context 444c1e1e5faSFam Zheng * 445c1e1e5faSFam Zheng * Disable the further processing of external clients. 446c1e1e5faSFam Zheng */ 447c1e1e5faSFam Zheng static inline void aio_disable_external(AioContext *ctx) 448c1e1e5faSFam Zheng { 449c1e1e5faSFam Zheng atomic_inc(&ctx->external_disable_cnt); 450c1e1e5faSFam Zheng } 451c1e1e5faSFam Zheng 452c1e1e5faSFam Zheng /** 453c1e1e5faSFam Zheng * aio_enable_external: 454c1e1e5faSFam Zheng * @ctx: the aio context 455c1e1e5faSFam Zheng * 456c1e1e5faSFam Zheng * Enable the processing of external clients. 457c1e1e5faSFam Zheng */ 458c1e1e5faSFam Zheng static inline void aio_enable_external(AioContext *ctx) 459c1e1e5faSFam Zheng { 460321d1dbaSStefan Hajnoczi int old; 461321d1dbaSStefan Hajnoczi 462321d1dbaSStefan Hajnoczi old = atomic_fetch_dec(&ctx->external_disable_cnt); 463321d1dbaSStefan Hajnoczi assert(old > 0); 464321d1dbaSStefan Hajnoczi if (old == 1) { 465321d1dbaSStefan Hajnoczi /* Kick event loop so it re-arms file descriptors */ 466321d1dbaSStefan Hajnoczi aio_notify(ctx); 467321d1dbaSStefan Hajnoczi } 468c1e1e5faSFam Zheng } 469c1e1e5faSFam Zheng 470c1e1e5faSFam Zheng /** 4715ceb9e39SFam Zheng * aio_external_disabled: 4725ceb9e39SFam Zheng * @ctx: the aio context 4735ceb9e39SFam Zheng * 4745ceb9e39SFam Zheng * Return true if the external clients are disabled. 4755ceb9e39SFam Zheng */ 4765ceb9e39SFam Zheng static inline bool aio_external_disabled(AioContext *ctx) 4775ceb9e39SFam Zheng { 4785ceb9e39SFam Zheng return atomic_read(&ctx->external_disable_cnt); 4795ceb9e39SFam Zheng } 4805ceb9e39SFam Zheng 4815ceb9e39SFam Zheng /** 482c1e1e5faSFam Zheng * aio_node_check: 483c1e1e5faSFam Zheng * @ctx: the aio context 484c1e1e5faSFam Zheng * @is_external: Whether or not the checked node is an external event source. 485c1e1e5faSFam Zheng * 486c1e1e5faSFam Zheng * Check if the node's is_external flag is okay to be polled by the ctx at this 487c1e1e5faSFam Zheng * moment. True means green light. 488c1e1e5faSFam Zheng */ 489c1e1e5faSFam Zheng static inline bool aio_node_check(AioContext *ctx, bool is_external) 490c1e1e5faSFam Zheng { 491c1e1e5faSFam Zheng return !is_external || !atomic_read(&ctx->external_disable_cnt); 492c1e1e5faSFam Zheng } 493c1e1e5faSFam Zheng 49437fcee5dSFam Zheng /** 4950c330a73SPaolo Bonzini * aio_co_schedule: 4960c330a73SPaolo Bonzini * @ctx: the aio context 4970c330a73SPaolo Bonzini * @co: the coroutine 4980c330a73SPaolo Bonzini * 4990c330a73SPaolo Bonzini * Start a coroutine on a remote AioContext. 5000c330a73SPaolo Bonzini * 5010c330a73SPaolo Bonzini * The coroutine must not be entered by anyone else while aio_co_schedule() 5020c330a73SPaolo Bonzini * is active. In addition the coroutine must have yielded unless ctx 5030c330a73SPaolo Bonzini * is the context in which the coroutine is running (i.e. the value of 5040c330a73SPaolo Bonzini * qemu_get_current_aio_context() from the coroutine itself). 5050c330a73SPaolo Bonzini */ 5060c330a73SPaolo Bonzini void aio_co_schedule(AioContext *ctx, struct Coroutine *co); 5070c330a73SPaolo Bonzini 5080c330a73SPaolo Bonzini /** 5090c330a73SPaolo Bonzini * aio_co_wake: 5100c330a73SPaolo Bonzini * @co: the coroutine 5110c330a73SPaolo Bonzini * 5120c330a73SPaolo Bonzini * Restart a coroutine on the AioContext where it was running last, thus 5130c330a73SPaolo Bonzini * preventing coroutines from jumping from one context to another when they 5140c330a73SPaolo Bonzini * go to sleep. 5150c330a73SPaolo Bonzini * 5160c330a73SPaolo Bonzini * aio_co_wake may be executed either in coroutine or non-coroutine 5170c330a73SPaolo Bonzini * context. The coroutine must not be entered by anyone else while 5180c330a73SPaolo Bonzini * aio_co_wake() is active. 5190c330a73SPaolo Bonzini */ 5200c330a73SPaolo Bonzini void aio_co_wake(struct Coroutine *co); 5210c330a73SPaolo Bonzini 5220c330a73SPaolo Bonzini /** 5238865852eSFam Zheng * aio_co_enter: 5248865852eSFam Zheng * @ctx: the context to run the coroutine 5258865852eSFam Zheng * @co: the coroutine to run 5268865852eSFam Zheng * 5278865852eSFam Zheng * Enter a coroutine in the specified AioContext. 5288865852eSFam Zheng */ 5298865852eSFam Zheng void aio_co_enter(AioContext *ctx, struct Coroutine *co); 5308865852eSFam Zheng 5318865852eSFam Zheng /** 532e4370165SPaolo Bonzini * Return the AioContext whose event loop runs in the current thread. 533e4370165SPaolo Bonzini * 534e4370165SPaolo Bonzini * If called from an IOThread this will be the IOThread's AioContext. If 535e4370165SPaolo Bonzini * called from another thread it will be the main loop AioContext. 536e4370165SPaolo Bonzini */ 537e4370165SPaolo Bonzini AioContext *qemu_get_current_aio_context(void); 538e4370165SPaolo Bonzini 539e4370165SPaolo Bonzini /** 540d2b63ba8SStefan Hajnoczi * in_aio_context_home_thread: 541e4370165SPaolo Bonzini * @ctx: the aio context 542e4370165SPaolo Bonzini * 543d2b63ba8SStefan Hajnoczi * Return whether we are running in the thread that normally runs @ctx. Note 544d2b63ba8SStefan Hajnoczi * that acquiring/releasing ctx does not affect the outcome, each AioContext 545d2b63ba8SStefan Hajnoczi * still only has one home thread that is responsible for running it. 546e4370165SPaolo Bonzini */ 547d2b63ba8SStefan Hajnoczi static inline bool in_aio_context_home_thread(AioContext *ctx) 548e4370165SPaolo Bonzini { 549e4370165SPaolo Bonzini return ctx == qemu_get_current_aio_context(); 550e4370165SPaolo Bonzini } 551e4370165SPaolo Bonzini 552e4370165SPaolo Bonzini /** 55337fcee5dSFam Zheng * aio_context_setup: 55437fcee5dSFam Zheng * @ctx: the aio context 55537fcee5dSFam Zheng * 55637fcee5dSFam Zheng * Initialize the aio context. 55737fcee5dSFam Zheng */ 5587e003465SCao jin void aio_context_setup(AioContext *ctx); 55937fcee5dSFam Zheng 5604a1cba38SStefan Hajnoczi /** 561cd0a6d2bSJie Wang * aio_context_destroy: 562cd0a6d2bSJie Wang * @ctx: the aio context 563cd0a6d2bSJie Wang * 564cd0a6d2bSJie Wang * Destroy the aio context. 565cd0a6d2bSJie Wang */ 566cd0a6d2bSJie Wang void aio_context_destroy(AioContext *ctx); 567cd0a6d2bSJie Wang 568cd0a6d2bSJie Wang /** 5694a1cba38SStefan Hajnoczi * aio_context_set_poll_params: 5704a1cba38SStefan Hajnoczi * @ctx: the aio context 5714a1cba38SStefan Hajnoczi * @max_ns: how long to busy poll for, in nanoseconds 57282a41186SStefan Hajnoczi * @grow: polling time growth factor 57382a41186SStefan Hajnoczi * @shrink: polling time shrink factor 5744a1cba38SStefan Hajnoczi * 5754a1cba38SStefan Hajnoczi * Poll mode can be disabled by setting poll_max_ns to 0. 5764a1cba38SStefan Hajnoczi */ 5774a1cba38SStefan Hajnoczi void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns, 57882a41186SStefan Hajnoczi int64_t grow, int64_t shrink, 5794a1cba38SStefan Hajnoczi Error **errp); 5804a1cba38SStefan Hajnoczi 581737e150eSPaolo Bonzini #endif 582