The concepts of IO scheduler and CPU scheduler confuse me. Below are my understanding:
- Linux uses CFS scheduler + nice values by default to schedule processes.
- Each process has an IO queue.
- There is an IO scheduler kernel thread.
- IO scheduler is in the block level, not in the file level.
- IO scheduler is a module of the file system.
Questions:
What is relationship between IO scheduler and CPU scheduler? Conceptually, it seems to me that CPU scheduler is superior over IO scheduler. CPU scheduling happens first. IO scheduler is a thread itself and subject to CPU scheduling.
A contrived scenario looks like this:
Step 1: CPU scheduler picks a process P1 to execute
Step 2: P1 puts IO requests in its own IO queue
Step 3+: CPU scheduler picks other threads to run. (Assuming no process has IO other than P1)
….(after a while)
Step n: CPU scheduler picks the IO scheduler thread to run.
Step n+1: IO scheduler thread 'notices' P1 has IO requests queued up and issues those requests to disk.
Does my understanding and the scenario make sense?
Best Answer
Let's start with the IO scheduler first. There's a IO scheduler per block device. Its job is to schedule (order) the requests that pile up in the device queue. There are three different algorithms currently shipped in the linux kernel:
deadline
,noop
andcfq
.cfq
is the default, and according to its doc:You can configure which scheduler governs which device via the
scheduler
file corresponding to your block device under/sys/
(You can issue the following command to find it:find /sys | grep queue/scheduler
).What that short description doesn't say is that
cfq
is the only scheduler that looks at theioprio
of a process.ioprio
is a setting that you can assign to the process, and the algorithm will take that into account when choosing a request before another.ioprio
can be set via theionice
utility.Then, there's the task scheduler. Its job is to allocate the CPUs amongst the processes that are ready to run. It takes into account things like the priority, the class and the niceness of a give process, as well as how long that process has run and other heuristics.
Now, to your questions:
Not much, besides the name. They schedule different shared resources. The first one orders the requests going to the disks, and the second one schedules the 'requests' (you can view a process as requesting CPU time to be able to run) to the CPU.
It doesn't happen like the the IO scheduler algorithm is run by whichever process is queuing a request. A good way to see this is to look at crashes that have
elv_add_request()
in their path. For example:Notice how the process enters the kernel calling open(), and this ends up involving the elevator (
elv
) algorithm.