简体   繁体   English

选择 Linux I/O 调度器

[英]Selecting a Linux I/O Scheduler

I read that it's supposedly possible to change the I/O scheduler for a particular device on a running kernel by writing to /sys/block/[disk]/queue/scheduler.我读到据说可以通过写入 /sys/block/[disk]/queue/scheduler 来更改正在运行的 kernel 上特定设备的 I/O 调度程序。 For example I can see on my system:例如,我可以在我的系统上看到:

anon@anon:~$ cat /sys/block/sda/queue/scheduler 
noop anticipatory deadline [cfq] 

that the default is the completely fair queuing scheduler.默认是完全公平的排队调度程序。 What I'm wondering is if there is any use in including all four schedulers in my custom kernel. It would seem that there's not much point in having more than one scheduler compiled in unless the kernel is smart enough to select the correct scheduler for the correct hardware, specifically the 'noop' scheduler for flash based drives and one of the others for a traditional hard drive.我想知道的是,在我的自定义 kernel 中包含所有四个调度程序是否有任何用处。似乎编译一个以上的调度程序没有多大意义,除非 kernel 足够聪明,可以 select 为正确的调度程序正确的硬件,特别是用于基于 flash 的驱动器的“noop”调度程序,以及用于传统硬盘驱动器的其他调度程序之一。

Is this the case?是这样吗?

As documented in /usr/src/linux/Documentation/block/switching-sched.txt , the I/O scheduler on any particular block device can be changed at runtime./usr/src/linux/Documentation/block/switching-sched.txt中所述,任何特定块设备上的 I/O 调度程序都可以在运行时更改。 There may be some latency as the previous scheduler's requests are all flushed before bringing the new scheduler into use, but it can be changed without problems even while the device is under heavy use.可能会有一些延迟,因为在使用新的调度程序之前,之前的调度程序的请求都被刷新了,但即使设备正在大量使用,也可以毫无问题地更改它。

# cat /sys/block/hda/queue/scheduler
noop deadline [cfq]
# echo anticipatory > /sys/block/hda/queue/scheduler
# cat /sys/block/hda/queue/scheduler
noop [deadline] cfq

Ideally, there would be a single scheduler to satisfy all needs.理想情况下,应该有一个调度程序来满足所有需求。 It doesn't seem to exist yet.它似乎还不存在。 The kernel often doesn't have enough knowledge to choose the best scheduler for your workload: kernel 通常没有足够的知识来为您的工作负载选择最佳调度程序:

  • noop is often the best choice for memory-backed block devices (eg ramdisks) and other non-rotational media (flash) where trying to reschedule I/O is a waste of resources noop通常是内存支持的块设备(例如 ramdisks)和其他非旋转介质(闪存)的最佳选择,在这些设备中尝试重新安排 I/O 是一种资源浪费
  • deadline is a lightweight scheduler which tries to put a hard limit on latency deadline是一个轻量级的调度器,它试图对延迟施加硬性限制
  • cfq tries to maintain system-wide fairness of I/O bandwidth cfq尝试维护系统范围内的 I/O 带宽公平性

The default was anticipatory for a long time, and it received a lot of tuning, but was removed in 2.6.33 (early 2010).默认值在很长一段时间内都是anticipatory的,并且进行了大量调整,但在2.6.33 (2010 年初)中被删除。 cfq became the default some while ago, as its performance is reasonable and fairness is a good goal for multi-user systems (and even single-user desktops). cfq不久前成为默认值,因为它的性能是合理的,公平是多用户系统(甚至单用户桌面)的一个很好的目标。 For some scenarios -- databases are often used as examples, as they tend to already have their own peculiar scheduling and access patterns, and are often the most important service (so who cares about fairness?) -- anticipatory has a long history of being tunable for best performance on these workloads, and deadline very quickly passes all requests through to the underlying device.对于某些场景——数据库经常被用作示例,因为它们往往已经有自己独特的调度和访问模式,并且通常是重要的服务(所以谁在乎公平性?)—— anticipatory有很长的历史可调整以在这些工作负载上获得最佳性能,并且deadline非常快速地将所有请求传递到底层设备。

It's possible to use a udev rule to let the system decide on the scheduler based on some characteristics of the hw.可以使用 udev 规则让系统根据硬件的某些特性来决定调度程序。
An example udev rule for SSDs and other non-rotational drives might look like SSD 和其他非旋转驱动器的示例 udev 规则可能如下所示

# set noop scheduler for non-rotating disks
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="noop"

