Hard Disk status/Tuning on Linux


hdparm - linux disk tuning

Hold on. Stop. Right there. That's it. Before you go running off and playing you need to understand what you are playing with and what you are trying to achieve. Data security? Blistering Performance? Power Saving? Reduced Nose? hdparm is a powerfull toy and can shoot you in the foot. RTFM: man hdparm and man hdparm.conf. If the fine manual hasn't warned you about potential mass breakage, then I shall. Failure to test things properly can result in breakage. Lots of nasty breakage. It's probably a bad thing to do this to a mounted drive, or even worse, one your OS is running on. Learn about using this on an empty or backed up disk. I tend to do this at system install time, and when I have a new disk to play with. Today I'm fixing my fileserver. I have a fresh debian etch install. Then have a look at what is already set:
 root@marvin:~# hdparm /dev/hda
 /dev/hda:
 multcount = 0 (off)
 IO_support = 0 (default 16-bit)
 unmaskirq = 0 (off)
 using_dma = 0 (off)
 keepsettings = 0 (off)
 readonly = 0 (off)
 readahead = 256 (on)
 geometry = 59582/16/63, sectors = 60058656, start = 0
 
Everything is turned off? Ok so this is a contrived example. Let's see how slow it is:
 root@marvin:~# hdparm  -Tt /dev/hda
 /dev/hda:
 Timing cached reads:   680 MB in  2.00 seconds = 339.33 MB/sec
 Timing buffered disk reads:   10 MB in  3.05 seconds =   3.27 MB/sec
 
Ouch now that is slow. But repeat a few times to check. Now Let's see what we can do to this drive:
 root@marvin:~# hdparm -i /dev/hda
 /dev/hda:
 Model=Maxtor XXXXXXXX, FwRev=XXXXXXXX, SerialNo=XXXXXXXX
 Config={ Fixed }
 RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=57
 BuffType=DualPortCache, BuffSize=2048kB, MaxMultSect=16, MultSect=off
 CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=60058656
 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 udma3 *udma4 udma5 udma6
 AdvancedPM=yes: disabled (255) WriteCache=enabled
 Drive conforms to: ATA/ATAPI-7 T13 1532D revision 0: ATA/ATAPI-1 ATA/ATAPI-2 ATA/ATAPI-3 ATA/ATAPI-4 ATA/ATAPI-5 ATA/ATAPI-6 ATA/ATAPI-7
 * signifies the current active mode
 root@marvin:~# hdparm -I /dev/hda
 /dev/hda:
 ATA device, with non-removable media
 Standards:
 Used: ATA/ATAPI-7 T13 1532D revision 0
 Supported: 7 6 5 4
 Configuration:
 Logical max current
 cylinders 16383 16383
 heads 16 16
 sectors/track 63 63
 --
 CHS current addressable sectors: 16514064
 LBA user addressable sectors: 60058656
 device size with M = 1024*1024: 29325 MBytes
 device size with M = 1000*1000: 30750 MBytes (30 GB)
 Capabilities:
 LBA, IORDY(can be disabled)
 Standby timer values: spec'd by Standard, no device specific minimum
 R/W multiple sector transfer: Max = 16 Current = 0
 Advanced power management level: unknown setting (0x0000)
 Recommended acoustic management value: 192, current value: 0
 DMA: mdma0 mdma1 mdma2 udma0 udma1 udma2 udma3 *udma4 udma5 udma6
 Cycle time: min=120ns recommended=120ns
 PIO: pio0 pio1 pio2 pio3 pio4
 Cycle time: no flow control=120ns IORDY flow control=120ns
 Commands/features:
 Enabled Supported:
 SMART feature set
 Security Mode feature set
 * Power Management feature set
 * Write cache
 Look-ahead
 * Host Protected Area feature set
 * WRITE_VERIFY command
 * WRITE_BUFFER command
 * READ_BUFFER command
 * NOP cmd
 * DOWNLOAD_MICROCODE
 Advanced Power Management feature set
 SET_MAX security extension
 Automatic Acoustic Management feature set
 * Device Configuration Overlay feature set
 * Mandatory FLUSH_CACHE
 * FLUSH_CACHE_EXT
 * SMART error logging
 * SMART self-test
 Security:
 Master password revision code = 65534
 supported
 not enabled
 not locked
 not frozen
 not expired: security count
 not supported: enhanced erase
 HW reset results:
 CBLID- above Vih
 Device num = 0 determined by CSEL
 Checksum: correct
 
