Prevent Firefox from fsyncing

Rationale#

Firefox fsyncs to disk every time you do just about anything. If you are writing to the hard drive, your Firefox can freeze for a long time.

To prove this is happening, do

strace -p `pidof firefox` -e trace=fsync,fdatasync

To get around this problem, you must redefine fsync for Firefox.

The fix#

Create a new library with a our own definition of fsync() inside. Or, for the technically adept explanation. We turn fsync() into a NOP function.

Steps#

One#

Create a new file. Let's name it fsync.c for simplicity.

$ gedit fsync.c

And create our function inside it.

int fsync (int fd)
{
    return 0;
}

Save and Quit.

Two#

Compile our library.

$ gcc -fPIC -g -c -Wall fsync.c

And link it

$ gcc -shared -Wl,-soname,fsync.so -o fsync.so fsync.o -lc

Three#

Install the library into a logically named directory. (ie: ~/.mozilla)

$ install fsync.so ~/.mozilla

Four#

Assign the library to the LD_PRELOAD shell variable.

$ export LD_PRELOAD=$HOME/.mozilla/fsync.so

Five#

Run firefox

$ firefox &

Now run the strace command on it again, and fsync() will be nowhere to be seen.

Putting it into Practice#

Option 1#

One way to set this the default on all Firefox Desktop icons in Ubuntu, we are going to create a script to be put in the /usr/local/bin directory.

$ gedit firefox-nofsync

And put the following inside:

#!/bin/sh
export LD_PRELOAD=/home/uname/.mozilla/fsync.so && firefox "$@"

Where uname is your username. Save and Quit, now install it.

$ sudo install firefox-nofsync /usr/local/bin

And now edit all Firefox menu/desktop items. (Right-Click on the menu, and select Edit Menus) and ensure that the Command box for firefox says this:

$ firefox-nofsync %u

And you are all done.

Option 2#

Instead of using a "workaround" script, we could just instead alter the main firefox script. This is useful if you have more than one users on your machine, or you don't want to change every single reference of firefox to something else.

$ sudo nano $(which firefox)

And put anywhere at the top of the file:

export LD_PRELOAD=/home/uname/.mozilla/fsync.so

Again, where uname is your username. Although, if you are opting for this option, then it may be better to put the library in a more globally accessible place whilst still being "out-the-way" from the rest of the system.

Also note, whenever you have a firefox upgrade, you will have to re-alter the firefox file to include the LD_PRELOAD line.

Sources#

http://ubuntuforums.org/showthread.php?p=6942172


Linux - Performance