inside a new udev rules file (eg, /etc/udev/rules.d/60-ssd-scheduler.rules ).在新的 udev 规则文件中(例如/etc/udev/rules.d/60-ssd-scheduler.rules )。 This answer is based on the debian wiki此答案基于debian wiki

To check whether ssd disks would use the rule, it's possible to check for the trigger attribute in advance:要检查 ssd 磁盘是否会使用该规则,可以提前检查 trigger 属性:

for f in /sys/block/sd?/queue/rotational; do printf "$f "; cat $f; done

The aim of having the kernel support different ones is that you can try them out without a reboot;让 kernel 支持不同的目的是您可以在不重新启动的情况下试用它们; you can then run test workloads through the sytsem, measure performance, and then make that the standard one for your app.然后,您可以通过系统运行测试工作负载,测量性能,然后将其作为您应用程序的标准。

On modern server-grade hardware, only the noop one appears to be at all useful.在现代服务器级硬件上,只有 noop 似乎完全有用。 The others seem slower in my tests.其他人在我的测试中似乎更慢。

You can set this at boot by adding the "elevator" parameter to the kernel cmdline (such as in grub.cfg)您可以在启动时通过将“elevator”参数添加到 kernel cmdline(例如在 grub.cfg 中)来设置它

Example:例子:

elevator=deadline

This will make "deadline" the default I/O scheduler for all block devices.这将使“deadline”成为所有块设备的默认 I/O 调度程序。

If you'd like to query or change the scheduler after the system has booted, or would like to use a different scheduler for a specific block device, I recommend installing and use the tool ioschedset to make this easy.如果您想在系统启动后查询或更改调度程序,或者想为特定块设备使用不同的调度程序,我建议安装并使用工具ioschedset以简化此操作。

https://github.com/kata198/ioschedset https://github.com/kata198/ioschedset

If you're on Archlinux it's available in aur:如果你使用的是 Archlinux,它在 aur 中可用:

https://aur.archlinux.org/packages/ioschedset https://aur.archlinux.org/packages/ioschedset

Some example usage:一些示例用法:

# Get i/o scheduler for all block devices
[username@hostname ~]$ io-get-sched
sda:    bfq
sr0:    bfq

# Query available I/O schedulers
[username@hostname ~]$ io-set-sched --list
mq-deadline kyber bfq none

# Set sda to use "kyber"
[username@hostname ~]$ io-set-sched kyber /dev/sda
Must be root to set IO Scheduler. Rerunning under sudo...

[sudo] password for username:
+ Successfully set sda to 'kyber'!

# Get i/o scheduler for all block devices to assert change
[username@hostname ~]$ io-get-sched
sda:    kyber
sr0:    bfq

# Set all block devices to use 'deadline' i/o scheduler
[username@hostname ~]$ io-set-sched deadline
Must be root to set IO Scheduler. Rerunning under sudo...

+ Successfully set sda to 'deadline'!
+ Successfully set sr0 to 'deadline'!

# Get the current block scheduler just for sda
[username@hostname ~]$ io-get-sched sda
sda:    mq-deadline

Usage should be self-explanatory.用法应该是不言自明的。 The tools are standalone and only require bash.这些工具是独立的,只需要 bash。

Hope this helps!希望这可以帮助!

EDIT: Disclaimer, these are scripts I wrote.编辑:免责声明,这些是我写的脚本。

The Linux Kernel does not automatically change the IO Scheduler at run-time. Linux Kernel 不会在运行时自动更改 IO 调度程序。 By this I mean, the Linux kernel, as of today, is not able to automatically choose an "optimal" scheduler depending on the type of secondary storage devise. During start-up, or during run-time, it is possible to change the IO scheduler manually .我的意思是,截至今天,Linux kernel 无法根据辅助存储 devise 的类型自动选择“最佳”调度程序。在启动期间或运行期间,可以更改IO手动调度程序。

The default scheduler is chosen at start-up based on the contents in the file located at /linux-2.6 /block/Kconfig.iosched .默认调度程序是在启动时根据位于 /linux-2.6 /block/Kconfig.iosched的文件中的内容选择的。 However, it is possible to change the IO scheduler during run-time by echo ing a valid scheduler name into the file located at /sys/block/[DEV]/queue/scheduler.但是,可以在运行时更改echo调度程序,方法是将有效的调度程序名称回显到位于 /sys/block/[DEV]/queue/scheduler 的文件中。 For example, echo deadline > /sys/block/hda/queue/scheduler例如echo deadline > /sys/block/hda/queue/scheduler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM