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.

Friday, March 01, 2013

Upgrading Linux Kernel 3.7.6 (Stable) release in Ubuntu 12.10


    Linux kernel 3.7.6 has recently released on February 04, 2013 with lots of bugfixes and improvements.

    The changelog contains all the improvements and bugfixes made in the kernel.

    Below mentioned are some important fixes and improvements.

    drm/i915: fix FORCEWAKE posting reads
    ALSA: hda - Fix non-snoop page handling
    ALSA: hda - fix inverted internal mic on Acer AOA150/ZG5
    ALSA: hda - Add a fixup for Packard-Bell desktop with ALC880
    ALSA: usb-audio: fix invalid length check for RME and other UAC 2 devices
    ath9k_hw: fix chain swap setting when setting rx chainmask to 5
    DM-RAID: Fix RAID10's check for sufficient redundancy
    xfs: fix periodic log flushing
    iwlegacy: fix IBSS cleanup
    xfs: fix _xfs_buf_find oops on blocks beyond the filesystem end
    x86, efi: Fix 32-bit EFI handover protocol entry point
    x86, efi: Fix display detection in EFI boot stub
    smp: Fix SMP function call empty cpu mask race
    NFSv4: Fix NFSv4 trunking discovery
    NFSv4: Fix NFSv4 reference counting for trunked sessions
    NFS: Fix error reporting in nfs_xdev_mount
    Bluetooth: Fix incorrect strncpy() in hidp_setup_hid()
    kvm: Fix irqfd resampler list walk
    drm/radeon: fix cursor corruption on DCE6 and newer
    drm/radeon: fix a rare case of double kfree
    drm/radeon: fix error path in kpage allocation


    We shall upgrade the default kernel version 3.5.0-17.28 shipped with Ubuntu 12.10 to 3.7.6-030706.

    Download the script to perform an automated kernel upgrade process.

    Now open the terminal and run the below command from the folder where script is downloaded to make it executable.
    $ sudo chmod +x ker-upg-3.7.6.sh

    $ sudo sh ker-upg-3.7.6.sh

    After the completion, reboot the system for the changes to take effect and after reboot run the below command to confirm the version.

    $ uname -r

    In case if the kernel 3.7.6 is not compatible with your system hardware, then run the below command to remove the kernel 3.7.6 and switch back to previous kernel.

    $ sudo apt-get purge linux-image-3.7.6-030706