Linux下异步IO是比较新的内核里面才有的,异步io的好处可以参考这里.
但是文章中aio_*系列的调用是glibc提供的,是glibc用线程+阻塞调用来模拟的,性能很差,千万千万不要用。

我们今天要说的是真正的原生的异步IO接口. 由于这几个系统调用glibc没有提供相应的封装,所以libaio来救急了:

libaio项目: http://oss.oracle.com/projects/libaio-oracle/

This is a library for accessing the new AIO system calls (asynchronous i/o) for the Linux kernel. It is a thin, state-keeping wrapper that conforms to the Single Unix Specification for aio_read, aio_write, aio_error, aio_return and aio_suspend functions, and also implements lio_listio and aio_reap for batch processing.

This library requires a kernel with the new AIO code and a recent version of the libaio userspace library.

libaio提供了4个函数: io_cancal, io_destroy, io_getevents, io_setup,io_submit来提供服务,具体的可以参看man io_*

淘宝的雕梁同学写了这篇文章介绍了libaio如何和event一起高效工作:看这里

使用很简单的,在RHEL 5U4上只要安装下开发包就好:

sudo yum install libaio-devel

其实大家会比较关心性能。 fio 测试工具支持同步(pread/pwrite)和异步(libaio)的测试,那我们比较下:

为了减低底层硬件(如raid)cache的影响,我们用总共2G左右的文件做随机读写操作, 完全随机的读写,块大小128K, directio绕过OS page buffer系统, 总共跑1分钟。

在异步io操作的时候只用一个进程保持io队列的长度为16, 同步io开16个进程,保持同样的队列这样会公平些。

测试机器配置:

$ summary
# Aspersa System Summary Report ##############################
     Release | Red Hat Enterprise Linux Server release 5.4 (Tikanga)
      Kernel | 2.6.18-164.el5
  Processors | physical = 2, cores = 8, virtual = 16, hyperthreading = yes
      Speeds | 16x2261.053
      Models | 16xIntel(R) Xeon(R) CPU E5520 @ 2.27GHz
      Caches | 16x8192 KB
# Memory #####################################################
       Total | 23.53G
...

先开始异步IO的测试:

cat aio-bench
[global]
ioengine=libaio
direct=1
rw=randrw
bs=128k
directory=.
ioscheduler=deadline
time_based
runtime=60
 
[libaio.dat]
size=2g
iodepth=16
 
sudo fio aio-bench 
libaio.dat: (g=0): rw=randrw, bs=128K-128K/128K-128K, ioengine=libaio, iodepth=16
fio 1.50.2
Starting 1 process
Jobs: 1 (f=1): [m] [100.0% done] [31462K/34595K /s] [240 /263  iops] [eta 00m:00s]
libaio.dat: (groupid=0, jobs=1): err= 0: pid=25892
  read : io=1795.4MB, bw=30575KB/s, iops=238 , runt= 60129msec
    slat (usec): min=14 , max=130031 , avg=25.13, stdev=1084.85
    clat (usec): min=178 , max=1959.4K, avg=66231.82, stdev=154044.70
     lat (msec): min=1 , max=1959 , avg=66.26, stdev=154.04
    bw (KB/s) : min=22721, max=95552, per=100.66%, avg=30775.78, stdev=7306.36
  write: io=1836.8MB, bw=31280KB/s, iops=244 , runt= 60129msec
    slat (usec): min=12 , max=55571 , avg=18.50, stdev=458.33
    clat (usec): min=252 , max=377818 , avg=658.98, stdev=7612.29
     lat (usec): min=266 , max=377832 , avg=677.73, stdev=7626.25
    bw (KB/s) : min=20223, max=94275, per=100.74%, avg=31511.06, stdev=7334.59
  cpu          : usr=0.13%, sys=0.72%, ctx=28484, majf=0, minf=24
  IO depths    : 1=0.1%, 2=0.1%, 4=0.1%, 8=0.1%, 16=99.9%, 32=0.0%, >=64=0.0%
     submit    : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
     complete  : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.1%, 32=0.0%, 64=0.0%, >=64=0.0%
     issued r/w/d: total=14363/14694/0, short=0/0/0
     lat (usec): 250=0.01%, 500=46.44%, 750=1.91%, 1000=0.39%
     lat (msec): 2=1.55%, 4=0.39%, 10=3.07%, 20=5.66%, 50=20.97%
     lat (msec): 100=14.42%, 250=4.46%, 500=0.19%, 750=0.02%, 1000=0.01%
     lat (msec): 2000=0.51%
 
