Rebuild glibc optimized for your CPU in Debian Testing as a Local Package Version

article #755, updated 3311 days ago

I just rebuilt my glibc, optimizing the build for my particular CPU. I was amazed at how much more speed it brought me on this >5-year-old laptop, 2G RAM, dual-core 2 GHz Intel. Here’s what I did. If you’re following this, bear in mind that version numbers will have to be changed as development goes on!

  1. Get everything you need to build glibc. You may very well discover more packages to install if errors show up further down, depending on how you installed Debian to begin with.
apt-get build-dep glibc
  1. Create a folder for your build, and get the current source.
cd ~/Downloads; mkdir glibc-recompile; cd glibc-recompile; apt-get source glibc
  1. Edit a few files to set the optimization.

First change directory here: cd ~/Downloads/glibc-recompile/glibc-2.19/debian

Now edit the file named rules, and look for these two lines:

BUILD_CFLAGS = -O2 -g
HOST_CFLAGS = -pipe -O2 -g $(call xx,extra_cflags)

Change them as follows:

BUILD_CFLAGS = -O2 -march=native -mtune=native
HOST_CFLAGS = -pipe -O2 $(call xx,extra_cflags) -march=native -mtune=native

Now change to here: cd ~/Downloads/glibc-recompile/glibc-2.19/debian/sysdeps

You’ll now want to edit the file x32.mk, find this line:

i386_extra_cflags = -march=pentium4 -mtune=generic

and change it to:

i386_extra_cflags = -march=native -mtune=native

Then, if your CPU is Intel/AMD-compatible and your OS is 32-bit, you’ll want to edit i386.mk, find this:

i686_extra_cflags = -march=i686 -mtune=generic

and change it to this:

i686_extra_cflags = -march=native -mtune=native

and also find this:

xen_extra_cflags = -march=i686 -mtune=generic -mno-tls-direct-seg-refs

and change it to:

xen_extra_cflags = -march=native -mtune=native -mno-tls-direct-seg-refs

and if you’re Intel-compatible but your OS is 64-bit, edit amd64.mk, find this:

i386_extra_cflags = -march=pentium4 -mtune=generic

and change it to this:

i386_extra_cflags = -march=native -mtune=native

If you are running outside of the Intel/AMD world, you’ll want to find the correct file at this point for your CPU and make the same sort of setting, the idea is that “native” refers to whatever CPU on which the compiler finds itself running.

  1. Use Debian packaging tools to set a local package version. The last command in the string below will load the appropriate file in an editor:
cd ~/Downloads/glibc-recompile/glibc-2.19/debian ; dch

At this writing the original is “2.19-13”, and dch has already added this to the top:

glibc (2.19-13.1) UNRELEASED; urgency=medium

and I changed that top line to this:

glibc (2.19-13+local-native.1) UNRELEASED; urgency=medium

and then we save and close. dch then takes care of telling the other files that this local version is legit, and renames the package directory to match, which prepares us for the next step.

  1. Create a .tar.gz of the new source tree.
cd ~/Downloads/glibc-recompile ; tar czvf glibc_2.19-13+local.orig.tar.gz glibc-2.19-13+local
  1. Begin the build.
cd ~/Downloads/glibc-recompile/glibc-2.19-13+local ; debuild -us -uc

At this point you may discover additional packages which are needed. Install them, and begin #6 again. Otherwise it will generate several .deb files one level up, in ~/Downloads/glibc-recompile.

  1. Install the .deb files.

First traverse here:

cd ~/Downloads/glibc-recompile/

Then try to install them all:

sudo dpkg -i *.deb

You may get errors related to the presence of libc6 or libc6_2.19-13+local-native.1_i386.deb. If you do, install this one individually:

sudo dpkg -i libc6_2.19-13+local-native.1_i386.deb

and then do them all again:

sudo dpkg -i *.deb

Then reboot, and see!

Categories: