linux-aio: add IO_CMD_FDSYNC command support
PATCH v4 1/1 linux-aio: add IO_CMD_FDSYNC command support
From: Prasad Pandit <[email protected]>
Libaio defines IO_CMD_FDSYNC command to sync all outstanding asynchronous I/O operations, by flushing out file data to the disk storage.
Enable linux-aio to submit such aio request. This helps to reduce latency induced via pthread_create calls by thread-pool (aio=threads).
--- block/file-posix.c | 7 +++++++ block/linux-aio.c | 21 ++++++++++++++++++++- include/block/raw-aio.h | 1 + 3 files changed, 28 insertions(+), 1 deletion(-)
v4: New boolean field to indicate if aio_fdsync is available or not.
It is set at file open time and checked before AIO_FLUSH call.
- https://lists.nongnu.org/archive/html/qemu-devel/2024-03/msg03701.html
block/file-posix.c +7 -0
@@ -159,6 +159,7 @@ typedef struct BDRVRawState { bool has_discard:1; bool has_write_zeroes:1; bool use_linux_aio:1; + bool has_laio_fdsync:1; bool use_linux_io_uring:1;int page_cache_inconsistent;/* errno from fdatasync failure */ bool has_fallocate;
@@ -718,6 +719,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options, ret = -EINVAL ; goto fail; } + s->has_laio_fdsync =laio_has_fdsync (s->fd);#else if (s->use_linux_aio) { error_setg(errp, "aio=native was specified, but is not supported "
@@ -2599,6 +2601,11 @@ static int coroutine_fn raw_co_flush_to_disk(BlockDriverState *bs)if (raw_check_linux_io_uring (s)) {return luring_co_submit (bs, s->fd,0 ,NULL ,QEMU_AIO_FLUSH ); } +#endif +#ifdef CONFIG_LINUX_AIO +if (s->has_laio_fdsync &&raw_check_linux_aio (s)) { +return laio_co_submit (s->fd,0 ,NULL ,QEMU_AIO_FLUSH ,0 ); + }#endif return raw_thread_pool_submit (handle_aiocb_flush, &acb); }
block/linux-aio.c +20 -1
@@ -384,6 +384,9 @@ static int laio_do_submit(int fd, struct qemu_laiocb *laiocb, off_t offset,case QEMU_AIO_READ :io_prep_preadv (iocbs, fd, qiov->iov, qiov->niov, offset);break ; +case QEMU_AIO_FLUSH : +io_prep_fdsync (iocbs, fd); +break ;/* Currently Linux kernel does not support other operations */ default : fprintf(stderr, "%s: invalid AIO request type 0x%x.\n",
@@ -412,7 +415,7 @@ int coroutine_fn laio_co_submit(int fd, uint64_t offset, QEMUIOVector *qiov,AioContext *ctx =qemu_get_current_aio_context ();struct qemu_laiocb laiocb = { .co =qemu_coroutine_self (), - .nbytes = qiov->size, + .nbytes = qiov ? qiov->size :0 , .ctx =aio_get_linux_aio (ctx), .ret = -EINPROGRESS , .is_read = (type ==QEMU_AIO_READ ),
@@ -486,3 +489,19 @@ void laio_cleanup(LinuxAioState *s) }g_free (s); } + +bool laio_has_fdsync (int fd) +{ +struct iocb cb; +struct iocb *cbs[] = {&cb,NULL }; + +io_context_t ctx =0 ; +io_setup (1 , &ctx); + +/* check if host kernel supports IO_CMD_FDSYNC */ +io_prep_fdsync (&cb, fd); +int ret =io_submit (ctx,1 , cbs); + +io_destroy (ctx); +return (ret == -EINVAL ) ? false : true; +}
include/block/raw-aio.h +1 -0
@@ -60,6 +60,7 @@ void laio_cleanup(LinuxAioState *s); int coroutine_fn laio_co_submit(int fd, uint64_t offset, QEMUIOVector *qiov,int type,uint64_t dev_max_batch); +bool laio_has_fdsync (int );void laio_detach_aio_context (LinuxAioState *s,AioContext *old_context);void laio_attach_aio_context (LinuxAioState *s,AioContext *new_context);#endif
Signature · 1 line
2.44.0
Quoted 17 lines · Prasad Pandit
From: Prasad Pandit <[email protected]>
Libaio defines
IO_CMD_FDSYNCcommand to sync all outstanding asynchronous I/O operations, by flushing out file data to the disk storage.Enable linux-aio to submit such aio request. This helps to reduce latency induced via
pthread_createcalls by thread-pool (aio=threads).Signed-off-by Prasad Pandit <[email protected]>--- block/file-posix.c | 7 +++++++ block/linux-aio.c | 21 ++++++++++++++++++++- include/block/raw-aio.h | 1 + 3 files changed, 28 insertions(+), 1 deletion(-)
Quoted 3 lines
v4: New boolean field to indicate if
aio_fdsyncis available or not.
It is set at file open time and checked beforeAIO_FLUSHcall.
- https://lists.nongnu.org/archive/html/qemu-devel/2024-03/msg03701.html
Am 14.03.2024 um 12:16 hat Prasad Pandit geschrieben:
Quoted 11 lines
From: Prasad Pandit <[email protected]>
Libaio defines
IO_CMD_FDSYNCcommand to sync all outstanding asynchronous I/O operations, by flushing out file data to the disk storage.Enable linux-aio to submit such aio request. This helps to reduce latency induced via
pthread_createcalls by thread-pool (aio=threads).Signed-off-by Prasad Pandit <[email protected]>
As we discussed in chat, it would be good to be more detailed about the scenario that we're really concerned about here. The commit message above sounds like submitting fdsync takes too long, but the real concern seems to be about the effect that creating and destroying a thread has on a vcpu by adding a TLB flush. Describing the mechanisms, the sequence of operations that happen and the problem this causes in more detail would make the commit message a lot more useful.
Quoted 22 lines
block/file-posix.c | 7 +++++++ block/linux-aio.c | 21 ++++++++++++++++++++- include/block/raw-aio.h | 1 + 3 files changed, 28 insertions(+), 1 deletion(-)v4: New boolean field to indicate if
aio_fdsyncis available or not.
It is set at file open time and checked beforeAIO_FLUSHcall.
- https://lists.nongnu.org/archive/html/qemu-devel/2024-03/msg03701.htmlblock/file-posix.c +1
-0@@ -159,6 +159,7 @@ typedef struct BDRVRawState { bool has_discard:1; bool has_write_zeroes:1; bool use_linux_aio:1; + bool has_laio_fdsync:1; bool use_linux_io_uring:1;int page_cache_inconsistent;/* errno from fdatasync failure */ bool has_fallocate;
block/file-posix.c static int raw_open_common(BlockDriverState *bs, QDict *options, · line 722
Quoted 4 lines
block/file-posix.c +1
-0@@ -718,6 +719,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options, ret = -EINVAL ; goto fail; } + s->has_laio_fdsync =laio_has_fdsync (s->fd);
I think this should be conditional on s->use_linux_aio. No point in probing it if we'll never call it anyway.
Kevin