Tuesday, March 12, 2013

Ubuntu Performance Tuning


There are several articles around the web to improve the performance of Ubuntu Linux. I have jotted down some common tweaks that I too have tried on my system and due to which I have found quite a significant change in the performance of my system. It has been possible only because Linux kernel and Ubuntu are flexible enough to let you make the modifications as you want on the fly. I would love to share some performance boosting techniques with you.

Dropping caches
The kernel 2.6.16 and later provide a mechanism to drop the cache and free up a lot of memory. The /proc/sys/vm directory contains files that help in tuning the virtual memory of the system. The drop_caches file causes the kernel to drop clean caches by freeing up pagecaches, dentries and inodes causing the memory to become free.
Dropping cache is a good practice untill it is really needed. Actually dropping caches has more negative effects and less positive ones. By dropping your cache you are actually degrading the performance of your system. When the cache is gone the CPU load and RAM utilization increases. There will be a glitch in system performance.
People go for dropping caches when they need to do some benchmarking on a file system or disk or when they hate it when their RAM goes unused. Otherwise, caches are meant to be cached.

To use drop_caches, echo the number to the file.
To free pagecache:
$ sync;sudo echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:
$ sync;sudo echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes
$ sync;sudo echo 3 > /proc/sys/vm/drop_caches

Note: Sync command is necessary to be executed before running the command to drop cache because it flushes any cached data to the disk so that the data that is required isn't lost.

Decrease swappiness
As we all know that disks are much slower than RAM, hence swap disk is slower than physical memory. This normally leads to slower response for both applications and system. The swappiness parameter controls the nature of the kernel to move out processes from physical memory to allocated swap memory on disk. The swappiness value can be between 1 to 100, where 0 and 100 being the least and maximum value of moved out processes respectively. One can find this parameter in /proc/sys/vm/swappiness file
default_swappiness.jpg
The default swappiness parameter is set to 60 by the kernel. You can reduce this value to 10. To set the parameter permanently, edit the file /etc/sysctl.conf and add the below mentioned line at the end of the file.
vm.swappiness=10
Now just save the file, reboot and feel the change.

Zram
Zram is an experimental module of Linux kernel. Zram was previously called “compcache”. Enabling Zram kernel module helps in increasing the performance by using physical memory for paging instead of using swap space on hard-disk. Zram creates a virtual compressed swap file on the physical memory itself so that the RAM can hold more programs and their data, allowing very fast I/O.
To install Zram in Ubuntu 12.04 type the below command in a terminal.
$ sudo apt-get install zram-config
Though zram helps in increasing performance but still zram on machines with lower configuration and memory will face little cpu utilization due to continuous compression and decompression that will take place all the time.
To check if zram is installed and working, check the file /proc/swaps. If zram partition is listed and active, we are done.
One can check the zram size from two files /proc/swaps and /sys/block/zram0/disksize.

Preload
Preload daemon runs in the background and analyzes the user activity. It increases the startup time of the applications by fetching and loading the binaries and dependencies of the application that the user might run next. The following command will install preload.
$ sudo apt-get install preload

There have been no side-effects of this program. There seems to be bit improvement in the performance. Its “install and forget” kind of application.


Enable DMA (Direct Memory Access) for non-SCSI disks' I/O performance
Use the below command to test the disk speed
$ sudo hdparm -t /dev/xxx

Enable DMA (Direct Memory Access)
$ sudo hdparm -d1 /dev/xxx

Now check the speed again using the previously executed command. If your system has not been DMA enabled, then you might get an improved transfer rate. Though there are also few considerations to be kept in mind before executing this command. One can check with “man hdparm” command.
In order to make this change persist across reboots one need to change /etc/init.d/rc.local file or any startup script that is being called.

Disk I/O Tweaks
The read performance of the disk can be improved by tweaking the parameter “read_ahead”. It defines the maximum number of kilobytes to read-ahead for filesystems on a particular device. The read ahead function is based upon two values: current window and ahead window. As the kernel when reading a file, always tries to take advantage of sequential disk access, hence it quickly turns the ahead window as the current window after the application has finished reading from the current window. One can view the current window size by issuing the below command.
$ cat /sys/block/sda/queue/read_ahead_kb

By default my Ubuntu 12.04 system has 256 as read_ahead value that means only 128KB of data is read in advance from the cache memory before the application needs it. You can use the below mentioned commands to get the “read ahead” default value in your system.
$ sudo blockdev --getra /dev/sda

To list all block devices
$ sudo blockdev –report

To set the value of read_ahead to 1024KB execute any one of the below commands
$ sudo echo 512 > /etc/block/sda/queue/read_ahead_kb
or
$ sudo blockdev --setra 1024 /dev/sda

The next parameter that we would look into is “nr_requests”.
Generally, every computer's task is to either read a block of data from the disk and move it to RAM or write a new block of data from the RAM to the disk. These tasks are called as the read/write requests or the I/O requests.
Linux has four different I/O schedulers namely noop, deadline, anticipatory and cfq, whereas Ubuntu 12.04 includes all except anticipatory scheduler. These schedulers sort the incoming I/O requests in a queue for optimizing the load. So, the /sys/block/sda/queue/nr_requests parameter defines the queue size or queue length. The default value assigned to this parameter is 128 which can be changed to 1024 using the below mentioned command.
$ sudo echo 256 > /sys/block/sda/queue/nr_requests

And to make both the changes permanent, edit the /etc/rc.local file and add the below mentioned lines before “exit 0”.
echo 1024 > /sys/block/sda/queue/read_ahead_kb
echo 256 > /sys/block/sda/queue/nr_requests

Well these are few to-dos that came to my mind. But again, if you have any suggestions, please feel free to drop some comments. Also there's a possibility that there might not be any performance improvement in your system, but they surely did work for me.

2 comments:

  1. Hello Bro.....
    I need help.....

    ReplyDelete
  2. Nice one buddy,

    Its really helpful.

    Thanks for sharing.

    ReplyDelete