June 13, 2009

Minimizing Disk I/O Using Temporary Filesystem

This writing tells You how to exploit memory resource to the limit, to reduce disk I/O operation. For example, my Thinkpad T61 have 2GB memory, with 25% average of utilization. Swap space never been used. Otherside, there are so many disk cache I/O operation that slowdown system response. I'm using KDE with file preview enabled, imagine how hard the disk is spinning?

Actually, it was my friend Arianto C. Nugroho idea. He show me his Firefox cache mounted in memory. He said that Firefox doing disk I/O very much due to web caching, therefore slow down his laptop because of disk latency. Wow! Cool isn't it?!

TMPFS

Below is quotation from Wikipedia about TMPFS.
Tmpfs is a common name for a temporary file storage facility on many Unix-like operating systems. It is intended to appear as a mounted file system, but one which uses virtual memory instead of a persistent storage device — http://en.wikipedia.org/wiki/Tmpfs

How To Use It?

First, exit firefox and tar the profile folder. To determine which one is active profile, look at ~/.mozilla/firefox/profiles.ini, here is my sample:
[General]
StartWithLastProfile=1
[Profile0]
Name=default
IsRelative=1
Path=lcvjudt4.default
Ok, the profile is determined, then create tar archive of it.
$ cd ~/.mozilla/firefox/lcvjudt4.default
$ tar cvf ../profile.tar .
Create new profile directory for TMPFS mount point.
$ cd ..
$ mkdir ardhy.default
The ardhy.default is the new name of my profile. you can choose your own name.
Then modify the profiles.ini above. Change the lcvjudt4.default to the chosen name, in this example is ardhy.default

Next. Mount a TMPFS to the new profile directory. The simple way is to add a new entry in /etc/fstab.
firefox /home/ardhy/.mozilla/firefox/ardhy.default tmpfs uid=1000,size=100M 0 0
The size may vary depending of Your browsing behaviour. You can estimate the size by disk usage of current profile. My choose is 100MB.
$ du -hs lcvjudt4.default
The uid is your uid, or the uid of user that the profile belings to. You can check in the /etc/passwd file.
$ grep ardhy /etc/passwd
ardhy:x:1000:1000:Ardhy Wisnu Nugroho,,,:/home/ardhy:/bin/bash
As shown above, my uid is 1000. After the new fstab entry defined, you can mount it like below.
# mount firefox
And check the result.
ardhy@zangetsu:~/.mozilla/firefox$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             7.4G  3.3G  3.7G  48% /
tmpfs                 991M     0  991M   0% /lib/init/rw
udev                   10M  104K  9.9M   2% /dev
tmpfs                 991M     0  991M   0% /dev/shm
/dev/sda2             7.5G  4.3M  7.5G   1% /usr/local
/dev/sda3             135G  118G   17G  88% /home
firefox               100M   0   100M  0% /home/ardhy/.mozilla/firefox/ardhy.default
Then, You can untar the profile archive.
$ cd ~/.mozilla/firefox/ardhy.default
$ tar xf ../profile.tar .

Init Script Integration

Because TMPFS resides in memory and will be disappear if electricity is gone, we must create a script to wrap/unwrap the profile hen reboot/boot. Create the script and merge it to system init.
#!/bin/bash
mozilla_firefox_profile()
{                        
    work_dir="/home/ardhy/.mozilla/firefox";
    cd $work_dir;                           

    ff_profile_dir="ardhy.default";     
    cd $ff_profile_dir;

    case $1 in
        start)
            echo "Mbalekno profil Mozilla Firefox-nya Ardhy";
            tar xf ../profile.tar;
        ;;
        stop)
            echo "Ngringkesi profil Mozilla Firefox-nya Ardhy";
            tar cf ../profile.tar .;
        ;;
    esac
}

mozilla_firefox_profile $1;
Save above script to /etc/init.d/ringkesringkes.sh file, and chmod'ed +x.
# chmod +x /etc/init.d/ringkesringkes.sh
This init script is executed in runlevel 0 (halt), 1(singe user), 2(default), and 6(reboot).
# cd /etc/rc0.d
# ln -s ../init.d/ringkesringkes.sh K99ringkesringkes
# cd /etc/rc1.d
# ln -s ../init.d/ringkesringkes.sh K99ringkesringkes
# cd /etc/rc2.d
# ln -s ../init.d/ringkesringkes.sh S99ringkesringkes
# cd /etc/rc6.d
# ln -s ../init.d/ringkesringkes.sh K99ringkesringkes
The K-prefix indicates that init will call the script with "stop" as argument, so the script will wrap the firefox profile. The S-prefix is the opposite action.

More Hacks

Here is my /etc/fstab additional entries…
# tmpfs
tmp /tmp tmpfs size=500M 0 0
vartmp /var/tmp tmpfs mode=777,size=600M 0 0
varlock /var/lock tmpfs mode=777,size=1M 0 0
varrun /var/run tmpfs mode=755,size=1M 0 0
# custom
firefox /home/ardhy/.mozilla/firefox/ardhy.default tmpfs uid=1000,size=100M 0 0
And here is full /etc/init.d/ringkesringkes.sh file
#!/bin/bash

mozilla_firefox_profile()
{                        
    work_dir="/home/ardhy/.mozilla/firefox";
    cd $work_dir;                           

    #ff_profile_dir=`ls | grep default`;
    ff_profile_dir="ardhy.default";     
    cd $ff_profile_dir;

    case $1 in
        start)
            echo "Mbalekno profil Mozilla Firefox-nya Ardhy";
            tar xf ../profile.tar;
        ;;
        stop)
            echo "Ngringkesi profil Mozilla Firefox-nya Ardhy";
            tar cf ../profile.tar .;
        ;;
    esac
}

kde_cache()
{
    work_dir="/var/tmp";
    cd $work_dir;

    case $1 in
        start)
            echo "Mbalekno isine /var/tmp";
            tar xf ../vartmp.tar;
        ;;
        stop)
            echo "Ngringkesi isine /var/tmp";
            tar cf ../vartmp.tar .;
        ;;
    esac

}

kde_cache $1;
mozilla_firefox_profile $1;

The KDE .thumbnails Cache

KDE store its icon thumbnail cache in ~/.thumbnails and this is quite often accessed. Therefore I move it to /var/tmp/kdecache-ardhy/.thumbnails which is stored in memory.
# mv .thumbnails /var/tmp/kdecache-ardhy

# ln -s /var/tmp/kdecache-ardhy/.thumbnails
Make sure, you allocate enough space in the memory.

Note on KDE 4

Be noted that every time you choose new KDE desktop theme, it will create new cache data in /var/tmp/kdecache-ardhy/kpc, so make sure you have enough space in the memory.

Enjoy!