17

I am looking for advice on how to get efficient and high performance asynchronous IO working for my application that runs on Ubuntu Linux 14.04.

My app processes transactions and creates a file on disk/flash. As the app is progressing through transactions additional blocks are created that must be appended to the file on disk/flash. The app needs also to frequently read blocks of this file as it is processing new transactions. Each transaction might need to read a different block from this file in addition to also creating a new block that has to be appended to this file. There is an incoming queue of transactions and the app can continue to process transactions from the queue to create a deep enough pipeline of IO ops to hide the latency of read accesses or write completions on disk or flash. For a read of a block (which was put in the write queue by a previous transaction) that has not yet been written to disk/flash, the app will stall until the corresponding write completes.

I have an important performance objective – the app should incur the lowest possible latency to issue the IO operation. My app takes approximately 10 microseconds to process each transaction and be ready to issue a write to or a read from the file on disk/flash. The additional latency to issue an asynchronous read or write should be as small as possible so that the app can complete processing each transaction at a rate as close to 10 usecs per transaction as possible, when only a file write is needed.

We are experimenting with an implementation that uses io_submit to issue write and read requests. I would appreciate any suggestions or feedback on the best approach for our requirement. Is io_submit going to give us the best performance to meet our objective? What should I expect for the latency of each write io_submit and the latency of each read io_submit?

Using our experimental code (running on a 2.3 GHz Haswell Macbook Pro, Ubuntu Linux 14.04), we are measuring about 50 usecs for a write io_submit when extending the output file. This is too long and we aren't even close to our performance requirements. Any guidance to help me launch a write request with the least latency will be greatly appreciated.

2
  • Why not read from and write to a memory buffer with a definite length? This buffer should be capable of holding enough amount of data so that 90% of your reads/writes should be served in memory (no disk access) once buffer is full, flush it to disk (background job) Or if you want to save the all of the above trouble then use in memory databases like aerospike or redis etc. Commented Jan 3, 2016 at 5:35
  • We don't expect temporal locality so a cache will not help reads. The output file size will be much larger than available memory. Writing first to an in-memory buffer can help reduce the number of write IOPs but it brings other complexities of ensuring persistence in non-volatile storage, etc. Would be great if I could get some answers to my questions related to latency of io_submit calls. Commented Jan 3, 2016 at 7:58

2 Answers 2

39

Linux AIO (sometimes known as KAIO or libaio) is something of a black art where experienced practitioners know the pitfalls but for some reason it's taboo to tell someone about gotchas they don't already know. From scratching around on the web and experience I've come up with a few examples where Linux's asynchronous I/O submission via io_submit() may become (silently) synchronous, thereby turning it into a blocking (i.e. no longer fast) call:

  1. You're submitting buffered (aka non-direct) I/O. You're at the mercy of Linux's caching and your submit can go synchronous when:
    • What you're reading isn't already in the "read cache".
    • The "write cache" is full and the new write request can't be accepted until some existing writeback has been completed.
  2. You're asking for direct I/O to a file in a filesystem but for whatever reason the filesystem decides to ignore the O_DIRECT "hint" (e.g. how you submitted the I/O didn't meet O_DIRECT alignment constraints, filesystem or particular filesystem's configuration doesn't support O_DIRECT) and it chooses to silently perform buffered I/O instead, resulting in the case above.
  3. You're doing direct I/O to a file in a filesystem but the filesystem has to do a synchronous operation (such as reading metadata/updating metadata via writeback) in order to fulfill your I/O. A common example of this is issuing an "allocating write" (e.g. because you're appending/extending the end of a file or filling in an unallocated hole) and this sounds like what the questioner is doing ("appended to the file"). Some filesystems such as XFS try harder to provide good AIO behaviour but even there a user has to be careful to avoid sending certain operations to the filesystem in parallel otherwise io_submit() again will turn into a blocking call while the other operation completes. The Seastar framework contains a small lookup table of filesystem specific cases.
  4. You're submitting too much outstanding I/O. Your disk/disk controller will have a maximum number of I/O requests that can be processed at the same time and there are maximum request queue sizes for each specific device (see the /sys/block/[disk]/queue/nr_requests documentation and the un(der) documented /sys/block/[disk]/device/queue_depth) within the kernel. Making I/O requests back-up and exceed the size of the kernel queues leads to blocking.
    • If you submit I/Os that are "too large" (e.g. bigger than /sys/block/[disk]/queue/max_sectors_kb but the true limit may be something smaller like 512 KiB) they will be split up within the block layer and go on to chew up more than one request.
    • The system global maximum number of concurrent AIO requests (see the /proc/sys/fs/aio-max-nr documentation) can also have an impact but the result will be seen in io_setup() rather than io_submit().
  5. A layer in the Linux block device stack between the request and the submission to the disk has to block. For example, things like Linux software RAID (md) can make I/O requests passing through it stall while updating RAID 1 metadata on individual disks.
  6. Your submission causes the kernel to wait because:
    • It needs to take a particular lock (e.g. i_rwsem) that is in use.
    • It needs to allocate some extra memory or page something in.
  7. You're submitting I/O to a file descriptor that's not a "regular" file or a block device (e.g. your descriptor is a pipe or a socket).

The list above is not exhaustive.

