linux 磁盘io技术2------libaio与block io性能比较

近来公司需要提升服务器写磁盘的性能,调研了一些资料,抽时间把资料整理一下,同时把自己的调研情况总结一下,拿出来和各位网友分享一下,不免有问题的地方,希望大家不吝赐教,多谢。


今天先来聊聊linux下的libaio吧。


一、I/O模型

基本 Linux I/O 模型的简单矩阵

这个图,太经典了,白色框,就不多说了,大家估计都明白它指的是啥,今天主要来说说绿色部分AIO。

具体参见:

http://www.ibm.com/developerworks/cn/linux/l-async/

二、glibc和linux版本的区别

具体的介绍可以参见

http://hi.baidu.com/_kouu/item/2b3cfecd49c17d10515058d9

http://stackoverflow.com/questions/8768083/difference-between-posix-aio-and-libaio-on-linux


三、libaio和block io性能比较

block io:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>


#define MAX_COUNT 10 * 1024
#define BUF_SIZE  1 * 1024 * 1024

int main(int args, void *argv[]){
    int fd = open("/home/block.d", O_WRONLY | O_CREAT | O_APPEND);
    char * buf = malloc(BUF_SIZE);

    int n = MAX_COUNT;
    while (n > 0) {
        write(fd, buf, BUF_SIZE);        
        n--;
    }

    return 1;
}


libaio:

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <libaio.h>
#include <errno.h>
#include <unistd.h>
#include <unistd.h>




#define MAX_COUNT 10 * 1024
#define BUF_SIZE  1 * 1024 * 1024

#ifndef O_DIRECT
#define O_DIRECT         040000 /* direct disk access hint */
#endif

int main(int args, void *argv[]){
    int fd;
    void * buf = NULL;

    int pagesize = sysconf(_SC_PAGESIZE);  
    posix_memalign(&buf, pagesize, BUF_SIZE);  
    
    io_context_t ctx;
    struct iocb io,*p=&io;
    struct io_event e[10];
    struct timespec timeout;
    
    memset(&ctx,0,sizeof(ctx)); 
    if(io_setup(10,&ctx)!=0){
        printf("io_setup error\n"); 
        return -1; 
    }   
    
    if((fd = open("/home/aio.d", O_WRONLY | O_CREAT | O_APPEND | O_DIRECT, 0644))<0) {   
        perror("open error");
        io_destroy(ctx);
        return -1; 
    }   

    int n = MAX_COUNT;

    while(n > 0) {
        io_prep_pwrite(&io, fd, buf, BUF_SIZE, 0);
        
        if(io_submit(ctx, 1, &p)!=1) {
            io_destroy(ctx);
            printf("io_submit error\n");    
            return -1; 
        }   

        int ret = io_getevents(ctx, 1, 10, e, NULL);
        if (ret != 1) {
            perror("ret != 1");
            break;
        }
        n--;
    }

    close(fd);
    io_destroy(ctx);
    return 0;
}



通过top、dstat、iostat命令查看其写情况的

await:写请求排队和设备处理时间,16418ms----->30ms

wkB/s:每秒写磁盘流量,25M----->50M

可以看出在设备使用率基本达到饱和状态下,libaio的写性能要比block io好多出1倍。但是要注意,利用aio技术对于单进程单线程,仅仅是提高了其并发读写的能力,但是并没有提高磁盘硬件读写能力,实验证明,利用16个线程同时写和单线程aio写,性能基本一样。






参考资料:

http://lse.sourceforge.net/io/aio.html

http://hi.baidu.com/_kouu/item/2b3cfecd49c17d10515058d9

http://www.ibm.com/developerworks/cn/linux/l-async/

http://blog.chinaunix.net/uid-28323465-id-3938121.html

http://backend.blog.163.com/blog/static/20229412620135257159731/

http://www.jiangmiao.org/blog/2290.html

http://www.cnblogs.com/aLittleBitCool/archive/2011/10/18/2216646.html

http://blog.sina.com.cn/s/blog_87c80c500100yol3.html


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值