本文介绍了Linux异步(io_submit)写入v/s正常(缓冲)写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

既然写操作仍然是立即的(复制到内核缓冲区并返回),那么使用io_submit进行写操作有什么优势?

Since writes are immediate anyway (copy to kernel buffer and return), what's the advantage of using io_submit for writes?

实际上,它(aio/io_submit)似乎更糟,因为您必须在堆上分配写缓冲区,并且不能使用基于堆栈的缓冲区.

In fact, it (aio/io_submit) seems worse since you have to allocate the write buffers on the heap and can't use stack-based buffers.

我的问题仅是关于写,而不是读.

My question is only about writes, not reads.

我说的是相对较小的写入(最多只有几KB),而不是MB或GB,因此缓冲区复制应该不是一个大问题.

I am talking about relatively small writes (few KB at most), not MB or GB, so buffer copy should not be a big problem.

推荐答案

不一定要立即将缓冲区复制到内核中.

Copying a buffer into the kernel is not necessarily instantaneous.

首先,内核需要找到一个空闲页面.如果没有任何磁盘(很可能在沉重的磁盘写入压力下),则必须决定驱逐其中一个磁盘.如果它决定退出一个脏页(而不是驱逐您的进程),则必须在可以使用该页之前对其进行实际编写.

First the kernel needs to find a free page. If there is none (which is fairly likely under heavy disk-write pressure), it has to decide to evict one. If it decides to evict a dirty page (instead of evicting your process for instance), it will have to actually write it before it can use that page.

当饱和写入慢速驱动器时,Linux中存在一个相关问题,页面高速缓存将填充慢速驱动器支持的脏页.每当内核需要页面时,无论出于何种原因,获取页面都将花费很长时间,结果整个系统将冻结.

there's a related issue in linux when saturating writing to a slow drive, the page cache fills up with dirty pages backed by a slow drive. Whenever the kernel needs a page, for any reason, it takes a long time to acquire one and the whole system freezes as a result.

每个单独写入的大小与系统的写入压力无关.如果您已经排队等待一百万次小写操作,则可能是必须阻止的写操作.

The size of each individual write is less relevant than the write pressure of the system. If you have a million small writes already queued up, this may be the one that has to block.

关于分配是驻留在堆栈上还是堆上也没有那么重要.如果要高效地分配要写入的块,则可以使用专用的池分配器(来自堆),而不必为通用堆分配器付费.

Regarding whether the allocation lives on the stack or the heap is also less relevant. If you want efficient allocation of blocks to write, you can use a dedicated pool allocator (from the heap) and not pay for the general purpose heap allocator.

aio_write()通过完全不将缓冲区复制到内核来解决此问题,甚至可以直接从缓冲区中直接DMAd出(考虑到对齐要求),这意味着您也可能保存副本.

aio_write() gets around this by not copying the buffer into the kernel at all, it may even be DMAd straight out of your buffer (given the alignment requirements), which means you're likely to save a copy as well.

这篇关于Linux异步(io_submit)写入v/s正常(缓冲)写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:23