Improving interactivity on Linux systems
[Posted March 11, 2003 by corbet]
The 2.5 kernel features a massively reworked scheduler which, among other
things, improves the interactive feel of a desktop system. It goes to
great lengths to try to separate interactive tasks from "background"
processes, and to give a priority boost to the former. One way that this
distinction is made is to look at how much time each process spends
sleeping. Processes that sleep a lot are generally waiting for humans to
do something, so the kernel tries to ensure that, when they wake up, they
get quick access to the processor.
This heuristic works well much of the time, but it also fails badly in some
situations. Consider, for example, the case of a user dragging a window
across the screen. That sort of operation can require a fair amount of
computation on the part of the X server. If the system is busy anyway
(with a kernel compilation, for example), the X server can end up using all
of the processor time that is available to it. When the server stops
sleeping, the kernel concludes that it is a compute-bound background task
and drops its priority. At that point, the pointer stops keeping up with
the mouse, and the desktop experience becomes generally unpleasant.
A classic solution (which predates Linux) for this problem is to raise the
priority of the X server. A higher-priority server can make things work
better for some users, but it ignores the fact that similar situations can
arise with other interactive processes that require
a fair amount of processor time. Streaming media applications tend to work
this way, for example. Raising the priority of the X server can make
things worse for this sort of application. Also, as Linus points out, tweaking priorities in this way is
an indication that the system has failed somehow:
Something is wrong, and we couldn't fix it, so here's the band-aid
to avoid that problem for hat particular case. It's acceptable as a
band-aid, but if you don't realize that it's indicative of a
problem, then you're just kidding yourself.
A few patches have gone into the 2.5.65 kernel which, by most reports, make
things a lot better. One of them, which originally came from Linus, is
based on the recognition that, if an interactive process is waiting for
another process to do something, that other process should be considered
interactive as well. The X server may be using a fair amount of CPU time,
but, since interactive processes (i.e. the clients that the user works
with) are waiting for it, the X server should still be seen as an
interactive process.
The ideal time to make this adjustment might be when an interactive process
goes to sleep waiting for an event. Unfortunately, that is hard to do; the
kernel has no way to know, in the general case, who will be waking up
processes that sleep on a particular queue. On the other hand, when the
wakeup actually occurs, the relationship is immediately obvious. So the
new scheduler will, at wakeup time, look at the interactivity bonus for the
process being awakened. If that process has maxed out its bonus (as
processes that sleep a lot will), the "excess" interactivity bonus is
given, instead, to the process which is performing the wakeup. Thus, a
sleeping mail client gives some of its bonus to the X server, which wakes
it up. This patch is said to improve the interactivity of X
significantly.
Ingo Molnar has taken Linus's patch and merged it into a larger set of
scheduler changes (which, in turn, has gone into 2.5.65). Some of the
additional changes that have been made include:
- Various scheduler parameter tweaks. The maximum timeslice given to
any process has been reduced, for example (to 200ms).
- One process can preempt another with the same priority, if the former
has a longer remaining timeslice.
- The first wakeup of a newly-forked child has been made smarter,
resulting in less work being redone.
The end result of these changes is a kernel which provides a much more
satisfying interactive experience. Note, however, that some causes of X
server stalls - in particular, those related to disk I/O scheduling - still
have not been resolved. Work is ongoing, however.
(See also: Jim Houston's self-tuning scheduler
patch, which takes a different approach to scheduler improvement).
(
Log in to post comments)