Thursday, April 16, 2009

Setting up Prelink in Ubuntu Linux

Prelink ELF shared libraries and binaries to speed up startup time.

Technical Details

Prelink is a program that modifies ELF shared libraries and ELF dynamically linked binaries in such a way that the time needed for the dynamic linker to perform relocations at startup significantly decreases. Due to fewer relocations, the run-time memory consumption decreases as well (especially the number of unshareable pages). The prelinking information is only used at startup time if none of the dependent libraries have changed since prelinking; otherwise programs are relocated normally.

Prelink first collects ELF binaries to be prelinked and all the ELF shared libraries they depend on. Then it assigns a unique virtual address space slot to each library and relinks the shared library to that base address. When the dynamic linker attempts to load such a library, unless that virtual address space slot is already occupied, it maps the library into the given slot. After this is done, prelink, with the help of dynamic linker, resolves all relocations in the binary or library against its dependent libraries and stores the relocations into the ELF object.

It also stores a list of all dependent libraries together with their checksums into the binary or library. For binaries, it also computes a list conflicts (relocations that resolve differently in the binary’s symbol search scope than in the smaller search scope in which the dependent library was resolved) and stores it into a special ELF section.

At runtime, the dynamic linker first checks whether all dependent libraries were successfully mapped into their designated address space slots, and whether they have not changed since the prelinking was done. If all checks are successful, the dynamic linker just replays the list of conflicts (which is usually significantly shorter than total number of relocations) instead of relocating each library.

To enable Prelink in Ubuntu, you have to enable the "Universe" repository. Before using Prelink do google about Prelink and do your research before trying it out.

How to enable prelink

  1. Activate Ubuntu universe sources. Goto System>Administration>Software Sources. Check the second box "Community-maintained Open source software (universe)". Reload the softwares list after this (prompted automatically).
  2. Now, use apt-get or synaptic to install prelink. (Type sudo apt-get install prelink in gnome-terminal)
  3. Open /etc/default/prelink file with your favorite editor, as sudo/root, e.g. (sudo gedit /etc/default/prelink)
  4. Change the line PRELINKING=unknown to PRELINKING=yes
  5. Adjust the other options if you know what the heck you're doing. Defaults work well.
  6. To start the first prelink type in sudo /etc/cron.daily/prelink. This would be the longest one to execute. Took about 2 minutes on my machine. So, be patient. In the future, prelink performs a quick prelink (a less-than-1-minute procedure on most systems) daily, usually at midnight. Every 14 days, or whatever you changed it to be, a full prelink will run.
If you just did a major apt-get upgrade that changed systemwide libraries (i.e. libc6, glibc, major gnome/X libs, etc etc etc) and experience cryptic errors about libs, rerun step 6.

Stopping Prelinking

In case you do not want to use Prelink anymore, maybe due to some problem etc., follow these steps.
  1. Change step 4, above, from PRELINKING=yes to PRELINKING=unknown
  2. Rerun step 6.
Prelinking works well in i386 architectures. Some users have reported some problems in AMD64. You can try it though. If you experience any problems, then you can stop it anytime :)

4 comments: