2022-11-06 Linux原生AIO-使用记录

摘要:

使用Linux原生AIO

参考:

https://dev.mysql.com/doc/refman/5.7/en/innodb-linux-native-aio.html

https://www.docs4dev.com/docs/zh/mysql/5.7/reference/innodb-parameters.html#sysvar_innodb_use_native_aio

https://zhuanlan.zhihu.com/p/485928080

https://blog.youkuaiyun.com/weixin_54516968/article/details/125793133

查看innodb是否开启异步IO

show variables like '%innodb_use_native_aio%';

centos安装linux原生aio

yum install -y libaio-devel

使用aio示例:
 

aio.c

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <libaio.h>
 
int main(void)
{
    int               output_fd;
    struct iocb       io, *p=&io;
    struct io_event   e;
    struct timespec   timeout;
    io_context_t      ctx;
    const char        *content="hello world!";
 
    // 1. init the io context.
    memset(&ctx, 0, sizeof(ctx));
    if(io_setup(10, &ctx)){
        printf("io_setup error\n");
        return -1;
    }
 
    // 2. try to open a file.
    if((output_fd=open("foobar.txt", O_CREAT|O_WRONLY, 0644)) < 0) {
        perror("open error");
        io_destroy(ctx);
        return -1;
    }
 
    // 3. prepare the data.
    io_prep_pwrite(&io, output_fd, (void*)content, strlen(content), 0);
    //io.data = content;   // set or not
    if(io_submit(ctx, 1, &p) < 0){
        io_destroy(ctx);
        printf("io_submit error\n");
        return -1;
    }
 
    // 4. wait IO finish.
    while(1) {
        timeout.tv_sec  = 0;
        timeout.tv_nsec = 500000000; // 0.51s
        if(io_getevents(ctx, 0, 1, &e, &timeout) == 1) {
            close(output_fd);
            break;
        }
        printf("haven't done\n");
        sleep(1);
    }

    io_destroy(ctx);
    return 0;
}

makefile:


aio:
	gcc -laio  aio.c  -o aio

clean:
	rm aio -rf
	rm foobar.txt  -rf

	

编译:

gcc -laio  aio.c  -o aio

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟世者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值