首页 > 编程知识 正文

异步io效率最高,非阻塞io和异步io的区别

时间:2023-05-03 11:57:06 阅读:18855 作者:3268

我们正在尝试Linux libaIO,以在服务器APP应用程序中优化io性能。 我相信我做了所有必要的事。 (使用O_DIRECT将缓冲区与内存页面对齐.)正在等待io_submit立即返回,但简单的测试显示,使用核心i7笔记本电脑返回实际需要80微秒左右。 你会不会期望过高,或者测试计划有问题? (编译G --std=的C 0x -laIO ) libaio性能有问题的Linux异步io

#包含

#包含

#包含

#包含

#包含

#包含

#包含

#包含

//Open the file for write,return the file descriptor

intopen_write(charconst*file ) )。

{

intfd=open (文件,O_DIRECT|O_CREAT|O_WRONLY,S_IRWXU|S_IRWXG|S_IROTH );

if(FD0 ) {

perror(open_write );

退出(1;

}

}

//Make a buffer of _size_ byte,fill with 'a ',return the buffer,it should be aligned to memory page

void * make _ write _ buffer (size _ tsize )

{

void* buf=0;

intret=POSIX_memalign((void** ) buf,sysconf ) _sc_pagesize ),size );

if(ret0|||buf==0) {

perror(make_write_buffer );

退出(1;

}

memset(buf,' a ',size );

返回缓冲区;

}

(intmain(intargc,char *argv[] ) ) ) ) ) ) ) ) ) ) )

{

静态常数大小_ t size=16 * 1024;

//Prepare file and buffer to write

int write _ FD=open _ write (' test.dat ';

void * buf=make _ write _ buffer (size;

//Prepare aio

io_context_t ctx;

短信(CTX,0,sizeof ) CTX );

常数int max events=32;

io_setup(maxevents,ctx );

iocb *iocbpp=new iocb;

io_prep_pwrite(iocbPP,write_fd,buf,SIZE,0 );

usingnamespacestd :3360 chrono;

//submit AIO任务

auto start=monotonic _ clock :3360 now (;

intstatus=io_submit(CTX,1,iocbpp );

if (状态0 ) {

errno=-status;

perror(io_submit );

退出(1;

}

auto dur=duration _ cast (monotonic _ clock :3360 now (-start ) );

STD :3360 cout ' io _ submit takes : ' dur.count () ' microseconds.' STD : endl;

io_event events[10];

intn=io_getevents(CTX,1,10,events,NULL );

关闭(write _ FD;

io_destroy(CTX;

delete iocbpp;

自由(buf );

返回0;

}

0

80微秒,慢吗? p最佳预期:你的过程已经安排好了。 如果增加缓冲区大小呢?

2011-12-25 13:07:38

0

此外,测量数百(或数千)个调用并计数该函数的第一个调用似乎不是一个好主意。

2011-12-25 13:08:37

0

谢谢你,wxdjr。 我做了几次,80微秒是平均水平。 “冷”操作可能需要400微秒。

2011-12-25 21:36:25

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。