简体   繁体   English

Linux磁盘文件AIO

[英]Linux Disk File AIO

According to this tutorial asynchronous disk file io can easily be achieved using AIO on linux, at least from a programming/api point-of-view. 根据本教程,异步磁盘文件io可以使用Linux上的AIO轻松实现,至少从编程/ api的角度来看。 But before and after this tutorial I had read a lot of posts and articles that this either can not be done or you should use libevent with a patch and many other issues. 但是在本教程之前和之后,我已经阅读了很多帖子和文章,这些都是无法完成的,或者你应该使用带有补丁和许多其他问题的libevent。 Another thing was the loop that I should have waited for a signal, but based on this tutorial I can use a callback mechanism, which obviously makes AIO much easier to use. 另一件事是我应该等待信号的循环,但基于本教程,我可以使用回调机制,这显然使AIO更容易使用。

Now, I am not a linux programmer even by a long shot I just wanted to find a straightforward way to support asynchronous disk file io on linux, learn it and add it to a async disk io library that I need for a personal project. 现在,我甚至不是一个Linux程序员,我只想找到一种简单的方法来支持Linux上的异步磁盘文件io,学习它并将其添加到个人项目所需的异步磁盘库中。 Currently I'm using overlapped io on windows and io worker threads on non-windows platforms. 目前我在非Windows平台上的Windows和io工作线程上使用重叠io。 Since the mutithreaded solution can be tricky, I wanted to replace it on linux with AIO. 由于mutithreaded解决方案可能很棘手,我想在Linux上用AIO替换它。

SO, what is wrong with AIO as described in this tutorial? 那么,本教程中描述的AIO有什么问题? Is it performance? 是性能吗? Is there a restriction on operations that can be done using AIO? 是否可以使用AIO对操作进行限制?

ps I don't care if the code will not be portable to other POSIX-compliant platforms, as long as it works on major linux distributions. ps我不关心代码是否无法移植到其他POSIX兼容平台,只要它适用于主要的Linux发行版。 And all I care about is regular disk file io. 而我所关心的只是常规磁盘文件io。

Thanks. 谢谢。

The tutorial gives an overview of asynchronous I/O in general and talks about how there is kernel support for it. 本教程概述了异步I / O,并讨论了如何为它提供内核支持。 Then it goes on to talk about posix AIO (which is the standardized API for accessing asynchronous I/O), implying that using the posix AIO API on linux, will give you access to the kernel support for AIO. 然后继续讨论posix AIO(这是用于访问异步I / O的标准化API),暗示在linux上使用posix AIO API将允许您访问AIO的内核支持。 This is not the case. 不是这种情况。

On linux, there are really two separate AIO implementations: 在linux上,实际上有两个独立的AIO实现:

  1. kernel AIO which uses io_submit() et al.) which is only supported in kernel 2.6 (or really 2.5 and there may be back-ported versions of it to 2.4. 使用io_submit()等的内核AIO,只在内核2.6中支持(或者实际上是2.5,并且可能有后端版本的2.4到2.4)。
  2. posix AIO which is a glibc feature, essentially unrelated to the kernel. posix AIO是一个glibc功能,基本上与内核无关。 It implements the posix API in terms of user-level threads making blocking disk I/O calls. 它根据用户级线程实现posix API,从而阻止磁盘I / O调用。

So, in short, if you already have a generic implementation of multiple threads for disk I/O, you might be better off using that than to use glibc's implementation (because you might have slightly more control over it). 因此,简而言之,如果您已经拥有磁盘I / O的多个线程的通用实现,那么使用它可能比使用glibc的实现更好(因为您可能对它有更多的控制权)。

If you're committed to actually use the io_submit() family of functions, you may have to do quite a lot of work to circumvent the restrictions on those functions. 如果您承诺实际使用io_submit()系列函数,则可能需要做很多工作来规避对这些函数的限制。

kernel AIO requires your files to be opened with O_DIRECT. kernel AIO 要求使用O_DIRECT打开文件。 Which in turn requires all your file offsets, read and write sizes to be aligned to blocks on the disk. 这反过来要求所有文件偏移量,读取和写入大小与磁盘上的块对齐。 This is typically fine if you're just using one large file and you can make it work very similar to the page cache in the OS. 如果您只使用一个大文件,并且可以使其与操作系统中的页面缓存非常相似,那么这通常很好。 For reading and writing arbitrary files at arbitrary offsets and lengths, it gets messy. 对于以任意偏移和长度读取和写入任意文件,它会变得混乱。

If you end up giving kernel AIO a shot, I would highly recommend looking into tying one or more eventfds to your iocbs so that you can wait on completions using epoll/select rather than having to block in io_getevents(). 如果你最终给内核AIO一个镜头,我强烈建议你考虑将一个或多个eventfds绑定到你的iocbs,这样你就可以使用epoll / select等待完成,而不必在io_getevents()中阻塞。

The Linux implementation of POSIX AIO spawns a thread for every write you do. POSIX AIO的Linux实现为您执行的每个写操作生成一个线程。 This is usually not good, and you're better off using your own worker threads to do the writes, so that you can control how many threads are in play. 这通常不好,最好使用自己的工作线程来执行写操作,这样就可以控制正在运行的线程数。 In other words, stick with what you have, AIO isn't going to buy you anything. 换句话说,坚持你拥有的东西,AIO不会给你买任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM