测试磁盘随机读取时间

写了一个测试磁盘寻道(+旋转)时间的程序,方法如下:创建一个大文件(如果太小,文件所有内容都在一个磁道上),使O_DIRECT打开文件,随机选择一个位置(4k的倍数),读取4K(文件系统块)大小的数据。

 

测试程序分别执行110100100010000次随机读文件操作,结果发现在1000次以内,消耗的时间总是1-3ms,超过10000才会略有大的增长,但也在1s范围内。发现DIRECTIO根本没有生效,百思不得其解。

 

仔细阅读了man手册上关于O_DIRECT的说明:

Try to minimize cache effects of the I/O to and from this file.  In general this  will  degrade  performance, but it is useful in special situations, such as when applications do their own caching.  File I/O is done directly to/from user space buffers.  The I/O is synchronous,  i.e.,  at  the  completion  of  a read(2)  or  write(2), data is guaranteed to have been transferred.  Under Linux 2.4 transfer sizes, and the alignment of user buffer and file offset must all be multiples of the logical block size of the file system. Under Linux 2.6 alignment to 512-byte boundaries suffices.

 

使用DIRECTIO要求缓冲区、offset跟块大小对齐,而程序中的缓冲区在栈上分配的,块对齐的几率很低。怎么保证内存跟块大小(4096B)对齐呢?man posix_memalign

int posix_memalign(void **memptr, size_t alignment, size_t size);

void *memalign(size_t boundary, size_t size);

 

经过修改后,程序工作正常,测试结果如下:执行10000次随机读请求,平均读时间为3.6ms,因为文件的顺序性,这里测试的随机性不是太好。

 

参考资料:

http://www.ukuug.org/events/linux2001/papers/html/AArcangeli-o_direct.html

http://ender.hitidea.org/tag/o_direct/

 

 

转载于:https://www.cnblogs.com/yunnotes/archive/2013/04/19/3032345.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值