With >= 4.14 kernels the RWF_NONBLOCK flag can be used to make some of the blocking scenarios above noisy. For example, when using buffering and trying to read data not yet in the page cache, the RWF_NONBLOCK flag will cause submission to fail with EAGAIN when blocking would otherwise occur. Obviously you still a) need a 4.14 (or later) kernel that supports this flag and b) have to be aware of the cases it doesn't cover. I notice there are patches that have been accepted or are being proposed to return EAGAIN in more scenarios that would otherwise block but at the time of writing (2019) RWF_NONBLOCK is not supported for buffered filesystem writes.

Alternatives

If your kernel is >=5.1, you could try using io_uring which does far better at not blocking on submission (it's an entirely different interface and was new in 2020).

References

Related:

Hopefully this post helps someone (and if does help you could you upvote it? Thanks!).

14

I speak as an author of proposed Boost.AFIO here.

Firstly, Linux KAIO (io_submit) is almost always blocking unless O_DIRECT is on and no extent allocation is required, and if O_DIRECT is on you need to be reading and writing 4Kb multiples on 4Kb aligned boundaries, else you force the device to do a read-modify-write. You therefore will gain nothing using Linux KAIO unless you rearchitect your application to be O_DIRECT and 4Kb aligned i/o friendly.

Secondly, never ever extend an output file during a write, you force an extent allocation and possibly a metadata flush. Instead fallocate the file's maximum extent to some suitably large value, and keep an internal atomic counter of the end of file. That should reduce the problem to just extent allocation which for ext4 is batched and lazy - more importantly you won't be forcing a metadata flush. That should mean KAIO on ext4 will be async most of the time, but unpredictably will synchronise as it flushes delayed allocations to the journal.

Thirdly, the way I'd probably approach your problem is to use atomic append (O_APPEND) without O_DIRECT nor O_SYNC, so what you do is append updates to an ever growing file in the kernel's page cache which is very fast and concurrency safe. You then, from time to time, garbage collect what data in the log file is stale and whose extents can be deallocated using fallocate(FALLOC_FL_PUNCH_HOLE) so physical storage doesn't grow forever. This pushes the problem of coalescing writes to storage onto the kernel where much effort has been spent on making this fast, and because it's an always forward progress write you will see writes hit physical storage in a fairly close order to the sequence they were written which makes power loss recovery straightforward. This latter option is how databases do it and indeed journalling filing systems do it, and despite the likely substantial redesign of your software you'll need to do this algorithm has been proven the best balance of latency to durability in a general purpose problem case.

In case all the above seems like a lot of work, the OS already provides all of the three techniques rolled together into a highly tuned implementation which is better known as memory maps: 4Kb aligned i/o, O_DIRECT, never extending the file, all async i/o. On a 64 bit system, simply fallocate the file to a very large amount and mmap it into memory. Read and write as you see fit. If your i/o patterns confuse the kernel page algorithms which can happen, you may need a touch of madvise() here and there to encourage better behaviour. Less is more with madvise(), trust me.

An awful lot of people try to duplicate mmaps using various O_DIRECT algorithms without realising mmaps already can do everything you need. I'd suggest exploring those first, if Linux won't behave try FreeBSD which has a much more predictable file i/o model, and only then delve into the realm of rolling your own i/o solution. Speaking as someone who does these all day long, I'd strongly recommend you avoid them whenever possible, filing systems are pits of devils of quirky and weird behaviour. Leave the never ending debugging to someone else.

6
  • thanks very much for your informative response. I need some time to digest your suggestions and run experiments. If I have follow up questions, I will post back here. Commented Jan 4, 2016 at 1:43
  • Also look up any posts on SO about Linux KAIO by @Arvid, he's as close to the authority on SO about it. He's the same guy who wrote blog.libtorrent.org/2012/10/asynchronous-disk-io Commented Jan 4, 2016 at 15:02
  • Hm. Doesn't mmap introduce extra latency on first write (page fault + find free page + map into process VM space)? Is it really the best way to extend a file if I am trying to keep up with a high-bandwidth stream of data? This is not a hypothetical problem for me at the moment :). What I really want, I think, is to preallocate a double-buffer and overlap filling one with flushing the other to disk...
    – Nemo
    Commented Nov 26, 2018 at 22:12
  • With the right poking of the kernel, you can create a map which auto-extends itself across all processes into an address space reservation. LLFIO does the right poking, and it works well on the systems which support said poking. This isn't to say double buffering may not suit your problem better, it really depends on your storage, your problem domain etc. But please always give mmaps a chance first. They always receive tuning and love from devs. Other stuff like O_DIRECT may not. Commented Nov 28, 2018 at 22:06
  • 2
    Linux back in 2008 had a fairly slow mmap. Recent Linux has an amazing mmap, it often surprises me how quick and constant time it is. That said, Linus is not wrong, a TLB shootdown is always expensive. mmaps basically let one pay tons of cost in cold code in exchange for usually least cost in hot code, if you get everything right. But it's vastly easier to get everything right with mmaps than your own custom userspace O_DIRECT filesystem algorithm. Those are fiendishly hard to get right, and performant on any arbitrary storage. The kernel does all that tuning and debugging for you! Commented Dec 4, 2018 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.