Linux内核的文件预读readahead

Linux的文件预读readahead,指Linux系统内核将指定文件的某区域预读进页缓存起来,便于接下来对该区域进行读取时,不会因缺页(page fault)而阻塞。因为从内存读取比从磁盘读取要快很多。预读可以有效的减少磁盘的寻道次数和应用程序的I/O等待时间,是改进磁盘读I/O性能的重要优化手段之一。

维基百科上关于readhead的介绍资料:

readahead is a system call of the Linux kernel that loads a file‘s contents into the page cache, providing that way a file prefetching technology. When a file is subsequently accessed, its contents are read from the main memory (RAM) rather than from a hard disk drive (HDD), resulting in much lower file access latencies due to much higher performance of the main memory.[1][2]

Many Linux distributions use readahead on a list of commonly used files to speed up booting. In such a setup, if the kernel is booted with the profile boot parameter, it will record all file accesses during bootup and write a new list of files to be read during later boot sequences. This will make additional installed services start faster, because they are not included in the default readahead list.[3]

In Linux distributions that use systemd, readahead binary (as part of the boot sequence) is replaced by systemd-readahead.[4][5] However, support for readahead was removed from systemd in its version 219, being described as unmaintained and unable to provide expected performance benefits.[6]

Certain experimental page-level prefetching systems have been developed to further improve performance.[7]

查看、设置readhead使用命令blockdev。如下所示:

[[email protected] ~]# /sbin/blockdev
 

Usage:

  blockdev -V

  blockdev --report [devices]

  blockdev [-v|-q] commands devices

 

Available commands:

        --getsz                        get size in 512-byte sectors

        --setro                        set read-only

        --setrw                        set read-write

        --getro                        get read-only

        --getss                        get logical block (sector) size

        --getpbsz                      get physical block (sector) size

        --getiomin                     get minimum I/O size

        --getioopt                     get optimal I/O size

        --getalignoff                  get alignment offset

        --getmaxsect                   get max sectors per request

        --getbsz                       get blocksize

        --setbsz BLOCKSIZE             set blocksize on file descriptor opening the block device

        --getsize                      get 32-bit sector count

        --getsize64                    get size in bytes

        --setra READAHEAD              set readahead

        --getra                        get readahead

        --setfra FSREADAHEAD           set filesystem readahead

        --getfra                       get filesystem readahead

        --flushbufs                    flush buffers

        --rereadpt                     reread partition table

查看磁盘的预读扇区

[[email protected] ~]# /sbin/blockdev --getra /dev/sda
 

256

 

[[email protected] ~]# /sbin/blockdev --getra /dev/sdb

 

256

 

[[email protected] ~]# 

设置磁盘的预读扇区

[[email protected] ~]# /sbin/blockdev --getra /dev/sda
256

[[email protected] ~]# /sbin/blockdev --setra 2048 /dev/sda

[[email protected] ~]# /sbin/blockdev --getra /dev/sda

2048

[[email protected] ~]# /sbin/blockdev --getra /dev/sdb

256

[[email protected] ~]# /sbin/blockdev --setra 2048 /dev/sdb

[[email protected] ~]# /sbin/blockdev --getra /dev/sdb

2048

[[email protected] ~]# 

必须将其写入配置文件/etc/rc.local,否则重启就会失效。

 
[[email protected] ~]# echo ‘/sbin/blockdev --setra 2048 /dev/sda‘ >> /etc/rc.local

[[email protected] ~]# more /etc/rc.local

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don‘t

# want to do the full Sys V style init stuff.

 

touch /var/lock/subsys/local

/sbin/blockdev --setra 2048 /dev/sda

[[email protected] ~]# echo ‘/sbin/blockdev --setra 2048 /dev/sdb‘ >> /etc/rc.local

[[email protected] ~]# more /etc/rc.local

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don‘t

# want to do the full Sys V style init stuff.

 

touch /var/lock/subsys/local

/sbin/blockdev --setra 2048 /dev/sda

/sbin/blockdev --setra 2048 /dev/sdb

参考资料:

https://en.wikipedia.org/wiki/Readahead

http://www.hustyx.com/cpp/96/

时间: 2024-08-14 09:48:44

Linux内核的文件预读readahead的相关文章

Linux 内核的文件 Cache 管理机制介绍-ibm

