自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從內(nèi)核看IO_Uring的實(shí)現(xiàn)(一)

系統(tǒng) Linux
最近研究了一下Linux的高性能異步IO框架io_uring,并嘗試引入Node.js中應(yīng)用起來。所以本文打算介紹一下io_uring在內(nèi)核的實(shí)現(xiàn),因?yàn)閕o_uring實(shí)現(xiàn)代碼量大,邏輯復(fù)雜,所以只能慢慢分析。這一篇介紹io_uring初始化接口io_uring_setup的實(shí)現(xiàn)。

[[410006]]

前言:最近研究了一下Linux的高性能異步IO框架io_uring,并嘗試引入Node.js中應(yīng)用起來。所以本文打算介紹一下io_uring在內(nèi)核的實(shí)現(xiàn),因?yàn)閕o_uring實(shí)現(xiàn)代碼量大,邏輯復(fù)雜,所以只能慢慢分析。這一篇介紹io_uring初始化接口io_uring_setup的實(shí)現(xiàn)。

  1. static long io_uring_setup(u32 entries, struct io_uring_params __user *params){ 
  2.     struct io_uring_params p; 
  3.     int i; 
  4.  
  5.     if (copy_from_user(&p, params, sizeof(p))) 
  6.         return -EFAULT; 
  7.     // 支持的flag 
  8.     if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | 
  9.             IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | 
  10.             IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) 
  11.         return -EINVAL; 
  12.  
  13.     return  io_uring_create(entries, &p, params); 

io_uring_setup是對io_uring_create的封裝。第一個參數(shù)entries指定請求隊列的長度,第二個參數(shù)params是用于調(diào)用方和內(nèi)核通信的結(jié)構(gòu)體。我們看一下定義。

  1. struct io_uring_params { 
  2.     // 定義請求隊列長度(2的sq_entries次方),調(diào)用方定義 
  3.     __u32 sq_entries; 
  4.     // 完成隊列長度,默認(rèn)是2 * 請求隊列長度 
  5.     __u32 cq_entries; 
  6.     // 控制內(nèi)核行為的標(biāo)記 
  7.     __u32 flags; 
  8.     // poll模式下開啟的內(nèi)核線程綁定的cpu 
  9.     __u32 sq_thread_cpu; 
  10.     // poll模式下開啟的內(nèi)核線程空閑時間,之后會掛起。 
  11.     __u32 sq_thread_idle; 
  12.     // 內(nèi)核當(dāng)前支持的能力,內(nèi)核設(shè)置 
  13.     __u32 features; 
  14.     __u32 wq_fd; 
  15.     __u32 resv[3]; 
  16.     // 記錄內(nèi)核數(shù)據(jù)的結(jié)構(gòu)體,調(diào)用方后續(xù)調(diào)用mmap需要用到。 
  17.     struct io_sqring_offsets sq_off; 
  18.     struct io_cqring_offsets cq_off; 
  19. }; 

我們接著看io_uring_create。

  1. static int io_uring_create(unsigned entries, struct io_uring_params *p, 
  2.                struct io_uring_params __user *params){ 
  3.     struct user_struct *user = NULL
  4.     struct io_ring_ctx *ctx; 
  5.     bool limit_mem; 
  6.     int ret; 
  7.  
  8.     p->sq_entries = roundup_pow_of_two(entries); 
  9.     // 自定義完成隊列長度 
  10.     if (p->flags & IORING_SETUP_CQSIZE) { 
  11.         p->cq_entries = roundup_pow_of_two(p->cq_entries); 
  12.         // 完成隊列不能小于請求隊列 
  13.         if (p->cq_entries < p->sq_entries) 
  14.             return -EINVAL; 
  15.         // 超過閾值則需要設(shè)置IORING_SETUP_CLAMP標(biāo)記 
  16.         if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { 
  17.             if (!(p->flags & IORING_SETUP_CLAMP)) 
  18.                 return -EINVAL; 
  19.             p->cq_entries = IORING_MAX_CQ_ENTRIES; 
  20.         } 
  21.     } else { 
  22.         // 默認(rèn)是兩倍的請求隊列長度 
  23.         p->cq_entries = 2 * p->sq_entries; 
  24.     } 
  25.     // 用戶信息 
  26.     user = get_uid(current_user()); 
  27.     // 分配一個ctx記錄上下文,因?yàn)檎{(diào)用方只能拿到fd,后續(xù)操作fd的時候會拿到關(guān)聯(lián)的上下文 
  28.     ctx = io_ring_ctx_alloc(p); 
  29.     ctx->user = user
  30.     // 和poll模式相關(guān)的數(shù)據(jù)結(jié)構(gòu) 
  31.     ctx->sqo_task = get_task_struct(current); 
  32.     // 分配一個io_rings 
  33.     ret = io_allocate_scq_urings(ctx, p); 
  34.     // 處理poll模式的邏輯 
  35.     ret = io_sq_offload_start(ctx, p); 
  36.     // 后面還有很多,一會分析 

io_uring_create代碼比較多,我們分步分析。首先分配了一個io_ring_ctx結(jié)構(gòu)體,這是核心的數(shù)據(jù)結(jié)構(gòu),用于記錄io_uring實(shí)例的上下文,不過我們暫時不需要了解它具體的定義,因?yàn)閷?shí)在太多,只關(guān)注本文相關(guān)的字段。

