Ever copied or moved tens or hundreds of gigabytes of data? If you did, you surely noticed that the system becomes much less responsive during that time. On Linux you can avoid this with the help of the ionice
command.
What Is I/O Priority?
I/O is short for input/output. There are many types of I/O devices, but in this case it’s about storage devices.
Each process that wants to read or write data to such a device is assigned a scheduling class and priority number (or “nice” value). This applies on Linux to filesystems such as ext4. Other filesystems, such as ZFS, may implement slightly different methods for scheduling read/write operations on disk. Also, the CFQ scheduler should be active for this to work. You can check with
cat /sys/block/*/queue/scheduler
A process with a high “nice” value has a lower priority. The logic behind this is that the higher the number, the more “nice” the process is to other processes.
How Does I/O Priority Work?
A storage device obviously has a limited number of I/O operations it can perform per second (IOPS). So when two processes want to read/write at the same time, they each get a share of IOPS. If they have the same priority, they get around 50% IOPS each.
But IOPS can seem abstract and complicated. For the sake of simplicity, you can just think about the end result: read/write speeds. Assume your disk can write with 100MB/s at most. Process A begins a write operation. It writes to disk with 100MB/s. Process B comes along and wants to write to the same disk. It will write with around 50MB/s, bringing the process A write speed to the same value, 50MB/s. Now, if you assign process B a higher I/O nice value, it will write with 20MB/s and let process A write with 80MB/s. When process A is done, process B will start to write with 100MB/s.
This example is useful to understand something that might confuse some people. If a process has a very low priority (high nice value), it doesn’t mean that it will write slowly all the time. If it is the only process using the disk, it will read/write with maximum speed. But when other processes need the disk, it will temporarily get out of the way and let them use more disk bandwidth. For a copy/write operation that will take hours to complete, it’s a good idea to give it low priority if you want to use your computer during that time.
How to Use ionice Command
The general syntax of the command is:
ionice -c scheduling_class -n priority_nice_value command
ionice Scheduling Classes
Idle (class 3): Processes in this class only read/write when no other program needs disk access. This means the process reads/writes at full speed when it has no competition. When another program needs disk time, the process in the idle class will only read/write with whatever resources remain. From 100MB/s it may temporarily write with 5MB/s, then go back to 100MB/s when the other program is done accessing the disk. This is the perfect class for long-running jobs that you don’t want to slow down your system. No priority has to be specified for this class.
Example command:
ionice -c 3 cp /home/user/largefile /Backups
Best-effort (class 2): Takes a priority/nice value between 0 and 7. Remember, lower number means higher priority. Use this class when you want to fine-tune disk time for two or more processes.
For example, you want your backup to finish faster and assign it nice value 0. You’re also moving six movies to another disk but aren’t in a hurry, so you assign this a nice value of 7.
Example command:
ionice -c 2 -n 0 backup_command
Realtime (class 1): Should be used only if it’s vital that the process should write as soon as possible, uninterrupted by any other programs. Most users will never need this and should avoid this, except for special cases. Also supports nice values between 0 and 7. Only root can use this class, which means you will probably prefix the command with sudo. Note that a process in the realtime class with priority 0 may starve other processes of resources. In practical terms, it means that another program might have to wait for minutes or even hours to finish writing/reading a few megabytes of data. Use with care, only if you’re sure you need this. If a vital process in class 2 or 3 needs disk access, your system might freeze until the realtime process is done writing.
Example command:
sudo ionice -c 1 -n 7 bash
Useful ionice Examples
In the last example, instead of running a copy/move command, a shell has been launched (Bash). Now every subsequent command you type in that shell will inherit the I/O scheduling class and priority. You can also do this on a graphical interface.
ionice -c 3 pcmanfm
The last command will launch a file explorer on the LXDE desktop environment. Replace “pcmanfm” with the name of the file explorer of your particular desktop. Now all disk operations you start there will be performed with the idle I/O scheduling class.
In other situations, a copy/move operation may already be active. In this case you can use ionice in a different way.
ionice -c 3 -p 4910
This changes the priority class of the program running with process id 4910. You can find the PID (process ID) with your task manager or with a command like pgrep
.
Conclusion
ionice can be useful on desktops that you don’t want to lag while you copy/move large files. But keep in mind this can also be even more useful on servers. You certainly don’t want a website you may host there to lag on your visitors while you do a full backup.
Our latest tutorials delivered straight to your inbox