Firefox on Laptops


Firefox cache in ramdisk

When using a laptop in a mobile environment, it is most efficient if you're effectively using ACPI and power savings in order to get the best battery life, for a sample of this, see my V100 section.  Beyond that, one must start digging a little deeper, and since I do a fair amount of web browsing on the couch, Firefox is one area I knew I could make improvements.

With my ACPI setup, my laptop goes into laptop mode which you can read up on, but basically means it makes all attempts to reduce disk IO and power usage.  This means that your hard drives are not spinning most of the time unless accessed.  Firefox caches to the hard drive by default, so this means that it will regularly write and read from the drive cache, causing the drive to spin up.  The process can be fairly slow, taking 1s or more to read the data off of a spun-down drive, so we're going to save time and power by changing Firefox from a hard drive cache to a ram cache.

The main complexity here is that you really want your browser cache to be persistent (ie: next time you reboot then launch Firefox, the same images/etc are already cached).  So we have a script to do that, more on that in a minute.  

Ram Disk 

First off, you must have a ramdisk.  I'll let you google on that one, tmpfs is what to look for.  You'll want to make sure it's mounted at boot and is writable by your normal user account.  I am personally using FC6 and this is available by default so I use it.

Firefox configuration 

In Firefox, type about:config into the URL bar, then right-click inside the main window somewhere and select New->String and put in browser.cache.disk.parent_directory in order to create a new configuration entry.  (In case you have trouble, you might look here.)  In Fedora Core 6 I have a /dev/shm available as ramdisk so I put that path into the new string we just created.  Firefox will create a subdirectory called Cache within it the next time you restart and will use the ram disk for all cached files.  There are some things Firefox will still write to the hard drive, but none accessed as often as the cache files.  After you restart Firefox, you can double-check it's using the new ram disk cache area by looking in wherever you set up browser.cache.disk.parent_directory (/dev/shm/Cache in my example).

Cache script 

To make the cache persistent, we add a script. This script should be ran when you first login to X to copy the files from your hard drive Cache location to the ramdisk Cache location, and periodically from the ramdisk Cache to the hard drive Cache.  Doing this, the cache stays in sync and is saved between reboots.

For bootup, I created a file ~/.kde/Autostart/firefoxcache which simply runs ~/bin/ffcache boot (click here to get the file directly).  Your particular X environment will differ if you don't use KDE, so find the appropriate place to put a script which will run on login for your setup.  For periodic syncing back to hard drive, I have a script called ffcache which uses rsync to copy the files back.  To do that, I did crontab -e and put the following line in:

0,15,30,45 * * * * ~/bin/ffcache

Which means every 15 minutes, run ffcache.  In this example I've put it in the bin subdirectory of my homedir, which is where I put all of my personal scripts.  If there are global users who might use this script, you might put it in /usr/local/bin, that's up to you.

Caveats

One thing to note here is that the ffcache script only copies back to the hard drive if there is AC power; this is intentional.  We don't want to sync our files back constantly if we're trying to keep the disks spun down, as that would ruin the whole exercise!  So it's just something to keep in mind.  If you're going to shut the machine down without plugging it back into the AC, you'll probably want to run the ffcache script manually or in some script before shutting your laptop down.  My laptop generally stays on 24/7 so this isn't a particular issue for me.  I'll look into a nice way to run at shutdown and add the information here later. 

Another thing to keep in mind is that you'll want to verify that the location of the $acstate variable in the ffcache script is correct for your laptop and it's not, correct it.  If you do cat /proc/acpi/ac_adapter/ADP1/state and you get "state:  on-line" or "off-line", you're in good shape.  Otherwise you'll need to figure out where your state file is.  Start in /proc/acpi/ac_adapter as I've seen the ADP1 subdirectory differ on various laptops so it might be different on yours.

Benchmarks

None.  You want to do it, feel free.  I see that my disk stays spun down for 5-10 minutes at a time while using the laptop when before every new page would cause the disk to spin up.  So for me, I am sure it's helping.  When I'm at home, I'm using my laptop doing various things, none of them would be consistent from one night to the next so I would have to set up a stable environment for the benchmark to be ran under and I'd rather just use my laptop. :) 


(Last Modified 10/6/2007 23:27)


You are visitor number 12038







Click here to register.