Adding userspace_libaio_reap option
PATCH v2 1/1 Adding userspace_libaio_reap option
When a single thread is reading from a libaio io_context_t object in a non-blocking polling manner (that is, with the minimum number of events to return being 0), then it is possible to safely read events directly from user-space, taking advantage of the fact that the io_context_t object is a pointer to memory with a certain layout. This patch adds an option, userspace_libaio_reap, which allows reading events in this manner when the libaio engine is used.
You can observe its effect by setting iodepth_batch_complete=0 and seeing the change in distribution of system/user time based on whether this new flag is set. If userspace_libaio_reap=1, then busy polling takes place in userspace, and there is a larger amount of usr CPU. If userspace_libaio_reap=0 (the default), then there is a larger amount of sys CPU from the polling in the kernel.
Polling from a queue in this manner is several times faster. In my testing, it took less than an eighth as much time to execute a polling operation in user-space than with the io_getevents syscall.
--- HOWTO | 7 +++++++ engines/libaio.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- fio.h | 2 ++ options.c | 9 +++++++++ 4 files changed, 69 insertions(+), 1 deletions(-)
---
Version 2 updates the HOWTO file and uses the necessary read barrier. I am not sure if this needs a flush_dcache_page call in the kernel for correctness. A future version should consider cleaning up the option interface and consider moving the reaping code to libaio.
HOWTO +7 -0
@@ -1187,6 +1187,13 @@ uid=int Instead of running as the invoking user, set the user ID to gid=int Set group ID, see uid. +userspace_libaio_reap=bool Normally, with the libaio engine in use, fio + will use the io_getevents system call to reap newly returned + events. With this flag turned on, the AIO ring will be read + directly from user-space to reap events. The reaping mode is + only enabled when polling for a minimum of 0 events (eg when + iodepth_batch_complete=0). + 6.0 Interpreting the output ---------------------------
engines/libaio.c +51 -1
64 lines not on this page · raw message
fio.h +2 -0
@@ -413,6 +413,8 @@ struct thread_options {unsigned int gid;unsigned int sync_file_range; + +unsigned int userspace_libaio_reap; };#define FIO_VERROR_SIZE 128
options.c +9 -0
@@ -2069,6 +2069,15 @@ static struct fio_option options[FIO_MAX_OPTS] = { .off1 = td_var_offset(gid), .help = "Run job with this group ID", }, +#ifdef FIO_HAVE_LIBAIO + { + .name = "userspace_libaio_reap", + .type = FIO_OPT_BOOL, + .off1 = td_var_offset(userspace_libaio_reap), + .help = "When using the libaio engine with iodepth_batch_complete=0, enable userspace reaping", + .def = "0", + }, +#endif { .name = NULL, },
Signature · 1 line
1.7.3.1
Quoted 5 lines · Dan Ehrenberg
engines/libaio.c +0
-1@@ -66,7 +107,16 @@ static int fio_libaio_getevents(struct thread_data *td, unsigned int min,int r, events =0 ; do { - r = io_getevents(ld->aio_ctx, actual_min, max, ld->aio_events + events, t);
engines/libaio.c static int fio_libaio_getevents(struct thread_data *td, unsigned int min, · line 119
Quoted 10 lines
engines/libaio.c +10
-0+if (td->o.userspace_libaio_reap ==1 + && actual_min ==0 + && ((struct aio_ring *)(ld->aio_ctx))->magic + ==AIO_RING_MAGIC ) { + r =user_io_getevents (ld->aio_ctx, max, + ld->aio_events + events); + }else { + r =io_getevents (ld->aio_ctx, actual_min, + max, ld->aio_events + events, t); + }
One question here - why depend on actual_min == 0? The check is practically free, would not hurt to do the user_io_getevents() first and punt to io_getevents() afterwards if need be.
Signature · 1 line
Jens Axboe
Quoted 4 lines · Jens Axboe
Quoted 3 lines · Dan Ehrenberg
engines/libaio.c +0
-0@@ -66,7 +107,16 @@ static int fio_libaio_getevents(struct thread_data *td, unsigned int min, int r, events = 0;
Quoted 16 lines
engines/libaio.c static int fio_libaio_getevents(struct thread_data *td, unsigned int min, · line 119
Quoted 12 lines
engines/libaio.c +10
-1do { - r = io_getevents(ld->aio_ctx, actual_min, max, ld->aio_events + events, t); + if (td->o.userspace_libaio_reap == 1 + && actual_min == 0 + && ((struct aio_ring *)(ld->aio_ctx))->magic + == AIO_RING_MAGIC) { + r = user_io_getevents(ld->aio_ctx, max, + ld->aio_events + events); + } else { + r = io_getevents(ld->aio_ctx, actual_min, + max, ld->aio_events + events, t); + }One question here - why depend on
actual_min== 0? The check is practically free, would not hurt to do theuser_io_getevents()first and punt toio_getevents()afterwards if need be.
I guess I don't need to depend on that, since you'll never be calling both user_io_getevents() and sys_io_getevents() at the same time still. It was just paranoia, really--if I'm not grabbing the ring_lock spinlock that exists in the kernel, then I'll be really safe if I never run code that wants to grab it. But I guess the condition is not really necessary since the whole thing is single-threaded.
Quoted 9 lines
--
Jens Axboe--
To unsubscribe from this list: send the line "unsubscribe fio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 22 lines · Daniel Ehrenberg
Quoted 4 lines · Jens Axboe
Quoted 3 lines · Dan Ehrenberg
engines/libaio.c +0
-0@@ -66,7 +107,16 @@ static int fio_libaio_getevents(struct thread_data *td, unsigned int min,int r, events =0 ;Quoted 16 lines
engines/libaio.c static int fio_libaio_getevents(struct thread_data *td, unsigned int min, · line 119
Quoted 12 lines
engines/libaio.c +10
-1do { - r = io_getevents(ld->aio_ctx, actual_min, max, ld->aio_events + events, t); + if (td->o.userspace_libaio_reap == 1 + && actual_min == 0 + && ((struct aio_ring *)(ld->aio_ctx))->magic + == AIO_RING_MAGIC) { + r = user_io_getevents(ld->aio_ctx, max, + ld->aio_events + events); + } else { + r = io_getevents(ld->aio_ctx, actual_min, + max, ld->aio_events + events, t); + }One question here - why depend on
actual_min== 0? The check is practically free, would not hurt to do theuser_io_getevents()first and punt toio_getevents()afterwards if need be.
Quoted 6 lines
I guess I don't need to depend on that, since you'll never be calling both
user_io_getevents()andsys_io_getevents()at the same time still. It was just paranoia, really--if I'm not grabbing thering_lockspinlock that exists in the kernel, then I'll be really safe if I never run code that wants to grab it. But I guess the condition is not really necessary since the whole thing is single-threaded.
I think it should be safe.
FWIW, I committed this version, so just base further improves on fio git. I also half-assed a patch to add the sub-option support, and converted this one. So you should use
ioengine=libaio:userspace_reap
to set it now. I say half-assed, since it's only really supported for ioengine at the moment, and it's not included in --cmdhelp output. But it should work, hopefully.
Please give it a spin :-)
Signature · 1 line
Jens Axboe