背景

一、O_DIRECT

研究libaio之前,必须怎明白O_DIRECT标志是干嘛的。

Buffered I/O:

user space buffer—-> kernel space buffer—->disk,这就需要用户空间数据拷贝到内核缓冲区,这就浪费了CPU和内存资源了。

DIRECT I/O:

不通过kernel内存缓存,直接将用户空间数据拷贝到磁盘。

那么使用O_DIRECT,则使用DIRECT I/O,这样会减少内存拷贝次数,cpu使用率也会减少。但是这样做就需要由用户层来做内存block对齐,并且其内存大小也必须是block整数倍。如果不这样做,用write是写不进数据的,直接报错。

通过命令dumpe2fs可以查看系统文件系统的block大小。

但是这个地方我做过实验,不需要是block对齐,或block整数倍,只需要512对齐,512整数倍就可以。512是正好一个扇区的大小

————————————————
版权声明:本文为CSDN博主「beginning1126」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/beginning1126/java/article/details/17001819

说明

What Works?

AIO read and write on raw (and O_DIRECT on blockdev)
AIO read and write on files opened with O_DIRECT on ext2, ext3, jfs, xfs
What Does Not Work?

AIO read and write on files opened without O_DIRECT (i.e. normal buffered filesystem AIO). On ext2, ext3, jfs, xfs and nfs, these do not return an explicit error, but quietly default to synchronous or rather non-AIO behaviour (i.e io_submit waits for I/O to complete in these cases). For most other filesystems, -EINVAL is reported.
AIO fsync (not supported for any filesystem)
AIO read and write on sockets (doesn’t return an explicit error, but quietly defaults to synchronous or rather non-AIO behavior)
AIO read and write on pipes (reports -EINVAL)
Not all devices (including TTY) support AIO (typically return -EINVAL)
Additional functionality is available HERE

Buffered filesystem AIO, i.e. AIO read and write on files opened without O_DIRECT, on ext2, ext3, jfs, xfs, reiserfs.
Does not include AIO fsync
AIO read and write on pipes (from Chris Mason)

相关代码

fs/aio.c                     | 495 ++++++++++++++++++++++++++++++++---
 fs/block_dev.c               |   2 +
 fs/direct-io.c               |   4 +-
 fs/iomap.c                   |   7 +-
 include/linux/fs.h           |   1 +
 include/uapi/linux/aio_abi.h |   2 +
 6 files changed, 478 insertions(+), 33 deletions(-)

参考

http://lse.sourceforge.net/io/aio.html

https://oxnz.github.io/2016/10/13/linux-aio/
https://lwn.net/Articles/772555/
https://www.cnblogs.com/shiyiwen/p/5407896.html