Ok, so that's a lot of info. But it tells us a lot. We can see the drive supports (U)DMA. DMA is a 'A Good Thing' but odd hardware can cause /issues/. Explaing waht it does may take time, read http://en.wikipedia.org/wiki/UDMA for some practical knowledge. Or have a brwse through Stalling's Computer Organisation and Archetecture, or start greppign your source code ;-p. I think Tanenbaum has a book that deals with it too. In short, DMA allows your processor to instruct your disk controller to read from the hard drive directly into memory, rather than going through the processor. This allows the processor to do soethign else while you're waiting for the disk, and provides some big speedups. Lets try turning that on:
 root@marvin:~# hdparm -d1 /dev/hda
 /dev/hda:
 setting using_dma to 1 (on)
 using_dma    =  1 (on)
 
 root@marvin:~# hdparm -Tt /dev/hda
 /dev/hda:
 Timing cached reads:   684 MB in  2.00 seconds = 341.31 MB/sec
 Timing buffered disk reads:   34 MB in  3.12 seconds =  10.91 MB/sec
 
Ok, so that gives us a big speedup. But still we want more. It's tempting to look at the transfer mode -X, however most hardware sets this right at bootup. Playing with it leads to certain disktruction, or at least headaches. Ony poke at it if you are sure your hardware is wrong. Then test thoroughly. So now we look at the multicount. This setting governts the number of sectors read at a time, reducing operating system overhead. Take a look at the info we dumped earlier, there should be an indication of the maximum value. You should be able to get it to work with powers of 2 upto that number.
 root@marvin:~# hdparm -m16 /dev/hda
 root@marvin:~# hdparm -Tt /dev/hda
 /dev/hda:
 Timing cached reads:   676 MB in  2.00 seconds = 337.86 MB/sec
 Timing buffered disk reads:   34 MB in  3.11 seconds =  11.10 MB/sec
 
It goes up some. So we keep it. Lookahed: This speculatively reads the next sectors just incase you want the sector after the one you were just reading. It's pretty useful and is often on by default.
 root@marvin:~# hdparm -A1 /dev/hda
 /dev/hda:
 setting drive read-lookahead to 1 (on)
 root@marvin:~# hdparm -Tt /dev/hda
 /dev/hda:
 Timing cached reads:   672 MB in  2.00 seconds = 335.23 MB/sec
 Timing buffered disk reads:  126 MB in  3.12 seconds =  40.41 MB/sec
 
Yep. That's a good one to have on. It's friend, readahead, (-a) is almost always set to 256 by default and is often a happy number. You can play with this some if you want but 256 seems to be a happy medium. Note that the laptop-mode tools play with this to set it to many Mebibytes so you can have entire MP3s or OGGs cached while your disk spins down. Great for powersaveing, but pretty slow if you're scaning a lot of small files.
Now for the more subtle tuning: Most modern drives want a 32bit transfermode, so try enableing that (-c1), some buggy hardware is fixed by -C3, but I am yet to encounter an example. Unmasking the IRQ (-u) is also usefull, as it allows your system to do other things with hardware whilst waiting for a disk to reply to a request.
 root@marvin:~# hdparm -c1 -u1 /dev/hda
 root@marvin:~# hdparm -Tt /dev/hda
 /dev/hda:
 Timing cached reads:   680 MB in  2.01 seconds = 338.83 MB/sec
 Timing buffered disk reads:  128 MB in  3.04 seconds =  42.13 MB/sec
 
And we get a little more. Now lets think about the awful whine and clatter that disk is making. Playing with the accusitc management setting seems to do little to the speed, maybe a 1% loss, and seems to make the thing quieter. Reading the info (from -I) may yeaild a recommended value.
 root@marvin:~# hdparm -M128 /dev/hda
 /dev/hda:
 setting acoustic management to 128
 acoustic     =  128 (128=quiet ... 254=fast)
 root@marvin:~# hdparm -Tt /dev/hda
 /dev/hda:
 Timing cached reads:   676 MB in  2.00 seconds = 337.83 MB/sec
 Timing buffered disk reads:  128 MB in  3.02 seconds =  42.42 MB/sec
 
The advaced power management feature (-B) again seems to make little difference to performance. If it saves power it may be a good thing. Also we have the spindown (-S) setting. This allows you to set the period of inactivity after which a drive spins down. This is good for data disks that may not be accessed in days, and good for saveing power on laptops. It is bad for traditional drives that are written to a lot, they are not designed to spin up and down all the time. Setting this to 15 second can cause a drive to be constantly spining up and down. Not good at all. I tend to use -S240 for suitabe drives, giving a 20 minute interval. Write Caching des exactly what is says on the tin. However it causes issues with journalling filesystems so it's best left turned off. Your kernel is probably smart enough to do a sensible job anyway. Let's not second guess it. Also it breaks the native command queuing of SATA drives. Well now we've decided on some settings and tested extensively, it's time to make them permanent. Different distros do things differently, in gentoo you specify the switches for each drive or set of drives, in debian, you can call the settings by name. This is time to RTFM again. Then carefully have your settings applied to your drives. Here's a the section for that drive in my hdparm.conf (debian style)
 /dev/hda {
 mult_sect_io = 16
 write_cache = off
 dma = on
 apm = 254
 acoustic_management = 128
 spindown_time = 0
 io32_support = 1
 lookahead = on
 }