Performance Tuning EXT4 in Ubuntu for an HDD

iozone_baselineDisk I/O performance is one of the most common performance bottlenecks, particularly on midrange and/or slightly older hardware running traditional platter-based hard disk drives. Ubuntu 12.04 and up is generally very good for getting maximum performance out of this type of hardware out of the box but there are a couple of tricks and tweaks which can yield substantial performance gains if implemented.

EXT4 Tuning

The first step is to make a couple of adjustments to any EXT4 partitions. To begin with, run the following command on each partition (XY replaced by partition identifier such as /dev/sda1):

sudo tune2fs -o journal_data_writeback /dev/sdXY

Then run the following to edit your fstab file:

sudo nano /etc/fstab

Identify any mount points with EXT4 partitions which you have modified with the tune2fs command like so:

UUID=95441b58-45a7-4e84-85f7-7859d51d22fb    /   ext4     errors=remount-ro    0    1

…and add the following into the Options column:

noatime,data=writeback,

..so that each of the lines in /etc/fstab which mounts an EXT4 like so:

UUID=95441b58-45a7-4e84-85f7-7859d51d22fb    /   ext4     noatime,data=writeback,errors=remount-ro    0    1

Hit CTRL+O to save, CTRL+X to exit and reboot

E4rat

Ureadahead is the default readahead application for Ubuntu and it works very well without requiring any specific configuration or user intervention. E4rat, on the other hand, requires a bit of work to get it functioning but provides a number of advantages including the following:

  • default run time for the analysis phase is 120 seconds for E4rat while ureadahead only tracks for 60 seconds by default. This means that E4rat accelerates the login process as well as the boot process.
  • E4rat reorganized the hard drive based on it’s analysis which cuts down on access time latency

Keep in mind that E4rat is built with HDDs in mind and will not help an SSD in any way. If you have an SSD, stick to ureadahead.

To begin with, remove ureadahead:

sudo apt-get remove ureadahead

Next, download the appropriate DEB from Sourceforge at http://sourceforge.net/projects/e4rat/files/ and install using the Software Center (double-click) or from the terminal:

sudo dpkg -i e4rat_0.2.3_amd64.deb

Now we need to run the e4rat collector by rebooting the computer, doing a one-time edit of the grub menu and adding the collector. To do this, reboot and hold down on the shift key starting at the BIOS screen to get a grub menu. Hit the e key on the first line, scroll down to the line that starts with linux /boot/vmlinuz… (usually the second from the bottom), cursor to the end of the line and add the text init=/sbin/e4rat-collect. Your line should look something like this when you are done:

linux /boot/vmlinuz-3.8.0-30-generic root=UUID-224049dd-f84e-41cd-623e56d497eo ro quiet splash $vt_handoff init=/sbin/e4rat-collect

On some systems I have found that the init=/sbin/e4rat-collect (or any other init line for that matter) needs to come before the $vt_handoff part. Once your line is set, hit the F10 key, boot up and log in. Once you log in, make sure to execute any programs that you might want to run immediately after startup such as an email client, web browser or chat client as the collector will record all activity in the first 120 seconds of startup. e4rat-collect will make a log of all files that are loaded in a log located at /var/lib/e4rat/startup.log. To confirm that the collector worked properly, run the following command:

tail /var/lib/e4rat/startup.log

The output should show the last ten files that the collector observed the system accessing before the 120 seconds were up. Now reboot the system into single user mode by rebooting, holding the shift key at bootup to get the grub menu and pressing e on the first option. Key down to the line that starts with linux /boot/vmlinuz… like before but this time add the word single to the end (or before the $vt_handoff if necessary) and hitting F10. If your system ends up at a blank screen with a blinking cursor in the top left, hit CTRL+ALT+F7 to get a terminal. Once at a root single user command prompt, enter the following command to reallocate the necessary files:

/sbin/e4rat-realloc /var/lib/e4rat/startup.log

Once the reallocation process is complete, reboot the system and log back in. The final step is to set the pre-allocation binary to run on startup every time. To do this, pull up a terminal enter the following:

sudo nano /etc/default/grub

Find the GRUB_CMDLINE_LINUX_DEFAULT option and add init=/sbin/e4rat-prealloc inside the quotes after “quiet spash” so the entire line looks something like this:

GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash init=/sbin/e4rat-preload”

Hit CTRL+SHIFT+O to write out and CTRL+SHIFT+X to exit, then type the following command to rebuild Grub:

sudo update-grub

Reboot your system and hang on to your hat!

Results

File System Tuning

The following is a comparison of results before and after tuning taken using iozone. This test was limited to a single run on a 100MB file with a 4k record size and not done under the most scientifically stringent conditions so they should be considered a rough estimation.