Run status group 0 (all jobs):
   READ: io=1795.4MB, aggrb=30575KB/s, minb=31309KB/s, maxb=31309KB/s, mint=60129msec, maxt=60129msec
  WRITE: io=1836.8MB, aggrb=31279KB/s, minb=32030KB/s, maxb=32030KB/s, mint=60129msec, maxt=60129msec
 
Disk stats (read/write):
  sda: ios=15108/20465, merge=4704/4206, ticks=969251/16763, in_queue=989615, util=99.84%
 
 
#通过strace可以看到
...
26112 io_submit(47710156091392, 1, {{(nil), 0, 1, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 1, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 1, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 1, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 1, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 1, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 0, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 0, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 0, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 0, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 1, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 0, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 0, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 0, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 0, 0, 9}}) = 1
26112 io_submit(47710156091392, 1, {{(nil), 0, 0, 0, 9}}) = 1
26112 io_getevents(47710156091392, 1, 1, {{(nil), 0x18ad9f00, 131072, 0}}, NULL) = 1
证明确实aio在工作。

再看下同步IO的测试:

cat psync-bench
[global]
ioengine=psync
direct=1
rw=randrw
bs=128k
directory=.
ioscheduler=deadline
time_based
runtime=60
 
[file1]
numjobs=16
 
sudo fio psync-bench
file1: (g=0): rw=randrw, bs=128K-128K/128K-128K, ioengine=psync, iodepth=1
...
file1: (g=0): rw=randrw, bs=128K-128K/128K-128K, ioengine=psync, iodepth=1
fio 1.50.2
Starting 16 processes
Jobs: 16 (f=16): [mmmmmmmmmmmmmmmm] [100.0% done] [31854K/31723K /s] [243 /242  iops] [eta 00m:00s]
file1: (groupid=0, jobs=1): err= 0: pid=26145
  read : io=118144KB, bw=1967.1KB/s, iops=15 , runt= 60036msec
    clat (msec): min=3 , max=1531 , avg=64.36, stdev=133.07
     lat (msec): min=3 , max=1531 , avg=64.36, stdev=133.07
    bw (KB/s) : min=   84, max= 6083, per=7.02%, avg=2205.14, stdev=807.87
  write: io=121984KB, bw=2031.9KB/s, iops=15 , runt= 60036msec
    clat (usec): min=264 , max=153817 , avg=660.30, stdev=6100.86
     lat (usec): min=265 , max=153818 , avg=660.53, stdev=6100.88
    bw (KB/s) : min=   84, max= 5765, per=7.32%, avg=2311.53, stdev=1256.97
  cpu          : usr=0.00%, sys=0.05%, ctx=1877, majf=0, minf=32
  IO depths    : 1=100.0%, 2=0.0%, 4=0.0%, 8=0.0%, 16=0.0%, 32=0.0%, >=64=0.0%
     submit    : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
     complete  : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
     issued r/w/d: total=923/953/0, short=0/0/0
     lat (usec): 500=46.75%, 750=2.40%, 1000=0.43%
     lat (msec): 2=1.01%, 4=0.16%, 10=2.99%, 20=4.69%, 50=20.31%
     lat (msec): 100=15.41%, 250=5.28%, 500=0.21%, 2000=0.37%
...
省去中间15个文件
...
Run status group 0 (all jobs):
   READ: io=1844.0MB, aggrb=31411KB/s, minb=1699KB/s, maxb=2186KB/s, mint=60001msec, maxt=60114msec
  WRITE: io=1852.9MB, aggrb=31562KB/s, minb=1620KB/s, maxb=2325KB/s, mint=60001msec, maxt=60114msec
 
Disk stats (read/write):
  sda: ios=15756/20708, merge=4553/4159, ticks=979074/16483, in_queue=999501, util=99.83%
 