1 分配一個io_rings結(jié)構(gòu)體

接著調(diào)用io_allocate_scq_urings分配一個io_rings結(jié)構(gòu)體,這是非常核心的邏輯,我們看一下io_rings的定義。

  1. struct io_rings { 
  2.     struct io_uring     sq, cq; 
  3.     u32         sq_ring_mask, cq_ring_mask; 
  4.     u32         sq_ring_entries, cq_ring_entries; 
  5.     u32         sq_dropped; 
  6.     u32         sq_flags; 
  7.     u32         cq_flags; 
  8.     u32         cq_overflow; 
  9.     struct io_uring_cqe cqes[]; 
  10. }; 

io_rings主要用于記錄請求和完成隊列的信息。我們繼續(xù)看io_allocate_scq_urings。

  1. static int io_allocate_scq_urings(struct io_ring_ctx *ctx, 
  2.                   struct io_uring_params *p){ 
  3.     struct io_rings *rings; 
  4.     size_t size, sq_array_offset; 
  5.     // 記錄請求和完成隊列大小到ctx 
  6.     ctx->sq_entries = p->sq_entries; 
  7.     ctx->cq_entries = p->cq_entries; 
  8.     /*  
  9.         計算結(jié)構(gòu)體和額外數(shù)組的大小,sq_array_offset保存結(jié)構(gòu)體大小, 
  10.         size保存結(jié)構(gòu)體+額外數(shù)組+另一個額外數(shù)組的大小 
  11.     */ 
  12.     size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); 
  13.     // 分配內(nèi)存 
  14.     rings = io_mem_alloc(size); 
  15.     // ... 

io_allocate_scq_urings細(xì)節(jié)比較多,我們分開分析,我們看一下rings_size的邏輯。

  1. static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, 
  2.                 size_t *sq_offset){ 
  3.     struct io_rings *rings; 
  4.     size_t off, sq_array_size; 
  5.     // 計算結(jié)構(gòu)體和格外數(shù)組的大小,見io_rings定義 
  6.     off = struct_size(rings, cqes, cq_entries); 
  7.     // sq_offset記錄結(jié)構(gòu)體大小 
  8.     if (sq_offset) 
  9.         *sq_offset = off
  10.     // 計算多個u32元素的數(shù)組的大小 
  11.     sq_array_size = array_size(sizeof(u32), sq_entries); 
  12.     // 計算結(jié)構(gòu)體大小 + sq_array_size的大小保存到off 
  13.     if (check_add_overflow(off, sq_array_size, &off)) 
  14.         return SIZE_MAX; 
  15.     return off

struct_size是計算結(jié)構(gòu)體和額外字段大小的宏,我們剛才看到io_rings結(jié)構(gòu)體的定義中,最后一個字段是struct io_uring_cqe cqes[],看起來是個空數(shù)組,其實(shí)他的內(nèi)存是緊跟著結(jié)構(gòu)體后面分配的,結(jié)構(gòu)如下。

下面我們看struct_size是如何計算的。

  1. #define struct_size(p, member, count)                   \ 
  2.     __ab_c_size(count,                      \ 
  3.             sizeof(*(p)->member) + __must_be_array((p)->member),\ 
  4.             sizeof(*(p))) 
  5.  
  6. static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c){ 
  7.     size_t bytes; 
  8.     // 計算a * b保存到bytes 
  9.     if (check_mul_overflow(a, b, &bytes)) 
  10.         return SIZE_MAX; 
  11.     // 計算bytes + c保存搭配bytes 
  12.     if (check_add_overflow(bytes, c, &bytes)) 
  13.         return SIZE_MAX; 
  14.  
  15.     return bytes; 

我們看到計算方式就是數(shù)組元素大小*元素個數(shù)+結(jié)構(gòu)體本身的大小。計算完結(jié)構(gòu)體大小后又通過array_size計算了另一個數(shù)組的大小并加起來,所以io_rings的結(jié)構(gòu)體如下所示。

分配了io_rings之后我們繼續(xù)看接下來的邏輯。

  1. static int io_allocate_scq_urings(struct io_ring_ctx *ctx, 
  2.                   struct io_uring_params *p){ 
  3.     // ... 
  4.     // 記錄到ctx中 
  5.     ctx->rings = rings; 
  6.     // sq_array記錄rings結(jié)構(gòu)體中,u32數(shù)組的首地址 
  7.     ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); 
  8.     // 用于回環(huán)處理 
  9.     rings->sq_ring_mask = p->sq_entries - 1; 
  10.     rings->cq_ring_mask = p->cq_entries - 1; 
  11.     // 隊列長度 
  12.     rings->sq_ring_entries = p->sq_entries; 
  13.     rings->cq_ring_entries = p->cq_entries; 
  14.     ctx->sq_mask = rings->sq_ring_mask; 
  15.     ctx->cq_mask = rings->cq_ring_mask; 
  16.     // 請求隊列的數(shù)組大小 
  17.     size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); 
  18.     // 分配內(nèi)存并記錄到sq_sqes 
  19.     ctx->sq_sqes = io_mem_alloc(size); 
  20.     return 0; 

進(jìn)行了一系列設(shè)置后,架構(gòu)如下。

創(chuàng)建完io_rings結(jié)構(gòu)體后,我們繼續(xù)回到io_uring_create中。

2 設(shè)置io_uring_params

內(nèi)核申請完系列結(jié)構(gòu)體后,需要通過io_uring_params結(jié)構(gòu)體返回給調(diào)用方。

  1. static int io_uring_create(unsigned entries, struct io_uring_params *p, 
  2.                struct io_uring_params __user *params) { 
  3.  
  4.     ret = io_allocate_scq_urings(ctx, p); 
  5.     // 初始化poll模式相關(guān)邏輯,如果開啟了的話 
  6.     ret = io_sq_offload_start(ctx, p); 
  7.     memset(&p->sq_off, 0, sizeof(p->sq_off)); 
  8.     // 記錄字段在結(jié)構(gòu)體的偏移 
  9.     p->sq_off.head = offsetof(struct io_rings, sq.head); 
  10.     p->sq_off.tail = offsetof(struct io_rings, sq.tail); 
  11.     p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); 
  12.     p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); 
  13.     p->sq_off.flags = offsetof(struct io_rings, sq_flags); 
  14.     p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); 
  15.     p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; 
  16.  
  17.     memset(&p->cq_off, 0, sizeof(p->cq_off)); 
  18.     p->cq_off.head = offsetof(struct io_rings, cq.head); 
  19.     p->cq_off.tail = offsetof(struct io_rings, cq.tail); 
  20.     p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); 
  21.     p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); 
  22.     p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); 
  23.     p->cq_off.cqes = offsetof(struct io_rings, cqes); 
  24.     p->cq_off.flags = offsetof(struct io_rings, cq_flags); 
  25.     // 內(nèi)核支持的屬性 
  26.     p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | 
  27.             IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | 
  28.             IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | 
  29.             IORING_FEAT_POLL_32BITS; 
  30.  
  31.     copy_to_user(params, p, sizeof(*p)) 
  32.     // 獲取fd 
  33.     ret = io_uring_get_fd(ctx); 
  34.     return ret; 

io_uring_create繼續(xù)進(jìn)行了一系列賦值,賦值完后架構(gòu)如下。

3 獲取文件描述符

內(nèi)核通過io_uring_get_fd獲取文件描述符返回給調(diào)用方。

  1. static int io_uring_get_fd(struct io_ring_ctx *ctx){ 
  2.     struct file *file; 
  3.     // 獲取一個可用fd 
  4.     int ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); 
  5.     // 分配一個file結(jié)構(gòu)體,設(shè)置函數(shù)集為io_uring_fops,并關(guān)聯(lián)上下文ctx 
  6.     file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, 
  7.                     O_RDWR | O_CLOEXEC); 
  8.  
  9.     // 關(guān)聯(lián)fd和file結(jié)構(gòu)體 
  10.     fd_install(ret, file); 
  11.     return ret; 

