Setting I/O priorities on Linux
Monday, July 30th, 2012
All us system admins know about nice, which lets you set the CPU priorities of a process. Every now and then I need to run an I/O-heavy process. This inevitably makes the system intermittently unresponsive or slow, which can be a real annoyance.
If your system is slow to respond, you can check to see if I/O is the problem (which it usually will be) using a program called iotop, which is similar to the normal top program except it doesn't show CPU/Memory but disk reads/writes. You may need to install it first:
# aptitude install iotop
The output looks like this:
Total DISK READ: 0.00 B/s | Total DISK WRITE: 0.00 B/s TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND 12404 be/4 fboender 124.52 K/s 124.52 K/s 0.00 % 99.99 % cp winxp.dev.local.vdi /home/fboender 1 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % init 2 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kthreadd] 3 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [ksoftirqd/0]
As you can see, the copy process with PID 12404 is taking up 99.99% of my I/O, leaving little for the rest of the system.
In recent Linux kernels (2.6.13 with the CFQ io scheduler), there's an option to renice the I/O of a process. The ionice tool allows you to renice the processes from userland. It comes pre-installed on Debian/Ubuntu machines in the util-linux package. To use it, you must specify a priority scheduling class using the -c option.
- -c0 is an old deprecated value of "None", which is now the same as Best-Effort (-c2)
- -c1 is Real Time priority, which will give the process the highest I/O priority
- -c2X is Best-Effort priority puts the process in a round-robin queue where it will get a slice of I/O every so often. How much it gets can be specified using the -n option which takes a value from 0 to 7
- -c3 is Idle, which means the process will only get I/O when no other process requires it.
For example, I want a certain process (PID 12404) to only use I/O when no other process requires it, because the task is I/O-heavy, but is not high priority:
# ionice -c3 -p 12404
The effects are noticeable immediately. My system responds faster, there is less jitter on the desktop and the commandline.