In my C program i am using very large file(approx 400MB) to read parts of it frequently. But due to large file the performance of the program goes down very badly. It shows very high I/O usage and I/O wait time.
My question is, What are the ways to optimize or tune I/O on linux or how i can get better performance when i have to read a large flat file?
I am using
Red Hat Enterprise Linux ES release 4 (Nahant Update 4)
2.6.9-42.EL, x86_64 GNU/Linux
For IDE disks, dma settings are quite important to performance.
$ hdparm -d /dev/hda
/dev/hda:
using_dma = 1 (on)
$ hdparm -I /dev/hda
/dev/hda:
ATA device, with non-removable media
Model Number: Maxtor 6L300R0
Serial Number: L60Z0NBG
Firmware Revision: BAJ41G20
Standards:
Used: ATA/ATAPI-7 T13 1532D revision 0
Supported: 7 6 5 4
Configuration:
Logical max current
cylinders 16383 65535
heads 16 1
sectors/track 63 63
--
CHS current addressable sectors: 4128705
LBA user addressable sectors: 268435455
LBA48 user addressable sectors: 586114704
device size with M = 1024*1024: 286188 MBytes
device size with M = 1000*1000: 300090 MBytes (300 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 = 16
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
* 48-bit Address feature set
* Device Configuration Overlay feature set
* Mandatory FLUSH_CACHE
* FLUSH_CACHE_EXT
* SMART error logging
* SMART self-test
Media Card Pass-Through
* General Purpose Logging feature set
* WRITE_{DMA|MULTIPLE}_FUA_EXT
* URG for READ_STREAM[_DMA]_EXT
* URG for WRITE_STREAM[_DMA]_EXT
* SMART Command Transport (SCT) feature set
* SCT Data Tables (AC5)
Security:
Master password revision code = 65534
supported
not enabled
not locked
frozen
not expired: security count
not supported: enhanced erase
HW reset results:
CBLID- above Vih
Device num = 0 determined by the jumper
Checksum: correct
Those two commands show whether DMA is enabled on an IDE drive, and if it is, what kind. hdparm -d1 /dev/hda will attempt to enable DMA transfer on an IDE drive if it isn't already.
hdparm can also do benchmarks:
$ hdparm -tT /dev/hda
/dev/hda:
Timing cached reads: 292 MB in 2.00 seconds = 145.96 MB/sec
Timing buffered disk reads: 140 MB in 3.04 seconds = 46.08 MB/sec
$
As already explained hdparm is for IDE disks. You can't enable IDE DMA on a disk or driver that's not IDE. It does seem to be able to do benchmarks though, which shows your disk running at rather good speeds indeed.
What really ruins disk performance is random seeking. Every time the head moves the disk has to wait and do nothing until its ready. Could your program be made to process data sequentially instead of seeking all over the place in the file?
Another thing you could do is add more memory. More of the file will be able to stay in cache, which will increase performance a lot.