Boost your hard drive performance in Linux using hdparm

December 04, 2004
Hdparm is a command-line utility that provides powerful control over your hard drive parameters (HD PARaMeters). It can also tell you a lot about your disk drive. Everything you do with hdparm, until you make a script for it, will be done at the command line. It can be adjusted manually and then put into a startup script to make your chosen settings effective every time the system starts up.

Suppose I want to improve the performance of my first hard disk drive - /dev/hda . The first thing I do is find out the characteristics of my hard disk. This can be done as follows:
# hdparm -i /dev/hda
And the output on my machine is this :
Model=QUANTUM FIREBALLlct10 10, FwRev=A03.0900, SerialNo=872007625887
Config={ HardSect NotMFM HdSw>15uSec Fixed DTR>10Mbs }
RawCHS=16383/16/63, TrkSize=32256, SectSize=21298, ECCbytes=4
BuffType=DualPortCache, BuffSize=418kB, MaxMultSect=16, MultSect=16
CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=20044080
IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120}
PIO modes: pio0 pio1 pio2 pio3 pio4
DMA modes: mdma0 mdma1 mdma2
UDMA modes: udma0 udma1 *udma2
AdvancedPM=no WriteCache=enabled
Drive conforms to: ATA/ATAPI-4 T13 1153D revision 15: 1 2 3 4

* signifies the current active mode
In the output shown above, a few important values to be noted with interest are as follows:
  • MaxMultSect - The maximum number of sectors your hard disk can read at a time.
  • MultSect - The current number of sectors being read at a time.
  • PIO modes, DMA and UDMA modes - The modes supported by your hard drive. The one marked with an asterisk (*) is the one currently set.
  • AdvancedPM - Indicates whether or not your hard drive supports Advanced Power Management.
Another command :
# hdparm /dev/hda
will reveal the following information ...
multcount = 16 (on)
IO_support = 0 (default 16-bit)
unmaskirq = 0 (off)
using_dma = 1 (on)
keepsettings = 0 (off)
readonly = 0 (off)
readahead = 256 (on)
geometry = 19885/16/63, sectors = 20044080, start = 0
The items of interest in the above output are as follows:
  • multcount - The number of sectors being read at a time.
  • I/O support - The operating mode of your hard disk (16/32/32sync).
  • using_dma - Whether or not the drive is using the DMA feature. This may be on by default if your version of Linux properly detects and supports your chipset and drive's DMA capabilities.
  • keepsettings - Whether the settings are kept after the drive resets (usually caused by errors).
  • readonly - Whether the drive is read-only. Normally set to 1 only for CD-ROMs.
  • readahead - How many sectors ahead will be read when you access the hard drive.
The hdparm program provides two performance testing features that are crucial in letting you know whether or not you are improving performance or not as you tweak along.
# hdparm -Tt /dev/hda1
will show results such as the following before enhancing the performance.
Timing buffer-cache reads: 340 MB in 2.01 seconds = 169.43 MB/sec
Timing buffered disk reads: 30 MB in 3.08 seconds = 9.73 MB/sec
and the results like these after enhancing the performance.
Timing buffer-cache reads: 340 MB in 0.91 seconds = 200.00 MB/sec
Timing buffered disk reads: 30 MB in 1.05 seconds = 19.73 MB/sec
The goal is to see the time in seconds decrease and the MB/sec to increase in the above output.You can do that by using a variety of parameters, invoked one at a time, then rerunning the performance tests to see if things are improving or not.

For example: Begin by setting the operating mode of the interface between the system and the disk drive using one of the following parameters:
  • -c0 - Sets operating mode to 16-bits
  • -c1 - Sets operating mode to 32-bits
  • -c3 - Sets operating mode to 32-bits synchronized
Mode -c1 is usually used for best performance. Mode -c3 is required only for certain chipsets.
# hdparm -c1 /dev/hda
Next set the data transfer parameters, which you can determine from the -i command shown earlier. In the above case, the value of MaxMultSect is 16 which is the maximum supported.
# hdparm -m16 /dev/hda
Next try activating DMA mode for your system interface:
# hdparm -d1
Then set the drive mode (a value of X32 is most common; UDMA-5 is X69):
# hdparm -X32 /dev/hda
Finally, try setting the read-ahead value, which is typically set to the same value as multcount from earlier (See the hdparm /dev/hda output above), or 16:
# hdparm -a16 /dev/hda
If any or all of these settings make incremental improvements in performance, remember them and create a script that sets them all sequentially or includes them all in one line.

From all these settings, you might be using the following optimal single command :
# hdparm -X34 -m16 -c1 -a16 -d1 /dev/hda
Now save this command into a file and make the file into a script and place in the directory for the runlevel at which you normally use linux. I use runlevel 2 by default on my home machine. So I create a file named /etc/init.d/hdparm.local with the above hdparm command. Then configure it to start at runlevel 2 as follows:
# ln -s /etc/init.d/hdparm.local /etc/rc2.d/S20hdparm.local
Update (Oct 26 2007): If you are using Ubuntu or any Debian based Linux distribution, you can set the hdparm command and its options in the /etc/hdparm.conf configuration file instead of creating the above mentioned script and saving it in init.d directory. Also note that in Ubuntu Gutsy Gibbon, even the IDE drives are provided device nodes with name /dev/sda instead of /dev/hda. So make the necessary changes while executing the commands.

The next step is to keep an eye on dmesg and/or /var/log/syslog. In some cases, an error will cause the settings to be reset. So that's where the -k (keep) flag comes in. If you are 100% positive that these settings won't corrupt your data, you can add -k to the script.

WARNING : Setting hdparm parameters too aggressively - that is, in excess of the disk controller or drive capabilities - can lead to data loss. It is best to test hdparm settings on a fresh installation of the operating system before committing any applications or programs to the drive and prepare to back down on the settings and reinstall the OS if the drive is unstable or the hdparm tests show erratic results or fail.