Team LiB
Previous Section Next Section

Hack 91. Tweak Your Kernel Without Recompiling

You don't need to compile a hot rod version of a Linux kernel or even recompile your existing kernel to improve Linux performance on the desktop. If you're running one of the later 2.6 kernels, this pack of hacks includes a few tricks you can use to modify the performance of the kernel you already have installed and running.

One of the most hotly debated performance topics is how Linux should determine what memory to swap to disk, how much it should swap, and when to do it. The answer is simple. Use one or more of the following tweaks to control this process yourself.

10.5.1. Setting Swappiness via /proc

A kernel parameter is available that is represented as a pseudofile called /proc/sys/vm/swappiness. The default value for the parameter is 70. You can log in and view the default value with this command:

# cat /proc/sys/vm/swappiness
70

The number is a rough gauge for how likely it is that Linux will swap to disk whatever it considers to be swappable. If you set it to 100, it will swap most aggressively. If you set it to 20, it will tend to swap a lot less. Here is how you can set it to either value (logged in as root):

# echo "100" > /proc/sys/vm/swappiness
# cat /proc/sys/vm/swappiness
100
# echo "20" > /proc/sys/vm/swappiness
# cat /proc/sys/vm/swappiness
20

The argument for either side goes like this. Set the swappiness value to a high number, even to 100, if you run several bloated applications and do not often switch between them. The end result will be that when you do switch to an application you left unused for a while, it might take several seconds to respond, because it must be swapped back into memory from disk. However, any application you use at any given time should run faster, because it is not competing with seldom-used applications for memory, as those applications are likely to be swapped out to disk.

On the other hand, if you set the swappiness value to a low number, your applications are likely to respond instantly when you switch between them, even if you leave one or more of them unused for long periods of time. The possible downside to this approach is that if one or more of your applications needs a lot of memory to store data (such as a database application), it will run slower than if you set the swappiness value higher.

So, here's what you should do. Set the value to a high number, use your computer for desktop operation as you normally would, and see how things behave for a few hours. Then set it to a low number, continue to use your computer for desktop operation as you normally would, and see for yourself what the difference feels like. Then pick the performance characteristics you like best. Perhaps you'll even be happiest with a default value of 70.

10.5.2. Tuning Network Performance via /proc

Here are a couple more parameters you might want to tweak, especially if you are using a home computer hooked directly to a DSL or cable router:

# echo "0" > /proc/sys/net/ipv4/tcp_sack
# echo "0" > /proc/sys/net/ipv4/tcp_timestamps

These settings turn off some unnecessary network activity. You might not perceive any change in performance, but it's worth a try.

10.5.3. Tuning Disk Access via Your Bootloader

Each program you run under Linux will want to access the disk at some point or another. The Linux kernel determines how to prioritize how each program gets access to the disk, using three methods: Anticipatory Scheduling, Complete Fairness Queuing, and Deadline Queuing. (Actually, the kernel uses a fourth method, called noop, but noop is not likely to be applicable as a default method for a desktop user.)

Each method has its advantages and disadvantages. Only two of them are likely to be appropriate for desktop performance: Anticipatory Scheduling and Complete Fairness Queuing. Most people seem to favor Complete Fairness Queuing for best desktop performance, but some argue that Anticipatory Scheduling is actually superior for desktop performance. Here's how the three methods work.


Anticipatory Scheduling

The kernel tries to anticipate how the disk will be accessed. If a program is reading data, the anticipatory scheduler assumes it's likely that it will want to continue reading data. So, while it pauses according to a schedule, it anticipates picking up where it left off. If it guesses correctly, the disk heads don't have to jump around much and you get better performance.


Complete Fairness Queuing

The kernel queues the disk to be used equally by every running program. No single program can hog disk access. If several programs are using the disk at the same time, each one will be responsive. On the other hand, this can also cause the disk head to jump around a lot more than it has to, thus slowing the entire system's overall responsiveness.


Deadline Queuing

This type of queuing allows an application to dominate disk access. Other applications that want to use the disk are put on the queue and must wait. If the application hogging the disk exceeds a certain deadline, the kernel passes disk access to the next requesting application on the queue. This is a good queuing system for a database server, but it is least likely to be best for a desktop system.

The choice really comes down to Anticipatory Scheduling and Complete Fairness Queuing. There are advocates for both when it comes to desktop performance, so you should try both and decide for yourself how they suit your desktop use.

You can specify your choice of scheduler by passing parameters to the kernel in your bootloader.

LILO lets you append instructions to the boot process by adding a line such as the following for each boot entry in the /etc/lilo.conf file:

append="<added instructions>"

If you want to use the Anticipatory Scheduler, use this:

append="elevator=as <possibly more added instructions>"

If you want to use Complete Fairness Queuing, use this instead:

append="elevator=cfq <possibly more added instructions>"

A complete example might look like this:

image=/boot/vmlinuz-2.6.9
    label=Linux
    root=/dev/hda1
    append="elevator=as video=vesafb:ywrap,mtrr,1024x768-16@60"

Run the lilo command after you make your changes to lilo.conf, and then reboot for the changes to take effect.

With the GRUB bootloader, you simply append the instructions to the kernel line yourself. For example, if you want to use Anticipatory Scheduling, your boot entry might look like this:

title My Default Linux
root (hd1,0)
kernel /boot/vmlinuz-2.6.9 ro root=/dev/hda1 elevator=as

If you want to try Complete Fairness Queuing, make your boot entry look more like this:

title My Default Linux
root (hd1,0)
kernel /boot/vmlinuz-2.6.9 ro root=/dev/hda1 elevator=cfq

You do not need to set up GRUB again. Simply reboot for the change to take effect.

If you are using the Con Kolivas Overloaded kernel [Hack #90], it defaults to Complete Fairness Queuing. If you are using a plain-vanilla kernel, it defaults to Anticipatory Scheduling. These default settings have changed over time, and they might change again in the future, so it is best to specify the queuing method you want.


    Team LiB
    Previous Section Next Section