Posted by Steve on Wed 27 Apr 2005 at 16:58
Previously we've discussed how to speed up compilation by running the job on multiple machines with "distcc". If you only have a single machine, and you are compiling the same program more than once a useful alternative is the compiler cache.
ccache is a cache for your compiler, every time you compile a source file it saves away the result. Then if you attempt to recompile it again you get the previous result instead of having to invoke the compiler.
ccache is a useful tool to know about if you're only running on a single machine and cannot use the distributed compiler distcc. To gain truly impressive speedups you can even combine them both!
The ccache speedup is impressive if you're working on a large project and only change a few files between compilation attempts. It is intelligent enough to recompile a file if you change the compiler options, and can show you statistics of how good it is.
The ccache package is included in all branches of Debian and can be installed and used very easily:
apt-get install ccache
Once the package is installed you will find that it installs a group of wrapper programs for your real compiler, these will be placed inside /usr/lib/ccache.
To use them all you have to do is setup your PATH variable so that the wrappers are used instead of the usual /usr/bin/gcc. Assuming you are running bash you need to run:
export PATH=/usr/lib/ccache:$PATH
Once that's done you can just run "gcc" or "g++" as normal, and gain the speed benefit.
For example I can compile a package (in this case jabber-1.4.2) and time it:
time make ... ... snip ... ... real 0m54.089s user 0m32.277s sys 0m7.486s
Now repeating that invocation will show a significant speedup, as the file should still be in the compiler cache's cache:
make clean time make real 0m16.622s user 0m10.928s sys 0m4.805s
There we can see a speedup of 54-16 = 38 seconds. Pretty good going.
We can use the cache statistics to see how many times we avoided re-compiling files that we'd already had cached:
skx@mystery:~/Programs/jabber-1.4.2$ ccache -s cache hit 133 cache miss 139 called for link 29 multiple source files 1 compile failed 1 not a C/C++ file 4 autoconf compile/link 4 no input file 1 files in cache 278 cache size 3.6 Mbytes max cache size 976.6 Mbytes
Here we see that 139 files were missed from the cache and had to be built, that's the first invocation. Then 133 files were found in the cache when it ran the second time.
(The disparity probably comes from the fact that I had to run "./configure" before the "make" command - which does some compilation to detect compiler flags, etc).
The output also shows the number of files currently sitting in the cache, inside ~/.ccache, and the maximum size the cache will grow.
To reset the statistics run:
ccache -z
To remove all cached files you can run the following command:
ccache -C
This removes all the files in the cache. A more brutal approach would be to remove all the files accociated with the cache, by running:
rm -rf ~/.ccache
More options can be specified either with different arguments, or with environmental variables and these are discussed in the manpage which you can read by entering the following into a terminal:
man ccache
For example this shows you how to run ccache together with distcc:
export CCACHE_PREFIX=distcc
This article can be found online at the Debian Administration website at the following bookmarkable URL:
This article is copyright 2005 Steve - please ask for permission to republish or translate.