struct buffer_head 效率低下

发布于 2024-10-29 05:10:52 字数 117 浏览 3 评论 0原文

有人能说出为什么 Linux Kernel 2.4 之前使用的 struct buffer_head 结构效率低下的原因以及从内核 2.5.1 开始重新设计的新块层中使用的 struct bio 结构中实现了哪些内容吗?

Can anybody tell the reason why the struct buffer_head structure which were used till Linux Kernel 2.4 is inefficient and what things are implemented in the struct bio structure used in the new redesigned block layer from kernel 2.5.1 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2024-11-05 05:10:52

在 2.5 之前的内核中,buffer_head 曾经是 IO 的单位。然而,根据 buffer_head 进行 IO 可能效率很低。例如,跨越多个块的单个写入将导致创建多个 buffer_head 对象,从而浪费空间和时间。因此,buffer_head 结构最适合提供块到物理内存的映射,这正是它从 2.5 开始所做的事情(也是它所做的唯一事情)内核开始。


另一方面,struct bio 被设计为后续内核版本中的 IO 单元。 **它代表当前内核中的活动 IO strong>.** 请注意,块到内存的映射仍然保留在 buffer_head 中。然而,buffer_head 现在并没有因包含当前活动 IO 信息的字段而过载。因此,当文件系统等更高级别的代码发出 IO 时,请求就会添加到 request_queue 中。该请求又包含指向 bio 结构的指针。设备驱动程序控制发出 IO 的设备,接收请求并为其提供服务。

struct bio 结构还用于分割 IO,例如,通过 RAID 磁盘设备驱动程序。查看 struct bio 定义,您会发现它维护了一个 io_vector 数组(它们只不过是内存页的偏移量)。此外,bi_idx 用于索引这些 io_vec。通过对不同的磁盘使用不同的 bi_idx 值,每个磁盘将拾取一组不同的 io_vec 进行写入(或读取),因此 IO 将以真正的并行方式完成。

In pre-2.5 kernels, buffer_head used to be the unit of IO . However, doing IO in terms of buffer_head can be inefficient. For example, a single write spanning multiple blocks would result in creation of multiple buffer_head objects, thus wasting space and time. So, a buffer_head struct is best used for providing the block-to-physical memory mapping, which is exactly what it does (and the only thing that it does) from 2.5 kernel onwards.


On the other hand, struct bio is designed to be the unit of IO in later kernel releases. **It represents the active IOs currently in the kernel.** Note that the mapping of block-to-memory is still maintained in buffer_head. However, the buffer_head is now not overloaded with the fields that contain info about currently active IO. So, when higher level code such as filesystem, issues an IO, a request is added to the request_queue. The request in turn contains a pointer to the bio structure. The device driver that controls the device on which IO is issued, picks up the request and serves it.

The struct bio structure is also used for splitting IOs, for example, by RAID disk device drivers. Look at the struct bio definition and you will find that it maintains an array of io_vectors (which are nothing but offsets into memory pages). Also, bi_idx is used to index into these io_vec. By using a different bi_idx value for different disk, each disk will pick up a different set of io_vec to write (or read), and thus the IO would be done in a true parallel fashion.

何止钟意 2024-11-05 05:10:52

使用来源,卢克!

来自 include/linux/buffer_head.h

/*
 * Historically, a buffer_head was used to map a single block
 * within a page, and of course as the unit of I/O through the
 * filesystem and block layers.  Nowadays the basic I/O unit
 * is the bio, and buffer_heads are used for extracting block
 * mappings (via a get_block_t call), for tracking state within
 * a page (via a page_mapping) and for wrapping bio submission
 * for backward compatibility reasons (e.g. submit_bh).
 */
