[20181214]open file using O_DIRECT.txt
[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的更多相关文章
- unity3d首次倒入工程文件出错Opening file Library/FailedAssetImports.txt failed解决方法
打开unity3d,首次倒入工程到unity编辑器,但是频繁弹出“Opening file Library/FailedAssetImports.txt failed”的错误对话框,很麻烦. 解决方法 ...
- [20181130]control file sequential read.txt
[20181130]control file sequential read.txt --//昨天上午探究了大量控制文件读的情况,链接:http://blog.itpub.net/267265/vie ...
- System.IO.File.WriteAllText("log.txt", "dddd");
System.IO.File.WriteAllText("log.txt", "dddd");
- 关于Java里面File类创建txt文件重复???
private JButton getOpenButton() { if (openButton == null) { openButton = new JButton(); openButton.s ...
- File操作-将txt里的内容写入到数据库表
package com.Cristin.File;//将txt里的内容写入到数据库表 import com.Cristin.MySQL.AddDataToDB;import org.testng.an ...
- Python3基础 file read 读取txt文件的前几个字符
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Python3基础 file open 打开txt文件并打印出全文
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- java中File类的使用
public class FileLei { public static void main(String[] args) throws IOException { //..表示上 ...
- c# 保存数据到txt (追加)
StringBuilder sb = new StringBuilder(); sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:s ...
随机推荐
- 浅谈《think in java》:一 对象导论总结
清单1. 抽象机制,面向对象程序设计方式 java所基于Smalltalk的特性表现一种纯粹的面向对象设计方式: 万物都是对象 程序是对象的集合(容器),他们通过发送消息(发送请求)来告知彼此所要做的 ...
- cookie、session总结
前几天在调试第三方支付接口时碰到一个session失效问题,用了几天时间才搞明白,现在回想一下,主要还是由于cookie和session这一块的一些基本概念没有搞清楚,现总结一下. 浏览器使用HTTP ...
- springboot 与 shiro 整合 (简洁版)
前言: 网上有很多springboot 与 shiro 整合的资料,有些确实写得很好, 对学习shiro和springboot 都有很大的帮助. 有些朋友比较省事, 直接转发或者复制粘贴.但是没有经过 ...
- ES6 系列之私有变量的实现
前言 在阅读 <ECMAScript 6 入门>的时候,零散的看到有私有变量的实现,所以在此总结一篇. 1. 约定 实现 class Example { constructor() { t ...
- solr调用lucene底层实现倒排索引源码解析
1.什么是Lucene? 作为一个开放源代码项目,Lucene从问世之后,引发了开放源代码社群的巨大反响,程序员们不仅使用它构建具体的全文检索应用,而且将之集成到各种系统软件中去,以及构建Web应用, ...
- TensorFlow中的Placement启发式算法模块——Placer
背景 [作者:DeepLearningStack,阿里巴巴算法工程师,开源TensorFlow Contributor] 受限于单个Device的计算能力和存储大小,许多深度学习模型都有着使用模型分片 ...
- MySQL 索引与查询优化
本文介绍一些优化 MySQL 索引设计和查询的建议.在进行优化工作前,请务必了解MySQL EXPLAIN命令: 查看执行计划 索引 索引在逻辑上是指从索引列(关键字)到数据的映射,通过索引可以快速的 ...
- 华为路由器 IPSec 与 GRE 结合实验
二者结合的目的 GRE 支持单播.组播.广播,IPSec 仅支持单播.GRE 不支持对于数据完整性以及身份认证的验证功能,并且也不具备数据加密保护.而 IPSec 恰恰拥有强大的安全机制.达到了互补的 ...
- 我的asp.net core目录
推荐 Asp.NETCore轻松学系列阅读指引目录(asp.net core 2.2) 官方文档翻译 http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore- ...
- Linux服务器GlashFish的Domain管理
1.本文内容 Glassfish(V3.1)的Domain创建,删除,登陆,部署等 Linux客户端工具: SecureCRTPortable和 WinSCP (请自行下载) 2.1 新建Domain ...