io_uring 内部如何运作?

How io_uring internally works?

显然,Linux 已经有一个 Asyn-IO (AIO) API。我相信它不是完全异步的。那么 AIO 的问题是什么?以及 io_uring 如何克服它?

PS:我尝试阅读 https://kernel.dk/io_uring.pdf 但无法完全理解,因为我与 C 语言脱节。

Apparently, Linux already had an Asyn[c]-IO (AIO) API. I believe it is not fully asynchronous. So what was the issue with AIO?

如果您格外小心并符合其所有约束,“旧”Linux AIO 接口将以异步方式运行。但是,如果您“破坏”任何(隐藏的)规则 submission 可能会突然(并且悄无声息地)以同步方式运行(即提交块非确定性地和不方便的长时间段). asynchronous IO io_submit latency in Ubuntu Linux (the overall issues are also listed in section 1.0 of the io_uring document 你 linked).

的答案给出了许多“规则”中的一些。

how io_uring overcomes it?

  • 这是一个完全不同的界面(见此 answer on the "Is there really no asynchronous block I/O on Linux?"),更不容易出错。
  • 它有一个 workqueue/thread pool 机制,当它知道在 提交结果 可以 returned(因此它能够 return 控制返回给调用者)。这允许它在更多(希望是所有!)提交案例中保持异步。
  • 它有一个可选的特权模式 (IORING_SETUP_SQPOLL),您 don't even have to make syscalls to submit/retrieve results。如果你“只是”操纵内存内容,你就很难在你从未打过的电话上被阻止!

How io_uring internally works?

有两个环形缓冲区,第一个环形缓冲区用于对提交进行排队,当工作完成时,“结果”通过第二个环形缓冲区(包含完成)宣布。如果您对 C structs/C 函数接口之类的东西感到不舒服,虽然很难给您提供非常高层次的视图,但您可能会喜欢这个 video by Jens presenting io_uring nonetheless and find the explanations in https://www.scylladb.com/2020/05/05/how-io_uring-and-ebpf-will-revolutionize-programming-in-linux/ and https://mattermost.com/blog/iouring-and-go/ 更易于访问。

io_uring 相对于 Linux AIO 的优势并不止于更好的异步性!请参阅前面提到的 link 以获得 "Is there really no asynchronous block I/O on Linux?" 的其他好处列表...