https://www.ibm.com/developerworks/cn/linux/l-cache/ 1 前言 自从诞生以来,Linux 就被不断完善和普及,目前它已经成为主流通用操作系统之一,使用得非常广泛,它与 Windows.UNIX 一起占据了操作系统领域几乎所有的市场份额.特别是在高性能计算领域,Linux 已经成为一个占主导地位的操作系统,在2005年6月全球TOP500 计算机中,有 301 台部署的是 Linux 操作系统.因此,研究和使用 Linux 已经成为开发者的不可回

Linux 内核的文件 Cache 管理机制介绍

Linux 内核的文件 Cache 管理机制介绍 文件 Cache 管理是 Linux 内核中一个很重要并且较难理解的组成部分.本文详细介绍了 Linux 内核中文件 Cache 管理的各个方面,希望能够对开发者理解相关代码有所帮助. http://www.ibm.com/developerworks/cn/linux/l-cache/ http://www.cnblogs.com/MYSQLZOUQI/p/4857437.html 1 前言 自从诞生以来,Linux 就被不断完善和普及,目前它

Linux文件预读对系统的影响

Linux系统很重要的一个性能提升点就是它的Pagecache, 因为内存比IO快太多了,所以大家都想进办法来利用这个cache. 文件系统也不例外,为了达到高性能,文件读取通常采用预读来预测用户的行为,把用户可能需要的数据预先读取到cache去,达到高性能的目的. Linux各个发行版readahead的实现差异很大,我们这里重点讨论2.6.18, RHEL 5U4发行版的行为.文件预读的实现主要在mm/readahead.c中,代码才603行. 预读的流程大概是这样的,用户需要文件页面的时候

linux内核头文件kdev_t.h 宏定义解析

kdev_t.h 宏定义解析 这个header file反正不多,直接原因是--遇到了,就搞定它! dev_t 类型的变量定义在linux/types.h 用来保存设备编号--包括主设备号和次设备号.dev_t 是一个32位的数,其中12位用来表示设备号,其余20位用来表示次设备号. 始终不要对这32位是高12位是主设备号还是低2位是主设备号做出假定,不利于代码的可移植性,始终记得使用宏定义来处理dev_t ! 都在这里了: #define MINORBITS 20 //次设备号的占位数目 #d

linux内核头文件 cdev.h 解析

遇到一个内核API--cdev_init 就找到这里来了. #ifndef _LINUX_CDEV_H #define _LINUX_CDEV_H #include <linux/kobject.h #include <linux/kdev_t.h> #include <linux/list.h> struct file_operations; struct inode; struct module; struct cdev { struct kobject kobj; st

首先看linux内核重要文件grub

linux内核启动修复 首先看linux内核重要文件grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /b

Linux内核Makefile文件(翻译自内核手册)

转载自:http://www.cnblogs.com/jason-lu/p/3728198.html --译自Linux3.9.5 Kernel Makefiles(内核目录documention/kbuild/makefiles.txt) kbuild(kernel build) 内核编译器 This document describes the Linux kernel Makefiles 本文当介绍了Linux内核的Makefile === Table of Contents=== 目录

linux 内核头文件及内核库文件

与用户空间应用不同的是,内核不能调用标准C库或其它任何不属于内核的库.主要原因是,速度及大小的限制. 标准C库对内核而言实在是太大了. 许多常用的的 libc 函数已经在内核中实现了.内核中的库文件是在内核的根目录下 lib 目录下存放. 如常用的字符串操作函数是在 lib/string.c 中定义的,调用时主需要将头文件 <linux/string.h>包含进即可. 关于内核的头文件,内核是不能包含内核之外的头文件的,如同内核不能调用内核之外的库一样. 1)基本的头文件放在内核的 inclu

深入解析Linux内核I/O剖析(open,write实现)

Linux内核将一切视为文件,那么Linux的文件是什么呢?其既可以是事实上的真正的物理文件,也可以是设备.管道,甚至还可以是一块内存.狭义的文件是指文件系统中的物理文件,而广义的文件则可以是Linux管理的所有对象.这些广义的文件利用VFS机制,以文件系统的形式挂载在Linux内核中,对外提供一致的文件操作接口. 从数值上看,文件描述符是一个非负整数,其本质就是一个句柄,所以也可以认为文件描述符就是一个文件句柄.那么何为句柄呢?一切对于用户透明的返回值,即可视为句柄.用户空间利用文件描述符与内