讲讲在Libuv中使用Io_Uring

开发 前端
git clone https://github.com/axboe/liburing.git。执行./configure && make -j2 && sudo make install(make j2开启两个线程编译,根据自己的核数定)。

 [[410623]]

本文转载自微信公众号「编程杂技  」,作者theanarkh 。转载本文请联系编程杂技公众号。

本文介绍如果在Libuv中使用io_uring。逻辑:

1 申请一个io_uring对应的fd。

2 初始化一个poll handle,封装1中的fd。

3 注册到Libuv的epoll中。

4 读取文件列表,给io_uring提交请求

5 io_uring完成,1中的fd可读,从而epoll返回。

6 Libuv的poll io阶段执行回调。

7 回调里获取io_uring的任务完成列表,拿到每个任务关联的请求,执行回调。

  1. #include <fcntl.h> 
  2. #include <stdio.h> 
  3. #include <string.h> 
  4. #include <sys/stat.h> 
  5. #include <sys/ioctl.h> 
  6. #include <liburing.h> 
  7. #include <stdlib.h> 
  8. #include <uv.h> 
  9. #define QUEUE_DEPTH 1 
  10. #define BLOCK_SZ    1024 
  11.  
  12. // 前向声明 
  13. struct file_info; 
  14. // 定义回调 
  15. typedef void (*file_callback)(struct file_info*); 
  16.  
  17. // 管理一个文件读取请求的结构体 
  18. struct file_info { 
  19.     // 文件大小 
  20.     off_t file_sz; 
  21.     // 回调 
  22.     file_callback cb; 
  23.     // 读取的大小 
  24.     int count
  25.     // 文件名 
  26.     char *name
  27.     // 读取的数据 
  28.     struct iovec iovecs[];      
  29. }; 
  30.  
  31. // 获取文件大小 
  32. off_t get_file_size(int fd) { 
  33.     struct stat st; 
  34.  
  35.     if(fstat(fd, &st) < 0) { 
  36.         perror("fstat"); 
  37.         return -1; 
  38.     } 
  39.     if (S_ISBLK(st.st_mode)) { 
  40.         unsigned long long bytes; 
  41.         if (ioctl(fd, BLKGETSIZE64, &bytes) != 0) { 
  42.             perror("ioctl"); 
  43.             return -1; 
  44.         } 
  45.         return bytes; 
  46.     } else if (S_ISREG(st.st_mode)) 
  47.         return st.st_size; 
  48.  
  49.     return -1; 
  50.  
  51. // 向内核提交一个请求 
  52. int submit_read_request(char *file_path, file_callback cb, struct io_uring *ring) { 
  53.     // 打开文件 
  54.     int file_fd = open(file_path, O_RDONLY); 
  55.     if (file_fd < 0) { 
  56.         perror("open"); 
  57.         return 1; 
  58.     } 
  59.     // 获取大小 
  60.     off_t file_sz = get_file_size(file_fd); 
  61.     off_t bytes_remaining = file_sz; 
  62.     int current_block = 0; 
  63.     int blocks = (int) file_sz / BLOCK_SZ; 
  64.     if (file_sz % BLOCK_SZ) blocks++; 
  65.     // 申请内存 
  66.     struct file_info *fi = malloc(sizeof(*fi) + (sizeof(struct iovec) * blocks)); 
  67.     // 保存文件名 
  68.     fi->name = file_path; 
  69.     // 计算和申请保存文件内容的内存 
  70.     while (bytes_remaining) { 
  71.         // 剩下的大小 
  72.         off_t bytes_to_read = bytes_remaining; 
  73.         // 一个buffer最大保存BLOCK_SZ大小 
  74.         if (bytes_to_read > BLOCK_SZ) 
  75.             bytes_to_read = BLOCK_SZ; 
  76.         // 记录buffer大小 
  77.         fi->iovecs[current_block].iov_len = bytes_to_read; 
  78.         // 申请内存 
  79.         void *buf; 
  80.         if( posix_memalign(&buf, BLOCK_SZ, BLOCK_SZ)) { 
  81.             perror("posix_memalign"); 
  82.             return 1; 
  83.         } 
  84.         // 记录内存地址 
  85.         fi->iovecs[current_block].iov_base = buf; 
  86.         // 下一块 
  87.         current_block++; 
  88.         // 更新剩下的大小 
  89.         bytes_remaining -= bytes_to_read; 
  90.     } 
  91.     // 保存文件大小 
  92.     fi->file_sz = file_sz; 
  93.     // 获取一个io_uring的请求结构体 
  94.     struct io_uring_sqe *sqe = io_uring_get_sqe(ring); 
  95.     // 填充请求 
  96.     io_uring_prep_readv(sqe, file_fd, fi->iovecs, blocks, 0); 
  97.     // 保存请求上下文,响应的时候用 
  98.     io_uring_sqe_set_data(sqe, fi); 
  99.     // 保存回调 
  100.     fi->cb = cb; 
  101.     // 提交请求给内核 
  102.     io_uring_submit(ring); 
  103.  
  104.     return 0; 
  105.  
  106. // io_uring相关的结构体 
  107. struct io_uring_info { 
  108.   int fd; 
  109.   int32_t pending; 
  110.   struct io_uring ring; 
  111.   uv_poll_t poll_handle; 
  112. }; 
  113.  
  114. // io_uring完成任务后,Libuv执行的回调 
  115. void uv__io_uring_done(uv_poll_t* handle, int status, int events) { 
  116.     struct io_uring* ring; 
  117.     struct io_uring_info* io_uring_data; 
  118.     struct io_uring_cqe* cqe; 
  119.     struct file_info* req; 
  120.     // 获取Libuv中保存的io_uring信息 
  121.     io_uring_data = uv_default_loop()->data; 
  122.     ring = &io_uring_data->ring; 
  123.     // 处理每一个完成的请求 
  124.     while (1) {  
  125.         io_uring_peek_cqe(ring, &cqe); 
  126.  
  127.         if (cqe == NULL
  128.             break; 
  129.         // 全部处理完则注销事件 
  130.         if (--io_uring_data->pending == 0) 
  131.            uv_poll_stop(handle); 
  132.         // 拿到请求上下文 
  133.         req = (void*) (uintptr_t) cqe->user_data; 
  134.         // 记录读取的大小 
  135.         req->count = cqe->res; 
  136.  
  137.         io_uring_cq_advance(ring, 1); 
  138.         // 执行回调 
  139.         req->cb(req); 
  140.     } 
  141.     // 处理完则退出 
  142.     if (io_uring_data->pending == 0) 
  143.         uv_stop(uv_default_loop()); 
  144.  
  145. // 文件读取后的业务回调 
  146. void filedone(struct file_info* info) { 
  147.     printf("读取的大小:%d,文件信息:%s => %d\n", (int)info->count, info->name, (int)info->file_sz);}int main(int argc, char *argv[]) { 
  148.  
  149.     if (argc < 2) { 
  150.         fprintf(stderr, "请输入文件名称\n"); 
  151.         return 1; 
  152.     } 
  153.     // 申请一个io_uring相关的结构体 
  154.     struct io_uring_info *io_uring_data = malloc(sizeof(*io_uring_data)); 
  155.     // 初始化io_uring 
  156.     io_uring_queue_init(1, &io_uring_data->ring, 0); 
  157.     // 初始化poll handle,保存监听的fd 
  158.     uv_poll_init(uv_default_loop(), &io_uring_data->poll_handle, io_uring_data->ring.ring_fd); 
  159.     // 注册事件和回调 
  160.     uv_poll_start(&io_uring_data->poll_handle, UV_READABLE, uv__io_uring_done); 
  161.     // 保存io_uring的上下文在loop中 
  162.     uv_default_loop()->data = (void *)io_uring_data; 
  163.     // 处理每一个文件 
  164.     for (int i = 1; i < argc; i++) { 
  165.         submit_read_request(argv[i], filedone, &io_uring_data->ring); 
  166.         io_uring_data->pending++; 
  167.     } 
  168.     // 开始事件循环 
  169.     uv_run(uv_default_loop(), UV_RUN_DEFAULT); 
  170.     // 退出 
  171.     uv_loop_close(uv_default_loop()); 
  172.     io_uring_queue_exit(&io_uring_data->ring); 
  173.     return 0; 

编译过程

1 git clone https://github.com/axboe/liburing.git。执行./configure && make -j2 && sudo make install(make j2开启两个线程编译,根据自己的核数定)。

2 git clone https://github.com/libuv/libuv.git。执行./autogen.sh && ./configure && make -j2 && sudo make install。

3 安装完依赖后新建test.cc。然后编译 gcc -xc test2.cc -luring -luv(xc指定按c语言编译,c++的话限制不一样,会报错)。

4 新建两个测试文件hello.cc和world.cc 。执行 ./a.out hello.cc world.cc。

5 输出

  1. 读取的大小:6997,文件信息:hello.cc => 6997 
  2. 读取的大小:11019,文件信息:world.cc => 11019 

代码仓库:https://github.com/theanarkh/learn-io_uring。

可以参考

1.https://github.com/shuveb/io_uring-by-example/blob/master/03_cat_liburing/main.c

2 https://github.com/libuv/libuv/pull/2322

 

责任编辑:武晓燕 来源: 编程杂技
相关推荐

2023-10-20 06:26:51

Libuvio_uring

2023-02-07 19:46:35

NIOCQ内核

2023-04-12 18:36:20

IO框架内核

2021-07-07 23:38:05

内核IOLinux

2023-12-28 11:24:29

IO系统请求

2021-07-03 08:04:10

io_uringNode.js异步IO

2020-09-30 06:44:39

存储IO

2021-09-05 17:46:21

云计算No.jsio_uringJS

2009-06-25 16:49:24

Hibernate

2013-12-13 17:21:14

Lua脚本语言

2023-04-12 15:25:09

Bytrace鸿蒙

2023-11-17 12:04:39

GORM并发

2021-06-05 06:49:54

LibuvN-API进程

2022-06-23 09:47:50

混沌工程系统Kubernetes

2021-03-22 08:45:30

异步编程Java

2018-03-26 14:25:55

KubernetesSkaffold命令

2012-04-19 12:58:26

TitaniumJSS

2009-06-16 09:06:37

JavaMailJSP

2013-05-14 10:13:06

WindowsLinux操作系统

2023-11-27 19:39:46

Goprotobuf
点赞
收藏

51CTO技术栈公众号