天天看點

2022-11-06 Linux原生AIO-使用記錄摘要:參考:檢視innodb是否開啟異步IOcentos安裝linux原生aio使用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.csdn.net/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