linux open o direct,[20181214]open file using O_DIRECT.txt

本文记录了作者在验证C语言使用O_DIRECT模式进行文件读取时遇到的问题,包括内存分配错误、O_DIRECT宏定义缺失以及性能低下。通过实例和dstat监控,展示了O_DIRECT如何避开OS缓存并实现同步I/O操作。
摘要由CSDN通过智能技术生成

[20181214]open file using O_DIRECT.txt

--//因为一个测试需要,需要写一个测试小例子,验证使用O_DIRECT打开文件每次都是从磁盘读取.

--//没想到浪费1个上午时间,C语言不是自己的擅长.

#include

#include

#include

#include

//#define __USE_GNU 1

#include

int main(void)

{

void *realbuff ;

int handle;

int bytes ;

int pagesize;

int nTemp ;

pagesize = getpagesize();

// printf("%d\n",pagesize);

realbuff = valloc( 1024000 );

nTemp = posix_memalign(&realbuff, pagesize, 1024000);

if (0!=nTemp)

{

perror("posix_memalign error");

return 1;

}

handle=open("test.bin",O_RDONLY|O_DIRECT);

if(handle==-1)

{

printf("ErrorOpeningFile\n");

exit(1);

}

while ( (bytes=read(handle,realbuff,1024000))>0 )

{

sleep(1);

printf("Read:%d bytes read.\n",bytes);

}

return 0 ;

}

--//自己的问题在于不能使用malloc分配缓存.而应该使用valloc分配,不然在posix_memalign这步就报错,使用valloc才能对齐pagesize.

--//顺便说一下getpagesize()我的测试返回是4096.我使用gcc版本:

$ gcc -v

Using built-in specs.

Target: x86_64-redhat-linux

Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux

Thread model: posix

gcc version 4.1.2 20080704 (Red Hat 4.1.2-54)

$ dd if=/dev/zero of=test.bin bs=1024000 count=10

10+0 records in

10+0 records out

10240000 bytes (10 MB) copied, 0.0343163 seconds, 298 MB/s

$ gcc -D_GNU_SOURCE direct_test.c -o direct_test

--//第2个问题就是使用O_DIRECT打开文件句柄,必须定义__USE_GNU 1.或者编译时加-D_GNU_SOURCE参数,否则找不到O_DIRECT的宏定义.

--//实际上这个应该是最先遇到的问题.

$ ./direct_test

Read:1024000 bytes read.

Read:1024000 bytes read.

Read:1024000 bytes read.

Read:1024000 bytes read.

Read:1024000 bytes read.

Read:1024000 bytes read.

Read:1024000 bytes read.

Read:1024000 bytes read.

Read:1024000 bytes read.

Read:1024000 bytes read.

# dstat -t -d -D cciss/c0d0p2,cciss/c0d0p6,total

-----time----- dsk/cciss/c-dsk/cciss/c--dsk/total-

date/time   | read  writ: read  writ: read  writ

14-12 15:45:13|1213B 7936B:  11k   65k:  24k  146k

....

14-12 15:45:16|  36k    0 :   0     0 :  72k    0

14-12 15:45:17|1000k    0 :   0     0 :2000k    0

14-12 15:45:18|1000k    0 :   0     0 :2000k    0

14-12 15:45:19|1004k    0 :   0     0 :2008k    0

14-12 15:45:20|1000k    0 :   0     0 :2000k    0

14-12 15:45:21|1000k    0 :   0     0 :2000k    0

14-12 15:45:22|1016k 2564k:   0     0 :2032k 5128k

14-12 15:45:23|1000k    0 :   0     0 :2000k    0

14-12 15:45:24|1000k    0 :   0     0 :2000k    0

14-12 15:45:25|1000k    0 :   0     0 :2000k    0

14-12 15:45:26|1000k    0 :   0     0 :2000k    0

14-12 15:45:27|   0     0 :   0     0 :   0     0

--//注:可以发现每秒从磁盘/dev/cciss/c0d0p2读取1000K.绕过os文件缓存.另外这个dstat版本有问题,total的记数会加倍.

# man open

...

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 must fit the block size of the device.

--//也就是这样读取效率低下,每次都是从磁盘读取.除非applications do their own caching.

--//顺便说一下国内开发有许多相关讨论的帖子,查询open O_DIRECT就能找到.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值