I don’t remember when the performance problems started when copying files, but then I didn’t attach much importance to it as I rarely copied files. Relatively recently, I had a high-speed connection to the Internet at my disposal, and now I often copy / download large files and the problem of performance loss has become very urgent for me.
I didn’t get results from googlegle and I started digging deeper, it turned out that many of the ubuntuaters have high CPU load, but not everyone has a high search problem, so I decided to write a small how-to to eliminate high load on processor when copying files.
Problem
With high-speed torrent downloads, multithreaded copying from disk to disk, to flash drives, the processor load surpasses 100%,
LA is growing rapidly.
At the same time, everything works well on file operations with a small number of threads.
')
A bit of theory
There are such things in the operating system, called
IO schedulers (
IO schedulers ), which are a layer between block devices and low-level drivers. The task of the scheduler is to provide the process with optimal access to the requested disk device. In the case of a hard disk, this means minimizing the movements of the read head.
The activity of the planners is reduced to the following aspects:
- Increase performance by reordering and merging queries
- Prevent freezes and chafing of read data by writing
- Honest allocation of access time to resources for different processes
There are many planners and, as it turned out, the problem solved by this post was connected with them.
Consider briefly the most common.
The simplest scheduler that works on the principle of
FIFO . There is no reordering, only requests that are nearby in the queue can be merged. Good for random access devices like Flash memory.
A scheduler that puts a higher priority on read requests than on write requests. Requests are reordered, but the request processing time must not exceed the specified limits (by default, 0.5 s for reading, 5 s for writing)
4 queues are used for implementation: 2
sorted-by-start-sector
queues (for reading and writing) and 2 FIFO queues (for reading and writing too). Usually, requests are taken from sorted queues, but if the deadline (timeout) of the first request from the FIFO queue is pressed, the query processing continues on the sorted queues from this element.
Best suited for organizing databases.
CFQ - Completely Fair Queuing
The goal of this scheduler is to honestly allocate access time to the resource for all requesters. CFQ can be configured to equalize processes, groups of processes, users.
By implementation, CFQ implies one FIFO queue per request initiator and switches between queues using the
Round-robin algorithm.
I do not know who needs such honesty, but there is no reordering of requests to minimize the movement of the hard disk read head in this scheduler.
The key idea is that before processing requests you can wait a bit, relax. But in these few milliseconds, information will gather in advance, which will allow us to make more balanced decisions on the order in which the requests will be executed.
Anticipatory scheduler is based on Deadline. Suitable for desktops, because The responsiveness of the I / O subsystem is maintained. Not suitable for RAID,
TCQ . It is not suitable in situations where synchronous requests are sent by different processes.
Decision
The default scheduler in Ubuntu 10.10 is CFQ, but as practice shows, it is this scheduler that causes a high load on the CPU when multi-threaded copying. We change the scheduler to another, for example, to Deadline and voila - there is no more CPU loading at 100%.
For SSD drives and Flash memory in general, as noted above, it is recommended to use Noop.
Some practice
Learn active scheduler
To view all available schedulers in the system and find out which of them is active, we execute:
$ cat /sys/block/{DEVICE-NAME}/queue/scheduler
Here
{DEVICE-NAME}
is the name of the block device, for example,
sda
.
For example, if the disk is
sda
, then you need to run:
$ cat /sys/block/sda/queue/scheduler
At the output we get a line like this:
noop anticipatory deadline [cfq]
In square brackets the current scheduler is indicated.
Change scheduler on the fly
To change the scheduler in real time without restarting execute:
$ sudo -i
# echo {SCHEDULER-NAME} > /sys/block/{DEVICE-NAME}/queue/scheduler
Here
{SCHEDULER-NAME}
is one of the schedulers present in the system, I have this:
noop anticipatory deadline cfq
. For example, to put the
deadline
scheduler, we execute
$ sudo -i
# echo deadline > /sys/block/sda/queue/scheduler
Fixing Scheduler Settings
Next, we need to force Ubuntu to use our chosen scheduler after rebooting. To do this, add the line
GRUB_CMDLINE_LINUX_DEFAULT="elevator={SCHEDULER-NAME}"
to the GRUB 2 config.
$ sudo nano /etc/default/grub
UPD: After making the changes, you need to update the grub configuration:
$ sudo update-grub
If you have grub, not grub2, then add the line
elevator={SCHEDULER-NAME}
to / boot / grub / grub.conf.
Help in choosing a planner
On Habré already
wrote about the automated choice of the scheduler. Of course, a simple check of the reading speed is not enough for an optimal choice, but it can give some idea.
useful links
Links used in writing the post:
Linux I / O Schedulers PresentationLinux Io Scheduler - Waikato Linux Users GroupA comparison of I / O SchedulersLinux I / O schedulers (linux kernel io schedule optimization tune)Book about Grub 2Having told friends about this tweak, I was surprised that no one knew about it, decided to post on Habré, suddenly someone would come in handy.
PS
UPD:This article told only about one, in fact the simplest way to solve a problem related, c so-called.
bug 12309 . There are more tips to solve this problem:
- do not change the CFQ scheduler, but
configure- put the
zen core-
adjust swap aggressivity- buy a hard disk with a hardware scheduler and a lot of RAM (so as not to leave Swap)
This text is distributed under the terms of the license CC BY-SA
Original author (problem, solution) - g3n1uss . Co-author (theory, design) - Your humble servant.