io_uring_get_fd申請了一個fd和file,這是遵循vfs的設(shè)計,最重要的是把io_uring的函數(shù)集掛在到file上,后續(xù)通過fd操作的io_uring實(shí)例的時候,經(jīng)過vfs后就會執(zhí)行對應(yīng)的函數(shù),另外還需要把ctx和file關(guān)聯(lián)起來,因?yàn)楹罄m(xù)通過fd操作io_uring時,需要拿到fd對應(yīng)的io_uring上下文。至此。

io_uring_setup就分析完了,但是還不能使用。io_uring在設(shè)計中,為了減少系統(tǒng)調(diào)用和用戶、內(nèi)核數(shù)據(jù)通信的成本,實(shí)現(xiàn)了用戶、內(nèi)核共享數(shù)據(jù)結(jié)構(gòu)的方式,這樣用戶和內(nèi)核就可以操作同一份數(shù)據(jù)結(jié)構(gòu)達(dá)到通信目的,而不用通過系統(tǒng)調(diào)用,更不需要設(shè)計來回復(fù)制。為了達(dá)到這個目的,用戶拿到io_uring實(shí)例后,還需要調(diào)用mmap獲取對應(yīng)的內(nèi)存映射。我們通過liburing庫的邏輯來分析。

4 從liburing庫看io_uring的使用

  1. int io_uring_queue_init_params(unsigned entries, struct io_uring *ring, 
  2.                    struct io_uring_params *p){ 
  3.     int fd, ret; 
  4.     // 調(diào)用io_uring_setup,拿到fd 
  5.     fd = __sys_io_uring_setup(entries, p); 
  6.     if (fd < 0) 
  7.         return -errno; 
  8.     // 內(nèi)存映射 
  9.     ret = io_uring_queue_mmap(fd, p, ring); 
  10.     // 保存系統(tǒng)支持的屬性 
  11.     ring->features = p->features; 
  12.     return 0; 

我們重點(diǎn)看一下io_uring_queue_mmap。

  1. int io_uring_queue_mmap(int fd, struct io_uring_params *p, struct io_uring *ring){ 
  2.     int ret; 
  3.  
  4.     memset(ring, 0, sizeof(*ring)); 
  5.     ret = io_uring_mmap(fd, p, &ring->sq, &ring->cq); 
  6.     // 記錄flags和fd 
  7.     if (!ret) { 
  8.         ring->flags = p->flags; 
  9.         ring->ring_fd = fd; 
  10.     } 
  11.     return ret; 

繼續(xù)看io_uring_mmap。

  1. static int io_uring_mmap(int fd, struct io_uring_params *p, 
  2.              struct io_uring_sq *sq, struct io_uring_cq *cq){ 
  3.     size_t size
  4.     int ret; 
  5.     // 請求隊列需要映射的內(nèi)存大小,即整個結(jié)構(gòu)體struct io_rings結(jié)構(gòu)體的大小 
  6.     sq->ring_sz = p->sq_off.array + p->sq_entries * sizeof(unsigned); 
  7.     // 請求隊列和完成隊列映射的內(nèi)存大小一樣,等于請求隊列的 
  8.     cq->ring_sz = sq->ring_sz; 
  9.     // 映射并拿到虛擬地址,大小是sq->ring_sz 
  10.     sq->ring_ptr = mmap(0, sq->ring_sz, PROT_READ | PROT_WRITE, 
  11.             MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING); 
  12.     cq->ring_ptr = sq->ring_ptr; 
  13.     // 通過首地址和偏移拿到對應(yīng)字段的地址 
  14.     sq->khead = sq->ring_ptr + p->sq_off.head; 
  15.     sq->ktail = sq->ring_ptr + p->sq_off.tail; 
  16.     sq->kring_mask = sq->ring_ptr + p->sq_off.ring_mask; 
  17.     sq->kring_entries = sq->ring_ptr + p->sq_off.ring_entries; 
  18.     sq->kflags = sq->ring_ptr + p->sq_off.flags; 
  19.     sq->kdropped = sq->ring_ptr + p->sq_off.dropped; 
  20.     sq->array = sq->ring_ptr + p->sq_off.array; 
  21.     // 映射保存請求隊列節(jié)點(diǎn)的內(nèi)存 
  22.     size = p->sq_entries * sizeof(struct io_uring_sqe); 
  23.     sq->sqes = mmap(0, size, PROT_READ | PROT_WRITE, 
  24.                 MAP_SHARED | MAP_POPULATE, fd, 
  25.                 IORING_OFF_SQES); 
  26.     // 同上 
  27.     cq->khead = cq->ring_ptr + p->cq_off.head; 
  28.     cq->ktail = cq->ring_ptr + p->cq_off.tail; 
  29.     cq->kring_mask = cq->ring_ptr + p->cq_off.ring_mask; 
  30.     cq->kring_entries = cq->ring_ptr + p->cq_off.ring_entries; 
  31.     cq->koverflow = cq->ring_ptr + p->cq_off.overflow; 
  32.     cq->cqes = cq->ring_ptr + p->cq_off.cqes; 
  33.     if (p->cq_off.flags) 
  34.         cq->kflags = cq->ring_ptr + p->cq_off.flags; 
  35.     return 0; 

io_uring_mmap除了保存一些常用的字段信息外,最重要的是做了內(nèi)存映射。我們看看mmap的最后一個參數(shù)分別是IORING_OFF_SQ_RING和IORING_OFF_SQES,接下來我們看看io_uring的mmap鉤子的實(shí)現(xiàn)。

  1. static int io_uring_mmap(struct file *file, struct vm_area_struct *vma){ 
  2.     size_t sz = vma->vm_end - vma->vm_start; 
  3.     unsigned long pfn; 
  4.     void *ptr; 
  5.  
  6.     ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); 
  7.  
  8.     pfn = virt_to_phys(ptr) >> PAGE_SHIFT; 
  9.     return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);}static void *io_uring_validate_mmap_request(struct file *file, 
  10.                         loff_t pgoff, size_t sz){ 
  11.     struct io_ring_ctx *ctx = file->private_data; 
  12.     loff_t offset = pgoff << PAGE_SHIFT; 
  13.     struct page *page; 
  14.     void *ptr; 
  15.  
  16.     switch (offset) { 
  17.     case IORING_OFF_SQ_RING: 
  18.     case IORING_OFF_CQ_RING: 
  19.         ptr = ctx->rings; 
  20.         break; 
  21.     case IORING_OFF_SQES: 
  22.         ptr = ctx->sq_sqes; 
  23.         break; 
  24.     default
  25.         return ERR_PTR(-EINVAL); 
  26.     } 
  27.  
  28.     page = virt_to_head_page(ptr); 
  29.     if (sz > page_size(page)) 
  30.         return ERR_PTR(-EINVAL); 
  31.  
  32.     return ptr; 

這里設(shè)計的內(nèi)容涉及到了復(fù)雜的內(nèi)存管理,從代碼中我們大概知道,返回的地址分別是ctx->rings和ctx->sq_sqes。即我們操作mmap返回的虛擬地址時,映射到內(nèi)核的數(shù)據(jù)結(jié)構(gòu)是ctx的字段。這樣就完成了數(shù)據(jù)共享。最后形成的架構(gòu)圖如下。

至此,分析就告一段落,io_uring的實(shí)現(xiàn)實(shí)在是復(fù)雜,需要反復(fù)閱讀和思考,才能慢慢理解和了解它的原理。

后記:io_uring作為新一代IO框架,未來應(yīng)該會在各大軟件中使用,尤其是對性能有極高要求的服務(wù)器,所以是非常值得關(guān)注和學(xué)習(xí)的。

 

責(zé)任編輯:武曉燕 來源: 編程雜技
相關(guān)推薦

2023-02-07 19:46:35

NIOCQ內(nèi)核

2023-04-12 18:36:20

IO框架內(nèi)核

2023-10-20 06:26:51

Libuvio_uring

2021-07-11 23:25:29

Libuvepoll文件

2021-07-03 08:04:10

io_uringNode.js異步IO

2023-12-28 11:24:29

IO系統(tǒng)請求

2025-02-03 09:53:42

2020-09-30 06:44:39

存儲IO

2021-06-26 07:04:24

Epoll服務(wù)器機(jī)制

2021-09-05 17:46:21

云計算No.jsio_uringJS

2022-03-03 08:01:41

阻塞與非阻塞同步與異步Netty

2021-06-18 06:02:24

內(nèi)核文件傳遞

2016-09-20 15:21:35

LinuxInnoDBMysql

2020-06-17 16:43:40

網(wǎng)絡(luò)IO框架

2020-09-23 12:32:18

網(wǎng)絡(luò)IOMySQL

2019-03-27 09:14:38

CPU內(nèi)核應(yīng)用程序

2017-04-05 20:00:32

ChromeObjectJS代碼

2021-05-06 10:33:30

C++Napiv8

2014-04-22 09:51:24

LongAdderAtomicLong

2017-01-15 23:46:37

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號