struct buffer_head {
[ ... ]

以及来自 linux/include/bio.h

/*
 * main unit of I/O for the block layer and lower layers (ie drivers and
 * stacking drivers)
 */
struct bio {
[ ... ]

这两个解耦 I/O 特性和“更高级别”的操作(I/ O 通过文件系统)和“低级别”(I/O 通过块开发)。这可以实现更高的性能,因为在某处等待的缓冲层不会阻止块层在其他地方执行工作。关于它的一些解释可以在此处找到,Jens Axboe 对此进行了讨论在这次采访中简要介绍。

Use the source, luke !

From include/linux/buffer_head.h:

/*
 * Historically, a buffer_head was used to map a single block
 * within a page, and of course as the unit of I/O through the
 * filesystem and block layers.  Nowadays the basic I/O unit
 * is the bio, and buffer_heads are used for extracting block
 * mappings (via a get_block_t call), for tracking state within
 * a page (via a page_mapping) and for wrapping bio submission
 * for backward compatibility reasons (e.g. submit_bh).
 */
struct buffer_head {
[ ... ]

And from linux/include/bio.h:

/*
 * main unit of I/O for the block layer and lower layers (ie drivers and
 * stacking drivers)
 */
struct bio {
[ ... ]

The two decouple I/O characteristics and operations for "higher levels" (I/O through filesystems) and "low levels" (I/O through block devs). This allows for higher performance because a buffer layer waiting somewhere doesn't stop the block layer from performing work elsewhere. Some explanation about it can be found here, and Jens Axboe talks about it in this interview briefly.

还给你自由 2024-11-05 05:10:52

Robert Love 的 LKD 3rd 对此有非常详细的解释。

旧与新

缓冲区头与新缓冲区头的区别
bio结构很重要。bio结构代表一个I/O
操作,可能包括内存中的一页或多页。另一方面
另一方面,buffer_head 结构代表单个缓冲区,其中
描述磁盘上的单个块。因为缓冲区头绑定到
单个页面中的单个磁盘块,缓冲区头导致
将请求不必要地划分为块大小的块,只是为了
之后重新组装它们。由于生物结构轻,
可以描述不连续的块并且不会不必要地分割 I/O
运营。从 struct buffer_head 切换到提供的 struct bio
还有其他好处:

  • bio结构体可以很方便的表示高端内存,因为struct bio
    仅处理物理页面,而不处理直接指针。

  • bio结构体既可以表示普通页I/O,也可以表示直接I/O(I/O
    不通过页面缓存的操作——参见第 16 章“
    页面缓存和页面写回,”有关页面缓存的讨论)。

  • 生物结构可以轻松执行分散-聚集(矢量)
    块I/O操作,以及操作中涉及的数据
    源自多个物理页面。

  • 生物结构很多
    比缓冲头更轻,因为它只包含
    表示块 I/O 操作所需的最少信息,而不是
    与缓冲区本身相关的不必要的信息。

Robert Love's LKD 3rd have a very detail explanation on this.

The Old Versus the New

The difference between buffer heads and the new
bio structure is important.The bio structure represents an I/O
operation, which may include one or more pages in memory. On the other
hand, the buffer_head structure represents a single buffer, which
describes a single block on the disk. Because buffer heads are tied to
a single disk block in a single page, buffer heads result in the
unnecessary dividing of requests into block-sized chunks, only to
later reassemble them. Because the bio structure is lightweight, it
can describe discontiguous blocks and does not unnecessarily split I/O
operations. Switching from struct buffer_head to struct bio provided
other benefits, as well:

  • The bio structure can easily represent high memory, because struct bio
    deals with only physical pages and not direct pointers.

  • The bio structure can represent both normal page I/O and direct I/O (I/O
    opera- tions that do not go through the page cache—see Chapter 16,“The
    Page Cache and Page Writeback,” for a discussion on the page cache).

  • The bio structure makes it easy to perform scatter-gather (vectored)
    block I/O operations, with the data involved in the operation
    originating from multiple physi- cal pages.

  • The bio structure is much
    more lightweight than a buffer head because it contains only the
    minimum information needed to represent a block I/O operation and not
    unnecessary information related to the buffer itself.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文