[20181214]open file using O_DIRECT.txt

--//因为一个测试需要,需要写一个测试小例子,验证使用O_DIRECT打开文件每次都是从磁盘读取.
--//没想到浪费1个上午时间,C语言不是自己的擅长.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
//#define __USE_GNU 1
#include <fcntl.h>

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就能找到.

[20181214]open file using O_DIRECT.txt的更多相关文章

  1. unity3d首次倒入工程文件出错Opening file Library/FailedAssetImports.txt failed解决方法

    打开unity3d,首次倒入工程到unity编辑器,但是频繁弹出“Opening file Library/FailedAssetImports.txt failed”的错误对话框,很麻烦. 解决方法 ...

  2. [20181130]control file sequential read.txt

    [20181130]control file sequential read.txt --//昨天上午探究了大量控制文件读的情况,链接:http://blog.itpub.net/267265/vie ...

  3. System.IO.File.WriteAllText("log.txt", "dddd");

    System.IO.File.WriteAllText("log.txt", "dddd");

  4. 关于Java里面File类创建txt文件重复???

    private JButton getOpenButton() { if (openButton == null) { openButton = new JButton(); openButton.s ...

  5. File操作-将txt里的内容写入到数据库表

    package com.Cristin.File;//将txt里的内容写入到数据库表 import com.Cristin.MySQL.AddDataToDB;import org.testng.an ...

  6. Python3基础 file read 读取txt文件的前几个字符

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. Python3基础 file open 打开txt文件并打印出全文

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. java中File类的使用

    public class FileLei {    public static void main(String[] args) throws IOException {        //..表示上 ...

  9. c# 保存数据到txt (追加)

    StringBuilder sb = new StringBuilder(); sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:s ...

随机推荐

  1. 改善JAVA代码01:考虑静态工厂方法代替构造器

    前言 系列文章:[传送门]   每次开始新的一本书,我都会很开心.新书新心情. 正文 静态工厂方法代替构造器 说起这个,好多可以念叨的.做了一年多的项目,慢慢也有感触. 说起构造器 大家很明白,构造器 ...

  2. 如何用vue-router为每个路由配置各自的title

    传统方法 以前在单页面路由中,就只能在html文件中定一个固定的网站的title.如果想要动态的去修改,需要使用如下的方法. document.title = '这是一个标题'; 这样会让我们做很多无 ...

  3. python练习五—简单web应用

    配置apache 我以前web开发基本都是基于java平台的,比如tomcat,servlet等等,由tomcat接收http请求,然后交给servlet处理,servlet处理完成以后把返回结果交给 ...

  4. go os/exec执行外部程序

    Go提供的os/exec包可以执行外部程序,比如调用系统命令等. 最简单的代码,调用pwd命令显示程序当前所在目录: package main import ( "fmt" &qu ...

  5. 海量数据处理之BitMap

    有这样一种场景:一台普通PC,2G内存,要求处理一个包含40亿个不重复并且没有排过序的无符号的int整数,给出一个整数,问如果快速地判断这个整数是否在文件40亿个数据当中? 问题思考: 40亿个int ...

  6. 一个Android常用的组件收集

    Android笔记之ViewPager实例一:制作欢迎引导界面:http://www.cnblogs.com/xingyyy/p/3335705.html Fragment 实现底部菜单栏:http: ...

  7. [日常] HTTP的媒体类型

    HTTP的媒体类型 1.MIME类型的数据格式标签(MultIpurpose Internet Mail Extension) 2.最初用于电子邮件系统之间搬移,多用途互联网邮件扩展 3.MIME类型 ...

  8. [PHP]算法-最长公共子串的PHP实现

    最长公共子串问题: 给定两个字符串,求出它们之间最长的相同子字符串的长度. 暴力解法思路: 1.以两个字符串的每个字符为开头,往后比较,这样就会需要两层循环 2.两层循环内部的比较方式,也是一层循环, ...

  9. java_自定义标签,我的第一个自定义标签!

    自定义标签,我的第一个自定义标签! 总共分两步 编写一个实现tag接口的java类,把jsp页面中的java代码移到这个类中,(标签处理器类) 编写标签库描述符(tld)文件,在tld文件中把标签处理 ...

  10. js 1.变量提升 2.条件语句 3.循环语句 4.加号+的使用

    1.变量提升 变量提升是浏览器的一个功能,在运行js 代码执行前,浏览器会给js一个全局作用域叫 window,window 分两个模块,一个叫运营模块,内存模块找到当前作用域下的所有带var和fun ...