#通过strace可以看到
26195 pwrite(20, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 131072, 5242880) = 131072
26195 pwrite(20, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 131072, 23855104) = 131072
26195 pwrite(20, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 131072, 7864320) = 131072
26195 pread(20,  <unfinished ...>
证明确实是pread/pwrite操作的。

从上面的数据我们可以看出,最后的结果都是设备的IO能力将近100%的被使用,二个测试的吞吐量差不多。 但是如果从当个进程提交的吞吐量来看的话,一个aio进程相当于16个同步进程的能力,也就是说如果大家都是一个进程在干活的话,那么aio的能力要比同步的好太多了。

大家玩得开心!

Post Footer automatically generated by wp-posturl plugin for wordpress.

Related posts:

  1. Linux下Fio和Blktrace模拟块设备的访问模式

Categories:Linux, 工具介绍, 源码分析, 调优Tags:libaio, posfix

Linux下异步IO(libaio)的使用以及性能相关推荐

  1. linux下异步IO的简单例子

    首先,贴一下异步IO中用的的一些结构体,因为平常很少用,整理起来方便查看. aio.h中的struct aiocb struct aiocb{int aio_fildes; /* File desri ...

  2. Linux 原生异步 IO 原理与使用

    目录 什么是异步 IO? Linux 原生 AIO 原理 Linux 原生 AIO 使用 什么是异步 IO? 异步 IO:当应用程序发起一个 IO 操作后,调用者不能立刻得到结果,而是在内核完成 IO ...

  3. Linux下磁盘IO读写测试工具-FIO详解

    FIO简介 FIO是Linux下开源的一款IOPS测试工具,主要用来对磁盘进行压力测试和性能验证. 它可以产生许多线程或进程来执行用户特定类型的I/O操作,通过编写作业文件(类似于k8s的yaml)或 ...

  4. Linux下面的IO模型

    1. Linux下的五种I/O模型 阻塞I/O模型: 一直阻塞      应用程序调用一个IO函数,导致应用程序阻塞,等待数据准备好. 如果数据没有准备好,一直等待-.数据准备好了,从内核拷贝到用户空 ...

  5. linux性能测试cpu内存io,Linux下磁盘IO性能测试工具介绍

    近来想了解一下开发环境的IO性能,分别用dd/orion/iozone/bonnie++四种工具测试了一下 开发环境系统配置如下: Intel SR1625 server, 2 CPU, 32GB内存 ...

  6. linux支持异步io吗,Linux 异步IO

    io_submit.io_setup和io_getevents示例 [摘要:注:本宣布正在 io_submit.io_setup战io_getevents战LINUX上的AIO体系挪用.那有一个特别很 ...

  7. windows下异步IO一

    介绍 简单讲解下我们程序进行IO的过程,当线程进行一个同步的设备IO请求时,他会被挂起,直到设备完成IO请求,返回给阻塞线程,线程激活继续处理.当进行一个异步的设备IO请求时,该线程可以先去做其他事, ...

  8. linux测试磁盘io脚本,脚本分享:Linux下磁盘io测试

    这两天在测试Dell的PS6000存储,在linux下也懒得去找测试工具了,就直接使用dd命令进行I/O测试,但因为需要测试的数据很多,从512b到4M的块,每一档都要进行读写的测试,这要是人肉来做, ...

  9. Linux下多路复用IO接口epoll/select/poll的区别

    select比epoll效率差的原因:select是轮询,epoll是触发式的,所以效率高. Select: 1.Socket数量限制:该模式可操作的Socket数由FD_SETSIZE决定,内核默认 ...

最新文章

  1. Intent四个重要属性
  2. 【知识发现】隐语义模型LFM算法python实现(一)
  3. Tableau实战系列浏览 Tableau 环境(二) -工作区域
  4. cpu倍频模式怎么调_CPU频率被锁定到800mhz怎么办?
  5. myisam数据表根据frm文件恢复数据表
  6. [剑指offer]面试题8:旋转数组的最小数字
  7. layui中table监听单元格_最全总结 | 聊聊 Python 办公自动化之 PPT(中)
  8. 四元数姿态解算c语言例程_(21)用四元数插值来对齐IMU和图像帧
  9. C++每日练笔之时间类(基类)
  10. Android APK反编译步骤
  11. 对instance进行二次封装
  12. php 执行shell命令的函数
  13. Python命令行参数选择
  14. 保留正常工作的环境,等自己的搞好后再替换
  15. 工业机器人技术试题_《工业机器人技术基础》课程试卷A卷
  16. Windows API函数大全(精心总结)
  17. java对接银联商务扫码支付(银联商务扫码支付文档)
  18. 每日新闻丨​百度地图发布语音定制功能​;Windows 10高CPU使用率问题已解
  19. Win10/11 更改电脑用户名的方法
  20. hive数据文件备份

热门文章

  1. 边框样式(border-style)、边框圆角(border-radius)以及媒体查询(@media)
  2. python 通达信板块_python 适用通达信
  3. XC3071充电IC(耐高压,带OVP)
  4. 业余草分享面试题,JVM结构、GC工作机制详解
  5. win凭据添加计算机名,使用Windows凭据管理自动登录局域网的方法
  6. AIWIN 心电图智能诊断Baseline【线上0.719】
  7. 新随笔--读书笔记《学会提问》:学习提出好问题的思维方法
  8. 漂亮的css网站js资源无限下载
  9. 网易有道词典总是提示网络连接失败--解决办法
  10. 妹子尚且如此! 少年努力吧