Command-line gems: hdparm

Jul 21, 06:56 am

Both Microsoft and Apple put a lot of work into optimizing their operating systems. Sadly, not all Linux distros make the same effort, although the situation is slowly getting better.

If you find that your system is a little sluggish, hdparm is definitely worth investigating. This is the hard-disk tweaing tool of choice under Linux. It works from the command-line and offers full control over all hard disk settings.

Below is a brief excerpt from my Ubuntu book, which explains how hdparm can be used. In the steps below I use sudo to attain root powers. If you're using an alternative distro you might want to simply switch to root user (ie su -). Also, the steps at the end involve editing the /etc/hdparm.conf file which is only present on Debian-based systems. Users of other distros can create their own run-level script to activate the command at boot, although ensure that it's one of the last things to run (ie precede the run-level link with S99).

Using hdparm

Because experimenting with hdparm can cause crashes, and because its benchmarking feature needs almost exclusive access to the hard disk, hdparm is best run with as few as possible additional programs up and running. Therefore, killing the GUI is a good idea. To stop the GUI, open a GNOME Terminal window (Applications -> Accessories -> Terminal), and type the following:

sudo /etc/init.d/gdm stop

You'll then need to log in again, so do so.

Technically speaking, switching to run level 1 is an even better idea, because this will deactivate all unnecessary services. Run level 1 is akin to the Windows Safe Mode, except without the GUI. However, you want realistic benchmark results to test the changes you make via hdparm, and it's debatable whether the restricted confines of run level 1 will provide such results.

Benchmarking

Let's start by benchmarking your hard disk to see its performance based on the current settings. Type the following (assuming Ubuntu is installed on the first hard disk in your system; if it's on the second hard disk, change /dev/hda to /dev/hdb):

sudo hdparm -tT /dev/hda

This will benchmark your disk in two ways. The first tests the PC's memory throughput, measuring the data rate of the memory, CPU, and cache. The second actually tests the disk's data rate. The second test affects the outcome of the first, which is why the two are used together. Between them, these two methods of benchmarking present the standard way your disk is used on a day-to-day basis.

On my system, which has a fairly average hard disk, I got the following scores.

/dev/hda:
Timing cached reads: 4288 MB in 2.00 seconds = 2143.87 MB/sec
Timing buffered disk reads: 170 MB in 3.02 seconds = 56.29 MB/sec

Make a note of the figures you get so that you can compare them to the results of these tests after you change hard disk settings.

Changing Hard Disk Settings

You can use hdparm to view your current hard disk settings by entering the following at the command prompt:

sudo hdparm /dev/hda

On my test PC, these are the results I got:

/dev/hda:
multcount = 0 (off)
IO_support = 1 (32-bit)
unmaskirq = 1 (on)
using_dma = 1 (on)
keepsettings = 0 (off)
readonly = 0 (off)
readahead = 256 (on)
geometry = 65535/16/63, sectors = 160086528, start = 0

Let's take a look at what these settings mean.

The Multcount Setting

The first, multcount, refers to how many sectors can be read from the hard disk at any one time. The theory is that the highest possible value here is best. You can find out your drive's highest potential setting by issuing the following command:

sudo hdparm -i /dev/hda

Look for MaxMultSect in the results. On my test PC, this read MaxMultSect=16. Ironically, although higher values are thought best, sometimes a lower value can speed up hard disk access. You can experiment with the multcount setting on your hard disk by using the -m hdparm command option:

sudo hdparm -m16 /dev/hda

You can then follow this by another benchmark to see if there is an improvement:

sudo hdparm -tT /dev/hda

If there isn't any improvement, you could try a lower value by simply swapping the -m16 for -m8. Any value that's a power of two is valid, so you might want to try -m4 too.

The IO_Support Setting

The IO_support line refers to the input/output (I/O) mode used by the hard disk controller. There are three possible settings: 0, to disable 32-bit support; 1 to enable 32-bit support; and 3 to enable 32-bit support with a special sync signal.

You can change the IO_support setting with the -c hdparm command option, and the 32-bit support with sync option (3) is generally considered the best choice:

sudo hdparm -c3 /dev/hda

The Unmaskirq Setting

The third setting, umaskirq, allows Ubuntu to attend to other tasks while waiting for your hard disk to return data. This won't affect hard disk performance very much, and generally it's a good idea for the health of your system to activate it if isn't already switched on. This command activates umaskirq:

sudo hdparm -u1 /dev/hda

The Using_Dma Setting

The fourth setting refers to whether Direct Memory Access (DMA) is in use. Hard disks are sold on the basis of their DMA modes, such as UltraDMA Burst 2 and the like. DMA is considered an indicator as to the speed of a hard disk, but the truth is that, like any specification, it is only a guide.

DMA is activated by default under Ubuntu, but you can alter the DMA mode using the -X command option. However, on most modern PCs, this isn't necessary because the computer's BIOS defaults to the fastest DMA mode.

Other Settings

The last three settings, above the summary of the geometry and sector information of the disk, are those you shouldn't change. The readahead setting controls how many hard disk blocks are loaded in advance. It doesn't affect the performance of modern IDE-based hard disks, because the drive electronics contain buffers that perform this task themselves.

The keepsettings setting refers to the ability of the drive to remember hdparm settings over a reboot, which isn't necessary because you intend to use the hdparm.conf file to change the disk settings at each boot.

The readonly setting sets whether or not the hard disk is read-only (so that no data can be written to it). Changing this setting is not advisable!

Making Disk Optimizations Permanent

Under Ubuntu and Debian the /etc/hdparm.conf file is read at each bootup, and any settings it contains are applied to the hard disk. You can edit this file in the Gedit text editor by typing the following:

sudo gedit /etc/hdparm.conf

The simplest way of using this file is to edit the hdparm command string at the end of the file. To do so, start by finding the line that reads as follows:

#command_line {

Delete the hash mark from the beginning of the line (hash marks cause the line to be ignored, which is obviously not what you want in this case). Then delete the next line (the one that starts with a hash mark, followed by hdparm -q -m16 . . .), and type your own version of the hdparm command, complete with the command options you discovered previously. For example, if your benchmarking has shown that the multcount and IO_support command options bring dividends, you might type something like the following:

hdparm -m16 -c3 /dev/hda

There's no need to type a hash mark at the beginning, as with the line you deleted. Then delete the hash mark from in front of the line beneath this, so that it's simply a bracket (}) on its own. Here's an example of how the three lines should look:

command_line {
hdparm -m16 -c3 /dev/hda
}

When you've finished, save the file and reboot to test your settings. Be sure to thoroughly test your system to make sure the new settings are stable.




Add your comments

Please keep your comments relevant to this blog entry: inappropriate or purely promotional comments may be removed. To add hyperlink, please follow this example: "your link text":http://your.link.url