Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4022899
  • 博文数量: 251
  • 博客积分: 11197
  • 博客等级: 上将
  • 技术积分: 6862
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-05 14:41
个人简介

@HUST张友东 work@taobao zyd_com@126.com

文章分类

全部博文(251)

文章存档

2014年(10)

2013年(20)

2012年(22)

2011年(74)

2010年(98)

2009年(27)

分类: 服务器与存储

2011-11-01 16:55:29

写了一个测试磁盘寻道(+旋转)时间的程序,方法如下:创建一个大文件(如果太小,文件所有内容都在一个磁道上),使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,因为文件的顺序性,这里测试的随机性不是太好。

 

参考资料:

 

阅读(4802) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~