在以下页面https://lwn.net/Articles/810414/
IORING_OP_READ_FIXED IORING_OP_WRITE_FIXED 这些操作码也提交 I/O 操作,但它们使用已经映射到内核的“注册”缓冲区,从而减少了总开销。
但是我在网上找不到一个关于如何使用它的例子。在 io_uring_enter 它说
在提交队列条目的操作码字段中指定了 EFAULT IORING_OP_READ_FIXED 或 IORING_OP_WRITE_FIXED,但要么没有为此 io_uring 实例注册缓冲区,要么 addr 和 len 描述的地址范围不适合在 buf_index 处注册的缓冲区。
在我看来,我应该选择一个内存地址并阻止它使用,但是使用像 0x555555500000 和 len as 4096 这样的地址会给我同样的错误。
IORING_OP_READ_FIXED 是如何工作的?下面是 IORING_OP_READ 的一个工作示例
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <linux/io_uring.h>
#define read_barrier() __asm__ __volatile__("":::"memory")
#define write_barrier() __asm__ __volatile__("":::"memory")
int main(int argc, char *argv[])
{
struct io_uring_params uring;
memset(&uring, 0, sizeof(uring));
auto queue_size = 5;
auto ring_fd = syscall(__NR_io_uring_setup, queue_size, &uring);
auto*uring_ptr = (char*)mmap(0, uring.sq_off.array + uring.sq_entries * 4, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, ring_fd, IORING_OFF_SQ_RING);
auto*submit_entries = (io_uring_sqe*)mmap(0, uring.sq_entries * sizeof(struct io_uring_sqe), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, ring_fd, IORING_OFF_SQES);
unsigned &sqHead = *(unsigned*)(uring_ptr + uring.sq_off.head);
unsigned &sqTail = *(unsigned*)(uring_ptr + uring.sq_off.tail);
unsigned &sqMask = *(unsigned*)(uring_ptr + uring.sq_off.ring_mask);
unsigned &sqFlags = *(unsigned*)(uring_ptr + uring.sq_off.flags);
unsigned *sqArray = (unsigned*)(uring_ptr + uring.sq_off.array);
unsigned &cqHead = *(unsigned*)(uring_ptr + uring.cq_off.head);
unsigned &cqTail = *(unsigned*)(uring_ptr + uring.cq_off.tail);
unsigned &cqMask = *(unsigned*)(uring_ptr + uring.cq_off.ring_mask);
io_uring_cqe *cqes = (io_uring_cqe*)(uring_ptr + uring.cq_off.cqes);
int fd[2];
fd[0] = open(argv[1], O_RDONLY);
struct stat stat;
if (fstat(fd[0], &stat) < 0) {
perror("fstat");
return -1;
}
int size_aligned = (stat.st_size & ~63) + (stat.st_size & 63 ? 64 : 0);
auto*fileBuf = (unsigned char*)malloc(size_aligned*2);
for(int i=0; i<1; i++)
{
io_uring_sqe&sqe = submit_entries[sqTail & sqMask];
sqe.fd = fd[i];
sqe.flags = 0;
sqe.opcode = IORING_OP_READ;
sqe.addr = (unsigned long long)fileBuf+i*size_aligned;
sqe.len = size_aligned;
sqe.user_data = (unsigned long long)fileBuf+i*size_aligned;
sqArray[sqTail&sqMask] = sqTail&sqMask;
sqTail++;
}
write_barrier();
//int ret = syscall(__NR_io_uring_enter, ring_fd, 2, 2, IORING_ENTER_GETEVENTS, 0);
int ret = syscall(__NR_io_uring_enter, ring_fd, 1, 1, IORING_ENTER_GETEVENTS, 0);
//int ret = syscall(__NR_io_uring_enter, ring_fd, 1, 0, IORING_ENTER_GETEVENTS, 0);
//sleep(1);
read_barrier();
while (cqHead != cqTail)
{
unsigned long long a = cqHead;
unsigned long long b = cqTail;
unsigned long long c = cqMask;
auto index=cqHead & cqMask;
io_uring_cqe&cqe = cqes[index];
auto u=cqe.user_data;
auto f=cqe.flags;
auto r=cqe.res;
puts((const char*)u);
cqHead++;
}
int a=0;
return 0;
}