Linux 上的 POSIX AIO 和 libaio 之间的区别? [英] Difference between POSIX AIO and libaio on Linux?

查看:14
本文介绍了Linux 上的 POSIX AIO 和 libaio 之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎理解的是:

POSIX AIO API 在 <aio.h> 中建立原型,您可以使用 librt(-lrt) 链接您的程序,而 libaio> 中的 API,并且您的程序与 libaio (-laio) 链接.

POSIX AIO APIs are prototyped in <aio.h> and you link your program with librt(-lrt), while the libaio APIs in <libaio.h> and your program is linked with libaio (-laio).

我想不通的地方:

1.内核对这两种方法的处理方式是否不同?

1.Does the kernel handle the either of these methods differently?

2.O_DIRECT 标志是否必须使用它们中的任何一个?

2.Is the O_DIRECT flag mandatory for using either of them?

这篇文章所述,libaio在没有O_DIRECT 使用 libaio 时.好的,明白了:

As mentioned in this post, libaio works fine without O_DIRECT when using libaio.Okay,understood but:

根据 R.Love 的 Linux System Programming 一书,Linux 支持 aio(我假设是 POSIX AIO)对常规文件使用 O_DIRECT 打开.但是我编写的一个小程序(使用 aio.h,与 -lrt 链接)调用 aio_write 在没有 O_DIRECT<的情况下打开的文件/code> 标志可以正常工作.

According to R.Love's Linux System Programming book, Linux supports aio (which I assume is POSIX AIO) on regular files only if opened with O_DIRECT.But a small program that I wrote (using aio.h,linked with -lrt) that calls aio_write on a file opened without the O_DIRECT flag works without issues.

推荐答案

在 linux 上,这两种 AIO 实现根本不同.

On linux, the two AIO implementations are fundamentally different.

POSIX AIO 是一个用户级实现,它在多个线程中执行正常的阻塞 I/O,因此给人一种 I/O 是异步的错觉.这样做的主要原因是:

The POSIX AIO is a user-level implementation that performs normal blocking I/O in multiple threads, hence giving the illusion that the I/Os are asynchronous. The main reason to do this is that:

  1. 它适用于任何文件系统
  2. 它(基本上)适用于任何操作系统(请记住,gnu 的 libc 是可移植的)
  3. 它适用于启用缓冲的文件(即未设置 O_DIRECT 标志)

主要缺点是您的队列深度(即您在实践中可以拥有的未完成操作的数量)受您选择拥有的线程数量的限制,这也意味着一个磁盘上的缓慢操作可能会阻塞一个操作转到不同的磁盘.它还影响内核和磁盘调度程序看到的 I/O(或多少).

The main drawback is that your queue depth (i.e. the number of outstanding operations you can have in practice) is limited by the number of threads you choose to have, which also means that a slow operation on one disk may block an operation going to a different disk. It also affects which I/Os (or how many) is seen by the kernel and the disk scheduler as well.

内核 AIO(即 io_submit() 等)是内核对异步 I/O 操作的支持,其中 io 请求实际上在内核中排队,按您拥有的任何磁盘调度程序排序,大概是其中的一些作为异步操作(使用 TCQ 或 NCQ)转发(以人们希望的某种最佳顺序)到实际磁盘.这种方法的主要限制是,并非所有文件系统都能很好地或根本无法使用异步 I/O(并且可能会退回到阻塞语义),必须使用 O_DIRECT 打开文件,这对I/O 请求.如果您无法使用 O_DIRECT 打开文件,它可能仍然有效",因为您会获得正确的数据,但它可能不是异步完成的,而是退回到阻塞语义.

The kernel AIO (i.e. io_submit() et.al.) is kernel support for asynchronous I/O operations, where the io requests are actually queued up in the kernel, sorted by whatever disk scheduler you have, presumably some of them are forwarded (in somewhat optimal order one would hope) to the actual disk as asynchronous operations (using TCQ or NCQ). The main restriction with this approach is that not all filesystems work that well or at all with async I/O (and may fall back to blocking semantics), files have to be opened with O_DIRECT which comes with a whole lot of other restrictions on the I/O requests. If you fail to open your files with O_DIRECT, it may still "work", as in you get the right data back, but it probably isn't done asynchronously, but is falling back to blocking semantics.

还要记住,io_submit() 在某些情况下实际上可以在磁盘上阻塞.

Also keep in mind that io_submit() can actually block on the disk under certain circumstances.

这篇关于Linux 上的 POSIX AIO 和 libaio 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